2
0

apr_uri.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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. * apr_uri.c: URI related utility things
  18. *
  19. */
  20. #include <stdlib.h>
  21. #include "apu.h"
  22. #include "apr.h"
  23. #include "apr_general.h"
  24. #include "apr_strings.h"
  25. #define APR_WANT_STRFUNC
  26. #include "apr_want.h"
  27. #include "apr_uri.h"
  28. typedef struct schemes_t schemes_t;
  29. /** Structure to store various schemes and their default ports */
  30. struct schemes_t {
  31. /** The name of the scheme */
  32. const char *name;
  33. /** The default port for the scheme */
  34. apr_port_t default_port;
  35. };
  36. /* Some WWW schemes and their default ports; this is basically /etc/services */
  37. /* This will become global when the protocol abstraction comes */
  38. /* As the schemes are searched by a linear search, */
  39. /* they are sorted by their expected frequency */
  40. static schemes_t schemes[] =
  41. {
  42. {"http", APR_URI_HTTP_DEFAULT_PORT},
  43. {"ftp", APR_URI_FTP_DEFAULT_PORT},
  44. {"https", APR_URI_HTTPS_DEFAULT_PORT},
  45. {"gopher", APR_URI_GOPHER_DEFAULT_PORT},
  46. {"ldap", APR_URI_LDAP_DEFAULT_PORT},
  47. {"nntp", APR_URI_NNTP_DEFAULT_PORT},
  48. {"snews", APR_URI_SNEWS_DEFAULT_PORT},
  49. {"imap", APR_URI_IMAP_DEFAULT_PORT},
  50. {"pop", APR_URI_POP_DEFAULT_PORT},
  51. {"sip", APR_URI_SIP_DEFAULT_PORT},
  52. {"rtsp", APR_URI_RTSP_DEFAULT_PORT},
  53. {"wais", APR_URI_WAIS_DEFAULT_PORT},
  54. {"z39.50r", APR_URI_WAIS_DEFAULT_PORT},
  55. {"z39.50s", APR_URI_WAIS_DEFAULT_PORT},
  56. {"prospero", APR_URI_PROSPERO_DEFAULT_PORT},
  57. {"nfs", APR_URI_NFS_DEFAULT_PORT},
  58. {"tip", APR_URI_TIP_DEFAULT_PORT},
  59. {"acap", APR_URI_ACAP_DEFAULT_PORT},
  60. {"telnet", APR_URI_TELNET_DEFAULT_PORT},
  61. {"ssh", APR_URI_SSH_DEFAULT_PORT},
  62. { NULL, 0xFFFF } /* unknown port */
  63. };
  64. APU_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str)
  65. {
  66. schemes_t *scheme;
  67. if (scheme_str) {
  68. for (scheme = schemes; scheme->name != NULL; ++scheme) {
  69. if (strcasecmp(scheme_str, scheme->name) == 0) {
  70. return scheme->default_port;
  71. }
  72. }
  73. }
  74. return 0;
  75. }
  76. /* Unparse a apr_uri_t structure to an URI string.
  77. * Optionally suppress the password for security reasons.
  78. */
  79. APU_DECLARE(char *) apr_uri_unparse(apr_pool_t *p,
  80. const apr_uri_t *uptr,
  81. unsigned flags)
  82. {
  83. char *ret = "";
  84. /* If suppressing the site part, omit both user name & scheme://hostname */
  85. if (!(flags & APR_URI_UNP_OMITSITEPART)) {
  86. /* Construct a "user:password@" string, honoring the passed
  87. * APR_URI_UNP_ flags: */
  88. if (uptr->user || uptr->password) {
  89. ret = apr_pstrcat(p,
  90. (uptr->user && !(flags & APR_URI_UNP_OMITUSER))
  91. ? uptr->user : "",
  92. (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))
  93. ? ":" : "",
  94. (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))
  95. ? ((flags & APR_URI_UNP_REVEALPASSWORD)
  96. ? uptr->password : "XXXXXXXX")
  97. : "",
  98. ((uptr->user && !(flags & APR_URI_UNP_OMITUSER)) ||
  99. (uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD)))
  100. ? "@" : "",
  101. NULL);
  102. }
  103. /* Construct scheme://site string */
  104. if (uptr->hostname) {
  105. int is_default_port;
  106. const char *lbrk = "", *rbrk = "";
  107. if (strchr(uptr->hostname, ':')) { /* v6 literal */
  108. lbrk = "[";
  109. rbrk = "]";
  110. }
  111. is_default_port =
  112. (uptr->port_str == NULL ||
  113. uptr->port == 0 ||
  114. uptr->port == apr_uri_port_of_scheme(uptr->scheme));
  115. if (uptr->scheme) {
  116. ret = apr_pstrcat(p,
  117. uptr->scheme, "://", ret,
  118. lbrk, uptr->hostname, rbrk,
  119. is_default_port ? "" : ":",
  120. is_default_port ? "" : uptr->port_str,
  121. NULL);
  122. }
  123. else {
  124. /* A violation of RFC2396, but it is clear from section 3.2
  125. * that the : belongs above to the scheme, while // belongs
  126. * to the authority, so include the authority prefix while
  127. * omitting the "scheme:" that the user neglected to pass us.
  128. */
  129. ret = apr_pstrcat(p,
  130. "//", ret, lbrk, uptr->hostname, rbrk,
  131. is_default_port ? "" : ":",
  132. is_default_port ? "" : uptr->port_str,
  133. NULL);
  134. }
  135. }
  136. }
  137. /* Should we suppress all path info? */
  138. if (!(flags & APR_URI_UNP_OMITPATHINFO)) {
  139. /* Append path, query and fragment strings: */
  140. ret = apr_pstrcat(p,
  141. ret,
  142. (uptr->path)
  143. ? uptr->path : "",
  144. (uptr->query && !(flags & APR_URI_UNP_OMITQUERY))
  145. ? "?" : "",
  146. (uptr->query && !(flags & APR_URI_UNP_OMITQUERY))
  147. ? uptr->query : "",
  148. (uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY))
  149. ? "#" : NULL,
  150. (uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY))
  151. ? uptr->fragment : NULL,
  152. NULL);
  153. }
  154. return ret;
  155. }
  156. /* Here is the hand-optimized parse_uri_components(). There are some wild
  157. * tricks we could pull in assembly language that we don't pull here... like we
  158. * can do word-at-time scans for delimiter characters using the same technique
  159. * that fast memchr()s use. But that would be way non-portable. -djg
  160. */
  161. /* We have a apr_table_t that we can index by character and it tells us if the
  162. * character is one of the interesting delimiters. Note that we even get
  163. * compares for NUL for free -- it's just another delimiter.
  164. */
  165. #define T_COLON 0x01 /* ':' */
  166. #define T_SLASH 0x02 /* '/' */
  167. #define T_QUESTION 0x04 /* '?' */
  168. #define T_HASH 0x08 /* '#' */
  169. #define T_NUL 0x80 /* '\0' */
  170. #if APR_CHARSET_EBCDIC
  171. /* Delimiter table for the EBCDIC character set */
  172. static const unsigned char uri_delims[256] = {
  173. T_NUL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  174. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  175. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  176. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  177. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  178. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  179. 0,T_SLASH,0,0,0,0,0,0,0,0,0,0,0,0,0,T_QUESTION,
  180. 0,0,0,0,0,0,0,0,0,0,T_COLON,T_HASH,0,0,0,0,
  181. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  182. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  183. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  184. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  185. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  186. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  187. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  188. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  189. };
  190. #else
  191. /* Delimiter table for the ASCII character set */
  192. static const unsigned char uri_delims[256] = {
  193. T_NUL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  194. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  195. 0,0,0,T_HASH,0,0,0,0,0,0,0,0,0,0,0,T_SLASH,
  196. 0,0,0,0,0,0,0,0,0,0,T_COLON,0,0,0,0,T_QUESTION,
  197. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  198. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  199. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  200. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  201. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  202. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  203. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  204. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  205. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  206. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  207. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  208. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  209. };
  210. #endif
  211. /* it works like this:
  212. if (uri_delims[ch] & NOTEND_foobar) {
  213. then we're not at a delimiter for foobar
  214. }
  215. */
  216. /* Note that we optimize the scheme scanning here, we cheat and let the
  217. * compiler know that it doesn't have to do the & masking.
  218. */
  219. #define NOTEND_SCHEME (0xff)
  220. #define NOTEND_HOSTINFO (T_SLASH | T_QUESTION | T_HASH | T_NUL)
  221. #define NOTEND_PATH (T_QUESTION | T_HASH | T_NUL)
  222. /* parse_uri_components():
  223. * Parse a given URI, fill in all supplied fields of a uri_components
  224. * structure. This eliminates the necessity of extracting host, port,
  225. * path, query info repeatedly in the modules.
  226. * Side effects:
  227. * - fills in fields of uri_components *uptr
  228. * - none on any of the r->* fields
  229. */
  230. APU_DECLARE(apr_status_t) apr_uri_parse(apr_pool_t *p, const char *uri,
  231. apr_uri_t *uptr)
  232. {
  233. const char *s;
  234. const char *s1;
  235. const char *hostinfo;
  236. char *endstr;
  237. int port;
  238. int v6_offset1 = 0, v6_offset2 = 0;
  239. /* Initialize the structure. parse_uri() and parse_uri_components()
  240. * can be called more than once per request.
  241. */
  242. memset (uptr, '\0', sizeof(*uptr));
  243. uptr->is_initialized = 1;
  244. /* We assume the processor has a branch predictor like most --
  245. * it assumes forward branches are untaken and backwards are taken. That's
  246. * the reason for the gotos. -djg
  247. */
  248. if (uri[0] == '/') {
  249. /* RFC2396 #4.3 says that two leading slashes mean we have an
  250. * authority component, not a path! Fixing this looks scary
  251. * with the gotos here. But if the existing logic is valid,
  252. * then presumably a goto pointing to deal_with_authority works.
  253. *
  254. * RFC2396 describes this as resolving an ambiguity. In the
  255. * case of three or more slashes there would seem to be no
  256. * ambiguity, so it is a path after all.
  257. */
  258. if (uri[1] == '/' && uri[2] != '/') {
  259. s = uri + 2 ;
  260. goto deal_with_authority ;
  261. }
  262. deal_with_path:
  263. /* we expect uri to point to first character of path ... remember
  264. * that the path could be empty -- http://foobar?query for example
  265. */
  266. s = uri;
  267. while ((uri_delims[*(unsigned char *)s] & NOTEND_PATH) == 0) {
  268. ++s;
  269. }
  270. if (s != uri) {
  271. uptr->path = apr_pstrmemdup(p, uri, s - uri);
  272. }
  273. if (*s == 0) {
  274. return APR_SUCCESS;
  275. }
  276. if (*s == '?') {
  277. ++s;
  278. s1 = strchr(s, '#');
  279. if (s1) {
  280. uptr->fragment = apr_pstrdup(p, s1 + 1);
  281. uptr->query = apr_pstrmemdup(p, s, s1 - s);
  282. }
  283. else {
  284. uptr->query = apr_pstrdup(p, s);
  285. }
  286. return APR_SUCCESS;
  287. }
  288. /* otherwise it's a fragment */
  289. uptr->fragment = apr_pstrdup(p, s + 1);
  290. return APR_SUCCESS;
  291. }
  292. /* find the scheme: */
  293. s = uri;
  294. while ((uri_delims[*(unsigned char *)s] & NOTEND_SCHEME) == 0) {
  295. ++s;
  296. }
  297. /* scheme must be non-empty and followed by :// */
  298. if (s == uri || s[0] != ':' || s[1] != '/' || s[2] != '/') {
  299. goto deal_with_path; /* backwards predicted taken! */
  300. }
  301. uptr->scheme = apr_pstrmemdup(p, uri, s - uri);
  302. s += 3;
  303. deal_with_authority:
  304. hostinfo = s;
  305. while ((uri_delims[*(unsigned char *)s] & NOTEND_HOSTINFO) == 0) {
  306. ++s;
  307. }
  308. uri = s; /* whatever follows hostinfo is start of uri */
  309. uptr->hostinfo = apr_pstrmemdup(p, hostinfo, uri - hostinfo);
  310. /* If there's a username:password@host:port, the @ we want is the last @...
  311. * too bad there's no memrchr()... For the C purists, note that hostinfo
  312. * is definately not the first character of the original uri so therefore
  313. * &hostinfo[-1] < &hostinfo[0] ... and this loop is valid C.
  314. */
  315. do {
  316. --s;
  317. } while (s >= hostinfo && *s != '@');
  318. if (s < hostinfo) {
  319. /* again we want the common case to be fall through */
  320. deal_with_host:
  321. /* We expect hostinfo to point to the first character of
  322. * the hostname. If there's a port it is the first colon,
  323. * except with IPv6.
  324. */
  325. if (*hostinfo == '[') {
  326. v6_offset1 = 1;
  327. v6_offset2 = 2;
  328. s = memchr(hostinfo, ']', uri - hostinfo);
  329. if (s == NULL) {
  330. return APR_EGENERAL;
  331. }
  332. if (*++s != ':') {
  333. s = NULL; /* no port */
  334. }
  335. }
  336. else {
  337. s = memchr(hostinfo, ':', uri - hostinfo);
  338. }
  339. if (s == NULL) {
  340. /* we expect the common case to have no port */
  341. uptr->hostname = apr_pstrmemdup(p,
  342. hostinfo + v6_offset1,
  343. uri - hostinfo - v6_offset2);
  344. goto deal_with_path;
  345. }
  346. uptr->hostname = apr_pstrmemdup(p,
  347. hostinfo + v6_offset1,
  348. s - hostinfo - v6_offset2);
  349. ++s;
  350. uptr->port_str = apr_pstrmemdup(p, s, uri - s);
  351. if (uri != s) {
  352. port = strtol(uptr->port_str, &endstr, 10);
  353. uptr->port = port;
  354. if (*endstr == '\0') {
  355. goto deal_with_path;
  356. }
  357. /* Invalid characters after ':' found */
  358. return APR_EGENERAL;
  359. }
  360. uptr->port = apr_uri_port_of_scheme(uptr->scheme);
  361. goto deal_with_path;
  362. }
  363. /* first colon delimits username:password */
  364. s1 = memchr(hostinfo, ':', s - hostinfo);
  365. if (s1) {
  366. uptr->user = apr_pstrmemdup(p, hostinfo, s1 - hostinfo);
  367. ++s1;
  368. uptr->password = apr_pstrmemdup(p, s1, s - s1);
  369. }
  370. else {
  371. uptr->user = apr_pstrmemdup(p, hostinfo, s - hostinfo);
  372. }
  373. hostinfo = s + 1;
  374. goto deal_with_host;
  375. }
  376. /* Special case for CONNECT parsing: it comes with the hostinfo part only */
  377. /* See the INTERNET-DRAFT document "Tunneling SSL Through a WWW Proxy"
  378. * currently at http://www.mcom.com/newsref/std/tunneling_ssl.html
  379. * for the format of the "CONNECT host:port HTTP/1.0" request
  380. */
  381. APU_DECLARE(apr_status_t) apr_uri_parse_hostinfo(apr_pool_t *p,
  382. const char *hostinfo,
  383. apr_uri_t *uptr)
  384. {
  385. const char *s;
  386. char *endstr;
  387. const char *rsb;
  388. int v6_offset1 = 0;
  389. /* Initialize the structure. parse_uri() and parse_uri_components()
  390. * can be called more than once per request.
  391. */
  392. memset(uptr, '\0', sizeof(*uptr));
  393. uptr->is_initialized = 1;
  394. uptr->hostinfo = apr_pstrdup(p, hostinfo);
  395. /* We expect hostinfo to point to the first character of
  396. * the hostname. There must be a port, separated by a colon
  397. */
  398. if (*hostinfo == '[') {
  399. if ((rsb = strchr(hostinfo, ']')) == NULL ||
  400. *(rsb + 1) != ':') {
  401. return APR_EGENERAL;
  402. }
  403. /* literal IPv6 address */
  404. s = rsb + 1;
  405. ++hostinfo;
  406. v6_offset1 = 1;
  407. }
  408. else {
  409. s = strchr(hostinfo, ':');
  410. }
  411. if (s == NULL) {
  412. return APR_EGENERAL;
  413. }
  414. uptr->hostname = apr_pstrndup(p, hostinfo, s - hostinfo - v6_offset1);
  415. ++s;
  416. uptr->port_str = apr_pstrdup(p, s);
  417. if (*s != '\0') {
  418. uptr->port = (unsigned short) strtol(uptr->port_str, &endstr, 10);
  419. if (*endstr == '\0') {
  420. return APR_SUCCESS;
  421. }
  422. /* Invalid characters after ':' found */
  423. }
  424. return APR_EGENERAL;
  425. }