Line data Source code
1 : #ifndef DDEBUG
2 : #define DDEBUG 0
3 : #endif
4 : #include "ddebug.h"
5 :
6 : #include <ndk.h>
7 : #include "ngx_http_set_escape_uri.h"
8 : #include "ngx_string.h"
9 :
10 :
11 : static uintptr_t ngx_escape_uri_patched(u_char *dst, u_char *src, size_t size,
12 : ngx_uint_t type);
13 :
14 :
15 : ngx_int_t
16 0 : ngx_http_set_misc_escape_uri(ngx_http_request_t *r, ngx_str_t *res,
17 : ngx_http_variable_value_t *v)
18 : {
19 : size_t len;
20 : uintptr_t escape;
21 : u_char *src, *dst;
22 :
23 0 : if (v->len == 0) {
24 0 : res->len = 0;
25 0 : res->data = NULL;
26 0 : return NGX_OK;
27 : }
28 :
29 0 : src = v->data;
30 :
31 : dd("before escape:%.*s", v->len, v->data);
32 0 : escape = 2 * ngx_escape_uri_patched(NULL, src, v->len, NGX_ESCAPE_URI);
33 : /* len = v->len + 2 * ngx_escape_uri(NULL, src, v->len, NGX_ESCAPE_URI); */
34 0 : len = escape + v->len;
35 : dd("escaped string len:%zu", len);
36 :
37 0 : dst = ngx_palloc(r->pool, len);
38 :
39 0 : if (dst == NULL) {
40 0 : return NGX_ERROR;
41 : }
42 :
43 0 : if (escape == 0) {
44 0 : ngx_memcpy(dst, src, len);
45 : dd("escape == 0");
46 :
47 : } else {
48 0 : ngx_escape_uri_patched(dst, src, v->len, NGX_ESCAPE_URI);
49 : }
50 :
51 0 : res->data = dst;
52 0 : res->len = len;
53 :
54 : dd("after eacape:%.*s", (int) res->len, res->data);
55 :
56 0 : return NGX_OK;
57 : }
58 :
59 :
60 : static uintptr_t
61 0 : ngx_escape_uri_patched(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
62 : {
63 : ngx_uint_t n;
64 : uint32_t *escape;
65 : static u_char hex[] = "0123456789ABCDEF";
66 :
67 : /* " ", "#", "%", "?", %00-%1F, %7F-%FF */
68 :
69 : static uint32_t uri[] = {
70 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
71 :
72 : /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
73 : 0xfc00886d, /* 1111 1100 0000 0000 1000 1000 0110 1101 */
74 :
75 : /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
76 : 0x78000000, /* 0111 1000 0000 0000 0000 0000 0000 0000 */
77 :
78 : /* ~}| {zyx wvut srqp onml kjih gfed cba` */
79 : 0xa8000000, /* 1010 1000 0000 0000 0000 0000 0000 0000 */
80 :
81 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
82 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
83 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
84 : 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
85 : };
86 :
87 : /* " ", "#", "%", "+", "?", %00-%1F, %7F-%FF */
88 :
89 : static uint32_t args[] = {
90 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
91 :
92 : /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
93 : 0x80000829, /* 1000 0000 0000 0000 0000 1000 0010 1001 */
94 :
95 : /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
96 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
97 :
98 : /* ~}| {zyx wvut srqp onml kjih gfed cba` */
99 : 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
100 :
101 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
102 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
103 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
104 : 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
105 : };
106 :
107 : /* " ", "#", """, "%", "'", %00-%1F, %7F-%FF */
108 :
109 : static uint32_t html[] = {
110 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
111 :
112 : /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
113 : 0x000000ad, /* 0000 0000 0000 0000 0000 0000 1010 1101 */
114 :
115 : /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
116 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
117 :
118 : /* ~}| {zyx wvut srqp onml kjih gfed cba` */
119 : 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
120 :
121 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
122 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
123 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
124 : 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
125 : };
126 :
127 : /* " ", """, "%", "'", %00-%1F, %7F-%FF */
128 :
129 : static uint32_t refresh[] = {
130 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
131 :
132 : /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
133 : 0x00000085, /* 0000 0000 0000 0000 0000 0000 1000 0101 */
134 :
135 : /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
136 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
137 :
138 : /* ~}| {zyx wvut srqp onml kjih gfed cba` */
139 : 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
140 :
141 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
142 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
143 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
144 : 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
145 : };
146 :
147 : /* " ", "%", %00-%1F */
148 :
149 : static uint32_t memcached[] = {
150 : 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
151 :
152 : /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
153 : 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */
154 :
155 : /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
156 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
157 :
158 : /* ~}| {zyx wvut srqp onml kjih gfed cba` */
159 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
160 :
161 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
162 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
163 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
164 : 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
165 : };
166 :
167 : /* mail_auth is the same as memcached */
168 :
169 : static uint32_t *map[] =
170 : { uri, args, html, refresh, memcached, memcached };
171 :
172 :
173 0 : escape = map[type];
174 :
175 0 : if (dst == NULL) {
176 :
177 : /* find the number of the characters to be escaped */
178 :
179 0 : n = 0;
180 :
181 0 : while (size) {
182 0 : if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
183 0 : n++;
184 : }
185 0 : src++;
186 0 : size--;
187 : }
188 :
189 0 : return (uintptr_t) n;
190 : }
191 :
192 0 : while (size) {
193 0 : if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
194 0 : *dst++ = '%';
195 0 : *dst++ = hex[*src >> 4];
196 0 : *dst++ = hex[*src & 0xf];
197 0 : src++;
198 :
199 : } else {
200 0 : *dst++ = *src++;
201 : }
202 0 : size--;
203 : }
204 :
205 0 : return (uintptr_t) dst;
206 : }
207 :
208 :
|