HTTP Response Code

Discussion and support for general JUCE issues

HTTP Response Code

Postby aftermathew » Mon Oct 26, 2009 9:56 pm

I'm downloading files from a server and need to know if for some reason the download fails.

There are times when my download fails, but I still have data to read. For example, if I were to have an erroneous URL, I may download something like this

Code: Select all
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /~mathew/YourApp/YourAppsUpdateInstaller.exe was not found on this server.</p>
</body></html>


When I thought I had downloaded an executable. The web server is indeed responding with a 404, but I can't find any place in the URL WebInputStream (private) or InputStream that gives access to anything like this. What ends up happening is that juce doesn't pay attention to the 404 and just downloads the file as if the server responded with a 200.

Are there plans to add the HTTP response to the URL or WebInputStream classes? Is it already there and I just missed it?

Thanks
aftermathew
JUCE Geek
 
Posts: 20
Joined: Mon Oct 20, 2008 3:52 am
Location: Seattle, WA, USA

Postby jules » Mon Oct 26, 2009 11:02 pm

good request - I think the classes have that information internally, it's just a bit tricky to return it, as the URL class just returns an opaque InputStream object.
User avatar
jules
Fearless Leader
 
Posts: 17373
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Postby aftermathew » Mon Oct 26, 2009 11:15 pm

I couldn't see the response code anywhere in the WebInputStream class in juce/src/juce_core/io/network/juce_URL.cpp.

I skimmed the mac code and didn't notice response codes being checked in there. I didn't look at the windows and linux code.

It's nice the redirects are auto-handled (i think they are anyway...). It would be really nice however if a true failure of some kind (4xx, 5xx, etc) were check-able, even with some sort of boolean method or something like that.

For now, in lieu of time, I think I need to switch to curl for this particular issue, but if you get this working please ping me :-)
aftermathew
JUCE Geek
 
Posts: 20
Joined: Mon Oct 20, 2008 3:52 am
Location: Seattle, WA, USA

Postby jules » Mon Oct 26, 2009 11:24 pm

Personally, I'd just do a sanity-check on the data you get back - a quick search for <html> in the first few bytes will tell you if you've been sent a webpage instead of a binary.
User avatar
jules
Fearless Leader
 
Posts: 17373
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Postby aftermathew » Tue Oct 27, 2009 4:53 am

Thats true, and it works out ok for a simple case, but I can't rely on it.

What if they replace their 404 with flash page?
What if it's replaced with a jpeg?
What if...?

I can control the binary I should get, but I have no control over the web server and what it will do if the file i need isn't where I think it is. I need the code to be as robust as possible, and hoping that I can think of all error conditions before they occur is not as good as checking my response codes.

Thanks for the idea though.
aftermathew
JUCE Geek
 
Posts: 20
Joined: Mon Oct 20, 2008 3:52 am
Location: Seattle, WA, USA

Postby aftermathew » Tue Oct 27, 2009 6:03 am

I forgot to mention that we already have some curl code compiled into our program from back before we started using juce, so it's not that big a deal for me to use that. I just wanted to keep this section of the program entirely in juce if i could.
aftermathew
JUCE Geek
 
Posts: 20
Joined: Mon Oct 20, 2008 3:52 am
Location: Seattle, WA, USA

Postby vishvesh » Tue Oct 27, 2009 8:00 am

aftermathew wrote:Thats true, and it works out ok for a simple case, but I can't rely on it.

What if they replace their 404 with flash page?
What if it's replaced with a jpeg?
What if...?

I can control the binary I should get, but I have no control over the web server and what it will do if the file i need isn't where I think it is. I need the code to be as robust as possible, and hoping that I can think of all error conditions before they occur is not as good as checking my response codes.

Thanks for the idea though.


Hi Mathew,
I had a curl download code, which didn't detect missing files on the server. It ended up downloading html files of this format on macintosh.
Code: Select all
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /~mathew/YourApp/YourAppsUpdateInstaller.exe was not found on this server.</p>
</body></html>

but on Windows it didn't download anything.


I ended up doing checks for files sizes, Since I knew the file size in advance. Did your curl code work?
Best Regards,
Vishvesh
User avatar
vishvesh
JUCE UberWeenie
 
Posts: 504
Joined: Fri Aug 01, 2008 10:16 am
Location: Bangalore, India.

Postby jules » Tue Oct 27, 2009 10:15 am

What if they replace their 404 with flash page?
What if it's replaced with a jpeg?
What if...?


If this is someone else's webserver and you have no idea what it'll give you, then shouldn't you be doing more security checking on your results anyway? Surely you need to check that it's a valid and uncorrupted binary, regardless of what tool you used to download it?
User avatar
jules
Fearless Leader
 
Posts: 17373
Joined: Mon Sep 06, 2004 9:03 am
Location: London, UK

Postby aftermathew » Tue Oct 27, 2009 5:35 pm

vishvesh - My curl code is almost working. I'll post it when I get that far.

jules - it's more complicated than that - my client owns both the web server and the program being downloaded, but has different teams working on both. Upgrading either because the other changed in a brittle way is expensive to the client and I don't want to make assumptions... yada yada. I can trust that the file is what I think it should be in most circumstances, but if the server fails for some reason I would like to be able to read the response, and handle it properly.

Really this discussion is about HTTP response codes. There are hacks I could hypothetically do to avoid reading the response codes, and things that I maybe should be doing as well as reading response codes, but that doesn't change the fact that having the codes would be useful.
aftermathew
JUCE Geek
 
Posts: 20
Joined: Mon Oct 20, 2008 3:52 am
Location: Seattle, WA, USA

Postby atom » Tue Oct 27, 2009 5:58 pm

i ahve a cURL (juce based) curl class i posted it in the useful tools and components, the latest is always in Edo svn http://edoapp.googlecode.com/svn/trunk/ ... ries/curl/
User avatar
atom
JUCE UberWeenie
 
Posts: 1130
Joined: Thu Feb 15, 2007 11:36 am

Postby aftermathew » Tue Oct 27, 2009 7:31 pm

oh man atom, that is much nicer than what I just wrote :-P
aftermathew
JUCE Geek
 
Posts: 20
Joined: Mon Oct 20, 2008 3:52 am
Location: Seattle, WA, USA

Postby vishvesh » Fri Oct 30, 2009 1:31 am

Thanks for sharing the code atom, it's much better than my code. :)
User avatar
vishvesh
JUCE UberWeenie
 
Posts: 504
Joined: Fri Aug 01, 2008 10:16 am
Location: Bangalore, India.

Re: HTTP Response Code

Postby P4tr3ck » Wed Mar 14, 2012 9:44 pm

I am resurrecting a very old thread here. But with all those fancy REST apis out there, any second thoughts about adding HTTP Response Code?

Two examples of REST api's utlizing status codes:
- Dropbox: https://www.dropbox.com/developers/reference/api under the heading "Error handling".
- Twitter: https://dev.twitter.com/docs/error-codes-responses
P4tr3ck
JUCE UberWeenie
 
Posts: 136
Joined: Tue Mar 29, 2011 9:28 pm


Return to General JUCE discussion

Who is online

Users browsing this forum: No registered users and 2 guests