/[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 37 - (hide annotations)
Sun May 9 23:18:12 2004 UTC (19 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 7059 byte(s)
more documentation improvements (small steps), fixed regexps and quouting,
better support for subjects with Re[4]:

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26