为什么在代理 HTTP CONNECT 响应后 iphone 没有响应

Why is there no response from iphone after proxy HTTP CONNECT response

我正在尝试创建一个代理服务器,目前想知道为什么我正在测试它的 iPhone 发送了一个 CONNECT 请求,得到了 "HTTP 200 Connection Established" 的响应并且之后通常不再响应. (有时会立即回复“0”或NULL)

我实际上并没有连接到 iPhone 想要去的地方,但我发回了一个指示代理已连接的响应

终端输出:

./proxy 9000

此时我在iPhone代理设置中输入我本地的ip

示例输出:

REC:
CONNECT clientmetrics.kik.com:443 HTTP/1.1
Host: clientmetrics.kik.com
User-Agent: Kik/14.4.0.11622 CFNetwork/897.15 Darwin/17.5.0
Connection: keep-alive
Proxy-Connection: keep-alive

Sent:
HTTP/1.1 200 Connection Established

REC:
// Typically [=12=] or a 0, or no recv takes place

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
if(argc == 2) {
    int main(int argc, char* argv[]) {
       system("clear");

        // Set up socket, family, port, ip address
        int serversocket = socket(AF_INET, SOCK_STREAM, 0);
        struct sockaddr_in serverinfo;
        serverinfo.sin_family = AF_INET;
        serverinfo.sin_addr.s_addr = INADDR_ANY;
        serverinfo.sin_port = htons(atoi(argv[1]));

        // bind server info to socket
        if(bind(serversocket, (struct sockaddr*)&serverinfo, sizeof(serverinfo)) == -1) {
             printf("failed  to bind port to socket, change port\n");
             return -1;
        }

        // accept a connection
        listen(serversocket, 1);
        int clientsocket = accept(serversocket, NULL, NULL);

        // recv and print client message
        char buffer[2048];
        memset(buffer, 0, sizeof(buffer));
        if(recv(clientsocket, buffer, sizeof(buffer), 0) == -1) {
            printf("Failed in recv function\n");
        } else {
            printf("REC\n%s\n", buffer);
        }

        // send Connection Established
        char* sendbuffer = "HTTP/1.1 200 Connection Established";
        if(send(clientsocket, sendbuffer, sizeof(sendbuffer), 0) == -1) {
            printf("Failed in send function\n");
        } else {
            printf("Sent:\n%s\n", sendbuffer);
            // Recv response from iPhone
            memset(buffer, 0, sizeof(buffer));
            if(recv(clientsocket, buffer, sizeof(buffer), 0) == -1) {
                 printf("Failed in recv function\n");
            } else {
                printf("REC:\n%s\n", buffer);
            }
        }

    } else {
        printf("Usage: ./proxy <port>\n");
        return 0;
    }
}

只是在乱搞一些东西,发现 iPhone 永远不会响应超出我的 CONNECT 请求的范围,因为在代理响应 HTTP 200 之后必须关闭连接 - 然后可以重新建立连接并且 iphone会继续发送数据