/[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 34 by dpavlin, Sun May 9 10:16:51 2004 UTC revision 52 by dpavlin, Tue May 25 18:55:46 2004 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3  # based on post  BEGIN {
4  # http://www.mail-archive.com/libwww@perl.org/msg04750.html          my $basedir = readlink($0) || $0; $basedir =~ s#/[^/]+$#/lib#;
5            unshift(@INC, $basedir);
6    }
7    
8    =head1 NAME
9    
10    httpd.pl - http server for Mail::Box Web Search
11    
12    =head1 SYNOPSYS
13    
14     httpd.pl [local.conf]
15    
16    =head1 DESCRIPTION
17    
18    This script implements user interface for Mail::Box Web Search as
19    a small single-user http server.
20    
21    =head1 SEE ALSO
22    
23    C<MWS> perl modules which are part of this package
24    C<MWS::HTTPD> module which implements the server itself
25    
26    =cut
27    
28  use strict;  use strict;
29  use warnings;  use warnings;
30  use HTTP::Daemon;  use MWS::SWISH;
31  use HTTP::Status;  #use MWS::Plucene;
32  use IO::String;  use HTTP::Daemon::Simple;
 use CGI::Lite;  
33  use Template;  use Template;
 use MWS;  
34  use URI::Escape;  use URI::Escape;
35    
36  use Data::Dumper;  use Data::Dumper;
# Line 28  be used. Line 48  be used.
48          exit 1;          exit 1;
49  }  }
50    
51  my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;  my $mws = MWS::SWISH->new(config_file => $config_file);
52  my $cgi = new CGI::Lite;  #my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug);
53  my $mws = MWS->new($config_file);  
54  my $tt = Template->new({  my $tt = Template->new({
55          INCLUDE_PATH => $mws->{config}->val('global', 'templates'),          INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
56          FILTERS => {          FILTERS => {
# Line 40  my $tt = Template->new({ Line 60  my $tt = Template->new({
60          EVAL_PERL => 1,          EVAL_PERL => 1,
61  });  });
62    
63  my $static_html = $mws->{config}->val('global', 'static_html');  my $d = new HTTP::Daemon::Simple(
64            'listen' => $mws->{config}->val('global', 'listen'),
65            'static_html' => $mws->{config}->val('global', 'static_html'),
66            'debug' => $debug,
67    ) || die "can't create HTTP::Daemon::Simple: $!";
68    
69    
70  print "Web server ready at: ", $d->url, "\n";  print "Web server ready at: ", $d->url, "\n";
71    
72    $d->run_server( \&request );
73    
74  while ( my $c = $d->accept ) {  sub request($$) {
75          while ( my $r = $c->get_request ) {          my ($url,$param) = @_;
76    
77                  # environs that a webserver should set.          print Dumper($param,$mws->{counter}),"\n" if ($debug);
                 $ENV{'REQUEST_METHOD'}    = $r->method;  
                 $ENV{'GATEWAY_INTERFACE'} = "CGI/1.0";  
                 $ENV{'SERVER_PROTOCOL'}   = $r->protocol;  
                 $ENV{'CONTENT_TYPE'}      = $r->content_type;  
   
                 # 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;  
   
                 if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {  
                         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);  
                 }  
78    
79                  my $param = $cgi->{web_data};          # template file name (use ?format=html as default)
80                  my $url = $r->url->path;          my $tpl_file = 'master.';
81            $tpl_file .= $param->{'format'} || 'html';
82    
83                  # XXX LOG          # parse date from url
84                  print $r->method," ",$url,"\n";          my ($yyyy,$mm,$dd) = $mws->yyyymmdd;
                 print Dumper($param),"\n" if ($debug);  
   
                 # is this static page?  
                 if ($static_html && -f "$static_html/$url") {  
                         print "static file: $static_html/$url\n" if ($debug);  
                         $c->send_file_response("$static_html/$url");  
                         $c->close;  
                         next;  
                 }  
85    
86                  # template file name (use ?format=html as default)          my $yyyymm;
                 my $tpl_file = 'master.';  
                 $tpl_file .= $param->{'format'} || 'html';  
   
                 # parse date from url  
                 my ($yyyy,$mm,$dd) = $mws->yyyymmdd;  
   
                 my $yyyymm;  
   
                 my $date_limit;  
   
                 if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {  
                         ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);  
                          $date_limit = "$yyyy-$mm-$dd";  
                 } elsif ($url =~ m,^/(\d{4})[/-](\d+),) {  
                         ($yyyy,$mm) = $mws->fmtdate($1,$2);  
                         $date_limit = "$yyyy-$mm";  
                 } elsif ($url =~ m,^/(\d{4}),) {  
                         $date_limit = $mws->fmtdate($1);  
                 }  
87    
88                  #          my $date_limit;
                 # implement functionality and generate HTML  
                 #  
                 my $html;  
   
                 if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {  
                         $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};  
                 } elsif ($param->{'search'}) {  
                         ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);  
                 }  
89    
90                  my $tpl_var = {          if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
91                          param   => $param,                  ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
92                          yyyy    => $yyyy,                   $date_limit = "$yyyy-$mm-$dd";
93                          mm      => $mm,          } elsif ($url =~ m,^/(\d{4})[/-](\d+),) {
94                          dd      => $dd,                  ($yyyy,$mm) = $mws->fmtdate($1,$2);
95                          date_limit => $date_limit,                  $date_limit = "$yyyy-$mm";
96                  };          } elsif ($url =~ m,^/(\d{4}),) {
97                    $date_limit = $mws->fmtdate($1);
98                  # is this access to root of web server?          }
                 if ($url eq "/" && !$param->{'search'}) {  
                         # if first access, go to current year  
                         $date_limit = $mws->fmtdate($yyyy);  
                         $param->{sort_by} = "date desc";  
                 }  
   
                 # ?show_id=XXXXxxxx___message_id___xxxxXXXX  
                 if ($param->{'show_id'}) {  
   
                         $mws->reset_counters;  
                         my $row = $mws->fetch_result_by_id($param->{'show_id'});  
                         $tpl_var->{message} = $row;  
                 } elsif ($param->{'search'} || $date_limit) {  
99    
100                          # show search results          #
101                          # ?search=foo:bar          # implement functionality and generate HTML
102            #
103            my $html;
104    
105            if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
106                    $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
107            } elsif ($param->{'search'}) {
108                    ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
109            }
110    
111                          my @search;          my $tpl_var = {
112                          push @search, $param->{'search'} if ($param->{'search'});                  param   => $param,
113                    yyyy    => $yyyy,
114                    mm      => $mm,
115                    dd      => $dd,
116                    date_limit => $date_limit,
117            };
118    
119            # is this access to root of web server?
120            if ($url eq "/" && !$param->{'search'}) {
121                    # if first access, go to current year
122                    $date_limit = $mws->fmtdate($yyyy);
123                    $param->{sort_by} = "date desc";
124            }
125    
126                          if ($date_limit) {          # ?show_id=XXXXxxxx___message_id___xxxxXXXX
127                                  push @search, "and" if (@search);          if ($param->{'show_id'}) {
                                 push @search, "date:\"$date_limit\"";  
                         }  
128    
129                          if ($param->{sort_by}) {                  $mws->reset_counters;
130                                  push @search, "sort:".$param->{sort_by};                  my $row = $mws->fetch_result_by_id($param->{'show_id'});
131                          }                  $tpl_var->{message} = $row;
132            } elsif ($param->{'search'} || $date_limit) {
133    
134                    # show search results
135                    # ?search=foo:bar
136    
137                    my @search;
138                    push @search, $param->{'search'} if ($param->{'search'});
139    
140                    if ($date_limit) {
141                            push @search, "and" if (@search);
142                            push @search, "date:\"$date_limit\"";
143                    }
144    
145                          print STDERR "search: ",join(" ",@search),"\n";                  if ($param->{sort_by}) {
146                            push @search, "sort:".$param->{sort_by};
147                    }
148    
149                          my $results = $mws->search(@search);                  print STDERR "search: ",join(" ",@search),"\n";
                         my @res = $mws->fetch_all_results();  
150    
151                          $tpl_var->{results} = \@res if (@res);                  my $results = $mws->search(@search);
152                          $tpl_var->{total_hits} = $mws->{total_hits} || 0;                  my @res = $mws->fetch_all_results();
153    
154                          # no hits, offer suggestions                  $tpl_var->{results} = \@res if (@res);
155                          if (! $tpl_var->{results}) {                  $tpl_var->{total_hits} = $mws->{total_hits} || 0;
                                 @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});  
                         }  
