Tiny Error
ID: 666501 • Letter: T
Question
HTTP C Programming Assignment Description Using network sockets, write a program that accepts a command-line argument representing the web server to connect to. Then, create a client socket that connects to the server you indicate (y can use the CSAPP open listenfd0 function for this), and ou sends an HTTP request to it over port 80 (you may use another command line argument for the port, if custom ports are desired). You should send the following over your socket GET /index.html HTTP /1.1 Host: www.google.com Then, read the entire result and display it on the screen. Modify the example HTTP request above so that the page index.html" is also a command-line argument, and the host sent should actually be the same hos that you connected to (also via a command line argument above You will find the sample CSAPP echo client helpful for this. Once this is done, write a second program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept0 connections, read requests of the form GET pat HTTP /1.1 read the file indicated by path, and return it over the connFD given from the call to acceptExplanation / Answer
#include #include #include #include "csapp.h" #include #include #define MAX_CACHE_SIZE 1049000 #define MAX_OBJECT_SIZE 102400 #ifdef BIT32 typedef unsigned long long aint; #else typedef unsigned long aint; #endif static const char *user_agent = "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:1.9.2.3) Gecko/20100423 Firefox/3.6.3 "; static const char *accept_encoding = "Accept-Encoding: gzip, deflate "; static const char *accept_0 = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 "; int proxy(int fd); int open_clientfd_safe(char *hostname, int port); int parse_uri(char *uri, char *hostname, int *port); int read_request(rio_t *rp, char *buf_request, char *hostname, int *port, char *uri); void *thread(void *vargp); void initial(); void clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg); sem_t mutex; static int set_num, line_num; static void init(); static int loaddata(char *tag, char *response); static void storedata(char *response, char *tag); static void pop_usage(int *array, int key, int len); struct cache_line { int valid; char *tag; char *block; }; struct cache_set { struct cache_line *line; int *usage; }; struct cache { struct cache_set *set; }; static struct cache c; int main(int argc,char **argv) { int listenfd, *connfd, port; unsigned clientlen; //struct hostent *hp; struct sockaddr_in clientaddr; //char *haddrp; pthread_t tid; /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s ", argv[0]); exit(1); } port = atoi(argv[1]); //ignore sigpipe signal(SIGPIPE, SIG_IGN); //initialization initial(); listenfd = Open_listenfd(port); while(1) { connfd = malloc(sizeof(int)); clientlen = sizeof(clientaddr); *connfd = accept(listenfd, (SA *)&clientaddr, &clientlen); //hp = Gethostbyaddr((const char *) & clientaddr.sin_addr.s_addr,sizeof(clientaddr.sin_addr.s_addr),AF_INET); //haddrp = inet_ntoa(clientaddr.sin_addr); printf("Build new connection! Connfd:%d ", *connfd); pthread_create(&tid, NULL, thread, connfd); } } /* Initialization*/ static void init() { int i, j; c.set = malloc(sizeof (struct cache_set) * set_num); for (i = 0; i 0) { printf("hostname:%s port:%d buf_response:%s ", hostname, port, buf_response); strcat(cache_response, buf_response); response_len += len; if(rio_writen(connfd, buf_response, len) < 0) { printf("buf_response error:%s len:%d %s ", strerror(errno), len, buf_response); fprintf(stderr, "rio_writen send response error "); close(serverfd); return -1; } /* reset the buffer*/ memset(buf_response, 0, sizeof(buf_response)); } if(response_lenRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.