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

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

  ViewVC Help
Powered by ViewVC 1.1.26