Blame


1 94a3f4e9 2024-03-30 thomas #!/usr/bin/env perl
2 94a3f4e9 2024-03-30 thomas #
3 94a3f4e9 2024-03-30 thomas # Copyright (c) 2024 Omar Polo <op@openbsd.org>
4 94a3f4e9 2024-03-30 thomas #
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.
8 94a3f4e9 2024-03-30 thomas #
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.
16 94a3f4e9 2024-03-30 thomas
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);
20 94a3f4e9 2024-03-30 thomas
21 94a3f4e9 2024-03-30 thomas my $port = 8000;
22 94a3f4e9 2024-03-30 thomas
23 94a3f4e9 2024-03-30 thomas GetOptions("p:i" => \$port)
24 94a3f4e9 2024-03-30 thomas or die("usage: $0 [-p port]\n");
25 94a3f4e9 2024-03-30 thomas
26 94a3f4e9 2024-03-30 thomas my $pid = open2(my $out, my $in, 'nc', '-l', 'localhost', $port);
27 94a3f4e9 2024-03-30 thomas
28 94a3f4e9 2024-03-30 thomas my $clen;
29 94a3f4e9 2024-03-30 thomas while (<$out>) {
30 94a3f4e9 2024-03-30 thomas local $/ = "\r\n";
31 94a3f4e9 2024-03-30 thomas chomp;
32 94a3f4e9 2024-03-30 thomas
33 94a3f4e9 2024-03-30 thomas last if /^$/;
34 94a3f4e9 2024-03-30 thomas
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$,;
37 94a3f4e9 2024-03-30 thomas next;
38 94a3f4e9 2024-03-30 thomas }
39 94a3f4e9 2024-03-30 thomas
40 94a3f4e9 2024-03-30 thomas if (m/^Host:/) {
41 94a3f4e9 2024-03-30 thomas die "bad Host header" unless /^Host: localhost:$port$/;
42 94a3f4e9 2024-03-30 thomas next;
43 94a3f4e9 2024-03-30 thomas }
44 94a3f4e9 2024-03-30 thomas
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$,;
48 94a3f4e9 2024-03-30 thomas next;
49 94a3f4e9 2024-03-30 thomas }
50 94a3f4e9 2024-03-30 thomas
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;
56 94a3f4e9 2024-03-30 thomas next;
57 94a3f4e9 2024-03-30 thomas }
58 94a3f4e9 2024-03-30 thomas
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$/;
62 94a3f4e9 2024-03-30 thomas next;
63 94a3f4e9 2024-03-30 thomas }
64 94a3f4e9 2024-03-30 thomas }
65 94a3f4e9 2024-03-30 thomas
66 94a3f4e9 2024-03-30 thomas die "no Content-Length header" unless defined $clen;
67 94a3f4e9 2024-03-30 thomas
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;
71 94a3f4e9 2024-03-30 thomas
72 94a3f4e9 2024-03-30 thomas my $r = read($out, my $buf, $len);
73 94a3f4e9 2024-03-30 thomas $clen -= $r;
74 94a3f4e9 2024-03-30 thomas
75 94a3f4e9 2024-03-30 thomas print $buf;
76 94a3f4e9 2024-03-30 thomas }
77 94a3f4e9 2024-03-30 thomas say "";
78 94a3f4e9 2024-03-30 thomas
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";
83 94a3f4e9 2024-03-30 thomas
84 94a3f4e9 2024-03-30 thomas close $in;
85 94a3f4e9 2024-03-30 thomas close $out;
86 94a3f4e9 2024-03-30 thomas
87 94a3f4e9 2024-03-30 thomas waitpid($pid, 0);
88 94a3f4e9 2024-03-30 thomas exit $? >> 8;