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"; |
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 ) { |
while ( my $c = $d->accept ) { |
31 |
while ( my $r = $c->get_request ) { |
while ( my $r = $c->get_request ) { |
32 |
|
|
36 |
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
$ENV{'SERVER_PROTOCOL'} = $r->protocol; |
37 |
$ENV{'CONTENT_TYPE'} = $r->content_type; |
$ENV{'CONTENT_TYPE'} = $r->content_type; |
38 |
|
|
39 |
my $form_parameters; # GET/POST storage. |
# this part is based on CGI::Lite |
40 |
|
|
41 |
# is this a happy GET? |
$cgi->close_all_files(); |
42 |
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
$cgi->{web_data} = {}; |
43 |
$form_parameters = $r->uri; |
$cgi->{ordered_keys} = []; |
44 |
$form_parameters =~ s/[^\?]+\?(.*)/$1/; |
$cgi->{all_handles} = []; |
45 |
$CGI::Q = new CGI($form_parameters); |
$cgi->{error_status} = 0; |
46 |
} |
$cgi->{error_message} = undef; |
47 |
|
|
48 |
# possibly POST? |
if ( $r->method eq 'GET' || $r->uri =~ /\?/ ) { |
49 |
if ( $r->method eq 'POST' ) { |
my $query_string = $r->uri; |
50 |
|
$query_string =~ s/[^\?]+\?(.*)/$1/; |
51 |
# now decide how we want to turn the parameters |
$cgi->_decode_url_encoded_data (\$query_string, 'form'); |
52 |
# over to CGI.pm. note that this will cause |
|
53 |
# problems with your STDIN with multipart forms. |
} elsif ( $r->method eq 'POST' ) { |
54 |
my $form_parameters = $r->content; |
|
55 |
$ENV{'CONTENT_LENGTH'} = $r->content_length || 0; |
if ($r->content_type eq 'application/x-www-form-urlencoded') { |
56 |
|
# local $^W = 0; |
57 |
# sounds like multipart. |
$cgi->_decode_url_encoded_data (\$r->content, 'form'); |
58 |
if ( $form_parameters =~ /^--/ ) { |
} elsif ($r->content_type =~ /multipart\/form-data/) { |
59 |
|
my ($boundary) = $r->content_type =~ /boundary=(\S+)$/; |
60 |
my ($boundary) = split ( /\n/, $form_parameters ); |
$cgi->_parse_multipart_data ($r->content_length, $boundary); |
|
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(); |
|
61 |
} |
} |
62 |
|
} else { |
|
else { $CGI::Q = new CGI($form_parameters); } |
|
|
} |
|
|
|
|
|
if (! $CGI::Q) { |
|
63 |
$c->send_error(RC_FORBIDDEN); |
$c->send_error(RC_FORBIDDEN); |
64 |
} |
} |
65 |
|
|
66 |
my $param = param("hello") || ''; |
my $param = $cgi->{web_data}; |
67 |
my $url = $r->url->path; |
my $url = $r->url->path; |
68 |
|
|
69 |
print "I saw: $param\n"; |
# XXX LOG |
70 |
|
print $r->method," ",$url,Dumper($param); |
71 |
|
|
72 |
# my $f = param("thefile"); |
# template file name (use ?format=html as default) |
73 |
# print "thefile filename: $f\n"; |
my $tpl_file = 'master.'; |
74 |
# { undef $/; print "thefile size: ", length(<$f>), "\n" } |
$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 |
my $res = HTTP::Response->new(RC_OK); |
# |
107 |
$res->content( qq{ |
# send HTMLto client |
108 |
<html> |
# |
|
hello = $param<br> |
|
|
URL: $url |
|
|
</html> |
|
|
} |
|
|
); |
|
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); |
$c->send_response($res); |
114 |
|
|
115 |
$c->close; |
$c->close; |
116 |
} |
} |
117 |
undef($c); |
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<--[cut]--8<--</span>,s; |
127 |
|
$text =~ s/[\n\r]+--\s*[\n\r]+.*$//s; |
128 |
|
return $text; |
129 |
|
} |
130 |
|
|