1

我的任务是实现简单的 HTTP 服务器。当我发送响应时,我应该支持分块传输编码。这是我向客户端发送响应的函数。

static int serve_request(int sock, struct conf_arg *arg, char version[])
{
    FILE *html = NULL;
    char buf[MAX_MSG];

    strcat(arg->root, arg->defdoc);
    html = fopen(arg->root, "r");
    if (!html) {
        not_found(sock, version);
        return 0;
    }
    good_responce(sock, version);
    do {
        fgets(buf, sizeof(buf), html);
        const unsigned chunk = CHUNK_SIZE;
        char *pbuf = buf;
        char tempbuf[chunk + 10];

        while (strlen(pbuf) >= chunk) {
            sprintf(tempbuf, "%x\r\n", chunk);
            write(sock, tempbuf, strlen(tempbuf));
            write(sock, pbuf, chunk);
            pbuf += chunk;
            strcpy(tempbuf, "\r\n");
            write(sock, tempbuf, strlen(tempbuf));
        }
        if (strlen(pbuf) == 0) {
            sprintf(tempbuf, "%x\r\n", 0);
            write(sock, tempbuf, strlen(tempbuf));
        }
        if (strlen(pbuf) > 0) {
            sprintf(tempbuf, "%x\r\n", (unsigned)strlen(pbuf));
            write(sock, tempbuf, strlen(tempbuf));
            write(sock, pbuf, strlen(pbuf));
            sprintf(tempbuf, "%x\r\n", 0);
            write(sock, tempbuf, strlen(tempbuf));
        }
        strcpy(tempbuf, "\r\n");
        write(sock, tempbuf, strlen(tempbuf));
    } while (!feof(html));
    fclose(html);
    return 0;
}

CHUNK_SIZE定义为 1024 因为我想发送 1KB 大小的块。当我打开页面时出现问题enter image description here 页面显示不正确。我还设置了 Transfer-Encoding: chunked

strcpy(buf, ENCODING);
send(sock, buf, strlen(buf), 0);

ENCODING 被定义为“Transfer-Encoding: chunked\r\n”

4

1 回答 1

1

我想我知道问题出在哪里,但并不完全确定。

在你的do循环中,你会得到一个buf完整的数据并发送它。然后你得到另一个充满数据的缓冲区并发送它。但是在发送了每个数据缓冲区之后,您可以通过发送一个0\r\n. 例如:

1024    // send first chunk
1024    // send second chunk
256     // last part of first bufer
0       // terminate transfer
1024    // send first chunk of second buffer
1024    //...
256
0

尽管在发送最后一个块之前再次填充缓冲区可能会更好(用于memmove将最后一部分向下移动,然后调用fgets以填充剩余部分),但您可以通过在循环0\r\n之后发送 only来“保存”,例如:do ... while

        if (strlen(pbuf) > 0) {
            sprintf(tempbuf, "%x\r\n", (unsigned)strlen(pbuf));
            write(sock, tempbuf, strlen(tempbuf));
            write(sock, pbuf, strlen(pbuf));
            //sprintf(tempbuf, "%x\r\n", 0);
            //write(sock, tempbuf, strlen(tempbuf));
        }
        //strcpy(tempbuf, "\r\n");
        //write(sock, tempbuf, strlen(tempbuf));
    } while (!feof(html));
    sprintf(tempbuf, "%x\r\n", 0);
    write(sock, tempbuf, strlen(tempbuf));
    strcpy(tempbuf, "\r\n");
    write(sock, tempbuf, strlen(tempbuf));

另请注意,您必须检查结果,fgets因为它可以在 eof 时返回零;缓冲区不会被刷新,您将再次发送最后一部分:

    if (fgets(buf, sizeof(buf), html)==NULL) break;

另请参阅有关您不必要地使用tempbuf.

于 2017-11-20T12:22:09.080 回答