2 # Monitor a remote HTTP server
6 # Connect to the server
11 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
12 alarm($_[0]->{'alarm'} ? $_[0]->{'alarm'} : 10);
14 local $re = $_[0]->{'regexp'};
15 local $method = $_[0]->{'method'} || "HEAD";
16 local $con = &make_http_connection($_[0]->{'host'}, $_[0]->{'port'},
17 $_[0]->{'ssl'}, $method, $_[0]->{'page'});
23 &write_http_connection($con, "Host: $_[0]->{'host'}\r\n");
24 &write_http_connection($con, "User-agent: Webmin\r\n");
25 if ($_[0]->{'user'}) {
26 local $auth = &encode_base64("$_[0]->{'user'}:$_[0]->{'pass'}");
28 &write_http_connection($con, "Authorization: Basic $auth\r\n");
30 &write_http_connection($con, "\r\n");
31 local $line = &read_http_connection($con);
32 $up = $line =~ /^HTTP\/1\..\s+(200|301|302|303)\s+/ ? 1 : 0;
37 $line = &read_http_connection($con);
39 $line =~ /^(\S+):\s+(.*)$/ || last;
45 while(defined($buf = &read_http_connection($con))) {
49 # Check for regexp match
51 if ($data !~ /$re/i) {
53 $desc = "No match on : $re";
62 &close_http_connection($con);
67 die unless $@ eq "alarm\n"; # propagate unexpected errors
73 'time' => time() - $st,
81 if ($_[0]->{'host'}) {
82 $url = ($_[0]->{'ssl'} ? "https" : "http")."://";
83 if (&check_ip6address($_[0]->{'host'})) {
84 $url .= "[".$_[0]->{'host'}."]";
87 $url .= $_[0]->{'host'};
89 $url .= ":$_[0]->{'port'}$_[0]->{'page'}";
94 print &ui_table_row($text{'http_url'},
95 &ui_textbox("url", $url, 50), 3);
97 print &ui_table_row($text{'http_alarm'},
98 &ui_opt_textbox("alarm", $_[0]->{'alarm'}, 5, $text{'default'}).
99 " ".$text{'oldfile_secs'});
101 print &ui_table_row($text{'http_method'},
102 &ui_select("method", $_[0]->{'method'},
103 [ [ "HEAD" ], [ "GET" ], [ "POST" ] ]));
105 print &ui_table_row($text{'http_login'},
106 &ui_radio("user_def", $_[0]->{'user'} ? 0 : 1,
107 [ [ 1, $text{'http_none'} ],
108 [ 0, $text{'http_user'}." ".
109 &ui_textbox("huser", $_[0]->{'user'}, 15)." ".
110 $text{'http_pass'}." ".
111 &ui_password("hpass", $_[0]->{'pass'}, 15) ] ]), 3);
113 print &ui_table_row($text{'http_regexp'},
114 &ui_opt_textbox("regexp", $_[0]->{'regexp'}, 50, $text{'http_none2'}),
118 sub parse_http_dialog
120 local ($host, $port, $page, $ssl) = &parse_http_url($in{'url'});
122 $_[0]->{'ssl'} = $ssl;
123 $_[0]->{'host'} = $host;
124 $_[0]->{'port'} = $port;
125 $_[0]->{'page'} = $page;
128 &error($text{'http_eurl'});
131 if ($in{'alarm_def'}) {
132 delete($_[0]->{'alarm'});
135 $in{'alarm'} =~ /^\d+$/ || &error($text{'http_ealarm'});
136 $_[0]->{'alarm'} = $in{'alarm'};
139 if ($in{'user_def'}) {
140 delete($_[0]->{'user'});
141 delete($_[0]->{'pass'});
144 $in{'huser'} || &error($text{'http_euser'});
145 $_[0]->{'user'} = $in{'huser'};
146 $_[0]->{'pass'} = $in{'hpass'};
149 $_[0]->{'method'} = $in{'method'};
151 if ($in{'regexp_def'}) {
152 delete($_[0]->{'regexp'});
155 $in{'regexp'} || &error($text{'http_eregexp'});
156 $_[0]->{'regexp'} = $in{'regexp'};
157 $in{'method'} eq 'HEAD' && &error($text{'http_ehead'});