int proxyRequest(string &request, char buffer[], struct hostent* host){
int sockfd, sockopt;
struct sockaddr_in their_addr;
if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){
perror("Socket generating failed");
return -1;
}
if(host==NULL){
strcpy(buffer, "HTTP/1.1 404 Not found\r\nContent-Type: text/html\r\n\r\n<h2>INET_E_RESOURCE_NOT_FOUND</h2>");
}
else{
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(SERVERPORT);
their_addr.sin_addr.s_addr = ((struct in_addr*)host->h_addr_list[0])->s_addr;
if(connect(sockfd, (struct sockaddr*)&their_addr, sizeof(their_addr)) == -1){
perror("Connection failed");
return -1;
}
write(sockfd, request.c_str(), request.length());
read(sockfd, buffer, BUFSIZE);
cout << buffer << endl;
}
close(sockfd);
return 0;
}
I am making a simple proxy server and everything's fine, except I can't receive a correct resposne body.
This is the request that I send to server(www.example.com). This is represented as "request" in the code.
It seems that http headers are received correctly. However, the html file(body) is not sent at all. And there is a weird character instead of it. Why does this happen? Is it related to null character?
However, the html file(body) is not sent at all. And there is a weird character instead of it. Why does this happen?
The body is sent, but compressed. The following tells you that the content is compressed using the gzip
algorithm:
Content-Encoding: gzip
You'll need to either decompress it (taking care around NUL characters) or tell the server that you're not prepared to deal with gzip
encoded content (i.e. remove the Accept-Encoding
header in your request).