1#!/usr/local/bin/perl
2# Proxy an Ajaxterm request to the real port
3use strict;
4use warnings;
5
6BEGIN { push(@INC, ".."); };
7use WebminCore;
8&init_config();
9
10# Parse out port
11$ENV{'PATH_INFO'} =~ /^\/(\d+)(.*)$/ ||
12	&error("Missing or invalid PATH_INFO");
13my $port = $1;
14my $path = $2;
15$| = 1;
16my $meth = $ENV{'REQUEST_METHOD'};
17
18# Connect to the Ajaxterm server, send HTTP request
19my $con = &make_http_connection("localhost", $port, 0, $meth, $path);
20&error($con) if (!ref($con));
21&write_http_connection($con, "Host: localhost\r\n");
22&write_http_connection($con, "User-agent: Webmin\r\n");
23my $cl = $ENV{'CONTENT_LENGTH'};
24&write_http_connection($con, "Content-length: $cl\r\n") if ($cl);
25&write_http_connection($con, "Content-type: $ENV{'CONTENT_TYPE'}\r\n")
26        if ($ENV{'CONTENT_TYPE'});
27&write_http_connection($con, "\r\n");
28my $post;
29if ($cl) {
30        &read_fully(\*STDIN, \$post, $cl);
31        &write_http_connection($con, $post);
32        }
33
34# read back the headers
35my $dummy = &read_http_connection($con);
36my %header;
37my $headers;
38while(1) {
39        my $headline;
40        ($headline = &read_http_connection($con)) =~ s/\r|\n//g;
41        last if (!$headline);
42        $headline =~ /^(\S+):\s+(.*)$/ || &error("Bad header");
43        $header{lc($1)} = $2;
44        $headers .= $headline."\n";
45        }
46print $headers,"\n";
47
48# read back contents
49while(my $buf = &read_http_connection($con, 1024)) {
50	print $buf;
51        }
52&close_http_connection($con);
53
54# Touch status file to indicate it is still running
55my $statusdir = $ENV{'WEBMIN_VAR'}."/ajaxterm";
56if (!-d $statusdir) {
57	&make_dir($statusdir, 0700);
58	}
59my $TOUCH;
60&open_tempfile($TOUCH, ">$statusdir/$port", 0, 1);
61&close_tempfile($TOUCH);
62
63