/[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 47 by dpavlin, Tue May 11 22:59:27 2004 UTC revision 53 by dpavlin, Fri Jun 18 14:56:40 2004 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3  # based on post  my $basedir = '';
 # http://www.mail-archive.com/libwww@perl.org/msg04750.html  
   
4  BEGIN {  BEGIN {
5          my $basedir = readlink($0) || $0; $basedir =~ s#/[^/]+$##;          $basedir = $0;
6          unshift(@INC, $basedir);          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  =head1 NAME
# Line 18  httpd.pl - http server for Mail::Box Web Line 23  httpd.pl - http server for Mail::Box Web
23    
24  =head1 DESCRIPTION  =head1 DESCRIPTION
25    
26  This is small http server, based on C<HTTP::Daemon> which is designed  This script implements user interface for Mail::Box Web Search as
27  for single-user use (on laptop for example) via loopback.  a small single-user http server.
   
 It doesn't provide any authentification or authorisation, and it can handle  
 just one request at the time, so it's not suted for public-facing sites,  
 even if you don't care about security of your mailboxes.  
28    
29  =head1 SEE ALSO  =head1 SEE ALSO
30    
31  C<MWS> perl modules which are part of this package  C<MWS> perl modules which are part of this package
32    C<MWS::HTTPD> module which implements the server itself
33    
34  =cut  =cut
35    
# Line 35  use strict; Line 37  use strict;
37  use warnings;  use warnings;
38  use MWS::SWISH;  use MWS::SWISH;
39  #use MWS::Plucene;  #use MWS::Plucene;
40  use HTTP::Daemon;  use HTTP::Daemon::Simple;
 use HTTP::Status;  
 use IO::String;  
 use CGI::Lite;  
41  use Template;  use Template;
42  use URI::Escape;  use URI::Escape;
43    
# Line 60  be used. Line 59  be used.
59  my $mws = MWS::SWISH->new(config_file => $config_file);  my $mws = MWS::SWISH->new(config_file => $config_file);
60  #my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug);  #my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug);
61    
 my ($local_addr,$local_port) = ('127.0.0.1',6969);  
   
 my $listen = $mws->{config}->val('global', 'listen');  
   
 print STDERR "using listen $listen\n" if ($listen);  
   
 if ($listen && $listen =~ m/:/) {  
         ($local_addr,$local_port) = split(/:/,$listen,2);  
 } elsif ($listen) {  
         $local_addr = $listen;  
 }  
   
 my $d = HTTP::Daemon->new(  
         Reuse => 1,  
         LocalAddr => $local_addr,  
         LocalPort => $local_port,  
 ) || die "can't create HTTP::Daemon on $local_addr:$local_port: $!";  
   
 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 88  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            'static_html' => $mws->{config}->val('global', 'static_html'),
74            'debug' => $debug,
75    ) || die "can't create HTTP::Daemon::Simple: $!";
76    
77    
78  print "Web server ready at: ", $d->url, "\n";  print "Web server ready at: ", $d->url, "\n";
79    
80    $d->run_server( \&request );
81    
82  while ( my $c = $d->accept ) {  sub request($$) {
83          while ( my $r = $c->get_request ) {          my ($url,$param) = @_;
84    
85                  # 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);  
                 }  
86    
87                  my $param = $cgi->{web_data};          # template file name (use ?format=html as default)
88                  my $url = $r->url->path;          my $tpl_file = 'master.';
89            $tpl_file .= $param->{'format'} || 'html';
90    
91                  # XXX LOG          # parse date from url
92                  print $r->method," ",$url,"\n";          my ($yyyy,$mm,$dd) = $mws->yyyymmdd;
                 print Dumper($param,$mws->{counter}),"\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;  
                 }  
   
                 # template file name (use ?format=html as default)  
                 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);  
                 }  
93    
94                  #          my $yyyymm;
                 # 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);  
                 }  
95    
96                  my $tpl_var = {          my $date_limit;
                         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";  
                 }  
97    
98                  # ?show_id=XXXXxxxx___message_id___xxxxXXXX          if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
99                  if ($param->{'show_id'}) {                  ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
100                     $date_limit = "$yyyy-$mm-$dd";
101            } 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                          $mws->reset_counters;          #
109                          my $row = $mws->fetch_result_by_id($param->{'show_id'});          # implement functionality and generate HTML
110                          $tpl_var->{message} = $row;          #
111                  } elsif ($param->{'search'} || $date_limit) {          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                          # show search results          my $tpl_var = {
120                          # ?search=foo:bar                  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                          my @search;          # ?show_id=XXXXxxxx___message_id___xxxxXXXX
135                          push @search, $param->{'search'} if ($param->{'search'});          if ($param->{'show_id'}) {
136    
137                          if ($date_limit) {                  $mws->reset_counters;
138                                  push @search, "and" if (@search);                  my $row = $mws->fetch_result_by_id($param->{'show_id'});
139                                  push @search, "date:\"$date_limit\"";                  $tpl_var->{message} = $row;
140                          }          } elsif ($param->{'search'} || $date_limit) {
141    
142                          if ($param->{sort_by}) {                  # show search results
143                                  push @search, "sort:".$param->{sort_by};                  # ?search=foo:bar
                         }  
144    
145                          print STDERR "search: ",join(" ",@search),"\n";                  my @search;
146                    push @search, $param->{'search'} if ($param->{'search'});
147    
148                          my $results = $mws->search(@search);                  if ($date_limit) {
149                          my @res = $mws->fetch_all_results();                          push @search, "and" if (@search);
150                            push @search, "date:\"$date_limit\"";
151                    }
152    
153                          $tpl_var->{results} = \@res if (@res);                  if ($param->{sort_by}) {
154                          $tpl_var->{total_hits} = $mws->{total_hits} || 0;                          push @search, "sort:".$param->{sort_by};
155                    }
156    
157                          # no hits, offer suggestions                  print STDERR "search: ",join(" ",@search),"\n";
                         if (! $tpl_var->{results}) {  
                                 @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});  
                         }  
158    
159                  }                  my $results = $mws->search(@search);
160                    my @res = $mws->fetch_all_results();
161    
162                    $tpl_var->{results} = \@res if (@res);
163                    $tpl_var->{total_hits} = $mws->{total_hits} || 0;
164    
165                  # push counters to template                  # no hits, offer suggestions
166                  foreach my $f (qw(from to cc bcc folder)) {                  if (! $tpl_var->{results} && $param->{'search_fld'} && $param->{'search_val'}) {
167                          my $h = $mws->counter($f) || next;                          @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});
                         my @a;  
                         foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {  
                                 push @a, $h->{$k};  
                         }  
                         $tpl_var->{counters}->{$f} = [ @a ] if (@a);  
168                  }                  }
169    
170                  # push calendar in template          }
                 $tpl_var->{calendar} = $mws->counter('calendar');  
   
                 $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();  
171    
172                  #          # push counters to template
173                  # send HTMLto client          foreach my $f (qw(from to cc bcc folder)) {
174                  #                  my $h = $mws->counter($f) || next;
175                    my @a;
176                    foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
177                            push @a, $h->{$k};
178                    }
179                    $tpl_var->{counters}->{$f} = [ @a ] if (@a);
180            }
181    
182                  my $res = HTTP::Response->new(RC_OK);          # push calendar in template
183                  $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );          $tpl_var->{calendar} = $mws->counter('calendar');
                 $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    

Legend:
Removed from v.47  
changed lines
  Added in v.53

  ViewVC Help
Powered by ViewVC 1.1.26