3 # Copyright (c) 2024 Omar Polo <op@openbsd.org>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 use Getopt::Long qw(:config bundling);
23 GetOptions("p:i" => \$port)
24 or die("usage: $0 [-p port]\n");
26 my $pid = open2(my $out, my $in, 'nc', '-l', 'localhost', $port);
36 die "bad http request" unless m,^POST / HTTP/1.1$,;
41 die "bad Host header" unless /^Host: localhost:$port$/;
45 if (m/^Content-Type/) {
46 die "bad content-type header"
47 unless m,Content-Type: application/json$,;
51 if (m/^Content-Length/) {
52 die "double content-length" if defined $clen;
53 die "bad content-length header"
54 unless m/Content-Length: (\d+)$/;
60 die "bad connection header"
61 unless m/Connection: close$/;
66 die "no Content-Length header" unless defined $clen;
70 $len = 512 if $clen > 512;
72 my $r = read($out, my $buf, $len);
79 print $in "HTTP/1.1 200 OK\r\n";
80 print $in "Content-Length: 0\r\n";
81 print $in "Connection: close\r\n";