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