2
0

lib1560.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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. /*
  25. * Note:
  26. *
  27. * Since the URL parser by default only accepts schemes that *this instance*
  28. * of libcurl supports, make sure that the test1560 file lists all the schemes
  29. * that this test will assume to be present!
  30. */
  31. #include "test.h"
  32. #if defined(USE_LIBIDN2) || defined(USE_WIN32_IDN) || defined(USE_APPLE_IDN)
  33. #define USE_IDN
  34. #endif
  35. #include "testutil.h"
  36. #include "warnless.h"
  37. #include "memdebug.h" /* LAST include file */
  38. struct part {
  39. CURLUPart part;
  40. const char *name;
  41. };
  42. static int checkparts(CURLU *u, const char *in, const char *wanted,
  43. unsigned int getflags)
  44. {
  45. int i;
  46. CURLUcode rc;
  47. char buf[256];
  48. char *bufp = &buf[0];
  49. size_t len = sizeof(buf);
  50. struct part parts[] = {
  51. {CURLUPART_SCHEME, "scheme"},
  52. {CURLUPART_USER, "user"},
  53. {CURLUPART_PASSWORD, "password"},
  54. {CURLUPART_OPTIONS, "options"},
  55. {CURLUPART_HOST, "host"},
  56. {CURLUPART_PORT, "port"},
  57. {CURLUPART_PATH, "path"},
  58. {CURLUPART_QUERY, "query"},
  59. {CURLUPART_FRAGMENT, "fragment"},
  60. {CURLUPART_URL, NULL}
  61. };
  62. memset(buf, 0, sizeof(buf));
  63. for(i = 0; parts[i].name; i++) {
  64. char *p = NULL;
  65. size_t n;
  66. rc = curl_url_get(u, parts[i].part, &p, getflags);
  67. if(!rc && p) {
  68. msnprintf(bufp, len, "%s%s", buf[0]?" | ":"", p);
  69. }
  70. else
  71. msnprintf(bufp, len, "%s[%d]", buf[0]?" | ":"", (int)rc);
  72. n = strlen(bufp);
  73. bufp += n;
  74. len -= n;
  75. curl_free(p);
  76. }
  77. if(strcmp(buf, wanted)) {
  78. fprintf(stderr, "in: %s\nwanted: %s\ngot: %s\n", in, wanted, buf);
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. struct redircase {
  84. const char *in;
  85. const char *set;
  86. const char *out;
  87. unsigned int urlflags;
  88. unsigned int setflags;
  89. CURLUcode ucode;
  90. };
  91. struct setcase {
  92. const char *in;
  93. const char *set;
  94. const char *out;
  95. unsigned int urlflags;
  96. unsigned int setflags;
  97. CURLUcode ucode; /* for the main URL set */
  98. CURLUcode pcode; /* for updating parts */
  99. };
  100. struct setgetcase {
  101. const char *in;
  102. const char *set;
  103. const char *out;
  104. unsigned int urlflags; /* for setting the URL */
  105. unsigned int setflags; /* for updating parts */
  106. unsigned int getflags; /* for getting parts */
  107. CURLUcode pcode; /* for updating parts */
  108. };
  109. struct testcase {
  110. const char *in;
  111. const char *out;
  112. unsigned int urlflags;
  113. unsigned int getflags;
  114. CURLUcode ucode;
  115. };
  116. struct urltestcase {
  117. const char *in;
  118. const char *out;
  119. unsigned int urlflags; /* pass to curl_url() */
  120. unsigned int getflags; /* pass to curl_url_get() */
  121. CURLUcode ucode;
  122. };
  123. struct querycase {
  124. const char *in;
  125. const char *q;
  126. const char *out;
  127. unsigned int urlflags; /* pass to curl_url() */
  128. unsigned int qflags; /* pass to curl_url_get() */
  129. CURLUcode ucode;
  130. };
  131. struct clearurlcase {
  132. CURLUPart part;
  133. const char *in;
  134. const char *out;
  135. CURLUcode ucode;
  136. };
  137. static const struct testcase get_parts_list[] ={
  138. {"https://curl.se/#",
  139. "https | [11] | [12] | [13] | curl.se | [15] | / | [16] | ",
  140. 0, CURLU_GET_EMPTY, CURLUE_OK},
  141. {"https://curl.se/?#",
  142. "https | [11] | [12] | [13] | curl.se | [15] | / | | ",
  143. 0, CURLU_GET_EMPTY, CURLUE_OK},
  144. {"https://curl.se/?",
  145. "https | [11] | [12] | [13] | curl.se | [15] | / | | [17]",
  146. 0, CURLU_GET_EMPTY, CURLUE_OK},
  147. {"https://curl.se/?",
  148. "https | [11] | [12] | [13] | curl.se | [15] | / | [16] | [17]",
  149. 0, 0, CURLUE_OK},
  150. {"https://curl.se/?#",
  151. "https | [11] | [12] | [13] | curl.se | [15] | / | [16] | [17]",
  152. 0, 0, CURLUE_OK},
  153. {"https://curl.se/# ",
  154. "https | [11] | [12] | [13] | curl.se | [15] | / | [16] | %20%20",
  155. CURLU_URLENCODE|CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  156. {"", "", 0, 0, CURLUE_MALFORMED_INPUT},
  157. {" ", "", 0, 0, CURLUE_MALFORMED_INPUT},
  158. {"1h://example.net", "", 0, 0, CURLUE_BAD_SCHEME},
  159. {"..://example.net", "", 0, 0, CURLUE_BAD_SCHEME},
  160. {"-ht://example.net", "", 0, 0, CURLUE_BAD_SCHEME},
  161. {"+ftp://example.net", "", 0, 0, CURLUE_BAD_SCHEME},
  162. {"hej.hej://example.net",
  163. "hej.hej | [11] | [12] | [13] | example.net | [15] | / | [16] | [17]",
  164. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  165. {"ht-tp://example.net",
  166. "ht-tp | [11] | [12] | [13] | example.net | [15] | / | [16] | [17]",
  167. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  168. {"ftp+more://example.net",
  169. "ftp+more | [11] | [12] | [13] | example.net | [15] | / | [16] | [17]",
  170. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  171. {"f1337://example.net",
  172. "f1337 | [11] | [12] | [13] | example.net | [15] | / | [16] | [17]",
  173. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  174. {"https://user@example.net?hello# space ",
  175. "https | user | [12] | [13] | example.net | [15] | / | hello | %20space%20",
  176. CURLU_ALLOW_SPACE|CURLU_URLENCODE, 0, CURLUE_OK},
  177. {"https://test%test", "", 0, 0, CURLUE_BAD_HOSTNAME},
  178. {"https://example.com%252f%40@example.net",
  179. "https | example.com%2f@ | [12] | [13] | example.net | [15] | / "
  180. "| [16] | [17]",
  181. 0, CURLU_URLDECODE, CURLUE_OK },
  182. #ifdef USE_IDN
  183. {"https://räksmörgås.se",
  184. "https | [11] | [12] | [13] | xn--rksmrgs-5wao1o.se | "
  185. "[15] | / | [16] | [17]", 0, CURLU_PUNYCODE, CURLUE_OK},
  186. {"https://xn--rksmrgs-5wao1o.se",
  187. "https | [11] | [12] | [13] | räksmörgås.se | "
  188. "[15] | / | [16] | [17]", 0, CURLU_PUNY2IDN, CURLUE_OK},
  189. #else
  190. {"https://räksmörgås.se",
  191. "https | [11] | [12] | [13] | [30] | [15] | / | [16] | [17]",
  192. 0, CURLU_PUNYCODE, CURLUE_OK},
  193. #endif
  194. /* https://ℂᵤⓇℒ。𝐒🄴 */
  195. {"https://"
  196. "%e2%84%82%e1%b5%a4%e2%93%87%e2%84%92%e3%80%82%f0%9d%90%92%f0%9f%84%b4",
  197. "https | [11] | [12] | [13] | ℂᵤⓇℒ。𝐒🄴 | [15] |"
  198. " / | [16] | [17]",
  199. 0, 0, CURLUE_OK},
  200. {"https://"
  201. "%e2%84%82%e1%b5%a4%e2%93%87%e2%84%92%e3%80%82%f0%9d%90%92%f0%9f%84%b4",
  202. "https | [11] | [12] | [13] | "
  203. "%e2%84%82%e1%b5%a4%e2%93%87%e2%84%92%e3%80%82%f0%9d%90%92%f0%9f%84%b4 "
  204. "| [15] | / | [16] | [17]",
  205. 0, CURLU_URLENCODE, CURLUE_OK},
  206. {"https://"
  207. "\xe2\x84\x82\xe1\xb5\xa4\xe2\x93\x87\xe2\x84\x92"
  208. "\xe3\x80\x82\xf0\x9d\x90\x92\xf0\x9f\x84\xb4",
  209. "https | [11] | [12] | [13] | "
  210. "%e2%84%82%e1%b5%a4%e2%93%87%e2%84%92%e3%80%82%f0%9d%90%92%f0%9f%84%b4 "
  211. "| [15] | / | [16] | [17]",
  212. 0, CURLU_URLENCODE, CURLUE_OK},
  213. {"https://user@example.net?he l lo",
  214. "https | user | [12] | [13] | example.net | [15] | / | he+l+lo | [17]",
  215. CURLU_ALLOW_SPACE, CURLU_URLENCODE, CURLUE_OK},
  216. {"https://user@example.net?he l lo",
  217. "https | user | [12] | [13] | example.net | [15] | / | he l lo | [17]",
  218. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  219. {"https://exam{}[]ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  220. {"https://exam{ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  221. {"https://exam}ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  222. {"https://exam]ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  223. {"https://exam\\ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  224. {"https://exam$ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  225. {"https://exam'ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  226. {"https://exam\"ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  227. {"https://exam^ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  228. {"https://exam`ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  229. {"https://exam*ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  230. {"https://exam<ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  231. {"https://exam>ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  232. {"https://exam=ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  233. {"https://exam;ple.net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  234. {"https://example,net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  235. {"https://example&net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  236. {"https://example+net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  237. {"https://example(net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  238. {"https://example)net", "", 0, 0, CURLUE_BAD_HOSTNAME},
  239. {"https://example.net/}",
  240. "https | [11] | [12] | [13] | example.net | [15] | /} | [16] | [17]",
  241. 0, 0, CURLUE_OK},
  242. /* blank user is blank */
  243. {"https://:password@example.net",
  244. "https | | password | [13] | example.net | [15] | / | [16] | [17]",
  245. 0, 0, CURLUE_OK},
  246. /* blank user + blank password */
  247. {"https://:@example.net",
  248. "https | | | [13] | example.net | [15] | / | [16] | [17]",
  249. 0, 0, CURLUE_OK},
  250. /* user-only (no password) */
  251. {"https://user@example.net",
  252. "https | user | [12] | [13] | example.net | [15] | / | [16] | [17]",
  253. 0, 0, CURLUE_OK},
  254. #ifdef USE_WEBSOCKETS
  255. {"ws://example.com/color/?green",
  256. "ws | [11] | [12] | [13] | example.com | [15] | /color/ | green |"
  257. " [17]",
  258. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  259. {"wss://example.com/color/?green",
  260. "wss | [11] | [12] | [13] | example.com | [15] | /color/ | green |"
  261. " [17]",
  262. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  263. #endif
  264. {"https://user:password@example.net/get?this=and#but frag then", "",
  265. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  266. {"https://user:password@example.net/get?this=and what", "",
  267. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  268. {"https://user:password@example.net/ge t?this=and-what", "",
  269. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  270. {"https://user:pass word@example.net/get?this=and-what", "",
  271. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  272. {"https://u ser:password@example.net/get?this=and-what", "",
  273. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  274. {"imap://user:pass;opt ion@server/path", "",
  275. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  276. /* no space allowed in scheme */
  277. {"htt ps://user:password@example.net/get?this=and-what", "",
  278. CURLU_NON_SUPPORT_SCHEME|CURLU_ALLOW_SPACE, 0, CURLUE_BAD_SCHEME},
  279. {"https://user:password@example.net/get?this=and what",
  280. "https | user | password | [13] | example.net | [15] | /get | "
  281. "this=and what | [17]",
  282. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  283. {"https://user:password@example.net/ge t?this=and-what",
  284. "https | user | password | [13] | example.net | [15] | /ge t | "
  285. "this=and-what | [17]",
  286. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  287. {"https://user:pass word@example.net/get?this=and-what",
  288. "https | user | pass word | [13] | example.net | [15] | /get | "
  289. "this=and-what | [17]",
  290. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  291. {"https://u ser:password@example.net/get?this=and-what",
  292. "https | u ser | password | [13] | example.net | [15] | /get | "
  293. "this=and-what | [17]",
  294. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  295. {"https://user:password@example.net/ge t?this=and-what",
  296. "https | user | password | [13] | example.net | [15] | /ge%20t | "
  297. "this=and-what | [17]",
  298. CURLU_ALLOW_SPACE | CURLU_URLENCODE, 0, CURLUE_OK},
  299. {"[0:0:0:0:0:0:0:1]",
  300. "http | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]",
  301. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  302. {"[::1]",
  303. "http | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]",
  304. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  305. {"[::]",
  306. "http | [11] | [12] | [13] | [::] | [15] | / | [16] | [17]",
  307. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  308. {"https://[::1]",
  309. "https | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]",
  310. 0, 0, CURLUE_OK },
  311. {"user:moo@ftp.example.com/color/#green?no-red",
  312. "ftp | user | moo | [13] | ftp.example.com | [15] | /color/ | [16] | "
  313. "green?no-red",
  314. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  315. {"ftp.user:moo@example.com/color/#green?no-red",
  316. "http | ftp.user | moo | [13] | example.com | [15] | /color/ | [16] | "
  317. "green?no-red",
  318. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  319. #ifdef _WIN32
  320. {"file:/C:\\programs\\foo",
  321. "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
  322. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  323. {"file://C:\\programs\\foo",
  324. "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
  325. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  326. {"file:///C:\\programs\\foo",
  327. "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
  328. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  329. {"file://host.example.com/Share/path/to/file.txt",
  330. "file | [11] | [12] | [13] | host.example.com | [15] | "
  331. "//host.example.com/Share/path/to/file.txt | [16] | [17]",
  332. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  333. #endif
  334. {"https://example.com/color/#green?no-red",
  335. "https | [11] | [12] | [13] | example.com | [15] | /color/ | [16] | "
  336. "green?no-red",
  337. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  338. {"https://example.com/color/#green#no-red",
  339. "https | [11] | [12] | [13] | example.com | [15] | /color/ | [16] | "
  340. "green#no-red",
  341. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  342. {"https://example.com/color/?green#no-red",
  343. "https | [11] | [12] | [13] | example.com | [15] | /color/ | green | "
  344. "no-red",
  345. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  346. {"https://example.com/#color/?green#no-red",
  347. "https | [11] | [12] | [13] | example.com | [15] | / | [16] | "
  348. "color/?green#no-red",
  349. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  350. {"https://example.#com/color/?green#no-red",
  351. "https | [11] | [12] | [13] | example. | [15] | / | [16] | "
  352. "com/color/?green#no-red",
  353. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  354. {"http://[ab.be:1]/x", "",
  355. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_IPV6},
  356. {"http://[ab.be]/x", "",
  357. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_IPV6},
  358. /* URL without host name */
  359. {"http://a:b@/x", "",
  360. CURLU_DEFAULT_SCHEME, 0, CURLUE_NO_HOST},
  361. {"boing:80",
  362. "https | [11] | [12] | [13] | boing | 80 | / | [16] | [17]",
  363. CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  364. {"http://[fd00:a41::50]:8080",
  365. "http | [11] | [12] | [13] | [fd00:a41::50] | 8080 | / | [16] | [17]",
  366. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  367. {"http://[fd00:a41::50]/",
  368. "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
  369. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  370. {"http://[fd00:a41::50]",
  371. "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
  372. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  373. {"https://[::1%252]:1234",
  374. "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
  375. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  376. /* here's "bad" zone id */
  377. {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
  378. "https | [11] | [12] | [13] | [fe80::20c:29ff:fe9c:409b] | 1234 "
  379. "| / | [16] | [17]",
  380. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  381. {"https://127.0.0.1:443",
  382. "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
  383. 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
  384. {"http://%3a:%3a@ex4mple/%3f+?+%3f+%23#+%23%3f%g7",
  385. "http | : | : | [13] | ex4mple | [15] | /?+ | ? # | +#?%g7",
  386. 0, CURLU_URLDECODE, CURLUE_OK},
  387. {"http://%3a:%3a@ex4mple/%3f?%3f%35#%35%3f%g7",
  388. "http | %3a | %3a | [13] | ex4mple | [15] | /%3f | %3f%35 | %35%3f%g7",
  389. 0, 0, CURLUE_OK},
  390. {"http://HO0_-st%41/",
  391. "http | [11] | [12] | [13] | HO0_-stA | [15] | / | [16] | [17]",
  392. 0, 0, CURLUE_OK},
  393. {"file://hello.html",
  394. "",
  395. 0, 0, CURLUE_BAD_FILE_URL},
  396. {"http://HO0_-st/",
  397. "http | [11] | [12] | [13] | HO0_-st | [15] | / | [16] | [17]",
  398. 0, 0, CURLUE_OK},
  399. {"imap://user:pass;option@server/path",
  400. "imap | user | pass | option | server | [15] | /path | [16] | [17]",
  401. 0, 0, CURLUE_OK},
  402. {"http://user:pass;option@server/path",
  403. "http | user | pass;option | [13] | server | [15] | /path | [16] | [17]",
  404. 0, 0, CURLUE_OK},
  405. {"file:/hello.html",
  406. "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
  407. 0, 0, CURLUE_OK},
  408. {"file:/h",
  409. "file | [11] | [12] | [13] | [14] | [15] | /h | [16] | [17]",
  410. 0, 0, CURLUE_OK},
  411. {"file:/",
  412. "file | [11] | [12] | [13] | [14] | [15] | | [16] | [17]",
  413. 0, 0, CURLUE_BAD_FILE_URL},
  414. {"file://127.0.0.1/hello.html",
  415. "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
  416. 0, 0, CURLUE_OK},
  417. {"file:////hello.html",
  418. "file | [11] | [12] | [13] | [14] | [15] | //hello.html | [16] | [17]",
  419. 0, 0, CURLUE_OK},
  420. {"file:///hello.html",
  421. "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
  422. 0, 0, CURLUE_OK},
  423. {"https://127.0.0.1",
  424. "https | [11] | [12] | [13] | 127.0.0.1 | 443 | / | [16] | [17]",
  425. 0, CURLU_DEFAULT_PORT, CURLUE_OK},
  426. {"https://127.0.0.1",
  427. "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
  428. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  429. {"https://[::1]:1234",
  430. "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
  431. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  432. {"https://127abc.com",
  433. "https | [11] | [12] | [13] | 127abc.com | [15] | / | [16] | [17]",
  434. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  435. {"https:// example.com?check", "",
  436. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  437. {"https://e x a m p l e.com?check", "",
  438. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  439. {"https://example.com?check",
  440. "https | [11] | [12] | [13] | example.com | [15] | / | check | [17]",
  441. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  442. {"https://example.com:65536",
  443. "",
  444. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
  445. {"https://example.com:-1#moo",
  446. "",
  447. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
  448. {"https://example.com:0#moo",
  449. "https | [11] | [12] | [13] | example.com | 0 | / | "
  450. "[16] | moo",
  451. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  452. {"https://example.com:01#moo",
  453. "https | [11] | [12] | [13] | example.com | 1 | / | "
  454. "[16] | moo",
  455. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  456. {"https://example.com:1#moo",
  457. "https | [11] | [12] | [13] | example.com | 1 | / | "
  458. "[16] | moo",
  459. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  460. {"http://example.com#moo",
  461. "http | [11] | [12] | [13] | example.com | [15] | / | "
  462. "[16] | moo",
  463. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  464. {"http://example.com",
  465. "http | [11] | [12] | [13] | example.com | [15] | / | "
  466. "[16] | [17]",
  467. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  468. {"http://example.com/path/html",
  469. "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
  470. "[16] | [17]",
  471. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  472. {"http://example.com/path/html?query=name",
  473. "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
  474. "query=name | [17]",
  475. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  476. {"http://example.com/path/html?query=name#anchor",
  477. "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
  478. "query=name | anchor",
  479. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  480. {"http://example.com:1234/path/html?query=name#anchor",
  481. "http | [11] | [12] | [13] | example.com | 1234 | /path/html | "
  482. "query=name | anchor",
  483. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  484. {"http:///user:password@example.com:1234/path/html?query=name#anchor",
  485. "http | user | password | [13] | example.com | 1234 | /path/html | "
  486. "query=name | anchor",
  487. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  488. {"https://user:password@example.com:1234/path/html?query=name#anchor",
  489. "https | user | password | [13] | example.com | 1234 | /path/html | "
  490. "query=name | anchor",
  491. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  492. {"http://user:password@example.com:1234/path/html?query=name#anchor",
  493. "http | user | password | [13] | example.com | 1234 | /path/html | "
  494. "query=name | anchor",
  495. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  496. {"http:/user:password@example.com:1234/path/html?query=name#anchor",
  497. "http | user | password | [13] | example.com | 1234 | /path/html | "
  498. "query=name | anchor",
  499. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  500. {"http:////user:password@example.com:1234/path/html?query=name#anchor",
  501. "",
  502. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_SLASHES},
  503. {NULL, NULL, 0, 0, CURLUE_OK},
  504. };
  505. static const struct urltestcase get_url_list[] = {
  506. {"http://user@example.com?#",
  507. "http://user@example.com/?#",
  508. 0, CURLU_GET_EMPTY, CURLUE_OK},
  509. /* WHATWG disgrees, it wants "https:/0.0.0.0/" */
  510. {"https://0x.0x.0", "https://0x.0x.0/", 0, 0, CURLUE_OK},
  511. {"https://example.com:000000000000000000000443/foo",
  512. "https://example.com/foo",
  513. 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
  514. {"https://example.com:000000000000000000000/foo",
  515. "https://example.com:0/foo",
  516. 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
  517. {"https://192.0x0000A80001", "https://192.168.0.1/", 0, 0, CURLUE_OK},
  518. {"https://0xffffffff", "https://255.255.255.255/", 0, 0, CURLUE_OK},
  519. {"https://1.0x1000000", "https://1.0x1000000/", 0, 0, CURLUE_OK},
  520. {"https://0x7f.1", "https://127.0.0.1/", 0, 0, CURLUE_OK},
  521. {"https://1.2.3.256.com", "https://1.2.3.256.com/", 0, 0, CURLUE_OK},
  522. {"https://10.com", "https://10.com/", 0, 0, CURLUE_OK},
  523. {"https://1.2.com", "https://1.2.com/", 0, 0, CURLUE_OK},
  524. {"https://1.2.3.com", "https://1.2.3.com/", 0, 0, CURLUE_OK},
  525. {"https://1.2.com.99", "https://1.2.com.99/", 0, 0, CURLUE_OK},
  526. {"https://[fe80::0000:20c:29ff:fe9c:409b]:80/moo",
  527. "https://[fe80::20c:29ff:fe9c:409b]:80/moo",
  528. 0, 0, CURLUE_OK},
  529. {"https://[fe80::020c:29ff:fe9c:409b]:80/moo",
  530. "https://[fe80::20c:29ff:fe9c:409b]:80/moo",
  531. 0, 0, CURLUE_OK},
  532. {"https://[fe80:0000:0000:0000:020c:29ff:fe9c:409b]:80/moo",
  533. "https://[fe80::20c:29ff:fe9c:409b]:80/moo",
  534. 0, 0, CURLUE_OK},
  535. {"https://[fe80:0:0:0:409b::]:80/moo",
  536. "https://[fe80::409b:0:0:0]:80/moo",
  537. 0, 0, CURLUE_OK},
  538. {"https://[::%25fakeit];80/moo",
  539. "",
  540. 0, 0, CURLUE_BAD_PORT_NUMBER},
  541. {"https://[fe80::20c:29ff:fe9c:409b]-80/moo",
  542. "",
  543. 0, 0, CURLUE_BAD_PORT_NUMBER},
  544. #ifdef USE_IDN
  545. {"https://räksmörgås.se/path?q#frag",
  546. "https://xn--rksmrgs-5wao1o.se/path?q#frag", 0, CURLU_PUNYCODE, CURLUE_OK},
  547. #endif
  548. /* unsupported schemes with no guessing enabled */
  549. {"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIEhUTUw+PG1ldGEgY",
  550. "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
  551. {"d:anything-really", "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
  552. {"about:config", "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
  553. {"example://foo", "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
  554. {"mailto:infobot@example.com?body=send%20current-issue", "", 0, 0,
  555. CURLUE_UNSUPPORTED_SCHEME},
  556. {"about:80", "https://about:80/", CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  557. /* percent encoded host names */
  558. {"http://example.com%40127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  559. {"http://example.com%21127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  560. {"http://example.com%3f127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  561. {"http://example.com%23127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  562. {"http://example.com%3a127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  563. {"http://example.com%09127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  564. {"http://example.com%2F127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
  565. {"https://%41", "https://A/", 0, 0, CURLUE_OK},
  566. {"https://%20", "", 0, 0, CURLUE_BAD_HOSTNAME},
  567. {"https://%41%0d", "", 0, 0, CURLUE_BAD_HOSTNAME},
  568. {"https://%25", "", 0, 0, CURLUE_BAD_HOSTNAME},
  569. {"https://_%c0_", "https://_\xC0_/", 0, 0, CURLUE_OK},
  570. {"https://_%c0_", "https://_%C0_/", 0, CURLU_URLENCODE, CURLUE_OK},
  571. /* IPv4 trickeries */
  572. {"https://16843009", "https://1.1.1.1/", 0, 0, CURLUE_OK},
  573. {"https://0177.1", "https://127.0.0.1/", 0, 0, CURLUE_OK},
  574. {"https://0111.02.0x3", "https://73.2.0.3/", 0, 0, CURLUE_OK},
  575. {"https://0111.02.0x3.", "https://0111.02.0x3./", 0, 0, CURLUE_OK},
  576. {"https://0111.02.030", "https://73.2.0.24/", 0, 0, CURLUE_OK},
  577. {"https://0111.02.030.", "https://0111.02.030./", 0, 0, CURLUE_OK},
  578. {"https://0xff.0xff.0377.255", "https://255.255.255.255/", 0, 0, CURLUE_OK},
  579. {"https://1.0xffffff", "https://1.255.255.255/", 0, 0, CURLUE_OK},
  580. /* IPv4 numerical overflows or syntax errors will not normalize */
  581. {"https://a127.0.0.1", "https://a127.0.0.1/", 0, 0, CURLUE_OK},
  582. {"https://\xff.127.0.0.1", "https://%FF.127.0.0.1/", 0, CURLU_URLENCODE,
  583. CURLUE_OK},
  584. {"https://127.-0.0.1", "https://127.-0.0.1/", 0, 0, CURLUE_OK},
  585. {"https://127.0. 1", "https://127.0.0.1/", 0, 0, CURLUE_MALFORMED_INPUT},
  586. {"https://1.2.3.256", "https://1.2.3.256/", 0, 0, CURLUE_OK},
  587. {"https://1.2.3.256.", "https://1.2.3.256./", 0, 0, CURLUE_OK},
  588. {"https://1.2.3.4.5", "https://1.2.3.4.5/", 0, 0, CURLUE_OK},
  589. {"https://1.2.0x100.3", "https://1.2.0x100.3/", 0, 0, CURLUE_OK},
  590. {"https://4294967296", "https://4294967296/", 0, 0, CURLUE_OK},
  591. {"https://123host", "https://123host/", 0, 0, CURLUE_OK},
  592. /* 40 bytes scheme is the max allowed */
  593. {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
  594. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa://hostname/path",
  595. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  596. /* 41 bytes scheme is not allowed */
  597. {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
  598. "",
  599. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_BAD_SCHEME},
  600. {"https://[fe80::20c:29ff:fe9c:409b%]:1234",
  601. "",
  602. 0, 0, CURLUE_BAD_IPV6},
  603. {"https://[fe80::20c:29ff:fe9c:409b%25]:1234",
  604. "https://[fe80::20c:29ff:fe9c:409b%2525]:1234/",
  605. 0, 0, CURLUE_OK},
  606. {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
  607. "https://[fe80::20c:29ff:fe9c:409b%25eth0]:1234/",
  608. 0, 0, CURLUE_OK},
  609. {"https://[::%25fakeit]/moo",
  610. "https://[::%25fakeit]/moo",
  611. 0, 0, CURLUE_OK},
  612. {"smtp.example.com/path/html",
  613. "smtp://smtp.example.com/path/html",
  614. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  615. {"https.example.com/path/html",
  616. "http://https.example.com/path/html",
  617. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  618. {"dict.example.com/path/html",
  619. "dict://dict.example.com/path/html",
  620. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  621. {"pop3.example.com/path/html",
  622. "pop3://pop3.example.com/path/html",
  623. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  624. {"ldap.example.com/path/html",
  625. "ldap://ldap.example.com/path/html",
  626. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  627. {"imap.example.com/path/html",
  628. "imap://imap.example.com/path/html",
  629. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  630. {"ftp.example.com/path/html",
  631. "ftp://ftp.example.com/path/html",
  632. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  633. {"example.com/path/html",
  634. "http://example.com/path/html",
  635. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  636. {"smtp.com/path/html",
  637. "smtp://smtp.com/path/html",
  638. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  639. {"dict.com/path/html",
  640. "dict://dict.com/path/html",
  641. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  642. {"pop3.com/path/html",
  643. "pop3://pop3.com/path/html",
  644. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  645. {"ldap.com/path/html",
  646. "ldap://ldap.com/path/html",
  647. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  648. {"imap.com/path/html",
  649. "imap://imap.com/path/html",
  650. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  651. {"ftp.com/path/html",
  652. "ftp://ftp.com/path/html",
  653. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  654. {"smtp/path/html",
  655. "http://smtp/path/html",
  656. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  657. {"dict/path/html",
  658. "http://dict/path/html",
  659. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  660. {"pop3/path/html",
  661. "http://pop3/path/html",
  662. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  663. {"ldap/path/html",
  664. "http://ldap/path/html",
  665. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  666. {"imap/path/html",
  667. "http://imap/path/html",
  668. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  669. {"ftp/path/html",
  670. "http://ftp/path/html",
  671. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  672. {"HTTP://test/", "http://test/", 0, 0, CURLUE_OK},
  673. {"http://HO0_-st..~./", "http://HO0_-st..~./", 0, 0, CURLUE_OK},
  674. {"http:/@example.com: 123/", "", 0, 0, CURLUE_MALFORMED_INPUT},
  675. {"http:/@example.com:123 /", "", 0, 0, CURLUE_MALFORMED_INPUT},
  676. {"http:/@example.com:123a/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
  677. {"http://host/file\r", "", 0, 0, CURLUE_MALFORMED_INPUT},
  678. {"http://host/file\n\x03", "", 0, 0, CURLUE_MALFORMED_INPUT},
  679. {"htt\x02://host/file", "",
  680. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  681. {" http://host/file", "", 0, 0, CURLUE_MALFORMED_INPUT},
  682. /* here the password ends at the semicolon and options is 'word' */
  683. {"imap://user:pass;word@host/file",
  684. "imap://user:pass;word@host/file",
  685. 0, 0, CURLUE_OK},
  686. /* here the password has the semicolon */
  687. {"http://user:pass;word@host/file",
  688. "http://user:pass;word@host/file", 0, 0, CURLUE_OK},
  689. {"file:///file.txt#moo", "file:///file.txt#moo", 0, 0, CURLUE_OK},
  690. {"file:////file.txt", "file:////file.txt", 0, 0, CURLUE_OK},
  691. {"file:///file.txt", "file:///file.txt", 0, 0, CURLUE_OK},
  692. {"file:./", "file://", 0, 0, CURLUE_OK},
  693. {"http://example.com/hello/../here",
  694. "http://example.com/hello/../here",
  695. CURLU_PATH_AS_IS, 0, CURLUE_OK},
  696. {"http://example.com/hello/../here",
  697. "http://example.com/here",
  698. 0, 0, CURLUE_OK},
  699. {"http://example.com:80",
  700. "http://example.com/",
  701. 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
  702. {"tp://example.com/path/html",
  703. "",
  704. 0, 0, CURLUE_UNSUPPORTED_SCHEME},
  705. {"http://hello:fool@example.com",
  706. "",
  707. CURLU_DISALLOW_USER, 0, CURLUE_USER_NOT_ALLOWED},
  708. {"http:/@example.com:123",
  709. "http://@example.com:123/",
  710. 0, 0, CURLUE_OK},
  711. {"http:/:password@example.com",
  712. "http://:password@example.com/",
  713. 0, 0, CURLUE_OK},
  714. {"http://user@example.com?#",
  715. "http://user@example.com/",
  716. 0, 0, CURLUE_OK},
  717. {"http://user@example.com?",
  718. "http://user@example.com/",
  719. 0, 0, CURLUE_OK},
  720. {"http://user@example.com#anchor",
  721. "http://user@example.com/#anchor",
  722. 0, 0, CURLUE_OK},
  723. {"example.com/path/html",
  724. "https://example.com/path/html",
  725. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  726. {"example.com/path/html",
  727. "",
  728. 0, 0, CURLUE_BAD_SCHEME},
  729. {"http://user:password@example.com:1234/path/html?query=name#anchor",
  730. "http://user:password@example.com:1234/path/html?query=name#anchor",
  731. 0, 0, CURLUE_OK},
  732. {"http://example.com:1234/path/html?query=name#anchor",
  733. "http://example.com:1234/path/html?query=name#anchor",
  734. 0, 0, CURLUE_OK},
  735. {"http://example.com/path/html?query=name#anchor",
  736. "http://example.com/path/html?query=name#anchor",
  737. 0, 0, CURLUE_OK},
  738. {"http://example.com/path/html?query=name",
  739. "http://example.com/path/html?query=name",
  740. 0, 0, CURLUE_OK},
  741. {"http://example.com/path/html",
  742. "http://example.com/path/html",
  743. 0, 0, CURLUE_OK},
  744. {"tp://example.com/path/html",
  745. "tp://example.com/path/html",
  746. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  747. {"custom-scheme://host?expected=test-good",
  748. "custom-scheme://host/?expected=test-good",
  749. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  750. {"custom-scheme://?expected=test-bad",
  751. "",
  752. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_NO_HOST},
  753. {"custom-scheme://?expected=test-new-good",
  754. "custom-scheme:///?expected=test-new-good",
  755. CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
  756. {"custom-scheme://host?expected=test-still-good",
  757. "custom-scheme://host/?expected=test-still-good",
  758. CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
  759. {NULL, NULL, 0, 0, CURLUE_OK}
  760. };
  761. static int checkurl(const char *org, const char *url, const char *out)
  762. {
  763. if(strcmp(out, url)) {
  764. fprintf(stderr,
  765. "Org: %s\n"
  766. "Wanted: %s\n"
  767. "Got : %s\n",
  768. org, out, url);
  769. return 1;
  770. }
  771. return 0;
  772. }
  773. /* 1. Set the URL
  774. 2. Set components
  775. 3. Extract all components (not URL)
  776. */
  777. static const struct setgetcase setget_parts_list[] = {
  778. {"https://example.com/",
  779. "query=\"\",",
  780. "https | [11] | [12] | [13] | example.com | [15] | / | | [17]",
  781. 0, 0, CURLU_GET_EMPTY, CURLUE_OK},
  782. {"https://example.com/",
  783. "fragment=\"\",",
  784. "https | [11] | [12] | [13] | example.com | [15] | / | [16] | ",
  785. 0, 0, CURLU_GET_EMPTY, CURLUE_OK},
  786. {"https://example.com/",
  787. "query=\"\",",
  788. "https | [11] | [12] | [13] | example.com | [15] | / | [16] | [17]",
  789. 0, 0, 0, CURLUE_OK},
  790. {"https://example.com",
  791. "path=get,",
  792. "https | [11] | [12] | [13] | example.com | [15] | /get | [16] | [17]",
  793. 0, 0, 0, CURLUE_OK},
  794. {"https://example.com",
  795. "path=/get,",
  796. "https | [11] | [12] | [13] | example.com | [15] | /get | [16] | [17]",
  797. 0, 0, 0, CURLUE_OK},
  798. {"https://example.com",
  799. "path=g e t,",
  800. "https | [11] | [12] | [13] | example.com | [15] | /g%20e%20t | "
  801. "[16] | [17]",
  802. 0, CURLU_URLENCODE, 0, CURLUE_OK},
  803. {NULL, NULL, NULL, 0, 0, 0, CURLUE_OK}
  804. };
  805. /* !checksrc! disable SPACEBEFORECOMMA 1 */
  806. static const struct setcase set_parts_list[] = {
  807. {"https://example.com/?param=value",
  808. "query=\"\",",
  809. "https://example.com/",
  810. 0, CURLU_APPENDQUERY | CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  811. {"https://example.com/",
  812. "host=\"\",",
  813. "https://example.com/",
  814. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_BAD_HOSTNAME},
  815. {"https://example.com/",
  816. "host=\"\",",
  817. "https://example.com/",
  818. 0, 0, CURLUE_OK, CURLUE_BAD_HOSTNAME},
  819. {"https://example.com",
  820. "path=get,",
  821. "https://example.com/get",
  822. 0, 0, CURLUE_OK, CURLUE_OK},
  823. {"https://example.com/",
  824. "scheme=ftp+-.123,",
  825. "ftp+-.123://example.com/",
  826. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  827. {"https://example.com/",
  828. "scheme=1234,",
  829. "https://example.com/",
  830. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  831. {"https://example.com/",
  832. "scheme=1http,",
  833. "https://example.com/",
  834. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  835. {"https://example.com/",
  836. "scheme=-ftp,",
  837. "https://example.com/",
  838. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  839. {"https://example.com/",
  840. "scheme=+ftp,",
  841. "https://example.com/",
  842. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  843. {"https://example.com/",
  844. "scheme=.ftp,",
  845. "https://example.com/",
  846. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  847. {"https://example.com/",
  848. "host=example.com%2fmoo,",
  849. "",
  850. 0, /* get */
  851. 0, /* set */
  852. CURLUE_OK, CURLUE_BAD_HOSTNAME},
  853. {"https://example.com/",
  854. "host=http://fake,",
  855. "",
  856. 0, /* get */
  857. 0, /* set */
  858. CURLUE_OK, CURLUE_BAD_HOSTNAME},
  859. {"https://example.com/",
  860. "host=test%,",
  861. "",
  862. 0, /* get */
  863. 0, /* set */
  864. CURLUE_OK, CURLUE_BAD_HOSTNAME},
  865. {"https://example.com/",
  866. "host=te st,",
  867. "",
  868. 0, /* get */
  869. 0, /* set */
  870. CURLUE_OK, CURLUE_BAD_HOSTNAME},
  871. {"https://example.com/",
  872. "host=0xff,", /* '++' there's no automatic URL decode when setting this
  873. part */
  874. "https://0xff/",
  875. 0, /* get */
  876. 0, /* set */
  877. CURLUE_OK, CURLUE_OK},
  878. {"https://example.com/",
  879. "query=Al2cO3tDkcDZ3EWE5Lh+LX8TPHs,", /* contains '+' */
  880. "https://example.com/?Al2cO3tDkcDZ3EWE5Lh%2bLX8TPHs",
  881. CURLU_URLDECODE, /* decode on get */
  882. CURLU_URLENCODE, /* encode on set */
  883. CURLUE_OK, CURLUE_OK},
  884. {"https://example.com/",
  885. /* Set a bad scheme *including* :// */
  886. "scheme=https://,",
  887. "https://example.com/",
  888. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  889. {"https://example.com/",
  890. /* Set a 41 bytes scheme. That's too long so the old scheme remains set. */
  891. "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbc,",
  892. "https://example.com/",
  893. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
  894. {"https://example.com/",
  895. /* set a 40 bytes scheme */
  896. "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,",
  897. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb://example.com/",
  898. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  899. {"https://[::1%25fake]:1234/",
  900. "zoneid=NULL,",
  901. "https://[::1]:1234/",
  902. 0, 0, CURLUE_OK, CURLUE_OK},
  903. {"https://host:1234/",
  904. "port=NULL,",
  905. "https://host/",
  906. 0, 0, CURLUE_OK, CURLUE_OK},
  907. {"https://host:1234/",
  908. "port=\"\",",
  909. "https://host:1234/",
  910. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  911. {"https://host:1234/",
  912. "port=56 78,",
  913. "https://host:1234/",
  914. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  915. {"https://host:1234/",
  916. "port=0,",
  917. "https://host:1234/",
  918. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  919. {"https://host:1234/",
  920. "port=65535,",
  921. "https://host:65535/",
  922. 0, 0, CURLUE_OK, CURLUE_OK},
  923. {"https://host:1234/",
  924. "port=65536,",
  925. "https://host:1234/",
  926. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  927. {"https://host/",
  928. "path=%4A%4B%4C,",
  929. "https://host/%4a%4b%4c",
  930. 0, 0, CURLUE_OK, CURLUE_OK},
  931. {"https://host/mooo?q#f",
  932. "path=NULL,query=NULL,fragment=NULL,",
  933. "https://host/",
  934. 0, 0, CURLUE_OK, CURLUE_OK},
  935. {"https://user:secret@host/",
  936. "user=NULL,password=NULL,",
  937. "https://host/",
  938. 0, 0, CURLUE_OK, CURLUE_OK},
  939. {NULL,
  940. "scheme=https,user= @:,host=foobar,",
  941. "https://%20%20%20%40%3a@foobar/",
  942. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  943. /* Setting a host name with spaces is not OK: */
  944. {NULL,
  945. "scheme=https,host= ,path= ,user= ,password= ,query= ,fragment= ,",
  946. "[nothing]",
  947. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_BAD_HOSTNAME},
  948. {NULL,
  949. "scheme=https,host=foobar,path=/this /path /is /here,",
  950. "https://foobar/this%20/path%20/is%20/here",
  951. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  952. {NULL,
  953. "scheme=https,host=foobar,path=\xc3\xa4\xc3\xb6\xc3\xbc,",
  954. "https://foobar/%c3%a4%c3%b6%c3%bc",
  955. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  956. {"imap://user:secret;opt@host/",
  957. "options=updated,scheme=imaps,password=p4ssw0rd,",
  958. "imaps://user:p4ssw0rd;updated@host/",
  959. 0, 0, CURLUE_NO_HOST, CURLUE_OK},
  960. {"imap://user:secret;optit@host/",
  961. "scheme=https,",
  962. "https://user:secret@host/",
  963. 0, 0, CURLUE_NO_HOST, CURLUE_OK},
  964. {"file:///file#anchor",
  965. "scheme=https,host=example,",
  966. "https://example/file#anchor",
  967. 0, 0, CURLUE_NO_HOST, CURLUE_OK},
  968. {NULL, /* start fresh! */
  969. "scheme=file,host=127.0.0.1,path=/no,user=anonymous,",
  970. "file:///no",
  971. 0, 0, CURLUE_OK, CURLUE_OK},
  972. {NULL, /* start fresh! */
  973. "scheme=ftp,host=127.0.0.1,path=/no,user=anonymous,",
  974. "ftp://anonymous@127.0.0.1/no",
  975. 0, 0, CURLUE_OK, CURLUE_OK},
  976. {NULL, /* start fresh! */
  977. "scheme=https,host=example.com,",
  978. "https://example.com/",
  979. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  980. {"http://user:foo@example.com/path?query#frag",
  981. "fragment=changed,",
  982. "http://user:foo@example.com/path?query#changed",
  983. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  984. {"http://example.com/",
  985. "scheme=foo,", /* not accepted */
  986. "http://example.com/",
  987. 0, 0, CURLUE_OK, CURLUE_UNSUPPORTED_SCHEME},
  988. {"http://example.com/",
  989. "scheme=https,path=/hello,fragment=snippet,",
  990. "https://example.com/hello#snippet",
  991. 0, 0, CURLUE_OK, CURLUE_OK},
  992. {"http://example.com:80",
  993. "user=foo,port=1922,",
  994. "http://foo@example.com:1922/",
  995. 0, 0, CURLUE_OK, CURLUE_OK},
  996. {"http://example.com:80",
  997. "user=foo,password=bar,",
  998. "http://foo:bar@example.com:80/",
  999. 0, 0, CURLUE_OK, CURLUE_OK},
  1000. {"http://example.com:80",
  1001. "user=foo,",
  1002. "http://foo@example.com:80/",
  1003. 0, 0, CURLUE_OK, CURLUE_OK},
  1004. {"http://example.com",
  1005. "host=www.example.com,",
  1006. "http://www.example.com/",
  1007. 0, 0, CURLUE_OK, CURLUE_OK},
  1008. {"http://example.com:80",
  1009. "scheme=ftp,",
  1010. "ftp://example.com:80/",
  1011. 0, 0, CURLUE_OK, CURLUE_OK},
  1012. {"custom-scheme://host",
  1013. "host=\"\",",
  1014. "custom-scheme://host/",
  1015. CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK,
  1016. CURLUE_BAD_HOSTNAME},
  1017. {"custom-scheme://host",
  1018. "host=\"\",",
  1019. "custom-scheme:///",
  1020. CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY,
  1021. CURLUE_OK, CURLUE_OK},
  1022. {NULL, NULL, NULL, 0, 0, CURLUE_OK, CURLUE_OK}
  1023. };
  1024. static CURLUPart part2id(char *part)
  1025. {
  1026. if(!strcmp("url", part))
  1027. return CURLUPART_URL;
  1028. if(!strcmp("scheme", part))
  1029. return CURLUPART_SCHEME;
  1030. if(!strcmp("user", part))
  1031. return CURLUPART_USER;
  1032. if(!strcmp("password", part))
  1033. return CURLUPART_PASSWORD;
  1034. if(!strcmp("options", part))
  1035. return CURLUPART_OPTIONS;
  1036. if(!strcmp("host", part))
  1037. return CURLUPART_HOST;
  1038. if(!strcmp("port", part))
  1039. return CURLUPART_PORT;
  1040. if(!strcmp("path", part))
  1041. return CURLUPART_PATH;
  1042. if(!strcmp("query", part))
  1043. return CURLUPART_QUERY;
  1044. if(!strcmp("fragment", part))
  1045. return CURLUPART_FRAGMENT;
  1046. if(!strcmp("zoneid", part))
  1047. return CURLUPART_ZONEID;
  1048. return (CURLUPart)9999; /* bad input => bad output */
  1049. }
  1050. static CURLUcode updateurl(CURLU *u, const char *cmd, unsigned int setflags)
  1051. {
  1052. const char *p = cmd;
  1053. CURLUcode uc;
  1054. /* make sure the last command ends with a comma too! */
  1055. while(p) {
  1056. char *e = strchr(p, ',');
  1057. if(e) {
  1058. size_t n = (size_t)(e - p);
  1059. char buf[80];
  1060. char part[80];
  1061. char value[80];
  1062. memset(part, 0, sizeof(part)); /* Avoid valgrind false positive. */
  1063. memset(value, 0, sizeof(value)); /* Avoid valgrind false positive. */
  1064. memcpy(buf, p, n);
  1065. buf[n] = 0;
  1066. if(2 == sscanf(buf, "%79[^=]=%79[^,]", part, value)) {
  1067. CURLUPart what = part2id(part);
  1068. #if 0
  1069. /* for debugging this */
  1070. fprintf(stderr, "%s = \"%s\" [%d]\n", part, value, (int)what);
  1071. #endif
  1072. if(what > CURLUPART_ZONEID)
  1073. fprintf(stderr, "UNKNOWN part '%s'\n", part);
  1074. if(!strcmp("NULL", value))
  1075. uc = curl_url_set(u, what, NULL, setflags);
  1076. else if(!strcmp("\"\"", value))
  1077. uc = curl_url_set(u, what, "", setflags);
  1078. else
  1079. uc = curl_url_set(u, what, value, setflags);
  1080. if(uc)
  1081. return uc;
  1082. }
  1083. p = e + 1;
  1084. continue;
  1085. }
  1086. break;
  1087. }
  1088. return CURLUE_OK;
  1089. }
  1090. static const struct redircase set_url_list[] = {
  1091. {"http://example.org/",
  1092. "../path/././../../moo",
  1093. "http://example.org/moo",
  1094. 0, 0, CURLUE_OK},
  1095. {"http://example.org/",
  1096. "//example.org/../path/../../",
  1097. "http://example.org/",
  1098. 0, 0, CURLUE_OK},
  1099. {"http://example.org/",
  1100. "///example.org/../path/../../",
  1101. "http://example.org/",
  1102. 0, 0, CURLUE_OK},
  1103. {"http://example.org/foo/bar",
  1104. ":23",
  1105. "http://example.org/foo/:23",
  1106. 0, 0, CURLUE_OK},
  1107. {"http://example.org/foo/bar",
  1108. "\\x",
  1109. "http://example.org/foo/\\x",
  1110. /* WHATWG disagrees */
  1111. 0, 0, CURLUE_OK},
  1112. {"http://example.org/foo/bar",
  1113. "#/",
  1114. "http://example.org/foo/bar#/",
  1115. 0, 0, CURLUE_OK},
  1116. {"http://example.org/foo/bar",
  1117. "?/",
  1118. "http://example.org/foo/bar?/",
  1119. 0, 0, CURLUE_OK},
  1120. {"http://example.org/foo/bar",
  1121. "#;?",
  1122. "http://example.org/foo/bar#;?",
  1123. 0, 0, CURLUE_OK},
  1124. {"http://example.org/foo/bar",
  1125. "#",
  1126. "http://example.org/foo/bar",
  1127. /* This happens because the parser removes empty fragments */
  1128. 0, 0, CURLUE_OK},
  1129. {"http://example.org/foo/bar",
  1130. "?",
  1131. "http://example.org/foo/bar",
  1132. /* This happens because the parser removes empty queries */
  1133. 0, 0, CURLUE_OK},
  1134. {"http://example.org/foo/bar",
  1135. "?#",
  1136. "http://example.org/foo/bar",
  1137. /* This happens because the parser removes empty queries and fragments */
  1138. 0, 0, CURLUE_OK},
  1139. {"http://example.com/please/../gimme/%TESTNUMBER?foobar#hello",
  1140. "http://example.net/there/it/is/../../tes t case=/%TESTNUMBER0002? yes no",
  1141. "http://example.net/there/tes%20t%20case=/%TESTNUMBER0002?+yes+no",
  1142. 0, CURLU_URLENCODE|CURLU_ALLOW_SPACE, CURLUE_OK},
  1143. {"http://local.test?redirect=http://local.test:80?-321",
  1144. "http://local.test:80?-123",
  1145. "http://local.test:80/?-123",
  1146. 0, CURLU_URLENCODE|CURLU_ALLOW_SPACE, CURLUE_OK},
  1147. {"http://local.test?redirect=http://local.test:80?-321",
  1148. "http://local.test:80?-123",
  1149. "http://local.test:80/?-123",
  1150. 0, 0, CURLUE_OK},
  1151. {"http://example.org/static/favicon/wikipedia.ico",
  1152. "//fake.example.com/licenses/by-sa/3.0/",
  1153. "http://fake.example.com/licenses/by-sa/3.0/",
  1154. 0, 0, CURLUE_OK},
  1155. {"https://example.org/static/favicon/wikipedia.ico",
  1156. "//fake.example.com/licenses/by-sa/3.0/",
  1157. "https://fake.example.com/licenses/by-sa/3.0/",
  1158. 0, 0, CURLUE_OK},
  1159. {"file://localhost/path?query#frag",
  1160. "foo#another",
  1161. "file:///foo#another",
  1162. 0, 0, CURLUE_OK},
  1163. {"http://example.com/path?query#frag",
  1164. "https://two.example.com/bradnew",
  1165. "https://two.example.com/bradnew",
  1166. 0, 0, CURLUE_OK},
  1167. {"http://example.com/path?query#frag",
  1168. "../../newpage#foo",
  1169. "http://example.com/newpage#foo",
  1170. 0, 0, CURLUE_OK},
  1171. {"http://user:foo@example.com/path?query#frag",
  1172. "../../newpage",
  1173. "http://user:foo@example.com/newpage",
  1174. 0, 0, CURLUE_OK},
  1175. {"http://user:foo@example.com/path?query#frag",
  1176. "../newpage",
  1177. "http://user:foo@example.com/newpage",
  1178. 0, 0, CURLUE_OK},
  1179. {"http://user:foo@example.com/path?query#frag",
  1180. "http://?hi",
  1181. "http:///?hi",
  1182. 0, CURLU_NO_AUTHORITY, CURLUE_OK},
  1183. {NULL, NULL, NULL, 0, 0, CURLUE_OK}
  1184. };
  1185. static int set_url(void)
  1186. {
  1187. int i;
  1188. int error = 0;
  1189. for(i = 0; set_url_list[i].in && !error; i++) {
  1190. CURLUcode rc;
  1191. CURLU *urlp = curl_url();
  1192. if(!urlp)
  1193. break;
  1194. rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].in,
  1195. set_url_list[i].urlflags);
  1196. if(!rc) {
  1197. rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].set,
  1198. set_url_list[i].setflags);
  1199. if(rc) {
  1200. fprintf(stderr, "%s:%d Set URL %s returned %d (%s)\n",
  1201. __FILE__, __LINE__, set_url_list[i].set,
  1202. (int)rc, curl_url_strerror(rc));
  1203. error++;
  1204. }
  1205. else {
  1206. char *url = NULL;
  1207. rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
  1208. if(rc) {
  1209. fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
  1210. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1211. error++;
  1212. }
  1213. else {
  1214. if(checkurl(set_url_list[i].in, url, set_url_list[i].out)) {
  1215. error++;
  1216. }
  1217. }
  1218. curl_free(url);
  1219. }
  1220. }
  1221. else if(rc != set_url_list[i].ucode) {
  1222. fprintf(stderr, "Set URL\nin: %s\nreturned %d (expected %d)\n",
  1223. set_url_list[i].in, (int)rc, set_url_list[i].ucode);
  1224. error++;
  1225. }
  1226. curl_url_cleanup(urlp);
  1227. }
  1228. return error;
  1229. }
  1230. /* 1. Set a URL
  1231. 2. Set one or more parts
  1232. 3. Extract and compare all parts - not the URL
  1233. */
  1234. static int setget_parts(void)
  1235. {
  1236. int i;
  1237. int error = 0;
  1238. for(i = 0; setget_parts_list[i].set && !error; i++) {
  1239. CURLUcode rc;
  1240. CURLU *urlp = curl_url();
  1241. if(!urlp) {
  1242. error++;
  1243. break;
  1244. }
  1245. if(setget_parts_list[i].in)
  1246. rc = curl_url_set(urlp, CURLUPART_URL, setget_parts_list[i].in,
  1247. setget_parts_list[i].urlflags);
  1248. else
  1249. rc = CURLUE_OK;
  1250. if(!rc) {
  1251. char *url = NULL;
  1252. CURLUcode uc = updateurl(urlp, setget_parts_list[i].set,
  1253. setget_parts_list[i].setflags);
  1254. if(uc != setget_parts_list[i].pcode) {
  1255. fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n",
  1256. setget_parts_list[i].set, (int)uc, setget_parts_list[i].pcode);
  1257. error++;
  1258. }
  1259. if(!uc) {
  1260. if(checkparts(urlp, setget_parts_list[i].set, setget_parts_list[i].out,
  1261. setget_parts_list[i].getflags))
  1262. error++; /* add */
  1263. }
  1264. curl_free(url);
  1265. }
  1266. else if(rc != CURLUE_OK) {
  1267. fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
  1268. setget_parts_list[i].in, (int)rc, 0);
  1269. error++;
  1270. }
  1271. curl_url_cleanup(urlp);
  1272. }
  1273. return error;
  1274. }
  1275. static int set_parts(void)
  1276. {
  1277. int i;
  1278. int error = 0;
  1279. for(i = 0; set_parts_list[i].set && !error; i++) {
  1280. CURLUcode rc;
  1281. CURLU *urlp = curl_url();
  1282. if(!urlp) {
  1283. error++;
  1284. break;
  1285. }
  1286. if(set_parts_list[i].in)
  1287. rc = curl_url_set(urlp, CURLUPART_URL, set_parts_list[i].in,
  1288. set_parts_list[i].urlflags);
  1289. else
  1290. rc = CURLUE_OK;
  1291. if(!rc) {
  1292. char *url = NULL;
  1293. CURLUcode uc = updateurl(urlp, set_parts_list[i].set,
  1294. set_parts_list[i].setflags);
  1295. if(uc != set_parts_list[i].pcode) {
  1296. fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n",
  1297. set_parts_list[i].set, (int)uc, set_parts_list[i].pcode);
  1298. error++;
  1299. }
  1300. if(!uc) {
  1301. /* only do this if it worked */
  1302. rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
  1303. if(rc) {
  1304. fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
  1305. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1306. error++;
  1307. }
  1308. else if(checkurl(set_parts_list[i].in, url, set_parts_list[i].out)) {
  1309. error++;
  1310. }
  1311. }
  1312. curl_free(url);
  1313. }
  1314. else if(rc != set_parts_list[i].ucode) {
  1315. fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
  1316. set_parts_list[i].in, (int)rc, set_parts_list[i].ucode);
  1317. error++;
  1318. }
  1319. curl_url_cleanup(urlp);
  1320. }
  1321. return error;
  1322. }
  1323. static int get_url(void)
  1324. {
  1325. int i;
  1326. int error = 0;
  1327. for(i = 0; get_url_list[i].in && !error; i++) {
  1328. CURLUcode rc;
  1329. CURLU *urlp = curl_url();
  1330. if(!urlp) {
  1331. error++;
  1332. break;
  1333. }
  1334. rc = curl_url_set(urlp, CURLUPART_URL, get_url_list[i].in,
  1335. get_url_list[i].urlflags);
  1336. if(!rc) {
  1337. char *url = NULL;
  1338. rc = curl_url_get(urlp, CURLUPART_URL, &url, get_url_list[i].getflags);
  1339. if(rc) {
  1340. fprintf(stderr, "%s:%d returned %d (%s). URL: '%s'\n",
  1341. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc),
  1342. get_url_list[i].in);
  1343. error++;
  1344. }
  1345. else {
  1346. if(checkurl(get_url_list[i].in, url, get_url_list[i].out)) {
  1347. error++;
  1348. }
  1349. }
  1350. curl_free(url);
  1351. }
  1352. if(rc != get_url_list[i].ucode) {
  1353. fprintf(stderr, "Get URL\nin: %s\nreturned %d (expected %d)\n",
  1354. get_url_list[i].in, (int)rc, get_url_list[i].ucode);
  1355. error++;
  1356. }
  1357. curl_url_cleanup(urlp);
  1358. }
  1359. return error;
  1360. }
  1361. static int get_parts(void)
  1362. {
  1363. int i;
  1364. int error = 0;
  1365. for(i = 0; get_parts_list[i].in && !error; i++) {
  1366. CURLUcode rc;
  1367. CURLU *urlp = curl_url();
  1368. if(!urlp) {
  1369. error++;
  1370. break;
  1371. }
  1372. rc = curl_url_set(urlp, CURLUPART_URL,
  1373. get_parts_list[i].in,
  1374. get_parts_list[i].urlflags);
  1375. if(rc != get_parts_list[i].ucode) {
  1376. fprintf(stderr, "Get parts\nin: %s\nreturned %d (expected %d)\n",
  1377. get_parts_list[i].in, (int)rc, get_parts_list[i].ucode);
  1378. error++;
  1379. }
  1380. else if(get_parts_list[i].ucode) {
  1381. /* the expected error happened */
  1382. }
  1383. else if(checkparts(urlp, get_parts_list[i].in, get_parts_list[i].out,
  1384. get_parts_list[i].getflags))
  1385. error++;
  1386. curl_url_cleanup(urlp);
  1387. }
  1388. return error;
  1389. }
  1390. static const struct querycase append_list[] = {
  1391. {"HTTP://test/?s", "name=joe\x02", "http://test/?s&name=joe%02",
  1392. 0, CURLU_URLENCODE, CURLUE_OK},
  1393. {"HTTP://test/?size=2#f", "name=joe=", "http://test/?size=2&name=joe%3d#f",
  1394. 0, CURLU_URLENCODE, CURLUE_OK},
  1395. {"HTTP://test/?size=2#f", "name=joe doe",
  1396. "http://test/?size=2&name=joe+doe#f",
  1397. 0, CURLU_URLENCODE, CURLUE_OK},
  1398. {"HTTP://test/", "name=joe", "http://test/?name=joe", 0, 0, CURLUE_OK},
  1399. {"HTTP://test/?size=2", "name=joe", "http://test/?size=2&name=joe",
  1400. 0, 0, CURLUE_OK},
  1401. {"HTTP://test/?size=2&", "name=joe", "http://test/?size=2&name=joe",
  1402. 0, 0, CURLUE_OK},
  1403. {"HTTP://test/?size=2#f", "name=joe", "http://test/?size=2&name=joe#f",
  1404. 0, 0, CURLUE_OK},
  1405. {NULL, NULL, NULL, 0, 0, CURLUE_OK}
  1406. };
  1407. static int append(void)
  1408. {
  1409. int i;
  1410. int error = 0;
  1411. for(i = 0; append_list[i].in && !error; i++) {
  1412. CURLUcode rc;
  1413. CURLU *urlp = curl_url();
  1414. if(!urlp) {
  1415. error++;
  1416. break;
  1417. }
  1418. rc = curl_url_set(urlp, CURLUPART_URL,
  1419. append_list[i].in,
  1420. append_list[i].urlflags);
  1421. if(rc)
  1422. error++;
  1423. else
  1424. rc = curl_url_set(urlp, CURLUPART_QUERY,
  1425. append_list[i].q,
  1426. append_list[i].qflags | CURLU_APPENDQUERY);
  1427. if(error)
  1428. ;
  1429. else if(rc != append_list[i].ucode) {
  1430. fprintf(stderr, "Append\nin: %s\nreturned %d (expected %d)\n",
  1431. append_list[i].in, (int)rc, append_list[i].ucode);
  1432. error++;
  1433. }
  1434. else if(append_list[i].ucode) {
  1435. /* the expected error happened */
  1436. }
  1437. else {
  1438. char *url;
  1439. rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
  1440. if(rc) {
  1441. fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
  1442. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1443. error++;
  1444. }
  1445. else {
  1446. if(checkurl(append_list[i].in, url, append_list[i].out)) {
  1447. error++;
  1448. }
  1449. curl_free(url);
  1450. }
  1451. }
  1452. curl_url_cleanup(urlp);
  1453. }
  1454. return error;
  1455. }
  1456. static int scopeid(void)
  1457. {
  1458. CURLU *u = curl_url();
  1459. int error = 0;
  1460. CURLUcode rc;
  1461. char *url;
  1462. rc = curl_url_set(u, CURLUPART_URL,
  1463. "https://[fe80::20c:29ff:fe9c:409b%25eth0]/hello.html", 0);
  1464. if(rc != CURLUE_OK) {
  1465. fprintf(stderr, "%s:%d curl_url_set returned %d (%s)\n",
  1466. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1467. error++;
  1468. }
  1469. rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
  1470. if(rc != CURLUE_OK) {
  1471. fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d (%s)\n",
  1472. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1473. error++;
  1474. }
  1475. else {
  1476. curl_free(url);
  1477. }
  1478. rc = curl_url_set(u, CURLUPART_HOST, "[::1]", 0);
  1479. if(rc != CURLUE_OK) {
  1480. fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
  1481. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1482. error++;
  1483. }
  1484. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1485. if(rc != CURLUE_OK) {
  1486. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1487. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1488. error++;
  1489. }
  1490. else {
  1491. curl_free(url);
  1492. }
  1493. rc = curl_url_set(u, CURLUPART_HOST, "example.com", 0);
  1494. if(rc != CURLUE_OK) {
  1495. fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
  1496. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1497. error++;
  1498. }
  1499. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1500. if(rc != CURLUE_OK) {
  1501. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1502. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1503. error++;
  1504. }
  1505. else {
  1506. curl_free(url);
  1507. }
  1508. rc = curl_url_set(u, CURLUPART_HOST,
  1509. "[fe80::20c:29ff:fe9c:409b%25eth0]", 0);
  1510. if(rc != CURLUE_OK) {
  1511. fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
  1512. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1513. error++;
  1514. }
  1515. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1516. if(rc != CURLUE_OK) {
  1517. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1518. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1519. error++;
  1520. }
  1521. else {
  1522. curl_free(url);
  1523. }
  1524. rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
  1525. if(rc != CURLUE_OK) {
  1526. fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d (%s)\n",
  1527. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1528. error++;
  1529. }
  1530. else {
  1531. curl_free(url);
  1532. }
  1533. rc = curl_url_get(u, CURLUPART_ZONEID, &url, 0);
  1534. if(rc != CURLUE_OK) {
  1535. fprintf(stderr, "%s:%d curl_url_get CURLUPART_ZONEID returned %d (%s)\n",
  1536. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1537. error++;
  1538. }
  1539. else {
  1540. curl_free(url);
  1541. }
  1542. rc = curl_url_set(u, CURLUPART_ZONEID, "clown", 0);
  1543. if(rc != CURLUE_OK) {
  1544. fprintf(stderr, "%s:%d curl_url_set CURLUPART_ZONEID returned %d (%s)\n",
  1545. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1546. error++;
  1547. }
  1548. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1549. if(rc != CURLUE_OK) {
  1550. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1551. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1552. error++;
  1553. }
  1554. else {
  1555. curl_free(url);
  1556. }
  1557. curl_url_cleanup(u);
  1558. return error;
  1559. }
  1560. static int get_nothing(void)
  1561. {
  1562. CURLU *u = curl_url();
  1563. if(u) {
  1564. char *p;
  1565. CURLUcode rc;
  1566. rc = curl_url_get(u, CURLUPART_SCHEME, &p, 0);
  1567. if(rc != CURLUE_NO_SCHEME)
  1568. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1569. rc = curl_url_get(u, CURLUPART_HOST, &p, 0);
  1570. if(rc != CURLUE_NO_HOST)
  1571. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1572. rc = curl_url_get(u, CURLUPART_USER, &p, 0);
  1573. if(rc != CURLUE_NO_USER)
  1574. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1575. rc = curl_url_get(u, CURLUPART_PASSWORD, &p, 0);
  1576. if(rc != CURLUE_NO_PASSWORD)
  1577. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1578. rc = curl_url_get(u, CURLUPART_OPTIONS, &p, 0);
  1579. if(rc != CURLUE_NO_OPTIONS)
  1580. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1581. rc = curl_url_get(u, CURLUPART_PATH, &p, 0);
  1582. if(rc != CURLUE_OK)
  1583. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1584. else
  1585. curl_free(p);
  1586. rc = curl_url_get(u, CURLUPART_QUERY, &p, 0);
  1587. if(rc != CURLUE_NO_QUERY)
  1588. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1589. rc = curl_url_get(u, CURLUPART_FRAGMENT, &p, 0);
  1590. if(rc != CURLUE_NO_FRAGMENT)
  1591. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1592. rc = curl_url_get(u, CURLUPART_ZONEID, &p, 0);
  1593. if(rc != CURLUE_NO_ZONEID)
  1594. fprintf(stderr, "unexpected return code %u on line %u\n", (int)rc,
  1595. __LINE__);
  1596. curl_url_cleanup(u);
  1597. }
  1598. return 0;
  1599. }
  1600. static const struct clearurlcase clear_url_list[] ={
  1601. {CURLUPART_SCHEME, "http", NULL, CURLUE_NO_SCHEME},
  1602. {CURLUPART_USER, "user", NULL, CURLUE_NO_USER},
  1603. {CURLUPART_PASSWORD, "password", NULL, CURLUE_NO_PASSWORD},
  1604. {CURLUPART_OPTIONS, "options", NULL, CURLUE_NO_OPTIONS},
  1605. {CURLUPART_HOST, "host", NULL, CURLUE_NO_HOST},
  1606. {CURLUPART_ZONEID, "eth0", NULL, CURLUE_NO_ZONEID},
  1607. {CURLUPART_PORT, "1234", NULL, CURLUE_NO_PORT},
  1608. {CURLUPART_PATH, "/hello", "/", CURLUE_OK},
  1609. {CURLUPART_QUERY, "a=b", NULL, CURLUE_NO_QUERY},
  1610. {CURLUPART_FRAGMENT, "anchor", NULL, CURLUE_NO_FRAGMENT},
  1611. {CURLUPART_URL, NULL, NULL, CURLUE_OK},
  1612. };
  1613. static int clear_url(void)
  1614. {
  1615. CURLU *u = curl_url();
  1616. int i, error = 0;
  1617. if(u) {
  1618. char *p = NULL;
  1619. CURLUcode rc;
  1620. for(i = 0; clear_url_list[i].in && !error; i++) {
  1621. rc = curl_url_set(u, clear_url_list[i].part, clear_url_list[i].in, 0);
  1622. if(rc != CURLUE_OK)
  1623. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1624. rc = curl_url_set(u, CURLUPART_URL, NULL, 0);
  1625. if(rc != CURLUE_OK)
  1626. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1627. rc = curl_url_get(u, clear_url_list[i].part, &p, 0);
  1628. if(rc != clear_url_list[i].ucode || (clear_url_list[i].out &&
  1629. 0 != strcmp(p, clear_url_list[i].out))) {
  1630. fprintf(stderr, "unexpected return code line %u\n", __LINE__);
  1631. error++;
  1632. }
  1633. if(rc == CURLUE_OK)
  1634. curl_free(p);
  1635. }
  1636. }
  1637. curl_url_cleanup(u);
  1638. return error;
  1639. }
  1640. static char total[128000];
  1641. static char bigpart[120000];
  1642. /*
  1643. * verify ridiculous URL part sizes
  1644. */
  1645. static int huge(void)
  1646. {
  1647. const char *smallpart = "c";
  1648. int i;
  1649. CURLU *urlp = curl_url();
  1650. CURLUcode rc;
  1651. CURLUPart part[]= {
  1652. CURLUPART_SCHEME,
  1653. CURLUPART_USER,
  1654. CURLUPART_PASSWORD,
  1655. CURLUPART_HOST,
  1656. CURLUPART_PATH,
  1657. CURLUPART_QUERY,
  1658. CURLUPART_FRAGMENT
  1659. };
  1660. int error = 0;
  1661. if(!urlp)
  1662. return 1;
  1663. bigpart[0] = '/'; /* for the path */
  1664. memset(&bigpart[1], 'a', sizeof(bigpart) - 2);
  1665. bigpart[sizeof(bigpart) - 1] = 0;
  1666. for(i = 0; i < 7; i++) {
  1667. char *partp;
  1668. msnprintf(total, sizeof(total),
  1669. "%s://%s:%s@%s/%s?%s#%s",
  1670. (i == 0)? &bigpart[1] : smallpart,
  1671. (i == 1)? &bigpart[1] : smallpart,
  1672. (i == 2)? &bigpart[1] : smallpart,
  1673. (i == 3)? &bigpart[1] : smallpart,
  1674. (i == 4)? &bigpart[1] : smallpart,
  1675. (i == 5)? &bigpart[1] : smallpart,
  1676. (i == 6)? &bigpart[1] : smallpart);
  1677. rc = curl_url_set(urlp, CURLUPART_URL, total, CURLU_NON_SUPPORT_SCHEME);
  1678. if((!i && (rc != CURLUE_BAD_SCHEME)) ||
  1679. (i && rc)) {
  1680. printf("URL %u: failed to parse [%s]\n", i, total);
  1681. error++;
  1682. }
  1683. /* only extract if the parse worked */
  1684. if(!rc) {
  1685. curl_url_get(urlp, part[i], &partp, 0);
  1686. if(!partp || strcmp(partp, &bigpart[1 - (i == 4)])) {
  1687. printf("URL %u part %u: failure\n", i, part[i]);
  1688. error++;
  1689. }
  1690. curl_free(partp);
  1691. }
  1692. }
  1693. curl_url_cleanup(urlp);
  1694. return error;
  1695. }
  1696. static int urldup(void)
  1697. {
  1698. const char *url[] = {
  1699. "http://"
  1700. "user:pwd@"
  1701. "[2a04:4e42:e00::347%25eth0]"
  1702. ":80"
  1703. "/path"
  1704. "?query"
  1705. "#fraggie",
  1706. "https://example.com",
  1707. "https://user@example.com",
  1708. "https://user.pwd@example.com",
  1709. "https://user.pwd@example.com:1234",
  1710. "https://example.com:1234",
  1711. "example.com:1234",
  1712. "https://user.pwd@example.com:1234/path?query#frag",
  1713. NULL
  1714. };
  1715. CURLU *copy = NULL;
  1716. char *h_str = NULL, *copy_str = NULL;
  1717. CURLU *h = curl_url();
  1718. int i;
  1719. if(!h)
  1720. goto err;
  1721. for(i = 0; url[i]; i++) {
  1722. CURLUcode rc = curl_url_set(h, CURLUPART_URL, url[i],
  1723. CURLU_GUESS_SCHEME);
  1724. if(rc)
  1725. goto err;
  1726. copy = curl_url_dup(h);
  1727. rc = curl_url_get(h, CURLUPART_URL, &h_str, 0);
  1728. if(rc)
  1729. goto err;
  1730. rc = curl_url_get(copy, CURLUPART_URL, &copy_str, 0);
  1731. if(rc)
  1732. goto err;
  1733. if(strcmp(h_str, copy_str)) {
  1734. printf("Original: %s\nParsed: %s\nCopy: %s\n",
  1735. url[i], h_str, copy_str);
  1736. goto err;
  1737. }
  1738. curl_free(copy_str);
  1739. curl_free(h_str);
  1740. curl_url_cleanup(copy);
  1741. copy_str = NULL;
  1742. h_str = NULL;
  1743. copy = NULL;
  1744. }
  1745. curl_url_cleanup(h);
  1746. return 0;
  1747. err:
  1748. curl_free(copy_str);
  1749. curl_free(h_str);
  1750. curl_url_cleanup(copy);
  1751. curl_url_cleanup(h);
  1752. return 1;
  1753. }
  1754. int test(char *URL)
  1755. {
  1756. (void)URL; /* not used */
  1757. if(urldup())
  1758. return 11;
  1759. if(setget_parts())
  1760. return 10;
  1761. if(get_url())
  1762. return 3;
  1763. if(huge())
  1764. return 9;
  1765. if(get_nothing())
  1766. return 7;
  1767. if(scopeid())
  1768. return 6;
  1769. if(append())
  1770. return 5;
  1771. if(set_url())
  1772. return 1;
  1773. if(set_parts())
  1774. return 2;
  1775. if(get_parts())
  1776. return 4;
  1777. if(clear_url())
  1778. return 8;
  1779. printf("success\n");
  1780. return 0;
  1781. }