| 1 |
NAME |
| 2 |
HTTP::Parser::XS - a fast, primitive HTTP request parser |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use HTTP::Parser::XS qw(parse_http_request); |
| 6 |
|
| 7 |
# for HTTP servers |
| 8 |
my $ret = parse_http_request( |
| 9 |
"GET / HTTP/1.0\r\nHost: ...\r\n\r\n", |
| 10 |
\%env, |
| 11 |
); |
| 12 |
if ($ret == -2) { |
| 13 |
# request is incomplete |
| 14 |
... |
| 15 |
} elsif ($ret == -1) { |
| 16 |
# request is broken |
| 17 |
... |
| 18 |
} else { |
| 19 |
# $ret includes the size of the request, %env now contains a PSGI |
| 20 |
# request, if it is a POST / PUT request, read request content by |
| 21 |
# yourself |
| 22 |
... |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
# for HTTP clients |
| 27 |
use HTTP::Parser::XS qw(parse_http_response HEADERS_AS_ARRAYREF); |
| 28 |
my %special_headers = ( |
| 29 |
'content-length' => undef, |
| 30 |
); |
| 31 |
my($ret, $minor_version, $status, $message, $headers) |
| 32 |
= parse_http_response($response, HEADERS_AS_ARRAYREF, \%special_headers); |
| 33 |
|
| 34 |
if($ret == -1) } |
| 35 |
# response is incomplete |
| 36 |
} |
| 37 |
elsif($ret == -2) { |
| 38 |
# response is broken |
| 39 |
} |
| 40 |
else { |
| 41 |
# $ret is the length of the headers, starting the content body |
| 42 |
|
| 43 |
# the other values are the response messages. For example: |
| 44 |
# $status = 200 |
| 45 |
# $message = "OK" |
| 46 |
# $headers = [ 'content-type' => 'text/html', ... ] |
| 47 |
|
| 48 |
# and $special_headers{'content-length'} will be filled in |
| 49 |
} |
| 50 |
|
| 51 |
DESCRIPTION |
| 52 |
HTTP::Parser::XS is a fast, primitive HTTP request/response parser. |
| 53 |
|
| 54 |
The request parser can be used either for writing a synchronous HTTP |
| 55 |
server or a event-driven server. |
| 56 |
|
| 57 |
The response parser can be used for writing HTTP clients. |
| 58 |
|
| 59 |
Note that even if this distribution name ends "::XS", pure Perl |
| 60 |
implementation is supported, so you can use this module on compiler-less |
| 61 |
environments. |
| 62 |
|
| 63 |
FUNCTIONS |
| 64 |
parse_http_request($request_string, \%env) |
| 65 |
Tries to parse given request string, and if successful, inserts |
| 66 |
variables into %env. For the name of the variables inserted, please |
| 67 |
refer to the PSGI specification. The return values are: |
| 68 |
|
| 69 |
>=0 length of the request (request line and the request |
| 70 |
headers), in bytes |
| 71 |
|
| 72 |
-1 given request is corrupt |
| 73 |
|
| 74 |
-2 given request is incomplete |
| 75 |
|
| 76 |
Note that the semantics of PATH_INFO is somewhat different from |
| 77 |
Apache. First, HTTP::Parser::XS does not validate the variable; it |
| 78 |
does not raise an error even if PATH_INFO does not start with "/". |
| 79 |
Second, the variable is conformant to RFC 3875 (and PSGI / Plack) in |
| 80 |
the fact that "//" and ".." appearing in PATH_INFO are preserved |
| 81 |
whereas Apache transcodes them. |
| 82 |
|
| 83 |
parse_http_response($response_string, $header_format, \%special_headers) |
| 84 |
Tries to parse given response string. *$header_format* must be |
| 85 |
"HEADERS_AS_ARRAYREF", "HEADERS_AS_HASHREF", or "HEADERS_NONE", |
| 86 |
which are exportable constants. |
| 87 |
|
| 88 |
The optional *%special_headers* is for headers you specifically |
| 89 |
require. You can set any HTTP response header names, which must be |
| 90 |
lower-cased, and their default values, and then the values are |
| 91 |
filled in by "parse_http_response()". For example, if you want the |
| 92 |
"Cointent-Length" field, set its name with default values like "%h = |
| 93 |
('content-length' => undef)" and pass it as *%special_headers*. |
| 94 |
After parsing, $h{'content-length'} is set if the response has the |
| 95 |
"Content-Length" field, otherwise it's not touched. |
| 96 |
|
| 97 |
The return values are: |
| 98 |
|
| 99 |
$ret The parsering status, which is the same as |
| 100 |
"parse_http_response()". i.e. the length of the response |
| 101 |
headers in bytes, -1 for incomplete headers, or -2 for |
| 102 |
errors. |
| 103 |
|
| 104 |
If the given response string is broken or imcomplete, |
| 105 |
"parse_http_response()" returns only this value. |
| 106 |
|
| 107 |
$minor_version |
| 108 |
The minor version of the given response. i.e. 1 for |
| 109 |
HTTP/1.1, 0 for HTTP/1.0. |
| 110 |
|
| 111 |
$status The HTTP status of the given response. e.g. 200 for success. |
| 112 |
|
| 113 |
$message |
| 114 |
The HTTP status message. e.g. "OK" for success. |
| 115 |
|
| 116 |
$headers |
| 117 |
The HTTP headers for the given response. It is an ARRAY |
| 118 |
reference if *$header_format* is "HEADERS_AS_ARRAYREF", a |
| 119 |
HASH reference on "HEADERS_AS_HASHREF", an "undef" on |
| 120 |
"HEADERS_NONE". |
| 121 |
|
| 122 |
The names of the headers are normalized to lower-cased. |
| 123 |
|
| 124 |
LIMITATIONS |
| 125 |
Both "parse_http_request()" and "parse_http_response()" in XS |
| 126 |
implementation have some size limitations. |
| 127 |
|
| 128 |
The number of headers |
| 129 |
The number of headers is limited to 128. If it exceeds, both parsing |
| 130 |
routines report parsing errors, i.e. return -1 for $ret. |
| 131 |
|
| 132 |
The size of header names |
| 133 |
The size of header names is limited to 1024, but the parsers do not the |
| 134 |
same action. |
| 135 |
|
| 136 |
"parse_http_request()" returns -1 if too-long header names exist. |
| 137 |
|
| 138 |
"parse_http_request()" simply ignores too-long header names. |
| 139 |
|
| 140 |
COPYRIGHT |
| 141 |
Copyright 2009- Kazuho Oku |
| 142 |
|
| 143 |
AUTHORS |
| 144 |
* Kazuho Oku <https://metacpan.org/author/KAZUHO> |
| 145 |
|
| 146 |
* gfx <https://metacpan.org/author/GFUJI> |
| 147 |
|
| 148 |
* mala <https://metacpan.org/author/MALA> |
| 149 |
|
| 150 |
* tokuhirom <https://metacpan.org/author/TOKUHIROM> |
| 151 |
|
| 152 |
THANKS TO |
| 153 |
* nothingmuch <https://metacpan.org/author/NUFFIN> |
| 154 |
|
| 155 |
* charsbar <https://metacpan.org/author/CHARSBAR> |
| 156 |
|
| 157 |
* DOLMEN <https://metacpan.org/author/DOLMEN> |
| 158 |
|
| 159 |
SEE ALSO |
| 160 |
* <http://github.com/kazuho/picohttpparser> |
| 161 |
|
| 162 |
* HTTP::Parser |
| 163 |
|
| 164 |
* HTTP::HeaderParser::XS |
| 165 |
|
| 166 |
* Plack |
| 167 |
|
| 168 |
* PSGI |
| 169 |
|
| 170 |
LICENSE |
| 171 |
This library is free software; you can redistribute it and/or modify it |
| 172 |
under the same terms as Perl itself. |
| 173 |
|