我在 C 中实现一个 HTTP 代理服务器。
我从browser(客户端)获取 HTTP 请求标头并对其进行解析以查找 url、端口号等。然后我以以下格式向远程服务器发出请求:
GET / HTTP1.0\r\nHost: www.gmail.com:80\r\n\r\n
但是在执行过程中,代理服务器无法从远程服务器读取整个日期。
从代理服务器到远程服务器的标头或从服务器读取数据是否有问题。
void handle_connection(......)
{
......
//request send from proxy to remote server
sprintf(buffer, "%s %s HTTP/1.%d\r\nHost: %s:80\r\n\r\n"
, http_args[0], requested_url->file, requested_url->proto, requested_url->host);
//sending the request to remote server connection socket rfd
n = write(rfd, buffer, sizeof(buffer));
//reading data from the response of the server
while ((n = read(rfd, buffer, BUFFSIZE)) > 0) {
write(connfd, buffer, BUFFSIZE); //sending it to client web brownser
bzero(buffer, BUFFSIZE);
}
close(rfd);
}