我在我的 LINUX 机器上使用 C 语言的 TCP 套接字编程制作了一个非常简单的 Web 服务器。我从本地机器的浏览器(chrome 和 mozilla)向它发送 HTTP GET 请求。这个问题是当我没有在响应中设置标题
Transfer-Encoding: chunked时,浏览器成功显示网页。但是当我保留这个标题时,浏览器没有响应,它说NO DATA IS AVAILABLE。
编辑:在我添加@RomanK指出的块大小(446字节)后,它现在适用于firefox。但是铬变得没有反应。
这是代码
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);
这里,add是将第二个参数附加到响应然后附加“/r/n”的函数。
响应是一个字符串。
responseIndex只是一个用于跟踪当前响应长度的 int。
filebuffer是一个字符串,其中包含要发送的 html 文件的所有文本。
回复 :
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/html
446 (or 1EB)
<html>
BODY
</html>
chrome给出的错误代码是:ERR_INVALID_CHUNKED_ENCODING