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
32 94a3f4e9 2024-03-30 thomas chomp;
33 94a3f4e9 2024-03-30 thomas say;
34 94a3f4e9 2024-03-30 thomas
35 94a3f4e9 2024-03-30 thomas last if /^$/;
36 94a3f4e9 2024-03-30 thomas
37 94a3f4e9 2024-03-30 thomas if (m/^POST/) {
38 94a3f4e9 2024-03-30 thomas die "bad http request" unless m,^POST / HTTP/1.1$,;
39 94a3f4e9 2024-03-30 thomas next;
40 94a3f4e9 2024-03-30 thomas }
41 94a3f4e9 2024-03-30 thomas
42 94a3f4e9 2024-03-30 thomas if (m/^Host:/) {
43 94a3f4e9 2024-03-30 thomas die "bad Host header" unless /^Host: localhost:$port$/;
44 94a3f4e9 2024-03-30 thomas next;
45 94a3f4e9 2024-03-30 thomas }
46 94a3f4e9 2024-03-30 thomas
47 94a3f4e9 2024-03-30 thomas if (m/^Content-Type/) {
48 94a3f4e9 2024-03-30 thomas die "bad content-type header"
49 94a3f4e9 2024-03-30 thomas unless m,Content-Type: application/json$,;
50 94a3f4e9 2024-03-30 thomas next;
51 94a3f4e9 2024-03-30 thomas }
52 94a3f4e9 2024-03-30 thomas
53 94a3f4e9 2024-03-30 thomas if (m/^Content-Length/) {
54 94a3f4e9 2024-03-30 thomas die "double content-length" if defined $clen;
55 94a3f4e9 2024-03-30 thomas die "bad content-length header"
56 94a3f4e9 2024-03-30 thomas unless m/Content-Length: (\d+)$/;
57 94a3f4e9 2024-03-30 thomas $clen = $1;
58 94a3f4e9 2024-03-30 thomas next;
59 94a3f4e9 2024-03-30 thomas }
60 94a3f4e9 2024-03-30 thomas
61 94a3f4e9 2024-03-30 thomas if (m/Connection/) {
62 94a3f4e9 2024-03-30 thomas die "bad connection header"
63 94a3f4e9 2024-03-30 thomas unless m/Connection: close$/;
64 94a3f4e9 2024-03-30 thomas next;
65 94a3f4e9 2024-03-30 thomas }
66 94a3f4e9 2024-03-30 thomas }
67 94a3f4e9 2024-03-30 thomas
68 94a3f4e9 2024-03-30 thomas die "no Content-Length header" unless defined $clen;
69 94a3f4e9 2024-03-30 thomas
70 94a3f4e9 2024-03-30 thomas while ($clen != 0) {
71 94a3f4e9 2024-03-30 thomas my $len = $clen;
72 94a3f4e9 2024-03-30 thomas $len = 512 if $clen > 512;
73 94a3f4e9 2024-03-30 thomas
74 94a3f4e9 2024-03-30 thomas my $r = read($out, my $buf, $len);
75 94a3f4e9 2024-03-30 thomas $clen -= $r;
76 94a3f4e9 2024-03-30 thomas
77 94a3f4e9 2024-03-30 thomas print $buf;
78 94a3f4e9 2024-03-30 thomas }
79 94a3f4e9 2024-03-30 thomas say "";
80 94a3f4e9 2024-03-30 thomas
81 94a3f4e9 2024-03-30 thomas print $in "HTTP/1.1 200 OK\r\n";
82 94a3f4e9 2024-03-30 thomas print $in "Content-Length: 0\r\n";
83 94a3f4e9 2024-03-30 thomas print $in "Connection: close\r\n";
84 94a3f4e9 2024-03-30 thomas print $in "\r\n";
85 94a3f4e9 2024-03-30 thomas
86 94a3f4e9 2024-03-30 thomas close $in;
87 94a3f4e9 2024-03-30 thomas close $out;
88 94a3f4e9 2024-03-30 thomas
89 94a3f4e9 2024-03-30 thomas waitpid($pid, 0);
90 94a3f4e9 2024-03-30 thomas exit $? >> 8;