2
0

lib1560.c 56 KB

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