| 1 | package PAGI::Server::Protocol::HTTP1; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 153 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 153 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 153 | our $VERSION = '0.002013'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 153 | ▶ | use HTTP::Parser::XS qw(parse_http_request); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 153 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 153 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 153 | ▶ | use PAGI::Server (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # Header Validation (CRLF Injection Prevention) | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # RFC 7230 Section 3.2.6: Field values MUST NOT contain CR or LF | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # Additionally, null bytes are rejected as they can cause truncation attacks | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 792 | ▶ | sub _validate_header_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_header_name: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 792 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 792 | ▶ | if ($name =~ /[\r\n\0]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | die "Invalid header name: contains CR, LF, or null byte\n"; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # RFC 7230: token = 1*tchar | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # For simplicity, we just reject control characters and delimiters | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 790 | ▶ | if ($name =~ /[[:cntrl:]]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | die "Invalid header name: contains control characters\n"; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 790 | return $name; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 790 | ▶ | sub _validate_header_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_header_value: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 790 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 790 | ▶ | if ($value =~ /[\r\n\0]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 13 | die "Invalid header value: contains CR, LF, or null byte\n"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 777 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | PAGI::Server::Protocol::HTTP1 - HTTP/1.1 protocol handler | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | use PAGI::Server::Protocol::HTTP1; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | my $proto = PAGI::Server::Protocol::HTTP1->new; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # Parse incoming request | ||||||||||||||||||||||||||||||||||||||||||
| 55 | my ($request, $consumed) = $proto->parse_request($buffer); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Serialize response | ||||||||||||||||||||||||||||||||||||||||||
| 58 | my $bytes = $proto->serialize_response_start(200, \@headers, $chunked); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | $bytes .= $proto->serialize_response_body($chunk, $more); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | PAGI::Server::Protocol::HTTP1 isolates HTTP/1.1 wire-format parsing and | ||||||||||||||||||||||||||||||||||||||||||
| 64 | serialization from PAGI event handling. This allows clean separation of | ||||||||||||||||||||||||||||||||||||||||||
| 65 | protocol handling and future addition of HTTP/2 or HTTP/3 modules with | ||||||||||||||||||||||||||||||||||||||||||
| 66 | the same interface. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | my $proto = PAGI::Server::Protocol::HTTP1->new(%options); | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | Creates a new HTTP1 protocol handler. Accepts the following options, which | ||||||||||||||||||||||||||||||||||||||||||
| 75 | bound resource use while parsing untrusted input: | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =item * C<max_header_size> - maximum size in bytes of the combined header | ||||||||||||||||||||||||||||||||||||||||||
| 80 | block. Default: 8192 (8KB). Exceeding it yields a 431 error result. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =item * C<max_request_line_size> - maximum size in bytes of the request line. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | Default: 8192 (8KB). Exceeding it yields a 414 error result. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =item * C<max_header_count> - maximum number of header fields. Default: 100. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | Exceeding it yields a 431 error result. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =item * C<max_chunk_size> - maximum size in bytes of a single chunk in a | ||||||||||||||||||||||||||||||||||||||||||
| 89 | chunked request body. Default: 10_485_760 (10MB). | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =head2 parse_request | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | my ($request_info, $bytes_consumed) = $proto->parse_request($buffer); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | Parses an HTTP request from the buffer. The first return value is one of three | ||||||||||||||||||||||||||||||||||||||||||
| 98 | things: | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =item * C<undef> when the request is incomplete (more bytes needed). The | ||||||||||||||||||||||||||||||||||||||||||
| 103 | second value is 0. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =item * an B<error descriptor> when the request is malformed or exceeds a | ||||||||||||||||||||||||||||||||||||||||||
| 106 | limit: | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | { error => 400, message => 'Bad Request' } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | The C<error> key is the HTTP status code to send (400, 413, 414, 431, or 501); | ||||||||||||||||||||||||||||||||||||||||||
| 111 | C<message> is a short reason. The second return value is the number of bytes | ||||||||||||||||||||||||||||||||||||||||||
| 112 | to discard. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =item * a B<request hash> on success: | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | $request_info = { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | method => 'GET', | ||||||||||||||||||||||||||||||||||||||||||
| 118 | path => '/foo', | ||||||||||||||||||||||||||||||||||||||||||
| 119 | raw_path => '/foo%20bar', | ||||||||||||||||||||||||||||||||||||||||||
| 120 | query_string => 'a=1', | ||||||||||||||||||||||||||||||||||||||||||
| 121 | http_version => '1.1', | ||||||||||||||||||||||||||||||||||||||||||
| 122 | headers => [ ['host', 'localhost'], ... ], | ||||||||||||||||||||||||||||||||||||||||||
| 123 | content_length => 0, # or undef if no Content-Length header | ||||||||||||||||||||||||||||||||||||||||||
| 124 | chunked => 0, # 1 if Transfer-Encoding: chunked | ||||||||||||||||||||||||||||||||||||||||||
| 125 | expect_continue => 0, # 1 if the request sent Expect: 100-continue | ||||||||||||||||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head2 serialize_response_start | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | my $bytes = $proto->serialize_response_start($status, \@headers, $chunked, $http_version); | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Serializes the status line and headers. C<$chunked> (default 0) adds a | ||||||||||||||||||||||||||||||||||||||||||
| 135 | C<Transfer-Encoding: chunked> header, but only when C<$http_version> (default | ||||||||||||||||||||||||||||||||||||||||||
| 136 | C<'1.1'>) is C<'1.1'> — chunked encoding is not emitted for HTTP/1.0 | ||||||||||||||||||||||||||||||||||||||||||
| 137 | responses. A default C<Server> header is added when the application does not | ||||||||||||||||||||||||||||||||||||||||||
| 138 | provide one. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | =head2 serialize_response_body | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | my $bytes = $proto->serialize_response_body($chunk, $more, $chunked); | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | Serializes a body chunk. Uses chunked encoding if $chunked is true. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =head2 serialize_chunk_end | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | my $bytes = $proto->serialize_chunk_end; | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | Returns the terminating zero-length chunk (C<"0\r\n\r\n">) that ends a | ||||||||||||||||||||||||||||||||||||||||||
| 151 | chunked response body. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =head2 serialize_continue | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | my $bytes = $proto->serialize_continue; | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Returns a C<HTTP/1.1 100 Continue> interim response, sent in reply to a | ||||||||||||||||||||||||||||||||||||||||||
| 158 | request that carried C<Expect: 100-continue>. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =head2 serialize_trailers | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | my $bytes = $proto->serialize_trailers(\@headers); | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | Serializes HTTP trailers. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | =head2 format_date | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | my $date = $proto->format_date; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | Returns the current time formatted as an RFC 7231 IMF-fixdate string suitable | ||||||||||||||||||||||||||||||||||||||||||
| 171 | for a C<Date> header (e.g. C<"Sun, 06 Nov 1994 08:49:37 GMT">). The value is | ||||||||||||||||||||||||||||||||||||||||||
| 172 | cached and regenerated at most once per second. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # Cached Date header (regenerated at most once per second) | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 153 | my $_cached_date; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 153 | my $_cached_date_time = 0; | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | # Cached default Server header (lazy-init to ensure VERSION is loaded) | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 153 | my $_server_header; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # HTTP status code reason phrases | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 153 | my %STATUS_PHRASES = ( | |||||||||||||||||||||||||||||||||||||||||
| 185 | 100 => 'Continue', | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 101 => 'Switching Protocols', | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 200 => 'OK', | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 201 => 'Created', | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 204 => 'No Content', | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 301 => 'Moved Permanently', | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 302 => 'Found', | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 304 => 'Not Modified', | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 400 => 'Bad Request', | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 401 => 'Unauthorized', | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 403 => 'Forbidden', | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 404 => 'Not Found', | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 405 => 'Method Not Allowed', | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 413 => 'Payload Too Large', | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 414 => 'URI Too Long', | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 431 => 'Request Header Fields Too Large', | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 500 => 'Internal Server Error', | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 502 => 'Bad Gateway', | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 503 => 'Service Unavailable', | ||||||||||||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 700 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 700 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||
| 210 | max_header_size => $args{max_header_size} // 8192, | ||||||||||||||||||||||||||||||||||||||||||
| 211 | max_request_line_size => $args{max_request_line_size} // 8192, # 8KB per RFC 7230 | ||||||||||||||||||||||||||||||||||||||||||
| 212 | max_header_count => $args{max_header_count} // 100, # Max number of headers | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 700 | ▶ | max_chunk_size => $args{max_chunk_size} // 10_485_760, # 10MB default | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%8/8
$args{"max_header_size"} // 8192
$args{"max_request_line_size"} // 8192
$args{"max_header_count"} // 100
$args{"max_chunk_size"} // 10485760
MC/DC100%1/1
$args{"max_header_size"} // 8192
$args{"max_header_size"}
MC/DC100%1/1
$args{"max_request_line_size"} // 8192
$args{"max_request_line_size"}
MC/DC100%1/1
$args{"max_header_count"} // 100
$args{"max_header_count"}
MC/DC100%1/1
$args{"max_chunk_size"} // 10485760
$args{"max_chunk_size"}
Truth table100%2/2
A
A= $args{"max_header_size"}
Truth table100%2/2
A
A= $args{"max_request_line_size"}
Truth table100%2/2
A
A= $args{"max_header_count"}
Truth table100%2/2
A
A= $args{"max_chunk_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 700 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 351 | ▶ | sub parse_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_request: called CC=36 SCAR=35.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 351 | my ($self, $buffer_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | # HTTP::Parser::XS expects a scalar, not a reference | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 351 | ▶ | my $buffer = ref $buffer_ref ? $$buffer_ref : $buffer_ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | # Check for complete headers (look for \r\n\r\n) | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 351 | my $header_end = index($buffer, "\r\n\r\n"); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 351 | ▶ | return (undef, 0) if $header_end < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # Check request line length (first line before \r\n) | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 348 | my $first_line_end = index($buffer, "\r\n"); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 348 | ▶ | if ($first_line_end > $self->{max_request_line_size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 2 | return ({ error => 414, message => 'URI Too Long' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | # Check max header size | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 346 | ▶ | if ($header_end > $self->{max_header_size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 2 | return ({ error => 431, message => 'Request Header Fields Too Large' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # Parse using HTTP::Parser::XS | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 344 | my %env; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 344 | my $ret = parse_http_request($buffer, \%env); | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | # Return error for malformed request (-1) | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 344 | ▶ | if ($ret == -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | # Find end of malformed request line/headers | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | my $consumed = $header_end + 4; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | return ({ error => 400, message => 'Bad Request' }, $consumed); | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | # Return undef if incomplete (-2) | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 343 | ▶ | return (undef, 0) if $ret < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | # Extract method and path | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 343 | my $method = $env{REQUEST_METHOD}; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 343 | ▶ | my $raw_uri = $env{REQUEST_URI} // '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$env{"REQUEST_URI"} // "/"
MC/DC0%0/1
$env{"REQUEST_URI"} // "/"
$env{"REQUEST_URI"}
Truth table50%1/2
A
A= $env{"REQUEST_URI"}
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | # Split path and query string | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 343 | my ($raw_path, $query_string) = split(/\?/, $raw_uri, 2); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 343 | ▶ | $raw_path //= '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$raw_path //= "/"
MC/DC0%0/1
$raw_path //= "/"
$raw_path
Truth table50%1/2
A
A= $raw_path
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 343 | ▶ | $query_string //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$query_string //= ""
MC/DC100%1/1
$query_string //= ""
$query_string
Truth table100%2/2
A
A= $query_string
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # Decode path (URL-decode, then UTF-8 decode with fallback) | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # Mojolicious-style: try UTF-8 decode, fall back to original bytes if invalid. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # Fast path: a path with no percent-escapes and no high bytes is already its | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # own decoded form (ASCII is its own UTF-8), so skip uri_unescape and the | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # eval + Encode::decode entirely -- the common case. | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 343 | my $path; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 343 | ▶ | if ($raw_path !~ /[%\x80-\xff]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 340 | $path = $raw_path; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 3 | my $unescaped = uri_unescape($raw_path); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 3 | ▶ | $path = eval { decode('UTF-8', $unescaped, Encode::FB_CROAK) } // $unescaped; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
eval {
do {
&decode("UTF-8", $unescaped, 1)
}
} // $unescaped
MC/DC0%0/2
eval {
do {
&decode("UTF-8", $unescaped, 1)
}
} // $unescaped
eval {
do {
&decode("UTF-8", $unescaped, 1)
}
}
$unescaped
Truth table66%2/3
A // B
A= eval {
do {
&decode("UTF-8", $unescaped, 1)
}
}B= $unescaped
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | # Build headers array with lowercase names | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 343 | my @headers; | |||||||||||||||||||||||||||||||||||||||||
| 278 | my $content_length; | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 343 | my $chunked = 0; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 343 | my $te_bad_order = 0; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 343 | my $te_unsupported = 0; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 343 | my $expect_continue = 0; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 343 | my @cookie_values; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 343 | for my $key (keys %env) { | |||||||||||||||||||||||||||||||||||||||||
| 286 | # Optimized: use index() + substr() instead of regex (faster per NYTProf) | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 3040 | ▶ | if (index($key, 'HTTP_') == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 957 | my $header_name = lc(substr($key, 5)); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 957 | $header_name =~ tr/_/-/; # Optimized: tr/// is faster than s///g | |||||||||||||||||||||||||||||||||||||||||
| 290 | 957 | my $value = $env{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | # Handle Cookie header normalization | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 957 | ▶ | if ($header_name eq 'cookie') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | push @cookie_values, $value; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # RFC 9112 Section 6.1: chunked must be the final transfer coding | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 956 | ▶ | if ($header_name eq 'transfer-encoding') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 7 | my @codings = map { s/^\s+|\s+$//gr } split /,/, lc($value); | |||||||||||||||||||||||||||||||||||||||||
| 301 | 7 | ▶ | if (@codings && $codings[-1] eq 'chunked') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@codings and $codings[-1] eq "chunked"
MC/DC50%1/2
@codings and $codings[-1] eq "chunked"
@codings
$codings[-1] eq "chunked"
Truth table66%2/3
A and B
A= @codingsB= $codings[-1] eq "chunked"
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 5 | $chunked = 1; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | } elsif (grep { $_ eq 'chunked' } @codings) { | |||||||||||||||||||||||||||||||||||||||||
| 304 | # chunked present but not final — protocol error | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | $te_bad_order = 1; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | $te_unsupported = 1; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | # Check for Expect: 100-continue | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 956 | ▶ | if ($header_name eq 'expect' && lc($value) eq '100-continue') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$header_name eq "expect" and lc $value eq "100-continue"
MC/DC50%1/2
$header_name eq "expect" and lc $value eq "100-continue"
$header_name eq "expect"
lc $value eq "100-continue"
Truth table66%2/3
A and B
A= $header_name eq "expect"B= lc $value eq "100-continue"
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 2 | $expect_continue = 1; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 956 | push @headers, [$header_name, $value]; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | # Add normalized cookie header if present | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 343 | ▶ | if (@cookie_values) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | push @headers, ['cookie', join('; ', @cookie_values)]; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | # Check header count limit (DoS protection) | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 343 | ▶ | if (@headers > $self->{max_header_count}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 1 | return ({ error => 431, message => 'Request Header Fields Too Large' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | # Add content-type and content-length from env | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 342 | ▶ | if (defined $env{CONTENT_TYPE}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 6 | push @headers, ['content-type', $env{CONTENT_TYPE}]; | |||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 342 | ▶ | if (defined $env{CONTENT_LENGTH}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 19 | my $cl_value = $env{CONTENT_LENGTH}; | |||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | # RFC 7230 Section 3.3.2: Content-Length = 1*DIGIT | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # Must be only digits, no whitespace, no negative sign | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 19 | ▶ | if ($cl_value !~ /^\d+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 4 | return ({ error => 400, message => 'Bad Request' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | # Check for unreasonably large values (>2GB indicates potential DoS) | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # Using string length check to avoid Perl's numeric conversion issues | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 15 | ▶ | if (length($cl_value) > 10 || $cl_value > 2_147_483_647) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $cl_value > 10 or $cl_value > 2147483647
MC/DC50%1/2
length $cl_value > 10 or $cl_value > 2147483647
length $cl_value > 10
$cl_value > 2147483647
Truth table66%2/3
A or B
A= length $cl_value > 10B= $cl_value > 2147483647
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 1 | return ({ error => 413, message => 'Payload Too Large' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | 14 | push @headers, ['content-length', $cl_value]; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 14 | $content_length = $cl_value + 0; | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | # Reject unsupported Transfer-Encoding before checking CL/TE conflict | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 337 | ▶ | if ($te_bad_order) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 1 | return ({ error => 400, message => 'chunked must be the final Transfer-Encoding' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 336 | ▶ | if ($te_unsupported) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 1 | return ({ error => 501, message => 'Unsupported Transfer-Encoding' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | # RFC 9112 Section 6.3.3: reject requests with both Transfer-Encoding | ||||||||||||||||||||||||||||||||||||||||||
| 362 | # and Content-Length to prevent request smuggling (CL/TE desync) | ||||||||||||||||||||||||||||||||||||||||||
| 363 | 335 | ▶ | if ($chunked && defined $content_length) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$chunked and defined $content_length
MC/DC100%2/2
$chunked and defined $content_length
$chunked
defined $content_length
Truth table100%3/3
A and B
A= $chunkedB= defined $content_length
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | 1 | return ({ error => 400, message => 'Transfer-Encoding and Content-Length are mutually exclusive' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | # Determine HTTP version (optimized: substr instead of regex) | ||||||||||||||||||||||||||||||||||||||||||
| 368 | 334 | my $http_version = '1.1'; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 334 | ▶ | if ($env{SERVER_PROTOCOL} && index($env{SERVER_PROTOCOL}, 'HTTP/') == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$env{"SERVER_PROTOCOL"} and index($env{"SERVER_PROTOCOL"}, "HTTP/") == 0
MC/DC0%0/2
$env{"SERVER_PROTOCOL"} and index($env{"SERVER_PROTOCOL"}, "HTTP/") == 0
$env{"SERVER_PROTOCOL"}
index($env{"SERVER_PROTOCOL"}, "HTTP/") == 0
Truth table33%1/3
A and B
A= $env{"SERVER_PROTOCOL"}B= index($env{"SERVER_PROTOCOL"}, "HTTP/") == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 334 | $http_version = substr($env{SERVER_PROTOCOL}, 5); | |||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | # RFC 7230 Section 5.4: A client MUST send a Host header field in all | ||||||||||||||||||||||||||||||||||||||||||
| 374 | # HTTP/1.1 request messages. A server MUST respond with a 400 (Bad Request) | ||||||||||||||||||||||||||||||||||||||||||
| 375 | # status code to any HTTP/1.1 request message that lacks a Host header field. | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 334 | ▶ | if ($http_version eq '1.1' && !defined $env{HTTP_HOST}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$http_version eq "1.1" and not defined $env{"HTTP_HOST"}
MC/DC100%2/2
$http_version eq "1.1" and not defined $env{"HTTP_HOST"}
$http_version eq "1.1"
not defined $env{"HTTP_HOST"}
Truth table100%3/3
A and not(B)
A= $http_version eq "1.1"B= not defined $env{"HTTP_HOST"}
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 1 | return ({ error => 400, message => 'Bad Request' }, $header_end + 4); | |||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 333 | my $request = { | |||||||||||||||||||||||||||||||||||||||||
| 381 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 382 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||
| 383 | raw_path => $raw_path, | ||||||||||||||||||||||||||||||||||||||||||
| 384 | query_string => $query_string, | ||||||||||||||||||||||||||||||||||||||||||
| 385 | http_version => $http_version, | ||||||||||||||||||||||||||||||||||||||||||
| 386 | headers => \@headers, | ||||||||||||||||||||||||||||||||||||||||||
| 387 | content_length => $content_length, | ||||||||||||||||||||||||||||||||||||||||||
| 388 | chunked => $chunked, | ||||||||||||||||||||||||||||||||||||||||||
| 389 | expect_continue => $expect_continue, | ||||||||||||||||||||||||||||||||||||||||||
| 390 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 333 | return ($request, $ret); | |||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 321 | ▶ | sub serialize_response_start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_response_start: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 321 | my ($self, $status, $headers, $chunked, $http_version) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 397 | 321 | ▶ | $chunked //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$chunked //= 0
MC/DC100%1/1
$chunked //= 0
$chunked
Truth table100%2/2
A
A= $chunked
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 321 | ▶ | $http_version //= '1.1'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$http_version //= "1.1"
MC/DC100%1/1
$http_version //= "1.1"
$http_version
Truth table100%2/2
A
A= $http_version
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | 321 | ▶ | my $phrase = $STATUS_PHRASES{$status} // 'Unknown'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$STATUS_PHRASES{$status} // "Unknown"
MC/DC100%1/1
$STATUS_PHRASES{$status} // "Unknown"
$STATUS_PHRASES{$status}
Truth table100%2/2
A
A= $STATUS_PHRASES{$status}
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 321 | my $response = "HTTP/$http_version $status $phrase\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | # Serialize headers and detect app-provided Server header in a single pass | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 321 | my $has_server = 0; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 321 | for my $header (@$headers) { | |||||||||||||||||||||||||||||||||||||||||
| 406 | 785 | my ($name, $value) = @$header; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 785 | ▶ | $has_server = 1 if lc($name) eq 'server'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 785 | $name = _validate_header_name($name); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 784 | $value = _validate_header_value($value); | |||||||||||||||||||||||||||||||||||||||||
| 410 | 774 | $response .= "$name: $value\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # Add default Server header if app didn't provide one | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 310 | ▶ | unless ($has_server) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 309 | ▶ | $_server_header //= "Server: PAGI::Server/$PAGI::Server::VERSION\r\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_server_header //= "Server: PAGI::Server/$PAGI::Server::VERSION\r\n"
MC/DC100%1/1
$_server_header //= "Server: PAGI::Server/$PAGI::Server::VERSION\r\n"
$_server_header
Truth table100%2/2
A
A= $_server_header
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 309 | $response .= $_server_header; | |||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | # Add Transfer-Encoding if chunked (HTTP/1.1 only) | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 310 | ▶ | if ($chunked && $http_version eq '1.1') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$chunked and $http_version eq "1.1"
MC/DC100%2/2
$chunked and $http_version eq "1.1"
$chunked
$http_version eq "1.1"
Truth table100%3/3
A and B
A= $chunkedB= $http_version eq "1.1"
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 225 | $response .= "Transfer-Encoding: chunked\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | 310 | $response .= "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 425 | 310 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | sub serialize_response_body { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_response_body: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 1 | my ($self, $chunk, $more, $chunked) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 1 | ▶ | $chunked //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$chunked //= 0
MC/DC0%0/1
$chunked //= 0
$chunked
Truth table50%1/2
A
A= $chunked
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | ▶ | return '' unless defined $chunk && length $chunk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $chunk and length $chunk
MC/DC0%0/2
defined $chunk and length $chunk
defined $chunk
length $chunk
Truth table33%1/3
A and B
A= defined $chunkB= length $chunk
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | ▶ | if ($chunked) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 1 | my $len = sprintf("%x", length($chunk)); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 1 | my $body = "$len\r\n$chunk\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | # Add final chunk if no more data | ||||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | ▶ | if (!$more) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 1 | $body .= "0\r\n\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 1 | return $body; | |||||||||||||||||||||||||||||||||||||||||
| 444 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | return $chunk; | |||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | sub serialize_chunk_end { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_chunk_end: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | return "0\r\n\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 2 | ▶ | sub serialize_continue { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_continue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | return "HTTP/1.1 100 Continue\r\n\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | 10 | ▶ | sub serialize_trailers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_trailers: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | 10 | my ($self, $headers) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 10 | my $trailers = ''; | |||||||||||||||||||||||||||||||||||||||||
| 465 | 10 | for my $header (@$headers) { | |||||||||||||||||||||||||||||||||||||||||
| 466 | 7 | my ($name, $value) = @$header; | |||||||||||||||||||||||||||||||||||||||||
| 467 | 7 | $name = _validate_header_name($name); | |||||||||||||||||||||||||||||||||||||||||
| 468 | 6 | $value = _validate_header_value($value); | |||||||||||||||||||||||||||||||||||||||||
| 469 | 3 | $trailers .= "$name: $value\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 6 | $trailers .= "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 6 | return $trailers; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 469 | ▶ | sub format_date { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_date: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 469 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 469 | my $now = time(); | |||||||||||||||||||||||||||||||||||||||||
| 479 | 469 | ▶ | if ($now != $_cached_date_time) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 247 | $_cached_date_time = $now; | |||||||||||||||||||||||||||||||||||||||||
| 481 | 247 | my @days = qw(Sun Mon Tue Wed Thu Fri Sat); | |||||||||||||||||||||||||||||||||||||||||
| 482 | 247 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||||||||||||||||||||||||||||||||||
| 483 | 247 | my @gmt = gmtime($now); | |||||||||||||||||||||||||||||||||||||||||
| 484 | 247 | $_cached_date = sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", | |||||||||||||||||||||||||||||||||||||||||
| 485 | $days[$gmt[6]], $gmt[3], $months[$gmt[4]], $gmt[5] + 1900, | ||||||||||||||||||||||||||||||||||||||||||
| 486 | $gmt[2], $gmt[1], $gmt[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | 469 | return $_cached_date; | |||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | =head2 parse_chunked_body | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | my ($data, $bytes_consumed, $complete) = $proto->parse_chunked_body($buffer); | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | Parses chunked Transfer-Encoding body from the buffer. Returns: | ||||||||||||||||||||||||||||||||||||||||||
| 496 | - $data: decoded body data (may be empty string) | ||||||||||||||||||||||||||||||||||||||||||
| 497 | - $bytes_consumed: number of bytes consumed from buffer | ||||||||||||||||||||||||||||||||||||||||||
| 498 | - $complete: 1 if final chunk (0-length) was seen, 0 otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | Returns (undef, 0, 0) if more data is needed. | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 17 | ▶ | sub parse_chunked_body { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_chunked_body: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 17 | my ($self, $buffer_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | 17 | ▶ | my $buffer = ref $buffer_ref ? $$buffer_ref : $buffer_ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | 17 | my $data = ''; | |||||||||||||||||||||||||||||||||||||||||
| 509 | 17 | my $total_consumed = 0; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 17 | my $complete = 0; | |||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 17 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 513 | # Find chunk size line | ||||||||||||||||||||||||||||||||||||||||||
| 514 | 28 | my $crlf = index($buffer, "\r\n", $total_consumed); | |||||||||||||||||||||||||||||||||||||||||
| 515 | 28 | ▶ | last if $crlf < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | # Parse chunk size (hex) | ||||||||||||||||||||||||||||||||||||||||||
| 518 | 28 | my $size_line = substr($buffer, $total_consumed, $crlf - $total_consumed); | |||||||||||||||||||||||||||||||||||||||||
| 519 | 28 | $size_line =~ s/;.*//; # Remove chunk extensions | |||||||||||||||||||||||||||||||||||||||||
| 520 | 28 | $size_line =~ s/^\s+|\s+$//g; # Trim whitespace | |||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | # Validate chunk size is valid hex (RFC 7230 Section 4.1) | ||||||||||||||||||||||||||||||||||||||||||
| 523 | 28 | ▶ | if ($size_line eq '' || $size_line !~ /^[0-9a-fA-F]+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$size_line eq "" or not $size_line =~ /^[0-9a-fA-F]+$/
MC/DC100%2/2
$size_line eq "" or not $size_line =~ /^[0-9a-fA-F]+$/
$size_line eq ""
not $size_line =~ /^[0-9a-fA-F]+$/
Truth table100%3/3
A or not(B)
A= $size_line eq ""B= not $size_line =~ /^[0-9a-fA-F]+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | 4 | return ({ error => 400, message => 'Invalid chunk size' }, 0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | # Reject obviously oversized chunk sizes before hex() conversion | ||||||||||||||||||||||||||||||||||||||||||
| 528 | # 7 hex digits = max 268MB, 8+ digits certainly exceeds any reasonable limit | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 24 | ▶ | if (length($size_line) > 7) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 1 | return ({ error => 413, message => 'Chunk Too Large' }, 0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | 23 | my $chunk_size = hex($size_line); | |||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | # Reject chunks exceeding max_chunk_size (DoS protection) | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 23 | ▶ | if ($chunk_size > $self->{max_chunk_size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | 2 | return ({ error => 413, message => 'Chunk Too Large' }, 0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | # Check if we have the full chunk + trailing CRLF | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 21 | my $chunk_start = $crlf + 2; | |||||||||||||||||||||||||||||||||||||||||
| 542 | 21 | my $chunk_end = $chunk_start + $chunk_size + 2; # +2 for trailing CRLF | |||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | 21 | ▶ | if (length($buffer) < $chunk_end) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 1 | last; # Need more data | |||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | # Extract chunk data | ||||||||||||||||||||||||||||||||||||||||||
| 549 | 20 | ▶ | if ($chunk_size > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 550 | 11 | $data .= substr($buffer, $chunk_start, $chunk_size); | |||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | 20 | $total_consumed = $chunk_end; | |||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | # Check for final chunk | ||||||||||||||||||||||||||||||||||||||||||
| 556 | 20 | ▶ | if ($chunk_size == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 9 | $complete = 1; | |||||||||||||||||||||||||||||||||||||||||
| 558 | 9 | last; | |||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | 10 | return ($data, $total_consumed, $complete); | |||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | 153 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | __END__ | ||||||||||||||||||||||||||||||||||||||||||