| 1 | # ex:ts=8 sw=4: | ||||||||||||||||||||||||||||
| 2 | # $OpenBSD$ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@senzilla.io> | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||||||||||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||||||||||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||||||||||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||||||||||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||||||||||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||||||||||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||||||||||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Protocol::HAP::HTTP; | ||||||||||||||||||||||||||||
| 21 | 1 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Protocol::HAP::HTTP - one HTTP/1.1 codec, for both ends of a connection. | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # The module is functions over strings. It opens no socket, holds no | ||||||||||||||||||||||||||||
| 26 | # connection state, and never logs. A server calls parse_request and | ||||||||||||||||||||||||||||
| 27 | # build_response; a client calls build_request and parse_response. | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # message_complete is the framing. A stream socket gives a reader | ||||||||||||||||||||||||||||
| 30 | # whatever arrived, which is not a message: a request can span two | ||||||||||||||||||||||||||||
| 31 | # reads, and two requests can share one. A reader that parses each | ||||||||||||||||||||||||||||
| 32 | # read as a whole message drops the second request and mangles the | ||||||||||||||||||||||||||||
| 33 | # split one. The caller keeps a buffer and asks this function how much | ||||||||||||||||||||||||||||
| 34 | # of it is a message. | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # The module knows nothing about any protocol built on HTTP. A status | ||||||||||||||||||||||||||||
| 37 | # code that belongs to one application, and the header defaults that | ||||||||||||||||||||||||||||
| 38 | # one application wants, are arguments. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # The bound on a message that a caller does not set for itself. The | ||||||||||||||||||||||||||||
| 41 | # value is generous for a request and small enough that an unpaired | ||||||||||||||||||||||||||||
| 42 | # client cannot make a server hold megabytes. | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use constant DEFAULT_MAX_SIZE => 65536; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # The status texts of the codes that RFC 9110 defines. A caller with a | ||||||||||||||||||||||||||||
| 46 | # code of its own passes the text. | ||||||||||||||||||||||||||||
| 47 | 1 | my %STATUS_TEXT = ( | |||||||||||||||||||||||||||
| 48 | 200 => 'OK', | ||||||||||||||||||||||||||||
| 49 | 201 => 'Created', | ||||||||||||||||||||||||||||
| 50 | 204 => 'No Content', | ||||||||||||||||||||||||||||
| 51 | 207 => 'Multi-Status', | ||||||||||||||||||||||||||||
| 52 | 304 => 'Not Modified', | ||||||||||||||||||||||||||||
| 53 | 400 => 'Bad Request', | ||||||||||||||||||||||||||||
| 54 | 401 => 'Unauthorized', | ||||||||||||||||||||||||||||
| 55 | 403 => 'Forbidden', | ||||||||||||||||||||||||||||
| 56 | 404 => 'Not Found', | ||||||||||||||||||||||||||||
| 57 | 405 => 'Method Not Allowed', | ||||||||||||||||||||||||||||
| 58 | 408 => 'Request Timeout', | ||||||||||||||||||||||||||||
| 59 | 413 => 'Content Too Large', | ||||||||||||||||||||||||||||
| 60 | 500 => 'Internal Server Error', | ||||||||||||||||||||||||||||
| 61 | 503 => 'Service Unavailable', | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # message_complete($buffer, %args): | ||||||||||||||||||||||||||||
| 65 | # Report how many bytes at the front of $buffer form one whole | ||||||||||||||||||||||||||||
| 66 | # message. | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # %args: | ||||||||||||||||||||||||||||
| 69 | # max_size => $bytes refuse a message larger than this | ||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||
| 71 | # The function returns the length of the message, 0 when more | ||||||||||||||||||||||||||||
| 72 | # bytes are necessary, and undef when the message is over the | ||||||||||||||||||||||||||||
| 73 | # limit. A caller that gets undef closes the connection: the peer | ||||||||||||||||||||||||||||
| 74 | # is either broken or hostile, and no further byte can make the | ||||||||||||||||||||||||||||
| 75 | # message valid. | ||||||||||||||||||||||||||||
| 76 | 77 | ▶ | sub message_complete ( $buffer, %args ) | ||||||||||||||||||||||||||
|
Subroutine
message_complete: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 77 | ▶ | my $max_size = $args{max_size} // DEFAULT_MAX_SIZE; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"max_size"} // 65536
MC/DC100%1/1
$args{"max_size"} // 65536
$args{"max_size"}
Truth table100%2/2
A
A= $args{"max_size"}
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 77 | my $end = index $buffer, "\r\n\r\n"; | |||||||||||||||||||||||||||
| 81 | 77 | ▶ | if ( $end < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # The header block alone is already over the limit, so | ||||||||||||||||||||||||||||
| 84 | # no body can make it fit | ||||||||||||||||||||||||||||
| 85 | 56 | ▶ | return if length($buffer) > $max_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 55 | return 0; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 21 | my $head_length = $end + 4; | |||||||||||||||||||||||||||
| 90 | 21 | my $head = substr $buffer, 0, $end; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # The trailing \r is part of the line terminator, and $ under /m | ||||||||||||||||||||||||||||
| 93 | # matches before the \n only. A class that omits it makes the | ||||||||||||||||||||||||||||
| 94 | # header match only when it is the last one in the block. | ||||||||||||||||||||||||||||
| 95 | 21 | my ($length) = $head =~ /^Content-Length:[ \t]*(\d+)[ \t\r]*$/mi; | |||||||||||||||||||||||||||
| 96 | 21 | ▶ | $length //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$length //= 0
MC/DC100%1/1
$length //= 0
$length
Truth table100%2/2
A
A= $length
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 21 | my $total = $head_length + $length; | |||||||||||||||||||||||||||
| 99 | 21 | ▶ | return if $total > $max_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 19 | ▶ | return 0 if length($buffer) < $total; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 5 | return $total; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # parse_request($data): | ||||||||||||||||||||||||||||
| 106 | # Parse one request message. The function returns a hashref with | ||||||||||||||||||||||||||||
| 107 | # method, path, version, headers and body. Header names are | ||||||||||||||||||||||||||||
| 108 | # lowercase, because a peer chooses their case and a caller must | ||||||||||||||||||||||||||||
| 109 | # not. | ||||||||||||||||||||||||||||
| 110 | # | ||||||||||||||||||||||||||||
| 111 | # The function returns undef when the request line is not a | ||||||||||||||||||||||||||||
| 112 | # request line. | ||||||||||||||||||||||||||||
| 113 | 8 | ▶ | sub parse_request ($data) | ||||||||||||||||||||||||||
|
Subroutine
parse_request: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | 8 | my ( $head, $body ) = _split_message($data); | |||||||||||||||||||||||||||
| 116 | 8 | my @lines = split /\r?\n/, $head; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 8 | ▶ | my $request_line = shift @lines // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift @lines // ""
MC/DC100%1/1
shift @lines // ""
shift @lines
Truth table100%2/2
A
A= shift @lines
| |||||||||||||||||||||||||||||
| 119 | return | ||||||||||||||||||||||||||||
| 120 | 8 | ▶ | unless $request_line =~ m{^(\S+)[ \t]+(\S+)[ \t]+HTTP/(\d+\.\d+)$}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | return { | ||||||||||||||||||||||||||||
| 123 | 6 | method => $1, | |||||||||||||||||||||||||||
| 124 | path => $2, | ||||||||||||||||||||||||||||
| 125 | version => $3, | ||||||||||||||||||||||||||||
| 126 | headers => _parse_headers( \@lines ), | ||||||||||||||||||||||||||||
| 127 | body => $body, | ||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # parse_response($data): | ||||||||||||||||||||||||||||
| 132 | # Parse one response message. The function returns a hashref with | ||||||||||||||||||||||||||||
| 133 | # status, reason, version, headers and body, or undef when the | ||||||||||||||||||||||||||||
| 134 | # status line is not a status line. | ||||||||||||||||||||||||||||
| 135 | 4 | ▶ | sub parse_response ($data) | ||||||||||||||||||||||||||
|
Subroutine
parse_response: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 4 | my ( $head, $body ) = _split_message($data); | |||||||||||||||||||||||||||
| 138 | 4 | my @lines = split /\r?\n/, $head; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 4 | ▶ | my $status_line = shift @lines // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift @lines // ""
MC/DC0%0/1
shift @lines // ""
shift @lines
Truth table50%1/2
A
A= shift @lines
| |||||||||||||||||||||||||||||
| 141 | return | ||||||||||||||||||||||||||||
| 142 | 4 | ▶ | unless $status_line =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | m{^HTTP/(\d+\.\d+)[ \t]+(\d{3})(?:[ \t]+(.*))?$}; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | return { | ||||||||||||||||||||||||||||
| 146 | 2 | ▶ | version => $1, | ||||||||||||||||||||||||||
|
Condition100%2/2
$3 // ""
MC/DC100%1/1
$3 // ""
$3
Truth table100%2/2
A
A= $3
| |||||||||||||||||||||||||||||
| 147 | status => $2, | ||||||||||||||||||||||||||||
| 148 | reason => $3 // '', | ||||||||||||||||||||||||||||
| 149 | headers => _parse_headers( \@lines ), | ||||||||||||||||||||||||||||
| 150 | body => $body, | ||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # build_request(%args): | ||||||||||||||||||||||||||||
| 155 | # Build one request message. | ||||||||||||||||||||||||||||
| 156 | # | ||||||||||||||||||||||||||||
| 157 | # %args: | ||||||||||||||||||||||||||||
| 158 | # method => $string default GET | ||||||||||||||||||||||||||||
| 159 | # path => $string default / | ||||||||||||||||||||||||||||
| 160 | # version => $string default 1.1 | ||||||||||||||||||||||||||||
| 161 | # headers => \%headers sent in sorted order | ||||||||||||||||||||||||||||
| 162 | # body => $bytes | ||||||||||||||||||||||||||||
| 163 | # | ||||||||||||||||||||||||||||
| 164 | # The function adds Content-Length when the caller gave a body | ||||||||||||||||||||||||||||
| 165 | # and no such header. A peer cannot frame a body without it. | ||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub build_request (%args) | ||||||||||||||||||||||||||
|
Subroutine
build_request: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||
| 168 | 3 | ▶ | my $method = $args{method} // 'GET'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"method"} // "GET"
MC/DC100%1/1
$args{"method"} // "GET"
$args{"method"}
Truth table100%2/2
A
A= $args{"method"}
| |||||||||||||||||||||||||||||
| 169 | 3 | ▶ | my $path = $args{path} // '/'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"path"} // "/"
MC/DC100%1/1
$args{"path"} // "/"
$args{"path"}
Truth table100%2/2
A
A= $args{"path"}
| |||||||||||||||||||||||||||||
| 170 | 3 | ▶ | my $version = $args{version} // '1.1'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"version"} // "1.1"
MC/DC0%0/1
$args{"version"} // "1.1"
$args{"version"}
Truth table50%1/2
A
A= $args{"version"}
| |||||||||||||||||||||||||||||
| 171 | 3 | my $body = $args{body}; | |||||||||||||||||||||||||||
| 172 | 3 | ▶ | my %headers = %{ $args{headers} // {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"headers"} // {}
MC/DC100%1/1
$args{"headers"} // {}
$args{"headers"}
Truth table100%2/2
A
A= $args{"headers"}
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 3 | ▶ | $headers{'Content-Length'} = length $body | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $body and not _has_header(\%headers, "content-length")
MC/DC100%2/2
defined $body and not _has_header(\%headers, "content-length")
defined $body
not _has_header(\%headers, "content-length")
Truth table100%3/3
A and not(B)
A= defined $bodyB= not _has_header(\%headers, "content-length")
| |||||||||||||||||||||||||||||
| 175 | if defined $body && !_has_header( \%headers, 'content-length' ); | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | return | ||||||||||||||||||||||||||||
| 178 | 3 | ▶ | "$method $path HTTP/$version\r\n" | ||||||||||||||||||||||||||
|
Condition100%2/2
$body // ""
MC/DC100%1/1
$body // ""
$body
Truth table100%2/2
A
A= $body
| |||||||||||||||||||||||||||||
| 179 | . _format_headers( \%headers ) . "\r\n" | ||||||||||||||||||||||||||||
| 180 | . ( $body // '' ); | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # build_response(%args): | ||||||||||||||||||||||||||||
| 184 | # Build one response message. | ||||||||||||||||||||||||||||
| 185 | # | ||||||||||||||||||||||||||||
| 186 | # %args: | ||||||||||||||||||||||||||||
| 187 | # status => $code default 200 | ||||||||||||||||||||||||||||
| 188 | # status_text => $string default from the code | ||||||||||||||||||||||||||||
| 189 | # version => $string default 1.1 | ||||||||||||||||||||||||||||
| 190 | # headers => \%headers | ||||||||||||||||||||||||||||
| 191 | # body => $bytes | ||||||||||||||||||||||||||||
| 192 | # | ||||||||||||||||||||||||||||
| 193 | # The function always adds Content-Length: a response with no | ||||||||||||||||||||||||||||
| 194 | # length and no close is a response that the peer waits out. | ||||||||||||||||||||||||||||
| 195 | 7 | ▶ | sub build_response (%args) | ||||||||||||||||||||||||||
|
Subroutine
build_response: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 196 | { | ||||||||||||||||||||||||||||
| 197 | 7 | ▶ | my $status = $args{status} // 200; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"status"} // 200
MC/DC0%0/1
$args{"status"} // 200
$args{"status"}
Truth table50%1/2
A
A= $args{"status"}
| |||||||||||||||||||||||||||||
| 198 | 7 | ▶ | my $version = $args{version} // '1.1'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"version"} // "1.1"
MC/DC0%0/1
$args{"version"} // "1.1"
$args{"version"}
Truth table50%1/2
A
A= $args{"version"}
| |||||||||||||||||||||||||||||
| 199 | 7 | ▶ | my $body = $args{body} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"body"} // ""
MC/DC100%1/1
$args{"body"} // ""
$args{"body"}
Truth table100%2/2
A
A= $args{"body"}
| |||||||||||||||||||||||||||||
| 200 | 7 | ▶ | my $text = $args{status_text} // status_text($status); | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{"status_text"} // status_text($status)
MC/DC0%0/2
$args{"status_text"} // status_text($status)
$args{"status_text"}
status_text($status)
Truth table66%2/3
A // B
A= $args{"status_text"}B= status_text($status)
| |||||||||||||||||||||||||||||
| 201 | 7 | ▶ | my %headers = %{ $args{headers} // {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"headers"} // {}
MC/DC100%1/1
$args{"headers"} // {}
$args{"headers"}
Truth table100%2/2
A
A= $args{"headers"}
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 7 | ▶ | $headers{'Content-Length'} = length $body | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | unless _has_header( \%headers, 'content-length' ); | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | return | ||||||||||||||||||||||||||||
| 207 | 7 | "HTTP/$version $status $text\r\n" | |||||||||||||||||||||||||||
| 208 | . _format_headers( \%headers ) . "\r\n" | ||||||||||||||||||||||||||||
| 209 | . $body; | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # status_text($code): | ||||||||||||||||||||||||||||
| 213 | # Return the reason phrase of a status code. An unknown code | ||||||||||||||||||||||||||||
| 214 | # gives 'Unknown': an application with a code of its own passes | ||||||||||||||||||||||||||||
| 215 | # the text to build_response. | ||||||||||||||||||||||||||||
| 216 | 10 | ▶ | sub status_text ($code) | ||||||||||||||||||||||||||
|
Subroutine
status_text: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 217 | { | ||||||||||||||||||||||||||||
| 218 | 10 | ▶ | return $STATUS_TEXT{$code} // 'Unknown'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$STATUS_TEXT{$code} // "Unknown"
MC/DC100%1/1
$STATUS_TEXT{$code} // "Unknown"
$STATUS_TEXT{$code}
Truth table100%2/2
A
A= $STATUS_TEXT{$code}
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # build_event($body): | ||||||||||||||||||||||||||||
| 222 | # Build one EVENT/1.0 notification message [HAP-HTTP ยง14]. The | ||||||||||||||||||||||||||||
| 223 | # message looks like a response, but its protocol name is EVENT, | ||||||||||||||||||||||||||||
| 224 | # so a controller can tell an unsolicited notification from the | ||||||||||||||||||||||||||||
| 225 | # answer to a request it sent. | ||||||||||||||||||||||||||||
| 226 | 1 | ▶ | sub build_event ($body) | ||||||||||||||||||||||||||
|
Subroutine
build_event: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 227 | { | ||||||||||||||||||||||||||||
| 228 | return | ||||||||||||||||||||||||||||
| 229 | 1 | "EVENT/1.0 200 OK\r\n" | |||||||||||||||||||||||||||
| 230 | . "Content-Type: application/hap+json\r\n" | ||||||||||||||||||||||||||||
| 231 | . 'Content-Length: ' | ||||||||||||||||||||||||||||
| 232 | . length($body) | ||||||||||||||||||||||||||||
| 233 | . "\r\n\r\n" | ||||||||||||||||||||||||||||
| 234 | . $body; | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # _split_message($data): | ||||||||||||||||||||||||||||
| 238 | # Split a message into its header block and its body. | ||||||||||||||||||||||||||||
| 239 | 12 | ▶ | sub _split_message ($data) | ||||||||||||||||||||||||||
|
Subroutine
_split_message: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 240 | { | ||||||||||||||||||||||||||||
| 241 | 12 | ▶ | $data //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data //= ""
MC/DC0%0/1
$data //= ""
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 12 | my ( $head, $body ) = split /\r?\n\r?\n/, $data, 2; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 12 | ▶ | return ( $head // '', $body // '' ); | ||||||||||||||||||||||||||
|
Condition100%4/4
$head // ""
$body // ""
MC/DC100%1/1
$head // ""
$head
MC/DC100%1/1
$body // ""
$body
Truth table100%2/2
A
A= $head
Truth table100%2/2
A
A= $body
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # _parse_headers($lines): | ||||||||||||||||||||||||||||
| 249 | # Turn the header lines into a hashref keyed by lowercase name. | ||||||||||||||||||||||||||||
| 250 | # A repeated header keeps its last value, as an assignment does. | ||||||||||||||||||||||||||||
| 251 | 8 | ▶ | sub _parse_headers ($lines) | ||||||||||||||||||||||||||
|
Subroutine
_parse_headers: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 252 | { | ||||||||||||||||||||||||||||
| 253 | 8 | my %headers; | |||||||||||||||||||||||||||
| 254 | 8 | for my $line (@$lines) { | |||||||||||||||||||||||||||
| 255 | 9 | ▶ | next unless $line =~ /^([^:]+):[ \t]*(.*?)[ \t]*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 9 | $headers{ lc $1 } = $2; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 8 | return \%headers; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # _has_header($headers, $name): | ||||||||||||||||||||||||||||
| 263 | # Report if the header set already holds a name, whatever case | ||||||||||||||||||||||||||||
| 264 | # the caller wrote it in. | ||||||||||||||||||||||||||||
| 265 | 9 | ▶ | sub _has_header ( $headers, $name ) | ||||||||||||||||||||||||||
|
Subroutine
_has_header: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 266 | { | ||||||||||||||||||||||||||||
| 267 | 9 | for my $key ( keys %$headers ) { | |||||||||||||||||||||||||||
| 268 | 12 | ▶ | return 1 if lc $key eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 8 | return 0; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # _format_headers($headers): | ||||||||||||||||||||||||||||
| 275 | # Format the headers, sorted. A stable order makes a response | ||||||||||||||||||||||||||||
| 276 | # comparable byte for byte, which a conformance test needs. | ||||||||||||||||||||||||||||
| 277 | 10 | ▶ | sub _format_headers ($headers) | ||||||||||||||||||||||||||
|
Subroutine
_format_headers: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 278 | { | ||||||||||||||||||||||||||||
| 279 | 10 | my $out = ''; | |||||||||||||||||||||||||||
| 280 | 10 | $out .= "$_: $headers->{$_}\r\n" for sort keys %$headers; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 10 | return $out; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 1 | 1; | |||||||||||||||||||||||||||