NAME ^

examples/io/httpd.pir - HTTP server

SYNOPSIS ^

  $ ./parrot examples/io/httpd.pir

DESCRIPTION ^

A very tiny HTTP-Server. It currently only understands the GET method. It's a nice way of testing pretty much all IO functions. By default (and not yet configurable) it binds to localhost:1234.

Serving Parrot Docs ^

If no filename is given it serves the HTML documentation in ./docs/html. Make sure you have built them with

  $ make html

After that you can browse the documentation with

  http://localhost:1234

which redirects to

  http://localhost:1234/docs/html/index.html

Serving Other HTML Files ^

If a html file is present in the request, this file will be served:

  http://localhost:1234/index.html

This will sent ./index.html from the directory, where httpd.pir was started.

CGI ^

If the file extension is .pir or .pbc, this file will be loaded below the directory cgi-pir and the function cgi_main will be invoked with the query as an argument. This functions should return a plain string, which will be sent to the browser.

cgi_main is called with 3 arguments: a todo/reserved PMC, a string with the original query and a Hash, with key=value items split by '+'. key and value are already urldecoded.

  $ cat cgi-pir/foo.pir
  .sub cgi_main
    .param pmc reserved         # TODO
    .param string query         # all after '?':  "foo=1+bar=A"
    .param pmc query_hash       # Hash { foo=>'1', bar=>'A' }
    .return ("<p>foo</p>")      # in practice use a full <html>doc</html>
                                # unless serving XMLHttpRequest's
  .end

The browser request:

  http://localhost:1234/foo.pir?foo=1+bar=%61

will serve, whatever the cgi_main function returned.

TODO ^

make it work on W32/IE

Transcode the received string to ascii, in order to have access to an implemented 'index' op. Or just use unicode instead.

SEE ALSO ^

RFC2616

AUTHOR ^

Original author is Markus Amsler - <markus.amsler@oribi.org> The code was heavily hacked by bernhard and leo.


parrot