urlapi.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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; /* zero 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. {
  435. char *portptr = NULL;
  436. char endbracket;
  437. int len;
  438. /*
  439. * Find the end of an IPv6 address, either on the ']' ending bracket or
  440. * a percent-encoded zone index.
  441. */
  442. if(1 == sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.]%c%n",
  443. &endbracket, &len)) {
  444. if(']' == endbracket)
  445. portptr = &hostname[len];
  446. else if('%' == endbracket) {
  447. int zonelen = len;
  448. if(1 == sscanf(hostname + zonelen, "%*[^]]%c%n", &endbracket, &len)) {
  449. if(']' != endbracket)
  450. return CURLUE_MALFORMED_INPUT;
  451. portptr = &hostname[--zonelen + len + 1];
  452. }
  453. else
  454. return CURLUE_MALFORMED_INPUT;
  455. }
  456. else
  457. return CURLUE_MALFORMED_INPUT;
  458. /* this is a RFC2732-style specified IP-address */
  459. if(portptr && *portptr) {
  460. if(*portptr != ':')
  461. return CURLUE_MALFORMED_INPUT;
  462. }
  463. else
  464. portptr = NULL;
  465. }
  466. else
  467. portptr = strchr(hostname, ':');
  468. if(portptr) {
  469. char *rest;
  470. long port;
  471. char portbuf[7];
  472. /* Browser behavior adaptation. If there's a colon with no digits after,
  473. just cut off the name there which makes us ignore the colon and just
  474. use the default port. Firefox, Chrome and Safari all do that. */
  475. if(!portptr[1]) {
  476. *portptr = '\0';
  477. return CURLUE_OK;
  478. }
  479. if(!ISDIGIT(portptr[1]))
  480. return CURLUE_BAD_PORT_NUMBER;
  481. port = strtol(portptr + 1, &rest, 10); /* Port number must be decimal */
  482. if((port <= 0) || (port > 0xffff))
  483. /* Single unix standard says port numbers are 16 bits long, but we don't
  484. treat port zero as OK. */
  485. return CURLUE_BAD_PORT_NUMBER;
  486. if(rest[0])
  487. return CURLUE_BAD_PORT_NUMBER;
  488. *portptr++ = '\0'; /* cut off the name there */
  489. *rest = 0;
  490. /* generate a new port number string to get rid of leading zeroes etc */
  491. msnprintf(portbuf, sizeof(portbuf), "%ld", port);
  492. u->portnum = port;
  493. u->port = strdup(portbuf);
  494. if(!u->port)
  495. return CURLUE_OUT_OF_MEMORY;
  496. }
  497. return CURLUE_OK;
  498. }
  499. /* scan for byte values < 31 or 127 */
  500. static CURLUcode junkscan(const char *part)
  501. {
  502. if(part) {
  503. static const char badbytes[]={
  504. /* */ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  505. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  506. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  507. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  508. 0x7f,
  509. 0x00 /* zero terminate */
  510. };
  511. size_t n = strlen(part);
  512. size_t nfine = strcspn(part, badbytes);
  513. if(nfine != n)
  514. /* since we don't know which part is scanned, return a generic error
  515. code */
  516. return CURLUE_MALFORMED_INPUT;
  517. }
  518. return CURLUE_OK;
  519. }
  520. static CURLUcode hostname_check(struct Curl_URL *u, char *hostname)
  521. {
  522. size_t len;
  523. size_t hlen = strlen(hostname);
  524. if(hostname[0] == '[') {
  525. #ifdef ENABLE_IPV6
  526. char dest[16]; /* fits a binary IPv6 address */
  527. #endif
  528. const char *l = "0123456789abcdefABCDEF:.";
  529. if(hlen < 4) /* '[::]' is the shortest possible valid string */
  530. return CURLUE_MALFORMED_INPUT;
  531. hostname++;
  532. hlen -= 2;
  533. if(hostname[hlen] != ']')
  534. return CURLUE_MALFORMED_INPUT;
  535. /* only valid letters are ok */
  536. len = strspn(hostname, l);
  537. if(hlen != len) {
  538. hlen = len;
  539. if(hostname[len] == '%') {
  540. /* this could now be '%[zone id]' */
  541. char zoneid[16];
  542. int i = 0;
  543. char *h = &hostname[len + 1];
  544. /* pass '25' if present and is a url encoded percent sign */
  545. if(!strncmp(h, "25", 2) && h[2] && (h[2] != ']'))
  546. h += 2;
  547. while(*h && (*h != ']') && (i < 15))
  548. zoneid[i++] = *h++;
  549. if(!i || (']' != *h))
  550. return CURLUE_MALFORMED_INPUT;
  551. zoneid[i] = 0;
  552. u->zoneid = strdup(zoneid);
  553. if(!u->zoneid)
  554. return CURLUE_OUT_OF_MEMORY;
  555. hostname[len] = ']'; /* insert end bracket */
  556. hostname[len + 1] = 0; /* terminate the hostname */
  557. }
  558. else
  559. return CURLUE_MALFORMED_INPUT;
  560. /* hostname is fine */
  561. }
  562. #ifdef ENABLE_IPV6
  563. hostname[hlen] = 0; /* end the address there */
  564. if(1 != Curl_inet_pton(AF_INET6, hostname, dest))
  565. return CURLUE_MALFORMED_INPUT;
  566. hostname[hlen] = ']'; /* restore ending bracket */
  567. #endif
  568. }
  569. else {
  570. /* letters from the second string is not ok */
  571. len = strcspn(hostname, " ");
  572. if(hlen != len)
  573. /* hostname with bad content */
  574. return CURLUE_MALFORMED_INPUT;
  575. }
  576. if(!hostname[0])
  577. return CURLUE_NO_HOST;
  578. return CURLUE_OK;
  579. }
  580. #define HOSTNAME_END(x) (((x) == '/') || ((x) == '?') || ((x) == '#'))
  581. static CURLUcode seturl(const char *url, CURLU *u, unsigned int flags)
  582. {
  583. char *path;
  584. bool path_alloced = FALSE;
  585. char *hostname;
  586. char *query = NULL;
  587. char *fragment = NULL;
  588. CURLUcode result;
  589. bool url_has_scheme = FALSE;
  590. char schemebuf[MAX_SCHEME_LEN + 1];
  591. const char *schemep = NULL;
  592. size_t schemelen = 0;
  593. size_t urllen;
  594. if(!url)
  595. return CURLUE_MALFORMED_INPUT;
  596. /*************************************************************
  597. * Parse the URL.
  598. ************************************************************/
  599. /* allocate scratch area */
  600. urllen = strlen(url);
  601. if(urllen > CURL_MAX_INPUT_LENGTH)
  602. /* excessive input length */
  603. return CURLUE_MALFORMED_INPUT;
  604. path = u->scratch = malloc(urllen * 2 + 2);
  605. if(!path)
  606. return CURLUE_OUT_OF_MEMORY;
  607. hostname = &path[urllen + 1];
  608. hostname[0] = 0;
  609. if(Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf))) {
  610. url_has_scheme = TRUE;
  611. schemelen = strlen(schemebuf);
  612. }
  613. /* handle the file: scheme */
  614. if(url_has_scheme && strcasecompare(schemebuf, "file")) {
  615. /* path has been allocated large enough to hold this */
  616. strcpy(path, &url[5]);
  617. hostname = NULL; /* no host for file: URLs */
  618. u->scheme = strdup("file");
  619. if(!u->scheme)
  620. return CURLUE_OUT_OF_MEMORY;
  621. /* Extra handling URLs with an authority component (i.e. that start with
  622. * "file://")
  623. *
  624. * We allow omitted hostname (e.g. file:/<path>) -- valid according to
  625. * RFC 8089, but not the (current) WHAT-WG URL spec.
  626. */
  627. if(path[0] == '/' && path[1] == '/') {
  628. /* swallow the two slashes */
  629. char *ptr = &path[2];
  630. /*
  631. * According to RFC 8089, a file: URL can be reliably dereferenced if:
  632. *
  633. * o it has no/blank hostname, or
  634. *
  635. * o the hostname matches "localhost" (case-insensitively), or
  636. *
  637. * o the hostname is a FQDN that resolves to this machine.
  638. *
  639. * For brevity, we only consider URLs with empty, "localhost", or
  640. * "127.0.0.1" hostnames as local.
  641. *
  642. * Additionally, there is an exception for URLs with a Windows drive
  643. * letter in the authority (which was accidentally omitted from RFC 8089
  644. * Appendix E, but believe me, it was meant to be there. --MK)
  645. */
  646. if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
  647. /* the URL includes a host name, it must match "localhost" or
  648. "127.0.0.1" to be valid */
  649. if(!checkprefix("localhost/", ptr) &&
  650. !checkprefix("127.0.0.1/", ptr)) {
  651. /* Invalid file://hostname/, expected localhost or 127.0.0.1 or
  652. none */
  653. return CURLUE_MALFORMED_INPUT;
  654. }
  655. ptr += 9; /* now points to the slash after the host */
  656. }
  657. path = ptr;
  658. }
  659. #if !defined(MSDOS) && !defined(WIN32) && !defined(__CYGWIN__)
  660. /* Don't allow Windows drive letters when not in Windows.
  661. * This catches both "file:/c:" and "file:c:" */
  662. if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
  663. STARTS_WITH_URL_DRIVE_PREFIX(path)) {
  664. /* File drive letters are only accepted in MSDOS/Windows */
  665. return CURLUE_MALFORMED_INPUT;
  666. }
  667. #else
  668. /* If the path starts with a slash and a drive letter, ditch the slash */
  669. if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
  670. /* This cannot be done with strcpy, as the memory chunks overlap! */
  671. memmove(path, &path[1], strlen(&path[1]) + 1);
  672. }
  673. #endif
  674. }
  675. else {
  676. /* clear path */
  677. const char *p;
  678. const char *hostp;
  679. size_t len;
  680. path[0] = 0;
  681. if(url_has_scheme) {
  682. int i = 0;
  683. p = &url[schemelen + 1];
  684. while(p && (*p == '/') && (i < 4)) {
  685. p++;
  686. i++;
  687. }
  688. if((i < 1) || (i>3))
  689. /* less than one or more than three slashes */
  690. return CURLUE_MALFORMED_INPUT;
  691. schemep = schemebuf;
  692. if(!Curl_builtin_scheme(schemep) &&
  693. !(flags & CURLU_NON_SUPPORT_SCHEME))
  694. return CURLUE_UNSUPPORTED_SCHEME;
  695. if(junkscan(schemep))
  696. return CURLUE_MALFORMED_INPUT;
  697. }
  698. else {
  699. /* no scheme! */
  700. if(!(flags & (CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME)))
  701. return CURLUE_MALFORMED_INPUT;
  702. if(flags & CURLU_DEFAULT_SCHEME)
  703. schemep = DEFAULT_SCHEME;
  704. /*
  705. * The URL was badly formatted, let's try without scheme specified.
  706. */
  707. p = url;
  708. }
  709. hostp = p; /* host name starts here */
  710. while(*p && !HOSTNAME_END(*p)) /* find end of host name */
  711. p++;
  712. len = p - hostp;
  713. if(len) {
  714. memcpy(hostname, hostp, len);
  715. hostname[len] = 0;
  716. }
  717. else {
  718. if(!(flags & CURLU_NO_AUTHORITY))
  719. return CURLUE_MALFORMED_INPUT;
  720. }
  721. len = strlen(p);
  722. memcpy(path, p, len);
  723. path[len] = 0;
  724. if(schemep) {
  725. u->scheme = strdup(schemep);
  726. if(!u->scheme)
  727. return CURLUE_OUT_OF_MEMORY;
  728. }
  729. }
  730. if(junkscan(path))
  731. return CURLUE_MALFORMED_INPUT;
  732. if((flags & CURLU_URLENCODE) && path[0]) {
  733. /* worst case output length is 3x the original! */
  734. char *newp = malloc(strlen(path) * 3);
  735. if(!newp)
  736. return CURLUE_OUT_OF_MEMORY;
  737. path_alloced = TRUE;
  738. strcpy_url(newp, path, TRUE); /* consider it relative */
  739. u->temppath = path = newp;
  740. }
  741. fragment = strchr(path, '#');
  742. if(fragment) {
  743. *fragment++ = 0;
  744. if(fragment[0]) {
  745. u->fragment = strdup(fragment);
  746. if(!u->fragment)
  747. return CURLUE_OUT_OF_MEMORY;
  748. }
  749. }
  750. query = strchr(path, '?');
  751. if(query) {
  752. *query++ = 0;
  753. /* done even if the query part is a blank string */
  754. u->query = strdup(query);
  755. if(!u->query)
  756. return CURLUE_OUT_OF_MEMORY;
  757. }
  758. if(!path[0])
  759. /* if there's no path left set, unset */
  760. path = NULL;
  761. else {
  762. if(!(flags & CURLU_PATH_AS_IS)) {
  763. /* remove ../ and ./ sequences according to RFC3986 */
  764. char *newp = Curl_dedotdotify(path);
  765. if(!newp)
  766. return CURLUE_OUT_OF_MEMORY;
  767. if(strcmp(newp, path)) {
  768. /* if we got a new version */
  769. if(path_alloced)
  770. Curl_safefree(u->temppath);
  771. u->temppath = path = newp;
  772. path_alloced = TRUE;
  773. }
  774. else
  775. free(newp);
  776. }
  777. u->path = path_alloced?path:strdup(path);
  778. if(!u->path)
  779. return CURLUE_OUT_OF_MEMORY;
  780. u->temppath = NULL; /* used now */
  781. }
  782. if(hostname) {
  783. /*
  784. * Parse the login details and strip them out of the host name.
  785. */
  786. if(junkscan(hostname))
  787. return CURLUE_MALFORMED_INPUT;
  788. result = parse_hostname_login(u, &hostname, flags);
  789. if(result)
  790. return result;
  791. result = Curl_parse_port(u, hostname);
  792. if(result)
  793. return result;
  794. if(0 == strlen(hostname) && (flags & CURLU_NO_AUTHORITY)) {
  795. /* Skip hostname check, it's allowed to be empty. */
  796. }
  797. else {
  798. result = hostname_check(u, hostname);
  799. if(result)
  800. return result;
  801. }
  802. u->host = strdup(hostname);
  803. if(!u->host)
  804. return CURLUE_OUT_OF_MEMORY;
  805. if((flags & CURLU_GUESS_SCHEME) && !schemep) {
  806. /* legacy curl-style guess based on host name */
  807. if(checkprefix("ftp.", hostname))
  808. schemep = "ftp";
  809. else if(checkprefix("dict.", hostname))
  810. schemep = "dict";
  811. else if(checkprefix("ldap.", hostname))
  812. schemep = "ldap";
  813. else if(checkprefix("imap.", hostname))
  814. schemep = "imap";
  815. else if(checkprefix("smtp.", hostname))
  816. schemep = "smtp";
  817. else if(checkprefix("pop3.", hostname))
  818. schemep = "pop3";
  819. else
  820. schemep = "http";
  821. u->scheme = strdup(schemep);
  822. if(!u->scheme)
  823. return CURLUE_OUT_OF_MEMORY;
  824. }
  825. }
  826. Curl_safefree(u->scratch);
  827. Curl_safefree(u->temppath);
  828. return CURLUE_OK;
  829. }
  830. /*
  831. * Parse the URL and set the relevant members of the Curl_URL struct.
  832. */
  833. static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
  834. {
  835. CURLUcode result = seturl(url, u, flags);
  836. if(result) {
  837. free_urlhandle(u);
  838. memset(u, 0, sizeof(struct Curl_URL));
  839. }
  840. return result;
  841. }
  842. /*
  843. */
  844. CURLU *curl_url(void)
  845. {
  846. return calloc(sizeof(struct Curl_URL), 1);
  847. }
  848. void curl_url_cleanup(CURLU *u)
  849. {
  850. if(u) {
  851. free_urlhandle(u);
  852. free(u);
  853. }
  854. }
  855. #define DUP(dest, src, name) \
  856. if(src->name) { \
  857. dest->name = strdup(src->name); \
  858. if(!dest->name) \
  859. goto fail; \
  860. }
  861. CURLU *curl_url_dup(CURLU *in)
  862. {
  863. struct Curl_URL *u = calloc(sizeof(struct Curl_URL), 1);
  864. if(u) {
  865. DUP(u, in, scheme);
  866. DUP(u, in, user);
  867. DUP(u, in, password);
  868. DUP(u, in, options);
  869. DUP(u, in, host);
  870. DUP(u, in, port);
  871. DUP(u, in, path);
  872. DUP(u, in, query);
  873. DUP(u, in, fragment);
  874. u->portnum = in->portnum;
  875. }
  876. return u;
  877. fail:
  878. curl_url_cleanup(u);
  879. return NULL;
  880. }
  881. CURLUcode curl_url_get(CURLU *u, CURLUPart what,
  882. char **part, unsigned int flags)
  883. {
  884. char *ptr;
  885. CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
  886. char portbuf[7];
  887. bool urldecode = (flags & CURLU_URLDECODE)?1:0;
  888. bool plusdecode = FALSE;
  889. (void)flags;
  890. if(!u)
  891. return CURLUE_BAD_HANDLE;
  892. if(!part)
  893. return CURLUE_BAD_PARTPOINTER;
  894. *part = NULL;
  895. switch(what) {
  896. case CURLUPART_SCHEME:
  897. ptr = u->scheme;
  898. ifmissing = CURLUE_NO_SCHEME;
  899. urldecode = FALSE; /* never for schemes */
  900. break;
  901. case CURLUPART_USER:
  902. ptr = u->user;
  903. ifmissing = CURLUE_NO_USER;
  904. break;
  905. case CURLUPART_PASSWORD:
  906. ptr = u->password;
  907. ifmissing = CURLUE_NO_PASSWORD;
  908. break;
  909. case CURLUPART_OPTIONS:
  910. ptr = u->options;
  911. ifmissing = CURLUE_NO_OPTIONS;
  912. break;
  913. case CURLUPART_HOST:
  914. ptr = u->host;
  915. ifmissing = CURLUE_NO_HOST;
  916. break;
  917. case CURLUPART_ZONEID:
  918. ptr = u->zoneid;
  919. break;
  920. case CURLUPART_PORT:
  921. ptr = u->port;
  922. ifmissing = CURLUE_NO_PORT;
  923. urldecode = FALSE; /* never for port */
  924. if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
  925. /* there's no stored port number, but asked to deliver
  926. a default one for the scheme */
  927. const struct Curl_handler *h =
  928. Curl_builtin_scheme(u->scheme);
  929. if(h) {
  930. msnprintf(portbuf, sizeof(portbuf), "%ld", h->defport);
  931. ptr = portbuf;
  932. }
  933. }
  934. else if(ptr && u->scheme) {
  935. /* there is a stored port number, but ask to inhibit if
  936. it matches the default one for the scheme */
  937. const struct Curl_handler *h =
  938. Curl_builtin_scheme(u->scheme);
  939. if(h && (h->defport == u->portnum) &&
  940. (flags & CURLU_NO_DEFAULT_PORT))
  941. ptr = NULL;
  942. }
  943. break;
  944. case CURLUPART_PATH:
  945. ptr = u->path;
  946. if(!ptr) {
  947. ptr = u->path = strdup("/");
  948. if(!u->path)
  949. return CURLUE_OUT_OF_MEMORY;
  950. }
  951. break;
  952. case CURLUPART_QUERY:
  953. ptr = u->query;
  954. ifmissing = CURLUE_NO_QUERY;
  955. plusdecode = urldecode;
  956. break;
  957. case CURLUPART_FRAGMENT:
  958. ptr = u->fragment;
  959. ifmissing = CURLUE_NO_FRAGMENT;
  960. break;
  961. case CURLUPART_URL: {
  962. char *url;
  963. char *scheme;
  964. char *options = u->options;
  965. char *port = u->port;
  966. char *allochost = NULL;
  967. if(u->scheme && strcasecompare("file", u->scheme)) {
  968. url = aprintf("file://%s%s%s",
  969. u->path,
  970. u->fragment? "#": "",
  971. u->fragment? u->fragment : "");
  972. }
  973. else if(!u->host)
  974. return CURLUE_NO_HOST;
  975. else {
  976. const struct Curl_handler *h = NULL;
  977. if(u->scheme)
  978. scheme = u->scheme;
  979. else if(flags & CURLU_DEFAULT_SCHEME)
  980. scheme = (char *) DEFAULT_SCHEME;
  981. else
  982. return CURLUE_NO_SCHEME;
  983. h = Curl_builtin_scheme(scheme);
  984. if(!port && (flags & CURLU_DEFAULT_PORT)) {
  985. /* there's no stored port number, but asked to deliver
  986. a default one for the scheme */
  987. if(h) {
  988. msnprintf(portbuf, sizeof(portbuf), "%ld", h->defport);
  989. port = portbuf;
  990. }
  991. }
  992. else if(port) {
  993. /* there is a stored port number, but asked to inhibit if it matches
  994. the default one for the scheme */
  995. if(h && (h->defport == u->portnum) &&
  996. (flags & CURLU_NO_DEFAULT_PORT))
  997. port = NULL;
  998. }
  999. if(h && !(h->flags & PROTOPT_URLOPTIONS))
  1000. options = NULL;
  1001. if((u->host[0] == '[') && u->zoneid) {
  1002. /* make it '[ host %25 zoneid ]' */
  1003. size_t hostlen = strlen(u->host);
  1004. size_t alen = hostlen + 3 + strlen(u->zoneid) + 1;
  1005. allochost = malloc(alen);
  1006. if(!allochost)
  1007. return CURLUE_OUT_OF_MEMORY;
  1008. memcpy(allochost, u->host, hostlen - 1);
  1009. msnprintf(&allochost[hostlen - 1], alen - hostlen + 1,
  1010. "%%25%s]", u->zoneid);
  1011. }
  1012. url = aprintf("%s://%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1013. scheme,
  1014. u->user ? u->user : "",
  1015. u->password ? ":": "",
  1016. u->password ? u->password : "",
  1017. options ? ";" : "",
  1018. options ? options : "",
  1019. (u->user || u->password || options) ? "@": "",
  1020. allochost ? allochost : u->host,
  1021. port ? ":": "",
  1022. port ? port : "",
  1023. (u->path && (u->path[0] != '/')) ? "/": "",
  1024. u->path ? u->path : "/",
  1025. (u->query && u->query[0]) ? "?": "",
  1026. (u->query && u->query[0]) ? u->query : "",
  1027. u->fragment? "#": "",
  1028. u->fragment? u->fragment : "");
  1029. free(allochost);
  1030. }
  1031. if(!url)
  1032. return CURLUE_OUT_OF_MEMORY;
  1033. *part = url;
  1034. return CURLUE_OK;
  1035. }
  1036. default:
  1037. ptr = NULL;
  1038. break;
  1039. }
  1040. if(ptr) {
  1041. *part = strdup(ptr);
  1042. if(!*part)
  1043. return CURLUE_OUT_OF_MEMORY;
  1044. if(plusdecode) {
  1045. /* convert + to space */
  1046. char *plus;
  1047. for(plus = *part; *plus; ++plus) {
  1048. if(*plus == '+')
  1049. *plus = ' ';
  1050. }
  1051. }
  1052. if(urldecode) {
  1053. char *decoded;
  1054. size_t dlen;
  1055. CURLcode res = Curl_urldecode(NULL, *part, 0, &decoded, &dlen, TRUE);
  1056. free(*part);
  1057. if(res) {
  1058. *part = NULL;
  1059. return CURLUE_URLDECODE;
  1060. }
  1061. *part = decoded;
  1062. }
  1063. return CURLUE_OK;
  1064. }
  1065. else
  1066. return ifmissing;
  1067. }
  1068. CURLUcode curl_url_set(CURLU *u, CURLUPart what,
  1069. const char *part, unsigned int flags)
  1070. {
  1071. char **storep = NULL;
  1072. long port = 0;
  1073. bool urlencode = (flags & CURLU_URLENCODE)? 1 : 0;
  1074. bool plusencode = FALSE;
  1075. bool urlskipslash = FALSE;
  1076. bool appendquery = FALSE;
  1077. bool equalsencode = FALSE;
  1078. if(!u)
  1079. return CURLUE_BAD_HANDLE;
  1080. if(!part) {
  1081. /* setting a part to NULL clears it */
  1082. switch(what) {
  1083. case CURLUPART_URL:
  1084. break;
  1085. case CURLUPART_SCHEME:
  1086. storep = &u->scheme;
  1087. break;
  1088. case CURLUPART_USER:
  1089. storep = &u->user;
  1090. break;
  1091. case CURLUPART_PASSWORD:
  1092. storep = &u->password;
  1093. break;
  1094. case CURLUPART_OPTIONS:
  1095. storep = &u->options;
  1096. break;
  1097. case CURLUPART_HOST:
  1098. storep = &u->host;
  1099. break;
  1100. case CURLUPART_ZONEID:
  1101. storep = &u->zoneid;
  1102. break;
  1103. case CURLUPART_PORT:
  1104. u->portnum = 0;
  1105. storep = &u->port;
  1106. break;
  1107. case CURLUPART_PATH:
  1108. storep = &u->path;
  1109. break;
  1110. case CURLUPART_QUERY:
  1111. storep = &u->query;
  1112. break;
  1113. case CURLUPART_FRAGMENT:
  1114. storep = &u->fragment;
  1115. break;
  1116. default:
  1117. return CURLUE_UNKNOWN_PART;
  1118. }
  1119. if(storep && *storep) {
  1120. free(*storep);
  1121. *storep = NULL;
  1122. }
  1123. return CURLUE_OK;
  1124. }
  1125. switch(what) {
  1126. case CURLUPART_SCHEME:
  1127. if(strlen(part) > MAX_SCHEME_LEN)
  1128. /* too long */
  1129. return CURLUE_MALFORMED_INPUT;
  1130. if(!(flags & CURLU_NON_SUPPORT_SCHEME) &&
  1131. /* verify that it is a fine scheme */
  1132. !Curl_builtin_scheme(part))
  1133. return CURLUE_UNSUPPORTED_SCHEME;
  1134. storep = &u->scheme;
  1135. urlencode = FALSE; /* never */
  1136. break;
  1137. case CURLUPART_USER:
  1138. storep = &u->user;
  1139. break;
  1140. case CURLUPART_PASSWORD:
  1141. storep = &u->password;
  1142. break;
  1143. case CURLUPART_OPTIONS:
  1144. storep = &u->options;
  1145. break;
  1146. case CURLUPART_HOST:
  1147. storep = &u->host;
  1148. free(u->zoneid);
  1149. u->zoneid = NULL;
  1150. break;
  1151. case CURLUPART_ZONEID:
  1152. storep = &u->zoneid;
  1153. break;
  1154. case CURLUPART_PORT:
  1155. {
  1156. char *endp;
  1157. urlencode = FALSE; /* never */
  1158. port = strtol(part, &endp, 10); /* Port number must be decimal */
  1159. if((port <= 0) || (port > 0xffff))
  1160. return CURLUE_BAD_PORT_NUMBER;
  1161. if(*endp)
  1162. /* weirdly provided number, not good! */
  1163. return CURLUE_MALFORMED_INPUT;
  1164. storep = &u->port;
  1165. }
  1166. break;
  1167. case CURLUPART_PATH:
  1168. urlskipslash = TRUE;
  1169. storep = &u->path;
  1170. break;
  1171. case CURLUPART_QUERY:
  1172. plusencode = urlencode;
  1173. appendquery = (flags & CURLU_APPENDQUERY)?1:0;
  1174. equalsencode = appendquery;
  1175. storep = &u->query;
  1176. break;
  1177. case CURLUPART_FRAGMENT:
  1178. storep = &u->fragment;
  1179. break;
  1180. case CURLUPART_URL: {
  1181. /*
  1182. * Allow a new URL to replace the existing (if any) contents.
  1183. *
  1184. * If the existing contents is enough for a URL, allow a relative URL to
  1185. * replace it.
  1186. */
  1187. CURLUcode result;
  1188. char *oldurl;
  1189. char *redired_url;
  1190. CURLU *handle2;
  1191. if(Curl_is_absolute_url(part, NULL, MAX_SCHEME_LEN + 1)) {
  1192. handle2 = curl_url();
  1193. if(!handle2)
  1194. return CURLUE_OUT_OF_MEMORY;
  1195. result = parseurl(part, handle2, flags);
  1196. if(!result)
  1197. mv_urlhandle(handle2, u);
  1198. else
  1199. curl_url_cleanup(handle2);
  1200. return result;
  1201. }
  1202. /* extract the full "old" URL to do the redirect on */
  1203. result = curl_url_get(u, CURLUPART_URL, &oldurl, flags);
  1204. if(result) {
  1205. /* couldn't get the old URL, just use the new! */
  1206. handle2 = curl_url();
  1207. if(!handle2)
  1208. return CURLUE_OUT_OF_MEMORY;
  1209. result = parseurl(part, handle2, flags);
  1210. if(!result)
  1211. mv_urlhandle(handle2, u);
  1212. else
  1213. curl_url_cleanup(handle2);
  1214. return result;
  1215. }
  1216. /* apply the relative part to create a new URL */
  1217. redired_url = concat_url(oldurl, part);
  1218. free(oldurl);
  1219. if(!redired_url)
  1220. return CURLUE_OUT_OF_MEMORY;
  1221. /* now parse the new URL */
  1222. handle2 = curl_url();
  1223. if(!handle2) {
  1224. free(redired_url);
  1225. return CURLUE_OUT_OF_MEMORY;
  1226. }
  1227. result = parseurl(redired_url, handle2, flags);
  1228. free(redired_url);
  1229. if(!result)
  1230. mv_urlhandle(handle2, u);
  1231. else
  1232. curl_url_cleanup(handle2);
  1233. return result;
  1234. }
  1235. default:
  1236. return CURLUE_UNKNOWN_PART;
  1237. }
  1238. DEBUGASSERT(storep);
  1239. {
  1240. const char *newp = part;
  1241. size_t nalloc = strlen(part);
  1242. if(nalloc > CURL_MAX_INPUT_LENGTH)
  1243. /* excessive input length */
  1244. return CURLUE_MALFORMED_INPUT;
  1245. if(urlencode) {
  1246. const unsigned char *i;
  1247. char *o;
  1248. bool free_part = FALSE;
  1249. char *enc = malloc(nalloc * 3 + 1); /* for worst case! */
  1250. if(!enc)
  1251. return CURLUE_OUT_OF_MEMORY;
  1252. if(plusencode) {
  1253. /* space to plus */
  1254. i = (const unsigned char *)part;
  1255. for(o = enc; *i; ++o, ++i)
  1256. *o = (*i == ' ') ? '+' : *i;
  1257. *o = 0; /* zero terminate */
  1258. part = strdup(enc);
  1259. if(!part) {
  1260. free(enc);
  1261. return CURLUE_OUT_OF_MEMORY;
  1262. }
  1263. free_part = TRUE;
  1264. }
  1265. for(i = (const unsigned char *)part, o = enc; *i; i++) {
  1266. if(Curl_isunreserved(*i) ||
  1267. ((*i == '/') && urlskipslash) ||
  1268. ((*i == '=') && equalsencode) ||
  1269. ((*i == '+') && plusencode)) {
  1270. if((*i == '=') && equalsencode)
  1271. /* only skip the first equals sign */
  1272. equalsencode = FALSE;
  1273. *o = *i;
  1274. o++;
  1275. }
  1276. else {
  1277. msnprintf(o, 4, "%%%02x", *i);
  1278. o += 3;
  1279. }
  1280. }
  1281. *o = 0; /* zero terminate */
  1282. newp = enc;
  1283. if(free_part)
  1284. free((char *)part);
  1285. }
  1286. else {
  1287. char *p;
  1288. newp = strdup(part);
  1289. if(!newp)
  1290. return CURLUE_OUT_OF_MEMORY;
  1291. p = (char *)newp;
  1292. while(*p) {
  1293. /* make sure percent encoded are lower case */
  1294. if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
  1295. (ISUPPER(p[1]) || ISUPPER(p[2]))) {
  1296. p[1] = (char)TOLOWER(p[1]);
  1297. p[2] = (char)TOLOWER(p[2]);
  1298. p += 3;
  1299. }
  1300. else
  1301. p++;
  1302. }
  1303. }
  1304. if(appendquery) {
  1305. /* Append the string onto the old query. Add a '&' separator if none is
  1306. present at the end of the exsting query already */
  1307. size_t querylen = u->query ? strlen(u->query) : 0;
  1308. bool addamperand = querylen && (u->query[querylen -1] != '&');
  1309. if(querylen) {
  1310. size_t newplen = strlen(newp);
  1311. char *p = malloc(querylen + addamperand + newplen + 1);
  1312. if(!p) {
  1313. free((char *)newp);
  1314. return CURLUE_OUT_OF_MEMORY;
  1315. }
  1316. strcpy(p, u->query); /* original query */
  1317. if(addamperand)
  1318. p[querylen] = '&'; /* ampersand */
  1319. strcpy(&p[querylen + addamperand], newp); /* new suffix */
  1320. free((char *)newp);
  1321. free(*storep);
  1322. *storep = p;
  1323. return CURLUE_OK;
  1324. }
  1325. }
  1326. if(what == CURLUPART_HOST) {
  1327. if(0 == strlen(newp) && (flags & CURLU_NO_AUTHORITY)) {
  1328. /* Skip hostname check, it's allowed to be empty. */
  1329. }
  1330. else {
  1331. if(hostname_check(u, (char *)newp)) {
  1332. free((char *)newp);
  1333. return CURLUE_MALFORMED_INPUT;
  1334. }
  1335. }
  1336. }
  1337. free(*storep);
  1338. *storep = (char *)newp;
  1339. }
  1340. /* set after the string, to make it not assigned if the allocation above
  1341. fails */
  1342. if(port)
  1343. u->portnum = port;
  1344. return CURLUE_OK;
  1345. }