diff --git a/lib/HTTP/Request.pm b/lib/HTTP/Request.pm index c808a57c..b614b03c 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; @@ -108,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/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("|", @_) } 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..96616c81 100644 --- a/t/request.t +++ b/t/request.t @@ -4,7 +4,7 @@ use strict; use Test; -plan tests => 11; +plan tests => 16; use HTTP::Request; @@ -30,3 +30,17 @@ 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'); + +my $r2_string = $req->as_string; +ok($r2_string, $raw_request."\n");