3db919 | 2009-08-20 | Peter Bortas | | #pike __REAL_VERSION__
|
a7b80c | 2017-01-21 | Martin Nilsson | | constant version = sprintf(#"Pike httpserver %d.%d.%d
",(int)__REAL_VERSION__,__REAL_MINOR__,__REAL_BUILD__);
constant description = "Minimal HTTP-server.";
class Options
{
inherit Arg.Options;
constant help_pre = #"Usage: httpserver [flags] [path]
|
3db919 | 2009-08-20 | Peter Bortas | | 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
|
8921dc | 2009-08-20 | Henrik Grubbström (Grubba) | | the world without any authentication.
|
3db919 | 2009-08-20 | Peter Bortas | | ";
|
a7b80c | 2017-01-21 | Martin Nilsson | | constant port_help = "Port to use. Defaults to 8080.";
constant version_help = "Displays version information.";
|
3db919 | 2009-08-20 | Peter Bortas | |
|
a7b80c | 2017-01-21 | Martin Nilsson | | Opt port = Int(HasOpt("--port")|Default(8080));
Opt version = NoOpt("--version");
}
Options opt;
|
833a17 | 2009-08-20 | Henrik Grubbström (Grubba) | |
|
3db919 | 2009-08-20 | Peter Bortas | | int main(int argc, array(string) argv)
{
|
a7b80c | 2017-01-21 | Martin Nilsson | | opt = Options(argv);
if(opt->version)
exit(0, version);
int port = opt->port;
if(sizeof(argv=opt[Arg.REST]))
{
string home = combine_path(getcwd(), argv[-1]);
if( Stdio.is_dir(home) )
cd(home);
else if(port==8080 && (int)argv[-1])
port=(int)argv[-1];
}
Protocols.HTTP.Server.Port(handle_request, port, NetUtils.ANY);
write("%s is now accessible on port %d through http, "
"without password.\n", getcwd(), port);
return -1;
|
3db919 | 2009-08-20 | Peter Bortas | | }
string dirlist( string dir )
{
|
352471 | 2015-05-26 | Martin Nilsson | | string res =
|
8921dc | 2009-08-20 | Henrik Grubbström (Grubba) | | "<html><head>\n"
|
3db919 | 2009-08-20 | Peter Bortas | | "<style>a { text-decoration: none; }\n"
|
8921dc | 2009-08-20 | Henrik 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";
|
3db919 | 2009-08-20 | Peter Bortas | |
|
897390 | 2013-03-10 | Chris Angelico | | foreach( sort( get_dir( dir ) ); int i; string fn )
|
3db919 | 2009-08-20 | Peter Bortas | | {
Stdio.Stat s = file_stat( combine_path(dir, fn) );
|
352471 | 2015-05-26 | Martin Nilsson | | if( !s )
|
3db919 | 2009-08-20 | Peter 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>";
|
352471 | 2015-05-26 | Martin Nilsson | | res +=
|
8921dc | 2009-08-20 | Henrik Grubbström (Grubba) | | sprintf("<tr class='%s'><td><a href='%s%s'>%s%[2]s</a></td>"
"<td align='right'>%s</td>"
|
352471 | 2015-05-26 | Martin Nilsson | | "<td align='right'>%s</td></tr>\n",
|
3db919 | 2009-08-20 | Peter Bortas | | (i&1?"odd":"even"),
|
352471 | 2015-05-26 | Martin Nilsson | | Protocols.HTTP.uri_encode(fn), s->isdir?"/":"",
|
dd167f | 2010-05-09 | Peter Bortas | | Parser.encode_html_entities(fn), t,
|
3db919 | 2009-08-20 | Peter Bortas | | s->isdir?"":String.int2size(s->size));
}
|
8921dc | 2009-08-20 | Henrik 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";
|
3db919 | 2009-08-20 | Peter Bortas | | }
void handle_request(Protocols.HTTP.Server.Request request)
{
string file = "."+combine_path("/",request->not_query);
|
dd167f | 2010-05-09 | Peter Bortas | | file = Protocols.HTTP.uri_decode(file);
|
3db919 | 2009-08-20 | Peter Bortas | | Stdio.Stat s = file_stat( file );
|
b23302 | 2017-01-21 | Martin Nilsson | |
mapping hdr = ([]);
|
3db919 | 2009-08-20 | Peter Bortas | | if( !s )
|
8921dc | 2009-08-20 | Henrik Grubbström (Grubba) | | request->response_and_finish( (["data":
file_not_found(request->not_query),
"type":"text/html",
|
b23302 | 2017-01-21 | Martin Nilsson | | "extra_heads" : hdr,
"error":404]) );
|
3db919 | 2009-08-20 | Peter Bortas | | else if( s->isdir )
|
8921dc | 2009-08-20 | Henrik Grubbström (Grubba) | | request->response_and_finish( ([ "data":dirlist(file),
|
b23302 | 2017-01-21 | Martin Nilsson | | "extra_heads" : hdr,
"type":"text/html" ]) );
|
3db919 | 2009-08-20 | Peter Bortas | | else
|
b23302 | 2017-01-21 | Martin Nilsson | | request->response_and_finish( ([ "file":Stdio.File(file),
"extra_heads" : hdr,
]) );
|
3db919 | 2009-08-20 | Peter Bortas | | }
|