urlapi.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "urldata.h"
  26. #include "urlapi-int.h"
  27. #include "strcase.h"
  28. #include "url.h"
  29. #include "escape.h"
  30. #include "curl_ctype.h"
  31. #include "inet_pton.h"
  32. #include "inet_ntop.h"
  33. #include "strdup.h"
  34. #include "idn.h"
  35. #include "curl_memrchr.h"
  36. /* The last 3 #include files should be in this order */
  37. #include "curl_printf.h"
  38. #include "curl_memory.h"
  39. #include "memdebug.h"
  40. /* MSDOS/Windows style drive prefix, eg c: in c:foo */
  41. #define STARTS_WITH_DRIVE_PREFIX(str) \
  42. ((('a' <= str[0] && str[0] <= 'z') || \
  43. ('A' <= str[0] && str[0] <= 'Z')) && \
  44. (str[1] == ':'))
  45. /* MSDOS/Windows style drive prefix, optionally with
  46. * a '|' instead of ':', followed by a slash or NUL */
  47. #define STARTS_WITH_URL_DRIVE_PREFIX(str) \
  48. ((('a' <= (str)[0] && (str)[0] <= 'z') || \
  49. ('A' <= (str)[0] && (str)[0] <= 'Z')) && \
  50. ((str)[1] == ':' || (str)[1] == '|') && \
  51. ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
  52. /* scheme is not URL encoded, the longest libcurl supported ones are... */
  53. #define MAX_SCHEME_LEN 40
  54. /*
  55. * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
  56. * sure we have _some_ value for AF_INET6 without polluting our fake value
  57. * everywhere.
  58. */
  59. #if !defined(USE_IPV6) && !defined(AF_INET6)
  60. #define AF_INET6 (AF_INET + 1)
  61. #endif
  62. /* Internal representation of CURLU. Point to URL-encoded strings. */
  63. struct Curl_URL {
  64. char *scheme;
  65. char *user;
  66. char *password;
  67. char *options; /* IMAP only? */
  68. char *host;
  69. char *zoneid; /* for numerical IPv6 addresses */
  70. char *port;
  71. char *path;
  72. char *query;
  73. char *fragment;
  74. unsigned short portnum; /* the numerical version (if 'port' is set) */
  75. BIT(query_present); /* to support blank */
  76. BIT(fragment_present); /* to support blank */
  77. };
  78. #define DEFAULT_SCHEME "https"
  79. static void free_urlhandle(struct Curl_URL *u)
  80. {
  81. free(u->scheme);
  82. free(u->user);
  83. free(u->password);
  84. free(u->options);
  85. free(u->host);
  86. free(u->zoneid);
  87. free(u->port);
  88. free(u->path);
  89. free(u->query);
  90. free(u->fragment);
  91. }
  92. /*
  93. * Find the separator at the end of the host name, or the '?' in cases like
  94. * http://www.example.com?id=2380
  95. */
  96. static const char *find_host_sep(const char *url)
  97. {
  98. const char *sep;
  99. const char *query;
  100. /* Find the start of the hostname */
  101. sep = strstr(url, "//");
  102. if(!sep)
  103. sep = url;
  104. else
  105. sep += 2;
  106. query = strchr(sep, '?');
  107. sep = strchr(sep, '/');
  108. if(!sep)
  109. sep = url + strlen(url);
  110. if(!query)
  111. query = url + strlen(url);
  112. return sep < query ? sep : query;
  113. }
  114. /* convert CURLcode to CURLUcode */
  115. #define cc2cu(x) ((x) == CURLE_TOO_LARGE ? CURLUE_TOO_LARGE : \
  116. CURLUE_OUT_OF_MEMORY)
  117. /*
  118. * Decide whether a character in a URL must be escaped.
  119. */
  120. #define urlchar_needs_escaping(c) (!(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c)))
  121. static const char hexdigits[] = "0123456789abcdef";
  122. /* urlencode_str() writes data into an output dynbuf and URL-encodes the
  123. * spaces in the source URL accordingly.
  124. *
  125. * URL encoding should be skipped for host names, otherwise IDN resolution
  126. * will fail.
  127. */
  128. static CURLUcode urlencode_str(struct dynbuf *o, const char *url,
  129. size_t len, bool relative,
  130. bool query)
  131. {
  132. /* we must add this with whitespace-replacing */
  133. bool left = !query;
  134. const unsigned char *iptr;
  135. const unsigned char *host_sep = (const unsigned char *) url;
  136. CURLcode result;
  137. if(!relative)
  138. host_sep = (const unsigned char *) find_host_sep(url);
  139. for(iptr = (unsigned char *)url; /* read from here */
  140. len; iptr++, len--) {
  141. if(iptr < host_sep) {
  142. result = Curl_dyn_addn(o, iptr, 1);
  143. if(result)
  144. return cc2cu(result);
  145. continue;
  146. }
  147. if(*iptr == ' ') {
  148. if(left)
  149. result = Curl_dyn_addn(o, "%20", 3);
  150. else
  151. result = Curl_dyn_addn(o, "+", 1);
  152. if(result)
  153. return cc2cu(result);
  154. continue;
  155. }
  156. if(*iptr == '?')
  157. left = FALSE;
  158. if(urlchar_needs_escaping(*iptr)) {
  159. char out[3]={'%'};
  160. out[1] = hexdigits[*iptr>>4];
  161. out[2] = hexdigits[*iptr & 0xf];
  162. result = Curl_dyn_addn(o, out, 3);
  163. }
  164. else
  165. result = Curl_dyn_addn(o, iptr, 1);
  166. if(result)
  167. return cc2cu(result);
  168. }
  169. return CURLUE_OK;
  170. }
  171. /*
  172. * Returns the length of the scheme if the given URL is absolute (as opposed
  173. * to relative). Stores the scheme in the buffer if TRUE and 'buf' is
  174. * non-NULL. The buflen must be larger than MAX_SCHEME_LEN if buf is set.
  175. *
  176. * If 'guess_scheme' is TRUE, it means the URL might be provided without
  177. * scheme.
  178. */
  179. size_t Curl_is_absolute_url(const char *url, char *buf, size_t buflen,
  180. bool guess_scheme)
  181. {
  182. int i = 0;
  183. DEBUGASSERT(!buf || (buflen > MAX_SCHEME_LEN));
  184. (void)buflen; /* only used in debug-builds */
  185. if(buf)
  186. buf[0] = 0; /* always leave a defined value in buf */
  187. #ifdef _WIN32
  188. if(guess_scheme && STARTS_WITH_DRIVE_PREFIX(url))
  189. return 0;
  190. #endif
  191. if(ISALPHA(url[0]))
  192. for(i = 1; i < MAX_SCHEME_LEN; ++i) {
  193. char s = url[i];
  194. if(s && (ISALNUM(s) || (s == '+') || (s == '-') || (s == '.') )) {
  195. /* RFC 3986 3.1 explains:
  196. scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  197. */
  198. }
  199. else {
  200. break;
  201. }
  202. }
  203. if(i && (url[i] == ':') && ((url[i + 1] == '/') || !guess_scheme)) {
  204. /* If this does not guess scheme, the scheme always ends with the colon so
  205. that this also detects data: URLs etc. In guessing mode, data: could
  206. be the host name "data" with a specified port number. */
  207. /* the length of the scheme is the name part only */
  208. size_t len = i;
  209. if(buf) {
  210. Curl_strntolower(buf, url, i);
  211. buf[i] = 0;
  212. }
  213. return len;
  214. }
  215. return 0;
  216. }
  217. /*
  218. * Concatenate a relative URL to a base URL making it absolute.
  219. * URL-encodes any spaces.
  220. * The returned pointer must be freed by the caller unless NULL
  221. * (returns NULL on out of memory).
  222. *
  223. * Note that this function destroys the 'base' string.
  224. */
  225. static CURLcode concat_url(char *base, const char *relurl, char **newurl)
  226. {
  227. /***
  228. TRY to append this new path to the old URL
  229. to the right of the host part. Oh crap, this is doomed to cause
  230. problems in the future...
  231. */
  232. struct dynbuf newest;
  233. char *protsep;
  234. char *pathsep;
  235. bool host_changed = FALSE;
  236. const char *useurl = relurl;
  237. CURLcode result = CURLE_OK;
  238. CURLUcode uc;
  239. bool skip_slash = FALSE;
  240. *newurl = NULL;
  241. /* protsep points to the start of the host name */
  242. protsep = strstr(base, "//");
  243. if(!protsep)
  244. protsep = base;
  245. else
  246. protsep += 2; /* pass the slashes */
  247. if('/' != relurl[0]) {
  248. int level = 0;
  249. /* First we need to find out if there's a ?-letter in the URL,
  250. and cut it and the right-side of that off */
  251. pathsep = strchr(protsep, '?');
  252. if(pathsep)
  253. *pathsep = 0;
  254. /* we have a relative path to append to the last slash if there's one
  255. available, or the new URL is just a query string (starts with a '?') or
  256. a fragment (starts with '#') we append the new one at the end of the
  257. current URL */
  258. if((useurl[0] != '?') && (useurl[0] != '#')) {
  259. pathsep = strrchr(protsep, '/');
  260. if(pathsep)
  261. *pathsep = 0;
  262. /* Check if there's any slash after the host name, and if so, remember
  263. that position instead */
  264. pathsep = strchr(protsep, '/');
  265. if(pathsep)
  266. protsep = pathsep + 1;
  267. else
  268. protsep = NULL;
  269. /* now deal with one "./" or any amount of "../" in the newurl
  270. and act accordingly */
  271. if((useurl[0] == '.') && (useurl[1] == '/'))
  272. useurl += 2; /* just skip the "./" */
  273. while((useurl[0] == '.') &&
  274. (useurl[1] == '.') &&
  275. (useurl[2] == '/')) {
  276. level++;
  277. useurl += 3; /* pass the "../" */
  278. }
  279. if(protsep) {
  280. while(level--) {
  281. /* cut off one more level from the right of the original URL */
  282. pathsep = strrchr(protsep, '/');
  283. if(pathsep)
  284. *pathsep = 0;
  285. else {
  286. *protsep = 0;
  287. break;
  288. }
  289. }
  290. }
  291. }
  292. else
  293. skip_slash = TRUE;
  294. }
  295. else {
  296. /* We got a new absolute path for this server */
  297. if(relurl[1] == '/') {
  298. /* the new URL starts with //, just keep the protocol part from the
  299. original one */
  300. *protsep = 0;
  301. useurl = &relurl[2]; /* we keep the slashes from the original, so we
  302. skip the new ones */
  303. host_changed = TRUE;
  304. }
  305. else {
  306. /* cut off the original URL from the first slash, or deal with URLs
  307. without slash */
  308. pathsep = strchr(protsep, '/');
  309. if(pathsep) {
  310. /* When people use badly formatted URLs, such as
  311. "http://www.example.com?dir=/home/daniel" we must not use the first
  312. slash, if there's a ?-letter before it! */
  313. char *sep = strchr(protsep, '?');
  314. if(sep && (sep < pathsep))
  315. pathsep = sep;
  316. *pathsep = 0;
  317. }
  318. else {
  319. /* There was no slash. Now, since we might be operating on a badly
  320. formatted URL, such as "http://www.example.com?id=2380" which
  321. doesn't use a slash separator as it is supposed to, we need to check
  322. for a ?-letter as well! */
  323. pathsep = strchr(protsep, '?');
  324. if(pathsep)
  325. *pathsep = 0;
  326. }
  327. }
  328. }
  329. Curl_dyn_init(&newest, CURL_MAX_INPUT_LENGTH);
  330. /* copy over the root url part */
  331. result = Curl_dyn_add(&newest, base);
  332. if(result)
  333. return result;
  334. /* check if we need to append a slash */
  335. if(('/' == useurl[0]) || (protsep && !*protsep) || skip_slash)
  336. ;
  337. else {
  338. result = Curl_dyn_addn(&newest, "/", 1);
  339. if(result)
  340. return result;
  341. }
  342. /* then append the new piece on the right side */
  343. uc = urlencode_str(&newest, useurl, strlen(useurl), !host_changed,
  344. FALSE);
  345. if(uc)
  346. return (uc == CURLUE_TOO_LARGE) ? CURLE_TOO_LARGE : CURLE_OUT_OF_MEMORY;
  347. *newurl = Curl_dyn_ptr(&newest);
  348. return CURLE_OK;
  349. }
  350. /* scan for byte values <= 31, 127 and sometimes space */
  351. static CURLUcode junkscan(const char *url, size_t *urllen, unsigned int flags)
  352. {
  353. static const char badbytes[]={
  354. /* */ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  355. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  356. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  357. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  358. 0x7f, 0x00 /* null-terminate */
  359. };
  360. size_t n = strlen(url);
  361. size_t nfine;
  362. if(n > CURL_MAX_INPUT_LENGTH)
  363. /* excessive input length */
  364. return CURLUE_MALFORMED_INPUT;
  365. nfine = strcspn(url, badbytes);
  366. if((nfine != n) ||
  367. (!(flags & CURLU_ALLOW_SPACE) && strchr(url, ' ')))
  368. return CURLUE_MALFORMED_INPUT;
  369. *urllen = n;
  370. return CURLUE_OK;
  371. }
  372. /*
  373. * parse_hostname_login()
  374. *
  375. * Parse the login details (user name, password and options) from the URL and
  376. * strip them out of the host name
  377. *
  378. */
  379. static CURLUcode parse_hostname_login(struct Curl_URL *u,
  380. const char *login,
  381. size_t len,
  382. unsigned int flags,
  383. size_t *offset) /* to the host name */
  384. {
  385. CURLUcode result = CURLUE_OK;
  386. CURLcode ccode;
  387. char *userp = NULL;
  388. char *passwdp = NULL;
  389. char *optionsp = NULL;
  390. const struct Curl_handler *h = NULL;
  391. /* At this point, we assume all the other special cases have been taken
  392. * care of, so the host is at most
  393. *
  394. * [user[:password][;options]]@]hostname
  395. *
  396. * We need somewhere to put the embedded details, so do that first.
  397. */
  398. char *ptr;
  399. DEBUGASSERT(login);
  400. *offset = 0;
  401. ptr = memchr(login, '@', len);
  402. if(!ptr)
  403. goto out;
  404. /* We will now try to extract the
  405. * possible login information in a string like:
  406. * ftp://user:password@ftp.my.site:8021/README */
  407. ptr++;
  408. /* if this is a known scheme, get some details */
  409. if(u->scheme)
  410. h = Curl_get_scheme_handler(u->scheme);
  411. /* We could use the login information in the URL so extract it. Only parse
  412. options if the handler says we should. Note that 'h' might be NULL! */
  413. ccode = Curl_parse_login_details(login, ptr - login - 1,
  414. &userp, &passwdp,
  415. (h && (h->flags & PROTOPT_URLOPTIONS)) ?
  416. &optionsp:NULL);
  417. if(ccode) {
  418. result = CURLUE_BAD_LOGIN;
  419. goto out;
  420. }
  421. if(userp) {
  422. if(flags & CURLU_DISALLOW_USER) {
  423. /* Option DISALLOW_USER is set and url contains username. */
  424. result = CURLUE_USER_NOT_ALLOWED;
  425. goto out;
  426. }
  427. free(u->user);
  428. u->user = userp;
  429. }
  430. if(passwdp) {
  431. free(u->password);
  432. u->password = passwdp;
  433. }
  434. if(optionsp) {
  435. free(u->options);
  436. u->options = optionsp;
  437. }
  438. /* the host name starts at this offset */
  439. *offset = ptr - login;
  440. return CURLUE_OK;
  441. out:
  442. free(userp);
  443. free(passwdp);
  444. free(optionsp);
  445. u->user = NULL;
  446. u->password = NULL;
  447. u->options = NULL;
  448. return result;
  449. }
  450. UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, struct dynbuf *host,
  451. bool has_scheme)
  452. {
  453. char *portptr;
  454. char *hostname = Curl_dyn_ptr(host);
  455. /*
  456. * Find the end of an IPv6 address on the ']' ending bracket.
  457. */
  458. if(hostname[0] == '[') {
  459. portptr = strchr(hostname, ']');
  460. if(!portptr)
  461. return CURLUE_BAD_IPV6;
  462. portptr++;
  463. /* this is a RFC2732-style specified IP-address */
  464. if(*portptr) {
  465. if(*portptr != ':')
  466. return CURLUE_BAD_PORT_NUMBER;
  467. }
  468. else
  469. portptr = NULL;
  470. }
  471. else
  472. portptr = strchr(hostname, ':');
  473. if(portptr) {
  474. char *rest = NULL;
  475. unsigned long port;
  476. size_t keep = portptr - hostname;
  477. /* Browser behavior adaptation. If there's a colon with no digits after,
  478. just cut off the name there which makes us ignore the colon and just
  479. use the default port. Firefox, Chrome and Safari all do that.
  480. Don't do it if the URL has no scheme, to make something that looks like
  481. a scheme not work!
  482. */
  483. Curl_dyn_setlen(host, keep);
  484. portptr++;
  485. if(!*portptr)
  486. return has_scheme ? CURLUE_OK : CURLUE_BAD_PORT_NUMBER;
  487. if(!ISDIGIT(*portptr))
  488. return CURLUE_BAD_PORT_NUMBER;
  489. errno = 0;
  490. port = strtoul(portptr, &rest, 10); /* Port number must be decimal */
  491. if(errno || (port > 0xffff) || *rest)
  492. return CURLUE_BAD_PORT_NUMBER;
  493. u->portnum = (unsigned short) port;
  494. /* generate a new port number string to get rid of leading zeroes etc */
  495. free(u->port);
  496. u->port = aprintf("%ld", port);
  497. if(!u->port)
  498. return CURLUE_OUT_OF_MEMORY;
  499. }
  500. return CURLUE_OK;
  501. }
  502. /* this assumes 'hostname' now starts with [ */
  503. static CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname,
  504. size_t hlen) /* length of hostname */
  505. {
  506. size_t len;
  507. DEBUGASSERT(*hostname == '[');
  508. if(hlen < 4) /* '[::]' is the shortest possible valid string */
  509. return CURLUE_BAD_IPV6;
  510. hostname++;
  511. hlen -= 2;
  512. /* only valid IPv6 letters are ok */
  513. len = strspn(hostname, "0123456789abcdefABCDEF:.");
  514. if(hlen != len) {
  515. hlen = len;
  516. if(hostname[len] == '%') {
  517. /* this could now be '%[zone id]' */
  518. char zoneid[16];
  519. int i = 0;
  520. char *h = &hostname[len + 1];
  521. /* pass '25' if present and is a url encoded percent sign */
  522. if(!strncmp(h, "25", 2) && h[2] && (h[2] != ']'))
  523. h += 2;
  524. while(*h && (*h != ']') && (i < 15))
  525. zoneid[i++] = *h++;
  526. if(!i || (']' != *h))
  527. return CURLUE_BAD_IPV6;
  528. zoneid[i] = 0;
  529. u->zoneid = strdup(zoneid);
  530. if(!u->zoneid)
  531. return CURLUE_OUT_OF_MEMORY;
  532. hostname[len] = ']'; /* insert end bracket */
  533. hostname[len + 1] = 0; /* terminate the hostname */
  534. }
  535. else
  536. return CURLUE_BAD_IPV6;
  537. /* hostname is fine */
  538. }
  539. /* Check the IPv6 address. */
  540. {
  541. char dest[16]; /* fits a binary IPv6 address */
  542. char norm[MAX_IPADR_LEN];
  543. hostname[hlen] = 0; /* end the address there */
  544. if(1 != Curl_inet_pton(AF_INET6, hostname, dest))
  545. return CURLUE_BAD_IPV6;
  546. /* check if it can be done shorter */
  547. if(Curl_inet_ntop(AF_INET6, dest, norm, sizeof(norm)) &&
  548. (strlen(norm) < hlen)) {
  549. strcpy(hostname, norm);
  550. hlen = strlen(norm);
  551. hostname[hlen + 1] = 0;
  552. }
  553. hostname[hlen] = ']'; /* restore ending bracket */
  554. }
  555. return CURLUE_OK;
  556. }
  557. static CURLUcode hostname_check(struct Curl_URL *u, char *hostname,
  558. size_t hlen) /* length of hostname */
  559. {
  560. size_t len;
  561. DEBUGASSERT(hostname);
  562. if(!hlen)
  563. return CURLUE_NO_HOST;
  564. else if(hostname[0] == '[')
  565. return ipv6_parse(u, hostname, hlen);
  566. else {
  567. /* letters from the second string are not ok */
  568. len = strcspn(hostname, " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()%");
  569. if(hlen != len)
  570. /* hostname with bad content */
  571. return CURLUE_BAD_HOSTNAME;
  572. }
  573. return CURLUE_OK;
  574. }
  575. /*
  576. * Handle partial IPv4 numerical addresses and different bases, like
  577. * '16843009', '0x7f', '0x7f.1' '0177.1.1.1' etc.
  578. *
  579. * If the given input string is syntactically wrong IPv4 or any part for
  580. * example is too big, this function returns HOST_NAME.
  581. *
  582. * Output the "normalized" version of that input string in plain quad decimal
  583. * integers.
  584. *
  585. * Returns the host type.
  586. */
  587. #define HOST_ERROR -1 /* out of memory */
  588. #define HOST_BAD -2 /* bad IPv4 address */
  589. #define HOST_NAME 1
  590. #define HOST_IPV4 2
  591. #define HOST_IPV6 3
  592. static int ipv4_normalize(struct dynbuf *host)
  593. {
  594. bool done = FALSE;
  595. int n = 0;
  596. const char *c = Curl_dyn_ptr(host);
  597. unsigned long parts[4] = {0, 0, 0, 0};
  598. CURLcode result = CURLE_OK;
  599. if(*c == '[')
  600. return HOST_IPV6;
  601. errno = 0; /* for strtoul */
  602. while(!done) {
  603. char *endp = NULL;
  604. unsigned long l;
  605. if(!ISDIGIT(*c))
  606. /* most importantly this doesn't allow a leading plus or minus */
  607. return HOST_NAME;
  608. l = strtoul(c, &endp, 0);
  609. if(errno)
  610. return HOST_NAME;
  611. #if SIZEOF_LONG > 4
  612. /* a value larger than 32 bits */
  613. if(l > UINT_MAX)
  614. return HOST_NAME;
  615. #endif
  616. parts[n] = l;
  617. c = endp;
  618. switch(*c) {
  619. case '.':
  620. if(n == 3)
  621. return HOST_NAME;
  622. n++;
  623. c++;
  624. break;
  625. case '\0':
  626. done = TRUE;
  627. break;
  628. default:
  629. return HOST_NAME;
  630. }
  631. }
  632. switch(n) {
  633. case 0: /* a -- 32 bits */
  634. Curl_dyn_reset(host);
  635. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  636. (unsigned int)(parts[0] >> 24),
  637. (unsigned int)((parts[0] >> 16) & 0xff),
  638. (unsigned int)((parts[0] >> 8) & 0xff),
  639. (unsigned int)(parts[0] & 0xff));
  640. break;
  641. case 1: /* a.b -- 8.24 bits */
  642. if((parts[0] > 0xff) || (parts[1] > 0xffffff))
  643. return HOST_NAME;
  644. Curl_dyn_reset(host);
  645. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  646. (unsigned int)(parts[0]),
  647. (unsigned int)((parts[1] >> 16) & 0xff),
  648. (unsigned int)((parts[1] >> 8) & 0xff),
  649. (unsigned int)(parts[1] & 0xff));
  650. break;
  651. case 2: /* a.b.c -- 8.8.16 bits */
  652. if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xffff))
  653. return HOST_NAME;
  654. Curl_dyn_reset(host);
  655. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  656. (unsigned int)(parts[0]),
  657. (unsigned int)(parts[1]),
  658. (unsigned int)((parts[2] >> 8) & 0xff),
  659. (unsigned int)(parts[2] & 0xff));
  660. break;
  661. case 3: /* a.b.c.d -- 8.8.8.8 bits */
  662. if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff) ||
  663. (parts[3] > 0xff))
  664. return HOST_NAME;
  665. Curl_dyn_reset(host);
  666. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  667. (unsigned int)(parts[0]),
  668. (unsigned int)(parts[1]),
  669. (unsigned int)(parts[2]),
  670. (unsigned int)(parts[3]));
  671. break;
  672. }
  673. if(result)
  674. return HOST_ERROR;
  675. return HOST_IPV4;
  676. }
  677. /* if necessary, replace the host content with a URL decoded version */
  678. static CURLUcode urldecode_host(struct dynbuf *host)
  679. {
  680. char *per = NULL;
  681. const char *hostname = Curl_dyn_ptr(host);
  682. per = strchr(hostname, '%');
  683. if(!per)
  684. /* nothing to decode */
  685. return CURLUE_OK;
  686. else {
  687. /* encoded */
  688. size_t dlen;
  689. char *decoded;
  690. CURLcode result = Curl_urldecode(hostname, 0, &decoded, &dlen,
  691. REJECT_CTRL);
  692. if(result)
  693. return CURLUE_BAD_HOSTNAME;
  694. Curl_dyn_reset(host);
  695. result = Curl_dyn_addn(host, decoded, dlen);
  696. free(decoded);
  697. if(result)
  698. return cc2cu(result);
  699. }
  700. return CURLUE_OK;
  701. }
  702. static CURLUcode parse_authority(struct Curl_URL *u,
  703. const char *auth, size_t authlen,
  704. unsigned int flags,
  705. struct dynbuf *host,
  706. bool has_scheme)
  707. {
  708. size_t offset;
  709. CURLUcode uc;
  710. CURLcode result;
  711. /*
  712. * Parse the login details and strip them out of the host name.
  713. */
  714. uc = parse_hostname_login(u, auth, authlen, flags, &offset);
  715. if(uc)
  716. goto out;
  717. result = Curl_dyn_addn(host, auth + offset, authlen - offset);
  718. if(result) {
  719. uc = cc2cu(result);
  720. goto out;
  721. }
  722. uc = Curl_parse_port(u, host, has_scheme);
  723. if(uc)
  724. goto out;
  725. if(!Curl_dyn_len(host))
  726. return CURLUE_NO_HOST;
  727. switch(ipv4_normalize(host)) {
  728. case HOST_IPV4:
  729. break;
  730. case HOST_IPV6:
  731. uc = ipv6_parse(u, Curl_dyn_ptr(host), Curl_dyn_len(host));
  732. break;
  733. case HOST_NAME:
  734. uc = urldecode_host(host);
  735. if(!uc)
  736. uc = hostname_check(u, Curl_dyn_ptr(host), Curl_dyn_len(host));
  737. break;
  738. case HOST_ERROR:
  739. uc = CURLUE_OUT_OF_MEMORY;
  740. break;
  741. case HOST_BAD:
  742. default:
  743. uc = CURLUE_BAD_HOSTNAME; /* Bad IPv4 address even */
  744. break;
  745. }
  746. out:
  747. return uc;
  748. }
  749. /* used for HTTP/2 server push */
  750. CURLUcode Curl_url_set_authority(CURLU *u, const char *authority)
  751. {
  752. CURLUcode result;
  753. struct dynbuf host;
  754. DEBUGASSERT(authority);
  755. Curl_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
  756. result = parse_authority(u, authority, strlen(authority),
  757. CURLU_DISALLOW_USER, &host, !!u->scheme);
  758. if(result)
  759. Curl_dyn_free(&host);
  760. else {
  761. free(u->host);
  762. u->host = Curl_dyn_ptr(&host);
  763. }
  764. return result;
  765. }
  766. /*
  767. * "Remove Dot Segments"
  768. * https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
  769. */
  770. /*
  771. * dedotdotify()
  772. * @unittest: 1395
  773. *
  774. * This function gets a null-terminated path with dot and dotdot sequences
  775. * passed in and strips them off according to the rules in RFC 3986 section
  776. * 5.2.4.
  777. *
  778. * The function handles a query part ('?' + stuff) appended but it expects
  779. * that fragments ('#' + stuff) have already been cut off.
  780. *
  781. * RETURNS
  782. *
  783. * Zero for success and 'out' set to an allocated dedotdotified string.
  784. */
  785. UNITTEST int dedotdotify(const char *input, size_t clen, char **outp);
  786. UNITTEST int dedotdotify(const char *input, size_t clen, char **outp)
  787. {
  788. char *outptr;
  789. const char *endp = &input[clen];
  790. char *out;
  791. *outp = NULL;
  792. /* the path always starts with a slash, and a slash has not dot */
  793. if((clen < 2) || !memchr(input, '.', clen))
  794. return 0;
  795. out = malloc(clen + 1);
  796. if(!out)
  797. return 1; /* out of memory */
  798. *out = 0; /* null-terminates, for inputs like "./" */
  799. outptr = out;
  800. do {
  801. bool dotdot = TRUE;
  802. if(*input == '.') {
  803. /* A. If the input buffer begins with a prefix of "../" or "./", then
  804. remove that prefix from the input buffer; otherwise, */
  805. if(!strncmp("./", input, 2)) {
  806. input += 2;
  807. clen -= 2;
  808. }
  809. else if(!strncmp("../", input, 3)) {
  810. input += 3;
  811. clen -= 3;
  812. }
  813. /* D. if the input buffer consists only of "." or "..", then remove
  814. that from the input buffer; otherwise, */
  815. else if(!strcmp(".", input) || !strcmp("..", input) ||
  816. !strncmp(".?", input, 2) || !strncmp("..?", input, 3)) {
  817. *out = 0;
  818. break;
  819. }
  820. else
  821. dotdot = FALSE;
  822. }
  823. else if(*input == '/') {
  824. /* B. if the input buffer begins with a prefix of "/./" or "/.", where
  825. "." is a complete path segment, then replace that prefix with "/" in
  826. the input buffer; otherwise, */
  827. if(!strncmp("/./", input, 3)) {
  828. input += 2;
  829. clen -= 2;
  830. }
  831. else if(!strcmp("/.", input) || !strncmp("/.?", input, 3)) {
  832. *outptr++ = '/';
  833. *outptr = 0;
  834. break;
  835. }
  836. /* C. if the input buffer begins with a prefix of "/../" or "/..",
  837. where ".." is a complete path segment, then replace that prefix with
  838. "/" in the input buffer and remove the last segment and its
  839. preceding "/" (if any) from the output buffer; otherwise, */
  840. else if(!strncmp("/../", input, 4)) {
  841. input += 3;
  842. clen -= 3;
  843. /* remove the last segment from the output buffer */
  844. while(outptr > out) {
  845. outptr--;
  846. if(*outptr == '/')
  847. break;
  848. }
  849. *outptr = 0; /* null-terminate where it stops */
  850. }
  851. else if(!strcmp("/..", input) || !strncmp("/..?", input, 4)) {
  852. /* remove the last segment from the output buffer */
  853. while(outptr > out) {
  854. outptr--;
  855. if(*outptr == '/')
  856. break;
  857. }
  858. *outptr++ = '/';
  859. *outptr = 0; /* null-terminate where it stops */
  860. break;
  861. }
  862. else
  863. dotdot = FALSE;
  864. }
  865. else
  866. dotdot = FALSE;
  867. if(!dotdot) {
  868. /* E. move the first path segment in the input buffer to the end of
  869. the output buffer, including the initial "/" character (if any) and
  870. any subsequent characters up to, but not including, the next "/"
  871. character or the end of the input buffer. */
  872. do {
  873. *outptr++ = *input++;
  874. clen--;
  875. } while(*input && (*input != '/') && (*input != '?'));
  876. *outptr = 0;
  877. }
  878. /* continue until end of path */
  879. } while(input < endp);
  880. *outp = out;
  881. return 0; /* success */
  882. }
  883. static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
  884. {
  885. const char *path;
  886. size_t pathlen;
  887. char *query = NULL;
  888. char *fragment = NULL;
  889. char schemebuf[MAX_SCHEME_LEN + 1];
  890. size_t schemelen = 0;
  891. size_t urllen;
  892. CURLUcode result = CURLUE_OK;
  893. size_t fraglen = 0;
  894. struct dynbuf host;
  895. DEBUGASSERT(url);
  896. Curl_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
  897. result = junkscan(url, &urllen, flags);
  898. if(result)
  899. goto fail;
  900. schemelen = Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf),
  901. flags & (CURLU_GUESS_SCHEME|
  902. CURLU_DEFAULT_SCHEME));
  903. /* handle the file: scheme */
  904. if(schemelen && !strcmp(schemebuf, "file")) {
  905. bool uncpath = FALSE;
  906. if(urllen <= 6) {
  907. /* file:/ is not enough to actually be a complete file: URL */
  908. result = CURLUE_BAD_FILE_URL;
  909. goto fail;
  910. }
  911. /* path has been allocated large enough to hold this */
  912. path = (char *)&url[5];
  913. pathlen = urllen - 5;
  914. u->scheme = strdup("file");
  915. if(!u->scheme) {
  916. result = CURLUE_OUT_OF_MEMORY;
  917. goto fail;
  918. }
  919. /* Extra handling URLs with an authority component (i.e. that start with
  920. * "file://")
  921. *
  922. * We allow omitted hostname (e.g. file:/<path>) -- valid according to
  923. * RFC 8089, but not the (current) WHAT-WG URL spec.
  924. */
  925. if(path[0] == '/' && path[1] == '/') {
  926. /* swallow the two slashes */
  927. const char *ptr = &path[2];
  928. /*
  929. * According to RFC 8089, a file: URL can be reliably dereferenced if:
  930. *
  931. * o it has no/blank hostname, or
  932. *
  933. * o the hostname matches "localhost" (case-insensitively), or
  934. *
  935. * o the hostname is a FQDN that resolves to this machine, or
  936. *
  937. * o it is an UNC String transformed to an URI (Windows only, RFC 8089
  938. * Appendix E.3).
  939. *
  940. * For brevity, we only consider URLs with empty, "localhost", or
  941. * "127.0.0.1" hostnames as local, otherwise as an UNC String.
  942. *
  943. * Additionally, there is an exception for URLs with a Windows drive
  944. * letter in the authority (which was accidentally omitted from RFC 8089
  945. * Appendix E, but believe me, it was meant to be there. --MK)
  946. */
  947. if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
  948. /* the URL includes a host name, it must match "localhost" or
  949. "127.0.0.1" to be valid */
  950. if(checkprefix("localhost/", ptr) ||
  951. checkprefix("127.0.0.1/", ptr)) {
  952. ptr += 9; /* now points to the slash after the host */
  953. }
  954. else {
  955. #if defined(_WIN32)
  956. size_t len;
  957. /* the host name, NetBIOS computer name, can not contain disallowed
  958. chars, and the delimiting slash character must be appended to the
  959. host name */
  960. path = strpbrk(ptr, "/\\:*?\"<>|");
  961. if(!path || *path != '/') {
  962. result = CURLUE_BAD_FILE_URL;
  963. goto fail;
  964. }
  965. len = path - ptr;
  966. if(len) {
  967. CURLcode code = Curl_dyn_addn(&host, ptr, len);
  968. if(code) {
  969. result = cc2cu(code);
  970. goto fail;
  971. }
  972. uncpath = TRUE;
  973. }
  974. ptr -= 2; /* now points to the // before the host in UNC */
  975. #else
  976. /* Invalid file://hostname/, expected localhost or 127.0.0.1 or
  977. none */
  978. result = CURLUE_BAD_FILE_URL;
  979. goto fail;
  980. #endif
  981. }
  982. }
  983. path = ptr;
  984. pathlen = urllen - (ptr - url);
  985. }
  986. if(!uncpath)
  987. /* no host for file: URLs by default */
  988. Curl_dyn_reset(&host);
  989. #if !defined(_WIN32) && !defined(MSDOS) && !defined(__CYGWIN__)
  990. /* Don't allow Windows drive letters when not in Windows.
  991. * This catches both "file:/c:" and "file:c:" */
  992. if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
  993. STARTS_WITH_URL_DRIVE_PREFIX(path)) {
  994. /* File drive letters are only accepted in MSDOS/Windows */
  995. result = CURLUE_BAD_FILE_URL;
  996. goto fail;
  997. }
  998. #else
  999. /* If the path starts with a slash and a drive letter, ditch the slash */
  1000. if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
  1001. /* This cannot be done with strcpy, as the memory chunks overlap! */
  1002. path++;
  1003. pathlen--;
  1004. }
  1005. #endif
  1006. }
  1007. else {
  1008. /* clear path */
  1009. const char *schemep = NULL;
  1010. const char *hostp;
  1011. size_t hostlen;
  1012. if(schemelen) {
  1013. int i = 0;
  1014. const char *p = &url[schemelen + 1];
  1015. while((*p == '/') && (i < 4)) {
  1016. p++;
  1017. i++;
  1018. }
  1019. schemep = schemebuf;
  1020. if(!Curl_get_scheme_handler(schemep) &&
  1021. !(flags & CURLU_NON_SUPPORT_SCHEME)) {
  1022. result = CURLUE_UNSUPPORTED_SCHEME;
  1023. goto fail;
  1024. }
  1025. if((i < 1) || (i > 3)) {
  1026. /* less than one or more than three slashes */
  1027. result = CURLUE_BAD_SLASHES;
  1028. goto fail;
  1029. }
  1030. hostp = p; /* host name starts here */
  1031. }
  1032. else {
  1033. /* no scheme! */
  1034. if(!(flags & (CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME))) {
  1035. result = CURLUE_BAD_SCHEME;
  1036. goto fail;
  1037. }
  1038. if(flags & CURLU_DEFAULT_SCHEME)
  1039. schemep = DEFAULT_SCHEME;
  1040. /*
  1041. * The URL was badly formatted, let's try without scheme specified.
  1042. */
  1043. hostp = url;
  1044. }
  1045. if(schemep) {
  1046. u->scheme = strdup(schemep);
  1047. if(!u->scheme) {
  1048. result = CURLUE_OUT_OF_MEMORY;
  1049. goto fail;
  1050. }
  1051. }
  1052. /* find the end of the host name + port number */
  1053. hostlen = strcspn(hostp, "/?#");
  1054. path = &hostp[hostlen];
  1055. /* this pathlen also contains the query and the fragment */
  1056. pathlen = urllen - (path - url);
  1057. if(hostlen) {
  1058. result = parse_authority(u, hostp, hostlen, flags, &host, schemelen);
  1059. if(result)
  1060. goto fail;
  1061. if((flags & CURLU_GUESS_SCHEME) && !schemep) {
  1062. const char *hostname = Curl_dyn_ptr(&host);
  1063. /* legacy curl-style guess based on host name */
  1064. if(checkprefix("ftp.", hostname))
  1065. schemep = "ftp";
  1066. else if(checkprefix("dict.", hostname))
  1067. schemep = "dict";
  1068. else if(checkprefix("ldap.", hostname))
  1069. schemep = "ldap";
  1070. else if(checkprefix("imap.", hostname))
  1071. schemep = "imap";
  1072. else if(checkprefix("smtp.", hostname))
  1073. schemep = "smtp";
  1074. else if(checkprefix("pop3.", hostname))
  1075. schemep = "pop3";
  1076. else
  1077. schemep = "http";
  1078. u->scheme = strdup(schemep);
  1079. if(!u->scheme) {
  1080. result = CURLUE_OUT_OF_MEMORY;
  1081. goto fail;
  1082. }
  1083. }
  1084. }
  1085. else if(flags & CURLU_NO_AUTHORITY) {
  1086. /* allowed to be empty. */
  1087. if(Curl_dyn_add(&host, "")) {
  1088. result = CURLUE_OUT_OF_MEMORY;
  1089. goto fail;
  1090. }
  1091. }
  1092. else {
  1093. result = CURLUE_NO_HOST;
  1094. goto fail;
  1095. }
  1096. }
  1097. fragment = strchr(path, '#');
  1098. if(fragment) {
  1099. fraglen = pathlen - (fragment - path);
  1100. u->fragment_present = TRUE;
  1101. if(fraglen > 1) {
  1102. /* skip the leading '#' in the copy but include the terminating null */
  1103. if(flags & CURLU_URLENCODE) {
  1104. struct dynbuf enc;
  1105. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1106. result = urlencode_str(&enc, fragment + 1, fraglen - 1, TRUE, FALSE);
  1107. if(result)
  1108. goto fail;
  1109. u->fragment = Curl_dyn_ptr(&enc);
  1110. }
  1111. else {
  1112. u->fragment = Curl_memdup0(fragment + 1, fraglen - 1);
  1113. if(!u->fragment) {
  1114. result = CURLUE_OUT_OF_MEMORY;
  1115. goto fail;
  1116. }
  1117. }
  1118. }
  1119. /* after this, pathlen still contains the query */
  1120. pathlen -= fraglen;
  1121. }
  1122. query = memchr(path, '?', pathlen);
  1123. if(query) {
  1124. size_t qlen = fragment ? (size_t)(fragment - query) :
  1125. pathlen - (query - path);
  1126. pathlen -= qlen;
  1127. u->query_present = TRUE;
  1128. if(qlen > 1) {
  1129. if(flags & CURLU_URLENCODE) {
  1130. struct dynbuf enc;
  1131. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1132. /* skip the leading question mark */
  1133. result = urlencode_str(&enc, query + 1, qlen - 1, TRUE, TRUE);
  1134. if(result)
  1135. goto fail;
  1136. u->query = Curl_dyn_ptr(&enc);
  1137. }
  1138. else {
  1139. u->query = Curl_memdup0(query + 1, qlen - 1);
  1140. if(!u->query) {
  1141. result = CURLUE_OUT_OF_MEMORY;
  1142. goto fail;
  1143. }
  1144. }
  1145. }
  1146. else {
  1147. /* single byte query */
  1148. u->query = strdup("");
  1149. if(!u->query) {
  1150. result = CURLUE_OUT_OF_MEMORY;
  1151. goto fail;
  1152. }
  1153. }
  1154. }
  1155. if(pathlen && (flags & CURLU_URLENCODE)) {
  1156. struct dynbuf enc;
  1157. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1158. result = urlencode_str(&enc, path, pathlen, TRUE, FALSE);
  1159. if(result)
  1160. goto fail;
  1161. pathlen = Curl_dyn_len(&enc);
  1162. path = u->path = Curl_dyn_ptr(&enc);
  1163. }
  1164. if(pathlen <= 1) {
  1165. /* there is no path left or just the slash, unset */
  1166. path = NULL;
  1167. }
  1168. else {
  1169. if(!u->path) {
  1170. u->path = Curl_memdup0(path, pathlen);
  1171. if(!u->path) {
  1172. result = CURLUE_OUT_OF_MEMORY;
  1173. goto fail;
  1174. }
  1175. path = u->path;
  1176. }
  1177. else if(flags & CURLU_URLENCODE)
  1178. /* it might have encoded more than just the path so cut it */
  1179. u->path[pathlen] = 0;
  1180. if(!(flags & CURLU_PATH_AS_IS)) {
  1181. /* remove ../ and ./ sequences according to RFC3986 */
  1182. char *dedot;
  1183. int err = dedotdotify((char *)path, pathlen, &dedot);
  1184. if(err) {
  1185. result = CURLUE_OUT_OF_MEMORY;
  1186. goto fail;
  1187. }
  1188. if(dedot) {
  1189. free(u->path);
  1190. u->path = dedot;
  1191. }
  1192. }
  1193. }
  1194. u->host = Curl_dyn_ptr(&host);
  1195. return result;
  1196. fail:
  1197. Curl_dyn_free(&host);
  1198. free_urlhandle(u);
  1199. return result;
  1200. }
  1201. /*
  1202. * Parse the URL and, if successful, replace everything in the Curl_URL struct.
  1203. */
  1204. static CURLUcode parseurl_and_replace(const char *url, CURLU *u,
  1205. unsigned int flags)
  1206. {
  1207. CURLUcode result;
  1208. CURLU tmpurl;
  1209. memset(&tmpurl, 0, sizeof(tmpurl));
  1210. result = parseurl(url, &tmpurl, flags);
  1211. if(!result) {
  1212. free_urlhandle(u);
  1213. *u = tmpurl;
  1214. }
  1215. return result;
  1216. }
  1217. /*
  1218. */
  1219. CURLU *curl_url(void)
  1220. {
  1221. return calloc(1, sizeof(struct Curl_URL));
  1222. }
  1223. void curl_url_cleanup(CURLU *u)
  1224. {
  1225. if(u) {
  1226. free_urlhandle(u);
  1227. free(u);
  1228. }
  1229. }
  1230. #define DUP(dest, src, name) \
  1231. do { \
  1232. if(src->name) { \
  1233. dest->name = strdup(src->name); \
  1234. if(!dest->name) \
  1235. goto fail; \
  1236. } \
  1237. } while(0)
  1238. CURLU *curl_url_dup(const CURLU *in)
  1239. {
  1240. struct Curl_URL *u = calloc(1, sizeof(struct Curl_URL));
  1241. if(u) {
  1242. DUP(u, in, scheme);
  1243. DUP(u, in, user);
  1244. DUP(u, in, password);
  1245. DUP(u, in, options);
  1246. DUP(u, in, host);
  1247. DUP(u, in, port);
  1248. DUP(u, in, path);
  1249. DUP(u, in, query);
  1250. DUP(u, in, fragment);
  1251. DUP(u, in, zoneid);
  1252. u->portnum = in->portnum;
  1253. u->fragment_present = in->fragment_present;
  1254. u->query_present = in->query_present;
  1255. }
  1256. return u;
  1257. fail:
  1258. curl_url_cleanup(u);
  1259. return NULL;
  1260. }
  1261. CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
  1262. char **part, unsigned int flags)
  1263. {
  1264. const char *ptr;
  1265. CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
  1266. char portbuf[7];
  1267. bool urldecode = (flags & CURLU_URLDECODE)?1:0;
  1268. bool urlencode = (flags & CURLU_URLENCODE)?1:0;
  1269. bool punycode = FALSE;
  1270. bool depunyfy = FALSE;
  1271. bool plusdecode = FALSE;
  1272. (void)flags;
  1273. if(!u)
  1274. return CURLUE_BAD_HANDLE;
  1275. if(!part)
  1276. return CURLUE_BAD_PARTPOINTER;
  1277. *part = NULL;
  1278. switch(what) {
  1279. case CURLUPART_SCHEME:
  1280. ptr = u->scheme;
  1281. ifmissing = CURLUE_NO_SCHEME;
  1282. urldecode = FALSE; /* never for schemes */
  1283. break;
  1284. case CURLUPART_USER:
  1285. ptr = u->user;
  1286. ifmissing = CURLUE_NO_USER;
  1287. break;
  1288. case CURLUPART_PASSWORD:
  1289. ptr = u->password;
  1290. ifmissing = CURLUE_NO_PASSWORD;
  1291. break;
  1292. case CURLUPART_OPTIONS:
  1293. ptr = u->options;
  1294. ifmissing = CURLUE_NO_OPTIONS;
  1295. break;
  1296. case CURLUPART_HOST:
  1297. ptr = u->host;
  1298. ifmissing = CURLUE_NO_HOST;
  1299. punycode = (flags & CURLU_PUNYCODE)?1:0;
  1300. depunyfy = (flags & CURLU_PUNY2IDN)?1:0;
  1301. break;
  1302. case CURLUPART_ZONEID:
  1303. ptr = u->zoneid;
  1304. ifmissing = CURLUE_NO_ZONEID;
  1305. break;
  1306. case CURLUPART_PORT:
  1307. ptr = u->port;
  1308. ifmissing = CURLUE_NO_PORT;
  1309. urldecode = FALSE; /* never for port */
  1310. if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
  1311. /* there's no stored port number, but asked to deliver
  1312. a default one for the scheme */
  1313. const struct Curl_handler *h = Curl_get_scheme_handler(u->scheme);
  1314. if(h) {
  1315. msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
  1316. ptr = portbuf;
  1317. }
  1318. }
  1319. else if(ptr && u->scheme) {
  1320. /* there is a stored port number, but ask to inhibit if
  1321. it matches the default one for the scheme */
  1322. const struct Curl_handler *h = Curl_get_scheme_handler(u->scheme);
  1323. if(h && (h->defport == u->portnum) &&
  1324. (flags & CURLU_NO_DEFAULT_PORT))
  1325. ptr = NULL;
  1326. }
  1327. break;
  1328. case CURLUPART_PATH:
  1329. ptr = u->path;
  1330. if(!ptr)
  1331. ptr = "/";
  1332. break;
  1333. case CURLUPART_QUERY:
  1334. ptr = u->query;
  1335. ifmissing = CURLUE_NO_QUERY;
  1336. plusdecode = urldecode;
  1337. if(ptr && !ptr[0] && !(flags & CURLU_GET_EMPTY))
  1338. /* there was a blank query and the user do not ask for it */
  1339. ptr = NULL;
  1340. break;
  1341. case CURLUPART_FRAGMENT:
  1342. ptr = u->fragment;
  1343. ifmissing = CURLUE_NO_FRAGMENT;
  1344. if(!ptr && u->fragment_present && flags & CURLU_GET_EMPTY)
  1345. /* there was a blank fragment and the user asks for it */
  1346. ptr = "";
  1347. break;
  1348. case CURLUPART_URL: {
  1349. char *url;
  1350. char *scheme;
  1351. char *options = u->options;
  1352. char *port = u->port;
  1353. char *allochost = NULL;
  1354. bool show_fragment =
  1355. u->fragment || (u->fragment_present && flags & CURLU_GET_EMPTY);
  1356. bool show_query =
  1357. (u->query && u->query[0]) ||
  1358. (u->query_present && flags & CURLU_GET_EMPTY);
  1359. punycode = (flags & CURLU_PUNYCODE)?1:0;
  1360. depunyfy = (flags & CURLU_PUNY2IDN)?1:0;
  1361. if(u->scheme && strcasecompare("file", u->scheme)) {
  1362. url = aprintf("file://%s%s%s",
  1363. u->path,
  1364. show_fragment ? "#": "",
  1365. u->fragment ? u->fragment : "");
  1366. }
  1367. else if(!u->host)
  1368. return CURLUE_NO_HOST;
  1369. else {
  1370. const struct Curl_handler *h = NULL;
  1371. if(u->scheme)
  1372. scheme = u->scheme;
  1373. else if(flags & CURLU_DEFAULT_SCHEME)
  1374. scheme = (char *) DEFAULT_SCHEME;
  1375. else
  1376. return CURLUE_NO_SCHEME;
  1377. h = Curl_get_scheme_handler(scheme);
  1378. if(!port && (flags & CURLU_DEFAULT_PORT)) {
  1379. /* there's no stored port number, but asked to deliver
  1380. a default one for the scheme */
  1381. if(h) {
  1382. msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
  1383. port = portbuf;
  1384. }
  1385. }
  1386. else if(port) {
  1387. /* there is a stored port number, but asked to inhibit if it matches
  1388. the default one for the scheme */
  1389. if(h && (h->defport == u->portnum) &&
  1390. (flags & CURLU_NO_DEFAULT_PORT))
  1391. port = NULL;
  1392. }
  1393. if(h && !(h->flags & PROTOPT_URLOPTIONS))
  1394. options = NULL;
  1395. if(u->host[0] == '[') {
  1396. if(u->zoneid) {
  1397. /* make it '[ host %25 zoneid ]' */
  1398. struct dynbuf enc;
  1399. size_t hostlen = strlen(u->host);
  1400. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1401. if(Curl_dyn_addf(&enc, "%.*s%%25%s]", (int)hostlen - 1, u->host,
  1402. u->zoneid))
  1403. return CURLUE_OUT_OF_MEMORY;
  1404. allochost = Curl_dyn_ptr(&enc);
  1405. }
  1406. }
  1407. else if(urlencode) {
  1408. allochost = curl_easy_escape(NULL, u->host, 0);
  1409. if(!allochost)
  1410. return CURLUE_OUT_OF_MEMORY;
  1411. }
  1412. else if(punycode) {
  1413. if(!Curl_is_ASCII_name(u->host)) {
  1414. #ifndef USE_IDN
  1415. return CURLUE_LACKS_IDN;
  1416. #else
  1417. CURLcode result = Curl_idn_decode(u->host, &allochost);
  1418. if(result)
  1419. return (result == CURLE_OUT_OF_MEMORY) ?
  1420. CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
  1421. #endif
  1422. }
  1423. }
  1424. else if(depunyfy) {
  1425. if(Curl_is_ASCII_name(u->host) && !strncmp("xn--", u->host, 4)) {
  1426. #ifndef USE_IDN
  1427. return CURLUE_LACKS_IDN;
  1428. #else
  1429. CURLcode result = Curl_idn_encode(u->host, &allochost);
  1430. if(result)
  1431. /* this is the most likely error */
  1432. return (result == CURLE_OUT_OF_MEMORY) ?
  1433. CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
  1434. #endif
  1435. }
  1436. }
  1437. url = aprintf("%s://%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1438. scheme,
  1439. u->user ? u->user : "",
  1440. u->password ? ":": "",
  1441. u->password ? u->password : "",
  1442. options ? ";" : "",
  1443. options ? options : "",
  1444. (u->user || u->password || options) ? "@": "",
  1445. allochost ? allochost : u->host,
  1446. port ? ":": "",
  1447. port ? port : "",
  1448. u->path ? u->path : "/",
  1449. show_query ? "?": "",
  1450. u->query ? u->query : "",
  1451. show_fragment ? "#": "",
  1452. u->fragment? u->fragment : "");
  1453. free(allochost);
  1454. }
  1455. if(!url)
  1456. return CURLUE_OUT_OF_MEMORY;
  1457. *part = url;
  1458. return CURLUE_OK;
  1459. }
  1460. default:
  1461. ptr = NULL;
  1462. break;
  1463. }
  1464. if(ptr) {
  1465. size_t partlen = strlen(ptr);
  1466. size_t i = 0;
  1467. *part = Curl_memdup0(ptr, partlen);
  1468. if(!*part)
  1469. return CURLUE_OUT_OF_MEMORY;
  1470. if(plusdecode) {
  1471. /* convert + to space */
  1472. char *plus = *part;
  1473. for(i = 0; i < partlen; ++plus, i++) {
  1474. if(*plus == '+')
  1475. *plus = ' ';
  1476. }
  1477. }
  1478. if(urldecode) {
  1479. char *decoded;
  1480. size_t dlen;
  1481. /* this unconditional rejection of control bytes is documented
  1482. API behavior */
  1483. CURLcode res = Curl_urldecode(*part, 0, &decoded, &dlen, REJECT_CTRL);
  1484. free(*part);
  1485. if(res) {
  1486. *part = NULL;
  1487. return CURLUE_URLDECODE;
  1488. }
  1489. *part = decoded;
  1490. partlen = dlen;
  1491. }
  1492. if(urlencode) {
  1493. struct dynbuf enc;
  1494. CURLUcode uc;
  1495. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1496. uc = urlencode_str(&enc, *part, partlen, TRUE, what == CURLUPART_QUERY);
  1497. if(uc)
  1498. return uc;
  1499. free(*part);
  1500. *part = Curl_dyn_ptr(&enc);
  1501. }
  1502. else if(punycode) {
  1503. if(!Curl_is_ASCII_name(u->host)) {
  1504. #ifndef USE_IDN
  1505. return CURLUE_LACKS_IDN;
  1506. #else
  1507. char *allochost;
  1508. CURLcode result = Curl_idn_decode(*part, &allochost);
  1509. if(result)
  1510. return (result == CURLE_OUT_OF_MEMORY) ?
  1511. CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
  1512. free(*part);
  1513. *part = allochost;
  1514. #endif
  1515. }
  1516. }
  1517. else if(depunyfy) {
  1518. if(Curl_is_ASCII_name(u->host) && !strncmp("xn--", u->host, 4)) {
  1519. #ifndef USE_IDN
  1520. return CURLUE_LACKS_IDN;
  1521. #else
  1522. char *allochost;
  1523. CURLcode result = Curl_idn_encode(*part, &allochost);
  1524. if(result)
  1525. return (result == CURLE_OUT_OF_MEMORY) ?
  1526. CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
  1527. free(*part);
  1528. *part = allochost;
  1529. #endif
  1530. }
  1531. }
  1532. return CURLUE_OK;
  1533. }
  1534. else
  1535. return ifmissing;
  1536. }
  1537. CURLUcode curl_url_set(CURLU *u, CURLUPart what,
  1538. const char *part, unsigned int flags)
  1539. {
  1540. char **storep = NULL;
  1541. bool urlencode = (flags & CURLU_URLENCODE)? 1 : 0;
  1542. bool plusencode = FALSE;
  1543. bool urlskipslash = FALSE;
  1544. bool leadingslash = FALSE;
  1545. bool appendquery = FALSE;
  1546. bool equalsencode = FALSE;
  1547. size_t nalloc;
  1548. if(!u)
  1549. return CURLUE_BAD_HANDLE;
  1550. if(!part) {
  1551. /* setting a part to NULL clears it */
  1552. switch(what) {
  1553. case CURLUPART_URL:
  1554. break;
  1555. case CURLUPART_SCHEME:
  1556. storep = &u->scheme;
  1557. break;
  1558. case CURLUPART_USER:
  1559. storep = &u->user;
  1560. break;
  1561. case CURLUPART_PASSWORD:
  1562. storep = &u->password;
  1563. break;
  1564. case CURLUPART_OPTIONS:
  1565. storep = &u->options;
  1566. break;
  1567. case CURLUPART_HOST:
  1568. storep = &u->host;
  1569. break;
  1570. case CURLUPART_ZONEID:
  1571. storep = &u->zoneid;
  1572. break;
  1573. case CURLUPART_PORT:
  1574. u->portnum = 0;
  1575. storep = &u->port;
  1576. break;
  1577. case CURLUPART_PATH:
  1578. storep = &u->path;
  1579. break;
  1580. case CURLUPART_QUERY:
  1581. storep = &u->query;
  1582. u->query_present = FALSE;
  1583. break;
  1584. case CURLUPART_FRAGMENT:
  1585. storep = &u->fragment;
  1586. u->fragment_present = FALSE;
  1587. break;
  1588. default:
  1589. return CURLUE_UNKNOWN_PART;
  1590. }
  1591. if(storep && *storep) {
  1592. Curl_safefree(*storep);
  1593. }
  1594. else if(!storep) {
  1595. free_urlhandle(u);
  1596. memset(u, 0, sizeof(struct Curl_URL));
  1597. }
  1598. return CURLUE_OK;
  1599. }
  1600. nalloc = strlen(part);
  1601. if(nalloc > CURL_MAX_INPUT_LENGTH)
  1602. /* excessive input length */
  1603. return CURLUE_MALFORMED_INPUT;
  1604. switch(what) {
  1605. case CURLUPART_SCHEME: {
  1606. size_t plen = strlen(part);
  1607. const char *s = part;
  1608. if((plen > MAX_SCHEME_LEN) || (plen < 1))
  1609. /* too long or too short */
  1610. return CURLUE_BAD_SCHEME;
  1611. /* verify that it is a fine scheme */
  1612. if(!(flags & CURLU_NON_SUPPORT_SCHEME) && !Curl_get_scheme_handler(part))
  1613. return CURLUE_UNSUPPORTED_SCHEME;
  1614. storep = &u->scheme;
  1615. urlencode = FALSE; /* never */
  1616. if(ISALPHA(*s)) {
  1617. /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
  1618. while(--plen) {
  1619. if(ISALNUM(*s) || (*s == '+') || (*s == '-') || (*s == '.'))
  1620. s++; /* fine */
  1621. else
  1622. return CURLUE_BAD_SCHEME;
  1623. }
  1624. }
  1625. else
  1626. return CURLUE_BAD_SCHEME;
  1627. break;
  1628. }
  1629. case CURLUPART_USER:
  1630. storep = &u->user;
  1631. break;
  1632. case CURLUPART_PASSWORD:
  1633. storep = &u->password;
  1634. break;
  1635. case CURLUPART_OPTIONS:
  1636. storep = &u->options;
  1637. break;
  1638. case CURLUPART_HOST:
  1639. storep = &u->host;
  1640. Curl_safefree(u->zoneid);
  1641. break;
  1642. case CURLUPART_ZONEID:
  1643. storep = &u->zoneid;
  1644. break;
  1645. case CURLUPART_PORT:
  1646. if(!ISDIGIT(part[0]))
  1647. /* not a number */
  1648. return CURLUE_BAD_PORT_NUMBER;
  1649. else {
  1650. char *tmp;
  1651. char *endp;
  1652. unsigned long port;
  1653. errno = 0;
  1654. port = strtoul(part, &endp, 10); /* must be decimal */
  1655. if(errno || (port > 0xffff) || *endp)
  1656. /* weirdly provided number, not good! */
  1657. return CURLUE_BAD_PORT_NUMBER;
  1658. tmp = strdup(part);
  1659. if(!tmp)
  1660. return CURLUE_OUT_OF_MEMORY;
  1661. free(u->port);
  1662. u->port = tmp;
  1663. u->portnum = (unsigned short)port;
  1664. return CURLUE_OK;
  1665. }
  1666. case CURLUPART_PATH:
  1667. urlskipslash = TRUE;
  1668. leadingslash = TRUE; /* enforce */
  1669. storep = &u->path;
  1670. break;
  1671. case CURLUPART_QUERY:
  1672. plusencode = urlencode;
  1673. appendquery = (flags & CURLU_APPENDQUERY)?1:0;
  1674. equalsencode = appendquery;
  1675. storep = &u->query;
  1676. u->query_present = TRUE;
  1677. break;
  1678. case CURLUPART_FRAGMENT:
  1679. storep = &u->fragment;
  1680. u->fragment_present = TRUE;
  1681. break;
  1682. case CURLUPART_URL: {
  1683. /*
  1684. * Allow a new URL to replace the existing (if any) contents.
  1685. *
  1686. * If the existing contents is enough for a URL, allow a relative URL to
  1687. * replace it.
  1688. */
  1689. CURLcode result;
  1690. CURLUcode uc;
  1691. char *oldurl;
  1692. char *redired_url;
  1693. if(!nalloc)
  1694. /* a blank URL is not a valid URL */
  1695. return CURLUE_MALFORMED_INPUT;
  1696. /* if the new thing is absolute or the old one is not
  1697. * (we could not get an absolute url in 'oldurl'),
  1698. * then replace the existing with the new. */
  1699. if(Curl_is_absolute_url(part, NULL, 0,
  1700. flags & (CURLU_GUESS_SCHEME|
  1701. CURLU_DEFAULT_SCHEME))
  1702. || curl_url_get(u, CURLUPART_URL, &oldurl, flags)) {
  1703. return parseurl_and_replace(part, u, flags);
  1704. }
  1705. /* apply the relative part to create a new URL
  1706. * and replace the existing one with it. */
  1707. result = concat_url(oldurl, part, &redired_url);
  1708. free(oldurl);
  1709. if(result)
  1710. return cc2cu(result);
  1711. uc = parseurl_and_replace(redired_url, u, flags);
  1712. free(redired_url);
  1713. return uc;
  1714. }
  1715. default:
  1716. return CURLUE_UNKNOWN_PART;
  1717. }
  1718. DEBUGASSERT(storep);
  1719. {
  1720. const char *newp;
  1721. struct dynbuf enc;
  1722. Curl_dyn_init(&enc, nalloc * 3 + 1 + leadingslash);
  1723. if(leadingslash && (part[0] != '/')) {
  1724. CURLcode result = Curl_dyn_addn(&enc, "/", 1);
  1725. if(result)
  1726. return cc2cu(result);
  1727. }
  1728. if(urlencode) {
  1729. const unsigned char *i;
  1730. for(i = (const unsigned char *)part; *i; i++) {
  1731. CURLcode result;
  1732. if((*i == ' ') && plusencode) {
  1733. result = Curl_dyn_addn(&enc, "+", 1);
  1734. if(result)
  1735. return CURLUE_OUT_OF_MEMORY;
  1736. }
  1737. else if(ISUNRESERVED(*i) ||
  1738. ((*i == '/') && urlskipslash) ||
  1739. ((*i == '=') && equalsencode)) {
  1740. if((*i == '=') && equalsencode)
  1741. /* only skip the first equals sign */
  1742. equalsencode = FALSE;
  1743. result = Curl_dyn_addn(&enc, i, 1);
  1744. if(result)
  1745. return cc2cu(result);
  1746. }
  1747. else {
  1748. char out[3]={'%'};
  1749. out[1] = hexdigits[*i>>4];
  1750. out[2] = hexdigits[*i & 0xf];
  1751. result = Curl_dyn_addn(&enc, out, 3);
  1752. if(result)
  1753. return cc2cu(result);
  1754. }
  1755. }
  1756. }
  1757. else {
  1758. char *p;
  1759. CURLcode result = Curl_dyn_add(&enc, part);
  1760. if(result)
  1761. return cc2cu(result);
  1762. p = Curl_dyn_ptr(&enc);
  1763. while(*p) {
  1764. /* make sure percent encoded are lower case */
  1765. if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
  1766. (ISUPPER(p[1]) || ISUPPER(p[2]))) {
  1767. p[1] = Curl_raw_tolower(p[1]);
  1768. p[2] = Curl_raw_tolower(p[2]);
  1769. p += 3;
  1770. }
  1771. else
  1772. p++;
  1773. }
  1774. }
  1775. newp = Curl_dyn_ptr(&enc);
  1776. if(appendquery && newp) {
  1777. /* Append the 'newp' string onto the old query. Add a '&' separator if
  1778. none is present at the end of the existing query already */
  1779. size_t querylen = u->query ? strlen(u->query) : 0;
  1780. bool addamperand = querylen && (u->query[querylen -1] != '&');
  1781. if(querylen) {
  1782. struct dynbuf qbuf;
  1783. Curl_dyn_init(&qbuf, CURL_MAX_INPUT_LENGTH);
  1784. if(Curl_dyn_addn(&qbuf, u->query, querylen)) /* add original query */
  1785. goto nomem;
  1786. if(addamperand) {
  1787. if(Curl_dyn_addn(&qbuf, "&", 1))
  1788. goto nomem;
  1789. }
  1790. if(Curl_dyn_add(&qbuf, newp))
  1791. goto nomem;
  1792. Curl_dyn_free(&enc);
  1793. free(*storep);
  1794. *storep = Curl_dyn_ptr(&qbuf);
  1795. return CURLUE_OK;
  1796. nomem:
  1797. Curl_dyn_free(&enc);
  1798. return CURLUE_OUT_OF_MEMORY;
  1799. }
  1800. }
  1801. else if(what == CURLUPART_HOST) {
  1802. size_t n = Curl_dyn_len(&enc);
  1803. if(!n && (flags & CURLU_NO_AUTHORITY)) {
  1804. /* Skip hostname check, it's allowed to be empty. */
  1805. }
  1806. else {
  1807. if(!n || hostname_check(u, (char *)newp, n)) {
  1808. Curl_dyn_free(&enc);
  1809. return CURLUE_BAD_HOSTNAME;
  1810. }
  1811. }
  1812. }
  1813. free(*storep);
  1814. *storep = (char *)newp;
  1815. }
  1816. return CURLUE_OK;
  1817. }