Text based web server.
A little http web server to play with. Might be perfect for an embedded system. What the http client looks like,
$ elinks http://localhost:10000
Update:
Using graphics via Firefox:
What the server side looks like.
$ ./httpsrvr
Server started at port no. 10000 with root directory as /home/eddie
GET / HTTP/1.1
Host: localhost:10000
User-Agent: ELinks/0.12~pre5-7ubuntu1 (textmode; Ubuntu; Linux 3.2.0-30-generic i686; 80x24-2)
Accept: */*
Accept-Language: en
Connection: Keep-Alive
file: /home/eddie/index.html
With graphics:
$ code/httpsrvr
Server started at port no. 10000 with root directory as /home/eddie
GET / HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
file: /home/eddie/index.html
GET /testpic.png HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://localhost:10000/
Connection: keep-alive
file: /home/eddie/testpic.png
GET /heaven.png HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://localhost:10000/
Connection: keep-alive
file: /home/eddie/heaven.png
GET /favicon.ico HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
file: /home/eddie/favicon.ico
GET /favicon.ico HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
file: /home/eddie/favicon.ico
Required files:
index.html
[code]
<html>
<body>
+------------------------+
<br />
/......................../|
<br />
+------------------------|.|
<br />
+........Testpage........| /
<br />
+------------------------|/
<br />
</body>
</html>
[/code]
httpsrvr.c (gcc httpsrvr.c -o httpsrvr)
[code]
$ elinks http://localhost:10000
Update:
Using graphics via Firefox:
What the server side looks like.
$ ./httpsrvr
Server started at port no. 10000 with root directory as /home/eddie
GET / HTTP/1.1
Host: localhost:10000
User-Agent: ELinks/0.12~pre5-7ubuntu1 (textmode; Ubuntu; Linux 3.2.0-30-generic i686; 80x24-2)
Accept: */*
Accept-Language: en
Connection: Keep-Alive
file: /home/eddie/index.html
With graphics:
$ code/httpsrvr
Server started at port no. 10000 with root directory as /home/eddie
GET / HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
file: /home/eddie/index.html
GET /testpic.png HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://localhost:10000/
Connection: keep-alive
file: /home/eddie/testpic.png
GET /heaven.png HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://localhost:10000/
Connection: keep-alive
file: /home/eddie/heaven.png
GET /favicon.ico HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
file: /home/eddie/favicon.ico
GET /favicon.ico HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
file: /home/eddie/favicon.ico
Required files:
index.html
[code]
<html>
<body>
+------------------------+
<br />
/......................../|
<br />
+------------------------|.|
<br />
+........Testpage........| /
<br />
+------------------------|/
<br />
</body>
</html>
[/code]
httpsrvr.c (gcc httpsrvr.c -o httpsrvr)
[code]
/* This is a very simple HTTP server. Default port is 10000 and ROOT for the server is your current working directory.. You can provide command line arguments like:- $./httpsrvr -p [port] -r [path] for ex. $./httpsrvr -p 50000 -r /home/ to start a server at port 50000 with root directory as "/home" $./httpsrvr -r /home/eddie starts the server at port 10000 with ROOT as /home/eddie
*/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netdb.h> #include<signal.h> #include<fcntl.h> #define CONNMAX 1000 #define BYTES 1024 char *ROOT; int listenfd, clients[CONNMAX]; void error(char *); void startServer(char *); void respond(int); int main(int argc, char* argv[]) { struct sockaddr_in clientaddr; socklen_t addrlen; char c; http://computoman.blogspot.com/2014/09/frico.html //Default Values PATH = ~/ and PORT=10000 char PORT[6]; ROOT = getenv("PWD"); strcpy(PORT,"10000"); int slot=0; //Parsing the command line arguments while ((c = getopt (argc, argv, "p:r:")) != -1) switch (c) { case 'r': ROOT = malloc(strlen(optarg)); strcpy(ROOT,optarg); break; case 'p': strcpy(PORT,optarg); break; case '?': fprintf(stderr,"Wrong arguments given!!!\n"); exit(1); default: exit(1); } 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"); // Setting all elements to -1: signifies there is no client connected int i; for (i=0; i<CONNMAX; i++) clients[i]=-1; startServer(PORT); // ACCEPT connections while (1) { addrlen = sizeof(clientaddr); clients[slot] = accept (listenfd, (struct sockaddr *) &clientaddr, &addrlen); if (clients[slot]<0) error ("accept() error"); else { if ( fork()==0 ) { respond(slot); exit(0); } } while (clients[slot]!=-1) slot = (slot+1)%CONNMAX; } return 0; } //start server void startServer(char *port) { struct addrinfo hints, *res, *p; // getaddrinfo for host memset (&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if (getaddrinfo( NULL, port, &hints, &res) != 0) { perror ("getaddrinfo() error"); exit(1); } // socket and bind for (p = res; p!=NULL; p=p->ai_next) { listenfd = socket (p->ai_family, p->ai_socktype, 0); if (listenfd == -1) continue; if (bind(listenfd, p->ai_addr, p->ai_addrlen) == 0) break; } if (p==NULL) { perror ("socket() or bind()"); exit(1); } freeaddrinfo(res); // listen for incoming connections if ( listen (listenfd, 1000000) != 0 ) { perror("listen() error"); exit(1); } } [/code]
Comments
Post a Comment