- May 8, 2002
- 2,702
- 1
- 81
I'm having a bit of trouble in creating a simple C-based HTTP proxy server in Linux. So far, the proxy can retrieve the Internet client GET requests fine. When the initial GET request is forwarded onto the HTTP server (i.e. www.anandtech.com), I can retrieve data from the HTTP server and pass it back to the Internet client just fine. However, when I pass on any GET requests onto the HTTP server after the initial GET, the HTTP server always responds with a value of 0 when I do a "recv" on it. Because of this, only the structure of the webpage does appear in the client (HTML portion), but none of the image files are loading. I have a attached a portion of my proxy code so far:
Am I implementing it the wrong way? I can't figure out why the server responds correctly the first time through, but doesn't for the rest of the loops.
Code:
int HTTPconnect = 0;
int resource = 0;
listen(sock, 100);
// The main while loop of the proxy program, it continuously waits for
// receiving connection requests and forwards them onto the client.
while (1) {
// Attempt to accept the socket.
clientLen = sizeof(client);
serverLen = sizeof(servaddr);
if((msgsock = accept(sock, (struct sockaddr*) &client, &clientLen)) == -1) {
fprintf(stderr, "ERROR: Cannot accept socket.\n");
}
// Clear buf array with spaces.
for (i = 0; i < 1024; i++) {
buf[i] = ' ';
}
// Read from msgsock and put recieved data into buf.
if ((rval = read(msgsock, buf, 1024)) < 0 ) {
perror("ERROR: Could not read from socket");
}
// Extracts the hostname and full URL from the client's GET request.
strcpy(argurl, buf);
token = strtok(argurl, " ");
token = strtok(0, "/");
hostname = strtok(0, "/?");
strcpy(url, hostname);
if (resource == 1) {
resourceID = strtok(0, " ");
strcpy(resourceMe, resourceID);
strcat(url, "/");
strcat(url, resourceMe);
}
resource = 1;
// Initialize the host name and port number to the HTTP server.
hp = gethostbyname(hostname);
bcopy(hp->h_addr, &(servaddr2.sin_addr.s_addr), hp->h_length);
servaddr2.sin_port = htons(atoi("80"));
// Attempt to connect to HTTP server.
if (HTTPconnect == 0) {
if ( connect(HTTPsock,(struct sockaddr *)&servaddr2, sizeof(servaddr2)) < 0 ) {
close(HTTPsock);
perror("Error in connecting a stream socket");
exit(1);
}
}
HTTPconnect = 1;
printf("Sending client request to the HTTP server...\n");
if (send(HTTPsock, buf, strlen(buf), 0) < 0) { /*send message by writing to the scoket sock */
fprintf(stderr,"Error in Writing on steam socket");
exit(1);
}
printf("Preparing to recieve response...\n");
// Recieveing the response from HTTP server.
while ((n = recv(HTTPsock, buf2, 4096, 0)) > 0) {
printf("Response value (Loop): ");
printf("%d \n", n);
send(msgsock, buf2, n, 0);
}
printf("Server Response value: ");
printf("%d", n);
printf("\n");
for ( i = 0; i < 4096; i++) {
buf2[i] = ' '; /* clear buf array with spaces */
}
printf("Loop finished. Beginning next loop...\n");
close(msgsock);
}
Am I implementing it the wrong way? I can't figure out why the server responds correctly the first time through, but doesn't for the rest of the loops.
