/[mws]/trunk/httpd.pl
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 /trunk/httpd.pl

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

revision 5 by dpavlin, Wed May 5 10:26:27 2004 UTC revision 12 by dpavlin, Thu May 6 12:40:11 2004 UTC
# Line 8  use warnings; Line 8  use warnings;
8  use HTTP::Daemon;  use HTTP::Daemon;
9  use HTTP::Status;  use HTTP::Status;
10  use IO::String;  use IO::String;
11  use CGI 2.50 qw/:standard :cgi-lib/;  use CGI::Lite;
12    use Template;
13    use MWS;
14    
15    use Data::Dumper;
16    
17  my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;  my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;
18  print "Please contact me at: <URL:", $d->url, ">\n";  print "Please contact me at: <URL:", $d->url, ">\n";
19    
20    my $cgi = new CGI::Lite;
21    my $mws = MWS->new('global.conf');
22    my $tt = Template->new({
23            INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
24            FILTERS => {
25                    'body5' => \&body5_filter,
26            },
27    });
28    
29    
30  while ( my $c = $d->accept ) {  while ( my $c = $d->accept ) {
31          while ( my $r = $c->get_request ) {          while ( my $r = $c->get_request ) {
32    
# Line 22  while ( my $c = $d->accept ) { Line 36  while ( my $c = $d->accept ) {
36                  $ENV{'SERVER_PROTOCOL'}   = $r->protocol;                  $ENV{'SERVER_PROTOCOL'}   = $r->protocol;
37                  $ENV{'CONTENT_TYPE'}      = $r->content_type;                  $ENV{'CONTENT_TYPE'}      = $r->content_type;
38    
39                  my $form_parameters;    # GET/POST storage.                  # this part is based on CGI::Lite
40    
41                    $cgi->close_all_files();
42                    $cgi->{web_data}       = {};
43                    $cgi->{ordered_keys}   = [];
44                    $cgi->{all_handles}    = [];
45                    $cgi->{error_status}   = 0;
46                    $cgi->{error_message}  = undef;
47    
                 # is this a happy GET?  
48                  if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {                  if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
49                          $form_parameters = $r->uri;                          my $query_string = $r->uri;
50                          $form_parameters =~ s/[^\?]+\?(.*)/$1/;                          $query_string =~ s/[^\?]+\?(.*)/$1/;
51                          $CGI::Q = new CGI($form_parameters);                          $cgi->_decode_url_encoded_data (\$query_string, 'form');
52    
53                    } elsif ( $r->method eq 'POST' ) {
54    
55                            if ($r->content_type eq 'application/x-www-form-urlencoded') {
56    #                               local $^W = 0;
57                                    $cgi->_decode_url_encoded_data (\$r->content, 'form');
58                            } elsif ($r->content_type =~ /multipart\/form-data/) {
59                                    my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
60                                    $cgi->_parse_multipart_data ($r->content_length, $boundary);
61                            }
62                    } else {
63                             $c->send_error(RC_FORBIDDEN);
64                  }                  }
65    
66                  # possibly POST?                  my $param = $cgi->{web_data};
67                  if ( $r->method eq 'POST' ) {                  my $url = $r->url->path;
68    
69                          # now decide how we want to turn the parameters                  # XXX LOG
70                          # over to CGI.pm. note that this will cause                  print $r->method," ",$url,Dumper($param);
                         # 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();  
                         }  
71    
72                          else { $CGI::Q = new CGI($form_parameters); }                  # template file name (use ?format=html as default)
73                  }                  my $tpl_file = 'master.';
74                    $tpl_file .= $param->{'format'} || 'html';
75    
76                    #
77                    # implement functionality and generate HTML
78                    #
79                    my $html;
80    
81                  if (! $CGI::Q) {                  if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
82                           $c->send_error(RC_FORBIDDEN);                          $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
83                  }                  }
84    
85                  my $param = param("hello") || '';                  # show search results
86                  my $url = $r->url->path;                  # ?search=foo:bar
87                    if ($param->{'search'}) {
88                  print "I saw: $param\n";  
89                            print STDERR "search: ",$param->{'search'},"\n";
90    
91                            my $results = $mws->search($param->{'search'});
92                            my @res = $mws->fetch_all_results();
93    
94                            $tt->process($tpl_file, {
95                                    query   => $param->{'search'},
96                                    results => \@res,
97                                    param   => $param,
98                            }, \$html) || die $tt->error();
99    
100                    #
101                    # ?show_id=XXXXxxxx___message_id___xxxxXXXX
102                    } elsif ($param->{'show_id'}) {
103                            
104                            my $row = $mws->fetch_result_by_id($param->{'show_id'});
105                            $tt->process($tpl_file, {
106                                    message => $row,
107                            }, \$html) || die $tt->error();
108                    }
109    
110                  #      my $f = param("thefile");                  #
111                  #      print "thefile filename: $f\n";                  # send HTMLto client
112                  #      { undef $/; print "thefile size: ", length(<$f>), "\n" }                  #
113    
114                  my $res = HTTP::Response->new(RC_OK);                  my $res = HTTP::Response->new(RC_OK);
115                  $res->content( qq{                  $res->header( 'Content-type' => 'text/html; charset=iso-8859-2' );
116  <html>                  $res->content($html);
         hello = $param<br>  
         URL: $url  
 </html>  
         }  
                 );  
   
117                  $c->send_response($res);                  $c->send_response($res);
118    
119                  $c->close;                  $c->close;
120          }          }
121          undef($c);          undef($c);
122  }  }
123    
124    # template toolkit filter
125    
126    #use Text::Context::EitherSide;
127    
128    sub body5_filter {
129            my $text = shift;
130            $text =~ s/^\s+//gs;
131            $text =~ s/^[\>:\|=]+\s*.*?$//msg;      # remove quoted text
132            $text =~ s/[\n\r]+/\n/gs;               # compress cr/lf
133            $text =~ s,^((?:.*?[\n\r]){5}).*$,$1<span style="color:#808080">--8&lt;--[cut]--8&lt;--</span>,s;
134            $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
135    
136    #       my $context = Text::Context::EitherSide->new($text, context => 5);
137    #       return $context->as_string("perl");
138    
139            return $text;
140    }
141    

Legend:
Removed from v.5  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26