156    
157                    # no hits, offer suggestions
158                    if (! $tpl_var->{results}) {
159                            @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});
160                  }                  }
161    
162            }
163    
164                  # push counters to template          # push counters to template
165                  foreach my $f (qw(from to cc bcc)) {          foreach my $f (qw(from to cc bcc folder)) {
166                          my $h = $mws->counter($f) || next;                  my $h = $mws->counter($f) || next;
167                          my @a;                  my @a;
168                          foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {                  foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
169                                  push @a, $h->{$k};                          push @a, $h->{$k};
                         }  
                         $tpl_var->{counters}->{$f} = [ @a ] if (@a);  
170                  }                  }
171                    $tpl_var->{counters}->{$f} = [ @a ] if (@a);
172            }
173    
174                  # push calendar in template          # push calendar in template
175                  $tpl_var->{calendar} = $mws->counter('calendar');          $tpl_var->{calendar} = $mws->counter('calendar');
   
                 $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();  
   
                 #  
                 # send HTMLto client  
                 #  
   
                 my $res = HTTP::Response->new(RC_OK);  
                 $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );  
                 $res->content($html);  
                 $c->send_response($res);  
176    
177                  $c->close;          $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
178          }          return $html;
179          undef($c);  };
 }  
180    
181  # template toolkit filter  # template toolkit filter
182    
# Line 217  sub html_escape($) { Line 184  sub html_escape($) {
184          my $text = shift || return;          my $text = shift || return;
185    
186          # don't re-escape html          # don't re-escape html
187          #return $text if ($text =~ /&(:?lt|gt|amp|quot);/);          #return $text if ($text =~ /&(?:lt|gt|amp|quot);/);
188    
189          # Escape <, >, & and ", and to produce valid XML          # Escape <, >, & and ", and to produce valid XML
190          my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');          my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 236  sub body5_filter { Line 203  sub body5_filter {
203          my $text = shift;          my $text = shift;
204    
205          # remove quote          # remove quote
206          $text =~ s/^[\>:\|=]+\s*.*?$/#-q-#/msg;          $text =~ s/^[\>:\|=]+[^\n\r]*[\n\r]*$/#-q-#/msg;
207          # remove quote author          # remove quote author
208          $text =~ s/[\n\r]+[^\n\r]+:\s*(:?#-q-#[\n\r*])+//gs;          $text =~ s/[\n\r]+[^\n\r]+:\s*(?:#-q-#[\n\r*])+//gs;
209          $text =~ s/^[^\n\r]+:\s*(:?#-q-#[\n\r]*)+//gs;          $text =~ s/^[^\n\r]+:\s*(?:#-q-#[\n\r]*)+//gs;
210          $text =~ s/#-q-#[\n\r]*//gs;          $text =~ s/#-q-#[\n\r]*//gs;
211          # outlook quoting          # outlook quoting
212          $text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si;          $text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si;
213          $text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;          $text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;
214    
215          # remove signature          # remove signature
216          $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;          $text =~ s/(?:^|[\n\r]+)*--\s*[\n\r]+.*$//s;
217            $text =~ s/(?:^|[\n\r]+)*_____+[\n\r]+.*$//s;
218    
219          # compress cr/lf          # compress cr/lf
220          $text =~ s/[\n\r]+/\n/gs;          $text =~ s/[\n\r]+/\n/gs;
# Line 278  sub body_filter { Line 246  sub body_filter {
246          # remove signature          # remove signature
247          if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {          if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {
248                  $sig = "$1#-#signature##$2##signature#-#";                  $sig = "$1#-#signature##$2##signature#-#";
249            } elsif ($text =~s/(^|[\n\r]+)*(_____+[\n\r]+.*)$//s) {
250                    $sig = "$1#-#signature##$2##signature#-#";
251          }          }
252    
253          # find quoted text          # find quoted text
254          $text =~ s/^([\>:\|=]+\s*.*?)$/#-#quote1##$1##quote1#-#/msg;          $text =~ s/^([\>:\|=]+[^\n\r]*[\n\r]*)$/#-#quote1##$1##quote1#-#/mg;
255          $text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si;          $text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si;
256          $text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si;          $text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si;
257    

Legend:
Removed from v.34  
changed lines
  Added in v.52

  ViewVC Help
Powered by ViewVC 1.1.26