Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

server socket receives 2 http requests when I send from chrome and receives one when I send from firefox

Tags:

http

sockets

I wrote a simple server using socket API in C under linux which listens at port 80 on localhost. Now when I send a request from the browser google chrome to the program it receives 2 requests while it receives only one when I send from firefox.

The URL I typed in the browser was: http://localhost/xyz.html

OUTPUT WHEN I TYPE URL IN CHROME

root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL# ./DCMTOL_RUN   Inside HTTP server Handler  Inside HTTP request Handler   **Detected request: clientsocket_fd = 6 clientportnumber = 38027**  GET /xyz.html HTTP/1.1  Host: localhost  Connection: keep-alive  Cache-Control: max-age=0  Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5  User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10  Accept-Encoding: gzip,deflate,sdch  Accept-Language: en-US,en;q=0.8  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3   Inside HTTP request Handler  **Detected request: clientsocket_fd = 7 clientportnumber = 38029**  ^C  root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL#  

the second request does not send any data so my code waits at the read call and so I have to terminate it '^C'.

OUTPUT WHEN I TYPE URL IN FIREFOX

root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL# ./DCMTOL_RUN  Inside HTTP server Handler Inside HTTP request Handler  **Detected request: clientsocket_fd = 6 clientportnumber = 45567**  GET /xyz.html HTTP/1.1  Host: localhost  User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  Accept-Language: en-us,en;q=0.5  Accept-Encoding: gzip,deflate  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7  Keep-Alive: 115  Connection: keep-alive   ^C  root@anirudh-Aspire-5920:/home/anirudh/workspace/DCMTOL#  

Question: How can chrome browser send 2 requests (one being empty) when I typed the URL only once. As you can see above I detected 2 requests. I tried to do netstat in the case of sending URL from chrome and I found that both of the request were sent by the browser only. and as u can see above when I send the URL from firefox only 1 request is received.

Here is the output of net stat when I send request from chrome

Active Internet connections (w/o servers)  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name  tcp        0      0 117.195.110.186:48701   74.125.77.102:80        TIME_WAIT   -  tcp        0      0 117.195.110.186:48700   74.125.77.102:80        ESTABLISHED 5699/google-chrome  tcp        0      0 117.195.110.186:55815   209.85.175.138:80       ESTABLISHED 5699/google-chrome  tcp        0      0 127.0.0.1:80            127.0.0.1:38029         ESTABLISHED -  tcp        0      0 127.0.0.1:38029         127.0.0.1:80            ESTABLISHED 5699/google-chrome  tcp        0      0 127.0.0.1:38027         127.0.0.1:80            ESTABLISHED 5699/google-chrome  tcp        0      0 127.0.0.1:80            127.0.0.1:38027         ESTABLISHED -  tcp        0      0 117.195.110.186:35402   74.125.153.125:5222     ESTABLISHED 4430/pidgin 

thanks in advance :)

like image 375
Durin Avatar asked Jan 21 '11 17:01

Durin


People also ask

Does an HTTP server use sockets?

An HTTP server is a listening socket that rapidly accepts incoming requests and opens new sockets to handle those requests. The connection socket returns an HTTP response, and closes the connection.

How do you define HTTP request?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.


2 Answers

I had a similar issue with my node server. It is due to the following bug in Chrome. In summary, Chrome is sending a request for a favicon on every request. As, is likely, you aren't sending a favicon back, it requests one after every legitimate request.

Firefox, and most other browsers, also send out a request for a favicon when they first connect, but cache the result i.e. if there isn't a favicon returned first time, they don't keep trying - which is why you're only seeing a single request from Firefox. It seems Chrome is unfortunately a little too persistent with its favicon requestiness.

like image 98
Matt Avatar answered Sep 23 '22 16:09

Matt


I am currently writing small async web server on Mono/.NET 4.0 and noticed the same thing. Chrome opens two TCP connection, but only one is used for communication. There is no data send using that socket. Even after you stop loading webpage from browser Chrome still keeps connection alive for quite some time.

I must agree with @RomanK, as it's probably for optimizations or it's a bug, but it's not for favicon as there is not data transfered throw that connection.

like image 34
davidlt Avatar answered Sep 24 '22 16:09

davidlt