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