void serv_proc(int fd){ char buf[MAXLINE]; Req_header header; char delims[] = "/"; char *result = NULL; readline(fd, buf, MAXLINE); #ifdef DEBUG fprintf(stderr, "%s", buf); #endif parse_request(buf, &header); memset(buf, 0, MAXLINE); strcpy(buf, header.locator); result = strtok(buf, delims); if (strcmp(result, "cgi-bin") == 0) { cgi_handle(fd, &header); } else { http_respond(fd, &header); } servlog(LOG_INFO, "[client:%s][%s] %d\n", "192.168.1.1", "GET /index.html HTTP/1.1", 200); }
/* * Handles all the steps needed for and HTTP exchange */ void *handle_client( void *args ){ int client_file_desc = *( ( int *) args ); // What's the client saying? char *client_request; int bytes_received = http_receive_request( client_file_desc, &client_request ); if( !( bytes_received > 0 ) ){ printf( "[THREAD %lu] Got less than 1 byte from http_receive_request()\n", ( unsigned long ) pthread_self( ) ); } //Parse out the request HTTPRequest *request = malloc( sizeof ( HTTPRequest ) ); memset( request, 0, sizeof( HTTPRequest ) ); // I'm putting this on the stack char sc[10]; // because snprintf( sc, 10, "%lu", ( unsigned long ) pthread_self( ) ); // shut up char *strtok_context = &sc[0]; // No ragrets char *http_request_line = strtok_r( client_request, "\r\n", &strtok_context ); request->command = strtok_r( http_request_line, " ", &strtok_context ); request->resource = strtok_r( NULL, " ", &strtok_context ); request->version = strtok_r( NULL, " ", &strtok_context ); // Oh look! I don't need to free strtok_context. // yay // Respond to the client if( strcmp( request->command, "GET") == 0 ){ http_respond( client_file_desc, request->resource ); }else{ // Nothing more for me to do here } // Clean up memory and terminate the thread // Don't need these anymore free( request ); free( client_request ); // Deallocate the thread slot and close the file descriptor for the socket close_thread( pthread_self( ) ); printf( "[THREAD %lu] Thread socket unlocked\n", ( unsigned long ) pthread_self( ) ); printf( "[MAIN THREAD] %d/%d clients connected\n", count_thread_sockets( ), MAX_CLIENTS ); shutdown( client_file_desc, 2 ); close( client_file_desc ); // Terminate the thread pthread_exit( NULL ); }
int main() { int port = 80; int sockfd; char* buffer = malloc(sizeof(char*) * 1024); char* filename = malloc(sizeof(char*) * 255); struct http_request* request = malloc(sizeof(*request)); struct http_response* response = malloc(sizeof(*response)); printf("Creating socket\n"); sockfd = create_socket(port); while(1) { int clientfd; clientfd = accept_client(sockfd); http_read_request(clientfd, &buffer); http_parse_request(request, &buffer); // If / is requested, substitute it for index if(strlen(request->path) == 1) { sprintf(request->path, "index"); } // Make sure path not containing . ends with .html if(strstr(request->path, ".") == NULL) { strcat(request->path, ".html"); } printf("[REQUEST] Method: %s\n", request->method); printf("[REQUEST] Path: %s\n", request->path); // Make sure that requested file exists sprintf(filename, "static/%s", request->path); if(file_exists(filename) == false) { sprintf(filename, "static/404.html"); } memset(buffer, '\0', sizeof(char*) * 255); read_file(filename, &buffer); char* content_type = malloc(sizeof(char*) * 255); http_resolve_content_type(&content_type, filename); printf("[RESPONSE] Content-Type: %s\n", content_type); http_set_response(response, &buffer, content_type, strlen(buffer)); http_respond(clientfd, response); close(clientfd); printf("--------------\n"); } free(response->data); free(response->content_type); free(response); free(request->method); free(request->path); free(request); free(filename); close(sockfd); return 0; }