Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Response before Request

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).

like image 323
m_vdbeek Avatar asked Sep 06 '12 02:09

m_vdbeek


People also ask

What are the 3 parts of an HTTP response?

HTTP Response broadly has 3 main components: Status Line. Headers. Body (Optional)

What is the first line of an HTTP response called?

The status line is the first line in the response message. It consists of three items: The HTTP version number, showing the HTTP specification to which the server has tried to make the message comply. A status code, which is a three-digit number indicating the result of the request.

What is the response phase of HTTP?

After receiving and interpreting a request message, a server responds with an HTTP response message: A Status-line. Zero or more header (General|Response|Entity) fields followed by CRLF. An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields.

What is the difference between HTTP request and response?

HTTP messages are how data is exchanged between a server and a client. There are two types of messages: requests sent by the client to trigger an action on the server, and responses, the answer from the server.


1 Answers

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).
like image 168
nha Avatar answered Sep 30 '22 15:09

nha