8 |
use HTTP::Daemon; |
use HTTP::Daemon; |
9 |
use HTTP::Status; |
use HTTP::Status; |
10 |
use IO::String; |
use IO::String; |
11 |
use CGI 2.50 qw/:standard :cgi-lib/; |
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; |
my $d = HTTP::Daemon->new( Reuse => 1, LocalPort => 6969 ) || die; |
18 |
print "Please contact me at: <URL:", $d->url, ">\n"; |
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 ) { |
while ( my $c = $d->accept ) { |
32 |
while ( my $r = $c->get_request ) { |
while ( my $r = $c->get_request ) { |
37 |
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
38 |
$ENV{'CONTENT_TYPE'} = $r->content_type; |
$ENV{'CONTENT_TYPE'} = $r->content_type; |
39 |
|
|
40 |
my $form_parameters; # GET/POST storage. |
# 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 |
|
|
|
# is this a happy GET? |
|
49 |
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
50 |
$form_parameters = $r->uri; |
my $query_string = $r->uri; |
51 |
$form_parameters =~ s/[^\?]+\?(.*)/$1/; |
$query_string =~ s/[^\?]+\?(.*)/$1/; |
52 |
$CGI::Q = new CGI($form_parameters); |
$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 |
# possibly POST? |
my $param = $cgi->{web_data}; |
68 |
if ( $r->method eq 'POST' ) { |
my $url = $r->url->path; |
69 |
|
|
70 |
# now decide how we want to turn the parameters |
# XXX LOG |
71 |
# over to CGI.pm. note that this will cause |
print $r->method," ",$url,Dumper($param); |
|
# problems with your STDIN with multipart forms. |
|
|
my $form_parameters = $r->content; |
|
|
$ENV{'CONTENT_LENGTH'} = $r->content_length || 0; |
|
|
|
|
|
# sounds like multipart. |
|
|
if ( $form_parameters =~ /^--/ ) { |
|
|
|
|
|
my ($boundary) = split ( /\n/, $form_parameters ); |
|
|
chop($boundary); |
|
|
substr( $boundary, 0, 2 ) = ''; # delete the leading "--" !!! |
|
|
$ENV{'CONTENT_TYPE'} = |
|
|
$r->content_type . "; boundary=$boundary"; |
|
|
|
|
|
# this breaks STDIN forever. I've yet to discover |
|
|
# how to properly save and reassign STDIN after |
|
|
# we're done breaking things horrifically here. |
|
|
close STDIN; |
|
|
my $t = tie *STDIN, 'IO::String'; |
|
|
$t->open($form_parameters); |
|
|
$CGI::Q = new CGI(); |
|
|
} |
|
72 |
|
|
73 |
else { $CGI::Q = new CGI($form_parameters); } |
# 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 |
|
} elsif ($param->{'search'}) { |
85 |
|
($param->{'search_fld'}, $param->{'search_val'}) = split(/:/,$param->{'search'},2); |
86 |
} |
} |
87 |
|
|
88 |
if (! $CGI::Q) { |
my $tpl_var = { |
89 |
$c->send_error(RC_FORBIDDEN); |
param => $param |
90 |
} |
}; |
91 |
|
|
92 |
my $param = param("hello") || ''; |
# show search results |
93 |
my $url = $r->url->path; |
# ?search=foo:bar |
94 |
|
if ($param->{'search'}) { |
95 |
|
|
96 |
print "I saw: $param\n"; |
print STDERR "search: ",$param->{'search'},"\n"; |
97 |
|
|
98 |
# my $f = param("thefile"); |
my $results = $mws->search($param->{'search'}); |
99 |
# print "thefile filename: $f\n"; |
my @res = $mws->fetch_all_results(); |
100 |
# { undef $/; print "thefile size: ", length(<$f>), "\n" } |
|
101 |
|
$tpl_var->{results} = \@res; |
102 |
|
|
|
my $res = HTTP::Response->new(RC_OK); |
|
|
$res->content( qq{ |
|
|
<html> |
|
|
hello = $param<br> |
|
|
URL: $url |
|
|
</html> |
|
|
} |
|
|
); |
|
103 |
|
|
104 |
|
# |
105 |
|
# ?show_id=XXXXxxxx___message_id___xxxxXXXX |
106 |
|
} elsif ($param->{'show_id'}) { |
107 |
|
|
108 |
|
my $row = $mws->fetch_result_by_id($param->{'show_id'}); |
109 |
|
$tpl_var->{message} = $row; |
110 |
|
} |
111 |
|
|
112 |
|
$tt->process($tpl_file, $tpl_var, \$html) || die $tt->error(); |
113 |
|
|
114 |
|
# |
115 |
|
# send HTMLto client |
116 |
|
# |
117 |
|
|
118 |
|
my $res = HTTP::Response->new(RC_OK); |
119 |
|
$res->header( 'Content-type' => 'text/html; charset=ISO-8859-2' ); |
120 |
|
$res->content($html); |
121 |
$c->send_response($res); |
$c->send_response($res); |
122 |
|
|
123 |
$c->close; |
$c->close; |
124 |
} |
} |
125 |
undef($c); |
undef($c); |
126 |
} |
} |
127 |
|
|
128 |
|
# template toolkit filter |
129 |
|
|
130 |
|
#use Text::Context::EitherSide; |
131 |
|
|
132 |
|
sub body5_filter { |
133 |
|
my $text = shift; |
134 |
|
$text =~ s/^\s+//gs; |
135 |
|
$text =~ s/^[\>:\|=]+\s*.*?$//msg; # remove quoted text |
136 |
|
$text =~ s/[\n\r]+/\n/gs; # compress cr/lf |
137 |
|
if ($text =~ s,^((?:.*?[\n\r]){5}).*$,$1,s) { |
138 |
|
$text =~ s/[\n\r]*$/ .../; |
139 |
|
} |
140 |
|
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
141 |
|
|
142 |
|
# my $context = Text::Context::EitherSide->new($text, context => 5); |
143 |
|
# return $context->as_string("perl"); |
144 |
|
|
145 |
|
return $text; |
146 |
|
} |
147 |
|
|
148 |
|
sub subject_search_filter { |
149 |
|
my $s = shift; |
150 |
|
# remove re: fdw: [list] preffixes from e-mail |
151 |
|
while ( $s =~ s/^\s*\[(?:re|fwd|fw):\s+(.+)\]\s*$/$1/ig || |
152 |
|
$s =~ s/^\s*(?:re|fwd|fw):\s+(.+?)\s*$/$1/ig || |
153 |
|
$s =~ s/^\[\S+\]\s*//ig || |
154 |
|
$s =~ s/^\[[^@]+@\w+\.\w+\s*:\s+(.+)\s*\]\s*$/$1/g |
155 |
|
) { }; |
156 |
|
return $s; |
157 |
|
} |