/[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

Annotation of /trunk/httpd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (hide annotations)
Mon May 10 20:26:17 2004 UTC (20 years ago) by dpavlin
File MIME type: text/plain
File size: 7573 byte(s)
major code re-structuring: separation of indexer code into target independent
and depended, documentation improvements

1 dpavlin 5 #!/usr/bin/perl
2    
3     # based on post
4     # http://www.mail-archive.com/libwww@perl.org/msg04750.html
5    
6     use strict;
7     use warnings;
8 dpavlin 41 use MWS::SWISH;
9     #use MWS::Plucene;
10 dpavlin 5 use HTTP::Daemon;
11     use HTTP::Status;
12     use IO::String;
13 dpavlin 6 use CGI::Lite;
14     use Template;
15 dpavlin 24 use URI::Escape;
16 dpavlin 5
17 dpavlin 6 use Data::Dumper;
18    
19 dpavlin 17 my $debug = 1;
20    
21 dpavlin 27 my $config_file = shift @ARGV || 'global.conf';
22    
23     if (! -f $config_file) {
24     print qq{Usage: $0 [/path/to/local.conf]
25    
26     If local.conf is not specified, global.conf in current directory will
27     be used.
28     };
29     exit 1;
30     }
31    
32 dpavlin 41 my $mws = MWS::SWISH->new(config_file => $config_file);
33     #my $mws = MWS::Plucene->new(config_file => $config_file, debug => $debug);
34 dpavlin 39
35     my ($local_addr,$local_port) = ('127.0.0.1',6969);
36    
37     my $listen = $mws->{config}->val('global', 'listen');
38 dpavlin 41
39     print STDERR "using listen $listen\n" if ($listen);
40    
41     if ($listen && $listen =~ m/:/) {
42     ($local_addr,$local_port) = split(/:/,$listen,2);
43 dpavlin 39 } elsif ($listen) {
44     $local_addr = $listen;
45     }
46    
47     my $d = HTTP::Daemon->new(
48     Reuse => 1,
49     LocalAddr => $local_addr,
50     LocalPort => $local_port,
51 dpavlin 41 ) || die "can't create HTTP::Daemon on $local_addr:$local_port: $!";
52 dpavlin 39
53 dpavlin 6 my $cgi = new CGI::Lite;
54     my $tt = Template->new({
55     INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
56     FILTERS => {
57     'body5' => \&body5_filter,
58 dpavlin 28 'body' => \&body_filter,
59 dpavlin 6 },
60 dpavlin 20 EVAL_PERL => 1,
61 dpavlin 6 });
62    
63 dpavlin 25 my $static_html = $mws->{config}->val('global', 'static_html');
64    
65 dpavlin 13 print "Web server ready at: ", $d->url, "\n";
66 dpavlin 6
67 dpavlin 13
68 dpavlin 5 while ( my $c = $d->accept ) {
69     while ( my $r = $c->get_request ) {
70    
71     # environs that a webserver should set.
72     $ENV{'REQUEST_METHOD'} = $r->method;
73     $ENV{'GATEWAY_INTERFACE'} = "CGI/1.0";
74     $ENV{'SERVER_PROTOCOL'} = $r->protocol;
75     $ENV{'CONTENT_TYPE'} = $r->content_type;
76    
77 dpavlin 6 # this part is based on CGI::Lite
78 dpavlin 5
79 dpavlin 6 $cgi->close_all_files();
80     $cgi->{web_data} = {};
81     $cgi->{ordered_keys} = [];
82     $cgi->{all_handles} = [];
83     $cgi->{error_status} = 0;
84     $cgi->{error_message} = undef;
85    
86 dpavlin 5 if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
87 dpavlin 6 my $query_string = $r->uri;
88     $query_string =~ s/[^\?]+\?(.*)/$1/;
89     $cgi->_decode_url_encoded_data (\$query_string, 'form');
90    
91     } elsif ( $r->method eq 'POST' ) {
92    
93     if ($r->content_type eq 'application/x-www-form-urlencoded') {
94     # local $^W = 0;
95     $cgi->_decode_url_encoded_data (\$r->content, 'form');
96     } elsif ($r->content_type =~ /multipart\/form-data/) {
97     my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
98     $cgi->_parse_multipart_data ($r->content_length, $boundary);
99     }
100     } else {
101     $c->send_error(RC_FORBIDDEN);
102 dpavlin 5 }
103    
104 dpavlin 6 my $param = $cgi->{web_data};
105     my $url = $r->url->path;
106 dpavlin 5
107 dpavlin 6 # XXX LOG
108 dpavlin 30 print $r->method," ",$url,"\n";
109     print Dumper($param),"\n" if ($debug);
110 dpavlin 5
111 dpavlin 25 # is this static page?
112     if ($static_html && -f "$static_html/$url") {
113     print "static file: $static_html/$url\n" if ($debug);
114     $c->send_file_response("$static_html/$url");
115     $c->close;
116     next;
117     }
118    
119 dpavlin 7 # template file name (use ?format=html as default)
120     my $tpl_file = 'master.';
121     $tpl_file .= $param->{'format'} || 'html';
122    
123 dpavlin 20 # parse date from url
124     my ($yyyy,$mm,$dd) = $mws->yyyymmdd;
125    
126     my $yyyymm;
127    
128     my $date_limit;
129    
130     if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
131     ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
132     $date_limit = "$yyyy-$mm-$dd";
133     } elsif ($url =~ m,^/(\d{4})[/-](\d+),) {
134     ($yyyy,$mm) = $mws->fmtdate($1,$2);
135     $date_limit = "$yyyy-$mm";
136     } elsif ($url =~ m,^/(\d{4}),) {
137     $date_limit = $mws->fmtdate($1);
138     }
139    
140 dpavlin 7 #
141     # implement functionality and generate HTML
142     #
143 dpavlin 6 my $html;
144 dpavlin 5
145 dpavlin 12 if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
146     $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
147 dpavlin 16 } elsif ($param->{'search'}) {
148     ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
149 dpavlin 12 }
150    
151 dpavlin 13 my $tpl_var = {
152 dpavlin 20 param => $param,
153     yyyy => $yyyy,
154     mm => $mm,
155     dd => $dd,
156 dpavlin 21 date_limit => $date_limit,
157 dpavlin 13 };
158    
159 dpavlin 27 # is this access to root of web server?
160     if ($url eq "/" && !$param->{'search'}) {
161     # if first access, go to current year
162     $date_limit = $mws->fmtdate($yyyy);
163 dpavlin 28 $param->{sort_by} = "date desc";
164 dpavlin 27 }
165    
166 dpavlin 21 # ?show_id=XXXXxxxx___message_id___xxxxXXXX
167     if ($param->{'show_id'}) {
168 dpavlin 20
169 dpavlin 21 $mws->reset_counters;
170     my $row = $mws->fetch_result_by_id($param->{'show_id'});
171     $tpl_var->{message} = $row;
172     } elsif ($param->{'search'} || $date_limit) {
173 dpavlin 5
174 dpavlin 21 # show search results
175     # ?search=foo:bar
176 dpavlin 5
177 dpavlin 27 my @search;
178     push @search, $param->{'search'} if ($param->{'search'});
179 dpavlin 21
180     if ($date_limit) {
181     push @search, "and" if (@search);
182     push @search, "date:\"$date_limit\"";
183     }
184    
185 dpavlin 24 if ($param->{sort_by}) {
186     push @search, "sort:".$param->{sort_by};
187     }
188    
189 dpavlin 21 print STDERR "search: ",join(" ",@search),"\n";
190    
191     my $results = $mws->search(@search);
192 dpavlin 6 my @res = $mws->fetch_all_results();
193 dpavlin 5
194 dpavlin 20 $tpl_var->{results} = \@res if (@res);
195 dpavlin 25 $tpl_var->{total_hits} = $mws->{total_hits} || 0;
196 dpavlin 7
197 dpavlin 30 # no hits, offer suggestions
198     if (! $tpl_var->{results}) {
199     @{$tpl_var->{apropos}} = $mws->apropos_index($param->{'search_fld'}, $param->{'search_val'});
200     }
201    
202 dpavlin 6 }
203 dpavlin 5
204 dpavlin 19
205     # push counters to template
206     foreach my $f (qw(from to cc bcc)) {
207     my $h = $mws->counter($f) || next;
208     my @a;
209     foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
210     push @a, $h->{$k};
211     }
212     $tpl_var->{counters}->{$f} = [ @a ] if (@a);
213     }
214    
215 dpavlin 20 # push calendar in template
216     $tpl_var->{calendar} = $mws->counter('calendar');
217    
218 dpavlin 13 $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
219    
220 dpavlin 7 #
221     # send HTMLto client
222     #
223    
224 dpavlin 5 my $res = HTTP::Response->new(RC_OK);
225 dpavlin 14 $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );
226 dpavlin 6 $res->content($html);
227 dpavlin 5 $c->send_response($res);
228    
229     $c->close;
230     }
231     undef($c);
232     }
233 dpavlin 6
234     # template toolkit filter
235    
236 dpavlin 28 sub html_escape($) {
237 dpavlin 30 my $text = shift || return;
238 dpavlin 28
239 dpavlin 30 # don't re-escape html
240 dpavlin 37 #return $text if ($text =~ /&(?:lt|gt|amp|quot);/);
241 dpavlin 30
242 dpavlin 28 # Escape <, >, & and ", and to produce valid XML
243     my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
244     my $escape_re = join '|' => keys %escape;
245    
246     $text =~ s/($escape_re)/$escape{$1}/gs;
247 dpavlin 34
248     while ($text =~ s/#-#(quote|signature)(\d*)##(.+?)##\1\2#-#/<span class="$1">$3<\/span>/gs) { } ;
249    
250 dpavlin 28 return $text;
251     }
252    
253 dpavlin 12 #use Text::Context::EitherSide;
254    
255 dpavlin 6 sub body5_filter {
256     my $text = shift;
257 dpavlin 28
258     # remove quote
259 dpavlin 37 $text =~ s/^[\>:\|=]+[^\n\r]*[\n\r]*$/#-q-#/msg;
260 dpavlin 28 # remove quote author
261 dpavlin 37 $text =~ s/[\n\r]+[^\n\r]+:\s*(?:#-q-#[\n\r*])+//gs;
262     $text =~ s/^[^\n\r]+:\s*(?:#-q-#[\n\r]*)+//gs;
263 dpavlin 30 $text =~ s/#-q-#[\n\r]*//gs;
264 dpavlin 28 # outlook quoting
265     $text =~ s/(\s*--+\s*Original\s+Message\s*--+.*)$//si;
266 dpavlin 34 $text =~ s/(\s*--+\s*Forwarded\s+message.+\s*--+.*)$//si;
267 dpavlin 28
268     # remove signature
269     $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
270    
271     # compress cr/lf
272     $text =~ s/[\n\r]+/\n/gs;
273    
274     # remove whitespaces
275     $text =~ s/^\n+//gs;
276     $text =~ s/[\s\n]+$//gs;
277    
278 dpavlin 34 if ($text eq "") {
279     $text="#-#quote##forwarded message##quote#-#";
280     }
281    
282 dpavlin 28 # cut to 5 lines;
283 dpavlin 14 if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
284     $text =~ s/[\n\r]*$/ .../;
285     }
286 dpavlin 12
287     # my $context = Text::Context::EitherSide->new($text, context => 5);
288     # return $context->as_string("perl");
289    
290 dpavlin 28 return html_escape($text);
291     }
292    
293     sub body_filter {
294     my $text = shift;
295    
296     my $sig = '';
297    
298     # remove signature
299     if ($text =~ s/([\n\r]+)(--\s*[\n\r]+.*)$//s) {
300     $sig = "$1#-#signature##$2##signature#-#";
301     }
302    
303     # find quoted text
304 dpavlin 37 $text =~ s/^([\>:\|=]+[^\n\r]*[\n\r]*)$/#-#quote1##$1##quote1#-#/mg;
305 dpavlin 30 $text =~ s/(--+\s*Original\s+Message\s*--+.*)$/#-#quote2##$1##quote2#-#/si;
306 dpavlin 34 $text =~ s/(--+\s*Forwarded\s+message.+\s*--+.*)$/#-#quote3##$1##quote3#-#/si;
307 dpavlin 28
308     $text = html_escape($text . $sig);
309 dpavlin 6 return $text;
310     }
311 dpavlin 7

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26