HTTP Response before Request

HttpNetworkingTcpComet

Http Problem Overview


My question might sound stupid, but I just wanted to be sure:

  • Is it possible to send an HTTP response before having the request for that resource?

Say for example you have an HTML page index.html that only shows a picture called img.jpg. Now, if your server knows that a visitor will request the HTML file and then the jpg image every time:

Would it be possible for the server to send the image just after the HTML file to save time?

I know that HTTP is a synchronous protocol, so in theory it should not work, but I just wanted someone to confirm it (or not).

Http Solutions


Solution 1 - Http

A recent post by Jacques Mattheij, referencing your very question, claims that although HTTP was designed as a synchronous protocol, the implementation was not. In practise the browser (he doesn't specify which exactly) accepts answers to requests have not been sent yet.

On the other hand, if you are looking to something less hacky, you could have a look at :

  • push techniques that allows the server to send content to the browser. The modern implementation that replace long-polling/Comet "hacks" are the websockets. You may want to have a look at socket.io also.
  • Alternatively you may want to have a look at client-side routing. Some implementations combine this with caching techniques (like in derby.js I believe).

Solution 2 - Http

If someone requests /index.html and you send two responses (one for /index.html and the other for /img.jpg), how do you know the recipient will get the two responses and know what to do with them before the second request goes in?

The problem is not really with the sending. The problem is with the receiver possibly getting unexpected data.

One other issue is that you're denying the client the ability to use HTTP caching tools like If-Modified-Since and If-None-Match (i.e. the client might not want /img.jpg to be sent because it already has a cached copy).

That said, you can approximate the server-push benefits by using Comet techniques. But that is much more involved than simply anticipating incoming HTTP requests.

Solution 3 - Http

You'll get a better result by caching resources effectively, i.e. setting proper cache headers and configuring your web server for caching. You can also inline images using base 64 encoding, if that's a specific concern.

You can also look at long polling javascript solutions.

Solution 4 - Http

You're looking for server push: it isn't available in HTTP. Protocols like SPDY have it, but you're out of luck if you're restricted to HTTP.

Solution 5 - Http

I don't think it is possible to mix .html and image in the same HTTP response. As for sending image data 'immediately', right after the first request - there is a concept of 'static resources' which could be of help (but it will require client to create a new reqest for a specific resource).

There are couple of interesting things mentioned in the the article.

Solution 6 - Http

No it is not possible.

The first line of the request holds the resource being requested so you wouldn't know what to respond with unless you examined the bytes (at least one line's worth) of the request first.

Solution 7 - Http

No. HTTP is defined as a request/response protocol. One request: one response. Anything else is not HTTP, it is something else, and you would have to specify it properly and implement it completely at both ends.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionm_vdbeekView Question on Stackoverflow
Solution 1 - HttpnhaView Answer on Stackoverflow
Solution 2 - HttpRaymond HettingerView Answer on Stackoverflow
Solution 3 - HttpTim M.View Answer on Stackoverflow
Solution 4 - HttpFemiView Answer on Stackoverflow
Solution 5 - HttpMaciek TalaskaView Answer on Stackoverflow
Solution 6 - HttpJason FuerstenbergView Answer on Stackoverflow
Solution 7 - Httpuser207421View Answer on Stackoverflow