【发布时间】:2015-03-26 17:11:38
【问题描述】:
我在我的 LINUX 机器上使用 C 语言的 TCP 套接字编程制作了一个非常简单的 Web 服务器。我从本地机器的浏览器(chrome 和 mozilla)向它发送 HTTP GET 请求。
这个问题是当我没有在响应中设置 header
Transfer-Encoding: chunked 时,浏览器成功显示网页。
但是当我保留这个标题时,浏览器没有响应,它说 NO DATA IS AVAILABLE。
编辑:在我添加@RomanK 指出的块大小(446 字节)之后,它现在适用于 firefox。 但是 chrome 变得没有响应。
这里是代码
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
【问题讨论】:
-
完成。请看@GoBusto。
-
请注意,我将文件作为 446 字节的单个块发送。我不知道如何将文件作为多个块发送。
标签: c http httpresponse chunked-encoding http-chunked