HTTP Response Code


#1

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

<!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


#2

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.


#3

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 :slight_smile:


#4

Personally, I’d just do a sanity-check on the data you get back - a quick search for in the first few bytes will tell you if you’ve been sent a webpage instead of a binary.


#5

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.


#6

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.


#7

[quote=“aftermathew”]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.[/quote]

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.

<!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?


#8

[quote]
What if they replace their 404 with flash page?
What if it’s replaced with a jpeg?
What if…? [/quote]

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?


#9

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.


#10

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/src/Libraries/curl/


#11

oh man atom, that is much nicer than what I just wrote :stuck_out_tongue:


#12

Thanks for sharing the code atom, it’s much better than my code. :slight_smile:


#13

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:


#14

Re-resurrecting this thread. I'm looking at interacting with a REST API and will need response codes coming back from the server. Any chance we can have these returned in the URL class? 


#15

I am confused… The createInputStream method of the URL class already has a parameter to grab the http response code. You can also query any other response header with this method. Am I missing something?


#16

I stand corrected, I missed that! Apologies for the confusion. 

Mods - feel free to delete my erronous post!