/src/httpd/srclib/apr/uri/apr_uri.c
Line | Count | Source |
1 | | /* Licensed to the Apache Software Foundation (ASF) under one or more |
2 | | * contributor license agreements. See the NOTICE file distributed with |
3 | | * this work for additional information regarding copyright ownership. |
4 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
5 | | * (the "License"); you may not use this file except in compliance with |
6 | | * the License. You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | /* |
18 | | * apr_uri.c: URI related utility things |
19 | | * |
20 | | */ |
21 | | |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include "apu.h" |
25 | | #include "apr.h" |
26 | | #include "apr_general.h" |
27 | | #include "apr_strings.h" |
28 | | #include "apr_cstr.h" |
29 | | |
30 | | #define APR_WANT_STRFUNC |
31 | | #include "apr_want.h" |
32 | | |
33 | | #include "apr_uri.h" |
34 | | |
35 | 544 | #define LINK_LOCAL(ipv6addr) ((strlen(ipv6addr) >= 5) && \ |
36 | 544 | ((ipv6addr)[4] == ':') && \ |
37 | 544 | !strncasecmp(ipv6addr, "fe", 2) && \ |
38 | 544 | strchr("89aAbB", (ipv6addr)[2]) && \ |
39 | 544 | strchr("0123456789aAbBcCdDeEfF", (ipv6addr)[3])) |
40 | | |
41 | | /* |
42 | | * *only* for IPv6 addresses with a zone identifier according to RFC6874 |
43 | | */ |
44 | | static apr_status_t detect_scope_zone_id(int *have_zone_id, char const *ipv6addr, |
45 | | size_t len) |
46 | 702 | { |
47 | 702 | const char *s; |
48 | | |
49 | 702 | *have_zone_id = 0; |
50 | | |
51 | 702 | if (len < 3 + 5) { |
52 | | /* |
53 | | * We neeed *at least* the three characters for a percent-encoded |
54 | | * percent sign. Furthermore scope id's are only allowed for link-local |
55 | | * addresses under prefix fe80::/10. |
56 | | */ |
57 | 262 | return APR_SUCCESS; |
58 | 262 | } |
59 | | |
60 | 440 | if (!LINK_LOCAL(ipv6addr)) { |
61 | | /* |
62 | | * Scope id's are only allowed for link-local addresses under prefix |
63 | | * fe80::/10. |
64 | | */ |
65 | 353 | return APR_SUCCESS; |
66 | 353 | } |
67 | | |
68 | 87 | s = memchr(ipv6addr, '%', len); |
69 | 87 | if (s != NULL && s < ipv6addr + len - 2) { |
70 | | /* RFC3986 is pretty specific about how to percent encode, but |
71 | | * decoding is to be performed per component, which is what we |
72 | | * already have here. On the other hand, RFC6874 is clear that |
73 | | * the delimiter for a zone identifier must be a percent encoded |
74 | | * percent, i.e. "%25". Any other percent-encoded character is |
75 | | * invalid here. |
76 | | */ |
77 | 70 | if (s[1] != '2' || s[2] != '5') { |
78 | 62 | return APR_EINVAL; |
79 | 62 | } |
80 | 8 | *have_zone_id = 1; |
81 | 8 | } |
82 | 25 | return APR_SUCCESS; |
83 | 87 | } |
84 | | |
85 | | static void percent_decode_scope_zone_id(char *hostname) |
86 | 8 | { |
87 | | /* RFC6874 is a little hand-wavy in terms of what to decode. Technically, |
88 | | * all percent-encoded characters should be decoded, but also, the RFC states |
89 | | * that they SHOULD not occur, basically. |
90 | | * |
91 | | * So let's assume they don't, to keep things simple. Because otherwise we'd |
92 | | * have to deal with full RFC3986 rules and perform UTF-8 decoding as well |
93 | | * and all that. |
94 | | */ |
95 | 8 | size_t len = strlen(hostname); |
96 | | |
97 | | /* We know from the caller already that this *is* a percent encoded |
98 | | * percent sign, so we just want to skip it. Trust the caller here. |
99 | | */ |
100 | 8 | char *s = memchr(hostname, '%', len); |
101 | 8 | size_t offset = s - hostname; |
102 | 8 | memmove(hostname + offset + 1, hostname + offset + 3, len - offset - 2); |
103 | 8 | } |
104 | | |
105 | | static char *percent_encode_scope_zone_id(apr_pool_t *p, apr_uri_t const *uptr) |
106 | 125 | { |
107 | | /* Inverse to the logic in the decode function, we need to encode the first |
108 | | * percent sign we encounter (if any). |
109 | | */ |
110 | 125 | size_t len = strlen(uptr->hostname); |
111 | 125 | char * s = memchr(uptr->hostname, '%', len); |
112 | 125 | size_t offset; |
113 | 125 | char *hostcopy; |
114 | | |
115 | 125 | if ((s == NULL) || !LINK_LOCAL(uptr->hostname)) { |
116 | | /* |
117 | | * Scope id's are only allowed for link-local addresses under prefix |
118 | | * fe80::/10. |
119 | | */ |
120 | 98 | return uptr->hostname; |
121 | 98 | } |
122 | | |
123 | 27 | offset = s - uptr->hostname; |
124 | | |
125 | 27 | hostcopy = apr_palloc(p, len + 3); |
126 | 27 | memcpy(hostcopy, uptr->hostname, offset + 1); |
127 | 27 | hostcopy[offset + 1] = '2'; |
128 | 27 | hostcopy[offset + 2] = '5'; |
129 | 27 | memcpy(hostcopy + offset + 3, uptr->hostname + offset + 1, |
130 | 27 | len - offset - 1); |
131 | 27 | hostcopy[len + 2] = '\0'; |
132 | | |
133 | 27 | return hostcopy; |
134 | 125 | } |
135 | | |
136 | | APR_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str) |
137 | 729 | { |
138 | 729 | if (!scheme_str) { |
139 | 8 | return 0; |
140 | 8 | } |
141 | | |
142 | | /* lowercase ASCII/EBCDIC (to be compared to a lowercase letter only) */ |
143 | 721 | #if !APR_CHARSET_EBCDIC |
144 | 801 | #define AS_LOWER(c) ((unsigned char)(c) | 0x20) |
145 | | #else |
146 | | #define AS_LOWER(c) ((unsigned char)(c) & ~0x40) |
147 | | #endif |
148 | | |
149 | | /* whether the rest matches (from offset 1) */ |
150 | 721 | #define REST_EQ(x, y) \ |
151 | 884 | (apr_cstr_casecmp(&(x)[1], &(y)[1]) == 0) |
152 | | |
153 | | /* whether the rest matches up to n (from offset 1) */ |
154 | 721 | #define REST_EQn(x, y, n) \ |
155 | 721 | (apr_cstr_casecmpn(&(x)[1], &(y)[1], (n) - 1) == 0) |
156 | | |
157 | | /* WWW schemes and their default ports (basically /etc/services), |
158 | | * matched case-insensitive (no locale) |
159 | | */ |
160 | 721 | switch (AS_LOWER(scheme_str[0])) { |
161 | 39 | case 'a': |
162 | 39 | if (REST_EQ(scheme_str, "acap")) |
163 | 5 | return APR_URI_ACAP_DEFAULT_PORT; |
164 | 34 | break; |
165 | | |
166 | 39 | case 'f': |
167 | 39 | if (REST_EQ(scheme_str, "ftp")) |
168 | 8 | return APR_URI_FTP_DEFAULT_PORT; |
169 | 31 | break; |
170 | | |
171 | 38 | case 'g': |
172 | 38 | if (REST_EQ(scheme_str, "gopher")) |
173 | 5 | return APR_URI_GOPHER_DEFAULT_PORT; |
174 | 33 | break; |
175 | | |
176 | 78 | case 'h': |
177 | 78 | if (REST_EQn(scheme_str, "http", 4)) { |
178 | 47 | if (scheme_str[4] == '\0') |
179 | 7 | return APR_URI_HTTP_DEFAULT_PORT; |
180 | | |
181 | 40 | if (AS_LOWER(scheme_str[4]) == 's' && scheme_str[5] == '\0') |
182 | 5 | return APR_URI_HTTPS_DEFAULT_PORT; |
183 | 40 | } |
184 | 66 | break; |
185 | | |
186 | 66 | case 'i': |
187 | 36 | if (REST_EQ(scheme_str, "imap")) |
188 | 4 | return APR_URI_IMAP_DEFAULT_PORT; |
189 | 32 | break; |
190 | | |
191 | 37 | case 'l': |
192 | 37 | if (REST_EQ(scheme_str, "ldap")) |
193 | 6 | return APR_URI_LDAP_DEFAULT_PORT; |
194 | 31 | break; |
195 | | |
196 | 52 | case 'n': |
197 | 52 | if (REST_EQ(scheme_str, "nntp")) |
198 | 4 | return APR_URI_NNTP_DEFAULT_PORT; |
199 | | |
200 | 48 | if (REST_EQ(scheme_str, "nfs")) |
201 | 4 | return APR_URI_NFS_DEFAULT_PORT; |
202 | 44 | break; |
203 | | |
204 | 81 | case 'p': |
205 | 81 | if (REST_EQ(scheme_str, "pop")) |
206 | 6 | return APR_URI_POP_DEFAULT_PORT; |
207 | | |
208 | 75 | if (REST_EQ(scheme_str, "prospero")) |
209 | 5 | return APR_URI_PROSPERO_DEFAULT_PORT; |
210 | 70 | break; |
211 | | |
212 | 70 | case 'r': |
213 | 47 | if (REST_EQ(scheme_str, "rtsp")) |
214 | 7 | return APR_URI_RTSP_DEFAULT_PORT; |
215 | 40 | break; |
216 | | |
217 | 82 | case 's': |
218 | 82 | if (REST_EQ(scheme_str, "snews")) |
219 | 4 | return APR_URI_SNEWS_DEFAULT_PORT; |
220 | | |
221 | 78 | if (REST_EQ(scheme_str, "sip")) |
222 | 5 | return APR_URI_SIP_DEFAULT_PORT; |
223 | | |
224 | 73 | if (REST_EQ(scheme_str, "ssh")) |
225 | 5 | return APR_URI_SSH_DEFAULT_PORT; |
226 | 68 | break; |
227 | | |
228 | 68 | case 't': |
229 | 64 | if (REST_EQ(scheme_str, "tip")) |
230 | 8 | return APR_URI_TIP_DEFAULT_PORT; |
231 | | |
232 | 56 | if (REST_EQ(scheme_str, "telnet")) |
233 | 4 | return APR_URI_TELNET_DEFAULT_PORT; |
234 | 52 | break; |
235 | | |
236 | 52 | case 'w': |
237 | 39 | if (REST_EQ(scheme_str, "wais")) |
238 | 6 | return APR_URI_WAIS_DEFAULT_PORT; |
239 | 33 | break; |
240 | | |
241 | 78 | case 'z': |
242 | 78 | if (REST_EQn(scheme_str, "z39.50", 6)) { |
243 | 40 | unsigned char c = AS_LOWER(scheme_str[6]); |
244 | 40 | if ((c == 's' || c == 'r') && scheme_str[7] == '\0') |
245 | 7 | return APR_URI_WAIS_DEFAULT_PORT; |
246 | 40 | } |
247 | 71 | break; |
248 | 721 | } |
249 | | |
250 | 616 | #undef REST_EQ |
251 | 616 | #undef REST_EQn |
252 | 616 | #undef AS_LOWER |
253 | | |
254 | 616 | return 0; |
255 | 721 | } |
256 | | |
257 | | /* Unparse a apr_uri_t structure to an URI string. |
258 | | * Optionally suppress the password for security reasons. |
259 | | */ |
260 | | APR_DECLARE(char *) apr_uri_unparse(apr_pool_t *p, |
261 | | const apr_uri_t *uptr, |
262 | | unsigned flags) |
263 | 385 | { |
264 | 385 | char *ret = ""; |
265 | | |
266 | | /* If suppressing the site part, omit both user name & scheme://hostname */ |
267 | 385 | if (!(flags & APR_URI_UNP_OMITSITEPART)) { |
268 | | |
269 | | /* Construct a "user:password@" string, honoring the passed |
270 | | * APR_URI_UNP_ flags: */ |
271 | 385 | if (uptr->user || uptr->password) { |
272 | 90 | ret = apr_pstrcat(p, |
273 | 90 | (uptr->user && !(flags & APR_URI_UNP_OMITUSER)) |
274 | 90 | ? uptr->user : "", |
275 | 90 | (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD)) |
276 | 90 | ? ":" : "", |
277 | 90 | (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD)) |
278 | 90 | ? ((flags & APR_URI_UNP_REVEALPASSWORD) |
279 | 36 | ? uptr->password : "XXXXXXXX") |
280 | 90 | : "", |
281 | 90 | ((uptr->user && !(flags & APR_URI_UNP_OMITUSER)) || |
282 | 0 | (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))) |
283 | 90 | ? "@" : "", |
284 | 90 | NULL); |
285 | 90 | } |
286 | | |
287 | | /* Construct scheme://site string */ |
288 | 385 | if (uptr->hostname) { |
289 | 337 | int is_default_port; |
290 | 337 | const char *lbrk = "", *rbrk = ""; |
291 | 337 | char *host = uptr->hostname; |
292 | | |
293 | 337 | if (strchr(host, ':')) { /* v6 literal */ |
294 | 125 | lbrk = "["; |
295 | 125 | rbrk = "]"; |
296 | | |
297 | 125 | host = percent_encode_scope_zone_id(p, uptr); |
298 | 125 | } |
299 | | |
300 | 337 | is_default_port = |
301 | 337 | (uptr->port_str == NULL || |
302 | 105 | uptr->port == 0 || |
303 | 63 | uptr->port == apr_uri_port_of_scheme(uptr->scheme)); |
304 | | |
305 | 337 | ret = apr_pstrcat(p, "//", ret, lbrk, host, rbrk, |
306 | 337 | is_default_port ? "" : ":", |
307 | 337 | is_default_port ? "" : uptr->port_str, |
308 | 337 | NULL); |
309 | 337 | } |
310 | 385 | if (uptr->scheme) { |
311 | 345 | ret = apr_pstrcat(p, uptr->scheme, ":", ret, NULL); |
312 | 345 | } |
313 | 385 | } |
314 | | |
315 | | /* Should we suppress all path info? */ |
316 | 385 | if (!(flags & APR_URI_UNP_OMITPATHINFO)) { |
317 | | /* Append path, query and fragment strings: */ |
318 | 385 | ret = apr_pstrcat(p, |
319 | 385 | ret, |
320 | 385 | (uptr->path) |
321 | 385 | ? uptr->path : "", |
322 | 385 | (uptr->query && !(flags & APR_URI_UNP_OMITQUERY)) |
323 | 385 | ? "?" : "", |
324 | 385 | (uptr->query && !(flags & APR_URI_UNP_OMITQUERY)) |
325 | 385 | ? uptr->query : "", |
326 | 385 | (uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY)) |
327 | 385 | ? "#" : NULL, |
328 | 385 | (uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY)) |
329 | 385 | ? uptr->fragment : NULL, |
330 | 385 | NULL); |
331 | 385 | } |
332 | 385 | return ret; |
333 | 385 | } |
334 | | |
335 | | /* Here is the hand-optimized apr_uri_parse(). There are some wild |
336 | | * tricks we could pull in assembly language that we don't pull here... like we |
337 | | * can do word-at-time scans for delimiter characters using the same technique |
338 | | * that fast memchr()s use. But that would be way non-portable. -djg |
339 | | */ |
340 | | |
341 | | /* We have a apr_table_t that we can index by character and it tells us if the |
342 | | * character is one of the interesting delimiters. Note that we even get |
343 | | * compares for NUL for free -- it's just another delimiter. |
344 | | */ |
345 | | |
346 | 15.6k | #define T_SLASH 0x01 /* '/' */ |
347 | 22.8k | #define T_QUESTION 0x02 /* '?' */ |
348 | 22.8k | #define T_HASH 0x04 /* '#' */ |
349 | 7.80k | #define T_ALPHA 0x08 /* 'A' ... 'Z', 'a' ... 'z' */ |
350 | 6.91k | #define T_SCHEME 0x10 /* '0' ... '9', '-', '+', '.' |
351 | | * (allowed in scheme except first char) |
352 | | */ |
353 | 22.8k | #define T_NUL 0x80 /* '\0' */ |
354 | | |
355 | | #if APR_CHARSET_EBCDIC |
356 | | /* Delimiter table for the EBCDIC character set */ |
357 | | static const unsigned char uri_delims[256] = { |
358 | | T_NUL, /* 0x00 */ |
359 | | 0, /* 0x01 */ |
360 | | 0, /* 0x02 */ |
361 | | 0, /* 0x03 */ |
362 | | 0, /* 0x04 */ |
363 | | 0, /* 0x05 */ |
364 | | 0, /* 0x06 */ |
365 | | 0, /* 0x07 */ |
366 | | 0, /* 0x08 */ |
367 | | 0, /* 0x09 */ |
368 | | 0, /* 0x0a */ |
369 | | 0, /* 0x0b */ |
370 | | 0, /* 0x0c */ |
371 | | 0, /* 0x0d */ |
372 | | 0, /* 0x0e */ |
373 | | 0, /* 0x0f */ |
374 | | 0, /* 0x10 */ |
375 | | 0, /* 0x11 */ |
376 | | 0, /* 0x12 */ |
377 | | 0, /* 0x13 */ |
378 | | 0, /* 0x14 */ |
379 | | 0, /* 0x15 */ |
380 | | 0, /* 0x16 */ |
381 | | 0, /* 0x17 */ |
382 | | 0, /* 0x18 */ |
383 | | 0, /* 0x19 */ |
384 | | 0, /* 0x1a */ |
385 | | 0, /* 0x1b */ |
386 | | 0, /* 0x1c */ |
387 | | 0, /* 0x1d */ |
388 | | 0, /* 0x1e */ |
389 | | 0, /* 0x1f */ |
390 | | 0, /* 0x20 */ |
391 | | 0, /* 0x21 */ |
392 | | 0, /* 0x22 */ |
393 | | 0, /* 0x23 */ |
394 | | 0, /* 0x24 */ |
395 | | 0, /* 0x25 */ |
396 | | 0, /* 0x26 */ |
397 | | 0, /* 0x27 */ |
398 | | 0, /* 0x28 */ |
399 | | 0, /* 0x29 */ |
400 | | 0, /* 0x2a */ |
401 | | 0, /* 0x2b */ |
402 | | 0, /* 0x2c */ |
403 | | 0, /* 0x2d */ |
404 | | 0, /* 0x2e */ |
405 | | 0, /* 0x2f */ |
406 | | 0, /* 0x30 */ |
407 | | 0, /* 0x31 */ |
408 | | 0, /* 0x32 */ |
409 | | 0, /* 0x33 */ |
410 | | 0, /* 0x34 */ |
411 | | 0, /* 0x35 */ |
412 | | 0, /* 0x36 */ |
413 | | 0, /* 0x37 */ |
414 | | 0, /* 0x38 */ |
415 | | 0, /* 0x39 */ |
416 | | 0, /* 0x3a */ |
417 | | 0, /* 0x3b */ |
418 | | 0, /* 0x3c */ |
419 | | 0, /* 0x3d */ |
420 | | 0, /* 0x3e */ |
421 | | 0, /* 0x3f */ |
422 | | 0, /* 0x40 ' ' */ |
423 | | 0, /* 0x41 */ |
424 | | 0, /* 0x42 */ |
425 | | 0, /* 0x43 */ |
426 | | 0, /* 0x44 */ |
427 | | 0, /* 0x45 */ |
428 | | 0, /* 0x46 */ |
429 | | 0, /* 0x47 */ |
430 | | 0, /* 0x48 */ |
431 | | 0, /* 0x49 */ |
432 | | 0, /* 0x4a '[' */ |
433 | | T_SCHEME, /* 0x4b '.' */ |
434 | | 0, /* 0x4c '<' */ |
435 | | 0, /* 0x4d '(' */ |
436 | | T_SCHEME, /* 0x4e '+' */ |
437 | | 0, /* 0x4f '!' */ |
438 | | 0, /* 0x50 '&' */ |
439 | | 0, /* 0x51 */ |
440 | | 0, /* 0x52 */ |
441 | | 0, /* 0x53 */ |
442 | | 0, /* 0x54 */ |
443 | | 0, /* 0x55 */ |
444 | | 0, /* 0x56 */ |
445 | | 0, /* 0x57 */ |
446 | | 0, /* 0x58 */ |
447 | | 0, /* 0x59 */ |
448 | | 0, /* 0x5a ']' */ |
449 | | 0, /* 0x5b '$' */ |
450 | | 0, /* 0x5c '*' */ |
451 | | 0, /* 0x5d ')' */ |
452 | | 0, /* 0x5e ';' */ |
453 | | 0, /* 0x5f '^' */ |
454 | | T_SCHEME, /* 0x60 '-' */ |
455 | | T_SLASH, /* 0x61 '/' */ |
456 | | 0, /* 0x62 */ |
457 | | 0, /* 0x63 */ |
458 | | 0, /* 0x64 */ |
459 | | 0, /* 0x65 */ |
460 | | 0, /* 0x66 */ |
461 | | 0, /* 0x67 */ |
462 | | 0, /* 0x68 */ |
463 | | 0, /* 0x69 */ |
464 | | 0, /* 0x6a '|' */ |
465 | | 0, /* 0x6b ',' */ |
466 | | 0, /* 0x6c '%' */ |
467 | | 0, /* 0x6d '_' */ |
468 | | 0, /* 0x6e '>' */ |
469 | | T_QUESTION, /* 0x6f '?' */ |
470 | | 0, /* 0x70 */ |
471 | | 0, /* 0x71 */ |
472 | | 0, /* 0x72 */ |
473 | | 0, /* 0x73 */ |
474 | | 0, /* 0x74 */ |
475 | | 0, /* 0x75 */ |
476 | | 0, /* 0x76 */ |
477 | | 0, /* 0x77 */ |
478 | | 0, /* 0x78 */ |
479 | | 0, /* 0x79 '`' */ |
480 | | 0, /* 0x7a ':' */ |
481 | | T_HASH, /* 0x7b '#' */ |
482 | | 0, /* 0x7c '@' */ |
483 | | 0, /* 0x7d ''' */ |
484 | | 0, /* 0x7e '=' */ |
485 | | 0, /* 0x7f '"' */ |
486 | | 0, /* 0x80 */ |
487 | | T_ALPHA, /* 0x81 'a' */ |
488 | | T_ALPHA, /* 0x82 'b' */ |
489 | | T_ALPHA, /* 0x83 'c' */ |
490 | | T_ALPHA, /* 0x84 'd' */ |
491 | | T_ALPHA, /* 0x85 'e' */ |
492 | | T_ALPHA, /* 0x86 'f' */ |
493 | | T_ALPHA, /* 0x87 'g' */ |
494 | | T_ALPHA, /* 0x88 'h' */ |
495 | | T_ALPHA, /* 0x89 'i' */ |
496 | | 0, /* 0x8a */ |
497 | | 0, /* 0x8b */ |
498 | | 0, /* 0x8c */ |
499 | | 0, /* 0x8d */ |
500 | | 0, /* 0x8e */ |
501 | | 0, /* 0x8f */ |
502 | | 0, /* 0x90 */ |
503 | | T_ALPHA, /* 0x91 'j' */ |
504 | | T_ALPHA, /* 0x92 'k' */ |
505 | | T_ALPHA, /* 0x93 'l' */ |
506 | | T_ALPHA, /* 0x94 'm' */ |
507 | | T_ALPHA, /* 0x95 'n' */ |
508 | | T_ALPHA, /* 0x96 'o' */ |
509 | | T_ALPHA, /* 0x97 'p' */ |
510 | | T_ALPHA, /* 0x98 'q' */ |
511 | | T_ALPHA, /* 0x99 'r' */ |
512 | | 0, /* 0x9a */ |
513 | | 0, /* 0x9b */ |
514 | | 0, /* 0x9c */ |
515 | | 0, /* 0x9d */ |
516 | | 0, /* 0x9e */ |
517 | | 0, /* 0x9f */ |
518 | | 0, /* 0xa0 */ |
519 | | 0, /* 0xa1 '~' */ |
520 | | T_ALPHA, /* 0xa2 's' */ |
521 | | T_ALPHA, /* 0xa3 't' */ |
522 | | T_ALPHA, /* 0xa4 'u' */ |
523 | | T_ALPHA, /* 0xa5 'v' */ |
524 | | T_ALPHA, /* 0xa6 'w' */ |
525 | | T_ALPHA, /* 0xa7 'x' */ |
526 | | T_ALPHA, /* 0xa8 'y' */ |
527 | | T_ALPHA, /* 0xa9 'z' */ |
528 | | 0, /* 0xaa */ |
529 | | 0, /* 0xab */ |
530 | | 0, /* 0xac */ |
531 | | 0, /* 0xad */ |
532 | | 0, /* 0xae */ |
533 | | 0, /* 0xaf */ |
534 | | 0, /* 0xb0 */ |
535 | | 0, /* 0xb1 */ |
536 | | 0, /* 0xb2 */ |
537 | | 0, /* 0xb3 */ |
538 | | 0, /* 0xb4 */ |
539 | | 0, /* 0xb5 */ |
540 | | 0, /* 0xb6 */ |
541 | | 0, /* 0xb7 */ |
542 | | 0, /* 0xb8 */ |
543 | | 0, /* 0xb9 */ |
544 | | 0, /* 0xba */ |
545 | | 0, /* 0xbb */ |
546 | | 0, /* 0xbc */ |
547 | | 0, /* 0xbd */ |
548 | | 0, /* 0xbe */ |
549 | | 0, /* 0xbf */ |
550 | | 0, /* 0xc0 '{' */ |
551 | | T_ALPHA, /* 0xc1 'A' */ |
552 | | T_ALPHA, /* 0xc2 'B' */ |
553 | | T_ALPHA, /* 0xc3 'C' */ |
554 | | T_ALPHA, /* 0xc4 'D' */ |
555 | | T_ALPHA, /* 0xc5 'E' */ |
556 | | T_ALPHA, /* 0xc6 'F' */ |
557 | | T_ALPHA, /* 0xc7 'G' */ |
558 | | T_ALPHA, /* 0xc8 'H' */ |
559 | | T_ALPHA, /* 0xc9 'I' */ |
560 | | 0, /* 0xca */ |
561 | | 0, /* 0xcb */ |
562 | | 0, /* 0xcc */ |
563 | | 0, /* 0xcd */ |
564 | | 0, /* 0xce */ |
565 | | 0, /* 0xcf */ |
566 | | 0, /* 0xd0 '}' */ |
567 | | T_ALPHA, /* 0xd1 'J' */ |
568 | | T_ALPHA, /* 0xd2 'K' */ |
569 | | T_ALPHA, /* 0xd3 'L' */ |
570 | | T_ALPHA, /* 0xd4 'M' */ |
571 | | T_ALPHA, /* 0xd5 'N' */ |
572 | | T_ALPHA, /* 0xd6 'O' */ |
573 | | T_ALPHA, /* 0xd7 'P' */ |
574 | | T_ALPHA, /* 0xd8 'Q' */ |
575 | | T_ALPHA, /* 0xd9 'R' */ |
576 | | 0, /* 0xda */ |
577 | | 0, /* 0xdb */ |
578 | | 0, /* 0xdc */ |
579 | | 0, /* 0xdd */ |
580 | | 0, /* 0xde */ |
581 | | 0, /* 0xdf */ |
582 | | 0, /* 0xe0 '\' */ |
583 | | 0, /* 0xe1 */ |
584 | | T_ALPHA, /* 0xe2 'S' */ |
585 | | T_ALPHA, /* 0xe3 'T' */ |
586 | | T_ALPHA, /* 0xe4 'U' */ |
587 | | T_ALPHA, /* 0xe5 'V' */ |
588 | | T_ALPHA, /* 0xe6 'W' */ |
589 | | T_ALPHA, /* 0xe7 'X' */ |
590 | | T_ALPHA, /* 0xe8 'Y' */ |
591 | | T_ALPHA, /* 0xe9 'Z' */ |
592 | | 0, /* 0xea */ |
593 | | 0, /* 0xeb */ |
594 | | 0, /* 0xec */ |
595 | | 0, /* 0xed */ |
596 | | 0, /* 0xee */ |
597 | | 0, /* 0xef */ |
598 | | T_SCHEME, /* 0xf0 '0' */ |
599 | | T_SCHEME, /* 0xf1 '1' */ |
600 | | T_SCHEME, /* 0xf2 '2' */ |
601 | | T_SCHEME, /* 0xf3 '3' */ |
602 | | T_SCHEME, /* 0xf4 '4' */ |
603 | | T_SCHEME, /* 0xf5 '5' */ |
604 | | T_SCHEME, /* 0xf6 '6' */ |
605 | | T_SCHEME, /* 0xf7 '7' */ |
606 | | T_SCHEME, /* 0xf8 '8' */ |
607 | | T_SCHEME, /* 0xf9 '9' */ |
608 | | 0, /* 0xfa */ |
609 | | 0, /* 0xfb */ |
610 | | 0, /* 0xfc */ |
611 | | 0, /* 0xfd */ |
612 | | 0, /* 0xfe */ |
613 | | 0 /* 0xff */ |
614 | | }; |
615 | | #else |
616 | | /* Delimiter table for the ASCII character set */ |
617 | | static const unsigned char uri_delims[256] = { |
618 | | T_NUL, /* 0x00 */ |
619 | | 0, /* 0x01 */ |
620 | | 0, /* 0x02 */ |
621 | | 0, /* 0x03 */ |
622 | | 0, /* 0x04 */ |
623 | | 0, /* 0x05 */ |
624 | | 0, /* 0x06 */ |
625 | | 0, /* 0x07 */ |
626 | | 0, /* 0x08 */ |
627 | | 0, /* 0x09 */ |
628 | | 0, /* 0x0a */ |
629 | | 0, /* 0x0b */ |
630 | | 0, /* 0x0c */ |
631 | | 0, /* 0x0d */ |
632 | | 0, /* 0x0e */ |
633 | | 0, /* 0x0f */ |
634 | | 0, /* 0x10 */ |
635 | | 0, /* 0x11 */ |
636 | | 0, /* 0x12 */ |
637 | | 0, /* 0x13 */ |
638 | | 0, /* 0x14 */ |
639 | | 0, /* 0x15 */ |
640 | | 0, /* 0x16 */ |
641 | | 0, /* 0x17 */ |
642 | | 0, /* 0x18 */ |
643 | | 0, /* 0x19 */ |
644 | | 0, /* 0x1a */ |
645 | | 0, /* 0x1b */ |
646 | | 0, /* 0x1c */ |
647 | | 0, /* 0x1d */ |
648 | | 0, /* 0x1e */ |
649 | | 0, /* 0x1f */ |
650 | | 0, /* 0x20 ' ' */ |
651 | | 0, /* 0x21 '!' */ |
652 | | 0, /* 0x22 '"' */ |
653 | | T_HASH, /* 0x23 '#' */ |
654 | | 0, /* 0x24 '$' */ |
655 | | 0, /* 0x25 '%' */ |
656 | | 0, /* 0x26 '&' */ |
657 | | 0, /* 0x27 ''' */ |
658 | | 0, /* 0x28 '(' */ |
659 | | 0, /* 0x29 ')' */ |
660 | | 0, /* 0x2a '*' */ |
661 | | T_SCHEME, /* 0x2b '+' */ |
662 | | 0, /* 0x2c ',' */ |
663 | | T_SCHEME, /* 0x2d '-' */ |
664 | | T_SCHEME, /* 0x2e '.' */ |
665 | | T_SLASH, /* 0x2f '/' */ |
666 | | T_SCHEME, /* 0x30 '0' */ |
667 | | T_SCHEME, /* 0x31 '1' */ |
668 | | T_SCHEME, /* 0x32 '2' */ |
669 | | T_SCHEME, /* 0x33 '3' */ |
670 | | T_SCHEME, /* 0x34 '4' */ |
671 | | T_SCHEME, /* 0x35 '5' */ |
672 | | T_SCHEME, /* 0x36 '6' */ |
673 | | T_SCHEME, /* 0x37 '7' */ |
674 | | T_SCHEME, /* 0x38 '8' */ |
675 | | T_SCHEME, /* 0x39 '9' */ |
676 | | 0, /* 0x3a ':' */ |
677 | | 0, /* 0x3b ';' */ |
678 | | 0, /* 0x3c '<' */ |
679 | | 0, /* 0x3d '=' */ |
680 | | 0, /* 0x3e '>' */ |
681 | | T_QUESTION, /* 0x3f '?' */ |
682 | | 0, /* 0x40 '@' */ |
683 | | T_ALPHA, /* 0x41 'A' */ |
684 | | T_ALPHA, /* 0x42 'B' */ |
685 | | T_ALPHA, /* 0x43 'C' */ |
686 | | T_ALPHA, /* 0x44 'D' */ |
687 | | T_ALPHA, /* 0x45 'E' */ |
688 | | T_ALPHA, /* 0x46 'F' */ |
689 | | T_ALPHA, /* 0x47 'G' */ |
690 | | T_ALPHA, /* 0x48 'H' */ |
691 | | T_ALPHA, /* 0x49 'I' */ |
692 | | T_ALPHA, /* 0x4a 'J' */ |
693 | | T_ALPHA, /* 0x4b 'K' */ |
694 | | T_ALPHA, /* 0x4c 'L' */ |
695 | | T_ALPHA, /* 0x4d 'M' */ |
696 | | T_ALPHA, /* 0x4e 'N' */ |
697 | | T_ALPHA, /* 0x4f 'O' */ |
698 | | T_ALPHA, /* 0x50 'P' */ |
699 | | T_ALPHA, /* 0x51 'Q' */ |
700 | | T_ALPHA, /* 0x52 'R' */ |
701 | | T_ALPHA, /* 0x53 'S' */ |
702 | | T_ALPHA, /* 0x54 'T' */ |
703 | | T_ALPHA, /* 0x55 'U' */ |
704 | | T_ALPHA, /* 0x56 'V' */ |
705 | | T_ALPHA, /* 0x57 'W' */ |
706 | | T_ALPHA, /* 0x58 'X' */ |
707 | | T_ALPHA, /* 0x59 'Y' */ |
708 | | T_ALPHA, /* 0x5a 'Z' */ |
709 | | 0, /* 0x5b '[' */ |
710 | | 0, /* 0x5c '\' */ |
711 | | 0, /* 0x5d ']' */ |
712 | | 0, /* 0x5e '^' */ |
713 | | 0, /* 0x5f '_' */ |
714 | | 0, /* 0x60 '`' */ |
715 | | T_ALPHA, /* 0x61 'a' */ |
716 | | T_ALPHA, /* 0x62 'b' */ |
717 | | T_ALPHA, /* 0x63 'c' */ |
718 | | T_ALPHA, /* 0x64 'd' */ |
719 | | T_ALPHA, /* 0x65 'e' */ |
720 | | T_ALPHA, /* 0x66 'f' */ |
721 | | T_ALPHA, /* 0x67 'g' */ |
722 | | T_ALPHA, /* 0x68 'h' */ |
723 | | T_ALPHA, /* 0x69 'i' */ |
724 | | T_ALPHA, /* 0x6a 'j' */ |
725 | | T_ALPHA, /* 0x6b 'k' */ |
726 | | T_ALPHA, /* 0x6c 'l' */ |
727 | | T_ALPHA, /* 0x6d 'm' */ |
728 | | T_ALPHA, /* 0x6e 'n' */ |
729 | | T_ALPHA, /* 0x6f 'o' */ |
730 | | T_ALPHA, /* 0x70 'p' */ |
731 | | T_ALPHA, /* 0x71 'q' */ |
732 | | T_ALPHA, /* 0x72 'r' */ |
733 | | T_ALPHA, /* 0x73 's' */ |
734 | | T_ALPHA, /* 0x74 't' */ |
735 | | T_ALPHA, /* 0x75 'u' */ |
736 | | T_ALPHA, /* 0x76 'v' */ |
737 | | T_ALPHA, /* 0x77 'w' */ |
738 | | T_ALPHA, /* 0x78 'x' */ |
739 | | T_ALPHA, /* 0x79 'y' */ |
740 | | T_ALPHA, /* 0x7a 'z' */ |
741 | | 0, /* 0x7b '{' */ |
742 | | 0, /* 0x7c '|' */ |
743 | | 0, /* 0x7d '}' */ |
744 | | 0, /* 0x7e '~' */ |
745 | | 0, /* 0x7f */ |
746 | | 0, /* 0x80 */ |
747 | | 0, /* 0x81 */ |
748 | | 0, /* 0x82 */ |
749 | | 0, /* 0x83 */ |
750 | | 0, /* 0x84 */ |
751 | | 0, /* 0x85 */ |
752 | | 0, /* 0x86 */ |
753 | | 0, /* 0x87 */ |
754 | | 0, /* 0x88 */ |
755 | | 0, /* 0x89 */ |
756 | | 0, /* 0x8a */ |
757 | | 0, /* 0x8b */ |
758 | | 0, /* 0x8c */ |
759 | | 0, /* 0x8d */ |
760 | | 0, /* 0x8e */ |
761 | | 0, /* 0x8f */ |
762 | | 0, /* 0x90 */ |
763 | | 0, /* 0x91 */ |
764 | | 0, /* 0x92 */ |
765 | | 0, /* 0x93 */ |
766 | | 0, /* 0x94 */ |
767 | | 0, /* 0x95 */ |
768 | | 0, /* 0x96 */ |
769 | | 0, /* 0x97 */ |
770 | | 0, /* 0x98 */ |
771 | | 0, /* 0x99 */ |
772 | | 0, /* 0x9a */ |
773 | | 0, /* 0x9b */ |
774 | | 0, /* 0x9c */ |
775 | | 0, /* 0x9d */ |
776 | | 0, /* 0x9e */ |
777 | | 0, /* 0x9f */ |
778 | | 0, /* 0xa0 */ |
779 | | 0, /* 0xa1 */ |
780 | | 0, /* 0xa2 */ |
781 | | 0, /* 0xa3 */ |
782 | | 0, /* 0xa4 */ |
783 | | 0, /* 0xa5 */ |
784 | | 0, /* 0xa6 */ |
785 | | 0, /* 0xa7 */ |
786 | | 0, /* 0xa8 */ |
787 | | 0, /* 0xa9 */ |
788 | | 0, /* 0xaa */ |
789 | | 0, /* 0xab */ |
790 | | 0, /* 0xac */ |
791 | | 0, /* 0xad */ |
792 | | 0, /* 0xae */ |
793 | | 0, /* 0xaf */ |
794 | | 0, /* 0xb0 */ |
795 | | 0, /* 0xb1 */ |
796 | | 0, /* 0xb2 */ |
797 | | 0, /* 0xb3 */ |
798 | | 0, /* 0xb4 */ |
799 | | 0, /* 0xb5 */ |
800 | | 0, /* 0xb6 */ |
801 | | 0, /* 0xb7 */ |
802 | | 0, /* 0xb8 */ |
803 | | 0, /* 0xb9 */ |
804 | | 0, /* 0xba */ |
805 | | 0, /* 0xbb */ |
806 | | 0, /* 0xbc */ |
807 | | 0, /* 0xbd */ |
808 | | 0, /* 0xbe */ |
809 | | 0, /* 0xbf */ |
810 | | 0, /* 0xc0 */ |
811 | | 0, /* 0xc1 */ |
812 | | 0, /* 0xc2 */ |
813 | | 0, /* 0xc3 */ |
814 | | 0, /* 0xc4 */ |
815 | | 0, /* 0xc5 */ |
816 | | 0, /* 0xc6 */ |
817 | | 0, /* 0xc7 */ |
818 | | 0, /* 0xc8 */ |
819 | | 0, /* 0xc9 */ |
820 | | 0, /* 0xca */ |
821 | | 0, /* 0xcb */ |
822 | | 0, /* 0xcc */ |
823 | | 0, /* 0xcd */ |
824 | | 0, /* 0xce */ |
825 | | 0, /* 0xcf */ |
826 | | 0, /* 0xd0 */ |
827 | | 0, /* 0xd1 */ |
828 | | 0, /* 0xd2 */ |
829 | | 0, /* 0xd3 */ |
830 | | 0, /* 0xd4 */ |
831 | | 0, /* 0xd5 */ |
832 | | 0, /* 0xd6 */ |
833 | | 0, /* 0xd7 */ |
834 | | 0, /* 0xd8 */ |
835 | | 0, /* 0xd9 */ |
836 | | 0, /* 0xda */ |
837 | | 0, /* 0xdb */ |
838 | | 0, /* 0xdc */ |
839 | | 0, /* 0xdd */ |
840 | | 0, /* 0xde */ |
841 | | 0, /* 0xdf */ |
842 | | 0, /* 0xe0 */ |
843 | | 0, /* 0xe1 */ |
844 | | 0, /* 0xe2 */ |
845 | | 0, /* 0xe3 */ |
846 | | 0, /* 0xe4 */ |
847 | | 0, /* 0xe5 */ |
848 | | 0, /* 0xe6 */ |
849 | | 0, /* 0xe7 */ |
850 | | 0, /* 0xe8 */ |
851 | | 0, /* 0xe9 */ |
852 | | 0, /* 0xea */ |
853 | | 0, /* 0xeb */ |
854 | | 0, /* 0xec */ |
855 | | 0, /* 0xed */ |
856 | | 0, /* 0xee */ |
857 | | 0, /* 0xef */ |
858 | | 0, /* 0xf0 */ |
859 | | 0, /* 0xf1 */ |
860 | | 0, /* 0xf2 */ |
861 | | 0, /* 0xf3 */ |
862 | | 0, /* 0xf4 */ |
863 | | 0, /* 0xf5 */ |
864 | | 0, /* 0xf6 */ |
865 | | 0, /* 0xf7 */ |
866 | | 0, /* 0xf8 */ |
867 | | 0, /* 0xf9 */ |
868 | | 0, /* 0xfa */ |
869 | | 0, /* 0xfb */ |
870 | | 0, /* 0xfc */ |
871 | | 0, /* 0xfd */ |
872 | | 0, /* 0xfe */ |
873 | | 0 /* 0xff */ |
874 | | }; |
875 | | #endif |
876 | | |
877 | | |
878 | | /* it works like this: |
879 | | if (uri_delims[ch] & NOTEND_foobar) { |
880 | | then we're not at a delimiter for foobar |
881 | | } |
882 | | */ |
883 | | |
884 | 15.6k | #define NOTEND_HOSTINFO (T_SLASH | T_QUESTION | T_HASH | T_NUL) |
885 | 7.27k | #define NOTEND_PATH (T_QUESTION | T_HASH | T_NUL) |
886 | | |
887 | | /* apr_uri_parse(): |
888 | | * Parse a given URI, fill in all supplied fields of a apr_uri_t |
889 | | * structure. This eliminates the necessity of extracting host, port, |
890 | | * path, query info repeatedly in the modules. |
891 | | * Side effects: |
892 | | * - fills in fields of apr_uri_t *uptr |
893 | | * - none on any of the r->* fields |
894 | | */ |
895 | | APR_DECLARE(apr_status_t) apr_uri_parse(apr_pool_t *p, const char *uri, |
896 | | apr_uri_t *uptr) |
897 | 932 | { |
898 | 932 | const char *s; |
899 | 932 | const char *s1; |
900 | 932 | const char *hostinfo; |
901 | 932 | char *endstr; |
902 | 932 | int port; |
903 | 932 | int v6_offset1 = 0, v6_offset2 = 0; |
904 | 932 | int have_zone_id = 0; |
905 | | |
906 | | /* Initialize the structure. |
907 | | */ |
908 | 932 | memset (uptr, '\0', sizeof(*uptr)); |
909 | 932 | uptr->is_initialized = 1; |
910 | | |
911 | | /* We assume the processor has a branch predictor like most -- |
912 | | * it assumes forward branches are untaken and backwards are taken. That's |
913 | | * the reason for the gotos. -djg |
914 | | */ |
915 | 932 | if (uri[0] == '/') { |
916 | | /* RFC2396 #4.3 says that two leading slashes mean we have an |
917 | | * authority component, not a path! Fixing this looks scary |
918 | | * with the gotos here. But if the existing logic is valid, |
919 | | * then presumably a goto pointing to deal_with_authority works. |
920 | | * |
921 | | * RFC2396 describes this as resolving an ambiguity. In the |
922 | | * case of three or more slashes there would seem to be no |
923 | | * ambiguity, so it is a path after all. |
924 | | */ |
925 | 41 | if (uri[1] == '/' && uri[2] != '/') { |
926 | 13 | s = uri + 2 ; |
927 | 13 | goto deal_with_authority ; |
928 | 13 | } |
929 | | |
930 | 858 | deal_with_path: |
931 | | /* we expect uri to point to first character of path ... remember |
932 | | * that the path could be empty -- http://foobar?query for example |
933 | | */ |
934 | 858 | s = uri; |
935 | 7.27k | while ((uri_delims[*(unsigned char *)s] & NOTEND_PATH) == 0) { |
936 | 6.41k | ++s; |
937 | 6.41k | } |
938 | 858 | if (s != uri) { |
939 | 377 | uptr->path = apr_pstrmemdup(p, uri, s - uri); |
940 | 377 | } |
941 | 858 | if (*s == 0) { |
942 | 712 | return APR_SUCCESS; |
943 | 712 | } |
944 | 146 | if (*s == '?') { |
945 | 104 | ++s; |
946 | 104 | s1 = strchr(s, '#'); |
947 | 104 | if (s1) { |
948 | 20 | uptr->fragment = apr_pstrdup(p, s1 + 1); |
949 | 20 | uptr->query = apr_pstrmemdup(p, s, s1 - s); |
950 | 20 | } |
951 | 84 | else { |
952 | 84 | uptr->query = apr_pstrdup(p, s); |
953 | 84 | } |
954 | 104 | return APR_SUCCESS; |
955 | 104 | } |
956 | | /* otherwise it's a fragment */ |
957 | 42 | uptr->fragment = apr_pstrdup(p, s + 1); |
958 | 42 | return APR_SUCCESS; |
959 | 146 | } |
960 | | |
961 | | /* find the scheme: */ |
962 | 891 | s = uri; |
963 | | /* first char must be letter */ |
964 | 891 | if (uri_delims[*(unsigned char *)s] & T_ALPHA) { |
965 | 831 | ++s; |
966 | 6.91k | while ((uri_delims[*(unsigned char *)s] & (T_ALPHA|T_SCHEME))) |
967 | 6.08k | ++s; |
968 | 831 | } |
969 | | /* scheme must be non-empty and followed by : */ |
970 | 891 | if (s != uri && s[0] == ':') { |
971 | 768 | uptr->scheme = apr_pstrmemdup(p, uri, s - uri); |
972 | 768 | s++; |
973 | 768 | } |
974 | 123 | else { |
975 | | /* No valid scheme, restart from the beginning */ |
976 | 123 | s = uri; |
977 | 123 | } |
978 | | |
979 | 891 | if (s[0] != '/' || s[1] != '/') { |
980 | 168 | if (uri == s) { |
981 | | /* |
982 | | * RFC 3986 3.3: If we have no scheme and no authority, |
983 | | * the leading segment of a relative path must not contain a ':'. |
984 | | */ |
985 | 123 | const char *first_slash = strchr(uri, '/'); |
986 | 123 | if (first_slash) { |
987 | 779 | while (s < first_slash) { |
988 | 748 | if (s[0] == ':') |
989 | 38 | return APR_EGENERAL; |
990 | 710 | ++s; |
991 | 710 | } |
992 | | /* no scheme but relative path, e.g. '../image.jpg' */ |
993 | 69 | } |
994 | 54 | else { |
995 | 54 | if (strchr(uri, ':') != NULL) |
996 | 8 | return APR_EGENERAL; |
997 | | /* no scheme, no slash, but relative path, e.g. 'image.jpg' */ |
998 | 54 | } |
999 | 77 | goto deal_with_path; |
1000 | 123 | } |
1001 | | /* scheme and relative path */ |
1002 | 45 | uri = s; |
1003 | 45 | goto deal_with_path; |
1004 | 168 | } |
1005 | | |
1006 | 723 | s += 2; |
1007 | | |
1008 | 736 | deal_with_authority: |
1009 | 736 | hostinfo = s; |
1010 | 15.6k | while ((uri_delims[*(unsigned char *)s] & NOTEND_HOSTINFO) == 0) { |
1011 | 14.8k | ++s; |
1012 | 14.8k | } |
1013 | 736 | uri = s; /* whatever follows hostinfo is start of uri */ |
1014 | 736 | uptr->hostinfo = apr_pstrmemdup(p, hostinfo, uri - hostinfo); |
1015 | | |
1016 | | /* If there's a username:password@host:port, the @ we want is the last @... |
1017 | | * too bad there's no memrchr()... For the C purists, note that hostinfo |
1018 | | * is definitely not the first character of the original uri so therefore |
1019 | | * &hostinfo[-1] < &hostinfo[0] ... and this loop is valid C. |
1020 | | */ |
1021 | 13.1k | do { |
1022 | 13.1k | --s; |
1023 | 13.1k | } while (s >= hostinfo && *s != '@'); |
1024 | 736 | if (s < hostinfo) { |
1025 | | /* again we want the common case to be fall through */ |
1026 | 736 | deal_with_host: |
1027 | | /* We expect hostinfo to point to the first character of |
1028 | | * the hostname. If there's a port it is the first colon, |
1029 | | * except with IPv6. |
1030 | | * |
1031 | | * IPv6 also has the interesting property (RFC6874) that it may contain |
1032 | | * a percent-encoded percent delimiting the zone identifier. We need to |
1033 | | * unescape that. |
1034 | | */ |
1035 | 736 | if (*hostinfo == '[') { |
1036 | 281 | apr_status_t err; |
1037 | | |
1038 | 281 | v6_offset1 = 1; |
1039 | 281 | v6_offset2 = 2; |
1040 | 281 | s = memchr(hostinfo, ']', uri - hostinfo); |
1041 | 281 | if (s == NULL) { |
1042 | 2 | return APR_EGENERAL; |
1043 | 2 | } |
1044 | | |
1045 | | /* zone identifier */ |
1046 | 279 | err = detect_scope_zone_id(&have_zone_id, hostinfo + 1, s - hostinfo - 1); |
1047 | | /* FIXME: Ignore APR_EINVAL (invalid escaped character) for now as |
1048 | | * old code may rely on it silently getting ignored? |
1049 | | */ |
1050 | 279 | if ((err != APR_SUCCESS) && (err != APR_EINVAL)) { |
1051 | 0 | return err; |
1052 | 0 | } |
1053 | | |
1054 | | /* Port */ |
1055 | 279 | if (*++s != ':') { |
1056 | 233 | s = NULL; /* no port */ |
1057 | 233 | } |
1058 | 279 | } |
1059 | 455 | else { |
1060 | 455 | s = memchr(hostinfo, ':', uri - hostinfo); |
1061 | 455 | } |
1062 | 734 | if (s == NULL) { |
1063 | | /* we expect the common case to have no port */ |
1064 | 477 | uptr->hostname = apr_pstrmemdup(p, |
1065 | 477 | hostinfo + v6_offset1, |
1066 | 477 | uri - hostinfo - v6_offset2); |
1067 | 477 | if (have_zone_id) { |
1068 | 3 | percent_decode_scope_zone_id(uptr->hostname); |
1069 | 3 | } |
1070 | 477 | uptr->port = apr_uri_port_of_scheme(uptr->scheme); |
1071 | 477 | goto deal_with_path; |
1072 | 477 | } |
1073 | 257 | uptr->hostname = apr_pstrmemdup(p, |
1074 | 257 | hostinfo + v6_offset1, |
1075 | 257 | s - hostinfo - v6_offset2); |
1076 | 257 | if (have_zone_id) { |
1077 | 2 | percent_decode_scope_zone_id(uptr->hostname); |
1078 | 2 | } |
1079 | 257 | ++s; |
1080 | 257 | uptr->port_str = apr_pstrmemdup(p, s, uri - s); |
1081 | 257 | if (uri != s) { |
1082 | 68 | port = strtol(uptr->port_str, &endstr, 10); |
1083 | 68 | uptr->port = port; |
1084 | 68 | if (*endstr == '\0') { |
1085 | 42 | goto deal_with_path; |
1086 | 42 | } |
1087 | | /* Invalid characters after ':' found */ |
1088 | 26 | return APR_EGENERAL; |
1089 | 68 | } |
1090 | 189 | uptr->port = apr_uri_port_of_scheme(uptr->scheme); |
1091 | 189 | goto deal_with_path; |
1092 | 257 | } |
1093 | | |
1094 | | /* first colon delimits username:password */ |
1095 | 237 | s1 = memchr(hostinfo, ':', s - hostinfo); |
1096 | 237 | if (s1) { |
1097 | 53 | uptr->user = apr_pstrmemdup(p, hostinfo, s1 - hostinfo); |
1098 | 53 | ++s1; |
1099 | 53 | uptr->password = apr_pstrmemdup(p, s1, s - s1); |
1100 | 53 | } |
1101 | 184 | else { |
1102 | 184 | uptr->user = apr_pstrmemdup(p, hostinfo, s - hostinfo); |
1103 | 184 | } |
1104 | 237 | hostinfo = s + 1; |
1105 | 237 | goto deal_with_host; |
1106 | 736 | } |
1107 | | |
1108 | | /* Special case for CONNECT parsing: it comes with the hostinfo part only */ |
1109 | | /* See the INTERNET-DRAFT document "Tunneling SSL Through a WWW Proxy" |
1110 | | * currently at http://www.mcom.com/newsref/std/tunneling_ssl.html |
1111 | | * for the format of the "CONNECT host:port HTTP/1.0" request |
1112 | | */ |
1113 | | APR_DECLARE(apr_status_t) apr_uri_parse_hostinfo(apr_pool_t *p, |
1114 | | const char *hostinfo, |
1115 | | apr_uri_t *uptr) |
1116 | 468 | { |
1117 | 468 | const char *s; |
1118 | 468 | char *endstr; |
1119 | 468 | const char *rsb; |
1120 | 468 | int v6_offset1 = 0; |
1121 | | |
1122 | | /* Initialize the structure. |
1123 | | */ |
1124 | 468 | memset(uptr, '\0', sizeof(*uptr)); |
1125 | 468 | uptr->is_initialized = 1; |
1126 | 468 | uptr->hostinfo = apr_pstrdup(p, hostinfo); |
1127 | | |
1128 | | /* We expect hostinfo to point to the first character of |
1129 | | * the hostname. There must be a port, separated by a colon |
1130 | | */ |
1131 | 468 | if (*hostinfo == '[') { |
1132 | 38 | if ((rsb = strchr(hostinfo, ']')) == NULL || |
1133 | 36 | *(rsb + 1) != ':') { |
1134 | 26 | return APR_EGENERAL; |
1135 | 26 | } |
1136 | | /* literal IPv6 address */ |
1137 | 12 | s = rsb + 1; |
1138 | 12 | ++hostinfo; |
1139 | 12 | v6_offset1 = 1; |
1140 | 12 | } |
1141 | 430 | else { |
1142 | 430 | s = strchr(hostinfo, ':'); |
1143 | 430 | } |
1144 | 442 | if (s == NULL) { |
1145 | 19 | return APR_EGENERAL; |
1146 | 19 | } |
1147 | 423 | uptr->hostname = apr_pstrndup(p, hostinfo, s - hostinfo - v6_offset1); |
1148 | | |
1149 | | /* Again, ensure zone IDs are decoded. */ |
1150 | 423 | int have_zone_id = 0; |
1151 | 423 | apr_status_t err = detect_scope_zone_id(&have_zone_id, uptr->hostname, |
1152 | 423 | strlen(uptr->hostname)); |
1153 | | /* FIXME: Ignore APR_EINVAL (invalid escaped character) for now as old code |
1154 | | * may rely on it silently getting ignored? |
1155 | | */ |
1156 | 423 | if ((err != APR_SUCCESS) && (err != APR_EINVAL)) { |
1157 | 0 | return err; |
1158 | 0 | } |
1159 | 423 | if (have_zone_id) { |
1160 | 3 | percent_decode_scope_zone_id(uptr->hostname); |
1161 | 3 | } |
1162 | | |
1163 | 423 | ++s; |
1164 | 423 | uptr->port_str = apr_pstrdup(p, s); |
1165 | 423 | if (*s != '\0') { |
1166 | 417 | uptr->port = (unsigned short) strtol(uptr->port_str, &endstr, 10); |
1167 | 417 | if (*endstr == '\0') { |
1168 | 5 | return APR_SUCCESS; |
1169 | 5 | } |
1170 | | /* Invalid characters after ':' found */ |
1171 | 417 | } |
1172 | 418 | return APR_EGENERAL; |
1173 | 423 | } |