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

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

  ViewVC Help
Powered by ViewVC 1.1.26