/[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 39 by dpavlin, Mon May 10 14:07:44 2004 UTC revision 54 by dpavlin, Sun Aug 1 22:30:53 2004 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3  # based on post  my $basedir = '';
4  # http://www.mail-archive.com/libwww@perl.org/msg04750.html  BEGIN {
5            $basedir = $0;
6            my $loop = 0;   # to prevent symlink loops
7            while (-l $basedir && $loop++ < 20) {
8                    $basedir = readlink($basedir);
9            }
10            $basedir =~ s#/+[^/]+$#/lib#;
11    }
12    if ($basedir) {
13            use lib "$basedir";
14    }
15    
16    =head1 NAME
17    
18    httpd.pl - http server for Mail::Box Web Search
19    
20    =head1 SYNOPSYS
21    
22     httpd.pl [local.conf]
23    
24    =head1 DESCRIPTION
25    
26    This script implements user interface for Mail::Box Web Search as
27    a small single-user http server.
28    
29    =head1 SEE ALSO
30    
31    C<MWS> perl modules which are part of this package
32    C<HTTP::Daemon::Simple> module which implements the server itself
33    
34    =cut
35    
36  use strict;  use strict;
37  use warnings;  use warnings;
38  use HTTP::Daemon;  use MWS::SWISH;
39  use HTTP::Status;  #use MWS::Plucene;
40  use IO::String;  use HTTP::Daemon::Simple;
 use CGI::Lite;  
41  use Template;  use Template;
 use MWS;  
42  use URI::Escape;  use URI::Escape;
43    
44  use Data::Dumper;  use Data::Dumper;
# Line 28  be used. Line 56  be used.
56          exit 1;          exit 1;
57  }  }
58    
59  my $mws = MWS->new($config_file);  my $mws = MWS::SWISH->new(config_file => $config_file);
60    #my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug);
 my ($local_addr,$local_port) = ('127.0.0.1',6969);  
   
 my $listen = $mws->{config}->val('global', 'listen');  
 if ($listen =~ m/:/) {  
         ($local_addr,$local_port) = split(/:/,$listen);  
 } elsif ($listen) {  
         $local_addr = $listen;  
 }  
61    
 my $d = HTTP::Daemon->new(  
         Reuse => 1,  
         LocalAddr => $local_addr,  
         LocalPort => $local_port,  
 ) || die "can't create HTTP::Daemon";  
   
 my $cgi = new CGI::Lite;  
62  my $tt = Template->new({  my $tt = Template->new({
63          INCLUDE_PATH => $mws->{config}->val('global', 'templates'),          INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
64          FILTERS => {          FILTERS => {
# Line 55  my $tt = Template->new({ Line 68  my $tt = Template->new({
68          EVAL_PERL => 1,          EVAL_PERL => 1,
69  });  });
70    
71  my $static_html = $mws->{config}->val('global', 'static_html');  my $d = new HTTP::Daemon::Simple(
72            'listen' => $mws->{config}->val('global', 'listen'),
73  print "Web server ready at: ", $d->url, "\n";          'static_html' => $mws->{config}->val('global', 'static_html'),
74            'debug' => $debug,
75    ) || die "can't create HTTP::Daemon::Simple: $!";
76    
77    
78  while ( my $c = $d->accept ) {  print "Web server ready at: ", $d->url, "\n";
         while ( my $r = $c->get_request ) {  
79    
80                  # environs that a webserver should set.  $d->run_server( \&request );
                 $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);  
                 }  
81    
82                  my $param = $cgi->{web_data};  sub request($$) {
83                  my $url = $r->url->path;          my ($url,$param) = @_;
84    
85                  # XXX LOG          print Dumper($param,$mws->{counter}),"\n" if ($debug);
                 print $r->method," ",$url,"\n";  
                 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;  
                 }  
86    
87                  # template file name (use ?format=html as default)          # template file name (use ?format=html as default)
88                  my $tpl_file = 'master.';          my $tpl_file = 'master.';
89                  $tpl_file .= $param->{'format'} || 'html';          $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);  
                 }  
90    
91                  #          # parse date from url
92                  # implement functionality and generate HTML          my ($yyyy,$mm,$dd) = $mws->yyyymmdd;
                 #  
                 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);  
                 }  
93    
94                  my $tpl_var = {          my $yyyymm;
                         param   => $param,  
                         yyyy    => $yyyy,  
                         mm      => $mm,  
                         dd      => $dd,  
                         date_limit => $date_limit,  
                 };  
   
                 # 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";  
                 }  
