Re: Please review new Perl 5 Module List
Gisle.Aas@nr.no
Tue, 28 Feb 95 14:52:35 +0100
m.koster@nexor.co.uk writes:
> Well, I hope to get a little bit more time to work on this soon. How
> up-to-date is your tar file? Can I send diffs? I'll try and put in the
> URL mehods and the callback/file handling tonight (if you haven't done
> it yet :).
The tar file is up-to date. I would be happy to incorportate any
patches. I will start using version numbers for the next release.
> Any thoughts on the desirability/hassle of splitting "sub request"
> into "request_response" and "request_data"?
I think splitting is a good idea. May I suggest something like this:
Classes:
WWW::Request
WWW::Response
WWW::URL
WWW::MIME::Header (or just WWW::Header)
In order to create an environment in order to issue requests you must
create a WWW::Request object. You might set some attributes of this
object before you perform the actual request with the doit method.
The doit method returns a WWW::Response object (the WWW::Response
object should have no other constructor).
The WWW::Response object will initially capture the state of the request
after the headers have been read, but before the content (if any) has
been read. You might use various methods to inquire about the
response code and various header entries. Or use the content method
to read the content.
The WWW::URL class is used to represent URLs. The current design is
OK, but the tie interface will be replaced by the metods like: scheme,
host, port, path, query, fragment.
The WWW::MIME::Header class is used to represent a collection of
header entries.
Typical usage would be:
$request = new WWW::Request;
$request->timeout(60);
$request->defaultHeader('user-agent', 'myprogram/0.1');
$url = new WWW::URL "http://host/path";
$response = request->doit("GET", $url);
if ($response->isOK) {
$filename = $url->basename . $suffix{response->contentType};
$response->content($filename);
}
As mentioned before the $response->content method might also take a
sub that it treats as an callback routine. The content method without
any arguments just returns the content as a string.
The library might also provide convenience routines like this:
sub get
{
my $request = new WWW::Request;
my $response = $request->doit("GET", @_);
return ($response->code, $response->headers, $response->content);
}
Some discussion on details:
---------------------------
WWW::Request methods:
new();
Constructor
set_default_header($key, $val);
set_timeout($seconds);
Returns the old value
set_content($contentRef);
Setting these things make more of the parameters to
doit() optional.
doit($method, $url, $headers, $content, $timeout);
Returns a reference to a WWW::Response object.
WWW::Response methods:
code()
Returns the the nummeric response code
mnemonic()
Returns the textual representation of the response code
isRedirect();
isOK();
isXXX();
Test the response code
headers();
Returns a ref to the WWW::MIME::Header object
contentType();
contentEncoding();
contentLength();
Returns the corresponing header value
content()
Returns the content as a string
content($file);
Writes the content to a file. Returns 0 ok.
content(sub {...})
Reads the content in suitable chunks and calls sub for each
chunk. At EOF it calls sub with undef as argument.
WWW::URL methods:
new($string);
Creates a new URL object based on the string representation.
scheme()
host()
port()
path()
query()
fragment()
Set or query parts/attributes of an URL
str()
Returns a string representation of the URL
parent($baseUrl)
Make a relative URL into an absoulute URL. Perhaps a better
name for this method.
proxify()
Convert an URL into a proxified versjon based on some
environment variables.
--Gisle