--- trunk/httpd.pl 2004/05/05 10:26:27 5 +++ trunk/httpd.pl 2004/05/05 13:42:27 6 @@ -8,11 +8,25 @@ use HTTP::Daemon; use HTTP::Status; use IO::String; -use CGI 2.50 qw/:standard :cgi-lib/; +use CGI::Lite; +use Template; +use MWS; + +use Data::Dumper; my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; print "Please contact me at: url, ">\n"; +my $cgi = new CGI::Lite; +my $mws = MWS->new('global.conf'); +my $tt = Template->new({ + INCLUDE_PATH => $mws->{config}->val('global', 'templates'), + FILTERS => { + 'body5' => \&body5_filter, + }, +}); + + while ( my $c = $d->accept ) { while ( my $r = $c->get_request ) { @@ -22,70 +36,77 @@ $ENV{'SERVER_PROTOCOL'} = $r->protocol; $ENV{'CONTENT_TYPE'} = $r->content_type; - my $form_parameters; # GET/POST storage. + # this part is based on CGI::Lite + + $cgi->close_all_files(); + $cgi->{web_data} = {}; + $cgi->{ordered_keys} = []; + $cgi->{all_handles} = []; + $cgi->{error_status} = 0; + $cgi->{error_message} = undef; - # is this a happy GET? if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { - $form_parameters = $r->uri; - $form_parameters =~ s/[^\?]+\?(.*)/$1/; - $CGI::Q = new CGI($form_parameters); + my $query_string = $r->uri; + $query_string =~ s/[^\?]+\?(.*)/$1/; + $cgi->_decode_url_encoded_data (\$query_string, 'form'); + + } elsif ( $r->method eq 'POST' ) { + + if ($r->content_type eq 'application/x-www-form-urlencoded') { +# local $^W = 0; + $cgi->_decode_url_encoded_data (\$r->content, 'form'); + } elsif ($r->content_type =~ /multipart\/form-data/) { + my ($boundary) = $r->content_type =~ /boundary=(\S+)$/; + $cgi->_parse_multipart_data ($r->content_length, $boundary); + } + } else { + $c->send_error(RC_FORBIDDEN); } - # possibly POST? - if ( $r->method eq 'POST' ) { + my $param = $cgi->{web_data}; + my $url = $r->url->path; - # now decide how we want to turn the parameters - # over to CGI.pm. note that this will cause - # problems with your STDIN with multipart forms. - my $form_parameters = $r->content; - $ENV{'CONTENT_LENGTH'} = $r->content_length || 0; - - # sounds like multipart. - if ( $form_parameters =~ /^--/ ) { - - my ($boundary) = split ( /\n/, $form_parameters ); - chop($boundary); - substr( $boundary, 0, 2 ) = ''; # delete the leading "--" !!! - $ENV{'CONTENT_TYPE'} = - $r->content_type . "; boundary=$boundary"; - - # this breaks STDIN forever. I've yet to discover - # how to properly save and reassign STDIN after - # we're done breaking things horrifically here. - close STDIN; - my $t = tie *STDIN, 'IO::String'; - $t->open($form_parameters); - $CGI::Q = new CGI(); - } + # XXX LOG + print $r->method," ",$url,Dumper($param); - else { $CGI::Q = new CGI($form_parameters); } - } + # generate HTML + my $html; - if (! $CGI::Q) { - $c->send_error(RC_FORBIDDEN); - } + my $s=$param->{'search'}; - my $param = param("hello") || ''; - my $url = $r->url->path; + if ($s) { - print "I saw: $param\n"; + print STDERR "search: $s\n"; - # my $f = param("thefile"); - # print "thefile filename: $f\n"; - # { undef $/; print "thefile size: ", length(<$f>), "\n" } + my $results = $mws->search($s); - my $res = HTTP::Response->new(RC_OK); - $res->content( qq{ - - hello = $param
- URL: $url - - } - ); + my @res = $mws->fetch_all_results(); + + my $tpl_file = 'master.'; + $tpl_file .= $param->{'format'} || 'html'; + + $tt->process($tpl_file, { + query => $s, + results => \@res, + param => $param, + }, \$html) || die $tt->error(); + } + my $res = HTTP::Response->new(RC_OK); + $res->header( 'Content-type' => 'text/html; charset=iso-8859-2' ); + $res->content($html); $c->send_response($res); $c->close; } undef($c); } + +# template toolkit filter + +sub body5_filter { + my $text = shift; + $text =~ s/^\s+//gs; + $text =~ s/^(.*?[\n\r]+){5}.*$/$1/s; + return $text; +}