2
0

urlapi.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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. /* The last 3 #include files should be in this order */
  36. #include "curl_printf.h"
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. /* MS-DOS/Windows style drive prefix, eg c: in c:foo */
  40. #define STARTS_WITH_DRIVE_PREFIX(str) \
  41. ((('a' <= str[0] && str[0] <= 'z') || \
  42. ('A' <= str[0] && str[0] <= 'Z')) && \
  43. (str[1] == ':'))
  44. /* MS-DOS/Windows style drive prefix, optionally with
  45. * a '|' instead of ':', followed by a slash or NUL */
  46. #define STARTS_WITH_URL_DRIVE_PREFIX(str) \
  47. ((('a' <= (str)[0] && (str)[0] <= 'z') || \
  48. ('A' <= (str)[0] && (str)[0] <= 'Z')) && \
  49. ((str)[1] == ':' || (str)[1] == '|') && \
  50. ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
  51. /* scheme is not URL encoded, the longest libcurl supported ones are... */
  52. #define MAX_SCHEME_LEN 40
  53. /*
  54. * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
  55. * sure we have _some_ value for AF_INET6 without polluting our fake value
  56. * everywhere.
  57. */
  58. #if !defined(USE_IPV6) && !defined(AF_INET6)
  59. #define AF_INET6 (AF_INET + 1)
  60. #endif
  61. /* Internal representation of CURLU. Point to URL-encoded strings. */
  62. struct Curl_URL {
  63. char *scheme;
  64. char *user;
  65. char *password;
  66. char *options; /* IMAP only? */
  67. char *host;
  68. char *zoneid; /* for numerical IPv6 addresses */
  69. char *port;
  70. char *path;
  71. char *query;
  72. char *fragment;
  73. unsigned short portnum; /* the numerical version (if 'port' is set) */
  74. BIT(query_present); /* to support blank */
  75. BIT(fragment_present); /* to support blank */
  76. BIT(guessed_scheme); /* when a URL without scheme is parsed */
  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 hostname, 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 hostnames, 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. size_t 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 hostname "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 hostname */
  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 is 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 is 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 is any slash after the hostname, 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 is 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 does
  321. not 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 (username, password and options) from the URL and
  376. * strip them out of the hostname
  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 hostname */
  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 hostname 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 is 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. Do not 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. /* Normalize the IPv6 address */
  540. {
  541. char dest[16]; /* fits a binary IPv6 address */
  542. hostname[hlen] = 0; /* end the address there */
  543. if(1 != Curl_inet_pton(AF_INET6, hostname, dest))
  544. return CURLUE_BAD_IPV6;
  545. if(Curl_inet_ntop(AF_INET6, dest, hostname, hlen)) {
  546. hlen = strlen(hostname); /* might be shorter now */
  547. hostname[hlen + 1] = 0;
  548. }
  549. hostname[hlen] = ']'; /* restore ending bracket */
  550. }
  551. return CURLUE_OK;
  552. }
  553. static CURLUcode hostname_check(struct Curl_URL *u, char *hostname,
  554. size_t hlen) /* length of hostname */
  555. {
  556. size_t len;
  557. DEBUGASSERT(hostname);
  558. if(!hlen)
  559. return CURLUE_NO_HOST;
  560. else if(hostname[0] == '[')
  561. return ipv6_parse(u, hostname, hlen);
  562. else {
  563. /* letters from the second string are not ok */
  564. len = strcspn(hostname, " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()%");
  565. if(hlen != len)
  566. /* hostname with bad content */
  567. return CURLUE_BAD_HOSTNAME;
  568. }
  569. return CURLUE_OK;
  570. }
  571. /*
  572. * Handle partial IPv4 numerical addresses and different bases, like
  573. * '16843009', '0x7f', '0x7f.1' '0177.1.1.1' etc.
  574. *
  575. * If the given input string is syntactically wrong IPv4 or any part for
  576. * example is too big, this function returns HOST_NAME.
  577. *
  578. * Output the "normalized" version of that input string in plain quad decimal
  579. * integers.
  580. *
  581. * Returns the host type.
  582. */
  583. #define HOST_ERROR -1 /* out of memory */
  584. #define HOST_NAME 1
  585. #define HOST_IPV4 2
  586. #define HOST_IPV6 3
  587. static int ipv4_normalize(struct dynbuf *host)
  588. {
  589. bool done = FALSE;
  590. int n = 0;
  591. const char *c = Curl_dyn_ptr(host);
  592. unsigned long parts[4] = {0, 0, 0, 0};
  593. CURLcode result = CURLE_OK;
  594. if(*c == '[')
  595. return HOST_IPV6;
  596. errno = 0; /* for strtoul */
  597. while(!done) {
  598. char *endp = NULL;
  599. unsigned long l;
  600. if(!ISDIGIT(*c))
  601. /* most importantly this does not allow a leading plus or minus */
  602. return HOST_NAME;
  603. l = strtoul(c, &endp, 0);
  604. if(errno)
  605. return HOST_NAME;
  606. #if SIZEOF_LONG > 4
  607. /* a value larger than 32 bits */
  608. if(l > UINT_MAX)
  609. return HOST_NAME;
  610. #endif
  611. parts[n] = l;
  612. c = endp;
  613. switch(*c) {
  614. case '.':
  615. if(n == 3)
  616. return HOST_NAME;
  617. n++;
  618. c++;
  619. break;
  620. case '\0':
  621. done = TRUE;
  622. break;
  623. default:
  624. return HOST_NAME;
  625. }
  626. }
  627. switch(n) {
  628. case 0: /* a -- 32 bits */
  629. Curl_dyn_reset(host);
  630. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  631. (unsigned int)(parts[0] >> 24),
  632. (unsigned int)((parts[0] >> 16) & 0xff),
  633. (unsigned int)((parts[0] >> 8) & 0xff),
  634. (unsigned int)(parts[0] & 0xff));
  635. break;
  636. case 1: /* a.b -- 8.24 bits */
  637. if((parts[0] > 0xff) || (parts[1] > 0xffffff))
  638. return HOST_NAME;
  639. Curl_dyn_reset(host);
  640. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  641. (unsigned int)(parts[0]),
  642. (unsigned int)((parts[1] >> 16) & 0xff),
  643. (unsigned int)((parts[1] >> 8) & 0xff),
  644. (unsigned int)(parts[1] & 0xff));
  645. break;
  646. case 2: /* a.b.c -- 8.8.16 bits */
  647. if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xffff))
  648. return HOST_NAME;
  649. Curl_dyn_reset(host);
  650. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  651. (unsigned int)(parts[0]),
  652. (unsigned int)(parts[1]),
  653. (unsigned int)((parts[2] >> 8) & 0xff),
  654. (unsigned int)(parts[2] & 0xff));
  655. break;
  656. case 3: /* a.b.c.d -- 8.8.8.8 bits */
  657. if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff) ||
  658. (parts[3] > 0xff))
  659. return HOST_NAME;
  660. Curl_dyn_reset(host);
  661. result = Curl_dyn_addf(host, "%u.%u.%u.%u",
  662. (unsigned int)(parts[0]),
  663. (unsigned int)(parts[1]),
  664. (unsigned int)(parts[2]),
  665. (unsigned int)(parts[3]));
  666. break;
  667. }
  668. if(result)
  669. return HOST_ERROR;
  670. return HOST_IPV4;
  671. }
  672. /* if necessary, replace the host content with a URL decoded version */
  673. static CURLUcode urldecode_host(struct dynbuf *host)
  674. {
  675. char *per = NULL;
  676. const char *hostname = Curl_dyn_ptr(host);
  677. per = strchr(hostname, '%');
  678. if(!per)
  679. /* nothing to decode */
  680. return CURLUE_OK;
  681. else {
  682. /* encoded */
  683. size_t dlen;
  684. char *decoded;
  685. CURLcode result = Curl_urldecode(hostname, 0, &decoded, &dlen,
  686. REJECT_CTRL);
  687. if(result)
  688. return CURLUE_BAD_HOSTNAME;
  689. Curl_dyn_reset(host);
  690. result = Curl_dyn_addn(host, decoded, dlen);
  691. free(decoded);
  692. if(result)
  693. return cc2cu(result);
  694. }
  695. return CURLUE_OK;
  696. }
  697. static CURLUcode parse_authority(struct Curl_URL *u,
  698. const char *auth, size_t authlen,
  699. unsigned int flags,
  700. struct dynbuf *host,
  701. bool has_scheme)
  702. {
  703. size_t offset;
  704. CURLUcode uc;
  705. CURLcode result;
  706. /*
  707. * Parse the login details and strip them out of the hostname.
  708. */
  709. uc = parse_hostname_login(u, auth, authlen, flags, &offset);
  710. if(uc)
  711. goto out;
  712. result = Curl_dyn_addn(host, auth + offset, authlen - offset);
  713. if(result) {
  714. uc = cc2cu(result);
  715. goto out;
  716. }
  717. uc = Curl_parse_port(u, host, has_scheme);
  718. if(uc)
  719. goto out;
  720. if(!Curl_dyn_len(host))
  721. return CURLUE_NO_HOST;
  722. switch(ipv4_normalize(host)) {
  723. case HOST_IPV4:
  724. break;
  725. case HOST_IPV6:
  726. uc = ipv6_parse(u, Curl_dyn_ptr(host), Curl_dyn_len(host));
  727. break;
  728. case HOST_NAME:
  729. uc = urldecode_host(host);
  730. if(!uc)
  731. uc = hostname_check(u, Curl_dyn_ptr(host), Curl_dyn_len(host));
  732. break;
  733. case HOST_ERROR:
  734. uc = CURLUE_OUT_OF_MEMORY;
  735. break;
  736. default:
  737. uc = CURLUE_BAD_HOSTNAME; /* Bad IPv4 address even */
  738. break;
  739. }
  740. out:
  741. return uc;
  742. }
  743. /* used for HTTP/2 server push */
  744. CURLUcode Curl_url_set_authority(CURLU *u, const char *authority)
  745. {
  746. CURLUcode result;
  747. struct dynbuf host;
  748. DEBUGASSERT(authority);
  749. Curl_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
  750. result = parse_authority(u, authority, strlen(authority),
  751. CURLU_DISALLOW_USER, &host, !!u->scheme);
  752. if(result)
  753. Curl_dyn_free(&host);
  754. else {
  755. free(u->host);
  756. u->host = Curl_dyn_ptr(&host);
  757. }
  758. return result;
  759. }
  760. /*
  761. * "Remove Dot Segments"
  762. * https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
  763. */
  764. /*
  765. * dedotdotify()
  766. * @unittest: 1395
  767. *
  768. * This function gets a null-terminated path with dot and dotdot sequences
  769. * passed in and strips them off according to the rules in RFC 3986 section
  770. * 5.2.4.
  771. *
  772. * The function handles a query part ('?' + stuff) appended but it expects
  773. * that fragments ('#' + stuff) have already been cut off.
  774. *
  775. * RETURNS
  776. *
  777. * Zero for success and 'out' set to an allocated dedotdotified string.
  778. */
  779. UNITTEST int dedotdotify(const char *input, size_t clen, char **outp);
  780. UNITTEST int dedotdotify(const char *input, size_t clen, char **outp)
  781. {
  782. char *outptr;
  783. const char *endp = &input[clen];
  784. char *out;
  785. *outp = NULL;
  786. /* the path always starts with a slash, and a slash has not dot */
  787. if((clen < 2) || !memchr(input, '.', clen))
  788. return 0;
  789. out = malloc(clen + 1);
  790. if(!out)
  791. return 1; /* out of memory */
  792. *out = 0; /* null-terminates, for inputs like "./" */
  793. outptr = out;
  794. do {
  795. bool dotdot = TRUE;
  796. if(*input == '.') {
  797. /* A. If the input buffer begins with a prefix of "../" or "./", then
  798. remove that prefix from the input buffer; otherwise, */
  799. if(!strncmp("./", input, 2)) {
  800. input += 2;
  801. clen -= 2;
  802. }
  803. else if(!strncmp("../", input, 3)) {
  804. input += 3;
  805. clen -= 3;
  806. }
  807. /* D. if the input buffer consists only of "." or "..", then remove
  808. that from the input buffer; otherwise, */
  809. else if(!strcmp(".", input) || !strcmp("..", input) ||
  810. !strncmp(".?", input, 2) || !strncmp("..?", input, 3)) {
  811. *out = 0;
  812. break;
  813. }
  814. else
  815. dotdot = FALSE;
  816. }
  817. else if(*input == '/') {
  818. /* B. if the input buffer begins with a prefix of "/./" or "/.", where
  819. "." is a complete path segment, then replace that prefix with "/" in
  820. the input buffer; otherwise, */
  821. if(!strncmp("/./", input, 3)) {
  822. input += 2;
  823. clen -= 2;
  824. }
  825. else if(!strcmp("/.", input) || !strncmp("/.?", input, 3)) {
  826. *outptr++ = '/';
  827. *outptr = 0;
  828. break;
  829. }
  830. /* C. if the input buffer begins with a prefix of "/../" or "/..",
  831. where ".." is a complete path segment, then replace that prefix with
  832. "/" in the input buffer and remove the last segment and its
  833. preceding "/" (if any) from the output buffer; otherwise, */
  834. else if(!strncmp("/../", input, 4)) {
  835. input += 3;
  836. clen -= 3;
  837. /* remove the last segment from the output buffer */
  838. while(outptr > out) {
  839. outptr--;
  840. if(*outptr == '/')
  841. break;
  842. }
  843. *outptr = 0; /* null-terminate where it stops */
  844. }
  845. else if(!strcmp("/..", input) || !strncmp("/..?", input, 4)) {
  846. /* remove the last segment from the output buffer */
  847. while(outptr > out) {
  848. outptr--;
  849. if(*outptr == '/')
  850. break;
  851. }
  852. *outptr++ = '/';
  853. *outptr = 0; /* null-terminate where it stops */
  854. break;
  855. }
  856. else
  857. dotdot = FALSE;
  858. }
  859. else
  860. dotdot = FALSE;
  861. if(!dotdot) {
  862. /* E. move the first path segment in the input buffer to the end of
  863. the output buffer, including the initial "/" character (if any) and
  864. any subsequent characters up to, but not including, the next "/"
  865. character or the end of the input buffer. */
  866. do {
  867. *outptr++ = *input++;
  868. clen--;
  869. } while(*input && (*input != '/') && (*input != '?'));
  870. *outptr = 0;
  871. }
  872. /* continue until end of path */
  873. } while(input < endp);
  874. *outp = out;
  875. return 0; /* success */
  876. }
  877. static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
  878. {
  879. const char *path;
  880. size_t pathlen;
  881. char *query = NULL;
  882. char *fragment = NULL;
  883. char schemebuf[MAX_SCHEME_LEN + 1];
  884. size_t schemelen = 0;
  885. size_t urllen;
  886. CURLUcode result = CURLUE_OK;
  887. size_t fraglen = 0;
  888. struct dynbuf host;
  889. DEBUGASSERT(url);
  890. Curl_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
  891. result = junkscan(url, &urllen, flags);
  892. if(result)
  893. goto fail;
  894. schemelen = Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf),
  895. flags & (CURLU_GUESS_SCHEME|
  896. CURLU_DEFAULT_SCHEME));
  897. /* handle the file: scheme */
  898. if(schemelen && !strcmp(schemebuf, "file")) {
  899. bool uncpath = FALSE;
  900. if(urllen <= 6) {
  901. /* file:/ is not enough to actually be a complete file: URL */
  902. result = CURLUE_BAD_FILE_URL;
  903. goto fail;
  904. }
  905. /* path has been allocated large enough to hold this */
  906. path = (char *)&url[5];
  907. pathlen = urllen - 5;
  908. u->scheme = strdup("file");
  909. if(!u->scheme) {
  910. result = CURLUE_OUT_OF_MEMORY;
  911. goto fail;
  912. }
  913. /* Extra handling URLs with an authority component (i.e. that start with
  914. * "file://")
  915. *
  916. * We allow omitted hostname (e.g. file:/<path>) -- valid according to
  917. * RFC 8089, but not the (current) WHAT-WG URL spec.
  918. */
  919. if(path[0] == '/' && path[1] == '/') {
  920. /* swallow the two slashes */
  921. const char *ptr = &path[2];
  922. /*
  923. * According to RFC 8089, a file: URL can be reliably dereferenced if:
  924. *
  925. * o it has no/blank hostname, or
  926. *
  927. * o the hostname matches "localhost" (case-insensitively), or
  928. *
  929. * o the hostname is a FQDN that resolves to this machine, or
  930. *
  931. * o it is an UNC String transformed to an URI (Windows only, RFC 8089
  932. * Appendix E.3).
  933. *
  934. * For brevity, we only consider URLs with empty, "localhost", or
  935. * "127.0.0.1" hostnames as local, otherwise as an UNC String.
  936. *
  937. * Additionally, there is an exception for URLs with a Windows drive
  938. * letter in the authority (which was accidentally omitted from RFC 8089
  939. * Appendix E, but believe me, it was meant to be there. --MK)
  940. */
  941. if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
  942. /* the URL includes a hostname, it must match "localhost" or
  943. "127.0.0.1" to be valid */
  944. if(checkprefix("localhost/", ptr) ||
  945. checkprefix("127.0.0.1/", ptr)) {
  946. ptr += 9; /* now points to the slash after the host */
  947. }
  948. else {
  949. #if defined(_WIN32)
  950. size_t len;
  951. /* the hostname, NetBIOS computer name, can not contain disallowed
  952. chars, and the delimiting slash character must be appended to the
  953. hostname */
  954. path = strpbrk(ptr, "/\\:*?\"<>|");
  955. if(!path || *path != '/') {
  956. result = CURLUE_BAD_FILE_URL;
  957. goto fail;
  958. }
  959. len = path - ptr;
  960. if(len) {
  961. CURLcode code = Curl_dyn_addn(&host, ptr, len);
  962. if(code) {
  963. result = cc2cu(code);
  964. goto fail;
  965. }
  966. uncpath = TRUE;
  967. }
  968. ptr -= 2; /* now points to the // before the host in UNC */
  969. #else
  970. /* Invalid file://hostname/, expected localhost or 127.0.0.1 or
  971. none */
  972. result = CURLUE_BAD_FILE_URL;
  973. goto fail;
  974. #endif
  975. }
  976. }
  977. path = ptr;
  978. pathlen = urllen - (ptr - url);
  979. }
  980. if(!uncpath)
  981. /* no host for file: URLs by default */
  982. Curl_dyn_reset(&host);
  983. #if !defined(_WIN32) && !defined(MSDOS) && !defined(__CYGWIN__)
  984. /* Do not allow Windows drive letters when not in Windows.
  985. * This catches both "file:/c:" and "file:c:" */
  986. if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
  987. STARTS_WITH_URL_DRIVE_PREFIX(path)) {
  988. /* File drive letters are only accepted in MS-DOS/Windows */
  989. result = CURLUE_BAD_FILE_URL;
  990. goto fail;
  991. }
  992. #else
  993. /* If the path starts with a slash and a drive letter, ditch the slash */
  994. if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
  995. /* This cannot be done with strcpy, as the memory chunks overlap! */
  996. path++;
  997. pathlen--;
  998. }
  999. #endif
  1000. }
  1001. else {
  1002. /* clear path */
  1003. const char *schemep = NULL;
  1004. const char *hostp;
  1005. size_t hostlen;
  1006. if(schemelen) {
  1007. int i = 0;
  1008. const char *p = &url[schemelen + 1];
  1009. while((*p == '/') && (i < 4)) {
  1010. p++;
  1011. i++;
  1012. }
  1013. schemep = schemebuf;
  1014. if(!Curl_get_scheme_handler(schemep) &&
  1015. !(flags & CURLU_NON_SUPPORT_SCHEME)) {
  1016. result = CURLUE_UNSUPPORTED_SCHEME;
  1017. goto fail;
  1018. }
  1019. if((i < 1) || (i > 3)) {
  1020. /* less than one or more than three slashes */
  1021. result = CURLUE_BAD_SLASHES;
  1022. goto fail;
  1023. }
  1024. hostp = p; /* hostname starts here */
  1025. }
  1026. else {
  1027. /* no scheme! */
  1028. if(!(flags & (CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME))) {
  1029. result = CURLUE_BAD_SCHEME;
  1030. goto fail;
  1031. }
  1032. if(flags & CURLU_DEFAULT_SCHEME)
  1033. schemep = DEFAULT_SCHEME;
  1034. /*
  1035. * The URL was badly formatted, let's try without scheme specified.
  1036. */
  1037. hostp = url;
  1038. }
  1039. if(schemep) {
  1040. u->scheme = strdup(schemep);
  1041. if(!u->scheme) {
  1042. result = CURLUE_OUT_OF_MEMORY;
  1043. goto fail;
  1044. }
  1045. }
  1046. /* find the end of the hostname + port number */
  1047. hostlen = strcspn(hostp, "/?#");
  1048. path = &hostp[hostlen];
  1049. /* this pathlen also contains the query and the fragment */
  1050. pathlen = urllen - (path - url);
  1051. if(hostlen) {
  1052. result = parse_authority(u, hostp, hostlen, flags, &host, schemelen);
  1053. if(result)
  1054. goto fail;
  1055. if((flags & CURLU_GUESS_SCHEME) && !schemep) {
  1056. const char *hostname = Curl_dyn_ptr(&host);
  1057. /* legacy curl-style guess based on hostname */
  1058. if(checkprefix("ftp.", hostname))
  1059. schemep = "ftp";
  1060. else if(checkprefix("dict.", hostname))
  1061. schemep = "dict";
  1062. else if(checkprefix("ldap.", hostname))
  1063. schemep = "ldap";
  1064. else if(checkprefix("imap.", hostname))
  1065. schemep = "imap";
  1066. else if(checkprefix("smtp.", hostname))
  1067. schemep = "smtp";
  1068. else if(checkprefix("pop3.", hostname))
  1069. schemep = "pop3";
  1070. else
  1071. schemep = "http";
  1072. u->scheme = strdup(schemep);
  1073. if(!u->scheme) {
  1074. result = CURLUE_OUT_OF_MEMORY;
  1075. goto fail;
  1076. }
  1077. u->guessed_scheme = TRUE;
  1078. }
  1079. }
  1080. else if(flags & CURLU_NO_AUTHORITY) {
  1081. /* allowed to be empty. */
  1082. if(Curl_dyn_add(&host, "")) {
  1083. result = CURLUE_OUT_OF_MEMORY;
  1084. goto fail;
  1085. }
  1086. }
  1087. else {
  1088. result = CURLUE_NO_HOST;
  1089. goto fail;
  1090. }
  1091. }
  1092. fragment = strchr(path, '#');
  1093. if(fragment) {
  1094. fraglen = pathlen - (fragment - path);
  1095. u->fragment_present = TRUE;
  1096. if(fraglen > 1) {
  1097. /* skip the leading '#' in the copy but include the terminating null */
  1098. if(flags & CURLU_URLENCODE) {
  1099. struct dynbuf enc;
  1100. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1101. result = urlencode_str(&enc, fragment + 1, fraglen - 1, TRUE, FALSE);
  1102. if(result)
  1103. goto fail;
  1104. u->fragment = Curl_dyn_ptr(&enc);
  1105. }
  1106. else {
  1107. u->fragment = Curl_memdup0(fragment + 1, fraglen - 1);
  1108. if(!u->fragment) {
  1109. result = CURLUE_OUT_OF_MEMORY;
  1110. goto fail;
  1111. }
  1112. }
  1113. }
  1114. /* after this, pathlen still contains the query */
  1115. pathlen -= fraglen;
  1116. }
  1117. query = memchr(path, '?', pathlen);
  1118. if(query) {
  1119. size_t qlen = fragment ? (size_t)(fragment - query) :
  1120. pathlen - (query - path);
  1121. pathlen -= qlen;
  1122. u->query_present = TRUE;
  1123. if(qlen > 1) {
  1124. if(flags & CURLU_URLENCODE) {
  1125. struct dynbuf enc;
  1126. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1127. /* skip the leading question mark */
  1128. result = urlencode_str(&enc, query + 1, qlen - 1, TRUE, TRUE);
  1129. if(result)
  1130. goto fail;
  1131. u->query = Curl_dyn_ptr(&enc);
  1132. }
  1133. else {
  1134. u->query = Curl_memdup0(query + 1, qlen - 1);
  1135. if(!u->query) {
  1136. result = CURLUE_OUT_OF_MEMORY;
  1137. goto fail;
  1138. }
  1139. }
  1140. }
  1141. else {
  1142. /* single byte query */
  1143. u->query = strdup("");
  1144. if(!u->query) {
  1145. result = CURLUE_OUT_OF_MEMORY;
  1146. goto fail;
  1147. }
  1148. }
  1149. }
  1150. if(pathlen && (flags & CURLU_URLENCODE)) {
  1151. struct dynbuf enc;
  1152. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1153. result = urlencode_str(&enc, path, pathlen, TRUE, FALSE);
  1154. if(result)
  1155. goto fail;
  1156. pathlen = Curl_dyn_len(&enc);
  1157. path = u->path = Curl_dyn_ptr(&enc);
  1158. }
  1159. if(pathlen <= 1) {
  1160. /* there is no path left or just the slash, unset */
  1161. path = NULL;
  1162. }
  1163. else {
  1164. if(!u->path) {
  1165. u->path = Curl_memdup0(path, pathlen);
  1166. if(!u->path) {
  1167. result = CURLUE_OUT_OF_MEMORY;
  1168. goto fail;
  1169. }
  1170. path = u->path;
  1171. }
  1172. else if(flags & CURLU_URLENCODE)
  1173. /* it might have encoded more than just the path so cut it */
  1174. u->path[pathlen] = 0;
  1175. if(!(flags & CURLU_PATH_AS_IS)) {
  1176. /* remove ../ and ./ sequences according to RFC3986 */
  1177. char *dedot;
  1178. int err = dedotdotify((char *)path, pathlen, &dedot);
  1179. if(err) {
  1180. result = CURLUE_OUT_OF_MEMORY;
  1181. goto fail;
  1182. }
  1183. if(dedot) {
  1184. free(u->path);
  1185. u->path = dedot;
  1186. }
  1187. }
  1188. }
  1189. u->host = Curl_dyn_ptr(&host);
  1190. return result;
  1191. fail:
  1192. Curl_dyn_free(&host);
  1193. free_urlhandle(u);
  1194. return result;
  1195. }
  1196. /*
  1197. * Parse the URL and, if successful, replace everything in the Curl_URL struct.
  1198. */
  1199. static CURLUcode parseurl_and_replace(const char *url, CURLU *u,
  1200. unsigned int flags)
  1201. {
  1202. CURLUcode result;
  1203. CURLU tmpurl;
  1204. memset(&tmpurl, 0, sizeof(tmpurl));
  1205. result = parseurl(url, &tmpurl, flags);
  1206. if(!result) {
  1207. free_urlhandle(u);
  1208. *u = tmpurl;
  1209. }
  1210. return result;
  1211. }
  1212. /*
  1213. */
  1214. CURLU *curl_url(void)
  1215. {
  1216. return calloc(1, sizeof(struct Curl_URL));
  1217. }
  1218. void curl_url_cleanup(CURLU *u)
  1219. {
  1220. if(u) {
  1221. free_urlhandle(u);
  1222. free(u);
  1223. }
  1224. }
  1225. #define DUP(dest, src, name) \
  1226. do { \
  1227. if(src->name) { \
  1228. dest->name = strdup(src->name); \
  1229. if(!dest->name) \
  1230. goto fail; \
  1231. } \
  1232. } while(0)
  1233. CURLU *curl_url_dup(const CURLU *in)
  1234. {
  1235. struct Curl_URL *u = calloc(1, sizeof(struct Curl_URL));
  1236. if(u) {
  1237. DUP(u, in, scheme);
  1238. DUP(u, in, user);
  1239. DUP(u, in, password);
  1240. DUP(u, in, options);
  1241. DUP(u, in, host);
  1242. DUP(u, in, port);
  1243. DUP(u, in, path);
  1244. DUP(u, in, query);
  1245. DUP(u, in, fragment);
  1246. DUP(u, in, zoneid);
  1247. u->portnum = in->portnum;
  1248. u->fragment_present = in->fragment_present;
  1249. u->query_present = in->query_present;
  1250. }
  1251. return u;
  1252. fail:
  1253. curl_url_cleanup(u);
  1254. return NULL;
  1255. }
  1256. CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
  1257. char **part, unsigned int flags)
  1258. {
  1259. const char *ptr;
  1260. CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
  1261. char portbuf[7];
  1262. bool urldecode = (flags & CURLU_URLDECODE) ? 1 : 0;
  1263. bool urlencode = (flags & CURLU_URLENCODE) ? 1 : 0;
  1264. bool punycode = FALSE;
  1265. bool depunyfy = FALSE;
  1266. bool plusdecode = FALSE;
  1267. (void)flags;
  1268. if(!u)
  1269. return CURLUE_BAD_HANDLE;
  1270. if(!part)
  1271. return CURLUE_BAD_PARTPOINTER;
  1272. *part = NULL;
  1273. switch(what) {
  1274. case CURLUPART_SCHEME:
  1275. ptr = u->scheme;
  1276. ifmissing = CURLUE_NO_SCHEME;
  1277. urldecode = FALSE; /* never for schemes */
  1278. if((flags & CURLU_NO_GUESS_SCHEME) && u->guessed_scheme)
  1279. return CURLUE_NO_SCHEME;
  1280. break;
  1281. case CURLUPART_USER:
  1282. ptr = u->user;
  1283. ifmissing = CURLUE_NO_USER;
  1284. break;
  1285. case CURLUPART_PASSWORD:
  1286. ptr = u->password;
  1287. ifmissing = CURLUE_NO_PASSWORD;
  1288. break;
  1289. case CURLUPART_OPTIONS:
  1290. ptr = u->options;
  1291. ifmissing = CURLUE_NO_OPTIONS;
  1292. break;
  1293. case CURLUPART_HOST:
  1294. ptr = u->host;
  1295. ifmissing = CURLUE_NO_HOST;
  1296. punycode = (flags & CURLU_PUNYCODE) ? 1 : 0;
  1297. depunyfy = (flags & CURLU_PUNY2IDN) ? 1 : 0;
  1298. break;
  1299. case CURLUPART_ZONEID:
  1300. ptr = u->zoneid;
  1301. ifmissing = CURLUE_NO_ZONEID;
  1302. break;
  1303. case CURLUPART_PORT:
  1304. ptr = u->port;
  1305. ifmissing = CURLUE_NO_PORT;
  1306. urldecode = FALSE; /* never for port */
  1307. if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
  1308. /* there is no stored port number, but asked to deliver
  1309. a default one for the scheme */
  1310. const struct Curl_handler *h = Curl_get_scheme_handler(u->scheme);
  1311. if(h) {
  1312. msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
  1313. ptr = portbuf;
  1314. }
  1315. }
  1316. else if(ptr && u->scheme) {
  1317. /* there is a stored port number, but ask to inhibit if
  1318. it matches the default one for the scheme */
  1319. const struct Curl_handler *h = Curl_get_scheme_handler(u->scheme);
  1320. if(h && (h->defport == u->portnum) &&
  1321. (flags & CURLU_NO_DEFAULT_PORT))
  1322. ptr = NULL;
  1323. }
  1324. break;
  1325. case CURLUPART_PATH:
  1326. ptr = u->path;
  1327. if(!ptr)
  1328. ptr = "/";
  1329. break;
  1330. case CURLUPART_QUERY:
  1331. ptr = u->query;
  1332. ifmissing = CURLUE_NO_QUERY;
  1333. plusdecode = urldecode;
  1334. if(ptr && !ptr[0] && !(flags & CURLU_GET_EMPTY))
  1335. /* there was a blank query and the user do not ask for it */
  1336. ptr = NULL;
  1337. break;
  1338. case CURLUPART_FRAGMENT:
  1339. ptr = u->fragment;
  1340. ifmissing = CURLUE_NO_FRAGMENT;
  1341. if(!ptr && u->fragment_present && flags & CURLU_GET_EMPTY)
  1342. /* there was a blank fragment and the user asks for it */
  1343. ptr = "";
  1344. break;
  1345. case CURLUPART_URL: {
  1346. char *url;
  1347. char *scheme;
  1348. char *options = u->options;
  1349. char *port = u->port;
  1350. char *allochost = NULL;
  1351. bool show_fragment =
  1352. u->fragment || (u->fragment_present && flags & CURLU_GET_EMPTY);
  1353. bool show_query =
  1354. (u->query && u->query[0]) ||
  1355. (u->query_present && flags & CURLU_GET_EMPTY);
  1356. punycode = (flags & CURLU_PUNYCODE) ? 1 : 0;
  1357. depunyfy = (flags & CURLU_PUNY2IDN) ? 1 : 0;
  1358. if(u->scheme && strcasecompare("file", u->scheme)) {
  1359. url = aprintf("file://%s%s%s",
  1360. u->path,
  1361. show_fragment ? "#": "",
  1362. u->fragment ? u->fragment : "");
  1363. }
  1364. else if(!u->host)
  1365. return CURLUE_NO_HOST;
  1366. else {
  1367. const struct Curl_handler *h = NULL;
  1368. char schemebuf[MAX_SCHEME_LEN + 5];
  1369. if(u->scheme)
  1370. scheme = u->scheme;
  1371. else if(flags & CURLU_DEFAULT_SCHEME)
  1372. scheme = (char *) DEFAULT_SCHEME;
  1373. else
  1374. return CURLUE_NO_SCHEME;
  1375. h = Curl_get_scheme_handler(scheme);
  1376. if(!port && (flags & CURLU_DEFAULT_PORT)) {
  1377. /* there is no stored port number, but asked to deliver
  1378. a default one for the scheme */
  1379. if(h) {
  1380. msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
  1381. port = portbuf;
  1382. }
  1383. }
  1384. else if(port) {
  1385. /* there is a stored port number, but asked to inhibit if it matches
  1386. the default one for the scheme */
  1387. if(h && (h->defport == u->portnum) &&
  1388. (flags & CURLU_NO_DEFAULT_PORT))
  1389. port = NULL;
  1390. }
  1391. if(h && !(h->flags & PROTOPT_URLOPTIONS))
  1392. options = NULL;
  1393. if(u->host[0] == '[') {
  1394. if(u->zoneid) {
  1395. /* make it '[ host %25 zoneid ]' */
  1396. struct dynbuf enc;
  1397. size_t hostlen = strlen(u->host);
  1398. Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
  1399. if(Curl_dyn_addf(&enc, "%.*s%%25%s]", (int)hostlen - 1, u->host,
  1400. u->zoneid))
  1401. return CURLUE_OUT_OF_MEMORY;
  1402. allochost = Curl_dyn_ptr(&enc);
  1403. }
  1404. }
  1405. else if(urlencode) {
  1406. allochost = curl_easy_escape(NULL, u->host, 0);
  1407. if(!allochost)
  1408. return CURLUE_OUT_OF_MEMORY;
  1409. }
  1410. else if(punycode) {
  1411. if(!Curl_is_ASCII_name(u->host)) {
  1412. #ifndef USE_IDN
  1413. return CURLUE_LACKS_IDN;
  1414. #else
  1415. CURLcode result = Curl_idn_decode(u->host, &allochost);
  1416. if(result)
  1417. return (result == CURLE_OUT_OF_MEMORY) ?
  1418. CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
  1419. #endif
  1420. }
  1421. }
  1422. else if(depunyfy) {
  1423. if(Curl_is_ASCII_name(u->host) && !strncmp("xn--", u->host, 4)) {
  1424. #ifndef USE_IDN
  1425. return CURLUE_LACKS_IDN;
  1426. #else
  1427. CURLcode result = Curl_idn_encode(u->host, &allochost);
  1428. if(result)
  1429. /* this is the most likely error */
  1430. return (result == CURLE_OUT_OF_MEMORY) ?
  1431. CURLUE_OUT_OF_MEMORY : CURLUE_BAD_HOSTNAME;
  1432. #endif
  1433. }
  1434. }
  1435. if(!(flags & CURLU_NO_GUESS_SCHEME) || !u->guessed_scheme)
  1436. msnprintf(schemebuf, sizeof(schemebuf), "%s://", scheme);
  1437. else
  1438. schemebuf[0] = 0;
  1439. url = aprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1440. schemebuf,
  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. u->guessed_scheme = FALSE;
  1560. break;
  1561. case CURLUPART_USER:
  1562. storep = &u->user;
  1563. break;
  1564. case CURLUPART_PASSWORD:
  1565. storep = &u->password;
  1566. break;
  1567. case CURLUPART_OPTIONS:
  1568. storep = &u->options;
  1569. break;
  1570. case CURLUPART_HOST:
  1571. storep = &u->host;
  1572. break;
  1573. case CURLUPART_ZONEID:
  1574. storep = &u->zoneid;
  1575. break;
  1576. case CURLUPART_PORT:
  1577. u->portnum = 0;
  1578. storep = &u->port;
  1579. break;
  1580. case CURLUPART_PATH:
  1581. storep = &u->path;
  1582. break;
  1583. case CURLUPART_QUERY:
  1584. storep = &u->query;
  1585. u->query_present = FALSE;
  1586. break;
  1587. case CURLUPART_FRAGMENT:
  1588. storep = &u->fragment;
  1589. u->fragment_present = FALSE;
  1590. break;
  1591. default:
  1592. return CURLUE_UNKNOWN_PART;
  1593. }
  1594. if(storep && *storep) {
  1595. Curl_safefree(*storep);
  1596. }
  1597. else if(!storep) {
  1598. free_urlhandle(u);
  1599. memset(u, 0, sizeof(struct Curl_URL));
  1600. }
  1601. return CURLUE_OK;
  1602. }
  1603. nalloc = strlen(part);
  1604. if(nalloc > CURL_MAX_INPUT_LENGTH)
  1605. /* excessive input length */
  1606. return CURLUE_MALFORMED_INPUT;
  1607. switch(what) {
  1608. case CURLUPART_SCHEME: {
  1609. size_t plen = strlen(part);
  1610. const char *s = part;
  1611. if((plen > MAX_SCHEME_LEN) || (plen < 1))
  1612. /* too long or too short */
  1613. return CURLUE_BAD_SCHEME;
  1614. /* verify that it is a fine scheme */
  1615. if(!(flags & CURLU_NON_SUPPORT_SCHEME) && !Curl_get_scheme_handler(part))
  1616. return CURLUE_UNSUPPORTED_SCHEME;
  1617. storep = &u->scheme;
  1618. urlencode = FALSE; /* never */
  1619. if(ISALPHA(*s)) {
  1620. /* ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
  1621. while(--plen) {
  1622. if(ISALNUM(*s) || (*s == '+') || (*s == '-') || (*s == '.'))
  1623. s++; /* fine */
  1624. else
  1625. return CURLUE_BAD_SCHEME;
  1626. }
  1627. }
  1628. else
  1629. return CURLUE_BAD_SCHEME;
  1630. u->guessed_scheme = FALSE;
  1631. break;
  1632. }
  1633. case CURLUPART_USER:
  1634. storep = &u->user;
  1635. break;
  1636. case CURLUPART_PASSWORD:
  1637. storep = &u->password;
  1638. break;
  1639. case CURLUPART_OPTIONS:
  1640. storep = &u->options;
  1641. break;
  1642. case CURLUPART_HOST:
  1643. storep = &u->host;
  1644. Curl_safefree(u->zoneid);
  1645. break;
  1646. case CURLUPART_ZONEID:
  1647. storep = &u->zoneid;
  1648. break;
  1649. case CURLUPART_PORT:
  1650. if(!ISDIGIT(part[0]))
  1651. /* not a number */
  1652. return CURLUE_BAD_PORT_NUMBER;
  1653. else {
  1654. char *tmp;
  1655. char *endp;
  1656. unsigned long port;
  1657. errno = 0;
  1658. port = strtoul(part, &endp, 10); /* must be decimal */
  1659. if(errno || (port > 0xffff) || *endp)
  1660. /* weirdly provided number, not good! */
  1661. return CURLUE_BAD_PORT_NUMBER;
  1662. tmp = strdup(part);
  1663. if(!tmp)
  1664. return CURLUE_OUT_OF_MEMORY;
  1665. free(u->port);
  1666. u->port = tmp;
  1667. u->portnum = (unsigned short)port;
  1668. return CURLUE_OK;
  1669. }
  1670. case CURLUPART_PATH:
  1671. urlskipslash = TRUE;
  1672. leadingslash = TRUE; /* enforce */
  1673. storep = &u->path;
  1674. break;
  1675. case CURLUPART_QUERY:
  1676. plusencode = urlencode;
  1677. appendquery = (flags & CURLU_APPENDQUERY) ? 1 : 0;
  1678. equalsencode = appendquery;
  1679. storep = &u->query;
  1680. u->query_present = TRUE;
  1681. break;
  1682. case CURLUPART_FRAGMENT:
  1683. storep = &u->fragment;
  1684. u->fragment_present = TRUE;
  1685. break;
  1686. case CURLUPART_URL: {
  1687. /*
  1688. * Allow a new URL to replace the existing (if any) contents.
  1689. *
  1690. * If the existing contents is enough for a URL, allow a relative URL to
  1691. * replace it.
  1692. */
  1693. CURLcode result;
  1694. CURLUcode uc;
  1695. char *oldurl;
  1696. char *redired_url;
  1697. if(!nalloc)
  1698. /* a blank URL is not a valid URL */
  1699. return CURLUE_MALFORMED_INPUT;
  1700. /* if the new thing is absolute or the old one is not
  1701. * (we could not get an absolute URL in 'oldurl'),
  1702. * then replace the existing with the new. */
  1703. if(Curl_is_absolute_url(part, NULL, 0,
  1704. flags & (CURLU_GUESS_SCHEME|
  1705. CURLU_DEFAULT_SCHEME))
  1706. || curl_url_get(u, CURLUPART_URL, &oldurl, flags)) {
  1707. return parseurl_and_replace(part, u, flags);
  1708. }
  1709. /* apply the relative part to create a new URL
  1710. * and replace the existing one with it. */
  1711. result = concat_url(oldurl, part, &redired_url);
  1712. free(oldurl);
  1713. if(result)
  1714. return cc2cu(result);
  1715. uc = parseurl_and_replace(redired_url, u, flags);
  1716. free(redired_url);
  1717. return uc;
  1718. }
  1719. default:
  1720. return CURLUE_UNKNOWN_PART;
  1721. }
  1722. DEBUGASSERT(storep);
  1723. {
  1724. const char *newp;
  1725. struct dynbuf enc;
  1726. Curl_dyn_init(&enc, nalloc * 3 + 1 + leadingslash);
  1727. if(leadingslash && (part[0] != '/')) {
  1728. CURLcode result = Curl_dyn_addn(&enc, "/", 1);
  1729. if(result)
  1730. return cc2cu(result);
  1731. }
  1732. if(urlencode) {
  1733. const unsigned char *i;
  1734. for(i = (const unsigned char *)part; *i; i++) {
  1735. CURLcode result;
  1736. if((*i == ' ') && plusencode) {
  1737. result = Curl_dyn_addn(&enc, "+", 1);
  1738. if(result)
  1739. return CURLUE_OUT_OF_MEMORY;
  1740. }
  1741. else if(ISUNRESERVED(*i) ||
  1742. ((*i == '/') && urlskipslash) ||
  1743. ((*i == '=') && equalsencode)) {
  1744. if((*i == '=') && equalsencode)
  1745. /* only skip the first equals sign */
  1746. equalsencode = FALSE;
  1747. result = Curl_dyn_addn(&enc, i, 1);
  1748. if(result)
  1749. return cc2cu(result);
  1750. }
  1751. else {
  1752. char out[3]={'%'};
  1753. out[1] = hexdigits[*i >> 4];
  1754. out[2] = hexdigits[*i & 0xf];
  1755. result = Curl_dyn_addn(&enc, out, 3);
  1756. if(result)
  1757. return cc2cu(result);
  1758. }
  1759. }
  1760. }
  1761. else {
  1762. char *p;
  1763. CURLcode result = Curl_dyn_add(&enc, part);
  1764. if(result)
  1765. return cc2cu(result);
  1766. p = Curl_dyn_ptr(&enc);
  1767. while(*p) {
  1768. /* make sure percent encoded are lower case */
  1769. if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
  1770. (ISUPPER(p[1]) || ISUPPER(p[2]))) {
  1771. p[1] = Curl_raw_tolower(p[1]);
  1772. p[2] = Curl_raw_tolower(p[2]);
  1773. p += 3;
  1774. }
  1775. else
  1776. p++;
  1777. }
  1778. }
  1779. newp = Curl_dyn_ptr(&enc);
  1780. if(appendquery && newp) {
  1781. /* Append the 'newp' string onto the old query. Add a '&' separator if
  1782. none is present at the end of the existing query already */
  1783. size_t querylen = u->query ? strlen(u->query) : 0;
  1784. bool addamperand = querylen && (u->query[querylen -1] != '&');
  1785. if(querylen) {
  1786. struct dynbuf qbuf;
  1787. Curl_dyn_init(&qbuf, CURL_MAX_INPUT_LENGTH);
  1788. if(Curl_dyn_addn(&qbuf, u->query, querylen)) /* add original query */
  1789. goto nomem;
  1790. if(addamperand) {
  1791. if(Curl_dyn_addn(&qbuf, "&", 1))
  1792. goto nomem;
  1793. }
  1794. if(Curl_dyn_add(&qbuf, newp))
  1795. goto nomem;
  1796. Curl_dyn_free(&enc);
  1797. free(*storep);
  1798. *storep = Curl_dyn_ptr(&qbuf);
  1799. return CURLUE_OK;
  1800. nomem:
  1801. Curl_dyn_free(&enc);
  1802. return CURLUE_OUT_OF_MEMORY;
  1803. }
  1804. }
  1805. else if(what == CURLUPART_HOST) {
  1806. size_t n = Curl_dyn_len(&enc);
  1807. if(!n && (flags & CURLU_NO_AUTHORITY)) {
  1808. /* Skip hostname check, it is allowed to be empty. */
  1809. }
  1810. else {
  1811. bool bad = FALSE;
  1812. if(!n)
  1813. bad = TRUE; /* empty hostname is not okay */
  1814. else if(!urlencode) {
  1815. /* if the host name part was not URL encoded here, it was set ready
  1816. URL encoded so we need to decode it to check */
  1817. size_t dlen;
  1818. char *decoded = NULL;
  1819. CURLcode result =
  1820. Curl_urldecode(newp, n, &decoded, &dlen, REJECT_CTRL);
  1821. if(result || hostname_check(u, decoded, dlen))
  1822. bad = TRUE;
  1823. free(decoded);
  1824. }
  1825. else if(hostname_check(u, (char *)newp, n))
  1826. bad = TRUE;
  1827. if(bad) {
  1828. Curl_dyn_free(&enc);
  1829. return CURLUE_BAD_HOSTNAME;
  1830. }
  1831. }
  1832. }
  1833. free(*storep);
  1834. *storep = (char *)newp;
  1835. }
  1836. return CURLUE_OK;
  1837. }