gpt4 book ai didi

c - HTTP代理实现: content encoding error

转载 作者:行者123 更新时间:2023-11-30 16:32:23 25 4
gpt4 key购买 nike

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

更详细地说,我只能渲染响应 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com