/[pxelator]/lib/PXElator/httpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 42 by dpavlin, Wed Jul 29 17:42:48 2009 UTC revision 71 by dpavlin, Thu Jul 30 22:55:08 2009 UTC
# Line 17  use Carp qw/confess/; Line 17  use Carp qw/confess/;
17  use File::Slurp;  use File::Slurp;
18  #use JSON;  #use JSON;
19  use IO::Socket::INET;  use IO::Socket::INET;
20    use Module::Refresh;
21    
22    our $pids;
23    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25    sub DESTROY {
26            warn "pids ",dump( $pids );
27            foreach ( values %$pids ) {
28                    warn "kill $_";
29                    kill 1,$_ || kill 9, $_;
30            }
31    }
32    
33  our $port = 7777;  our $port = 7777;
 our $debug = 1;  
34    
35  use server;  use server;
36  our $url = "http://$server::ip:$port/";  our $debug = server::debug;
37    our $url = "http://$server::ip:$port";
38    
39    use html;
40    our $static_pids;
41    
42  sub static {  sub static {
43          my ($client,$path) = @_;          my ($client,$path) = @_;
44    
45          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
46    
47            return if ! -f $full;
48    
49          if ( ! -e $path ||  -d $path ) {          if ( my $pid = fork ) {
50                  print $client "HTTP/1.0 404 $path not found\r\n";                  # parent
51                  return;                  close($client);
52                    print "http static child $pid\n";
53                    $static_pids->{$pid} = $path;
54                    return 1;
55          }          }
56    
57          my $type = 'text/plain';          my $type = 'application/octet-stream';
58          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
59          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
60            $type = 'text/plain' if $path =~ m{\.txt};
61    
62            my $size = -s $full || return;
63    
64          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
65          open(my $html, $path);  
66          while(<$html>) {          open(my $fh, $full);
67                  print $client $_;          print "static $path $type $size\n";
68    
69            my $block = 8192;
70            my $buff;
71            my $pos = 0;
72    
73            while( my $len = read $fh, $buff, $block ) {
74                    print $client $buff;
75                    $pos += $len;
76                    printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;
77          }          }
78          close($html);          close($fh);
79            close($client);
80    
81            print "$path $pos == $size OK\n";
82    
83            exit;
84    }
85    
86    use boolean;
87    
88          return $path;  use screen;
89    use kvm;
90    
91    $SIG{CHLD} = 'IGNORE';
92    
93    sub start_stop {
94            my $daemon = shift;
95            my $pid = $pids->{$daemon};
96    
97            warn "start_stop $daemon $pid pids: ",dump( $pids );
98    
99            if ( $pid =~ m{^\d+$} ) {
100                    warn "kill 9 $pid";
101                    kill 9, $pid;
102                    $pids->{$daemon} = 'stopped';
103                    return qq|$daemon pid $pid stopped|;
104            } else {
105                    if ( $pid = fork ) {
106                            # parent
107                            $pids->{$daemon} = $pid;
108                            warn "forked $daemon $pid";
109                            return qq|$daemon pid $pid started|;
110                    } elsif ( defined $pid ) {
111                            # child
112                            my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';
113                            warn "eval $eval";
114                            eval $eval;
115                            warn "can't start $daemon: $@" if $@;
116                            exit;
117                    } else {
118                            die "fork error $!";
119                    }
120            }
121  }  }
122    
123  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";  my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
124    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
125    
126    sub get_request {
127            my ( $client, $path, $param ) = @_;
128    
129            warn "get_request $client $path ",dump( $param );
130    
131            if ( my $found = static( $client,$path ) ) {
132                    warn "static $found" if $debug;
133            } elsif ( $path eq '/' ) {
134    
135                    my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';
136                    my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
137                                             $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
138    
139                    my @rows = (
140                            'ip',           html::tt( $server::ip ),
141                            'netmask',      html::tt( $server::netmask ),
142    
143                            'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
144                    );
145    
146                    my $debug_proc;
147    
148                    warn 'pids: ', dump( $pids );
149                    foreach my $name ( keys %$pids ) {
150                            my $pid = $pids->{$name} || next;
151    
152                            my $html = qq|<a href=/$name>$pid</a>|;
153    
154                            if ( $debug ) {
155                                    $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
156    
157                                    my $proc = "/proc/$pid/status";
158                                    $debug_proc
159                                            .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
160                                            .  read_file($proc)
161                                            .  qq|</pre>|
162                                            if -e $proc;
163                            }
164    
165                            push @rows, ( $name => $html );
166                    }
167    
168                    warn 'static_pids: ', dump( $static_pids );
169                    foreach my $pid ( keys %$static_pids ) {
170                            my $path = $static_pids->{$pid};
171                            if ( -d "/proc/$pid" ) {
172                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
173                            } else {
174                                    push @rows, ( $path => "$pid competed" );
175                            }
176                    }
177    
178                    print $client $ok, html::table( 2, @rows ), $debug_proc;
179    
180            } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
181                    eval 'our $' . $1 . ' = ' . $2;
182                    warn $@ if $@;
183                    print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
184                    server::debug( $debug ) if $1 eq 'debug';
185            } elsif ( $path =~ m{^/(screen|kvm)} ) {
186                    print $client $redirect, start_stop($1);
187            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
188                    print $client $redirect;
189                    kill 9, $1 && warn "killed $1";
190            } elsif ( $path eq '/exit' ) {
191    #               DESTROY;
192                    exit 0;
193            } elsif ( $path =~ m{/boot} ) {
194                    print $client qq{$ok
195    #!gpxe
196    imgfree
197    login
198    chain http://$server::ip:$httpd::port/
199    
200                            };
201            } else {
202                    print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
203                    warn "404 $path";
204            }
205    
206    }
207    
208    use browser;
209    
210  sub start {  sub start {
211    
# Line 58  sub start { Line 214  sub start {
214                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
215                          Listen    => SOMAXCONN,                          Listen    => SOMAXCONN,
216                          Reuse     => 1                          Reuse     => 1
217          );          ) || die "can't start server on $url: $!";
                                                                             
         die "can't setup server" unless $server;  
