简体   繁体   English

C解析HTTP Chunked传输编码响应

[英]C parsing HTTP Chunked transfer encoding response

I am developing a client that needs to parse Chunked-type HTTP transfers. 我正在开发一个需要解析Chunked类型HTTP传输的客户端。 I've beat my head against the wall trying to figure out the error with the following, and would appreciate it if someone might be able to catch my error a bit quicker. 我试着用下面的方法弄清楚错误,并且如果有人能够更快地捕捉到我的错误,我会很感激。 To sum up the issue: it seems as though, the client does not receive ALL of the chunk, thereby screwing up the rest of the process. 总结一下这个问题:似乎客户端没有收到所有的块,从而搞砸了剩下的进程。 Thanks in advance! 提前致谢!

 while(cflag){
    pfile_chunk = malloc(CHUNK_SIZE+1);
    memset(pfile_chunk, 0, CHUNK_SIZE);
    cPtr = pfile_chunk;
    cPtr2 = NULL;
    k=0;
    while(*(cPtr-1) != '\n'){
        k++;
        recv(sock, cPtr, 1, 0);
        cPtr = pfile_chunk+k;
    }
    cPtr2 = strchr(pfile_chunk, '\r');
    *cPtr2 = '\0';
    sscanf(pfile_chunk, "%x", &l);
    if(l == 0)
        break;
    printf("\nServer wants to deliver %ld bytes.\n", l);
    pfile_chunk = realloc(pfile_chunk, l+1);
    memset(pfile_chunk, 0, l);
    recv(sock, pfile_chunk, l, 0);
    fputs(pfile_chunk, f);
    printf("GOT THIS, SIZE %ld:\n%s\n", strlen(pfile_chunk), pfile_chunk);
    //get next \r\n bytes.
    recv(sock, NULL, 2, 0);
}

At the very least, you should check the return value of recv to see if you are getting the number of bytes you are expecting to get. 至少,你应该检查recv的返回值,看看你是否得到了你期望获得的字节数。

A short read is definitely possible on the network, since the system call will return whatever is available in the socket receive buffer at the time you make the call. 网络上肯定可以进行简短的读取,因为系统调用将在您进行呼叫时返回套接字接收缓冲区中可用的任何内容。

Implement a loop until you have read in your entire chunk, or pass the MSG_WAITALL flag to recv in the last parameter. 实现循环,直到读入整个块,或者将MSG_WAITALL标志传递给最后一个参数中的recv However, you still need to check for an error from recv . 但是,您仍需要检查recv的错误。

ssize_t r = recv(sock, pfile_chunk, l, MSG_WAITALL);
if (r < l) {
    /* check for errors ... */
} else {
    /* got the data */
}

It looks as though your very first dereference for the check in your while loop will access before the beginning of your array, which is likely not to be desired behavior. 看起来您的while循环中的检查的第一个取消引用将在数组开始之前访问,这可能不是期望的行为。 Hopefully, that memory location usually won't contain \\n . 希望,该内存位置通常不会包含\\n That could mess up your read . 那会弄乱你的read I expect it probably contains some information to do with your malloc , which is unlikely to be \\n , so you might never see a problem from that. 我希望它可能包含一些与你的malloc有关的信息,这不太可能是\\n ,因此你可能永远不会发现问题。

Also, hopefully you can trust the other end of the socket not to send more than CHUNK_SIZE+1 before they give you a \\n . 另外,希望你可以相信套接字的另一端在它们给你一个\\n之前不要发送超过CHUNK_SIZE+1 Otherwise, it could seg-fault out. 否则,它可能会出错。 Normally, though, I would expect a sender to just send 10 or fewer ASCII numeric characters and a CRLF for a chunk header anyways, but they could theoretically send a bunch of long chunk extension header fields with it. 但是,通常情况下,我希望发送者只发送10个或更少的ASCII数字字符和一个块头的CRLF,但理论上他们可以发送一堆长的块扩展头字段。

Apart from that, there's just the more important issue already found by user315052 that you should either tell the recv method to wait for all the data you requested, or check how much data it actually read. 除此之外,user315052已经找到了一个更重要的问题,你应该告诉recv方法等待你请求的所有数据,或者检查它实际读取了多少数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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