Hi there,

if the LWP::UserAgent has a cookie jar attached to it, it should
only use it for http/https requests -- in case we're fetching
ftp: or file: or other URLs, it should keep the cookie jar untouched,
just like a regular browser does. With LWP 5.64, if you try this:

    use LWP::UserAgent;
    use HTTP::Cookies;

    my $ua  = LWP::UserAgent->new();
    my $jar = HTTP::Cookies->new();

    $ua->cookie_jar($jar);
    my $resp = $ua->get("file://etc/passwd");

you'll get

    Can't locate object method "port" via package "URI::file" 

Fix (checks for http/s scheme before using the cookie jar in
UserAgent.pm) and a test case for t/base/cookies.t are appended below.

-- Mike
--------------------------------------------------------
Mike Schilli [EMAIL PROTECTED] http://perlmeister.com
--------------------------------------------------------

diff -Naur lwp5/lib/LWP/UserAgent.pm lwp5.new/lib/LWP/UserAgent.pm
--- lwp5/lib/LWP/UserAgent.pm   Tue Dec 11 13:11:29 2001
+++ lwp5.new/lib/LWP/UserAgent.pm       Tue Mar  5 23:49:13 2002
@@ -393,7 +393,8 @@
        $last = 0 if $last < 0;  # there is no way to actually request no content
        $request->init_header('Range' => "bytes=0-$last");
     }
-    $cookie_jar->add_cookie_header($request) if $cookie_jar;
+    $cookie_jar->add_cookie_header($request) if $cookie_jar and
+        $request->uri->scheme() =~ /^https?$/;

     return($request);
 }
diff -Naur lwp5/t/base/cookies.t lwp5.new/t/base/cookies.t
--- lwp5/t/base/cookies.t       Thu Feb  7 11:08:08 2002
+++ lwp5.new/t/base/cookies.t   Tue Mar  5 23:47:24 2002
@@ -1,6 +1,7 @@
-print "1..36\n";
+print "1..37\n";

 #use LWP::Debug '+';
+use LWP::UserAgent;
 use HTTP::Cookies;
 use HTTP::Request;
 use HTTP::Response;
@@ -598,6 +599,18 @@
                 $counter{"session_before"} == 0;
 #print $c->as_string;
 print "ok 36\n";
+
+#-------------------------------------------------------------------
+# Verify the LWP::UserAgent works with the cookie jar attached
+
+my $ua = LWP::UserAgent->new();
+$c = HTTP::Cookies->new();
+$ua->cookie_jar($c);
+my $resp = $ua->get("file://this/file/does/not/exist");
+if($resp->is_success()) {
+    print "not ";
+}
+print "ok 37\n";

 #-------------------------------------------------------------------

Reply via email to