1 #!/usr/local/bin/perl -wT
3 # This is a CGI script that interfaces to stem. it collects all the
4 # CGI data and sends it to a Stem::SockMsg cell as a single
5 # Stem::Packet. It reads a single Stem::Packet back from the socket
6 # and uses the data in there to generate a response page.
11 use lib '/wrk/stem/src/stem/lib/' ;
14 use CGI::Carp qw(fatalsToBrowser) ;
19 my $cgi = CGI->new() ;
23 # get all the cgi data we can
25 $cgi_data{ 'params' } = get_cgi_data( 'param' ) ;
26 $cgi_data{ 'cookies' } = get_cgi_data( 'cookie' ) ;
27 #$cgi_data{ 'env' } = { %ENV } ;
28 #$cgi_data{ 'self_url' } = $cgi->self_url() ;
29 $cgi_data{ 'url' } = $cgi->url() ;
30 #$cgi_data{ 'cgi' } = $cgi ;
32 # todo: handle default host:port here
34 my $data = send_and_get_packet( \%cgi_data ) ;
38 # print $cgi->header() ;
39 # # print "<PRE>\n", Dumper( \%cgi_data ), "\n</PRE>\n" ;
40 # print "<PRE>\n", Dumper( $data ), "\n</PRE>\n" ;
44 if ( ref $data eq 'SCALAR' ) {
50 print $cgi->header(), <<HTML ;
58 # this works for both cookies and params as their APIs are the same
66 foreach my $name ( $cgi->$type() ) { ;
68 my @values = $cgi->$type( $name ) ;
71 $cgi_info{ $type } = \@values ;
75 $cgi_info{ $name } = shift @values ;
81 sub send_and_get_packet {
83 my( $in_data, $host, $port ) = @_ ;
86 $host ||= 'localhost' ;
88 my $sock = IO::Socket::INET->new( "$host:$port" ) ;
90 $sock or return "can't connect to $host:$port\n" ;
92 my $packet = Stem::Packet->new( codec => 'Storable' ) ;
94 my $write_buf = $packet->to_packet($in_data) ;
96 syswrite( $sock, $$write_buf ) ;
102 my $bytes_read = sysread( $sock, $read_buf, 8192 ) ;
104 return "sysread error $!" unless defined $bytes_read ;
105 return "sysread closed" if $bytes_read == 0 ;
107 my $result = $packet->to_data( $read_buf ) ;
109 return $result if $result ;