{
    "content": [
        {
            "type": "text",
            "text": "# HTTP::Server::Simple (perldoc)\n\n## NAME\n\nHTTP::Server::Simple - Lightweight HTTP server\n\n## SYNOPSIS\n\nuse warnings;\nuse strict;\nuse HTTP::Server::Simple;\nmy $server = HTTP::Server::Simple->new();\n$server->run();\nHowever, normally you will sub-class the HTTP::Server::Simple::CGI module (see\nHTTP::Server::Simple::CGI);\npackage Your::Web::Server;\nuse base qw(HTTP::Server::Simple::CGI);\nsub handlerequest {\nmy ($self, $cgi) = @;\n#... do something, print output to default\n# selected filehandle...\n}\n1;\n\n## DESCRIPTION\n\nThis is a simple standalone HTTP server. By default, it doesn't thread or fork. It does,\nhowever, act as a simple frontend which can be used to build a standalone web-based application\nor turn a CGI into one.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **EXAMPLE**\n- **METHODS**\n- **IMPORTANT SUB-CLASS METHODS**\n- **AUTHOR**\n- **CONTRIBUTORS**\n- **BUGS**\n- **LICENSE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "HTTP::Server::Simple",
        "section": "",
        "mode": "perldoc",
        "summary": "HTTP::Server::Simple - Lightweight HTTP server",
        "synopsis": "use warnings;\nuse strict;\nuse HTTP::Server::Simple;\nmy $server = HTTP::Server::Simple->new();\n$server->run();\nHowever, normally you will sub-class the HTTP::Server::Simple::CGI module (see\nHTTP::Server::Simple::CGI);\npackage Your::Web::Server;\nuse base qw(HTTP::Server::Simple::CGI);\nsub handlerequest {\nmy ($self, $cgi) = @;\n#... do something, print output to default\n# selected filehandle...\n}\n1;",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "#!/usr/bin/perl",
            "package MyWebServer;",
            "use HTTP::Server::Simple::CGI;",
            "use base qw(HTTP::Server::Simple::CGI);",
            "my %dispatch = (",
            "'/hello' => \\&resphello,",
            "# ...",
            ");",
            "sub handlerequest {",
            "my $self = shift;",
            "my $cgi  = shift;",
            "my $path = $cgi->pathinfo();",
            "my $handler = $dispatch{$path};",
            "if (ref($handler) eq \"CODE\") {",
            "print \"HTTP/1.0 200 OK\\r\\n\";",
            "$handler->($cgi);",
            "} else {",
            "print \"HTTP/1.0 404 Not found\\r\\n\";",
            "print $cgi->header,",
            "$cgi->starthtml('Not found'),",
            "$cgi->h1('Not found'),",
            "$cgi->endhtml;",
            "sub resphello {",
            "my $cgi  = shift;   # CGI.pm object",
            "return if !ref $cgi;",
            "my $who = $cgi->param('name');",
            "print $cgi->header,",
            "$cgi->starthtml(\"Hello\"),",
            "$cgi->h1(\"Hello $who!\"),",
            "$cgi->endhtml;",
            "# start the server on port 8080",
            "my $pid = MyWebServer->new(8080)->background();",
            "print \"Use 'kill $pid' to stop server.\\n\";"
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 24,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 20,
                "subsections": []
            },
            {
                "name": "EXAMPLE",
                "lines": 50,
                "subsections": []
            },
            {
                "name": "METHODS",
                "lines": 57,
                "subsections": []
            },
            {
                "name": "IMPORTANT SUB-CLASS METHODS",
                "lines": 98,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "CONTRIBUTORS",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "BUGS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "LICENSE",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "HTTP::Server::Simple - Lightweight HTTP server\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "use warnings;\nuse strict;\n\nuse HTTP::Server::Simple;\n\nmy $server = HTTP::Server::Simple->new();\n$server->run();\n\nHowever, normally you will sub-class the HTTP::Server::Simple::CGI module (see\nHTTP::Server::Simple::CGI);\n\npackage Your::Web::Server;\nuse base qw(HTTP::Server::Simple::CGI);\n\nsub handlerequest {\nmy ($self, $cgi) = @;\n\n#... do something, print output to default\n# selected filehandle...\n\n}\n\n1;\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This is a simple standalone HTTP server. By default, it doesn't thread or fork. It does,\nhowever, act as a simple frontend which can be used to build a standalone web-based application\nor turn a CGI into one.\n\nIt is possible to use Net::Server classes to create forking, pre-forking, and other types of\nmore complicated servers; see \"netserver\".\n\nBy default, the server traps a few signals:\n\nHUP When you \"kill -HUP\" the server, it lets the current request finish being processed, then\nuses the \"restart\" method to re-exec itself. Please note that in order to provide\nrestart-on-SIGHUP, HTTP::Server::Simple sets a SIGHUP handler during initialisation. If your\nrequest handling code forks you need to make sure you reset this or unexpected things will\nhappen if somebody sends a HUP to all running processes spawned by your app (e.g. by \"kill\n-HUP <script>\")\n\nPIPE\nIf the server detects a broken pipe while writing output to the client, it ignores the\nsignal. Otherwise, a client closing the connection early could kill the server.\n",
                "subsections": []
            },
            "EXAMPLE": {
                "content": "#!/usr/bin/perl\n{\npackage MyWebServer;\n\nuse HTTP::Server::Simple::CGI;\nuse base qw(HTTP::Server::Simple::CGI);\n\nmy %dispatch = (\n'/hello' => \\&resphello,\n# ...\n);\n\nsub handlerequest {\nmy $self = shift;\nmy $cgi  = shift;\n\nmy $path = $cgi->pathinfo();\nmy $handler = $dispatch{$path};\n\nif (ref($handler) eq \"CODE\") {\nprint \"HTTP/1.0 200 OK\\r\\n\";\n$handler->($cgi);\n\n} else {\nprint \"HTTP/1.0 404 Not found\\r\\n\";\nprint $cgi->header,\n$cgi->starthtml('Not found'),\n$cgi->h1('Not found'),\n$cgi->endhtml;\n}\n}\n\nsub resphello {\nmy $cgi  = shift;   # CGI.pm object\nreturn if !ref $cgi;\n\nmy $who = $cgi->param('name');\n\nprint $cgi->header,\n$cgi->starthtml(\"Hello\"),\n$cgi->h1(\"Hello $who!\"),\n$cgi->endhtml;\n}\n\n}\n\n# start the server on port 8080\nmy $pid = MyWebServer->new(8080)->background();\nprint \"Use 'kill $pid' to stop server.\\n\";\n",
                "subsections": []
            },
            "METHODS": {
                "content": "HTTP::Server::Simple->new($port, $family)\nAPI call to start a new server. Does not actually start listening until you call \"->run()\". If\nomitted, $port defaults to 8080, and $family defaults to Socket::AFINET. The alternative domain\nis Socket::AFINET6.\n\nlookuplocalhost\nLooks up the local host's IP address, and returns it. For most hosts, this is 127.0.0.1, or\npossibly \"::1\".\n\nport [NUMBER]\nTakes an optional port number for this server to listen on.\n\nReturns this server's port. (Defaults to 8080)\n\nfamily [NUMBER]\nTakes an optional address family for this server to use. Valid values are Socket::AFINET and\nSocket::AFINET6. All other values are silently changed into Socket::AFINET for backwards\ncompatibility with previous versions of the module.\n\nReturns the address family of the present listening socket. (Defaults to Socket::AFINET.)\n\nhost [address]\nTakes an optional host address for this server to bind to.\n\nReturns this server's bound address (if any). Defaults to \"undef\" (bind to all interfaces).\n\nbackground [ARGUMENTS]\nRuns the server in the background, and returns the process ID of the started process. Any\narguments will be passed through to \"run\".\n\nrun [ARGUMENTS]\nRun the server. If all goes well, this won't ever return, but it will start listening for \"HTTP\"\nrequests. Any arguments passed to this will be passed on to the underlying Net::Server\nimplementation, if one is used (see \"netserver\").\n\nnetserver\nUser-overridable method. If you set it to a Net::Server subclass, that subclass is used for the\n\"run\" method. Otherwise, a minimal implementation is used as default.\n\nrestart\nRestarts the server. Usually called by a HUP signal, not directly.\n\nstdiohandle [FILEHANDLE]\nWhen called with an argument, sets the socket to the server to that arg.\n\nReturns the socket to the server; you should only use this for actual socket-related calls like\n\"getsockname\". If all you want is to read or write to the socket, you should use \"stdinhandle\"\nand \"stdouthandle\" to get the in and out filehandles explicitly.\n\nstdinhandle\nReturns a filehandle used for input from the client. By default, returns whatever was set with\n\"stdiohandle\", but a subclass could do something interesting here.\n\nstdouthandle\nReturns a filehandle used for output to the client. By default, returns whatever was set with\n\"stdiohandle\", but a subclass could do something interesting here.\n",
                "subsections": []
            },
            "IMPORTANT SUB-CLASS METHODS": {
                "content": "A selection of these methods should be provided by sub-classes of this module.\n\nhandler\nThis method is called after setup, with no parameters. It should print a valid, *full* HTTP\nresponse to the default selected filehandle.\n\nsetup(name => $value, ...)\nThis method is called with a name => value list of various things to do with the request. This\nlist is given below.\n\nThe default setup handler simply tries to call methods with the names of keys of this list.\n\nITEM/METHOD   Set to                Example\n-----------  ------------------    ------------------------\nmethod       Request Method        \"GET\", \"POST\", \"HEAD\"\nprotocol     HTTP version          \"HTTP/1.1\"\nrequesturi  Complete Request URI  \"/foobar/baz?foo=bar\"\npath         Path part of URI      \"/foobar/baz\"\nquerystring Query String          undef, \"foo=bar\"\nport         Received Port         80, 8080\npeername     Remote name           \"200.2.4.5\", \"foo.com\"\npeeraddr     Remote address        \"200.2.4.5\", \"::1\"\npeerport     Remote port           42424\nlocalname    Local interface       \"localhost\", \"myhost.com\"\n\nheaders([Header => $value, ...])\nReceives HTTP headers and does something useful with them. This is called by the default\n\"setup()\" method.\n\nYou have lots of options when it comes to how you receive headers.\n\nYou can, if you really want, define \"parseheaders()\" and parse them raw yourself.\n\nSecondly, you can intercept them very slightly cooked via the \"setup()\" method, above.\n\nThirdly, you can leave the \"setup()\" header as-is (or calling the superclass \"setup()\" for\nunknown request items). Then you can define \"headers()\" in your sub-class and receive them all\nat once.\n\nFinally, you can define handlers to receive individual HTTP headers. This can be useful for very\nsimple SOAP servers (to name a crack-fueled standard that defines its own special HTTP headers).\n\nTo do so, you'll want to define the \"header()\" method in your subclass. That method will be\nhanded a (key,value) pair of the header name and the value.\n\naccepthook\nIf defined by a sub-class, this method is called directly after an accept happens. An\naccepthook to add SSL support might look like this:\n\nsub accepthook {\nmy $self = shift;\nmy $fh   = $self->stdiohandle;\n\n$self->SUPER::accepthook(@);\n\nmy $newfh =\nIO::Socket::SSL->startSSL( $fh,\nSSLserver    => 1,\nSSLusecert  => 1,\nSSLcertfile => 'myserver.crt',\nSSLkeyfile  => 'myserver.key',\n)\nor warn \"problem setting up SSL socket: \" . IO::Socket::SSL::errstr();\n\n$self->stdiohandle($newfh) if $newfh;\n}\n\npostsetuphook\nIf defined by a sub-class, this method is called after all setup has finished, before the\nhandler method.\n\nprintbanner\nThis routine prints a banner before the server request-handling loop starts.\n\nMethods below this point are probably not terribly useful to define yourself in subclasses.\n\nparserequest\nParse the HTTP request line. Returns three values, the request method, request URI and the\nprotocol.\n\nparseheaders\nParses incoming HTTP headers from STDIN, and returns an arrayref of \"(header => value)\" pairs.\nSee \"headers\" for possibilities on how to inspect headers.\n\nsetuplistener\nThis routine binds the server to a port and interface.\n\naftersetuplistener\nThis method is called immediately after setuplistener. It's here just for you to override.\n\nbadrequest\nThis method should print a valid HTTP response that says that the request was invalid.\n\nvalidhttpmethod($method)\nGiven a candidate HTTP method in $method, determine if it is valid. Override if, for example,\nyou'd like to do some WebDAV. The default implementation only accepts \"GET\", \"POST\", \"HEAD\",\n\"PUT\", \"PATCH\", \"DELETE\" and \"OPTIONS\".\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "Best Practical Solutions, LLC <modules@bestpractical.com>\n",
                "subsections": []
            },
            "CONTRIBUTORS": {
                "content": "Jesse Vincent, <jesse@bestpractical.com>. Original author.\n\nMarcus Ramberg <drave@thefeed.no> contributed tests, cleanup, etc\n\nSam Vilain, <samv@cpan.org> contributed the CGI.pm split-out and header/setup API.\n\nExample section by almut on perlmonks, suggested by Mark Fuller.\n",
                "subsections": []
            },
            "BUGS": {
                "content": "There certainly are some. Please report them via rt.cpan.org\n",
                "subsections": []
            },
            "LICENSE": {
                "content": "This software is Copyright (c) 2004-2015 Best Practical Solutions\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
                "subsections": []
            }
        }
    }
}