【发布时间】:2014-09-22 20:13:47
【问题描述】:
我正在为嵌入式目标上的 http 服务器编写代码。我想解析我收到的原始 HTTP 数据包的内容长度。我的问题源于我不知道内容长度是多少字节。例如,
//内容长度为4个字符
内容长度:1000
//内容长度为2个字符
内容长度:20
我的代码是:
(buf is a char array with the contents of the http data, pLength is a char *)
pLength = strstr(buf, lenTok);
//advance pointer past "Content-Length: " to point to data
if (pLength != NULL)
pLength = (pLength + 16);
int length = 0;
for (int i = 0; i < /*length of Content-Length:*/; i++)
{
length = length * 10;
length = length + (pLength[i] - '0');
}
我知道我的代码有效,因为现在我已经在循环迭代器条件中放置了一个硬编码值。我只需要能够在运行时确定它。
编辑:在找到 \r 之前继续阅读字符是否安全?
【问题讨论】:
-
读取rfc2616,header格式固定(但注意可以发送恶意请求)。
标签: c http http-headers