METHOD space Request-URI space HTTP-Version \n\n
Methods are GET, HEAD, OPTIONS, POST, DELETE, etc. The Request-URI is usually the path and name of the document (eg. "/" or "/somedir/index.html"). The HTTP-Version is currently either 1.0 or 1.1.
The HTTP request is normally generated by the user's browser. A typical request with "GET" is
GET /cgi-bin/somefile.cgi HTTP/1.0 CONNECTION: Keep-Alive USER-AGENT: Mozilla/4.0 (compatible; MSIE 5.22; Mac_PowerPC) PRAGMA: no-cache HOST: www.dcs.napier.ac.uk ACCEPT: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */* QUERY_STRING: name=Mary&comments=Hello
A typical request with "POST" is
POST /cgi-bin/somefile.cgi HTTP/1.0 REFERER: http://www.dcs.napier.ac.uk/cgi-bin/someform.html CONNECTION: Keep-Alive USER-AGENT: Mozilla/4.0 (compatible; MSIE 5.22; Mac_PowerPC) HOST: www.dcs.napier.ac.uk ACCEPT: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */* CONTENT-TYPE: application/x-www-form-urlencoded CONTENT-LENGTH: 42 name=Mary&comments=Hello
A typical answer from a webserver starts like this:
HTTP/1.1 200 OK Date: Thu, 18 Nov 2004 15:41:04 GMT Server: Apache/2.0.40 (Red Hat Linux) Accept-Ranges: bytes X-Powered-By: Open SoCks 1.2.0000 Last modified: Thu, 18 Nov 2004 15:41:04 GMT Connection: close Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>
These are different possible first lines (302 indicates a redirection):
HTTP/1.1 200 OK HTTP/1.1 404 Not Found HTTP/1.1 302 Found HTTP/1.0 500 internal error
#!/usr/local/bin/perl
use IO::Socket;
$current_host = "www.napier.ac.uk";
$current_doc = "/";
$remote =IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $current_host,
PeerPort => "http(80)",
);
if (!$remote) {die "cannot connect to http daemon on $current_host"}
$remote->autoflush(1);
print $remote "GET $current_doc HTTP/1.0\r\n";
print $remote "Host: $current_host\r\n\r\n";
$line = <$remote> ;
while ($line) {
print "$line";
$line = <$remote> ;
}
close $remote;
3) Print the output to a file and then view the file through your browser.
use LWP::Simple;
$doc = get 'http://www.napier.ac.uk/';
print $doc;
The user-agent version of LWP allows to control the HTTP request header information, to use cookies, https and password protected pages.
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$req = HTTP::Request->new(GET => 'http://www.napier.ac.uk/');
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if ($res->is_success) {
print $res->content;
} else {
print "Error: " . $res->status_line . "\n";
}
Note: it may be illegal to incorporate other CGI scripts into your script without asking the owner of the original script for permission. At a minimum you would have to inform users about the underlying script.
#!/usr/local/bin/perl -w
use IO::Socket;
$current_host ="ftp.somewhere.ac.uk";
$email_address ="user\@napier.ac.uk";
$remote =IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $current_host,
PeerPort => "ftp(21)",
);
if (!$remote) { die "cannot connect to http daemon on $current_host"}
$remote->autoflush(1);
print $remote "USER anonymous\n";
print $remote "PASS $email_address\n";
print $remote "quit\n";
while (<$remote>)
{print;}
close $remote;