File: http.t

package info (click to toggle)
libhttp-daemon-perl 6.12-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 492 kB
  • sloc: perl: 715; makefile: 2
file content (401 lines) | stat: -rw-r--r-- 10,303 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use Test::Needs 'LWP::UserAgent';

if ($^O eq "MacOS") {
    print "1..0\n";
    exit(0);
}

unless (-f "CAN_TALK_TO_OURSELF") {
    print "1..0 # Skipped: Can't talk to ourself (misconfigured system)\n";
    exit;
}

$| = 1;    # autoflush

require IO::Socket::IP;   # make sure this work before we try to make a HTTP::Daemon

# First we make ourself a daemon in another process
my $D = shift || '';
if ($D eq 'daemon') {

    require HTTP::Daemon;

    my $d = HTTP::Daemon->new(Timeout => 10);

    print "Please to meet you at: <URL:", $d->url, ">\n";
    open(STDOUT, $^O eq 'VMS' ? ">nl: " : ">/dev/null");

    while ($c = $d->accept) {
        $r = $c->get_request;
        if ($r) {
            my $p    = ($r->uri->path_segments)[1];
            my $func = lc("httpd_" . $r->method . "_$p");
            if (defined &$func) {
                &$func($c, $r);
            }
            else {
                $c->send_error(404);
            }
        }
        $c = undef;    # close connection
    }
    print STDERR "HTTP Server terminated\n";
    exit;
}
else {
    use Config;
    my $perl = $Config{'perlpath'};
    $perl = $^X if $^O eq 'VMS' or -x $^X and $^X =~ m,^([a-z]:)?/,i;
    open(DAEMON, "$perl local/http.t daemon |") or die "Can't exec daemon: $!";
}

use Test;
plan tests => 54;

my $greeting = <DAEMON>;
$greeting =~ /(<[^>]+>)/;

require URI;
my $base = URI->new($1);

sub url {
    my $u = URI->new(@_);
    $u = $u->abs($_[1]) if @_ > 1;
    $u->as_string;
}

print "Will access HTTP server at $base\n";

require HTTP::Request;
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/0.01 " . $ua->agent);
$ua->from('gisle@aas.no');

#----------------------------------------------------------------
print "Bad request...\n";
$req = HTTP::Request->new(GET => url("/not_found", $base));
$req->header(X_Foo => "Bar");
$res = $ua->request($req);

ok($res->is_error);
ok($res->code,    404);
ok($res->message, qr/not\s+found/i);

# we also expect a few headers
ok($res->server);
ok($res->date);

#----------------------------------------------------------------
print "Simple echo...\n";

sub httpd_get_echo {
    my ($c, $req) = @_;
    $c->send_basic_header(200);
    print $c "Content-Type: message/http\015\012";
    $c->send_crlf;
    print $c $req->as_string;
}

$req = HTTP::Request->new(GET => url("/echo/path_info?query", $base));
$req->push_header(Accept     => 'text/html');
$req->push_header(Accept     => 'text/plain; q=0.9');
$req->push_header(Accept     => 'image/*');
$req->push_header(':foo_bar' => 1);
$req->if_modified_since(time - 300);
$req->header(
    Long_text => 'This is a very long header line
which is broken between
more than one line.'
);
$req->header(X_Foo => "Bar");

$res = $ua->request($req);

#print $res->as_string;

ok($res->is_success);
ok($res->code,    200);
ok($res->message, "OK");

$_      = $res->content;
@accept = /^Accept:\s*(.*)/mg;

ok($_,      qr/^From:\s*gisle\@aas\.no\n/m);
ok($_,      qr/^Host:/m);
ok(@accept, 3);
ok($_,      qr/^Accept:\s*text\/html/m);
ok($_,      qr/^Accept:\s*text\/plain/m);
ok($_,      qr/^Accept:\s*image\/\*/m);
ok($_,      qr/^If-Modified-Since:\s*\w{3},\s+\d+/m);
ok($_,      qr/^Long-Text:\s*This.*broken between/m);
ok($_,      qr/^Foo-Bar:\s*1\n/m);
ok($_,      qr/^X-Foo:\s*Bar\n/m);
ok($_,      qr/^User-Agent:\s*Mozilla\/0.01/m);

