0

I have made a very simple web server on my LINUX machine using TCP socket programming in C language.I am sending it a HTTP GET request from a browser(both chrome and mozilla ) from the local machine. This problem is that when i do not set the header
Transfer-Encoding: chunked in the response , the browser successfully displays the webpage. But when i keep this header , the browser does not respond, it says NO DATA IS AVAILABLE.

EDIT: It works for firefox now after i added the chunk size (446 bytes) as pointed by @RomanK. But chrome becomes unresponsive.

Here is the code

 responseIndex = add(response,"HTTP/1.1 200 OK",responseIndex);

 responseIndex = add(response,"Transfer-Encoding: chunked",responseIndex);


 responseIndex = add(response,"Content-Type: text/html",responseIndex);

response[responseIndex++]='\r';

response[responseIndex++]='\n';

updateIndex = add(response,"446",updateIndex);

responseIndex = add(response,filebuffer,responseIndex);

response[responseIndex++]='\0';   


send(clntSock, response, strlen(response), 0) ;

close(clntSock);
exit(0);

Here, add is a function to append the second argument to response and then append "/r/n".

response is a string.

responseIndex is just an int to keep track of the current length of response.

filebuffer is a string which contains all the text of the html file to be sent.

Response :

              HTTP/1.1 200 OK
              Transfer-Encoding: chunked
              Content-Type: text/html

              446 (or 1EB)
              <html>
              BODY
             </html>

The error code given by chrome is : ERR_INVALID_CHUNKED_ENCODING

jps
  • 173
  • 1
  • 13

2 Answers2

1

Content-Length and chunked transfer encoding are mutually exclusive.

You should omit Content-Length and rather add the chunk size at the start of each chunk as per the Wikipedia article.

Or, in other words, you need to output the chunk size in hexadecimal before this line

responseIndex = add(response,filebuffer,responseIndex);

EDIT : Note that the you need to provide the size of the chunk only, not of the entire HTTP response. In your case it should be the size of the HTML body only; for example it looks like your sample body would be 30 or 31 in size in hex (not sure about the whitespace).

So, 3 points: a) Use hex b) Use lowercase c) Use size of the chunk (in your case, the body, as you have a single chunk). Do not include the size of the HTTP meta-data.

It's also a bit questionable that you use chunks in the first place; they should be used only in cases where you do not know the response size when you start generating the response. Here you know the response size at the start and can use Content-Length without Transfer-Encoding: chunked.

RomanK
  • 1,258
  • 7
  • 18
  • Thanks a lot. Although it works for firefox now, chrome shows a weird behaviour.Chrome just hangs and the page becomes unresponsive. I have added 446(which is the length of my page) just before the body. I have also tried the hexadecimal version of 446 for that matter – jps Mar 26 '15 at 19:48
  • Can you edit your question and provide your new HTTP response after the fix? – RomanK Mar 26 '15 at 19:52
  • Thanks for the edit - amended my answer accordingly. – RomanK Mar 26 '15 at 20:26
  • I was used chunked encoding as i wanted to learn a bit about all headers in http response. I am still not able to make it work for chrome after making various changes. :( – jps Mar 28 '15 at 06:03
  • Not much else I can help with - you might want to go carefully over my response here and the link article. – RomanK Mar 28 '15 at 06:30
-1

The point of chunked transfer is (sorry for tautology) to send data in chunks. The browser doesn't know how many chunks to expect, so you need to tell it that some chunk is the last one. The protocol specifies that the last chunk should be of size 0:

          HTTP/1.1 200 OK
          Transfer-Encoding: chunked
          Content-Type: text/html

          446\r\n
          Precisely 446 bytes of data
          0\r\n
user58697
  • 7,808
  • 1
  • 14
  • 28
  • I have added the last line (0\r\n) to the response. Still it does not seem to be working for chrome . Firefox also seems to be misinterpreting the last 0 and shows it on the screen along with the html body. Would it be appropriate if put the whole code on pastebin for you.? – jps Mar 27 '15 at 18:40