From 7a0470146c2606c07c4efa46a76f230445615535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Fri, 23 Aug 2013 18:35:58 +0200 Subject: [PATCH 1/3] Fix plan for tests in t/headers.t. --- t/headers.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/headers.t b/t/headers.t index 2f5a503d..0d8cfcf7 100644 --- a/t/headers.t +++ b/t/headers.t @@ -3,7 +3,7 @@ use strict; use Test qw(plan ok); -plan tests => 166; +plan tests => 175; my($h, $h2); sub j { join("|", @_) } From c7664bd275fd9df9a31b2d7c9813995f0199bc06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 25 Aug 2013 08:13:51 +0200 Subject: [PATCH 2/3] Fix parse() method to get right URL. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Špaček --- lib/HTTP/Request.pm | 8 ++++++++ t/message-parts.t | 2 +- t/message.t | 2 +- t/request.t | 13 ++++++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/HTTP/Request.pm b/lib/HTTP/Request.pm index c808a57c..50b1ad92 100644 --- a/lib/HTTP/Request.pm +++ b/lib/HTTP/Request.pm @@ -33,6 +33,14 @@ sub parse my $self = $class->SUPER::parse($str); my($method, $uri, $protocol) = split(' ', $request_line); $self->method($method) if defined($method); + my $headers = $self->headers; + if (defined($headers)) { + my $host = $headers->header('Host'); + if (defined($host)) { + $uri = '' unless defined($uri); + $uri = 'http://'.$host.$uri; + } + } $self->uri($uri) if defined($uri); $self->protocol($protocol) if $protocol; $self; diff --git a/t/message-parts.t b/t/message-parts.t index c4e7311f..4b0e0c8f 100644 --- a/t/message-parts.t +++ b/t/message-parts.t @@ -71,7 +71,7 @@ EOT @parts = $m->parts; ok(@parts, 1); ok($parts[0]->method, "GET"); -ok($parts[0]->uri, "/"); +ok($parts[0]->uri, "http://example.com/"); ok($parts[0]->protocol, "HTTP/1.0"); ok($parts[0]->header("Host"), "example.com"); ok($parts[0]->content, "How is this?\n"); diff --git a/t/message.t b/t/message.t index 3182b765..ec202f18 100644 --- a/t/message.t +++ b/t/message.t @@ -212,7 +212,7 @@ ok(@parts, 1); $m2 = $parts[0]; ok(ref($m2), "HTTP::Request"); ok($m2->method, "GET"); -ok($m2->uri, "/"); +ok($m2->uri, "http://www.example.com:8008/"); ok($m2->protocol, "HTTP/1.1"); ok($m2->header("Host"), "www.example.com:8008"); ok($m2->content, ""); diff --git a/t/request.t b/t/request.t index 368df60a..85cfb1c7 100644 --- a/t/request.t +++ b/t/request.t @@ -4,7 +4,7 @@ use strict; use Test; -plan tests => 11; +plan tests => 15; use HTTP::Request; @@ -30,3 +30,14 @@ ok($r2->method, "DELETE"); ok($r2->uri, "http:"); ok($r2->protocol, "HTTP/1.1"); ok($r2->header("Accept-Encoding"), $req->header("Accept-Encoding")); + +my $raw_request = <<'END'; +GET / HTTP/1.1 +Host: example.com +END +$req = HTTP::Request->parse($raw_request); +ok($req->method, 'GET'); +ok($req->uri, 'http://example.com/'); +ok($req->protocol, 'HTTP/1.1'); +my $headers = $req->headers; +ok($headers->header('Host'), 'example.com'); From d240244c06f2a2c33c1d18467293e625dd260274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 25 Aug 2013 08:20:21 +0200 Subject: [PATCH 3/3] Fix as_string() method to conformity with previous commit. --- lib/HTTP/Request.pm | 12 +++++++++++- t/request.t | 5 ++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/HTTP/Request.pm b/lib/HTTP/Request.pm index 50b1ad92..b614b03c 100644 --- a/lib/HTTP/Request.pm +++ b/lib/HTTP/Request.pm @@ -116,7 +116,17 @@ sub as_string my $req_line = $self->method || "-"; my $uri = $self->uri; - $uri = (defined $uri) ? $uri->as_string : "-"; + my $headers = $self->headers; + if (defined $headers) { + my $host = $headers->header('Host'); + if (defined $host) { + $uri = $uri->path_query; + } + } elsif (defined $uri) { + $uri = $uri->as_string; + } else { + $uri = '-'; + } $req_line .= " $uri"; my $proto = $self->protocol; $req_line .= " $proto" if $proto; diff --git a/t/request.t b/t/request.t index 85cfb1c7..96616c81 100644 --- a/t/request.t +++ b/t/request.t @@ -4,7 +4,7 @@ use strict; use Test; -plan tests => 15; +plan tests => 16; use HTTP::Request; @@ -41,3 +41,6 @@ ok($req->uri, 'http://example.com/'); ok($req->protocol, 'HTTP/1.1'); my $headers = $req->headers; ok($headers->header('Host'), 'example.com'); + +my $r2_string = $req->as_string; +ok($r2_string, $raw_request."\n");