title: How to format the HTTP response
body: <p>I have written a socket program in C. I used this program as a chat server/client using TCP. I tried to change the chat server to use it as a HTTP server by changing the port to 80. I referred to the HTTP request/response format in <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session</a> , and made my program to reply with sample response. I tried the url</p>
  <pre><code>http://127.0.0.1/ 
  </code></pre>
  <p>in browser. My program read the request and replied with response. At first, I used google-chrome. Chrome didn't load the page correctly until i added the correct data length in Content-Length header. After setting content length header, chrome loaded the page correctly. But, firefox doesn't load the page. Firefox doesn't showed any errors, but still loading the page like it is still waiting for some data. Only When i stop the server or close the socket, complete page is loaded. I tried to follow rfc2616 <a href="https://www.rfc-editor.org/rfc/rfc2616" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc2616</a> , and made the response exactly , but the still the results are same.</p>
  <p>Request:</p>
  <blockquote>
  <p>GET / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nUser-Agent: Mozilla/5.0
  (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101
  Firefox/33.0\r\nAccept:
  text/html,application/xhtml+xml,application/xml;q=0.9,<em>/</em>;q=0.8\r\nAccept-Language:
  en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection:
  keep-alive\r\n\r\n</p>
  </blockquote>
  <p>For the above request, my program write to the socket with following response &amp; content.</p>
  <p>Response:</p>
  <blockquote>
  <p>HTTP/1.1 200 OK\r\nCache-Control : no-cache, private\r\nContent-Length
  : 107\r\nDate : Mon, 24 Nov 2014 10:21:21 GMT\r\n\r\n</p>
  </blockquote>
  <p>Content:</p>
  <pre><code>&lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;TIME : 1416824843 &lt;br&gt;DATE: Mon Nov 24 15:57:23 2014 &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  <p>This response is loading in Chrome, but not in firefox. Chrome is loading the page instantly whereas firefox is waiting for data. Note that the data length 107 is specified in the header. I donot have any addons enabled in firefox. My firefox version is in the request. Chrome version: Version 38.0.2125.111 (64-bit).</p>
  <p>Code:</p>
  <pre><code>void *socket_read(void *args)
  {
      int socket,*s,length;
      char buf[1024];
      s=(int *)args;
      socket=*s;
      while(1){
          buf[0]='\0';
          length=read(socket,buf,1024);
          if(length==0)
              break;
          else if(length==-1){
              perror(&quot;Read&quot;);
              return;
          }
          else{
              printf(&quot;Request: %s\n&quot;,buf);
              send_response(socket);
          }       
      }
      printf(&quot;End of read thread [%d]\n&quot;,socket);
  }
  int start_accept(int port)
  {
      int socket,csocket;
      pthread_t thread;
      struct sockaddr_in client;
      socklen_t addrlen=sizeof(client);
      pthread_attr_t attr;
      socket=create_socket(port);
      while(1){
          if((csocket=accept(socket,(struct sockaddr *)&amp;client,&amp;addrlen))==-1)
          {   
              perror(&quot;Accept&quot;);
              break;
          }
          pthread_attr_init(&amp;attr);
          if(0!=pthread_create(&amp;thread,&amp;attr,socket_read,&amp;csocket))
          {
              perror(&quot;Read thread&quot;);
              return;
          }
          usleep(10000);
      }
  }
  void send_response(int socket)
  {
      char buf1[1024];
      int content_length;
      char buf2[1024]=&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;TIME : 1416824843 &lt;br&gt;DATE: Mon Nov 24 15:57:23 2014 &lt;/body&gt;&lt;/html&gt;&quot;;
      content_length=strlen(buf2);
      sprintf(buf1,&quot;HTTP/1.1 200 OK\r\nCache-Control : no-cache, private\r\nContent-Length : %d\r\nDate : Mon, 24 Nov 2014 12:03:43 GMT\r\n\r\n&quot;,content_length);
      printf(&quot;Written: %d \n&quot;,write(socket,buf1,strlen(buf1)));
      fflush(stdout);
      printf(&quot;Written: %d \n&quot;,write(socket,buf2,content_length));
      fflush(stdout);
  }
  </code></pre>