/[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 25 - (show annotations)
Sat May 8 16:06:58 2004 UTC (19 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 5001 byte(s)
improvements in user-interface (css). http server can now server static files
(and it's serving style.css from html dir).

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26