| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::FrontEnd::Web - web FrontEnd |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::FrontEnd::Web; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use IO::Socket; |
| 13 |
use IO::Select; |
| 14 |
use CGI; |
| 15 |
use Debconf::Gettext; |
| 16 |
use base qw(Debconf::FrontEnd); |
| 17 |
|
| 18 |
=head1 DESCRIPTION |
| 19 |
|
| 20 |
This is a FrontEnd that acts as a small, stupid web server. It is worth noting |
| 21 |
that this doesn't worry about security at all, so it really isn't ready for |
| 22 |
use. It's a proof-of-concept only. In fact, it's probably the crappiest web |
| 23 |
server ever. It only accepts one client at a time! |
| 24 |
|
| 25 |
=head1 FIELDS |
| 26 |
|
| 27 |
=over 4 |
| 28 |
|
| 29 |
=item port |
| 30 |
|
| 31 |
The port to bind to. |
| 32 |
|
| 33 |
=cut |
| 34 |
|
| 35 |
=back |
| 36 |
|
| 37 |
=head1 METHODS |
| 38 |
|
| 39 |
=over 4 |
| 40 |
|
| 41 |
=item init |
| 42 |
|
| 43 |
Bind to the port. |
| 44 |
|
| 45 |
=cut |
| 46 |
|
| 47 |
sub init { |
| 48 |
my $this=shift; |
| 49 |
|
| 50 |
$this->SUPER::init(@_); |
| 51 |
|
| 52 |
$this->port(8001) unless defined $this->port; |
| 53 |
$this->formid(0); |
| 54 |
$this->interactive(1); |
| 55 |
$this->capb('backup'); |
| 56 |
$this->need_tty(0); |
| 57 |
|
| 58 |
# Bind to the port. |
| 59 |
$this->server(IO::Socket::INET->new( |
| 60 |
LocalPort => $this->port, |
| 61 |
Proto => 'tcp', |
| 62 |
Listen => 1, |
| 63 |
Reuse => 1, |
| 64 |
LocalAddr => '127.0.0.1', |
| 65 |
)) || die "Can't bind to ".$this->port.": $!"; |
| 66 |
|
| 67 |
print STDERR sprintf(gettext("Note: Debconf is running in web mode. Go to http://localhost:%i/"),$this->port)."\n"; |
| 68 |
} |
| 69 |
|
| 70 |
=item client |
| 71 |
|
| 72 |
This method ensures that a client is connected to the web server and waiting for |
| 73 |
input. If there is no client, it blocks until one connects. As a side affect, when |
| 74 |
a client connects, this also reads in any HTTP commands it has for us and puts them |
| 75 |
in the commands field. |
| 76 |
|
| 77 |
=cut |
| 78 |
|
| 79 |
sub client { |
| 80 |
my $this=shift; |
| 81 |
|
| 82 |
$this->{client}=shift if @_; |
| 83 |
return $this->{client} if $this->{client}; |
| 84 |
|
| 85 |
my $select=IO::Select->new($this->server); |
| 86 |
1 while ! $select->can_read(1); |
| 87 |
my $client=$this->server->accept; |
| 88 |
my $commands=''; |
| 89 |
while (<$client>) { |
| 90 |
last if $_ eq "\r\n"; |
| 91 |
$commands.=$_; |
| 92 |
} |
| 93 |
$this->commands($commands); |
| 94 |
$this->{client}=$client; |
| 95 |
} |
| 96 |
|
| 97 |
=item closeclient |
| 98 |
|
| 99 |
Forcibly close the current client's connection to the web server. |
| 100 |
|
| 101 |
=cut |
| 102 |
|
| 103 |
sub closeclient { |
| 104 |
my $this=shift; |
| 105 |
|
| 106 |
close $this->client; |
| 107 |
$this->client(''); |
| 108 |
} |
| 109 |
|
| 110 |
=item showclient |
| 111 |
|
| 112 |
Displays the passed text to the client. Can be called multiple times to |
| 113 |
build up a page. |
| 114 |
|
| 115 |
=cut |
| 116 |
|
| 117 |
sub showclient { |
| 118 |
my $this=shift; |
| 119 |
my $page=shift; |
| 120 |
|
| 121 |
my $client=$this->client; |
| 122 |
print $client $page; |
| 123 |
} |
| 124 |
|
| 125 |
=item go |
| 126 |
|
| 127 |
This overrides to go method in the parent FrontEnd. It goes through each |
| 128 |
pending Element and asks it to return the html that corresponds to that |
| 129 |
Element. It bundles all the html together into a web page and displays the |
| 130 |
web page to the client. Then it waits for the client to fill out the form, |
| 131 |
parses the client's response and uses that to set values in the database. |
| 132 |
|
| 133 |
=cut |
| 134 |
|
| 135 |
sub go { |
| 136 |
my $this=shift; |
| 137 |
|
| 138 |
$this->backup(''); |
| 139 |
|
| 140 |
my $httpheader="HTTP/1.0 200 Ok\nContent-type: text/html\n\n"; |
| 141 |
my $form=''; |
| 142 |
my $id=0; |
| 143 |
my %idtoelt; |
| 144 |
foreach my $elt (@{$this->elements}) { |
| 145 |
# Each element has a unique id that it'll use on the form. |
| 146 |
$idtoelt{$id}=$elt; |
| 147 |
$elt->id($id++); |
| 148 |
my $html=$elt->show; |
| 149 |
if ($html ne '') { |
| 150 |
$form.=$html."<hr>\n"; |
| 151 |
} |
| 152 |
} |
| 153 |
# If the elements generated no html, return now so we |
| 154 |
# don't display empty pages. |
| 155 |
return 1 if $form eq ''; |
| 156 |
|
| 157 |
# Each form sent out has a unique id. |
| 158 |
my $formid=$this->formid(1 + $this->formid); |
| 159 |
|
| 160 |
# Add the standard header to the html we already have. |
| 161 |
$form="<html>\n<title>".$this->title."</title>\n<body>\n". |
| 162 |
"<form><input type=hidden name=formid value=$formid>\n". |
| 163 |
$form."<p>\n"; |
| 164 |
|
| 165 |
# Should the back button be displayed? |
| 166 |
if ($this->capb_backup) { |
| 167 |
$form.="<input type=submit value=".gettext("Back")." name=back>\n"; |
| 168 |
} |
| 169 |
$form.="<input type=submit value=".gettext("Next").">\n"; |
| 170 |
$form.="</form>\n</body>\n</html>\n"; |
| 171 |
|
| 172 |
my $query; |
| 173 |
# We'll loop here until we get a valid response from a client. |
| 174 |
do { |
| 175 |
$this->showclient($httpheader . $form); |
| 176 |
|
| 177 |
# Now get the next connection to us, which causes any http |
| 178 |
# commands to be read. |
| 179 |
$this->closeclient; |
| 180 |
$this->client; |
| 181 |
|
| 182 |
# Now parse the http commands and get the query string out |
| 183 |
# of it. |
| 184 |
my @get=grep { /^GET / } split(/\r\n/, $this->commands); |
| 185 |
my $get=shift @get; |
| 186 |
my ($qs)=$get=~m/^GET\s+.*?\?(.*?)(?:\s+.*)?$/; |
| 187 |
|
| 188 |
# Now parse the query string. |
| 189 |
$query=CGI->new($qs); |
| 190 |
} until (defined $query->param('formid') && |
| 191 |
$query->param('formid') eq $formid); |
| 192 |
|
| 193 |
# Did they hit the back button? If so, ignore their input and inform |
| 194 |
# the ConfModule of this. |
| 195 |
if ($this->capb_backup && defined $query->param('back') && |
| 196 |
$query->param('back') ne '') { |
| 197 |
return ''; |
| 198 |
} |
| 199 |
|
| 200 |
# Now it's just a matter of matching up the element id's with values |
| 201 |
# from the form, and passing the values from the form into the |
| 202 |
# elements. |
| 203 |
foreach my $id ($query->param) { |
| 204 |
next unless $idtoelt{$id}; |
| 205 |
|
| 206 |
$idtoelt{$id}->value($query->param($id)); |
| 207 |
delete $idtoelt{$id}; |
| 208 |
} |
| 209 |
# If there are any elements that did not get a result back, that in |
| 210 |
# itself is significant. For example, an unchecked checkbox will not |
| 211 |
# get anything back. |
| 212 |
foreach my $elt (values %idtoelt) { |
| 213 |
$elt->value(''); |
| 214 |
} |
| 215 |
|
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
=back |
| 220 |
|
| 221 |
=head1 AUTHOR |
| 222 |
|
| 223 |
Joey Hess <[email protected]> |
| 224 |
|
| 225 |
=cut |
| 226 |
|
| 227 |
1 |