c - HTTP代理实现: content encoding error

标签 c proxy

我正在实现一个代理。我可以从服务器接收响应,但无法将响应发送到客户端。

更详细地说,我只能渲染响应 header 内容,但无法发送消息正文。并且网页显示“内容编码错误”

    //I could sending request to server successfully.
    send(connfd_to_server, request, strlen(request), 0);

    //receive response from server
    char res_buf[1024];
    while(1){
        bzero(res_buf, 1024);
        if(recv(connfd_to_server, res_buf, sizeof(res_buf),0) <=0){
            break; //if recv failed, then message body is finished.
        }  //receive response using recv
        send(connfd_to_client, res_buf, strlen(res_buf));
    }

Here is what I got, header is sent to client successfully, but following message body is not

我也尝试过:

char* response = (char*)malloc(strlen(res_buf));
char* res_line;
res_line = strtok(res_buf, "\r\n");
for(int i = 0; i<=11; i++){
     strcat(response, res_line);
     strcat(response, "\r\n");
     res_line = strtok(NULL, "\r\n");
}    //copy header content using strcat

while(res_line!= NULL){
    memcpy(response, res_line, sizeof(res_line));
    res_line = strtok(NULL, "\r\n");
}   //copy message body as bytes using memcpy

then send response to client using send function.

但是,无论我使用哪个函数,消息正文都无法成功发送。 就像上面一个奇怪的符号所示 有什么提示吗?

提前非常感谢

最佳答案

函数recv返回读取的字节数,您可以在调用函数send时使用该字节数。

您使用了基于检测空字符的 strlen 来查找缓冲区的末尾,在本例中这是无效的。

关于c - HTTP代理实现: content encoding error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50169234/

相关文章:

c# - 如何查看 HttpWebRequest 类发送的原始 HTTP 请求?

c - C DLL 目录中的加载时动态链接

c - SendMessage 适用于 WM_CHAR,但不适用于 WM_KEYDOWN

c - 为什么编译器不警告我明显的错误?

.net - WCF WS2007FederationHttpBinding 与 HTTPS

http - 通过代理强制使用 HTTP1.1 而不是 HTTP2 (Charles)

c - 给定一组数字,找到具有最小 LCM(最小公倍数)的对

C 在文本中沿对角线查找字符串

proxy - 尝试使用 sidecar 代理连接到网络时出现错误连接被拒绝

java - HTTPS URL 的基本代理身份验证返回 HTTP/1.0 407 需要代理身份验证