File: log-server

package info (click to toggle)
libwww-mechanize-perl 1.71-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 620 kB
  • sloc: perl: 3,272; makefile: 4
file content (196 lines) | stat: -rwxr-xr-x 6,417 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# vi: ft=perl
# Thanks to merlyn for nudging me and giving me this snippet!
use strict;
use HTTP::Daemon;
use CGI;
use encoding 'iso-8859-1';
use Getopt::Long;
use vars qw($VERSION);
$VERSION = '0.55';

$|++;

GetOptions(
    'e=s' => \my $expression,
);

my $host = 'localhost';
my $d = HTTP::Daemon->new(
    LocalAddr => $host,
) or die;

# HTTP::Deamon doesn't return http://localhost:.../
# for LocalAddr => 'localhost'. This causes the
# tests to fail of many machines.
( my $url = URI->new($d->url) )->host($host);
print "$url\n";

my ($filename,$logfile) = @ARGV[0,1];
if ($filename) {
  open DATA, "< $filename"
    or die "Couldn't read page '$filename' : $!\n";
};
#open LOG, ">", $logfile
#  or die "Couldn't create logfile '$logfile' : $!\n";
my $log;
binmode DATA,':encoding(iso-8859-1)';
my $body = join "", <DATA>;

sub debug($) {
  my $message = $_[0];
  $message =~ s!\n!\n#SERVER:!g;
  warn "#SERVER: $message"
    if $ENV{TEST_HTTP_VERBOSE};
};

SERVERLOOP: {
  my $quitserver;
  while (my $c = $d->accept) {
    debug "New connection";
    while (my $r = $c->get_request) {
      debug "Request:\n" . $r->as_string;
      my $location = ($r->uri->path || "/");
      my ($link1,$link2) = ('','');
      if ($location =~ m!^/link/([^/]+)/(.*)$!) {
        ($link1,$link2) = ($1,$2);
      };
      my $res;
      if ($location eq '/get_server_log') {
        $res = HTTP::Response->new(200, "OK", undef, $log);
        $log = '';
      } elsif ( $location eq '/quit_server') {
        debug "Quitting";
        $res = HTTP::Response->new(200, "OK", [Connection => 'close'], "quit");
        $quitserver = 1;
      } else {
        eval $expression
            if $expression;
        warn "eval: $@" if $@;
        $log .= "Request:\n" . $r->as_string . "\n";
        if ($location =~ m!^/redirect/(.*)$!) {
            $res = HTTP::Response->new(302);
            $res->header('location', $d->url . $1);
        } elsif ($location =~ m!^/error/notfound/(.*)$!) {
            $res = HTTP::Response->new(404, "Not found", [Connection => 'close']);
        } elsif ($location =~ m!^/error/timeout/(\d+)$!) {
            sleep $1;
            $res = HTTP::Response->new(599, "Timeout reached", [Connection => 'close']);
        } elsif ($location =~ m!^/error/close/(\d+)$!) {
            sleep $1;
            $res = undef;
        } elsif ( $location =~ m!^/chunks!) {
            my $count = 5;
            $res = HTTP::Response->new(200, "OK", undef, sub {
               sleep 1;
               my $buf = 'x' x 16;
               return $buf if $count-- > 0;
               return undef; # done
            });
        } elsif ($location =~ m!^/error/after_headers$!) {
            my $count = 2;
            $res = HTTP::Response->new(200, "OK", undef, sub {
               sleep 1;
               my $buf = 'x' x 16;
               return $buf if $count-- > 0;
               die "Planned error after headers";
            });
        } elsif ($location =~ m!^/encoding/(.*)!) {
            my $encoding = $1;
            $res = HTTP::Response->new(
                200, "OK",
                [ 'Content-Type' => "text/html; charset=$encoding" ],
                "encoding $encoding"
            );
        } else {
            my $q = CGI->new($r->uri->query);

            # Make sticky form fields
            my ($query,$session,%cat);
            $query = defined $q->param('query') ? $q->param('query') : "(empty)";
            $session = defined $q->param('session') ? $q->param('session') : 1;
            %cat = map { $_ => 1 } (defined $q->param('cat') ? $q->param('cat') : qw( cat_foo cat_bar ));
            my @categories = map { $cat{$_} ? "checked" : "" } qw( cat_foo cat_bar cat_baz );
            (my $h = $r->headers->{host}) =~ s/:\d+//;
            my $rbody = sprintf $body,$location,$session,$query,@categories;
            $res = HTTP::Response->new(200, "OK", [
                  "Set-Cookie" => $q->cookie(-name => 'log-server',-value=>'shazam2', -discard=>1,),
                  'Cache-Control' => 'no-cache',
                  'Pragma' => 'no-cache',
                  'Max-Age' => 0,
                  'Connection' => 'close',
                  'Content-Length' => length($rbody),
              ], $rbody);

            $res->content_type(
                $q->param('xml') ? 'application/xhtml+xml' : 'text/html'
            );

            debug "Request " . ($r->uri->path || "/");
        };
      };
      debug "Response:\n" . $res->as_string
          if $res;
      eval {
        $c->send_response($res)
            if $res;
      };
      if (my $err = $@) {
          debug "Server raised error: $err";
          if ($err !~ /^Planned error\b/) {
              warn $err;
          };
          $c->close;
      };
      if (! $res) {
          $c->close;
      };
      last if $quitserver;
    }
    sleep 1;
    undef($c);
    last SERVERLOOP
      if $quitserver;
  }
};
END { debug "Server stopped" };

__DATA__
<html>
<head>
<title>WWW::Mechanize test page</title>
</head>
<body>
<h1>Location: %s</h1>
<p>
  <a href="/test">Link /test</a>
  <a href="/foo">Link /foo</a>
  <a href="/slash_end">Link /</a>
  <a href="/slash_front">/Link </a>
  <a href="/slash_both">/Link in slashes/</a>
  <a href="/foo1.save_log_server_test.tmp">Link foo1.save_log_server_test.tmp</a>
  <a href="/foo2.save_log_server_test.tmp">Link foo2.save_log_server_test.tmp</a>
  <a href="/foo3.save_log_server_test.tmp">Link foo3.save_log_server_test.tmp</a>
  <a href="/o-umlaut">Lschen -- testing for o-umlaut.</a>
  <a href="/o-umlaut-encoded">St&ouml;sberg -- testing for encoded o-umlaut.</a>

  <table>
    <tr><th>Col1</th><th>Col2</th><th>Col3</th></tr>
    <tr><td>A1</td><td>A2</td><td>A3</td></tr>
    <tr><td>B1</td><td>B2</td><td>B3</td></tr>
    <tr><td>C1</td><td>C2</td><td>C3</td></tr>
  </table>
  <form name="f" action="/formsubmit">
    <input type="hidden" name="session" value="%s"/>
    <input type="text" name="query" value="%s"/>
    <input type="submit" name="submit" value="Go"/>
    <input type="checkbox" name="cat" value="cat_foo" %s />
    <input type="checkbox" name="cat" value="cat_bar" %s />
    <input type="checkbox" name="cat" value="cat_baz" %s />
    <input type="file" name="upload" value="README" />
  </form>
  <form id="pounder" action="/formsubmit">
    <input type="text" name="query" value="%s"/>
  </form>
</p>
</body>
</html>