/[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 39 - (hide annotations)
Mon May 10 14:07:44 2004 UTC (20 years ago) by dpavlin
File MIME type: text/plain
File size: 7351 byte(s)
added listen directive to configuration to specify IP address and port to
listen to

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26