# Try it with the higher level 'get' interface
$res = $ua->get(
    url("/echo/path_info?query", $base),
    Accept => 'text/html',
    Accept => 'text/plain; q=0.9',
    Accept => 'image/*',
    X_Foo  => "Bar",
);

#$res->dump;
ok($res->code,    200);
ok($res->content, qr/^From: gisle\@aas.no$/m);

#----------------------------------------------------------------
print "Send file...\n";

my $file = "test-$$.html";
open(FILE, ">$file") or die "Can't create $file: $!";
binmode FILE or die "Can't binmode $file: $!";
print FILE <<EOT;
<html><title>En pr�ve</title>
<h1>Dette er en testfil</h1>
Jeg vet ikke hvor stor fila beh�ver � v�re heller, men dette
er sikkert nok i massevis.
EOT
close(FILE);

sub httpd_get_file {
    my ($c, $r) = @_;
    my %form = $r->uri->query_form;
    my $file = $form{'name'};
    $c->send_file_response($file);
    unlink($file) if $file =~ /^test-/;
}

$req = HTTP::Request->new(GET => url("/file?name=$file", $base));
$res = $ua->request($req);

#print $res->as_string;

ok($res->is_success);
ok($res->content_type,   'text/html');
ok($res->content_length, 147);
ok($res->title,          'En pr�ve');
ok($res->content,        qr/� v�re/);

# A second try on the same file, should fail because we unlink it
$res = $ua->request($req);

#print $res->as_string;
ok($res->is_error);
ok($res->code, 404);    # not found

# Then try to list current directory
$req = HTTP::Request->new(GET => url("/file?name=.", $base));
$res = $ua->request($req);

#print $res->as_string;
ok($res->code, 501);    # NYI


#----------------------------------------------------------------
print "Check redirect...\n";

sub httpd_get_redirect {
    my ($c) = @_;
    $c->send_redirect("/echo/redirect");
}

$req = HTTP::Request->new(GET => url("/redirect/foo", $base));
$res = $ua->request($req);

#print $res->as_string;

ok($res->is_success);
ok($res->content, qr|/echo/redirect|);
ok($res->previous->is_redirect);
ok($res->previous->code, 301);

# Let's test a redirect loop too
sub httpd_get_redirect2 { shift->send_redirect("/redirect3/") }
sub httpd_get_redirect3 { shift->send_redirect("/redirect2/") }

$req->uri(url("/redirect2", $base));
$ua->max_redirect(5);
$res = $ua->request($req);

#print $res->as_string;
ok($res->is_redirect);
ok($res->header("Client-Warning"), qr/loop detected/i);
ok($res->redirects,                5);

$ua->max_redirect(0);
$res = $ua->request($req);
ok($res->previous,  undef);
ok($res->redirects, 0);
$ua->max_redirect(5);

#----------------------------------------------------------------
print "Check basic authorization...\n";

sub httpd_get_basic {
    my ($c, $r) = @_;

    #print STDERR $r->as_string;
    my ($u, $p) = $r->authorization_basic;
    if (defined($u) && $u eq 'ok 12' && $p eq 'xyzzy') {
        $c->send_basic_header(200);
        print $c "Content-Type: text/plain";
        $c->send_crlf;
        $c->send_crlf;
        $c->print("$u\n");
    }
    else {
        $c->send_basic_header(401);
        $c->print("WWW-Authenticate: Basic realm=\"libwww-perl\"\015\012");
        $c->send_crlf;
    }
}

{

    package MyUA;
    @ISA = qw(LWP::UserAgent);

    sub get_basic_credentials {
        my ($self, $realm, $uri, $proxy) = @_;
        if ($realm eq "libwww-perl" && $uri->rel($base) eq "basic") {
            return ("ok 12", "xyzzy");
        }
        else {
            return undef;
        }
    }
}
$req = HTTP::Request->new(GET => url("/basic", $base));
$res = MyUA->new->request($req);

