|
| 1 | +/* |
| 2 | +AUTHOR: Abhijeet Rastogi (http://www.google.com/profiles/abhijeet.1989) |
| 3 | +
|
| 4 | +This is a very simple HTTP server. Default port is 10000 and ROOT for the server is your current working directory.. |
| 5 | +
|
| 6 | +You can provide command line arguments like:- $./a.aout -p [port] -r [path] |
| 7 | +
|
| 8 | +for ex. |
| 9 | +$./a.out -p 50000 -r /home/ |
| 10 | +to start a server at port 50000 with root directory as "/home" |
| 11 | +
|
| 12 | +$./a.out -r /home/shadyabhi |
| 13 | +starts the server at port 10000 with ROOT as /home/shadyabhi |
| 14 | +
|
| 15 | +*/ |
| 16 | + |
| 17 | +#include<stdio.h> |
| 18 | +#include<string.h> |
| 19 | +#include<stdlib.h> |
| 20 | +#include<unistd.h> |
| 21 | +#include<sys/types.h> |
| 22 | +#include<sys/stat.h> |
| 23 | +#include<sys/socket.h> |
| 24 | +#include<arpa/inet.h> |
| 25 | +#include<netdb.h> |
| 26 | +#include<signal.h> |
| 27 | +#include<fcntl.h> |
| 28 | + |
| 29 | +#define CONNMAX 1000 |
| 30 | +#define BYTES 1024 |
| 31 | + |
| 32 | +char *ROOT; |
| 33 | +int listenfd, clients[CONNMAX]; |
| 34 | +void error(char *); |
| 35 | +void startServer(char *); |
| 36 | +void respond(int); |
| 37 | + |
| 38 | +int main(int argc, char* argv[]) |
| 39 | +{ |
| 40 | + struct sockaddr_in clientaddr; |
| 41 | + socklen_t addrlen; |
| 42 | + char c; |
| 43 | + |
| 44 | + //Default Values PATH = ~/ and PORT=10000 |
| 45 | + char PORT[6]; |
| 46 | + ROOT = getenv("PWD"); |
| 47 | + strcpy(PORT,"10000"); |
| 48 | + |
| 49 | + int slot=0; |
| 50 | + |
| 51 | + //Parsing the command line arguments |
| 52 | + while ((c = getopt (argc, argv, "p:r:")) != -1) |
| 53 | + switch (c) |
| 54 | + { |
| 55 | + case 'r': |
| 56 | + ROOT = malloc(strlen(optarg)); |
| 57 | + strcpy(ROOT,optarg); |
| 58 | + break; |
| 59 | + case 'p': |
| 60 | + strcpy(PORT,optarg); |
| 61 | + break; |
| 62 | + case '?': |
| 63 | + fprintf(stderr,"Wrong arguments given!!!\n"); |
| 64 | + exit(1); |
| 65 | + default: |
| 66 | + exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + printf("Server started at port no. %s%s%s with root directory as %s%s%s\n","\033[92m",PORT,"\033[0m","\033[92m",ROOT,"\033[0m"); |
| 70 | + // Setting all elements to -1: signifies there is no client connected |
| 71 | + int i; |
| 72 | + for (i=0; i<CONNMAX; i++) |
| 73 | + clients[i]=-1; |
| 74 | + startServer(PORT); |
| 75 | + |
| 76 | + // ACCEPT connections |
| 77 | + while (1) |
| 78 | + { |
| 79 | + addrlen = sizeof(clientaddr); |
| 80 | + clients[slot] = accept (listenfd, (struct sockaddr *) &clientaddr, &addrlen); |
| 81 | + |
| 82 | + if (clients[slot]<0) |
| 83 | + error ("accept() error"); |
| 84 | + else |
| 85 | + { |
| 86 | + if ( fork()==0 ) |
| 87 | + { |
| 88 | + respond(slot); |
| 89 | + exit(0); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + while (clients[slot]!=-1) slot = (slot+1)%CONNMAX; |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | +//start server |
| 100 | +void startServer(char *port) |
| 101 | +{ |
| 102 | + struct addrinfo hints, *res, *p; |
| 103 | + |
| 104 | + // getaddrinfo for host |
| 105 | + memset (&hints, 0, sizeof(hints)); |
| 106 | + hints.ai_family = AF_INET; |
| 107 | + hints.ai_socktype = SOCK_STREAM; |
| 108 | + hints.ai_flags = AI_PASSIVE; |
| 109 | + if (getaddrinfo( NULL, port, &hints, &res) != 0) |
| 110 | + { |
| 111 | + perror ("getaddrinfo() error"); |
| 112 | + exit(1); |
| 113 | + } |
| 114 | + // socket and bind |
| 115 | + for (p = res; p!=NULL; p=p->ai_next) |
| 116 | + { |
| 117 | + listenfd = socket (p->ai_family, p->ai_socktype, 0); |
| 118 | + if (listenfd == -1) continue; |
| 119 | + if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0) break; |
| 120 | + } |
| 121 | + if (p==NULL) |
| 122 | + { |
| 123 | + perror ("socket() or bind()"); |
| 124 | + exit(1); |
| 125 | + } |
| 126 | + |
| 127 | + freeaddrinfo(res); |
| 128 | + |
| 129 | + // listen for incoming connections |
| 130 | + if ( listen (listenfd, 1000000) != 0 ) |
| 131 | + { |
| 132 | + perror("listen() error"); |
| 133 | + exit(1); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +//client connection |
| 138 | +void respond(int n) |
| 139 | +{ |
| 140 | + char mesg[99999], *reqline[3], data_to_send[BYTES], path[99999]; |
| 141 | + int rcvd, fd, bytes_read; |
| 142 | + |
| 143 | + memset( (void*)mesg, (int)'\0', 99999 ); |
| 144 | + |
| 145 | + rcvd=recv(clients[n], mesg, 99999, 0); |
| 146 | + |
| 147 | + if (rcvd<0) // receive error |
| 148 | + fprintf(stderr,("recv() error\n")); |
| 149 | + else if (rcvd==0) // receive socket closed |
| 150 | + fprintf(stderr,"Client disconnected upexpectedly.\n"); |
| 151 | + else // message received |
| 152 | + { |
| 153 | + printf("%s", mesg); |
| 154 | + reqline[0] = strtok (mesg, " \t\n"); |
| 155 | + if ( strncmp(reqline[0], "GET\0", 4)==0 ) |
| 156 | + { |
| 157 | + reqline[1] = strtok (NULL, " \t"); |
| 158 | + reqline[2] = strtok (NULL, " \t\n"); |
| 159 | + if ( strncmp( reqline[2], "HTTP/1.0", 8)!=0 && strncmp( reqline[2], "HTTP/1.1", 8)!=0 ) |
| 160 | + { |
| 161 | + write(clients[n], "HTTP/1.0 400 Bad Request\n", 25); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + if ( strncmp(reqline[1], "/\0", 2)==0 ) |
| 166 | + reqline[1] = "/index.html"; //Because if no file is specified, index.html will be opened by default (like it happens in APACHE... |
| 167 | + |
| 168 | + strcpy(path, ROOT); |
| 169 | + strcpy(&path[strlen(ROOT)], reqline[1]); |
| 170 | + printf("file: %s\n", path); |
| 171 | + |
| 172 | + if ( (fd=open(path, O_RDONLY))!=-1 ) //FILE FOUND |
| 173 | + { |
| 174 | + send(clients[n], "HTTP/1.0 200 OK\n\n", 17, 0); |
| 175 | + while ( (bytes_read=read(fd, data_to_send, BYTES))>0 ) |
| 176 | + write (clients[n], data_to_send, bytes_read); |
| 177 | + } |
| 178 | + else write(clients[n], "HTTP/1.0 404 Not Found\n", 23); //FILE NOT FOUND |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + //Closing SOCKET |
| 184 | + shutdown (clients[n], SHUT_RDWR); //All further send and recieve operations are DISABLED... |
| 185 | + close(clients[n]); |
| 186 | + clients[n]=-1; |
| 187 | +} |
0 commit comments