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

Contents of /trunk/httpd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations)
Thu May 6 19:46:58 2004 UTC (19 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 3655 byte(s)
Make mbox2swish universal for all indexer (moving index-specific
thinks in own .pm) -- after MWS_plucene.pm update it should work with it.
Use Text::AutoFormat to re-format messages wider than wrap_margin
(from global.conf). Hard-coded ISO-8859-2 encoding now works correctly,
Subject searches now remove common subject prefixes, template updates.

1 #!/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 use CGI::Lite;
12 use Template;
13 use MWS;
14
15 use Data::Dumper;
16
17 my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die;
18 my $cgi = new CGI::Lite;
19 my $mws = MWS->new('global.conf');
20 my $tt = Template->new({
21 INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
22 FILTERS => {
23 'body5' => \&body5_filter,
24 'subject_search' => \&subject_search_filter,
25 },
26 });
27
28 print "Web server ready at: ", $d->url, "\n";
29
30
31 while ( my $c = $d->accept ) {
32 while ( my $r = $c->get_request ) {
33
34 # environs that a webserver should set.
35 $ENV{'REQUEST_METHOD'} = $r->method;
36 $ENV{'GATEWAY_INTERFACE'} = "CGI/1.0";
37 $ENV{'SERVER_PROTOCOL'} = $r->protocol;
38 $ENV{'CONTENT_TYPE'} = $r->content_type;
39
40 # this part is based on CGI::Lite
41
42 $cgi->close_all_files();
43 $cgi->{web_data} = {};
44 $cgi->{ordered_keys} = [];
45 $cgi->{all_handles} = [];
46 $cgi->{error_status} = 0;
47 $cgi->{error_message} = undef;
48
49 if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
50 my $query_string = $r->uri;
51 $query_string =~ s/[^\?]+\?(.*)/$1/;
52 $cgi->_decode_url_encoded_data (\$query_string, 'form');
53
54 } elsif ( $r->method eq 'POST' ) {
55
56 if ($r->content_type eq 'application/x-www-form-urlencoded') {
57 # local $^W = 0;
58 $cgi->_decode_url_encoded_data (\$r->content, 'form');
59 } elsif ($r->content_type =~ /multipart\/form-data/) {
60 my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
61 $cgi->_parse_multipart_data ($r->content_length, $boundary);
62 }
63 } else {
64 $c->send_error(RC_FORBIDDEN);
65 }
66
67 my $param = $cgi->{web_data};
68 my $url = $r->url->path;
69
70 # XXX LOG
71 print $r->method," ",$url,Dumper($param);
72
73 # template file name (use ?format=html as default)
74 my $tpl_file = 'master.';
75 $tpl_file .= $param->{'format'} || 'html';
76
77 #
78 # implement functionality and generate HTML
79 #
80 my $html;
81
82 if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
83 $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
84 }
85
86 my $tpl_var = {
87 param => $param
88 };
89
90 # show search results
91 # ?search=foo:bar
92 if ($param->{'search'}) {
93
94 print STDERR "search: ",$param->{'search'},"\n";
95
96 my $results = $mws->search($param->{'search'});
97 my @res = $mws->fetch_all_results();
98
99 $tpl_var->{results} = \@res;
100
101
102 #
103 # ?show_id=XXXXxxxx___message_id___xxxxXXXX
104 } elsif ($param->{'show_id'}) {
105
106 my $row = $mws->fetch_result_by_id($param->{'show_id'});
107 $tpl_var->{message} = $row;
108 }
109
110 $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
111
112 #
113 # send HTMLto client
114 #
115
116 my $res = HTTP::Response->new(RC_OK);
117 $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );
118 $res->content($html);
119 $c->send_response($res);
120
121 $c->close;
122 }
123 undef($c);
124 }
125
126 # template toolkit filter
127
128 #use Text::Context::EitherSide;
129
130 sub body5_filter {
131 my $text = shift;
132 $text =~ s/^\s+//gs;
133 $text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text
134 $text =~ s/[\n\r]+/\n/gs; # compress cr/lf
135 if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
136 $text =~ s/[\n\r]*$/ .../;
137 }
138 $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
139
140 # my $context = Text::Context::EitherSide->new($text, context => 5);
141 # return $context->as_string("perl");
142
143 return $text;
144 }
145
146 sub subject_search_filter {
147 my $s = shift;
148 # remove re: fdw: [list] preffixes from e-mail
149 while ( $s =~ s/^\s*\[(?:re|fwd):\s+(.+)\]\s*$/$1/ig ||
150 $s =~ s/^\s*(?:re|fwd):\s+(.+?)\s*$/$1/ig ||
151 $s =~ s/^\[\w+\]\s*//ig
152 ) { };
153 return $s;
154 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26