简体   繁体   中英

Transfer-Encoding: chunked— Browser does not respond

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM