/[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 27 - (hide annotations)
Sat May 8 20:34:26 2004 UTC (20 years ago) by dpavlin
File MIME type: text/plain
File size: 5447 byte(s)
this is 0.9-rc1:
- scripts now accept configuration file as parametar, if none is specified,
  they will use global.conf in current directory
- more css design
- how to install using PAR
- mbox2index can now call swish-e by itself, swish-e configuration
  moved to MWS_swish.pm
- httpd server now shows 30 newest messages in this year when accessed
  through root URL

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     },
39 dpavlin 20 EVAL_PERL => 1,
40 dpavlin 6 });
41    
42 dpavlin 25 my $static_html = $mws->{config}->val('global', 'static_html');
43    
44 dpavlin 13 print "Web server ready at: ", $d->url, "\n";
45 dpavlin 6
46 dpavlin 13
47 dpavlin 5 while ( my $c = $d->accept ) {
48     while ( my $r = $c->get_request ) {
49    
50     # environs that a webserver should set.
51     $ENV{'REQUEST_METHOD'} = $r->method;
52     $ENV{'GATEWAY_INTERFACE'} = "CGI/1.0";
53     $ENV{'SERVER_PROTOCOL'} = $r->protocol;
54     $ENV{'CONTENT_TYPE'} = $r->content_type;
55    
56 dpavlin 6 # this part is based on CGI::Lite
57 dpavlin 5
58 dpavlin 6 $cgi->close_all_files();
59     $cgi->{web_data} = {};
60     $cgi->{ordered_keys} = [];
61     $cgi->{all_handles} = [];
62     $cgi->{error_status} = 0;
63     $cgi->{error_message} = undef;
64    
65 dpavlin 5 if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
66 dpavlin 6 my $query_string = $r->uri;
67     $query_string =~ s/[^\?]+\?(.*)/$1/;
68     $cgi->_decode_url_encoded_data (\$query_string, 'form');
69    
70     } elsif ( $r->method eq 'POST' ) {
71    
72     if ($r->content_type eq 'application/x-www-form-urlencoded') {
73     # local $^W = 0;
74     $cgi->_decode_url_encoded_data (\$r->content, 'form');
75     } elsif ($r->content_type =~ /multipart\/form-data/) {
76     my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
77     $cgi->_parse_multipart_data ($r->content_length, $boundary);
78     }
79     } else {
80     $c->send_error(RC_FORBIDDEN);
81 dpavlin 5 }
82    
83 dpavlin 6 my $param = $cgi->{web_data};
84     my $url = $r->url->path;
85 dpavlin 5
86 dpavlin 6 # XXX LOG
87 dpavlin 17 print $r->method," ",$url,"\n",Dumper($param),"\n" if ($debug);
88 dpavlin 5
89 dpavlin 25 # is this static page?
90     if ($static_html && -f "$static_html/$url") {
91     print "static file: $static_html/$url\n" if ($debug);
92     $c->send_file_response("$static_html/$url");
93     $c->close;
94     next;
95     }
96    
97 dpavlin 7 # template file name (use ?format=html as default)
98     my $tpl_file = 'master.';
99     $tpl_file .= $param->{'format'} || 'html';
100    
101 dpavlin 20 # parse date from url
102     my ($yyyy,$mm,$dd) = $mws->yyyymmdd;
103    
104     my $yyyymm;
105    
106     my $date_limit;
107    
108     if ($url =~ m,^/(\d{4})[/-](\d+)[/-](\d+),) {
109     ($yyyy, $mm, $dd) = $mws->fmtdate($1,$2,$3);
110     $date_limit = "$yyyy-$mm-$dd";
111     } elsif ($url =~ m,^/(\d{4})[/-](\d+),) {
112     ($yyyy,$mm) = $mws->fmtdate($1,$2);
113     $date_limit = "$yyyy-$mm";
114     } elsif ($url =~ m,^/(\d{4}),) {
115     $date_limit = $mws->fmtdate($1);
116     }
117    
118 dpavlin 7 #
119     # implement functionality and generate HTML
120     #
121 dpavlin 6 my $html;
122 dpavlin 5
123 dpavlin 12 if ($param->{'search_val'} && $param->{'search_fld'} && !$param->{'search'}) {
124     $param->{'search'} = $param->{'search_fld'}.":".$param->{'search_val'};
125 dpavlin 16 } elsif ($param->{'search'}) {
126     ($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2);
127 dpavlin 12 }
128    
129 dpavlin 13 my $tpl_var = {
130 dpavlin 20 param => $param,
131     yyyy => $yyyy,
132     mm => $mm,
133     dd => $dd,
134 dpavlin 21 date_limit => $date_limit,
135 dpavlin 13 };
136    
137 dpavlin 27 # is this access to root of web server?
138     if ($url eq "/" && !$param->{'search'}) {
139     # if first access, go to current year
140     $date_limit = $mws->fmtdate($yyyy);
141     $param->{sort} = "date desc";
142     }
143    
144 dpavlin 21 # ?show_id=XXXXxxxx___message_id___xxxxXXXX
145     if ($param->{'show_id'}) {
146 dpavlin 20
147 dpavlin 21 $mws->reset_counters;
148     my $row = $mws->fetch_result_by_id($param->{'show_id'});
149     $tpl_var->{message} = $row;
150     } elsif ($param->{'search'} || $date_limit) {
151 dpavlin 5
152 dpavlin 21 # show search results
153     # ?search=foo:bar
154 dpavlin 5
155 dpavlin 27 my @search;
156     push @search, $param->{'search'} if ($param->{'search'});
157 dpavlin 21
158     if ($date_limit) {
159     push @search, "and" if (@search);
160     push @search, "date:\"$date_limit\"";
161     }
162    
163 dpavlin 24 if ($param->{sort_by}) {
164     push @search, "sort:".$param->{sort_by};
165     }
166    
167 dpavlin 21 print STDERR "search: ",join(" ",@search),"\n";
168    
169     my $results = $mws->search(@search);
170 dpavlin 6 my @res = $mws->fetch_all_results();
171 dpavlin 5
172 dpavlin 20 $tpl_var->{results} = \@res if (@res);
173 dpavlin 25 $tpl_var->{total_hits} = $mws->{total_hits} || 0;
174 dpavlin 7
175 dpavlin 6 }
176 dpavlin 5
177 dpavlin 19
178     # push counters to template
179     foreach my $f (qw(from to cc bcc)) {
180     my $h = $mws->counter($f) || next;
181     my @a;
182     foreach my $k (sort { $h->{$b}->{usage} <=> $h->{$a}->{usage} } keys %$h) {
183     push @a, $h->{$k};
184     }
185     $tpl_var->{counters}->{$f} = [ @a ] if (@a);
186     }
187    
188 dpavlin 20 # push calendar in template
189     $tpl_var->{calendar} = $mws->counter('calendar');
190    
191 dpavlin 13 $tt->process($tpl_file, $tpl_var, \$html) || die $tt->error();
192    
193 dpavlin 7 #
194     # send HTMLto client
195     #
196    
197 dpavlin 5 my $res = HTTP::Response->new(RC_OK);
198 dpavlin 14 $res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' );
199 dpavlin 6 $res->content($html);
200 dpavlin 5 $c->send_response($res);
201    
202     $c->close;
203     }
204     undef($c);
205     }
206 dpavlin 6
207     # template toolkit filter
208    
209 dpavlin 12 #use Text::Context::EitherSide;
210    
211 dpavlin 6 sub body5_filter {
212     my $text = shift;
213     $text =~ s/^\s+//gs;
214 dpavlin 12 $text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text
215     $text =~ s/[\n\r]+/\n/gs; # compress cr/lf
216 dpavlin 14 if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) {
217     $text =~ s/[\n\r]*$/ .../;
218     }
219 dpavlin 7 $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
220 dpavlin 12
221     # my $context = Text::Context::EitherSide->new($text, context => 5);
222     # return $context->as_string("perl");
223    
224 dpavlin 6 return $text;
225     }
226 dpavlin 7

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26