#print $res->as_string;

ok($res->is_success);

#print $res->content;

# Let's try with a $ua that does not pass out credentials
$res = $ua->request($req);
ok($res->code, 401);

# Let's try to set credentials for this realm
$ua->credentials($req->uri->host_port, "libwww-perl", "ok 12", "xyzzy");
$res = $ua->request($req);
ok($res->is_success);

# Then illegal credentials
$ua->credentials($req->uri->host_port, "libwww-perl", "user", "passwd");
$res = $ua->request($req);
ok($res->code, 401);


#----------------------------------------------------------------
print "Check proxy...\n";

sub httpd_get_proxy {
    my ($c, $r) = @_;
    if ($r->method eq "GET" and $r->uri->scheme eq "ftp") {
        $c->send_basic_header(200);
        $c->send_crlf;
    }
    else {
        $c->send_error;
    }
}

$ua->proxy(ftp => $base);
$req = HTTP::Request->new(GET => "ftp://ftp.perl.com/proxy");
$res = $ua->request($req);

#print $res->as_string;
ok($res->is_success);

#----------------------------------------------------------------
print "Check POSTing...\n";

sub httpd_post_echo {
    my ($c, $r) = @_;
    $c->send_basic_header;
    $c->print("Content-Type: text/plain");
    $c->send_crlf;
    $c->send_crlf;

    # Do it the hard way to test the send_file
    open(TMP, ">tmp$$") || die;
    binmode(TMP);
    print TMP $r->as_string;
    close(TMP) || die;

    $c->send_file("tmp$$");

    unlink("tmp$$");
}

$req = HTTP::Request->new(POST => url("/echo/foo", $base));
$req->content_type("application/x-www-form-urlencoded");
$req->content("foo=bar&bar=test");
$res = $ua->request($req);

#print $res->as_string;

$_ = $res->content;
ok($res->is_success);
ok($_, qr/^Content-Length:\s*16$/mi);
ok($_, qr/^Content-Type:\s*application\/x-www-form-urlencoded$/mi);
ok($_, qr/^foo=bar&bar=test$/m);

$req = HTTP::Request->new(POST => url("/echo/foo", $base));
$req->content_type("multipart/form-data");
$req->add_part(HTTP::Message->new(["Content-Type" => "text/plain"], "Hi\n"));
$req->add_part(HTTP::Message->new(["Content-Type" => "text/plain"], "there\n"));
$res = $ua->request($req);

#print $res->as_string;
ok($res->is_success);
ok($res->content =~ /^Content-Type: multipart\/form-data; boundary=/m);

#----------------------------------------------------------------
print "Check partial content response...\n";

sub httpd_get_partial {
    my ($c) = @_;
    $c->send_basic_header(206);
    print $c "Content-Type: image/jpeg\015\012";
    $c->send_crlf;
    print $c "some fake JPEG content";

}

{
    $req = HTTP::Request->new(GET => url("/partial", $base));
    $res = $ua->request($req);
    ok($res->is_success);    # "a 206 response is considered successful"
}
{
    $ua->max_size(3);
    $req = HTTP::Request->new(GET => url("/partial", $base));
    $res = $ua->request($req);
    ok($res->is_success);    # "a 206 response is considered successful"
                             # Put max_size back how we found it.
    $ua->max_size(undef);
    ok($res->as_string, qr/Client-Aborted: max_size/)
        ;                    # Client-Aborted is returned when max_size is given
}


#----------------------------------------------------------------
print "Terminating server...\n";

sub httpd_get_quit {
    my ($c) = @_;
    $c->send_error(503, "Bye, bye");
    exit;                    # terminate HTTP server
}

$req = HTTP::Request->new(GET => url("/quit", $base));
$res = $ua->request($req);

ok($res->code,    503);
ok($res->content, qr/Bye, bye/);