Skip to content

Commit

Permalink
Merge pull request alibaba#324 from flygoast/master
Browse files Browse the repository at this point in the history
add 'raw_uri' built-in variable for original request URI but without arguments.
  • Loading branch information
Chuanwen Chen committed Oct 17, 2013
2 parents 65ab60d + 730e646 commit 31c4cc0
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/http/modules/lua/ngx_http_lua_subrequest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ ngx_http_lua_subrequest(ngx_http_request_t *r,
sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;
sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;

sr->raw_uri = r->raw_uri;
sr->unparsed_uri = r->unparsed_uri;
sr->method_name = ngx_http_core_get_method;
sr->http_protocol = r->http_protocol;
Expand Down
1 change: 1 addition & 0 deletions src/http/ngx_http_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,7 @@ ngx_http_subrequest(ngx_http_request_t *r,
sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;
sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;

sr->raw_uri = r->raw_uri;
sr->unparsed_uri = r->unparsed_uri;
sr->method_name = ngx_http_core_get_method;
sr->http_protocol = r->http_protocol;
Expand Down
9 changes: 9 additions & 0 deletions src/http/ngx_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,15 @@ ngx_http_process_request_line(ngx_event_t *rev)
}


if (r->args_start) {
r->raw_uri.len = r->args_start - 1 - r->uri_start;

} else {
r->raw_uri.len = r->uri_end - r->uri_start;
}
r->raw_uri.data = r->uri_start;


r->unparsed_uri.len = r->uri_end - r->uri_start;
r->unparsed_uri.data = r->uri_start;

Expand Down
1 change: 1 addition & 0 deletions src/http/ngx_http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ struct ngx_http_request_s {
ngx_uint_t http_version;

ngx_str_t request_line;
ngx_str_t raw_uri;
ngx_str_t uri;
ngx_str_t args;
ngx_str_t exten;
Expand Down
3 changes: 3 additions & 0 deletions src/http/ngx_http_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ static ngx_http_variable_t ngx_http_core_variables[] = {
{ ngx_string("request_uri"), NULL, ngx_http_variable_request,
offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },

{ ngx_string("raw_uri"), NULL, ngx_http_variable_request,
offsetof(ngx_http_request_t, raw_uri), 0, 0 },

{ ngx_string("uri"), NULL, ngx_http_variable_request,
offsetof(ngx_http_request_t, uri),
NGX_HTTP_VAR_NOCACHEABLE, 0 },
Expand Down
112 changes: 112 additions & 0 deletions tests/nginx-tests/nginx-tests/http_raw_uri_variable.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/perl

# (C) Maxim Dounin
# (C) flygoast

# Tests for 'raw_uri' built-in variable.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http rewrite addition/)->plan(18)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
location / {
add_header X-RAW-URI $raw_uri;
return 200;
}
location /sr {
types { }
default_type text/html;
add_before_body /addition;
return 200;
}
location /addition {
return 200 "X-RAW-URI: $raw_uri\r\n";
}
location /lua {
default_type 'text/html';
content_by_lua '
local res = ngx.location.capture("/addition");
ngx.print(res.body);
';
}
}
}
EOF

$t->run();

###############################################################################

raw_uri('/rawuri', '/rawuri', 'uri');
raw_uri('/rawuri%3F', '/rawuri%3F', 'escaped uri');
raw_uri('/rawuri?arg=foo', '/rawuri', 'uri with arguments');
raw_uri('/rawuri?arg=%2F', '/rawuri', 'uri with escaped arguments');
raw_uri('/rawuri%3F?arg=foo', '/rawuri%3F', 'escaped uri with arguments');
raw_uri('/rawuri%3F?arg=%2F', '/rawuri%3F',
'escaped uri with escaped arguments');
raw_uri('/sr', '/sr', 'uri in subrequest');
raw_uri('/sr%3F', '/sr%3F', 'escaped uri in subrequest');
raw_uri('/sr?arg=foo', '/sr', 'uri with arguments in subrequest');
raw_uri('/sr?arg=%2F', '/sr', 'uri with escaped arguments in subrequest');
raw_uri('/sr%3F?arg=foo', '/sr%3F',
'escaped uri with arguments in subrequest');
raw_uri('/sr%3F?arg=%2F', '/sr%3F',
'escaped uri with escaped arguments in subrequest');
raw_uri('/lua', '/lua', 'uri in lua subrequest');
raw_uri('/lua%3F', '/lua%3F', 'escaped uri in lua subrequest');
raw_uri('/lua?arg=foo', '/lua', 'uri with arguments in lua subrequest');
raw_uri('/lua?arg=%2F', '/lua', 'uri with escaped arguments in lua subrequest');
raw_uri('/lua%3F?arg=foo', '/lua%3F',
'escaped uri with arguments in lua subrequest');
raw_uri('/lua%3F?arg=%2F', '/lua%3F',
'escaped uri with escaped arguments in lua subrequest');

###############################################################################

sub raw_uri {
my ($url, $value, $name) = @_;
my $data = http_get($url);
if ($data !~ qr!^X-RAW-URI: (.*?)\x0d?$!ms) {
fail($name);
return;
}
my $raw_uri = $1;
is($raw_uri, $value, $name);
}

###############################################################################

0 comments on commit 31c4cc0

Please sign in to comment.