diff --git a/server.c b/server.c index 591935fde634a6bc88602fde3f6df02995e6e71f..4c86aa68b902e85e71c76b365d152d0ea0c34b96 100644 --- a/server.c +++ b/server.c @@ -66,14 +66,10 @@ char *get_path(char header[]){ } } + printf("from inside get path: %s", path); return slice(path, 0, point_in_string); } -void *connection_handler(void *sock){ - -} - - int main(int argc, char **argv) { int sockfd, newsockfd, portno; @@ -101,28 +97,23 @@ int main(int argc, char **argv) portno = atoi(argv[1]); // create address to connect - serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; - serv_addr.sin_port = htons(portno); // store in machine-neutral format - - /* Bind address to the socket */ + serv_addr.sin_port = htons(portno); + // Bind away if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(1); } - /* Listen on socket - means we're ready to accept connections - - incoming connection requests will be queued */ + // listen and queue other connections listen(sockfd,5); clilen = sizeof(cli_addr); - /* Accept a connection - block until a connection is ready to - be accepted. Get back a new file descriptor to communicate on. */ - + // accept a connection newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) @@ -133,8 +124,7 @@ int main(int argc, char **argv) bzero(buffer, 256); - /* Read characters from the connection, - then process */ + //read what the message asks n = read(newsockfd,buffer,255); if (n < 0) { @@ -148,18 +138,16 @@ int main(int argc, char **argv) char path[80]; strcpy(path, argv[2]); strcat(path, get_path(buffer)); - printf("This is the path: |%s| %d\n", path, strlen(path)); - - printf("Here is the message: %s\n",buffer); FILE *file; file = fopen(path, "r"); char msg[1024]; if(!file){ - printf("the file is not there"); + // file not found, generate a 404 message strncpy(msg, unfound_header, 1024); } else{ + // file found, generate a proper header with the info strncpy(msg, found_header, 1024); char *mime; mime = get_mime(path); @@ -170,7 +158,6 @@ int main(int argc, char **argv) while(fgets(line, 1024, file)){ strcat(msg, line); } - printf("we got the file: %s\n", msg); } n = write(newsockfd,msg,strlen(msg)); @@ -179,9 +166,6 @@ int main(int argc, char **argv) perror("ERROR writing to socket"); exit(1); } - - /* close socket */ - close(sockfd); return 0;