c - 为什么代理 HTTP CONNECT 响应后 iphone 没有响应
问题描述
我正在尝试制作一个代理服务器,目前想知道为什么我正在测试它的 iPhone 发送一个 CONNECT 请求,得到“HTTP 200 连接已建立”的响应,并且通常在此之后不再响应。(有时它会立即以“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 \0 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将继续发送数据
推荐阅读
- android - 如何在 Spinner (android Kotlin) 中实现 For 循环逻辑
- node.js - 当错误出现时,我的代码不会进入节点 js 中的 else 块
- sql - 使用 oracle sql 将行转换为列
- druid - 如何自动化德鲁伊批量摄取工作?
- replace - 如何在notepad ++中替换多个相同的行而不手动进行?
- python - 如何将 TensorFlow 保存的模型加载到缓存中以便下次更快加载?
- javascript - 缺少一些 Kurento 媒体服务器视频
- javascript - 在javascript中读取和转换每月网格excel文件数据
- docker - 为什么我的 docker 图像大于 du -hd 1 /
- android - How to Remove an item from recyclerview after recyclerview item's countdown timer expires?