HTTP response before request

My question may seem silly, but I just wanted to be sure:

  • Is it possible to send an HTTP response before requesting this resource?

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

Is it possible for the server to send the image immediately after the HTML file to save time?

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

+49
networking comet tcp
Sep 06
source share
7 answers

If someone asks for /index.html and you send two responses (one for /index.html and the other for /img.jpg), how do you know that the recipient will receive two answers and know what to do with them before how is the second request going?

The problem is not related to shipping. The problem is that the receiver may receive unexpected data.

Another problem is that you are denying the client the use of HTTP-caching tools such as If-Modified-Since and If-None-Match (that is, the client may not want to send /img.jpg since it already has a cached copy )

This way you can get closer to the benefits of a push server using the Comet methods. But this is much more than just anticipating incoming HTTP requests.

+15
Sep 06
source

A recent publication by Jacques Mattheij , referring to your question, claims that although HTTP was designed as a synchronous protocol, there was no implementation. In practice, the browser (it does not specify what exactly) accepts responses to requests that have not yet been sent.

On the other hand, if you are looking for something less hacked, you can take a look at:

  • push, which allows the server to send content to the browser. The modern implementation that replaces the long polls / comets “hacks” is websites . You might want to look at socket.io .
  • Alternatively, you can see client routing . Some implementations combine this with caching methods (for example, in derby.js I believe).
+22
Jun 09 '14 at 15:08
source

The effect of resource caching will be better, i.e. set the correct cache headers and configure your web server for caching. You can also embed images using base 64 encoding if this is of particular concern.

You can also watch long polls of javascript solutions.

+2
Sep 06
source

You are looking for server push : it is not available in HTTP. Protocols like SPDY exist, but you're out of luck if you're limited to HTTP.

+1
Sep 06
source

I don’t think it is possible to mix .html and image in the same HTTP response. As for sending image data “right away”, right after the first request - there is a concept of “static resources” that can help (but this will require a client to create a new request for a specific resource).

The article has some interesting things .

+1
Sep 06
source

No, It is Immpossible.

The first line of the request contains the requested resource, so you do not know what to answer if you did not first consider the bytes (at least one line) of the request.

-one
Sep 06
source

No. HTTP is defined as a request / response protocol. One request: one answer. Everything else is not HTTP, it is something else, and you will need to specify it correctly and fully implement at both ends.

-2
Sep 06
source



All Articles