urlapi.c 54 KB

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