NullLogic

This is a basic single-threaded HTTP web server.

#!/usr/bin/nsp function serve_sendfile(sock, requested_file) { f=file.readall(requested_file); if (typeof(f)=='string') sock.write(f); return; } function serve_dirlist(sock, requested_file) { if (typeof(dirlist)!='function') throw "dirlist() not found"; dir=dirlist(requested_file); if (typeof(dir)!='table') return; sock.write("<CENTER><TABLE BORDER=1 CELLPADDING=1 CELLSPACING=0 WIDTH='80%'>\n"); foreach (n,v in dir) { if ((v.type!='dir')&&(v.type!='dirp')) continue; if (string.cmp(v.name, '.')==0) { continue; } else if (string.cmp(v.name, '..')==0) { sock.write("<TR><TD COLSPAN=3 STYLE='border-style:solid'> <A HREF='"+v.name+"'>Parent Directory</A></TD></TR>\n"); continue; } sock.write( "<TR>"+ "<TD ALIGN=left NOWRAP STYLE='border-style:solid' WIDTH='66%'>dir - <A HREF='"+v.name+"/'>"+v.name+"/</A></TD>"+ "<TD ALIGN=right NOWRAP STYLE='border-style:solid'> </TD>"+ "<TD ALIGN=right NOWRAP STYLE='border-style:solid'>"+time.sqldate(v.mtime)+" "+time.sqltime(v.mtime)+"</TD>"+ "</TR>\n" ); } io.flush(); foreach (n,v in dir) { if ((v.type!='file')&&(v.type!='filep')) continue; sock.write( "<TR>"+ "<TD ALIGN=left NOWRAP STYLE='border-style:solid' WIDTH='66%'>file - <A HREF='"+v.name+"'>"+v.name+"</A></TD>"+ "<TD ALIGN=right NOWRAP STYLE='border-style:solid'>"+v.size+" </TD>"+ "<TD ALIGN=right NOWRAP STYLE='border-style:solid'>"+time.sqldate(v.mtime)+" "+time.sqltime(v.mtime)+"</TD>"+ "</TR>\n" ); } sock.write("</TABLE></CENTER>\n"); return; } function serve_page(sock) { global _SERVER = {}; if (counter==null) global counter=0; print("."); i=sock.gets(); if (typeof(i)!='string') return -1; if (i=="") return -1; r=string.split(i, " "); _SERVER['REQUEST_METHOD'] = r[0]; _SERVER['REQUEST_URI'] = r[1]; _SERVER['SERVER_PROTOCOL'] = r[2]; if (_SERVER['REQUEST_URI']=="/favicon.ico") { sock.write("HTTP/1.0 404 Not Found\r\n"); sock.write("Connection: Close\r\n\r\n"); sock.write("fake 404 for favicon.ico\r\n"); return -1; } while (1) { i=sock.gets(); //printf("[%s]\r\n", i);io.flush(); if (typeof(i)!='string') break; if (i=="") break; x=string.str(i, ":"); if (x==null) break; p=string.sub(i, 0, sizeof(i)-sizeof(x)); c=string.sub(x, 2, sizeof(x)); _SERVER[string.toupper(p)]=c; } sort.byname(_SERVER, 0, +1); page="<HTML><HEAD><TITLE>NESLA *COUGH* HTTPD</TITLE></HEAD><BODY><CENTER>THIS IS NOT A REAL WEB SERVER<HR>BTW. You are visitor "+(++counter)+".</CENTER>"; pagef="</BODY></HTML>"; sock.write("HTTP/1.0 200 OK\r\n"); sock.write("Connection: Close\r\n"); requested_file=pageroot+_SERVER['REQUEST_URI']; if (string.sub(_ostype_, 0, 7)=='Windows') { requested_file=string.replace(requested_file, '/', '\\'); if (string.sub(requested_file, -1, 1)=='\\') requested_file=string.sub(requested_file, 0, string.len(requested_file)-1); } x=file.stat(requested_file); if (typeof(x)=='table') { if (x.type=='file') { sock.write("Content-Length: "+x.size+"\r\n"); sock.write("Content-Type: application/octet-stream\r\n\r\n"); serve_sendfile(sock, requested_file); } else if (x.type=='dir') { sock.write("Content-Type: text/html\r\n\r\n"); sock.write(page); serve_dirlist(sock, requested_file); sock.write(pagef); } } else { sock.write("Content-Type: text/html\r\n\r\n"); sock.write(page); sock.write(pagef); } io.flush(); return; } try { dl.load("net"); if (string.sub(_ostype_, 0, 7)=='Windows') { global pageroot="c:\\x"; } else { global pageroot="/var/www/html"; } host="INADDR_ANY"; use_ssl=true; if (typeof(net.tcp.socket)!='function') throw "net.tcp.socket not found"; bsock=new net.tcp.socket(); if (use_ssl) { port=8042; certs = { certfile='/etc/nullgw/ssl-cert.pem', keyfile='/etc/nullgw/ssl-priv.pem' }; print("binding to ",host,":",port," (SSL)\n"); bsock.bind(host, port, use_ssl, certs); } else { port=8041; print("binding to ",host,":",port,"\n"); bsock.bind(host, port); } if (typeof(bsock.socket)!='sock4') throw "bind failure\n"; if (win.ShellExecute) { win.ShellExecute("http"+(use_ssl?"s":"")+"://localhost:"+port); } while (1) { io.flush(); asock=bsock.accept(); if (typeof(asock.socket)=='sock4') { //asock.setsockopt('SO_KEEPALIVE', false); //asock.setsockopt('SO_RCVTIMEO', 10000); serve_page(asock); asock.close(); delete asock; } } bsock.close(); } catch (ex) { printf("Exception: %s\r\n", ex.description); }