C HTTP Proxy,浏览器无限期显示 "waiting for"

标签 c sockets http proxy blocking

我很难用 C 语言实现代理服务器。它适用于最初的几个网页,但随后我在等待新请求时被阻止。

设计:

Firefox -> Proxy -> Webserver --.
Firefox <- Proxy <- Webserver <-'

因此,每个请求都是从浏览器到代理和服务器然后返回的往返过程。在请求响应返回之前,不应发生任何事情。我不使用管道、线程或类似的东西,而是仅以线性串行方式使用recv()send()(为了简单起见)直觉)。我也不会关闭任何套接字,因为我想拥有持久连接

我希望能够获取整个网页,包括 css、img、js 等子请求的资源。

在我的实现中,我设法获取一些对网页的第一个请求。然后它在第 1 步挂起

实现:

puts("Waiting for user to connect..");
int sock_user = accept(sock, (struct sockaddr*)NULL, NULL);
int sock_host = -1;
printf("User connected.\n");


// Accept requests
while(1){

   http_request req;
   http_response resp;


   // 1. Client ==> Proxy     Server
   http_parse_request(sock_user, &req);                         // uses recv()

   // 2. Client     Proxy ==> Server
   if (sock_host < 0)
      sock_host = proxy_connect_host(req.header->host);
   write(sock_host, req.header->raw_data, req.header->raw_size);

   // 3. Client     Proxy <== Server
   http_parse_response(sock_host, &resp);                      // uses recv()

   // 4. Client <== Proxy     Server
   write(sock_user, resp.header->raw_data,          resp.header->raw_size);
   write(sock_user, resp.body  ->first_block->data, resp.body  ->first_block->size);

}

日志:

     ---- ......................................... ----

     ---- after succesfully responded to 4 requests ----


Client ==> Proxy     Server
                                Received              389

Client     Proxy ==> Server
                                Sending..             389
                                Sent                  389

Client     Proxy <== Server
                                Got header            312
                                Got body              1437
                                Response total        1749

Client <== Proxy     Server
                                Sending header..      312
                                Sent                  312
                                Sending body..        1437
                                Sent                  1437

Client ==> Proxy     Server


                ---- Hangs/blocks here ----

Firebug :

enter image description here

Wireshark: enter image description here

我不知道该阻塞的原因是什么,我花了整整一周的时间试图解决这个问题,但没有取得突破。

尝试解决问题的方法包括:

  1. 为每个响应正文发送一些额外的 CRLF
  2. 检查了每个 recv()send() 的返回值。 (在上面的日志中, 打印的值是来自 recv 的返回值和 发送)

我希望有人至少能给出一些关于如何解决这个问题的指导,否则我的大脑很快就会爆炸:)

最佳答案

您必须非常小心,不要读取太多数据。例如。确保:

  • header 只读直至双 CRLF;存储额外的数据并与正文一起发送

  • 正文的发送不会在服务器发送完整 header 之前开始(不适用于此 GET 情况,但对于 POST 或 CONNECT 很重要)

  • 仅从正文中接收和发送 Content-Length 字节

这适用于客户端 -> 代理请求和服务器 -> 代理响应。

您的示例代码处于无限循环中(while (1) ...)。你如何中止这个?您尊重“Proxy-Connection” header 吗?

关于C HTTP Proxy,浏览器无限期显示 "waiting for",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19607225/

相关文章:

c - 最大行长度程序在 c 中打印不可读的字符

node.js - 递归使用 socket.pipe 时的 MaxListenersExceededWarning

使用 curses.h 库的光标跳转

c++ - 在 TCP 线程服务器 C++ 中传输文件

c# - 无法从 Nuget 安装包 System.Net.Http

c - 这是基于堆栈的内存吗

c - 语法错误 : missing ')' before ';'

http - 当 Web 服务器返回 JPEG 图像(mime 类型图像/jpeg)时,它是如何编码的?

angularjs - 将 ui-router $stateParams 传递给 $http 工厂服务

c - 使用 Linux C 通过 SOCKS5 进行 HTTP 请求