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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * apr_uri.c: URI related utility things
26 #include "apr_general.h"
27 #include "apr_strings.h"
29 #define APR_WANT_STRFUNC
34 typedef struct schemes_t schemes_t;
36 /** Structure to store various schemes and their default ports */
38 /** The name of the scheme */
40 /** The default port for the scheme */
41 apr_port_t default_port;
44 /* Some WWW schemes and their default ports; this is basically /etc/services */
45 /* This will become global when the protocol abstraction comes */
46 /* As the schemes are searched by a linear search, */
47 /* they are sorted by their expected frequency */
48 static schemes_t schemes[] =
50 {"http", APR_URI_HTTP_DEFAULT_PORT},
51 {"ftp", APR_URI_FTP_DEFAULT_PORT},
52 {"https", APR_URI_HTTPS_DEFAULT_PORT},
53 {"gopher", APR_URI_GOPHER_DEFAULT_PORT},
54 {"ldap", APR_URI_LDAP_DEFAULT_PORT},
55 {"nntp", APR_URI_NNTP_DEFAULT_PORT},
56 {"snews", APR_URI_SNEWS_DEFAULT_PORT},
57 {"imap", APR_URI_IMAP_DEFAULT_PORT},
58 {"pop", APR_URI_POP_DEFAULT_PORT},
59 {"sip", APR_URI_SIP_DEFAULT_PORT},
60 {"rtsp", APR_URI_RTSP_DEFAULT_PORT},
61 {"wais", APR_URI_WAIS_DEFAULT_PORT},
62 {"z39.50r", APR_URI_WAIS_DEFAULT_PORT},
63 {"z39.50s", APR_URI_WAIS_DEFAULT_PORT},
64 {"prospero", APR_URI_PROSPERO_DEFAULT_PORT},
65 {"nfs", APR_URI_NFS_DEFAULT_PORT},
66 {"tip", APR_URI_TIP_DEFAULT_PORT},
67 {"acap", APR_URI_ACAP_DEFAULT_PORT},
68 {"telnet", APR_URI_TELNET_DEFAULT_PORT},
69 {"ssh", APR_URI_SSH_DEFAULT_PORT},
70 { NULL, 0xFFFF } /* unknown port */
73 APU_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str)
78 for (scheme = schemes; scheme->name != NULL; ++scheme) {
79 if (strcasecmp(scheme_str, scheme->name) == 0) {
80 return scheme->default_port;
87 /* Unparse a apr_uri_t structure to an URI string.
88 * Optionally suppress the password for security reasons.
90 APU_DECLARE(char *) apr_uri_unparse(apr_pool_t *p,
91 const apr_uri_t *uptr,
96 /* If suppressing the site part, omit both user name & scheme://hostname */
97 if (!(flags & APR_URI_UNP_OMITSITEPART)) {
99 /* Construct a "user:password@" string, honoring the passed
100 * APR_URI_UNP_ flags: */
101 if (uptr->user || uptr->password) {
103 (uptr->user && !(flags & APR_URI_UNP_OMITUSER))
105 (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))
107 (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))
108 ? ((flags & APR_URI_UNP_REVEALPASSWORD)
109 ? uptr->password : "XXXXXXXX")
111 ((uptr->user && !(flags & APR_URI_UNP_OMITUSER)) ||
112 (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD)))
117 /* Construct scheme://site string */
118 if (uptr->hostname) {
120 const char *lbrk = "", *rbrk = "";
122 if (strchr(uptr->hostname, ':')) { /* v6 literal */
128 (uptr->port_str == NULL ||
130 uptr->port == apr_uri_port_of_scheme(uptr->scheme));
132 ret = apr_pstrcat(p, "//", ret, lbrk, uptr->hostname, rbrk,
133 is_default_port ? "" : ":",
134 is_default_port ? "" : uptr->port_str,
138 ret = apr_pstrcat(p, uptr->scheme, ":", ret, NULL);
142 /* Should we suppress all path info? */
143 if (!(flags & APR_URI_UNP_OMITPATHINFO)) {
144 /* Append path, query and fragment strings: */
149 (uptr->query && !(flags & APR_URI_UNP_OMITQUERY))
151 (uptr->query && !(flags & APR_URI_UNP_OMITQUERY))
153 (uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY))
155 (uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY))
156 ? uptr->fragment : NULL,
162 /* Here is the hand-optimized parse_uri_components(). There are some wild
163 * tricks we could pull in assembly language that we don't pull here... like we
164 * can do word-at-time scans for delimiter characters using the same technique
165 * that fast memchr()s use. But that would be way non-portable. -djg
168 /* We have a apr_table_t that we can index by character and it tells us if the
169 * character is one of the interesting delimiters. Note that we even get
170 * compares for NUL for free -- it's just another delimiter.
173 #define T_SLASH 0x01 /* '/' */
174 #define T_QUESTION 0x02 /* '?' */
175 #define T_HASH 0x04 /* '#' */
176 #define T_ALPHA 0x08 /* 'A' ... 'Z', 'a' ... 'z' */
177 #define T_SCHEME 0x10 /* '0' ... '9', '-', '+', '.'
178 * (allowed in scheme except first char)
180 #define T_NUL 0x80 /* '\0' */
182 #if APR_CHARSET_EBCDIC
183 /* Delimiter table for the EBCDIC character set */
184 static const unsigned char uri_delims[256] = {
260 T_SCHEME, /* 0x4b '.' */
263 T_SCHEME, /* 0x4e '+' */
281 T_SCHEME, /* 0x60 '-' */
282 T_SLASH, /* 0x61 '/' */
296 T_QUESTION, /* 0x6f '?' */
308 T_HASH, /* 0x7b '#' */
314 T_ALPHA, /* 0x81 'a' */
315 T_ALPHA, /* 0x82 'b' */
316 T_ALPHA, /* 0x83 'c' */
317 T_ALPHA, /* 0x84 'd' */
318 T_ALPHA, /* 0x85 'e' */
319 T_ALPHA, /* 0x86 'f' */
320 T_ALPHA, /* 0x87 'g' */
321 T_ALPHA, /* 0x88 'h' */
322 T_ALPHA, /* 0x89 'i' */
330 T_ALPHA, /* 0x91 'j' */
331 T_ALPHA, /* 0x92 'k' */
332 T_ALPHA, /* 0x93 'l' */
333 T_ALPHA, /* 0x94 'm' */
334 T_ALPHA, /* 0x95 'n' */
335 T_ALPHA, /* 0x96 'o' */
336 T_ALPHA, /* 0x97 'p' */
337 T_ALPHA, /* 0x98 'q' */
338 T_ALPHA, /* 0x99 'r' */
347 T_ALPHA, /* 0xa2 's' */
348 T_ALPHA, /* 0xa3 't' */
349 T_ALPHA, /* 0xa4 'u' */
350 T_ALPHA, /* 0xa5 'v' */
351 T_ALPHA, /* 0xa6 'w' */
352 T_ALPHA, /* 0xa7 'x' */
353 T_ALPHA, /* 0xa8 'y' */
354 T_ALPHA, /* 0xa9 'z' */
378 T_ALPHA, /* 0xc1 'A' */
379 T_ALPHA, /* 0xc2 'B' */
380 T_ALPHA, /* 0xc3 'C' */
381 T_ALPHA, /* 0xc4 'D' */
382 T_ALPHA, /* 0xc5 'E' */
383 T_ALPHA, /* 0xc6 'F' */
384 T_ALPHA, /* 0xc7 'G' */
385 T_ALPHA, /* 0xc8 'H' */
386 T_ALPHA, /* 0xc9 'I' */
394 T_ALPHA, /* 0xd1 'J' */
395 T_ALPHA, /* 0xd2 'K' */
396 T_ALPHA, /* 0xd3 'L' */
397 T_ALPHA, /* 0xd4 'M' */
398 T_ALPHA, /* 0xd5 'N' */
399 T_ALPHA, /* 0xd6 'O' */
400 T_ALPHA, /* 0xd7 'P' */
401 T_ALPHA, /* 0xd8 'Q' */
402 T_ALPHA, /* 0xd9 'R' */
411 T_ALPHA, /* 0xe2 'S' */
412 T_ALPHA, /* 0xe3 'T' */
413 T_ALPHA, /* 0xe4 'U' */
414 T_ALPHA, /* 0xe5 'V' */
415 T_ALPHA, /* 0xe6 'W' */
416 T_ALPHA, /* 0xe7 'X' */
417 T_ALPHA, /* 0xe8 'Y' */
418 T_ALPHA, /* 0xe9 'Z' */
425 T_SCHEME, /* 0xf0 '0' */
426 T_SCHEME, /* 0xf1 '1' */
427 T_SCHEME, /* 0xf2 '2' */
428 T_SCHEME, /* 0xf3 '3' */
429 T_SCHEME, /* 0xf4 '4' */
430 T_SCHEME, /* 0xf5 '5' */
431 T_SCHEME, /* 0xf6 '6' */
432 T_SCHEME, /* 0xf7 '7' */
433 T_SCHEME, /* 0xf8 '8' */
434 T_SCHEME, /* 0xf9 '9' */
443 /* Delimiter table for the ASCII character set */
444 static const unsigned char uri_delims[256] = {
480 T_HASH, /* 0x23 '#' */
488 T_SCHEME, /* 0x2b '+' */
490 T_SCHEME, /* 0x2d '-' */
491 T_SCHEME, /* 0x2e '.' */
492 T_SLASH, /* 0x2f '/' */
493 T_SCHEME, /* 0x30 '0' */
494 T_SCHEME, /* 0x31 '1' */
495 T_SCHEME, /* 0x32 '2' */
496 T_SCHEME, /* 0x33 '3' */
497 T_SCHEME, /* 0x34 '4' */
498 T_SCHEME, /* 0x35 '5' */
499 T_SCHEME, /* 0x36 '6' */
500 T_SCHEME, /* 0x37 '7' */
501 T_SCHEME, /* 0x38 '8' */
502 T_SCHEME, /* 0x39 '9' */
508 T_QUESTION, /* 0x3f '?' */
510 T_ALPHA, /* 0x41 'A' */
511 T_ALPHA, /* 0x42 'B' */
512 T_ALPHA, /* 0x43 'C' */
513 T_ALPHA, /* 0x44 'D' */
514 T_ALPHA, /* 0x45 'E' */
515 T_ALPHA, /* 0x46 'F' */
516 T_ALPHA, /* 0x47 'G' */
517 T_ALPHA, /* 0x48 'H' */
518 T_ALPHA, /* 0x49 'I' */
519 T_ALPHA, /* 0x4a 'J' */
520 T_ALPHA, /* 0x4b 'K' */
521 T_ALPHA, /* 0x4c 'L' */
522 T_ALPHA, /* 0x4d 'M' */
523 T_ALPHA, /* 0x4e 'N' */
524 T_ALPHA, /* 0x4f 'O' */
525 T_ALPHA, /* 0x50 'P' */
526 T_ALPHA, /* 0x51 'Q' */
527 T_ALPHA, /* 0x52 'R' */
528 T_ALPHA, /* 0x53 'S' */
529 T_ALPHA, /* 0x54 'T' */
530 T_ALPHA, /* 0x55 'U' */
531 T_ALPHA, /* 0x56 'V' */
532 T_ALPHA, /* 0x57 'W' */
533 T_ALPHA, /* 0x58 'X' */
534 T_ALPHA, /* 0x59 'Y' */
535 T_ALPHA, /* 0x5a 'Z' */
542 T_ALPHA, /* 0x61 'a' */
543 T_ALPHA, /* 0x62 'b' */
544 T_ALPHA, /* 0x63 'c' */
545 T_ALPHA, /* 0x64 'd' */
546 T_ALPHA, /* 0x65 'e' */
547 T_ALPHA, /* 0x66 'f' */
548 T_ALPHA, /* 0x67 'g' */
549 T_ALPHA, /* 0x68 'h' */
550 T_ALPHA, /* 0x69 'i' */
551 T_ALPHA, /* 0x6a 'j' */
552 T_ALPHA, /* 0x6b 'k' */
553 T_ALPHA, /* 0x6c 'l' */
554 T_ALPHA, /* 0x6d 'm' */
555 T_ALPHA, /* 0x6e 'n' */
556 T_ALPHA, /* 0x6f 'o' */
557 T_ALPHA, /* 0x70 'p' */
558 T_ALPHA, /* 0x71 'q' */
559 T_ALPHA, /* 0x72 'r' */
560 T_ALPHA, /* 0x73 's' */
561 T_ALPHA, /* 0x74 't' */
562 T_ALPHA, /* 0x75 'u' */
563 T_ALPHA, /* 0x76 'v' */
564 T_ALPHA, /* 0x77 'w' */
565 T_ALPHA, /* 0x78 'x' */
566 T_ALPHA, /* 0x79 'y' */
567 T_ALPHA, /* 0x7a 'z' */
705 /* it works like this:
706 if (uri_delims[ch] & NOTEND_foobar) {
707 then we're not at a delimiter for foobar
711 #define NOTEND_HOSTINFO (T_SLASH | T_QUESTION | T_HASH | T_NUL)
712 #define NOTEND_PATH (T_QUESTION | T_HASH | T_NUL)
714 /* parse_uri_components():
715 * Parse a given URI, fill in all supplied fields of a uri_components
716 * structure. This eliminates the necessity of extracting host, port,
717 * path, query info repeatedly in the modules.
719 * - fills in fields of uri_components *uptr
720 * - none on any of the r->* fields
722 APU_DECLARE(apr_status_t) apr_uri_parse(apr_pool_t *p, const char *uri,
727 const char *hostinfo;
730 int v6_offset1 = 0, v6_offset2 = 0;
732 /* Initialize the structure. parse_uri() and parse_uri_components()
733 * can be called more than once per request.
735 memset (uptr, '\0', sizeof(*uptr));
736 uptr->is_initialized = 1;
738 /* We assume the processor has a branch predictor like most --
739 * it assumes forward branches are untaken and backwards are taken. That's
740 * the reason for the gotos. -djg
743 /* RFC2396 #4.3 says that two leading slashes mean we have an
744 * authority component, not a path! Fixing this looks scary
745 * with the gotos here. But if the existing logic is valid,
746 * then presumably a goto pointing to deal_with_authority works.
748 * RFC2396 describes this as resolving an ambiguity. In the
749 * case of three or more slashes there would seem to be no
750 * ambiguity, so it is a path after all.
752 if (uri[1] == '/' && uri[2] != '/') {
754 goto deal_with_authority ;
758 /* we expect uri to point to first character of path ... remember
759 * that the path could be empty -- http://foobar?query for example
762 while ((uri_delims[*(unsigned char *)s] & NOTEND_PATH) == 0) {
766 uptr->path = apr_pstrmemdup(p, uri, s - uri);
775 uptr->fragment = apr_pstrdup(p, s1 + 1);
776 uptr->query = apr_pstrmemdup(p, s, s1 - s);
779 uptr->query = apr_pstrdup(p, s);
783 /* otherwise it's a fragment */
784 uptr->fragment = apr_pstrdup(p, s + 1);
788 /* find the scheme: */
790 /* first char must be letter */
791 if (uri_delims[*(unsigned char *)s] & T_ALPHA) {
793 while ((uri_delims[*(unsigned char *)s] & (T_ALPHA|T_SCHEME)))
796 /* scheme must be non-empty and followed by : */
797 if (s != uri && s[0] == ':') {
798 uptr->scheme = apr_pstrmemdup(p, uri, s - uri);
802 /* No valid scheme, restart from the beginning */
806 if (s[0] != '/' || s[1] != '/') {
809 * RFC 3986 3.3: If we have no scheme and no authority,
810 * the leading segment of a relative path must not contain a ':'.
812 char *first_slash = strchr(uri, '/');
814 while (s < first_slash) {
819 /* no scheme but relative path, e.g. '../image.jpg' */
822 if (strchr(uri, ':') != NULL)
824 /* no scheme, no slash, but relative path, e.g. 'image.jpg' */
828 /* scheme and relative path */
837 while ((uri_delims[*(unsigned char *)s] & NOTEND_HOSTINFO) == 0) {
840 uri = s; /* whatever follows hostinfo is start of uri */
841 uptr->hostinfo = apr_pstrmemdup(p, hostinfo, uri - hostinfo);
843 /* If there's a username:password@host:port, the @ we want is the last @...
844 * too bad there's no memrchr()... For the C purists, note that hostinfo
845 * is definitely not the first character of the original uri so therefore
846 * &hostinfo[-1] < &hostinfo[0] ... and this loop is valid C.
850 } while (s >= hostinfo && *s != '@');
852 /* again we want the common case to be fall through */
854 /* We expect hostinfo to point to the first character of
855 * the hostname. If there's a port it is the first colon,
858 if (*hostinfo == '[') {
861 s = memchr(hostinfo, ']', uri - hostinfo);
866 s = NULL; /* no port */
870 s = memchr(hostinfo, ':', uri - hostinfo);
873 /* we expect the common case to have no port */
874 uptr->hostname = apr_pstrmemdup(p,
875 hostinfo + v6_offset1,
876 uri - hostinfo - v6_offset2);
879 uptr->hostname = apr_pstrmemdup(p,
880 hostinfo + v6_offset1,
881 s - hostinfo - v6_offset2);
883 uptr->port_str = apr_pstrmemdup(p, s, uri - s);
885 port = strtol(uptr->port_str, &endstr, 10);
887 if (*endstr == '\0') {
890 /* Invalid characters after ':' found */
893 uptr->port = apr_uri_port_of_scheme(uptr->scheme);
897 /* first colon delimits username:password */
898 s1 = memchr(hostinfo, ':', s - hostinfo);
900 uptr->user = apr_pstrmemdup(p, hostinfo, s1 - hostinfo);
902 uptr->password = apr_pstrmemdup(p, s1, s - s1);
905 uptr->user = apr_pstrmemdup(p, hostinfo, s - hostinfo);
911 /* Special case for CONNECT parsing: it comes with the hostinfo part only */
912 /* See the INTERNET-DRAFT document "Tunneling SSL Through a WWW Proxy"
913 * currently at http://www.mcom.com/newsref/std/tunneling_ssl.html
914 * for the format of the "CONNECT host:port HTTP/1.0" request
916 APU_DECLARE(apr_status_t) apr_uri_parse_hostinfo(apr_pool_t *p,
917 const char *hostinfo,
925 /* Initialize the structure. parse_uri() and parse_uri_components()
926 * can be called more than once per request.
928 memset(uptr, '\0', sizeof(*uptr));
929 uptr->is_initialized = 1;
930 uptr->hostinfo = apr_pstrdup(p, hostinfo);
932 /* We expect hostinfo to point to the first character of
933 * the hostname. There must be a port, separated by a colon
935 if (*hostinfo == '[') {
936 if ((rsb = strchr(hostinfo, ']')) == NULL ||
940 /* literal IPv6 address */
946 s = strchr(hostinfo, ':');
951 uptr->hostname = apr_pstrndup(p, hostinfo, s - hostinfo - v6_offset1);
953 uptr->port_str = apr_pstrdup(p, s);
955 uptr->port = (unsigned short) strtol(uptr->port_str, &endstr, 10);
956 if (*endstr == '\0') {
959 /* Invalid characters after ':' found */