/[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 7 - (show annotations)
Wed May 5 15:38:35 2004 UTC (20 years ago) by dpavlin
File MIME type: text/plain
File size: 3106 byte(s)
extracted more methods, support for other formats, misc improvements

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 print "Please contact me at: <URL:", $d->url, ">\n";
19
20 my $cgi = new CGI::Lite;
21 my $mws = MWS->new('global.conf');
22 my $tt = Template->new({
23 INCLUDE_PATH => $mws->{config}->val('global', 'templates'),
24 FILTERS => {
25 'body5' => \&body5_filter,
26 },
27 });
28
29
30 while ( my $c = $d->accept ) {
31 while ( my $r = $c->get_request ) {
32
33 # environs that a webserver should set.
34 $ENV{'REQUEST_METHOD'} = $r->method;
35 $ENV{'GATEWAY_INTERFACE'} = "CGI/1.0";
36 $ENV{'SERVER_PROTOCOL'} = $r->protocol;
37 $ENV{'CONTENT_TYPE'} = $r->content_type;
38
39 # this part is based on CGI::Lite
40
41 $cgi->close_all_files();
42 $cgi->{web_data} = {};
43 $cgi->{ordered_keys} = [];
44 $cgi->{all_handles} = [];
45 $cgi->{error_status} = 0;
46 $cgi->{error_message} = undef;
47
48 if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) {
49 my $query_string = $r->uri;
50 $query_string =~ s/[^\?]+\?(.*)/$1/;
51 $cgi->_decode_url_encoded_data (\$query_string, 'form');
52
53 } elsif ( $r->method eq 'POST' ) {
54
55 if ($r->content_type eq 'application/x-www-form-urlencoded') {
56 # local $^W = 0;
57 $cgi->_decode_url_encoded_data (\$r->content, 'form');
58 } elsif ($r->content_type =~ /multipart\/form-data/) {
59 my ($boundary) = $r->content_type =~ /boundary=(\S+)$/;
60 $cgi->_parse_multipart_data ($r->content_length, $boundary);
61 }
62 } else {
63 $c->send_error(RC_FORBIDDEN);
64 }
65
66 my $param = $cgi->{web_data};
67 my $url = $r->url->path;
68
69 # XXX LOG
70 print $r->method," ",$url,Dumper($param);
71
72 # template file name (use ?format=html as default)
73 my $tpl_file = 'master.';
74 $tpl_file .= $param->{'format'} || 'html';
75
76 #
77 # implement functionality and generate HTML
78 #
79 my $html;
80
81 # show search results
82 # ?search=foo:bar
83 if ($param->{'search'}) {
84
85 print STDERR "search: ",$param->{'search'},"\n";
86
87 my $results = $mws->search($param->{'search'});
88 my @res = $mws->fetch_all_results();
89
90 $tt->process($tpl_file, {
91 query => $param->{'search'},
92 results => \@res,
93 param => $param,
94 }, \$html) || die $tt->error();
95
96 #
97 # ?show_id=XXXXxxxx___message_id___xxxxXXXX
98 } elsif ($param->{'show_id'}) {
99
100 my $row = $mws->fetch_result_by_id($param->{'show_id'});
101 $tt->process($tpl_file, {
102 message => $row,
103 }, \$html) || die $tt->error();
104 }
105
106 #
107 # send HTMLto client
108 #
109
110 my $res = HTTP::Response->new(RC_OK);
111 $res->header( 'Content-type' => 'text/html; charset=iso-8859-2' );
112 $res->content($html);
113 $c->send_response($res);
114
115 $c->close;
116 }
117 undef($c);
118 }
119
120 # template toolkit filter
121
122 sub body5_filter {
123 my $text = shift;
124 $text =~ s/^\s+//gs;
125 $text =~ s/[\n\r]+[\>:\|=]+\s*.*?[\n\r]+//sg; # remove quoted text
126 $text =~ s,^((?:.*?[\n\r]){5}).*$,$1<span style="color:#808080">--8&lt;--[cut]--8&lt;--</span>,s;
127 $text =~ s/[\n\r]+--\s*[\n\r]+.*$//s;
128 return $text;
129 }
130

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26