{
    "mode": "perldoc",
    "parameter": "Net::Server::HTTP",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AHTTP/json",
    "generated": "2026-06-13T02:45:24Z",
    "synopsis": "use base qw(Net::Server::HTTP);\nPACKAGE->run;\nsub processhttprequest {\nmy $self = shift;\nprint \"Content-type: text/html\\n\\n\";\nprint \"<form method=post action=/bam><input type=text name=foo><input type=submit></form>\\n\";\nrequire Data::Dumper;\nlocal $Data::Dumper::Sortkeys = 1;\nrequire CGI;\nmy $form = {};\nmy $q = CGI->new; $form->{$} = $q->param($) for $q->param;\nprint \"<pre>\".Data::Dumper->Dump([\\%ENV, $form], ['*ENV', 'form']).\"</pre>\";\n}",
    "sections": {
        "NAME": {
            "content": "Net::Server::HTTP - very basic Net::Server based HTTP server class\n",
            "subsections": []
        },
        "TEST ONE LINER": {
            "content": "perl -e 'use base qw(Net::Server::HTTP); main->run(port => 8080)'\n# will start up an echo server\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use base qw(Net::Server::HTTP);\nPACKAGE->run;\n\nsub processhttprequest {\nmy $self = shift;\n\nprint \"Content-type: text/html\\n\\n\";\nprint \"<form method=post action=/bam><input type=text name=foo><input type=submit></form>\\n\";\n\nrequire Data::Dumper;\nlocal $Data::Dumper::Sortkeys = 1;\n\nrequire CGI;\nmy $form = {};\nmy $q = CGI->new; $form->{$} = $q->param($) for $q->param;\n\nprint \"<pre>\".Data::Dumper->Dump([\\%ENV, $form], ['*ENV', 'form']).\"</pre>\";\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Even though Net::Server::HTTP doesn't fall into the normal parallel of the other Net::Server\nflavors, handling HTTP requests is an often requested feature and is a standard and simple\nprotocol.\n\nNet::Server::HTTP begins with base type MultiType defaulting to Net::Server::Fork. It is easy to\nchange it to any of the other Net::Server flavors by passing servertype => $otherflavor in the\nserver configuration. The port has also been defaulted to port 80 - but could easily be changed\nto another through the server configuration. You can also very easily add ssl by including,\nproto=>\"ssl\" and provide a SSLcertfile and SSLkeyfile.\n\nFor example, here is a basic server that will bind to all interfaces, will speak both HTTP on\nport 8080 as well as HTTPS on 8443, and will speak both IPv4, as well as IPv6 if it is\navailable.\n\nuse base qw(Net::Server::HTTP);\n\nPACKAGE->run(\nport  => [8080, \"8443/ssl\"],\nipv   => '*', # IPv6 if available\nSSLkeyfile  => '/my/key',\nSSLcertfile => '/my/cert',\n);\n",
            "subsections": []
        },
        "METHODS": {
            "content": "\"initaccesslog\"\nUsed to open and initialize any requested accesslog (see accesslogfile and\naccesslogformat).\n\n\"tieclientstdout\"\nUsed to initialize automatic response header parsing.\n\n\"processhttprequest\"\nWill be passed the client handle, and will have STDOUT and STDIN tied to the client.\n\nDuring this method, the %ENV will have been set to a standard CGI style environment. You\nwill need to be sure to print the Content-type header. This is one change from the other\nstandard Net::Server base classes.\n\nDuring this method you can read from %ENV and STDIN just like a normal HTTP request in other\nweb servers. You can print to STDOUT and Net::Server will handle the header negotiation for\nyou.\n\nNote: Net::Server::HTTP has no concept of document root or script aliases or default\nhandling of static content. That is up to the consumer of Net::Server::HTTP to work out.\n\nNet::Server::HTTP comes with a basic %ENV display installed as the default\nprocesshttprequest method.\n\n\"processrequest\"\nThis method has been overridden in Net::Server::HTTP - you should not use it while using\nNet::Server::HTTP. This overridden method parses the environment and sets up request alarms\nand handles dying failures. It calls processhttprequest once the request is ready and\nheaders have been parsed.\n\n\"processheaders\"\nUsed to read in the incoming headers and set the ENV.\n\n\"inithttprequestinfo\"\nCalled at the end of processheaders. Initializes the contents of httprequestinfo.\n\n\"httprequestinfo\"\nReturns a hashref of information specific to the current request. This information will be\nused for logging later on.\n\n\"sendstatus\"\nTakes an HTTP status and a message. Sends out the correct headers.\n\n\"send500\"\nCalls sendstatus with 500 and the argument passed to send500.\n\nc<loghttprequest>\nCalled at the end of postprocessrequest. The default method looks for the default\naccesslogformat and checks if logging was initialized during initaccesslog. If both of\nthese exist, the httprequestinfo is formatted using httplogformat and the result is\nlogged.\n\n\"httplogformat\"\nTakes a format string, and requestinfo and returns a formatted string. The format should\nfollow the apache modlogconfig specification. As in the modlogconfig specification,\nbackslashes, quotes should be escaped with backslashes and you may also include \\n and \\t\ncharacters as well.\n\nThe following is a listing of the available parameters as well as sample output based on a\nvery basic HTTP server.\n\n%%                %                 # a percent\n%a                ::1               # remote ip\n%A                ::1               # local ip\n%b                83                # response size (- if 0) Common Log Format\n%B                83                # response size\n%{bar}C           baz               # value of cookie by that name\n%D                916               # elapsed in microseconds\n%{HTTPCOOKIE}e   bar=baz           # value of %ENV by that name\n%f                -                 # filename - unused\n%h                ::1               # remote host if lookups are on, remote ip otherwise\n%H                http              # request protocol\n%{Host}i          localhost:8080    # request header by that name\n%I                336               # bytes received including headers\n%l                -                 # remote logname - unsused\n%m                GET               # request method\n%n                Just a note       # httpnote by that name\n%{Content-type}o  text/html         # output header by that name\n%O                189               # response size including headers\n%p                8080              # server port\n%P                22999             # pid - does not support %{tid}P\nq                 ?hello=there      # querystring including ? (- otherwise)\nr                 GET /bam?hello=there HTTP/1.1      # the first line of the request\n%s                200               # response status\n%u                -                 # remote user - unused\n%U                /bam              # request path (no query string)\n%t                [06/Jun/2012:12:14:21 -0600]       # httplogtime standard format\n%t{%F %T %z}t     [2012-06-06 12:14:21 -0600]        # httplogtime with format\n%T                0                 # elapsed time in seconds\n%v                localhost:8080    # httplogvhost - partial implementation\n%V                localhost:8080    # httplogvhost - partial implementation\n%X                -                 # Connection completed and is 'close' (-)\n\nAdditionally, the log parsing allows for the following formats.\n\n%>s               200               # status of last request\n%<s               200               # status of original request\n%400a             -                 # remote ip if status is 400\n%!400a            ::1               # remote ip if status is not 400\n%!200a            -                 # remote ip if status is not 200\n\nThere are few bits not completely implemented:\n\n> and <    # There is no internal redirection\n%I         # The answer to this is based on header size and Content-length\ninstead of the more correct actual number of bytes read though\nin common cases those would be the same.\n%X         # There is no Connection keepalive in the default server.\n%v and %V  # There are no virtual hosts in the default HTTP server.\n%{tid}P    # The default servers are not threaded.\n\nSee the \"accesslogformat\" option for how to set a different format as well as to see the\ndefault string.\n\n\"execcgi\"\nAllow for calling an external script as a CGI. This will use IPC::Open3 to fork a new\nprocess and read/write from it.\n\nuse base qw(Net::Server::HTTP);\nPACKAGE->run;\n\nsub processhttprequest {\nmy $self = shift;\n\nif ($ENV{'PATHINFO'} && $ENV{'PATHINFO'} =~ s{^ (/foo) (?= $ | /) }{}x) {\n$ENV{'SCRIPTNAME'} = $1;\nmy $file = \"/var/www/cgi-bin/foo\"; # assuming this exists\nreturn $self->execcgi($file);\n}\n\nprint \"Content-type: text/html\\n\\n\";\nprint \"<a href=/foo>Foo</a>\";\n}\n\nAt this first release, the parent server is not tracking the child script which may cause\nissues if the script is running when a HUP is received.\n\n\"httplogtime\"\nUsed to implement the %t format.\n\n\"httplogenv\"\nUsed to implement the %e format.\n\n\"httplogcookie\"\nUsed to implement the %C format.\n\n\"httplogheaderin\"\nused to implement the %i format.\n\n\"httplognote\"\nUsed to implement the %n format.\n\n\"httpnote\"\nTakes a key and an optional value. If passed a key and value, sets the note for that key.\nAlways returns the value. These notes currently only are used for %{key}n output format.\n\n\"httplogheaderout\"\nUsed to implement the %o format.\n\n\"httplogpid\"\nUsed to implement the %P format.\n\n\"httplogvhost\"\nUsed to implement the %v and %V formats.\n\n\"httplogconstat\"\nUsed to implement the %X format.\n\n\"exectrustedperl\"\nAllow for calling an external perl script. This method will still fork, but instead of using\nIPC::Open3, it simply requires the perl script. That means that the running script will be\nable to make use of any shared memory. It also means that the STDIN/STDOUT/STDERR handles\nthe script is using are those directly bound by the server process.\n\nuse base qw(Net::Server::HTTP);\nPACKAGE->run;\n\nsub processhttprequest {\nmy $self = shift;\n\nif ($ENV{'PATHINFO'} && $ENV{'PATHINFO'} =~ s{^ (/foo) (?= $ | /) }{}x) {\n$ENV{'SCRIPTNAME'} = $1;\nmy $file = \"/var/www/cgi-bin/foo\"; # assuming this exists\nreturn $self->exectrustedperl($file);\n}\n\nprint \"Content-type: text/html\\n\\n\";\nprint \"<a href=/foo>Foo</a>\";\n}\n\nAt this first release, the parent server is not tracking the child script which may cause\nissues if the script is running when a HUP is received.\n\n\"execforkhook\"\nThis method is called after the fork of exectrustedperl and execcgi hooks. It is passed\nthe pid (0 if the child) and the file being ran. Note, that the hook will not be called from\nthe child during execcgi.\n\n\"httpdispatch\"\nCalled if the default processhttprequest and processrequest methods have not been\noverridden and \"app\" configuration parameters have been passed. In this case this replaces\nthe default echo server. You can also enable this subsystem for your own direct use by\nsetting enabledispatch to true during configuration. See the \"app\" configuration item. It\nwill be passed a dispatch qr (regular expression) generated during checkdispatch, and a\ndispatch table. The qr will be applied to pathinfo. This mechanism could be used to augment\nNet::Server::HTTP with document root and virtual host capabilities.\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "In addition to the command line arguments of the Net::Server base classes you can also set the\nfollowing options.\n\nmaxheadersize\nDefaults to 100000. Maximum number of bytes to read while parsing headers.\n\nserverrevision\nDefaults to Net::Server::HTTP/$Net::Server::VERSION.\n\ntimeoutheader\nDefaults to 15 - number of seconds to wait for parsing headers.\n\ntimeoutidle\nDefaults to 60 - number of seconds a request can be idle before the request is closed.\n\naccesslogfile\nDefaults to undef. If true, this represents the location of where the access log should be\nwritten to. If a special value of STDERR is passed, the access log entry will be writing to\nthe same location as the ERROR log.\n\naccesslogformat\nShould be a valid apache log format that will be passed to httplogformat. See the\nhttplogformat method for more information.\n\nThe default value is the NCSA extended/combined log format:\n\n'%h %l %u %t \\\"%r\\\" %>s %b \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"'\n\napp Takes one or more items and registers them for dispatch. Arguments may be supplied as an\narrayref containing a location/target pairs, a hashref containing a location/target pairs, a\nbare code ref that will use \"/\" as the location and the codref as the target, a string with\na space indicating \"location target\", a string containing \"location=target\", or finally a\nstring that will be used as both location and target. For items passed as an arrayref or\nhashref, the target may be a coderef which will be called and should handle the request. In\nall other cases the target should be a valid executable suitable for passing to execcgi.\n\nThe locations will be added in the order that they are configured. They will be added to a\nregular expression which will be applied to the incoming PATHINFO string. If the match is\nsuccessful, the $ENV{'SCRIPTNAME'} will be set to the matched portion and the matched\nportion will be removed from $ENV{'PATHINFO'}.\n\nOnce an app has been passed, it is necessary for the server to listen on /. Therefore if \"/\"\nhas not been specifically configured for dispatch, the first found dispatch target will also\nbe used to handle \"/\".\n\nFor convenience, if the loglevel is 2 or greater, the dispatch table is output to the log.\n\nThis mechanism is left as a generic mechanism suitable for overriding by servers meant to\nhandle more complex dispatch. At the moment there is no handling of virtual hosts. At some\npoint we will add in the default ability to play static content and likely for the ability\nto configure virtual hosts - or that may have to wait for a third party module.\n\napp => \"/home/paul/foo.cgi\",\n# Dispatch: /home/paul/foo.cgi => home/paul/foo.cgi\n# Dispatch: / => home/paul/foo.cgi (default)\n\n\napp => \"../../foo.cgi\",\napp => \"./bar.cgi\",\napp => \"baz ./bar.cgi\",\napp => \"bim=./bar.cgi\",\n# Dispatch: /foo.cgi => ../../foo.cgi\n# Dispatch: /bar.cgi => ./bar.cgi\n# Dispatch: /baz => ./bar.cgi\n# Dispatch: /bim => ./bar.cgi\n# Dispatch: / => ../../foo.cgi (default)\n\n\napp => \"../../foo.cgi\",\napp => \"/=./bar.cgi\",\n# Dispatch: /foo.cgi => ../../foo.cgi\n# Dispatch: / => ./bar.cgi\n\n# you could also do this on the commandline\nnet-server HTTP app ../../foo.cgi app /=./bar.cgi\n\n# extended options when configured from code\n\nNet::Server::HTTP->run(app => { # loses order of matching\n'/' => sub { ... },\n'/foo' => sub { ... },\n'/bar' => '/path/to/some.cgi',\n});\n\nNet::Server::HTTP->run(app => [\n'/' => sub { ... },\n'/foo' => sub { ... },\n'/bar' => '/path/to/some.cgi',\n]);\n",
            "subsections": []
        },
        "TODO": {
            "content": "Add support for writing out HTTP/1.1.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Paul T. Seamons paul@seamons.com\n",
            "subsections": []
        },
        "THANKS": {
            "content": "See Net::Server\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Please see also Net::Server::Fork, Net::Server::INET, Net::Server::PreFork,\nNet::Server::PreForkSimple, Net::Server::MultiType, Net::Server::Single Net::Server::SIG\nNet::Server::Daemonize Net::Server::Proto\n",
            "subsections": []
        }
    },
    "summary": "Net::Server::HTTP - very basic Net::Server based HTTP server class",
    "flags": [],
    "examples": [],
    "see_also": []
}