2
0

urlapi.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "urldata.h"
  24. #include "urlapi-int.h"
  25. #include "strcase.h"
  26. #include "dotdot.h"
  27. #include "url.h"
  28. #include "escape.h"
  29. #include "curl_ctype.h"
  30. #include "inet_pton.h"
  31. /* The last 3 #include files should be in this order */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. /* MSDOS/Windows style drive prefix, eg c: in c:foo */
  36. #define STARTS_WITH_DRIVE_PREFIX(str) \
  37. ((('a' <= str[0] && str[0] <= 'z') || \
  38. ('A' <= str[0] && str[0] <= 'Z')) && \
  39. (str[1] == ':'))
  40. /* MSDOS/Windows style drive prefix, optionally with
  41. * a '|' instead of ':', followed by a slash or NUL */
  42. #define STARTS_WITH_URL_DRIVE_PREFIX(str) \
  43. ((('a' <= (str)[0] && (str)[0] <= 'z') || \
  44. ('A' <= (str)[0] && (str)[0] <= 'Z')) && \
  45. ((str)[1] == ':' || (str)[1] == '|') && \
  46. ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
  47. /* Internal representation of CURLU. Point to URL-encoded strings. */
  48. struct Curl_URL {
  49. char *scheme;
  50. char *user;
  51. char *password;
  52. char *options; /* IMAP only? */
  53. char *host;
  54. char *zoneid; /* for numerical IPv6 addresses */
  55. char *port;
  56. char *path;
  57. char *query;
  58. char *fragment;
  59. char *scratch; /* temporary scratch area */
  60. char *temppath; /* temporary path pointer */
  61. long portnum; /* the numerical version */
  62. };
  63. #define DEFAULT_SCHEME "https"
  64. static void free_urlhandle(struct Curl_URL *u)
  65. {
  66. free(u->scheme);
  67. free(u->user);
  68. free(u->password);
  69. free(u->options);
  70. free(u->host);
  71. free(u->zoneid);
  72. free(u->port);
  73. free(u->path);
  74. free(u->query);
  75. free(u->fragment);
  76. free(u->scratch);
  77. free(u->temppath);
  78. }
  79. /* move the full contents of one handle onto another and
  80. free the original */
  81. static void mv_urlhandle(struct Curl_URL *from,
  82. struct Curl_URL *to)
  83. {
  84. free_urlhandle(to);
  85. *to = *from;
  86. free(from);
  87. }
  88. /*
  89. * Find the separator at the end of the host name, or the '?' in cases like
  90. * http://www.url.com?id=2380
  91. */
  92. static const char *find_host_sep(const char *url)
  93. {
  94. const char *sep;
  95. const char *query;
  96. /* Find the start of the hostname */
  97. sep = strstr(url, "//");
  98. if(!sep)
  99. sep = url;
  100. else
  101. sep += 2;
  102. query = strchr(sep, '?');
  103. sep = strchr(sep, '/');
  104. if(!sep)
  105. sep = url + strlen(url);
  106. if(!query)
  107. query = url + strlen(url);
  108. return sep < query ? sep : query;
  109. }
  110. /*
  111. * Decide in an encoding-independent manner whether a character in an
  112. * URL must be escaped. The same criterion must be used in strlen_url()
  113. * and strcpy_url().
  114. */
  115. static bool urlchar_needs_escaping(int c)
  116. {
  117. return !(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c));
  118. }
  119. /*
  120. * strlen_url() returns the length of the given URL if the spaces within the
  121. * URL were properly URL encoded.
  122. * URL encoding should be skipped for host names, otherwise IDN resolution
  123. * will fail.
  124. */
  125. static size_t strlen_url(const char *url, bool relative)
  126. {
  127. const unsigned char *ptr;
  128. size_t newlen = 0;
  129. bool left = TRUE; /* left side of the ? */
  130. const unsigned char *host_sep = (const unsigned char *) url;
  131. if(!relative)
  132. host_sep = (const unsigned char *) find_host_sep(url);
  133. for(ptr = (unsigned char *)url; *ptr; ptr++) {
  134. if(ptr < host_sep) {
  135. ++newlen;
  136. continue;
  137. }
  138. switch(*ptr) {
  139. case '?':
  140. left = FALSE;
  141. /* FALLTHROUGH */
  142. default:
  143. if(urlchar_needs_escaping(*ptr))
  144. newlen += 2;
  145. newlen++;
  146. break;
  147. case ' ':
  148. if(left)
  149. newlen += 3;
  150. else
  151. newlen++;
  152. break;
  153. }
  154. }
  155. return newlen;
  156. }
  157. /* strcpy_url() copies a url to a output buffer and URL-encodes the spaces in
  158. * the source URL accordingly.
  159. * URL encoding should be skipped for host names, otherwise IDN resolution
  160. * will fail.
  161. */
  162. static void strcpy_url(char *output, const char *url, bool relative)
  163. {
  164. /* we must add this with whitespace-replacing */
  165. bool left = TRUE;
  166. const unsigned char *iptr;
  167. char *optr = output;
  168. const unsigned char *host_sep = (const unsigned char *) url;
  169. if(!relative)
  170. host_sep = (const unsigned char *) find_host_sep(url);
  171. for(iptr = (unsigned char *)url; /* read from here */
  172. *iptr; /* until zero byte */
  173. iptr++) {
  174. if(iptr < host_sep) {
  175. *optr++ = *iptr;
  176. continue;
  177. }
  178. switch(*iptr) {
  179. case '?':
  180. left = FALSE;
  181. /* FALLTHROUGH */
  182. default:
  183. if(urlchar_needs_escaping(*iptr)) {
  184. msnprintf(optr, 4, "%%%02x", *iptr);
  185. optr += 3;
  186. }
  187. else
  188. *optr++=*iptr;
  189. break;
  190. case ' ':
  191. if(left) {
  192. *optr++='%'; /* add a '%' */
  193. *optr++='2'; /* add a '2' */
  194. *optr++='0'; /* add a '0' */
  195. }
  196. else
  197. *optr++='+'; /* add a '+' here */
  198. break;
  199. }
  200. }
  201. *optr = 0; /* null-terminate output buffer */
  202. }
  203. /*
  204. * Returns true if the given URL is absolute (as opposed to relative) within
  205. * the buffer size. Returns the scheme in the buffer if TRUE and 'buf' is
  206. * non-NULL.
  207. */
  208. bool Curl_is_absolute_url(const char *url, char *buf, size_t buflen)
  209. {
  210. size_t i;
  211. #ifdef WIN32
  212. if(STARTS_WITH_DRIVE_PREFIX(url))
  213. return FALSE;
  214. #endif
  215. for(i = 0; i < buflen && url[i]; ++i) {
  216. char s = url[i];
  217. if((s == ':') && (url[i + 1] == '/')) {
  218. if(buf)
  219. buf[i] = 0;
  220. return TRUE;
  221. }
  222. /* RFC 3986 3.1 explains:
  223. scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  224. */
  225. else if(ISALNUM(s) || (s == '+') || (s == '-') || (s == '.') ) {
  226. if(buf)
  227. buf[i] = (char)TOLOWER(s);
  228. }
  229. else
  230. break;
  231. }
  232. return FALSE;
  233. }
  234. /*
  235. * Concatenate a relative URL to a base URL making it absolute.
  236. * URL-encodes any spaces.
  237. * The returned pointer must be freed by the caller unless NULL
  238. * (returns NULL on out of memory).
  239. */
  240. static char *concat_url(const char *base, const char *relurl)
  241. {
  242. /***
  243. TRY to append this new path to the old URL
  244. to the right of the host part. Oh crap, this is doomed to cause
  245. problems in the future...
  246. */
  247. char *newest;
  248. char *protsep;
  249. char *pathsep;
  250. size_t newlen;
  251. bool host_changed = FALSE;
  252. const char *useurl = relurl;
  253. size_t urllen;
  254. /* we must make our own copy of the URL to play with, as it may
  255. point to read-only data */
  256. char *url_clone = strdup(base);
  257. if(!url_clone)
  258. return NULL; /* skip out of this NOW */
  259. /* protsep points to the start of the host name */
  260. protsep = strstr(url_clone, "//");
  261. if(!protsep)
  262. protsep = url_clone;
  263. else
  264. protsep += 2; /* pass the slashes */
  265. if('/' != relurl[0]) {
  266. int level = 0;
  267. /* First we need to find out if there's a ?-letter in the URL,
  268. and cut it and the right-side of that off */
  269. pathsep = strchr(protsep, '?');
  270. if(pathsep)
  271. *pathsep = 0;
  272. /* we have a relative path to append to the last slash if there's one
  273. available, or if the new URL is just a query string (starts with a
  274. '?') we append the new one at the end of the entire currently worked
  275. out URL */
  276. if(useurl[0] != '?') {
  277. pathsep = strrchr(protsep, '/');
  278. if(pathsep)
  279. *pathsep = 0;
  280. }
  281. /* Check if there's any slash after the host name, and if so, remember
  282. that position instead */
  283. pathsep = strchr(protsep, '/');
  284. if(pathsep)
  285. protsep = pathsep + 1;
  286. else
  287. protsep = NULL;
  288. /* now deal with one "./" or any amount of "../" in the newurl
  289. and act accordingly */
  290. if((useurl[0] == '.') && (useurl[1] == '/'))
  291. useurl += 2; /* just skip the "./" */
  292. while((useurl[0] == '.') &&
  293. (useurl[1] == '.') &&
  294. (useurl[2] == '/')) {
  295. level++;
  296. useurl += 3; /* pass the "../" */
  297. }
  298. if(protsep) {
  299. while(level--) {
  300. /* cut off one more level from the right of the original URL */
  301. pathsep = strrchr(protsep, '/');
  302. if(pathsep)
  303. *pathsep = 0;
  304. else {
  305. *protsep = 0;
  306. break;
  307. }
  308. }
  309. }
  310. }
  311. else {
  312. /* We got a new absolute path for this server */
  313. if(relurl[1] == '/') {
  314. /* the new URL starts with //, just keep the protocol part from the
  315. original one */
  316. *protsep = 0;
  317. useurl = &relurl[2]; /* we keep the slashes from the original, so we
  318. skip the new ones */
  319. host_changed = TRUE;
  320. }
  321. else {
  322. /* cut off the original URL from the first slash, or deal with URLs
  323. without slash */
  324. pathsep = strchr(protsep, '/');
  325. if(pathsep) {
  326. /* When people use badly formatted URLs, such as
  327. "http://www.url.com?dir=/home/daniel" we must not use the first
  328. slash, if there's a ?-letter before it! */
  329. char *sep = strchr(protsep, '?');
  330. if(sep && (sep < pathsep))
  331. pathsep = sep;
  332. *pathsep = 0;
  333. }
  334. else {
  335. /* There was no slash. Now, since we might be operating on a badly
  336. formatted URL, such as "http://www.url.com?id=2380" which doesn't
  337. use a slash separator as it is supposed to, we need to check for a
  338. ?-letter as well! */
  339. pathsep = strchr(protsep, '?');
  340. if(pathsep)
  341. *pathsep = 0;
  342. }
  343. }
  344. }
  345. /* If the new part contains a space, this is a mighty stupid redirect
  346. but we still make an effort to do "right". To the left of a '?'
  347. letter we replace each space with %20 while it is replaced with '+'
  348. on the right side of the '?' letter.
  349. */
  350. newlen = strlen_url(useurl, !host_changed);
  351. urllen = strlen(url_clone);
  352. newest = malloc(urllen + 1 + /* possible slash */
  353. newlen + 1 /* zero byte */);
  354. if(!newest) {
  355. free(url_clone); /* don't leak this */
  356. return NULL;
  357. }
  358. /* copy over the root url part */
  359. memcpy(newest, url_clone, urllen);
  360. /* check if we need to append a slash */
  361. if(('/' == useurl[0]) || (protsep && !*protsep) || ('?' == useurl[0]))
  362. ;
  363. else
  364. newest[urllen++]='/';
  365. /* then append the new piece on the right side */
  366. strcpy_url(&newest[urllen], useurl, !host_changed);
  367. free(url_clone);
  368. return newest;
  369. }
  370. /*
  371. * parse_hostname_login()
  372. *
  373. * Parse the login details (user name, password and options) from the URL and
  374. * strip them out of the host name
  375. *
  376. */
  377. static CURLUcode parse_hostname_login(struct Curl_URL *u,
  378. char **hostname,
  379. unsigned int flags)
  380. {
  381. CURLUcode result = CURLUE_OK;
  382. CURLcode ccode;
  383. char *userp = NULL;
  384. char *passwdp = NULL;
  385. char *optionsp = NULL;
  386. const struct Curl_handler *h = NULL;
  387. /* At this point, we're hoping all the other special cases have
  388. * been taken care of, so conn->host.name is at most
  389. * [user[:password][;options]]@]hostname
  390. *
  391. * We need somewhere to put the embedded details, so do that first.
  392. */
  393. char *ptr = strchr(*hostname, '@');
  394. char *login = *hostname;
  395. if(!ptr)
  396. goto out;
  397. /* We will now try to extract the
  398. * possible login information in a string like:
  399. * ftp://user:password@ftp.my.site:8021/README */
  400. *hostname = ++ptr;
  401. /* if this is a known scheme, get some details */
  402. if(u->scheme)
  403. h = Curl_builtin_scheme(u->scheme);
  404. /* We could use the login information in the URL so extract it. Only parse
  405. options if the handler says we should. Note that 'h' might be NULL! */
  406. ccode = Curl_parse_login_details(login, ptr - login - 1,
  407. &userp, &passwdp,
  408. (h && (h->flags & PROTOPT_URLOPTIONS)) ?
  409. &optionsp:NULL);
  410. if(ccode) {
  411. result = CURLUE_MALFORMED_INPUT;
  412. goto out;
  413. }
  414. if(userp) {
  415. if(flags & CURLU_DISALLOW_USER) {
  416. /* Option DISALLOW_USER is set and url contains username. */
  417. result = CURLUE_USER_NOT_ALLOWED;
  418. goto out;
  419. }
  420. u->user = userp;
  421. }
  422. if(passwdp)
  423. u->password = passwdp;
  424. if(optionsp)
  425. u->options = optionsp;
  426. return CURLUE_OK;
  427. out:
  428. free(userp);
  429. free(passwdp);
  430. free(optionsp);
  431. return result;
  432. }
  433. UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, char *hostname,
  434. bool has_scheme)
  435. {
  436. char *portptr = NULL;
  437. char endbracket;
  438. int len;
  439. /*
  440. * Find the end of an IPv6 address, either on the ']' ending bracket or
  441. * a percent-encoded zone index.
  442. */
  443. if(1 == sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.]%c%n",
  444. &endbracket, &len)) {
  445. if(']' == endbracket)
  446. portptr = &hostname[len];
  447. else if('%' == endbracket) {
  448. int zonelen = len;
  449. if(1 == sscanf(hostname + zonelen, "%*[^]]%c%n", &endbracket, &len)) {
  450. if(']' != endbracket)
  451. return CURLUE_MALFORMED_INPUT;
  452. portptr = &hostname[--zonelen + len + 1];
  453. }
  454. else
  455. return CURLUE_MALFORMED_INPUT;
  456. }
  457. else
  458. return CURLUE_MALFORMED_INPUT;
  459. /* this is a RFC2732-style specified IP-address */
  460. if(portptr && *portptr) {
  461. if(*portptr != ':')
  462. return CURLUE_MALFORMED_INPUT;
  463. }
  464. else
  465. portptr = NULL;
  466. }
  467. else
  468. portptr = strchr(hostname, ':');
  469. if(portptr) {
  470. char *rest;
  471. long port;
  472. char portbuf[7];
  473. /* Browser behavior adaptation. If there's a colon with no digits after,
  474. just cut off the name there which makes us ignore the colon and just
  475. use the default port. Firefox, Chrome and Safari all do that.
  476. Don't do it if the URL has no scheme, to make something that looks like
  477. a scheme not work!
  478. */
  479. if(!portptr[1]) {
  480. *portptr = '\0';
  481. return has_scheme ? CURLUE_OK : CURLUE_BAD_PORT_NUMBER;
  482. }
  483. if(!ISDIGIT(portptr[1]))
  484. return CURLUE_BAD_PORT_NUMBER;
  485. port = strtol(portptr + 1, &rest, 10); /* Port number must be decimal */
  486. if((port <= 0) || (port > 0xffff))
  487. /* Single unix standard says port numbers are 16 bits long, but we don't
  488. treat port zero as OK. */
  489. return CURLUE_BAD_PORT_NUMBER;
  490. if(rest[0])
  491. return CURLUE_BAD_PORT_NUMBER;
  492. *portptr++ = '\0'; /* cut off the name there */
  493. *rest = 0;
  494. /* generate a new port number string to get rid of leading zeroes etc */
  495. msnprintf(portbuf, sizeof(portbuf), "%ld", port);
  496. u->portnum = port;
  497. u->port = strdup(portbuf);
  498. if(!u->port)
  499. return CURLUE_OUT_OF_MEMORY;
  500. }
  501. return CURLUE_OK;
  502. }
  503. /* scan for byte values < 31 or 127 */
  504. static bool junkscan(const char *part, unsigned int flags)
  505. {
  506. if(part) {
  507. static const char badbytes[]={
  508. /* */ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  509. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  510. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  511. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  512. 0x7f, 0x00 /* null-terminate */
  513. };
  514. size_t n = strlen(part);
  515. size_t nfine = strcspn(part, badbytes);
  516. if(nfine != n)
  517. /* since we don't know which part is scanned, return a generic error
  518. code */
  519. return TRUE;
  520. if(!(flags & CURLU_ALLOW_SPACE) && strchr(part, ' '))
  521. return TRUE;
  522. }
  523. return FALSE;
  524. }
  525. static CURLUcode hostname_check(struct Curl_URL *u, char *hostname)
  526. {
  527. size_t len;
  528. size_t hlen = strlen(hostname);
  529. if(hostname[0] == '[') {
  530. #ifdef ENABLE_IPV6
  531. char dest[16]; /* fits a binary IPv6 address */
  532. #endif
  533. const char *l = "0123456789abcdefABCDEF:.";
  534. if(hlen < 4) /* '[::]' is the shortest possible valid string */
  535. return CURLUE_MALFORMED_INPUT;
  536. hostname++;
  537. hlen -= 2;
  538. if(hostname[hlen] != ']')
  539. return CURLUE_MALFORMED_INPUT;
  540. /* only valid letters are ok */
  541. len = strspn(hostname, l);
  542. if(hlen != len) {
  543. hlen = len;
  544. if(hostname[len] == '%') {
  545. /* this could now be '%[zone id]' */
  546. char zoneid[16];
  547. int i = 0;
  548. char *h = &hostname[len + 1];
  549. /* pass '25' if present and is a url encoded percent sign */
  550. if(!strncmp(h, "25", 2) && h[2] && (h[2] != ']'))
  551. h += 2;
  552. while(*h && (*h != ']') && (i < 15))
  553. zoneid[i++] = *h++;
  554. if(!i || (']' != *h))
  555. return CURLUE_MALFORMED_INPUT;
  556. zoneid[i] = 0;
  557. u->zoneid = strdup(zoneid);
  558. if(!u->zoneid)
  559. return CURLUE_OUT_OF_MEMORY;
  560. hostname[len] = ']'; /* insert end bracket */
  561. hostname[len + 1] = 0; /* terminate the hostname */
  562. }
  563. else
  564. return CURLUE_MALFORMED_INPUT;
  565. /* hostname is fine */
  566. }
  567. #ifdef ENABLE_IPV6
  568. hostname[hlen] = 0; /* end the address there */
  569. if(1 != Curl_inet_pton(AF_INET6, hostname, dest))
  570. return CURLUE_MALFORMED_INPUT;
  571. hostname[hlen] = ']'; /* restore ending bracket */
  572. #endif
  573. }
  574. else {
  575. /* letters from the second string is not ok */
  576. len = strcspn(hostname, " ");
  577. if(hlen != len)
  578. /* hostname with bad content */
  579. return CURLUE_MALFORMED_INPUT;
  580. }
  581. if(!hostname[0])
  582. return CURLUE_NO_HOST;
  583. return CURLUE_OK;
  584. }
  585. #define HOSTNAME_END(x) (((x) == '/') || ((x) == '?') || ((x) == '#'))
  586. /*
  587. * Handle partial IPv4 numerical addresses and different bases, like
  588. * '16843009', '0x7f', '0x7f.1' '0177.1.1.1' etc.
  589. *
  590. * If the given input string is syntactically wrong or any part for example is
  591. * too big, this function returns FALSE and doesn't create any output.
  592. *
  593. * Output the "normalized" version of that input string in plain quad decimal
  594. * integers and return TRUE.
  595. */
  596. static bool ipv4_normalize(const char *hostname, char *outp, size_t olen)
  597. {
  598. bool done = FALSE;
  599. int n = 0;
  600. const char *c = hostname;
  601. unsigned long parts[4] = {0, 0, 0, 0};
  602. while(!done) {
  603. char *endp;
  604. unsigned long l;
  605. if((*c < '0') || (*c > '9'))
  606. /* most importantly this doesn't allow a leading plus or minus */
  607. return FALSE;
  608. l = strtoul(c, &endp, 0);
  609. /* overflow or nothing parsed at all */
  610. if(((l == ULONG_MAX) && (errno == ERANGE)) || (endp == c))
  611. return FALSE;
  612. #if SIZEOF_LONG > 4
  613. /* a value larger than 32 bits */
  614. if(l > UINT_MAX)
  615. return FALSE;
  616. #endif
  617. parts[n] = l;
  618. c = endp;
  619. switch (*c) {
  620. case '.' :
  621. if(n == 3)
  622. return FALSE;
  623. n++;
  624. c++;
  625. break;
  626. case '\0':
  627. done = TRUE;
  628. break;
  629. default:
  630. return FALSE;
  631. }
  632. }
  633. /* this is deemed a valid IPv4 numerical address */
  634. switch(n) {
  635. case 0: /* a -- 32 bits */
  636. msnprintf(outp, olen, "%u.%u.%u.%u",
  637. parts[0] >> 24, (parts[0] >> 16) & 0xff,
  638. (parts[0] >> 8) & 0xff, parts[0] & 0xff);
  639. break;
  640. case 1: /* a.b -- 8.24 bits */
  641. if((parts[0] > 0xff) || (parts[1] > 0xffffff))
  642. return FALSE;
  643. msnprintf(outp, olen, "%u.%u.%u.%u",
  644. parts[0], (parts[1] >> 16) & 0xff,
  645. (parts[1] >> 8) & 0xff, parts[1] & 0xff);
  646. break;
  647. case 2: /* a.b.c -- 8.8.16 bits */
  648. if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xffff))
  649. return FALSE;
  650. msnprintf(outp, olen, "%u.%u.%u.%u",
  651. parts[0], parts[1], (parts[2] >> 8) & 0xff,
  652. parts[2] & 0xff);
  653. break;
  654. case 3: /* a.b.c.d -- 8.8.8.8 bits */
  655. if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff) ||
  656. (parts[3] > 0xff))
  657. return FALSE;
  658. msnprintf(outp, olen, "%u.%u.%u.%u",
  659. parts[0], parts[1], parts[2], parts[3]);
  660. break;
  661. }
  662. return TRUE;
  663. }
  664. static CURLUcode seturl(const char *url, CURLU *u, unsigned int flags)
  665. {
  666. char *path;
  667. bool path_alloced = FALSE;
  668. bool uncpath = FALSE;
  669. char *hostname;
  670. char *query = NULL;
  671. char *fragment = NULL;
  672. CURLUcode result;
  673. bool url_has_scheme = FALSE;
  674. char schemebuf[MAX_SCHEME_LEN + 1];
  675. const char *schemep = NULL;
  676. size_t schemelen = 0;
  677. size_t urllen;
  678. DEBUGASSERT(url);
  679. /*************************************************************
  680. * Parse the URL.
  681. ************************************************************/
  682. /* allocate scratch area */
  683. urllen = strlen(url);
  684. if(urllen > CURL_MAX_INPUT_LENGTH)
  685. /* excessive input length */
  686. return CURLUE_MALFORMED_INPUT;
  687. path = u->scratch = malloc(urllen * 2 + 2);
  688. if(!path)
  689. return CURLUE_OUT_OF_MEMORY;
  690. hostname = &path[urllen + 1];
  691. hostname[0] = 0;
  692. if(Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf))) {
  693. url_has_scheme = TRUE;
  694. schemelen = strlen(schemebuf);
  695. }
  696. /* handle the file: scheme */
  697. if(url_has_scheme && strcasecompare(schemebuf, "file")) {
  698. /* path has been allocated large enough to hold this */
  699. strcpy(path, &url[5]);
  700. u->scheme = strdup("file");
  701. if(!u->scheme)
  702. return CURLUE_OUT_OF_MEMORY;
  703. /* Extra handling URLs with an authority component (i.e. that start with
  704. * "file://")
  705. *
  706. * We allow omitted hostname (e.g. file:/<path>) -- valid according to
  707. * RFC 8089, but not the (current) WHAT-WG URL spec.
  708. */
  709. if(path[0] == '/' && path[1] == '/') {
  710. /* swallow the two slashes */
  711. char *ptr = &path[2];
  712. /*
  713. * According to RFC 8089, a file: URL can be reliably dereferenced if:
  714. *
  715. * o it has no/blank hostname, or
  716. *
  717. * o the hostname matches "localhost" (case-insensitively), or
  718. *
  719. * o the hostname is a FQDN that resolves to this machine, or
  720. *
  721. * o it is an UNC String transformed to an URI (Windows only, RFC 8089
  722. * Appendix E.3).
  723. *
  724. * For brevity, we only consider URLs with empty, "localhost", or
  725. * "127.0.0.1" hostnames as local, otherwise as an UNC String.
  726. *
  727. * Additionally, there is an exception for URLs with a Windows drive
  728. * letter in the authority (which was accidentally omitted from RFC 8089
  729. * Appendix E, but believe me, it was meant to be there. --MK)
  730. */
  731. if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
  732. /* the URL includes a host name, it must match "localhost" or
  733. "127.0.0.1" to be valid */
  734. if(checkprefix("localhost/", ptr) ||
  735. checkprefix("127.0.0.1/", ptr)) {
  736. ptr += 9; /* now points to the slash after the host */
  737. }
  738. else {
  739. #if defined(WIN32)
  740. size_t len;
  741. /* the host name, NetBIOS computer name, can not contain disallowed
  742. chars, and the delimiting slash character must be appended to the
  743. host name */
  744. path = strpbrk(ptr, "/\\:*?\"<>|");
  745. if(!path || *path != '/')
  746. return CURLUE_MALFORMED_INPUT;
  747. len = path - ptr;
  748. if(len) {
  749. memcpy(hostname, ptr, len);
  750. hostname[len] = 0;
  751. uncpath = TRUE;
  752. }
  753. ptr -= 2; /* now points to the // before the host in UNC */
  754. #else
  755. /* Invalid file://hostname/, expected localhost or 127.0.0.1 or
  756. none */
  757. return CURLUE_MALFORMED_INPUT;
  758. #endif
  759. }
  760. }
  761. path = ptr;
  762. }
  763. if(!uncpath)
  764. hostname = NULL; /* no host for file: URLs by default */
  765. #if !defined(MSDOS) && !defined(WIN32) && !defined(__CYGWIN__)
  766. /* Don't allow Windows drive letters when not in Windows.
  767. * This catches both "file:/c:" and "file:c:" */
  768. if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
  769. STARTS_WITH_URL_DRIVE_PREFIX(path)) {
  770. /* File drive letters are only accepted in MSDOS/Windows */
  771. return CURLUE_MALFORMED_INPUT;
  772. }
  773. #else
  774. /* If the path starts with a slash and a drive letter, ditch the slash */
  775. if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
  776. /* This cannot be done with strcpy, as the memory chunks overlap! */
  777. memmove(path, &path[1], strlen(&path[1]) + 1);
  778. }
  779. #endif
  780. }
  781. else {
  782. /* clear path */
  783. const char *p;
  784. const char *hostp;
  785. size_t len;
  786. path[0] = 0;
  787. if(url_has_scheme) {
  788. int i = 0;
  789. p = &url[schemelen + 1];
  790. while(p && (*p == '/') && (i < 4)) {
  791. p++;
  792. i++;
  793. }
  794. if((i < 1) || (i>3))
  795. /* less than one or more than three slashes */
  796. return CURLUE_MALFORMED_INPUT;
  797. schemep = schemebuf;
  798. if(!Curl_builtin_scheme(schemep) &&
  799. !(flags & CURLU_NON_SUPPORT_SCHEME))
  800. return CURLUE_UNSUPPORTED_SCHEME;
  801. if(junkscan(schemep, flags))
  802. return CURLUE_MALFORMED_INPUT;
  803. }
  804. else {
  805. /* no scheme! */
  806. if(!(flags & (CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME)))
  807. return CURLUE_MALFORMED_INPUT;
  808. if(flags & CURLU_DEFAULT_SCHEME)
  809. schemep = DEFAULT_SCHEME;
  810. /*
  811. * The URL was badly formatted, let's try without scheme specified.
  812. */
  813. p = url;
  814. }
  815. hostp = p; /* host name starts here */
  816. while(*p && !HOSTNAME_END(*p)) /* find end of host name */
  817. p++;
  818. len = p - hostp;
  819. if(len) {
  820. memcpy(hostname, hostp, len);
  821. hostname[len] = 0;
  822. }
  823. else {
  824. if(!(flags & CURLU_NO_AUTHORITY))
  825. return CURLUE_MALFORMED_INPUT;
  826. }
  827. len = strlen(p);
  828. memcpy(path, p, len);
  829. path[len] = 0;
  830. if(schemep) {
  831. u->scheme = strdup(schemep);
  832. if(!u->scheme)
  833. return CURLUE_OUT_OF_MEMORY;
  834. }
  835. }
  836. if(junkscan(path, flags))
  837. return CURLUE_MALFORMED_INPUT;
  838. if((flags & CURLU_URLENCODE) && path[0]) {
  839. /* worst case output length is 3x the original! */
  840. char *newp = malloc(strlen(path) * 3);
  841. if(!newp)
  842. return CURLUE_OUT_OF_MEMORY;
  843. path_alloced = TRUE;
  844. strcpy_url(newp, path, TRUE); /* consider it relative */
  845. u->temppath = path = newp;
  846. }
  847. fragment = strchr(path, '#');
  848. if(fragment) {
  849. *fragment++ = 0;
  850. if(fragment[0]) {
  851. u->fragment = strdup(fragment);
  852. if(!u->fragment)
  853. return CURLUE_OUT_OF_MEMORY;
  854. }
  855. }
  856. query = strchr(path, '?');
  857. if(query) {
  858. *query++ = 0;
  859. /* done even if the query part is a blank string */
  860. u->query = strdup(query);
  861. if(!u->query)
  862. return CURLUE_OUT_OF_MEMORY;
  863. }
  864. if(!path[0])
  865. /* if there's no path left set, unset */
  866. path = NULL;
  867. else {
  868. if(!(flags & CURLU_PATH_AS_IS)) {
  869. /* remove ../ and ./ sequences according to RFC3986 */
  870. char *newp = Curl_dedotdotify(path);
  871. if(!newp)
  872. return CURLUE_OUT_OF_MEMORY;
  873. if(strcmp(newp, path)) {
  874. /* if we got a new version */
  875. if(path_alloced)
  876. Curl_safefree(u->temppath);
  877. u->temppath = path = newp;
  878. path_alloced = TRUE;
  879. }
  880. else
  881. free(newp);
  882. }
  883. u->path = path_alloced?path:strdup(path);
  884. if(!u->path)
  885. return CURLUE_OUT_OF_MEMORY;
  886. u->temppath = NULL; /* used now */
  887. }
  888. if(hostname) {
  889. char normalized_ipv4[sizeof("255.255.255.255") + 1];
  890. /*
  891. * Parse the login details and strip them out of the host name.
  892. */
  893. if(junkscan(hostname, flags))
  894. return CURLUE_MALFORMED_INPUT;
  895. result = parse_hostname_login(u, &hostname, flags);
  896. if(result)
  897. return result;
  898. result = Curl_parse_port(u, hostname, url_has_scheme);
  899. if(result)
  900. return result;
  901. if(0 == strlen(hostname) && (flags & CURLU_NO_AUTHORITY)) {
  902. /* Skip hostname check, it's allowed to be empty. */
  903. }
  904. else {
  905. result = hostname_check(u, hostname);
  906. if(result)
  907. return result;
  908. }
  909. if(ipv4_normalize(hostname, normalized_ipv4, sizeof(normalized_ipv4)))
  910. u->host = strdup(normalized_ipv4);
  911. else
  912. u->host = strdup(hostname);
  913. if(!u->host)
  914. return CURLUE_OUT_OF_MEMORY;
  915. if((flags & CURLU_GUESS_SCHEME) && !schemep) {
  916. /* legacy curl-style guess based on host name */
  917. if(checkprefix("ftp.", hostname))
  918. schemep = "ftp";
  919. else if(checkprefix("dict.", hostname))
  920. schemep = "dict";
  921. else if(checkprefix("ldap.", hostname))
  922. schemep = "ldap";
  923. else if(checkprefix("imap.", hostname))
  924. schemep = "imap";
  925. else if(checkprefix("smtp.", hostname))
  926. schemep = "smtp";
  927. else if(checkprefix("pop3.", hostname))
  928. schemep = "pop3";
  929. else
  930. schemep = "http";
  931. u->scheme = strdup(schemep);
  932. if(!u->scheme)
  933. return CURLUE_OUT_OF_MEMORY;
  934. }
  935. }
  936. Curl_safefree(u->scratch);
  937. Curl_safefree(u->temppath);
  938. return CURLUE_OK;
  939. }
  940. /*
  941. * Parse the URL and set the relevant members of the Curl_URL struct.
  942. */
  943. static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
  944. {
  945. CURLUcode result = seturl(url, u, flags);
  946. if(result) {
  947. free_urlhandle(u);
  948. memset(u, 0, sizeof(struct Curl_URL));
  949. }
  950. return result;
  951. }
  952. /*
  953. */
  954. CURLU *curl_url(void)
  955. {
  956. return calloc(sizeof(struct Curl_URL), 1);
  957. }
  958. void curl_url_cleanup(CURLU *u)
  959. {
  960. if(u) {
  961. free_urlhandle(u);
  962. free(u);
  963. }
  964. }
  965. #define DUP(dest, src, name) \
  966. do { \
  967. if(src->name) { \
  968. dest->name = strdup(src->name); \
  969. if(!dest->name) \
  970. goto fail; \
  971. } \
  972. } while(0)
  973. CURLU *curl_url_dup(CURLU *in)
  974. {
  975. struct Curl_URL *u = calloc(sizeof(struct Curl_URL), 1);
  976. if(u) {
  977. DUP(u, in, scheme);
  978. DUP(u, in, user);
  979. DUP(u, in, password);
  980. DUP(u, in, options);
  981. DUP(u, in, host);
  982. DUP(u, in, port);
  983. DUP(u, in, path);
  984. DUP(u, in, query);
  985. DUP(u, in, fragment);
  986. u->portnum = in->portnum;
  987. }
  988. return u;
  989. fail:
  990. curl_url_cleanup(u);
  991. return NULL;
  992. }
  993. CURLUcode curl_url_get(CURLU *u, CURLUPart what,
  994. char **part, unsigned int flags)
  995. {
  996. char *ptr;
  997. CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
  998. char portbuf[7];
  999. bool urldecode = (flags & CURLU_URLDECODE)?1:0;
  1000. bool plusdecode = FALSE;
  1001. (void)flags;
  1002. if(!u)
  1003. return CURLUE_BAD_HANDLE;
  1004. if(!part)
  1005. return CURLUE_BAD_PARTPOINTER;
  1006. *part = NULL;
  1007. switch(what) {
  1008. case CURLUPART_SCHEME:
  1009. ptr = u->scheme;
  1010. ifmissing = CURLUE_NO_SCHEME;
  1011. urldecode = FALSE; /* never for schemes */
  1012. break;
  1013. case CURLUPART_USER:
  1014. ptr = u->user;
  1015. ifmissing = CURLUE_NO_USER;
  1016. break;
  1017. case CURLUPART_PASSWORD:
  1018. ptr = u->password;
  1019. ifmissing = CURLUE_NO_PASSWORD;
  1020. break;
  1021. case CURLUPART_OPTIONS:
  1022. ptr = u->options;
  1023. ifmissing = CURLUE_NO_OPTIONS;
  1024. break;
  1025. case CURLUPART_HOST:
  1026. ptr = u->host;
  1027. ifmissing = CURLUE_NO_HOST;
  1028. break;
  1029. case CURLUPART_ZONEID:
  1030. ptr = u->zoneid;
  1031. break;
  1032. case CURLUPART_PORT:
  1033. ptr = u->port;
  1034. ifmissing = CURLUE_NO_PORT;
  1035. urldecode = FALSE; /* never for port */
  1036. if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
  1037. /* there's no stored port number, but asked to deliver
  1038. a default one for the scheme */
  1039. const struct Curl_handler *h =
  1040. Curl_builtin_scheme(u->scheme);
  1041. if(h) {
  1042. msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
  1043. ptr = portbuf;
  1044. }
  1045. }
  1046. else if(ptr && u->scheme) {
  1047. /* there is a stored port number, but ask to inhibit if
  1048. it matches the default one for the scheme */
  1049. const struct Curl_handler *h =
  1050. Curl_builtin_scheme(u->scheme);
  1051. if(h && (h->defport == u->portnum) &&
  1052. (flags & CURLU_NO_DEFAULT_PORT))
  1053. ptr = NULL;
  1054. }
  1055. break;
  1056. case CURLUPART_PATH:
  1057. ptr = u->path;
  1058. if(!ptr) {
  1059. ptr = u->path = strdup("/");
  1060. if(!u->path)
  1061. return CURLUE_OUT_OF_MEMORY;
  1062. }
  1063. break;
  1064. case CURLUPART_QUERY:
  1065. ptr = u->query;
  1066. ifmissing = CURLUE_NO_QUERY;
  1067. plusdecode = urldecode;
  1068. break;
  1069. case CURLUPART_FRAGMENT:
  1070. ptr = u->fragment;
  1071. ifmissing = CURLUE_NO_FRAGMENT;
  1072. break;
  1073. case CURLUPART_URL: {
  1074. char *url;
  1075. char *scheme;
  1076. char *options = u->options;
  1077. char *port = u->port;
  1078. char *allochost = NULL;
  1079. if(u->scheme && strcasecompare("file", u->scheme)) {
  1080. url = aprintf("file://%s%s%s",
  1081. u->path,
  1082. u->fragment? "#": "",
  1083. u->fragment? u->fragment : "");
  1084. }
  1085. else if(!u->host)
  1086. return CURLUE_NO_HOST;
  1087. else {
  1088. const struct Curl_handler *h = NULL;
  1089. if(u->scheme)
  1090. scheme = u->scheme;
  1091. else if(flags & CURLU_DEFAULT_SCHEME)
  1092. scheme = (char *) DEFAULT_SCHEME;
  1093. else
  1094. return CURLUE_NO_SCHEME;
  1095. h = Curl_builtin_scheme(scheme);
  1096. if(!port && (flags & CURLU_DEFAULT_PORT)) {
  1097. /* there's no stored port number, but asked to deliver
  1098. a default one for the scheme */
  1099. if(h) {
  1100. msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
  1101. port = portbuf;
  1102. }
  1103. }
  1104. else if(port) {
  1105. /* there is a stored port number, but asked to inhibit if it matches
  1106. the default one for the scheme */
  1107. if(h && (h->defport == u->portnum) &&
  1108. (flags & CURLU_NO_DEFAULT_PORT))
  1109. port = NULL;
  1110. }
  1111. if(h && !(h->flags & PROTOPT_URLOPTIONS))
  1112. options = NULL;
  1113. if((u->host[0] == '[') && u->zoneid) {
  1114. /* make it '[ host %25 zoneid ]' */
  1115. size_t hostlen = strlen(u->host);
  1116. size_t alen = hostlen + 3 + strlen(u->zoneid) + 1;
  1117. allochost = malloc(alen);
  1118. if(!allochost)
  1119. return CURLUE_OUT_OF_MEMORY;
  1120. memcpy(allochost, u->host, hostlen - 1);
  1121. msnprintf(&allochost[hostlen - 1], alen - hostlen + 1,
  1122. "%%25%s]", u->zoneid);
  1123. }
  1124. url = aprintf("%s://%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1125. scheme,
  1126. u->user ? u->user : "",
  1127. u->password ? ":": "",
  1128. u->password ? u->password : "",
  1129. options ? ";" : "",
  1130. options ? options : "",
  1131. (u->user || u->password || options) ? "@": "",
  1132. allochost ? allochost : u->host,
  1133. port ? ":": "",
  1134. port ? port : "",
  1135. (u->path && (u->path[0] != '/')) ? "/": "",
  1136. u->path ? u->path : "/",
  1137. (u->query && u->query[0]) ? "?": "",
  1138. (u->query && u->query[0]) ? u->query : "",
  1139. u->fragment? "#": "",
  1140. u->fragment? u->fragment : "");
  1141. free(allochost);
  1142. }
  1143. if(!url)
  1144. return CURLUE_OUT_OF_MEMORY;
  1145. *part = url;
  1146. return CURLUE_OK;
  1147. }
  1148. default:
  1149. ptr = NULL;
  1150. break;
  1151. }
  1152. if(ptr) {
  1153. *part = strdup(ptr);
  1154. if(!*part)
  1155. return CURLUE_OUT_OF_MEMORY;
  1156. if(plusdecode) {
  1157. /* convert + to space */
  1158. char *plus;
  1159. for(plus = *part; *plus; ++plus) {
  1160. if(*plus == '+')
  1161. *plus = ' ';
  1162. }
  1163. }
  1164. if(urldecode) {
  1165. char *decoded;
  1166. size_t dlen;
  1167. /* this unconditional rejection of control bytes is documented
  1168. API behavior */
  1169. CURLcode res = Curl_urldecode(NULL, *part, 0, &decoded, &dlen,
  1170. REJECT_CTRL);
  1171. free(*part);
  1172. if(res) {
  1173. *part = NULL;
  1174. return CURLUE_URLDECODE;
  1175. }
  1176. *part = decoded;
  1177. }
  1178. return CURLUE_OK;
  1179. }
  1180. else
  1181. return ifmissing;
  1182. }
  1183. CURLUcode curl_url_set(CURLU *u, CURLUPart what,
  1184. const char *part, unsigned int flags)
  1185. {
  1186. char **storep = NULL;
  1187. long port = 0;
  1188. bool urlencode = (flags & CURLU_URLENCODE)? 1 : 0;
  1189. bool plusencode = FALSE;
  1190. bool urlskipslash = FALSE;
  1191. bool appendquery = FALSE;
  1192. bool equalsencode = FALSE;
  1193. if(!u)
  1194. return CURLUE_BAD_HANDLE;
  1195. if(!part) {
  1196. /* setting a part to NULL clears it */
  1197. switch(what) {
  1198. case CURLUPART_URL:
  1199. break;
  1200. case CURLUPART_SCHEME:
  1201. storep = &u->scheme;
  1202. break;
  1203. case CURLUPART_USER:
  1204. storep = &u->user;
  1205. break;
  1206. case CURLUPART_PASSWORD:
  1207. storep = &u->password;
  1208. break;
  1209. case CURLUPART_OPTIONS:
  1210. storep = &u->options;
  1211. break;
  1212. case CURLUPART_HOST:
  1213. storep = &u->host;
  1214. break;
  1215. case CURLUPART_ZONEID:
  1216. storep = &u->zoneid;
  1217. break;
  1218. case CURLUPART_PORT:
  1219. u->portnum = 0;
  1220. storep = &u->port;
  1221. break;
  1222. case CURLUPART_PATH:
  1223. storep = &u->path;
  1224. break;
  1225. case CURLUPART_QUERY:
  1226. storep = &u->query;
  1227. break;
  1228. case CURLUPART_FRAGMENT:
  1229. storep = &u->fragment;
  1230. break;
  1231. default:
  1232. return CURLUE_UNKNOWN_PART;
  1233. }
  1234. if(storep && *storep) {
  1235. Curl_safefree(*storep);
  1236. }
  1237. return CURLUE_OK;
  1238. }
  1239. switch(what) {
  1240. case CURLUPART_SCHEME:
  1241. if(strlen(part) > MAX_SCHEME_LEN)
  1242. /* too long */
  1243. return CURLUE_MALFORMED_INPUT;
  1244. if(!(flags & CURLU_NON_SUPPORT_SCHEME) &&
  1245. /* verify that it is a fine scheme */
  1246. !Curl_builtin_scheme(part))
  1247. return CURLUE_UNSUPPORTED_SCHEME;
  1248. storep = &u->scheme;
  1249. urlencode = FALSE; /* never */
  1250. break;
  1251. case CURLUPART_USER:
  1252. storep = &u->user;
  1253. break;
  1254. case CURLUPART_PASSWORD:
  1255. storep = &u->password;
  1256. break;
  1257. case CURLUPART_OPTIONS:
  1258. storep = &u->options;
  1259. break;
  1260. case CURLUPART_HOST:
  1261. storep = &u->host;
  1262. Curl_safefree(u->zoneid);
  1263. break;
  1264. case CURLUPART_ZONEID:
  1265. storep = &u->zoneid;
  1266. break;
  1267. case CURLUPART_PORT:
  1268. {
  1269. char *endp;
  1270. urlencode = FALSE; /* never */
  1271. port = strtol(part, &endp, 10); /* Port number must be decimal */
  1272. if((port <= 0) || (port > 0xffff))
  1273. return CURLUE_BAD_PORT_NUMBER;
  1274. if(*endp)
  1275. /* weirdly provided number, not good! */
  1276. return CURLUE_MALFORMED_INPUT;
  1277. storep = &u->port;
  1278. }
  1279. break;
  1280. case CURLUPART_PATH:
  1281. urlskipslash = TRUE;
  1282. storep = &u->path;
  1283. break;
  1284. case CURLUPART_QUERY:
  1285. plusencode = urlencode;
  1286. appendquery = (flags & CURLU_APPENDQUERY)?1:0;
  1287. equalsencode = appendquery;
  1288. storep = &u->query;
  1289. break;
  1290. case CURLUPART_FRAGMENT:
  1291. storep = &u->fragment;
  1292. break;
  1293. case CURLUPART_URL: {
  1294. /*
  1295. * Allow a new URL to replace the existing (if any) contents.
  1296. *
  1297. * If the existing contents is enough for a URL, allow a relative URL to
  1298. * replace it.
  1299. */
  1300. CURLUcode result;
  1301. char *oldurl;
  1302. char *redired_url;
  1303. CURLU *handle2;
  1304. if(Curl_is_absolute_url(part, NULL, MAX_SCHEME_LEN + 1)) {
  1305. handle2 = curl_url();
  1306. if(!handle2)
  1307. return CURLUE_OUT_OF_MEMORY;
  1308. result = parseurl(part, handle2, flags);
  1309. if(!result)
  1310. mv_urlhandle(handle2, u);
  1311. else
  1312. curl_url_cleanup(handle2);
  1313. return result;
  1314. }
  1315. /* extract the full "old" URL to do the redirect on */
  1316. result = curl_url_get(u, CURLUPART_URL, &oldurl, flags);
  1317. if(result) {
  1318. /* couldn't get the old URL, just use the new! */
  1319. handle2 = curl_url();
  1320. if(!handle2)
  1321. return CURLUE_OUT_OF_MEMORY;
  1322. result = parseurl(part, handle2, flags);
  1323. if(!result)
  1324. mv_urlhandle(handle2, u);
  1325. else
  1326. curl_url_cleanup(handle2);
  1327. return result;
  1328. }
  1329. /* apply the relative part to create a new URL */
  1330. redired_url = concat_url(oldurl, part);
  1331. free(oldurl);
  1332. if(!redired_url)
  1333. return CURLUE_OUT_OF_MEMORY;
  1334. /* now parse the new URL */
  1335. handle2 = curl_url();
  1336. if(!handle2) {
  1337. free(redired_url);
  1338. return CURLUE_OUT_OF_MEMORY;
  1339. }
  1340. result = parseurl(redired_url, handle2, flags);
  1341. free(redired_url);
  1342. if(!result)
  1343. mv_urlhandle(handle2, u);
  1344. else
  1345. curl_url_cleanup(handle2);
  1346. return result;
  1347. }
  1348. default:
  1349. return CURLUE_UNKNOWN_PART;
  1350. }
  1351. DEBUGASSERT(storep);
  1352. {
  1353. const char *newp = part;
  1354. size_t nalloc = strlen(part);
  1355. if(nalloc > CURL_MAX_INPUT_LENGTH)
  1356. /* excessive input length */
  1357. return CURLUE_MALFORMED_INPUT;
  1358. if(urlencode) {
  1359. const unsigned char *i;
  1360. char *o;
  1361. char *enc = malloc(nalloc * 3 + 1); /* for worst case! */
  1362. if(!enc)
  1363. return CURLUE_OUT_OF_MEMORY;
  1364. for(i = (const unsigned char *)part, o = enc; *i; i++) {
  1365. if((*i == ' ') && plusencode) {
  1366. *o = '+';
  1367. o++;
  1368. }
  1369. else if(Curl_isunreserved(*i) ||
  1370. ((*i == '/') && urlskipslash) ||
  1371. ((*i == '=') && equalsencode)) {
  1372. if((*i == '=') && equalsencode)
  1373. /* only skip the first equals sign */
  1374. equalsencode = FALSE;
  1375. *o = *i;
  1376. o++;
  1377. }
  1378. else {
  1379. msnprintf(o, 4, "%%%02x", *i);
  1380. o += 3;
  1381. }
  1382. }
  1383. *o = 0; /* null-terminate */
  1384. newp = enc;
  1385. }
  1386. else {
  1387. char *p;
  1388. newp = strdup(part);
  1389. if(!newp)
  1390. return CURLUE_OUT_OF_MEMORY;
  1391. p = (char *)newp;
  1392. while(*p) {
  1393. /* make sure percent encoded are lower case */
  1394. if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
  1395. (ISUPPER(p[1]) || ISUPPER(p[2]))) {
  1396. p[1] = (char)TOLOWER(p[1]);
  1397. p[2] = (char)TOLOWER(p[2]);
  1398. p += 3;
  1399. }
  1400. else
  1401. p++;
  1402. }
  1403. }
  1404. if(appendquery) {
  1405. /* Append the string onto the old query. Add a '&' separator if none is
  1406. present at the end of the exsting query already */
  1407. size_t querylen = u->query ? strlen(u->query) : 0;
  1408. bool addamperand = querylen && (u->query[querylen -1] != '&');
  1409. if(querylen) {
  1410. size_t newplen = strlen(newp);
  1411. char *p = malloc(querylen + addamperand + newplen + 1);
  1412. if(!p) {
  1413. free((char *)newp);
  1414. return CURLUE_OUT_OF_MEMORY;
  1415. }
  1416. strcpy(p, u->query); /* original query */
  1417. if(addamperand)
  1418. p[querylen] = '&'; /* ampersand */
  1419. strcpy(&p[querylen + addamperand], newp); /* new suffix */
  1420. free((char *)newp);
  1421. free(*storep);
  1422. *storep = p;
  1423. return CURLUE_OK;
  1424. }
  1425. }
  1426. if(what == CURLUPART_HOST) {
  1427. if(0 == strlen(newp) && (flags & CURLU_NO_AUTHORITY)) {
  1428. /* Skip hostname check, it's allowed to be empty. */
  1429. }
  1430. else {
  1431. if(hostname_check(u, (char *)newp)) {
  1432. free((char *)newp);
  1433. return CURLUE_MALFORMED_INPUT;
  1434. }
  1435. }
  1436. }
  1437. free(*storep);
  1438. *storep = (char *)newp;
  1439. }
  1440. /* set after the string, to make it not assigned if the allocation above
  1441. fails */
  1442. if(port)
  1443. u->portnum = port;
  1444. return CURLUE_OK;
  1445. }