Thread wget (7 answers)
Opened by Strazke at 2005-07-11 20:04

esskar
 2005-07-11 20:08
#95429 #95429
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
bzw.
Code: (dl )
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
#!/usr/bin/perl

# Copyright 2005 Sascha Kiefer
# This library is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

use strict;
use warnings;

use IO::Socket::INET ();
use URI ();
use HTTP::Request ();
use HTTP::Response ();

sub max {
    return $_[0] > $_[1] ? $_[0] : $_[1];
}

unless(@ARGV) {
    print "Usage: miniget <url>...\n";
    exit 0;
}

my $url = URI->new($ARGV[0]);

unless(defined $url->scheme and lc($url->scheme) eq 'http') {
    print STDERR "Error: Url must start with http://.\n";
    exit 1;
}

my $host = $url->host;
my $port = $url->port || $url->default_port;

print "Try to connect to $host:$port ...\n";

my $sock = IO::Socket::INET->new(
    PeerAddr => $host,
   PeerPort => "http($port)",
   Proto    => 'tcp'
);

unless($sock) {
    print STDERR "Error: Unable to connect to server.\n";
    exit 1;
}


# Sending request
my $request = HTTP::Request->new();

$request->method('GET');
$request->uri($url->path || '/');
$request->protocol('HTTP/1.0');

$request->header('User-Agent' => 'miniget/0.01');
$request->header('Connection' => 'close');
$request->header('Host' => $host);

print "Sending request ...\n\n";
print $request->as_string;
$sock->print($request->as_string("\015\012");



# Reading response
my $response = HTTP::Response->new();

my $line = $sock->getline;
unless($line =~ m!^(HTTP/\d.\d) (\d+) (.*)\r?\n$!) {
    print STDERR "Error: Response not understandable.\n";
    exit 1;
}

$response->protocol($1);
$response->code($2);
$response->message($3);

$line = $sock->getline;
$line =~ s!\r?\n$!!g;
while($line) {
    my ($key, $val) = split /:\s/, $line;

    $response->header($key => $val);

    $line = $sock->getline;
    $line =~ s!\r?\n$!!g;
}

my $contentlen = $response->header('Content-Length') || 0;

my $bufdata;
my $buflen = 0;

my $len;
while($len = $sock->read($bufdata, max(1024, $contentlen), $buflen)) {
    $buflen += $len;
    last if $contentlen and $buflen >= $contentlen;
}
print "Received response ...\n\n";
$response->content($bufdata);

print $response->as_string;
\n\n

<!--EDIT|esskar|1122748403-->

View full thread wget