/usr/lib/x86_64-linux-gnu/perl5/5.20/HTTP/Parser/XS.pm is in libhttp-parser-xs-perl 0.16-1+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | package HTTP::Parser::XS;
use strict;
use warnings;
use base qw(Exporter);
our %EXPORT_TAGS = (
'all' => [ qw/parse_http_request parse_http_response
HEADERS_NONE HEADERS_AS_HASHREF HEADERS_AS_ARRAYREF/ ],
);
our @EXPORT_OK = @{$EXPORT_TAGS{all}};
our @EXPORT = ();
# header format for parse_http_response()
use constant {
HEADERS_NONE => 0, # don't parse headers. It's fastest. if you want only special headers, also fastest.
HEADERS_AS_HASHREF => 1, # HTTP::Headers compatible HashRef, { header_name => "header_value" or ["val1", "val2"] }
HEADERS_AS_ARRAYREF =>2, # Ordered ArrayRef : [ name, value, name2, value2 ... ]
};
our $VERSION = '0.16';
our $BACKEND;
if (not __PACKAGE__->can('parse_http_response')) {
$BACKEND = $ENV{PERL_HTTP_PARSER_XS} || ($ENV{PERL_ONLY} ? 'pp' : '');
if ($BACKEND !~ /\b pp \b/xms) {
eval {
require XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);
$BACKEND = 'xs';
};
die $@ if $@ && $BACKEND =~ /\bxs\b/;
}
if (not __PACKAGE__->can('parse_http_response')) {
require HTTP::Parser::XS::PP;
$BACKEND = 'pp';
}
}
1;
__END__
=head1 NAME
HTTP::Parser::XS - a fast, primitive HTTP request parser
=head1 SYNOPSIS
use HTTP::Parser::XS qw(parse_http_request);
# for HTTP servers
my $ret = parse_http_request(
"GET / HTTP/1.0\r\nHost: ...\r\n\r\n",
\%env,
);
if ($ret == -2) {
# request is incomplete
...
} elsif ($ret == -1) {
# request is broken
...
} else {
# $ret includes the size of the request, %env now contains a PSGI
# request, if it is a POST / PUT request, read request content by
# yourself
...
}
# for HTTP clients
use HTTP::Parser::XS qw(parse_http_response HEADERS_AS_ARRAYREF);
my %special_headers = (
'content-length' => undef,
);
my($ret, $minor_version, $status, $message, $headers)
= parse_http_response($response, HEADERS_AS_ARRAYREF, \%special_headers);
if($ret == -1) }
# response is incomplete
}
elsif($ret == -2) {
# response is broken
}
else {
# $ret is the length of the headers, starting the content body
# the other values are the response messages. For example:
# $status = 200
# $message = "OK"
# $headers = [ 'content-type' => 'text/html', ... ]
# and $special_headers{'content-length'} will be filled in
}
=head1 DESCRIPTION
HTTP::Parser::XS is a fast, primitive HTTP request/response parser.
The request parser can be used either for writing a synchronous HTTP server or a event-driven server.
The response parser can be used for writing HTTP clients.
Note that even if this distribution name ends C<::XS>, B<pure Perl>
implementation is supported, so you can use this module on compiler-less
environments.
=head1 FUNCTIONS
=over 4
=item parse_http_request($request_string, \%env)
Tries to parse given request string, and if successful, inserts variables into %env. For the name of the variables inserted, please refer to the PSGI specification. The return values are:
=over 8
=item >=0
length of the request (request line and the request headers), in bytes
=item -1
given request is corrupt
=item -2
given request is incomplete
=back
Note that the semantics of PATH_INFO is somewhat different from Apache. First, L<HTTP::Parser::XS> does not validate the variable; it does not raise an error even if PATH_INFO does not start with "/". Second, the variable is conformant to RFC 3875 (and L<PSGI> / L<Plack>) in the fact that "//" and ".." appearing in PATH_INFO are preserved whereas Apache transcodes them.
=item parse_http_response($response_string, $header_format, \%special_headers)
Tries to parse given response string. I<$header_format> must be
C<HEADERS_AS_ARRAYREF>, C<HEADERS_AS_HASHREF>, or C<HEADERS_NONE>,
which are exportable constants.
The optional I<%special_headers> is for headers you specifically require.
You can set any HTTP response header names, which must be lower-cased,
and their default values, and then the values are filled in by
C<parse_http_response()>.
For example, if you want the C<Cointent-Length> field, set its name with
default values like C<< %h = ('content-length' => undef) >> and pass it as
I<%special_headers>. After parsing, C<$h{'content-length'}> is set
if the response has the C<Content-Length> field, otherwise it's not touched.
The return values are:
=over 8
=item C<$ret>
The parsering status, which is the same as C<parse_http_response()>. i.e.
the length of the response headers in bytes, C<-1> for incomplete headers,
or C<-2> for errors.
If the given response string is broken or imcomplete, C<parse_http_response()>
returns only this value.
=item C<$minor_version>
The minor version of the given response.
i.e. C<1> for HTTP/1.1, C<0> for HTTP/1.0.
=item C<$status>
The HTTP status of the given response. e.g. C<200> for success.
=item C<$message>
The HTTP status message. e.g. C<OK> for success.
=item C<$headers>
The HTTP headers for the given response. It is an ARRAY reference
if I<$header_format> is C<HEADERS_AS_ARRAYREF>, a HASH reference on
C<HEADERS_AS_HASHREF>, an C<undef> on C<HEADERS_NONE>.
The names of the headers are normalized to lower-cased.
=back
=back
=head1 LIMITATIONS
Both C<parse_http_request()> and C<parse_http_response()> in XS
implementation have some size limitations.
=head2 The number of headers
The number of headers is limited to C<128>. If it exceeds, both parsing
routines report parsing errors, i.e. return C<-1> for C<$ret>.
=head2 The size of header names
The size of header names is limited to C<1024>, but the parsers do not the
same action.
C<parse_http_request()> returns C<-1> if too-long header names exist.
C<parse_http_request()> simply ignores too-long header names.
=head1 COPYRIGHT
Copyright 2009- Kazuho Oku
=head1 AUTHOR
Kazuho Oku
gfx
mala
tokuhirom
=head1 THANKS TO
nothingmuch
charsbar
=head1 SEE ALSO
L<http://github.com/kazuho/picohttpparser>
L<HTTP::Parser>
L<HTTP::HeaderParser::XS>
L<Plack>
L<PSGI>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
|