Blame


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