1 5565365c 2024-03-27 op #!/usr/bin/env perl
3 5565365c 2024-03-27 op # Copyright (c) 2024 Omar Polo <op@openbsd.org>
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.
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.
18 5565365c 2024-03-27 op use IPC::Open2;
19 5565365c 2024-03-27 op use Getopt::Long qw(:config bundling);
21 5565365c 2024-03-27 op my $port = 8000;
23 5565365c 2024-03-27 op GetOptions("p:i" => \$port)
24 5565365c 2024-03-27 op or die("usage: $0 [-p port]\n");
26 5565365c 2024-03-27 op my $pid = open2(my $out, my $in, 'nc', '-l', 'localhost', $port);
29 5565365c 2024-03-27 op while (<$out>) {
30 5565365c 2024-03-27 op local $/ = "\r\n";
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$,;
40 5565365c 2024-03-27 op if (m/^Host:/) {
41 5565365c 2024-03-27 op die "bad Host header" unless /^Host: localhost:$port$/;
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$,;
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+)$/;
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$/;
66 5565365c 2024-03-27 op die "no Content-Length header" unless defined $clen;
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;
72 5565365c 2024-03-27 op my $r = read($out, my $buf, $len);
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";
87 5565365c 2024-03-27 op waitpid($pid, 0);