/[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 19 by dpavlin, Fri May 7 20:52:34 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 $debug = 1;
18    
19  my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;  my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;
20  print "Please contact me at: <URL:", $d->url, ">\n";  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                    'subject_search' => \&subject_search_filter,
27            },
28    });
29    
30    print "Web server ready at: ", $d->url, "\n";
31    
32    
33  while ( my $c = $d->accept ) {  while ( my $c = $d->accept ) {
34          while ( my $r = $c->get_request ) {          while ( my $r = $c->get_request ) {
# Line 22  while ( my $c = $d->accept ) { Line 39  while ( my $c = $d->accept ) {
39                  $ENV{'SERVER_PROTOCOL'}   = $r->protocol;                  $ENV{'SERVER_PROTOCOL'}   = $r->protocol;
40                  $ENV{'CONTENT_TYPE'}      = $r->content_type;                  $ENV{'CONTENT_TYPE'}      = $r->content_type;
41    
42                  my $form_parameters;    # GET/POST storage.                  # this part is based on CGI::Lite
43    
44                    $cgi->close_all_files();
45                    $cgi->{web_data}       = {};
46                    $cgi->{ordered_keys}   = [];
47                    $cgi->{all_handles}    = [];
48                    $cgi->{error_status}   = 0;
49                    $cgi->{error_message}  = undef;
50    
                 # is this a happy GET?  
51                  if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {                  if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
52                          $form_parameters = $r->uri;                          my $query_string = $r->uri;
53                          $form_parameters =~ s/[^\?]+\?(.*)/$1/;                          $query_string =~ s/[^\?]+\?(.*)/$1/;
54                          $CGI::Q = new CGI($form_parameters);                          $cgi->_decode_url_encoded_data (\$query_string, 'form');
55    
56                    } elsif ( $r->method eq 'POST' ) {
57    
58                            if ($r->content_type eq 'application/x-www-form-urlencoded') {
59    #                               local $^W = 0;
60                                    $cgi->_decode_url_encoded_data (\$r->content, 'form');
61                            } elsif ($r->content_type =~ /multipart\/form-data/) {
62                                    my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
63                                    $cgi->_parse_multipart_data ($r->content_length, $boundary);
64                            }
65                    } else {
66                             $c->send_error(RC_FORBIDDEN);
67                  }                  }
68    
69                  # possibly POST?                  my $param = $cgi->{web_data};
70                  if ( $r->method eq 'POST' ) {                  my $url = $r->url->path;
71    
72                          # now decide how we want to turn the parameters                  # XXX LOG
73                          # over to CGI.pm. note that this will cause                  print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug);
                         # 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();  
                         }  
74    
75                          else { $CGI::Q = new CGI($form_parameters); }                  # template file name (use ?format=html as default)
76                    my $tpl_file = 'master.';
77                    $tpl_file .= $param->{'format'} || 'html';
78    
79                    #
80                    # implement functionality and generate HTML
81                    #
82                    my $html;
83    
84                    if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
85                            $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
86                    } elsif ($param->{'search'}) {
87                            ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
88                  }                  }
89    
90                  if (! $CGI::Q) {                  my $tpl_var = {
91                           $c->send_error(RC_FORBIDDEN);                          param   => $param
92                    };
93    
94                    # show search results
95                    # ?search=foo:bar
96                    if ($param->{'search'}) {
97    
98                            print STDERR "search: ",$param->{'search'},"\n";
99    
100                            my $results = $mws->search($param->{'search'});
101                            my @res = $mws->fetch_all_results();
102    
103                            $tpl_var->{results} = \@res;
104                            $tpl_var->{total_hits} = $mws->{total_hits};
105    
106    
107                    #
108                    # ?show_id=XXXXxxxx___message_id___xxxxXXXX
109                    } elsif ($param->{'show_id'}) {
110    
111                            $mws->reset_counters;
112                            my $row = $mws->fetch_result_by_id($param->{'show_id'});
113                            $tpl_var->{message} = $row;
114                  }                  }
115    
116                  my $param = param("hello") || '';  print Dumper($mws->{counter});
117                  my $url = $r->url->path;  
118                    # push counters to template
119                    foreach my $f (qw(from to cc bcc)) {
120                            my $h = $mws->counter($f) || next;
121                            my @a;
122                            foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
123                                    push @a, $h->{$k};
124                            }
125                            $tpl_var->{counters}->{$f} = [ @a ] if (@a);
126                    }
127    
128                  print "I saw: $param\n";                  $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
129    
130                  #      my $f = param("thefile");                  #
131                  #      print "thefile filename: $f\n";                  # send HTMLto client
132                  #      { undef $/; print "thefile size: ", length(<$f>), "\n" }                  #
133    
134                  my $res = HTTP::Response->new(RC_OK);                  my $res = HTTP::Response->new(RC_OK);
135                  $res->content( qq{                  $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );
136  <html>                  $res->content($html);
         hello = $param<br>  
         URL: $url  
 </html>  
         }  
                 );  
   
137                  $c->send_response($res);                  $c->send_response($res);
138    
139                  $c->close;                  $c->close;
140          }          }
141          undef($c);          undef($c);
142  }  }
143    
144    # template toolkit filter
145    
146    #use Text::Context::EitherSide;
147    
148    sub body5_filter {
149            my $text = shift;
150            $text =~ s/^\s+//gs;
151            $text =~ s/^[\>:\|=]+\s*.*?$//msg;      # remove quoted text
152            $text =~ s/[\n\r]+/\n/gs;               # compress cr/lf
153            if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
154                    $text =~ s/[\n\r]*$/ .../;
155            }
156            $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
157    
158    #       my $context = Text::Context::EitherSide->new($text, context => 5);
159    #       return $context->as_string("perl");
160    
161            return $text;
162    }
163    
164    sub subject_search_filter {
165            my $s = shift;
166            # remove re: fdw: [list] preffixes from e-mail
167            while ( $s =~ s/^\s*\[(?:re|fwd|fw):\s+(.+)\]\s*$/$1/ig ||
168                    $s =~ s/^\s*(?:re|fwd|fw):\s+(.+?)\s*$/$1/ig ||
169                    $s =~ s/^\[\S+\]\s*//ig ||
170                    $s =~ s/^\[[^@]+@\w+\.\w+\s*:\s+(.+)\s*\]\s*$/$1/g ||
171                    $s =~ s/\(fwd\)\s*$//ig ||
172                    $s =~ s/\"//g
173            ) { };
174            return $s;
175    }

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

  ViewVC Help
Powered by ViewVC 1.1.26