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

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

  ViewVC Help
Powered by ViewVC 1.1.26