218    
219          print "url $url\n";          print "url $url\n";
220    
221          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
222            start_stop 'screen';
223            start_stop 'kvm';
224    
225          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
226                  $client->autoflush(1);                  $client->autoflush(1);
# Line 72  sub start { Line 228  sub start {
228    
229                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
230    
231                    Module::Refresh->refresh;
232    
233                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
234                          my $method = $1;                          my $path = $1;
235                          my $param;                          my $param;
236                          if ( $method =~ s{\?(.+)}{} ) {                          if ( $path =~ s{\?(.+)}{} ) {
237                                  foreach my $p ( split(/[&;]/, $1) ) {                                  foreach my $p ( split(/[&;]/, $1) ) {
238                                          my ($n,$v) = split(/=/, $p, 2);                                          my ($n,$v) = split(/=/, $p, 2);
239                                          $param->{$n} = $v;                                          $param->{$n} = $v;
240                                  }                                  }
241                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
242                          }                          }
243                          warn "method $method";                          warn "path $path param: ",dump( $param );
244                            get_request $client, $path, $param;
                         if ( my $path = static( $client,$1 ) ) {  
                                 warn "static $path" if $debug;  
                         } elsif ( $method eq '/' ) {  
                                 print $client qq{$ok  
                                         pid <tt>$$</tt>  
                                         debug $debug  
                                 };  
                                   
                         } elsif ( $method =~ m{/boot} ) {  
                                 print $client qq{$ok  
 #!gpxe  
 imgfree  
 login  
 chain http://$server::ip:$httpd::port/  
   
                                         };  
                         } else {  
                                 print $client "HTTP/1.0 404 Unkown method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n404 $request";  
                                 warn "404 $request";  
                         }  
245                  } else {                  } else {
246                          print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";                          print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
247                          warn "500 $request";                          warn "500 $request";
248                  }                  }
249    
250                  print $client qq{                  print $client qq{
251                  <a href="">reload</a>                    <div style="font-size: 80%; color: #888">
252                  };                  <a href="">reload</a>
253                    <a href=/>index</a>
254                    <a href=/exit>exit</a>
255                    </div>
256                    } if $client->connected;
257    
                 close($client);  
258          }          }
259    
260          die "server died";          die "server died";
261  }  }
262    
263    warn "loaded";
264    
265  1;  1;

Legend:
Removed from v.42  
changed lines
  Added in v.71

  ViewVC Help
Powered by ViewVC 1.1.26