/* Implement a web server that just forwards every request to www.eng.utah.edu */ #include "csapp.h" int main(int argc, char **argv) { char *portno = "11111"; int ls, cs, s; ls = Open_listenfd(portno); while (1) { struct sockaddr_in addr; unsigned int len = sizeof(addr); size_t total_amt, amt; char *buffer; s = Accept(ls, (struct sockaddr *)&addr, &len); cs = Open_clientfd("www.eng.utah.edu", "80"); //char *req_str = "GET /~cs4400/ HTTP/1.0\r\n\r\n"; buffer = malloc(MAXBUF); rio_t rio; Rio_readinitb(&rio, s); while((amt = Rio_readlineb(&rio, buffer, MAXBUF)) != 2) { Rio_writen(cs, buffer, amt); } Rio_writen(cs, "\r\n", 2); /* If a client is using HTTP/1.1 and has requested that the connection stay alive, then this loop will get stuck --- until the server decides that the connection has been open long enough and closes it anyway. To avoid this problem, intercept any "Connection:" header from the client and add a "Connection: close" header line. */ while((amt = Rio_readn(cs, buffer, MAXBUF)) != 0){ Rio_writen(s, buffer, amt); } Close(s); free(buffer); Close(cs); } }