3db9192009-08-20Peter Bortas #pike __REAL_VERSION__ //#pragma strict_types constant doc = #"Usage: httpserver [flags] [port] Starts a simple HTTP server on port 8080 unless another port is specified. The server will present the contents of the current directory and it's children to
8921dc2009-08-20Henrik Grubbström (Grubba) the world without any authentication.
3db9192009-08-20Peter Bortas  --version print version information and exit --help display this help and exit "; constant version = sprintf(#"Pike httpserver %d.%d.%d ",(int)__REAL_VERSION__,__REAL_MINOR__,__REAL_BUILD__);
833a172009-08-20Henrik Grubbström (Grubba) constant description = "Minimal HTTP-server.";
3db9192009-08-20Peter Bortas int main(int argc, array(string) argv) { int my_port = 8080; if(argc>1) switch (argv[-1]) { case "--version": exit(0, version); case "--help": exit(0, doc); default:
6b91762016-03-04Martin Nilsson  string home = combine_path(getcwd(), argv[-1]); if( Stdio.is_dir(home) ) cd(home); else
3db9192009-08-20Peter Bortas  my_port=(int)argv[-1]; }
df03b02015-03-16Martin Nilsson 
00c9702015-03-24Martin Nilsson  Protocols.HTTP.Server.Port(handle_request, my_port, NetUtils.ANY);
5f82d72009-08-21Martin Nilsson  write("%s is now accessible on port %d through http, " "without password.\n", getcwd(), my_port);
3db9192009-08-20Peter Bortas  return -1; } string dirlist( string dir ) {
3524712015-05-26Martin Nilsson  string res =
8921dc2009-08-20Henrik Grubbström (Grubba)  "<html><head>\n"
3db9192009-08-20Peter Bortas  "<style>a { text-decoration: none; }\n"
8921dc2009-08-20Henrik Grubbström (Grubba)  ".odd { background-color:#efefef; }\n" ".even { background-color:#fefefe; }\n" "</style>\n" "</head><body>\n" "<h1>"+Parser.encode_html_entities(dir[2..])+"</h1>" "<table cellspacing='0' cellpadding='2'>\n" "<tr><th align='left'>Filename</th>" "<th align='right'>Type</th>" "<th align='right'>Size</th></tr>\n";
3db9192009-08-20Peter Bortas 
8973902013-03-10Chris Angelico  foreach( sort( get_dir( dir ) ); int i; string fn )
3db9192009-08-20Peter Bortas  { Stdio.Stat s = file_stat( combine_path(dir, fn) );
3524712015-05-26Martin Nilsson  if( !s )
3db9192009-08-20Peter Bortas  continue; string t = s->isdir?"":Protocols.HTTP.Server.filename_to_type(fn); if( t == "application/octet-stream" ) t = "<span style='color:darkgrey'>unknown</span>";
3524712015-05-26Martin Nilsson  res +=
8921dc2009-08-20Henrik Grubbström (Grubba)  sprintf("<tr class='%s'><td><a href='%s%s'>%s%[2]s</a></td>" "<td align='right'>%s</td>"
3524712015-05-26Martin Nilsson  "<td align='right'>%s</td></tr>\n",
3db9192009-08-20Peter Bortas  (i&1?"odd":"even"),
3524712015-05-26Martin Nilsson  Protocols.HTTP.uri_encode(fn), s->isdir?"/":"",
dd167f2010-05-09Peter Bortas  Parser.encode_html_entities(fn), t,
3db9192009-08-20Peter Bortas  s->isdir?"":String.int2size(s->size)); }
8921dc2009-08-20Henrik Grubbström (Grubba)  return res+"</table></body></html>\n"; } string file_not_found(string fname) { return "<html><body><h1>File not found</h1>\n" "<tt>" + Parser.encode_html_entities(fname) + "</tt><br />\n" "</body></html>\n";
3db9192009-08-20Peter Bortas } void handle_request(Protocols.HTTP.Server.Request request) { string file = "."+combine_path("/",request->not_query);
dd167f2010-05-09Peter Bortas  file = Protocols.HTTP.uri_decode(file);
3db9192009-08-20Peter Bortas  Stdio.Stat s = file_stat( file ); if( !s )
8921dc2009-08-20Henrik Grubbström (Grubba)  request->response_and_finish( (["data": file_not_found(request->not_query), "type":"text/html", "error":404]) );
3db9192009-08-20Peter Bortas  else if( s->isdir )
8921dc2009-08-20Henrik Grubbström (Grubba)  request->response_and_finish( ([ "data":dirlist(file), "type":"text/html" ]) );
3db9192009-08-20Peter Bortas  else
6c27022016-03-23Martin Nilsson  request->response_and_finish( ([ "file":Stdio.File(file) ]) );
3db9192009-08-20Peter Bortas }