Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/HTTP/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion t/headers.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use Test qw(plan ok);

plan tests => 166;
plan tests => 175;

my($h, $h2);
sub j { join("|", @_) }
Expand Down
2 changes: 1 addition & 1 deletion t/message-parts.t
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion t/message.t
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand Down
16 changes: 15 additions & 1 deletion t/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use strict;

use Test;
plan tests => 11;
plan tests => 16;

use HTTP::Request;

Expand All @@ -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");