File Coverage

blib/lib/HTTP/Parser/XS.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


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