95    
96                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX          my $date_limit;
                 if ($param->{'show_id'}) {  
97    
98                          $mws->reset_counters;          if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
99                          my $row = $mws->fetch_result_by_id($param->{'show_id'});                  ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
100                          $tpl_var->{message} = $row;                   $date_limit = "$yyyy-$mm-$dd";
101                  } elsif ($param->{'search'} || $date_limit) {          } elsif ($url =~ m,^/(\d{4})[/-](\d+),) {
102                    ($yyyy,$mm) = $mws->fmtdate($1,$2);
103                    $date_limit = "$yyyy-$mm";
104            } elsif ($url =~ m,^/(\d{4}),) {
105                    $date_limit = $mws->fmtdate($1);
106            }
107    
108                          # show search results          #
109                          # ?search=foo:bar          # implement functionality and generate HTML
110            #
111            my $html;
112    
113            if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
114                    $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
115            } elsif ($param->{'search'}) {
116                    ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
117            }
118    
119                          my @search;          my $tpl_var = {
120                          push @search, $param->{'search'} if ($param->{'search'});                  param   => $param,
121                    yyyy    => $yyyy,
122                    mm      => $mm,
123                    dd      => $dd,
124                    date_limit => $date_limit,
125            };
126    
127            # is this access to root of web server?
128            if ($url eq "/" && !$param->{'search'}) {
129                    # if first access, go to current year
130                    $date_limit = $mws->fmtdate($yyyy);
131                    $param->{sort_by} = "date desc";
132            }
133    
134                          if ($date_limit) {          # ?show_id=XXXXxxxx___message_id___xxxxXXXX
135                                  push @search, "and" if (@search);          if ($param->{'show_id'}) {
                                 push @search, "date:\"$date_limit\"";  
                         }  
136    
137                          if ($param->{sort_by}) {                  $mws->reset_counters;
138                                  push @search, "sort:".$param->{sort_by};                  my $row = $mws->fetch_result_by_id($param->{'show_id'});
139                          }                  $tpl_var->{message} = $row;
140            } elsif ($param->{'search'} || $date_limit) {
141    
142                    # show search results
143                    # ?search=foo:bar
144    
145                    my @search;
146                    push @search, $param->{'search'} if ($param->{'search'});
147    
148                    if ($date_limit) {
149                            push @search, "and" if (@search);
150                            push @search, "date:\"$date_limit\"";
151                    }
152    
153                          print STDERR "search: ",join(" ",@search),"\n";                  if ($param->{sort_by}) {
154                            push @search, "sort:".$param->{sort_by};
155                    }
156    
157                          my $results = $mws->search(@search);                  print STDERR "search: ",join(" ",@search),"\n";
                         my @res = $mws->fetch_all_results();  
158    
159                          $tpl_var->{results} = \@res if (@res);                  my $results = $mws->search(@search);
160                          $tpl_var->{total_hits} = $mws->{total_hits} || 0;                  my @res = $mws->fetch_all_results();
161    
162                          # no hits, offer suggestions                  $tpl_var->{results} = \@res if (@res);
163                          if (! $tpl_var->{results}) {                  $tpl_var->{total_hits} = $mws->{total_hits} || 0;
                                 @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});  
                         }  
164    
165                    # no hits, offer suggestions
166                    if (! $tpl_var->{results} && $param->{'search_fld'} && $param->{'search_val'}) {
167                            @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});
168                  }                  }
169    
170            }
171    
172                  # push counters to template          # push counters to template
173                  foreach my $f (qw(from to cc bcc)) {          foreach my $f (qw(from to cc bcc folder)) {
174                          my $h = $mws->counter($f) || next;                  my $h = $mws->counter($f) || next;
175                          my @a;                  my @a;
176                          foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {                  foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
177                                  push @a, $h->{$k};                          push @a, $h->{$k};
                         }  
                         $tpl_var->{counters}->{$f} = [ @a ] if (@a);  
178                  }                  }
179                    $tpl_var->{counters}->{$f} = [ @a ] if (@a);
180            }
181    
182                  # push calendar in template          # push calendar in template
183                  $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);  
184    
185                  $c->close;          $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
186          }          return $html;
187          undef($c);  };
 }  
188    
189  # template toolkit filter  # template toolkit filter
190    
# Line 261  sub body5_filter { Line 221  sub body5_filter {
221          $text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;          $text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;
222    
223          # remove signature          # remove signature
224          $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;          $text =~ s/(?:^|[\n\r]+)*--\s*[\n\r]+.*$//s;
225            $text =~ s/(?:^|[\n\r]+)*_____+[\n\r]+.*$//s;
226    
227          # compress cr/lf          # compress cr/lf
228          $text =~ s/[\n\r]+/\n/gs;          $text =~ s/[\n\r]+/\n/gs;
# Line 293  sub body_filter { Line 254  sub body_filter {
254          # remove signature          # remove signature
255          if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {          if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {
256                  $sig = "$1#-#signature##$2##signature#-#";                  $sig = "$1#-#signature##$2##signature#-#";
257            } elsif ($text =~s/(^|[\n\r]+)*(_____+[\n\r]+.*)$//s) {
258                    $sig = "$1#-#signature##$2##signature#-#";
259          }          }
260    
261          # find quoted text          # find quoted text

Legend:
Removed from v.39  
changed lines
  Added in v.54

  ViewVC Help
Powered by ViewVC 1.1.26