#!/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 ...\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;