├── README.markdown ├── minhttp-80x24.c ├── minhttp-commented.c └── minhttp.c /README.markdown: -------------------------------------------------------------------------------- 1 | minHTTP 2 | ------- 3 | 4 | A tiny and surprisingly featureful webserver. 5 | 6 | 7 | ### minhttp.c 8 | 9 | The full version, containing some documentation and configuration 10 | instructions. Builds as a single file with gcc or any other C 11 | compiler. 12 | 13 | gcc minhttp.c -o minhttp 14 | ./minhttp 15 | 16 | ### minhttp-80x24.c 17 | 18 | Smaller version to fit in an 80x24 terminal. Still has the same 19 | features, but reconfiguring is a little fiddly. You'll need to consult 20 | minttp.c to find out what the config variables mean, and the change 21 | the -DVAR=foo as desired. There are no documentation comments in this 22 | version and it requires a trickier set of compiler flags to make it 23 | compile. 24 | 25 | Luckily, the program is also a valid shell script which will correctly 26 | compile itself and then run. 27 | 28 | ./minhttp-80x24.c 29 | -------------------------------------------------------------------------------- /minhttp-80x24.c: -------------------------------------------------------------------------------- 1 | /*bin/true;sed -n 's/.*.\/\*\(.*\)../\1/p' "$0"|I="$0" sh;exit;[minHTTP 80x24]*/ 2 | #define T(k)(i=(x[k]|32)-48,i-=39*(i>9),i)/*echo gcc -includesys/sendfile,sys\*/ 3 | #define I(x,y);if(d,x){y;close(c);goto g;}/*/socket,netinet/in,sys/time,sys/s\*/ 4 | #define Y(f)(close(d),d=open(f,O_RDONLY))*tat,unistd,stdlib,string,dirent,s\*/ 5 | #define O(o)setsockopt(c,L,o,&i,sizeof(i))/*ignal,fcntl,stdio.h -DN=3 -DP=800\*/ 6 | #define S(f,a)write(i=c,r,sprintf(r,f,a));/*0 -DE=0 -DC=0 -DV=0 -DU=0 -om "$I\*/ 7 | #define H()"\r\nConnection: close\r\n\r\n"/*" $C|sed s/,/.h\ -include/g|sh;./m*/ 8 | #define R(c,h,d,a)S("HTTP/1.1 "c h H()d,a)/*#See full version for docs/config#*/ 9 | int main(){enum{Z=AF_INET,L=SOL_SOCKET,D=SO_RCVTIMEO,F=SIGPIPE,G=SO_REUSEADDR,X= 10 | 9001};struct dirent*w;int i=1,s,d=-1,c=socket(Z,SOCK_STREAM,0);char*x,*y,*h,q[X] 11 | ={0},M[]="Content-type:text/html;charset=UTF-8",f[X],r[X];struct sockaddr_in a={ 12 | Z};struct stat t;void*A=&a;a.sin_port=htons(P);O(G);if(C&&chroot(C)||bind(s=c,A, 13 | sizeof(a))||listen(s,5)||U&&setuid(U))perror(0),exit(1);signal(F,SIG_IGN);for(i= 14 | N;fork()&&--i;);i||pause();g:c=accept(s,A,(void*)q);{struct timeval i={5};O(D);O 15 | (SO_SNDTIMEO);}I(read(c,q,X/2)<0||strncmp(q,"GET /",5),;)for(y=x=q+4;*x&~32;)if( 16 | *x-37)*y++=*x++;else{*y=T(1)<<4;*y+++=T(2);x+=3;}*y=0;if((h=strstr(x+1,"\nHost:" 17 | ))&&(x=strchr(h+=7,13)))*x=0;h=h?h:"";sprintf(f,"/%s%s",h,x=q+4);i=y[-1]-47;if(Y 18 | (y=V?f:x)0){R("404 Not Found\r\n","%s","",M)I(Y(E)0,S("