HTTP::Cookies::Guess - Guesses UserAgent from file name.


HTTP-Cookies-Guess documentation Contained in the HTTP-Cookies-Guess distribution.

Index


Code Index:

NAME

Top

HTTP::Cookies::Guess - Guesses UserAgent from file name.

SYNOPSIS

Top

  use HTTP::Cookies::Guess;
  $cookie_jar = HTTP::Cookies::Guess->create('/home/user/.w3m/cookie');
  $cookie_jar = HTTP::Cookies::Guess->create(file => '/home/user/.w3m/cookie');
  $cookie_jar = HTTP::Cookies::Guess->create(file => '/home/user/.w3m/cookie'i, type => 'w3m');

  $cookie_jar = HTTP::Cookies::Guess->create(file => '/home/user/cookies.txt'); # mozilla

DESCRIPTION

Top

HTTP::Cookies::Guess is a factory class to create HTTP::Cookies subclass instances by detecting the proper subclass using its filename (and possibly magic, if the filename format is share amongst multiple subclasses, eventually).

HTTP::Cookies::Guess is split/rewrite module by Yappo from Plagger::Cookies module by miyagawa.

AUTHOR

Top

Tatsuhiko Miyagawa, Kazuhiro Osawa <ko@yappo.ne.jp>

LICENSE

Top

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

SEE ALSO

Top

HTTP::Cookies, Plagger::Cookies, HTTP::Cookies::Mozilla, HTTP::Cookies::Microsoft, HTTP::Cookies::Netscape HTTP::Cookies::Safari, HTTP::Cookies::w3m, HTTP::Cookies::Omniweb, HTTP::Cookies::iCab


HTTP-Cookies-Guess documentation Contained in the HTTP-Cookies-Guess distribution.

package HTTP::Cookies::Guess;
use strict;
use Carp qw(carp croak);
use UNIVERSAL::require;

our $VERSION = '0.01';

sub create {
    my $class = shift;
    my %opt = @_ > 1 ? @_ : (file => $_[0]);

    croak 'Please set option for cookie file path.' unless $opt{file};
    unless ($opt{type}) {
        my $guess = $class->auto_guess($opt{file});
        $guess->{type} ? ($opt{type} = $guess->{type}) : (%opt = %{ $guess });
    }

    my $type = delete $opt{type};
    my $impl = $type ? "HTTP::Cookies::$type" : "HTTP::Cookies";
    $impl->require or return croak "Error loading $impl: $@";
    $impl->new(%opt);
}

sub auto_guess {
    my($self, $filename) = @_;

    # autosave is off by default for foreign cookies files

    if ($filename =~ /cookies\.txt$/i) {
        return { type => 'Mozilla', file => $filename };
    } elsif ($filename =~ /index\.dat$/i) {
        return { type => 'Microsoft', file => $filename };
    } elsif ($filename =~ /Cookies\.plist$/i) {
        return { type => 'Safari', file => $filename };
    } elsif ($filename =~ m!\.w3m/cookie$!) {
        return { type => 'w3m', file => $filename };
    }

    carp ("Don't know type of $filename. Use it as LWP default");
    return { file => $filename, autosave => 1 };
}

1;

__END__