Commit 7e7b95b5 authored by David Golden's avatar David Golden

tighten code and s/Cookies/Cookie/ for the header

parent 148a686b
......@@ -387,17 +387,14 @@ sub _request {
$handle->connect($scheme, $host, $port);
}
$self->_prepare_headers_and_cb($request, $args);
$self->_prepare_headers_and_cb($request, $args, $url);
$handle->write_request($request);
my $response;
do { $response = $handle->read_response_header }
until (substr($response->{status},0,1) ne '1');
### Update the cookie jar if there is one
if( defined $self->cookie_jar() ) {
$self->_update_cookie_jar( $url, $response );
}
$self->_update_cookie_jar( $url, $response ) if $self->{cookie_jar};
if ( my @redir_args = $self->_maybe_redirect($request, $response, $args) ) {
$handle->close;
......@@ -419,7 +416,7 @@ sub _request {
}
sub _prepare_headers_and_cb {
my ($self, $request, $args) = @_;
my ($self, $request, $args, $url) = @_;
for ($self->{default_headers}, $args->{headers}) {
next unless defined;
......@@ -454,13 +451,10 @@ sub _prepare_headers_and_cb {
if ref $args->{trailer_callback} eq 'CODE';
}
### If we have a cookie jar
if (defined $self->cookie_jar()) {
### Then create cookie headers
my %hdrs = $self->_prepare_cookie_headers( $request );
### Add the cookie headers (if any) to the request)
@{$request->{headers}}{keys %hdrs} = values %hdrs;
### If we have a cookie jar, then maybe add relevant cookies
if ( $self->{cookie_jar} ) {
my $cookies = $self->cookie_jar->cookie_header( $url );
$request->{headers}{cookie} = $cookies if length $cookies;
}
return;
......@@ -486,34 +480,14 @@ sub _prepare_data_cb {
return $data_cb;
}
sub _prepare_cookie_headers {
my ($self, $request) = @_;
### Get any cookies for this URL
my %headers = ();
my $url = sprintf('%s://%s%s', @{$request}{qw (scheme host_port uri)});
my $cookies = $self->cookie_jar->cookie_header( $url );
if( defined $cookies && $cookies ne '' ) {
### Create the cookies header
$headers{'cookies'} = $cookies;
}
return %headers;
}
sub _update_cookie_jar {
my ($self, $url, $response) = @_;
### If there are cookies
return unless exists $response->{headers}->{'set-cookie'};
my $cookies = $response->{headers}->{'set-cookie'};
return unless defined $cookies;
my @cookies = ref $cookies ? @$cookies : $cookies;
### Then add them to the cookie jar
$self->cookie_jar->add( $url, $_ ) for @cookies;
return;
......
......@@ -30,8 +30,8 @@ for my $file ( dir_list("t/cases", qr/^cookies/ ) ) {
my %headers = hashify( $case->{headers} );
my %new_args = hashify( $case->{new_args} );
if( exists $headers{Cookies} ) {
my $cookies = delete $headers{Cookies};
if( exists $headers{Cookie} ) {
my $cookies = delete $headers{Cookie};
$jar->add( $url, $cookies );
}
......
......@@ -22,7 +22,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue
Cookie: cname=cvalue
----------
HTTP/1.1 200 OK
......
......@@ -22,7 +22,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue01
Cookie: cname=cvalue01
----------
HTTP/1.1 200 OK
......@@ -40,7 +40,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue02
Cookie: cname=cvalue02
----------
HTTP/1.1 200 OK
......
......@@ -22,7 +22,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue01
Cookie: cname=cvalue01
----------
HTTP/1.1 200 OK
......@@ -57,7 +57,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue02
Cookie: cname=cvalue02
----------
HTTP/1.1 200 OK
......@@ -75,7 +75,7 @@ GET /index02.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue03
Cookie: cname=cvalue03
----------
HTTP/1.1 200 OK
......
url
http://example.com/index.html
headers
Cookies: cname=cvalue05; domain=example.com; path=/
Cookie: cname=cvalue05; domain=example.com; path=/
----------
GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue05
Cookie: cname=cvalue05
----------
HTTP/1.1 200 OK
......@@ -25,7 +25,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname=cvalue06
Cookie: cname=cvalue06
----------
HTTP/1.1 200 OK
......
......@@ -22,7 +22,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname1=cvalue01
Cookie: cname1=cvalue01
----------
HTTP/1.1 200 OK
......@@ -40,7 +40,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname1=cvalue01; cname2=cvalue02
Cookie: cname1=cvalue01; cname2=cvalue02
----------
HTTP/1.1 200 OK
......
......@@ -23,7 +23,7 @@ GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
Cookies: cname1=cvalue01; cname2=cvalue02
Cookie: cname1=cvalue01; cname2=cvalue02
----------
HTTP/1.1 200 OK
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment