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