pike.git / lib / modules / Tools.pmod / Standalone.pmod / httpserver.pike

version» Context lines:

pike.git/lib/modules/Tools.pmod/Standalone.pmod/httpserver.pike:11:    inherit Arg.Options;       constant help_pre = #"Usage: httpserver [flags] [path]   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   the world without any authentication.   ";       constant port_help = "Port to use. Defaults to 8080.";    constant version_help = "Displays version information."; +  constant headers_help = "Set additional header and value, e.g. --header X-Content-Type-Options:nosniff";       Opt port = Int(HasOpt("--port")|Default(8080));    Opt version = NoOpt("--version"); -  +  Opt headers = Multiple(HasOpt("--header"));   }      Options opt; -  + mapping headers = ([]);      int main(int argc, array(string) argv)   {    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];    }    -  +  if( opt->headers ) +  { +  foreach(opt->headers, string hdr) +  { +  array h = hdr/":"; +  if(sizeof(h)<2) error("Illegal header format %O.\n", hdr); +  headers[h[0]] = h[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;   }      string dirlist( string dir )   {    string res =    "<html><head>\n"
pike.git/lib/modules/Tools.pmod/Standalone.pmod/httpserver.pike:92:    "<tt>" + Parser.encode_html_entities(fname) + "</tt><br />\n"    "</body></html>\n";   }      void handle_request(Protocols.HTTP.Server.Request request)   {    string file = "."+combine_path("/",request->not_query);    file = Protocols.HTTP.uri_decode(file);    Stdio.Stat s = file_stat( file );    -  mapping hdr = ([]); -  +     if( !s )    request->response_and_finish( (["data":    file_not_found(request->not_query),    "type":"text/html", -  "extra_heads" : hdr, +  "extra_heads" : headers,    "error":404]) );    else if( s->isdir )    request->response_and_finish( ([ "data":dirlist(file), -  "extra_heads" : hdr, +  "extra_heads" : headers,    "type":"text/html" ]) );    else    request->response_and_finish( ([ "file":Stdio.File(file), -  "extra_heads" : hdr, +  "extra_heads" : headers,    ]) );   }