-
Notifications
You must be signed in to change notification settings - Fork 5
/
ngx_http_variable_urlencode_and_urldecode.patch
166 lines (161 loc) · 4.41 KB
/
ngx_http_variable_urlencode_and_urldecode.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Index: nginx-catap/nginx/src/http/ngx_http_variables.c
===================================================================
--- nginx-catap.orig/nginx/src/http/ngx_http_variables.c
+++ nginx-catap/nginx/src/http/ngx_http_variables.c
@@ -36,6 +36,10 @@ static ngx_int_t ngx_http_variable_cooki
static ngx_int_t ngx_http_variable_ssi(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
#endif
+static ngx_int_t ngx_http_variable_urlencode(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_urldecode(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_crc32(ngx_http_request_t *r,
@@ -568,6 +572,24 @@ ngx_http_get_variable(ngx_http_request_t
}
#endif
+ if (ngx_strncmp(name->data, "urlencode_", 10) == 0) {
+
+ if (ngx_http_variable_urlencode(r, vv, (uintptr_t) name) == NGX_OK) {
+ return vv;
+ }
+
+ return NULL;
+ }
+
+ if (ngx_strncmp(name->data, "urldecode_", 10) == 0) {
+
+ if (ngx_http_variable_urldecode(r, vv, (uintptr_t) name) == NGX_OK) {
+ return vv;
+ }
+
+ return NULL;
+ }
+
if (ngx_strncmp(name->data, "arg_", 4) == 0) {
if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) {
@@ -938,6 +960,104 @@ ngx_http_variable_ssi(ngx_http_request_t
static ngx_int_t
+ngx_http_variable_urlencode(ngx_http_request_t *r, ngx_http_variable_value_t *v,
+ uintptr_t data)
+{
+ ngx_str_t *name = (ngx_str_t *) data;
+
+ ngx_str_t sub_name;
+
+ ngx_uint_t key;
+
+ ngx_http_variable_value_t *vv;
+
+ sub_name.len = name->len - (sizeof("urlencode_") - 1);
+ sub_name.data = name->data + sizeof("urlencode_") - 1;
+
+ key = ngx_hash_strlow(sub_name.data, sub_name.data, sub_name.len);
+
+ vv = ngx_http_get_variable(r, &sub_name, key);
+
+ if (vv == NULL || !vv->valid) {
+ return NGX_ERROR;
+ }
+
+ if (vv->not_found) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ v->len = vv->len +
+ 2 * ngx_escape_uri(NULL, vv->data, vv->len, NGX_ESCAPE_ARGS);
+
+ v->data = ngx_palloc(r->pool, v->len);
+ if (v->data == NULL) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ (void) ngx_escape_uri(v->data, vv->data, vv->len, NGX_ESCAPE_ARGS);
+
+ v->valid = 1;
+ v->not_found = 0;
+ v->no_cacheable = vv->no_cacheable;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_variable_urldecode(ngx_http_request_t *r, ngx_http_variable_value_t *v,
+ uintptr_t data)
+{
+ ngx_str_t *name = (ngx_str_t *) data;
+
+ u_char *dst, *src;
+ ngx_str_t sub_name;
+ ngx_uint_t key;
+
+ ngx_http_variable_value_t *vv;
+
+ sub_name.len = name->len - (sizeof("urldecode_") - 1);
+ sub_name.data = name->data + sizeof("urldecode_") - 1;
+
+ key = ngx_hash_strlow(sub_name.data, sub_name.data, sub_name.len);
+
+ vv = ngx_http_get_variable(r, &sub_name, key);
+
+ if (vv == NULL || !vv->valid) {
+ return NGX_ERROR;
+ }
+
+ if (vv->not_found) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ v->len = vv->len;
+
+ v->data = ngx_palloc(r->pool, v->len);
+ if (v->data == NULL) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+
+ dst = v->data;
+ src = vv->data;
+
+ ngx_unescape_uri(&dst, &src, vv->len, NGX_ESCAPE_ARGS);
+
+ v->len = dst - v->data;
+
+ v->valid = 1;
+ v->not_found = 0;
+ v->no_cacheable = vv->no_cacheable;
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
ngx_http_variable_argument(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
@@ -2365,6 +2485,20 @@ ngx_http_variables_init_vars(ngx_conf_t
}
#endif
+ if (ngx_strncmp(v[i].name.data, "urlencode_", 10) == 0) {
+ v[i].get_handler = ngx_http_variable_urlencode;
+ v[i].data = (uintptr_t) &v[i].name;
+
+ continue;
+ }
+
+ if (ngx_strncmp(v[i].name.data, "urldecode_", 10) == 0) {
+ v[i].get_handler = ngx_http_variable_urldecode;
+ v[i].data = (uintptr_t) &v[i].name;
+
+ continue;
+ }
+
if (ngx_strncmp(v[i].name.data, "arg_", 4) == 0) {
v[i].get_handler = ngx_http_variable_argument;
v[i].data = (uintptr_t) &v[i].name;