我正在使用C中的旧版mongoose(开放源代码Web服务器),它没有提供对请求负载的本地访问。为了支持POST和PUT请求,我手动修改了它:在猫鼬读取头文件之后,我检查是否设置了Content-Length
,如果是,我再从套接字中读取Content-Lenght
个字符。在Mongoose WebServer中检索请求正文时出错
findCL = strstr(conn->buf, "Content-Length:");
if (findCL)
{
// skip "Content-Length:" string
findCL += 15 * sizeof(char);
findCLEnd = (char*)strchr(findCL, delimiter);
sizeLen = findCLEnd - findCL;
strncpy(CLSize, findCL, sizeLen);
CLSize[sizeLen] = '\0';
size = strtoll(CLSize, NULL, 10);
if (size > 0)
{
conn->content_len = read_request(NULL, conn->client.sock, conn->ssl,
conn->buf, conn->buf_size, &conn->data_len);
conn->content_len = size;
perror("recv");
body = (char*)malloc(sizeof(char) * (size + 1));
strncpy(body, conn->buf + conn->request_len, size);
body[size] = '\0';
}
}
到目前为止,即使代码不是那么美丽,它也不会做肮脏的工作。问题是,在调试时代码工作正常,但是当代码作为一个简单的后台进程运行时,主体没有被正确解析:有时候生成的主体被截断,有时候它只是空的。看来问题是由客户端的快速查询造成的。
从哪里发送?无法在Chrome或Firefox中重现...都使用'$ .ajax'正确添加Content-Length。创建一个再现丢失标题的演示 – charlietfl
打开一个只有jQuery和AJAX标记的问题很奇怪,然后看到C代码但没有Javascript。 –
@JaredFarrish,我也标记为“mongoose”。 – phagio