1 /* J. David's webserver */ 2 /* This is a simple webserver. 3 * Created November 1999 by J. David Blackstone. 4 * CSE 4344 (Network concepts), Prof. Zeigler 5 * University of Texas at Arlington 6 */ 7 /* This program compiles for Sparc Solaris 2.6. 8 * To compile for Linux: 9 * 1) Comment out the #include <pthread.h> line. 10 * 2) Comment out the line that defines the variable newthread. 11 * 3) Comment out the two lines that run pthread_create(). 12 * 4) Uncomment the line that runs accept_request(). 13 * 5) Remove -lsocket from the Makefile. 14 */ 15 #include <stdio.h> 16 #include <sys/socket.h> 17 #include <sys/types.h> 18 #include <netinet/in.h> 19 #include <arpa/inet.h> 20 #include <unistd.h> 21 #include <ctype.h> 22 #include <strings.h> 23 #include <string.h> 24 #include <sys/stat.h> 25 #include <pthread.h> 26 #include <sys/wait.h> 27 #include <stdlib.h> 28 #include <stdint.h> 29 30 #define ISspace(x) isspace((int)(x)) 31 32 #define SERVER_STRING "Server: jdbhttpd/0.1.0 " 33 #define STDIN 0 34 #define STDOUT 1 35 #define STDERR 2 36 37 void accept_request(void *); 38 void bad_request(int); 39 void cat(int, FILE *); 40 void cannot_execute(int); 41 void error_die(const char *); 42 void execute_cgi(int, const char *, const char *, const char *); 43 int get_line(int, char *, int); 44 void headers(int, const char *); 45 void not_found(int); 46 void serve_file(int, const char *); 47 int startup(u_short *); 48 void unimplemented(int); 49 50 /**********************************************************************/ 51 /* A request has caused a call to accept() on the server port to 52 * return. Process the request appropriately. 53 * Parameters: the socket connected to the client */ 54 /**********************************************************************/ 55 void accept_request(void *arg) 56 { 57 int client = (intptr_t)arg; 58 char buf[1024]; 59 size_t numchars; 60 char method[255]; 61 char url[255]; 62 char path[512]; 63 size_t i, j; 64 struct stat st; 65 int cgi = 0; /* becomes true if server decides this is a CGI 66 * program */ 67 char *query_string = NULL; 68 69 numchars = get_line(client, buf, sizeof(buf)); 70 i = 0; j = 0; 71 while (!ISspace(buf[i]) && (i < sizeof(method) - 1)) 72 { 73 method[i] = buf[i]; 74 i++; 75 } 76 j=i; 77 method[i] = '