lib1560.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, 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. ***************************************************************************/
  22. /*
  23. * Note:
  24. *
  25. * Since the URL parser by default only accepts schemes that *this instance*
  26. * of libcurl supports, make sure that the test1560 file lists all the schemes
  27. * that this test will assume to be present!
  28. */
  29. #include "test.h"
  30. #include "testutil.h"
  31. #include "warnless.h"
  32. #include "memdebug.h" /* LAST include file */
  33. struct part {
  34. CURLUPart part;
  35. const char *name;
  36. };
  37. static int checkparts(CURLU *u, const char *in, const char *wanted,
  38. unsigned int getflags)
  39. {
  40. int i;
  41. CURLUcode rc;
  42. char buf[256];
  43. char *bufp = &buf[0];
  44. size_t len = sizeof(buf);
  45. struct part parts[] = {
  46. {CURLUPART_SCHEME, "scheme"},
  47. {CURLUPART_USER, "user"},
  48. {CURLUPART_PASSWORD, "password"},
  49. {CURLUPART_OPTIONS, "options"},
  50. {CURLUPART_HOST, "host"},
  51. {CURLUPART_PORT, "port"},
  52. {CURLUPART_PATH, "path"},
  53. {CURLUPART_QUERY, "query"},
  54. {CURLUPART_FRAGMENT, "fragment"},
  55. {0, NULL}
  56. };
  57. memset(buf, 0, sizeof(buf));
  58. for(i = 0; parts[i].name; i++) {
  59. char *p = NULL;
  60. size_t n;
  61. rc = curl_url_get(u, parts[i].part, &p, getflags);
  62. if(!rc && p) {
  63. msnprintf(bufp, len, "%s%s", buf[0]?" | ":"", p);
  64. }
  65. else
  66. msnprintf(bufp, len, "%s[%d]", buf[0]?" | ":"", (int)rc);
  67. n = strlen(bufp);
  68. bufp += n;
  69. len -= n;
  70. curl_free(p);
  71. }
  72. if(strcmp(buf, wanted)) {
  73. fprintf(stderr, "in: %s\nwanted: %s\ngot: %s\n", in, wanted, buf);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. struct redircase {
  79. const char *in;
  80. const char *set;
  81. const char *out;
  82. unsigned int urlflags;
  83. unsigned int setflags;
  84. CURLUcode ucode;
  85. };
  86. struct setcase {
  87. const char *in;
  88. const char *set;
  89. const char *out;
  90. unsigned int urlflags;
  91. unsigned int setflags;
  92. CURLUcode ucode; /* for the main URL set */
  93. CURLUcode pcode; /* for updating parts */
  94. };
  95. struct testcase {
  96. const char *in;
  97. const char *out;
  98. unsigned int urlflags;
  99. unsigned int getflags;
  100. CURLUcode ucode;
  101. };
  102. struct urltestcase {
  103. const char *in;
  104. const char *out;
  105. unsigned int urlflags; /* pass to curl_url() */
  106. unsigned int getflags; /* pass to curl_url_get() */
  107. CURLUcode ucode;
  108. };
  109. struct querycase {
  110. const char *in;
  111. const char *q;
  112. const char *out;
  113. unsigned int urlflags; /* pass to curl_url() */
  114. unsigned int qflags; /* pass to curl_url_get() */
  115. CURLUcode ucode;
  116. };
  117. static const struct testcase get_parts_list[] ={
  118. {"https://user:[email protected]/get?this=and what", "",
  119. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  120. {"https://user:[email protected]/ge t?this=and-what", "",
  121. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  122. {"https://user:pass [email protected]/get?this=and-what", "",
  123. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  124. {"https://u ser:[email protected]/get?this=and-what", "",
  125. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  126. /* no space allowed in scheme */
  127. {"htt ps://user:[email protected]/get?this=and-what", "",
  128. CURLU_NON_SUPPORT_SCHEME|CURLU_ALLOW_SPACE, 0, CURLUE_MALFORMED_INPUT},
  129. {"https://user:[email protected]/get?this=and what",
  130. "https | user | password | [13] | example.net | [15] | /get | "
  131. "this=and what | [17]",
  132. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  133. {"https://user:[email protected]/ge t?this=and-what",
  134. "https | user | password | [13] | example.net | [15] | /ge t | "
  135. "this=and-what | [17]",
  136. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  137. {"https://user:pass [email protected]/get?this=and-what",
  138. "https | user | pass word | [13] | example.net | [15] | /get | "
  139. "this=and-what | [17]",
  140. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  141. {"https://u ser:[email protected]/get?this=and-what",
  142. "https | u ser | password | [13] | example.net | [15] | /get | "
  143. "this=and-what | [17]",
  144. CURLU_ALLOW_SPACE, 0, CURLUE_OK},
  145. {"https://user:[email protected]/ge t?this=and-what",
  146. "https | user | password | [13] | example.net | [15] | /ge%20t | "
  147. "this=and-what | [17]",
  148. CURLU_ALLOW_SPACE | CURLU_URLENCODE, 0, CURLUE_OK},
  149. {"[::1]",
  150. "http | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]",
  151. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  152. {"[::]",
  153. "http | [11] | [12] | [13] | [::] | [15] | / | [16] | [17]",
  154. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  155. {"https://[::1]",
  156. "https | [11] | [12] | [13] | [::1] | [15] | / | [16] | [17]",
  157. 0, 0, CURLUE_OK },
  158. {"user:[email protected]/color/#green?no-red",
  159. "ftp | user | moo | [13] | ftp.example.com | [15] | /color/ | [16] | "
  160. "green?no-red",
  161. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  162. {"ftp.user:[email protected]/color/#green?no-red",
  163. "http | ftp.user | moo | [13] | example.com | [15] | /color/ | [16] | "
  164. "green?no-red",
  165. CURLU_GUESS_SCHEME, 0, CURLUE_OK },
  166. #ifdef WIN32
  167. {"file:/C:\\programs\\foo",
  168. "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
  169. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  170. {"file://C:\\programs\\foo",
  171. "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
  172. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  173. {"file:///C:\\programs\\foo",
  174. "file | [11] | [12] | [13] | [14] | [15] | C:\\programs\\foo | [16] | [17]",
  175. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  176. {"file://host.example.com/Share/path/to/file.txt",
  177. "file | [11] | [12] | [13] | host.example.com | [15] | "
  178. "//host.example.com/Share/path/to/file.txt | [16] | [17]",
  179. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  180. #endif
  181. {"https://example.com/color/#green?no-red",
  182. "https | [11] | [12] | [13] | example.com | [15] | /color/ | [16] | "
  183. "green?no-red",
  184. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  185. {"https://example.com/color/#green#no-red",
  186. "https | [11] | [12] | [13] | example.com | [15] | /color/ | [16] | "
  187. "green#no-red",
  188. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  189. {"https://example.com/color/?green#no-red",
  190. "https | [11] | [12] | [13] | example.com | [15] | /color/ | green | "
  191. "no-red",
  192. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  193. {"https://example.com/#color/?green#no-red",
  194. "https | [11] | [12] | [13] | example.com | [15] | / | [16] | "
  195. "color/?green#no-red",
  196. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  197. {"https://example.#com/color/?green#no-red",
  198. "https | [11] | [12] | [13] | example. | [15] | / | [16] | "
  199. "com/color/?green#no-red",
  200. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK },
  201. {"http://[ab.be:1]/x", "",
  202. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  203. {"http://[ab.be]/x", "",
  204. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  205. /* URL without host name */
  206. {"http://a:b@/x", "",
  207. CURLU_DEFAULT_SCHEME, 0, CURLUE_NO_HOST},
  208. {"boing:80",
  209. "https | [11] | [12] | [13] | boing | 80 | / | [16] | [17]",
  210. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  211. {"http://[fd00:a41::50]:8080",
  212. "http | [11] | [12] | [13] | [fd00:a41::50] | 8080 | / | [16] | [17]",
  213. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  214. {"http://[fd00:a41::50]/",
  215. "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
  216. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  217. {"http://[fd00:a41::50]",
  218. "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
  219. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  220. {"https://[::1%252]:1234",
  221. "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
  222. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  223. /* here's "bad" zone id */
  224. {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
  225. "https | [11] | [12] | [13] | [fe80::20c:29ff:fe9c:409b] | 1234 "
  226. "| / | [16] | [17]",
  227. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  228. {"https://127.0.0.1:443",
  229. "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
  230. 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
  231. {"http://%3a:%3a@ex4mple/%3f+?+%3f+%23#+%23%3f%g7",
  232. "http | : | : | [13] | ex4mple | [15] | /?+ | ? # | +#?%g7",
  233. 0, CURLU_URLDECODE, CURLUE_OK},
  234. {"http://%3a:%3a@ex4mple/%3f?%3f%35#%35%3f%g7",
  235. "http | %3a | %3a | [13] | ex4mple | [15] | /%3f | %3f%35 | %35%3f%g7",
  236. 0, 0, CURLUE_OK},
  237. {"http://HO0_-st%41/",
  238. "http | [11] | [12] | [13] | HO0_-stA | [15] | / | [16] | [17]",
  239. 0, 0, CURLUE_OK},
  240. {"file://hello.html",
  241. "",
  242. 0, 0, CURLUE_MALFORMED_INPUT},
  243. {"http://HO0_-st/",
  244. "http | [11] | [12] | [13] | HO0_-st | [15] | / | [16] | [17]",
  245. 0, 0, CURLUE_OK},
  246. {"imap://user:pass;option@server/path",
  247. "imap | user | pass | option | server | [15] | /path | [16] | [17]",
  248. 0, 0, CURLUE_OK},
  249. {"http://user:pass;option@server/path",
  250. "http | user | pass;option | [13] | server | [15] | /path | [16] | [17]",
  251. 0, 0, CURLUE_OK},
  252. {"file:/hello.html",
  253. "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
  254. 0, 0, CURLUE_OK},
  255. {"file://127.0.0.1/hello.html",
  256. "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
  257. 0, 0, CURLUE_OK},
  258. {"file:////hello.html",
  259. "file | [11] | [12] | [13] | [14] | [15] | //hello.html | [16] | [17]",
  260. 0, 0, CURLUE_OK},
  261. {"file:///hello.html",
  262. "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
  263. 0, 0, CURLUE_OK},
  264. {"https://127.0.0.1",
  265. "https | [11] | [12] | [13] | 127.0.0.1 | 443 | / | [16] | [17]",
  266. 0, CURLU_DEFAULT_PORT, CURLUE_OK},
  267. {"https://127.0.0.1",
  268. "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
  269. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  270. {"https://[::1]:1234",
  271. "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
  272. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  273. {"https://127abc.com",
  274. "https | [11] | [12] | [13] | 127abc.com | [15] | / | [16] | [17]",
  275. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  276. {"https:// example.com?check", "",
  277. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  278. {"https://e x a m p l e.com?check", "",
  279. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  280. {"https://example.com?check",
  281. "https | [11] | [12] | [13] | example.com | [15] | / | check | [17]",
  282. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  283. {"https://example.com:65536",
  284. "",
  285. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
  286. {"https://example.com:0#moo",
  287. "",
  288. CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
  289. {"https://example.com:01#moo",
  290. "https | [11] | [12] | [13] | example.com | 1 | / | "
  291. "[16] | moo",
  292. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  293. {"https://example.com:1#moo",
  294. "https | [11] | [12] | [13] | example.com | 1 | / | "
  295. "[16] | moo",
  296. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  297. {"http://example.com#moo",
  298. "http | [11] | [12] | [13] | example.com | [15] | / | "
  299. "[16] | moo",
  300. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  301. {"http://example.com",
  302. "http | [11] | [12] | [13] | example.com | [15] | / | "
  303. "[16] | [17]",
  304. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  305. {"http://example.com/path/html",
  306. "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
  307. "[16] | [17]",
  308. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  309. {"http://example.com/path/html?query=name",
  310. "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
  311. "query=name | [17]",
  312. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  313. {"http://example.com/path/html?query=name#anchor",
  314. "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
  315. "query=name | anchor",
  316. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  317. {"http://example.com:1234/path/html?query=name#anchor",
  318. "http | [11] | [12] | [13] | example.com | 1234 | /path/html | "
  319. "query=name | anchor",
  320. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  321. {"http:///user:[email protected]:1234/path/html?query=name#anchor",
  322. "http | user | password | [13] | example.com | 1234 | /path/html | "
  323. "query=name | anchor",
  324. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  325. {"https://user:[email protected]:1234/path/html?query=name#anchor",
  326. "https | user | password | [13] | example.com | 1234 | /path/html | "
  327. "query=name | anchor",
  328. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  329. {"http://user:[email protected]:1234/path/html?query=name#anchor",
  330. "http | user | password | [13] | example.com | 1234 | /path/html | "
  331. "query=name | anchor",
  332. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  333. {"http:/user:[email protected]:1234/path/html?query=name#anchor",
  334. "http | user | password | [13] | example.com | 1234 | /path/html | "
  335. "query=name | anchor",
  336. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  337. {"http:////user:[email protected]:1234/path/html?query=name#anchor",
  338. "",
  339. CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  340. {NULL, NULL, 0, 0, CURLUE_OK},
  341. };
  342. static const struct urltestcase get_url_list[] = {
  343. /* percent encoded host names */
  344. {"https://%this", "https://%25this/", 0, 0, CURLUE_OK},
  345. {"https://h%c", "https://h%25c/", 0, 0, CURLUE_OK},
  346. {"https://%%%%%%", "https://%25%25%25%25%25%25/", 0, 0, CURLUE_OK},
  347. {"https://%41", "https://A/", 0, 0, CURLUE_OK},
  348. {"https://%20", "", 0, 0, CURLUE_MALFORMED_INPUT},
  349. {"https://%41%0d", "", 0, 0, CURLUE_MALFORMED_INPUT},
  350. {"https://%25", "https://%25/", 0, 0, CURLUE_OK},
  351. {"https://_%c0_", "https://_\xC0_/", 0, 0, CURLUE_OK},
  352. {"https://_%c0_", "https://_%C0_/", 0, CURLU_URLENCODE, CURLUE_OK},
  353. /* IPv4 trickeries */
  354. {"https://16843009", "https://1.1.1.1/", 0, 0, CURLUE_OK},
  355. {"https://0x7f.1", "https://127.0.0.1/", 0, 0, CURLUE_OK},
  356. {"https://0177.1", "https://127.0.0.1/", 0, 0, CURLUE_OK},
  357. {"https://0111.02.0x3", "https://73.2.0.3/", 0, 0, CURLUE_OK},
  358. {"https://0xff.0xff.0377.255", "https://255.255.255.255/", 0, 0, CURLUE_OK},
  359. {"https://1.0xffffff", "https://1.255.255.255/", 0, 0, CURLUE_OK},
  360. /* IPv4 numerical overflows or syntax errors will not normalize */
  361. {"https://+127.0.0.1", "https://+127.0.0.1/", 0, 0, CURLUE_OK},
  362. {"https://+127.0.0.1", "https://%2B127.0.0.1/", 0, CURLU_URLENCODE,
  363. CURLUE_OK},
  364. {"https://127.-0.0.1", "https://127.-0.0.1/", 0, 0, CURLUE_OK},
  365. {"https://127.0. 1", "https://127.0.0.1/", 0, 0, CURLUE_MALFORMED_INPUT},
  366. {"https://1.0x1000000", "https://1.0x1000000/", 0, 0, CURLUE_OK},
  367. {"https://1.2.3.256", "https://1.2.3.256/", 0, 0, CURLUE_OK},
  368. {"https://1.2.3.4.5", "https://1.2.3.4.5/", 0, 0, CURLUE_OK},
  369. {"https://1.2.0x100.3", "https://1.2.0x100.3/", 0, 0, CURLUE_OK},
  370. {"https://4294967296", "https://4294967296/", 0, 0, CURLUE_OK},
  371. /* 40 bytes scheme is the max allowed */
  372. {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
  373. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa://hostname/path",
  374. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  375. /* 41 bytes scheme is not allowed */
  376. {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
  377. "",
  378. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  379. {"https://[fe80::20c:29ff:fe9c:409b%]:1234",
  380. "",
  381. 0, 0, CURLUE_MALFORMED_INPUT},
  382. {"https://[fe80::20c:29ff:fe9c:409b%25]:1234",
  383. "https://[fe80::20c:29ff:fe9c:409b%2525]:1234/",
  384. 0, 0, CURLUE_OK},
  385. {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
  386. "https://[fe80::20c:29ff:fe9c:409b%25eth0]:1234/",
  387. 0, 0, CURLUE_OK},
  388. {"https://[::%25fakeit]/moo",
  389. "https://[::%25fakeit]/moo",
  390. 0, 0, CURLUE_OK},
  391. {"smtp.example.com/path/html",
  392. "smtp://smtp.example.com/path/html",
  393. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  394. {"https.example.com/path/html",
  395. "http://https.example.com/path/html",
  396. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  397. {"dict.example.com/path/html",
  398. "dict://dict.example.com/path/html",
  399. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  400. {"pop3.example.com/path/html",
  401. "pop3://pop3.example.com/path/html",
  402. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  403. {"ldap.example.com/path/html",
  404. "ldap://ldap.example.com/path/html",
  405. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  406. {"imap.example.com/path/html",
  407. "imap://imap.example.com/path/html",
  408. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  409. {"ftp.example.com/path/html",
  410. "ftp://ftp.example.com/path/html",
  411. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  412. {"example.com/path/html",
  413. "http://example.com/path/html",
  414. CURLU_GUESS_SCHEME, 0, CURLUE_OK},
  415. {"HTTP://test/", "http://test/", 0, 0, CURLUE_OK},
  416. {"http://HO0_-st..~./", "http://HO0_-st..~./", 0, 0, CURLUE_OK},
  417. {"http:/@example.com: 123/", "", 0, 0, CURLUE_MALFORMED_INPUT},
  418. {"http:/@example.com:123 /", "", 0, 0, CURLUE_MALFORMED_INPUT},
  419. {"http:/@example.com:123a/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
  420. {"http://host/file\r", "", 0, 0, CURLUE_MALFORMED_INPUT},
  421. {"http://host/file\n\x03", "", 0, 0, CURLUE_MALFORMED_INPUT},
  422. {"htt\x02://host/file", "",
  423. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  424. {" http://host/file", "", 0, 0, CURLUE_MALFORMED_INPUT},
  425. /* here the password ends at the semicolon and options is 'word' */
  426. {"imap://user:pass;word@host/file",
  427. "imap://user:pass;word@host/file",
  428. 0, 0, CURLUE_OK},
  429. /* here the password has the semicolon */
  430. {"http://user:pass;word@host/file",
  431. "http://user:pass;word@host/file",
  432. 0, 0, CURLUE_OK},
  433. {"file:///file.txt#moo",
  434. "file:///file.txt#moo",
  435. 0, 0, CURLUE_OK},
  436. {"file:////file.txt",
  437. "file:////file.txt",
  438. 0, 0, CURLUE_OK},
  439. {"file:///file.txt",
  440. "file:///file.txt",
  441. 0, 0, CURLUE_OK},
  442. {"file:./",
  443. "file://",
  444. 0, 0, CURLUE_MALFORMED_INPUT},
  445. {"http://example.com/hello/../here",
  446. "http://example.com/hello/../here",
  447. CURLU_PATH_AS_IS, 0, CURLUE_OK},
  448. {"http://example.com/hello/../here",
  449. "http://example.com/here",
  450. 0, 0, CURLUE_OK},
  451. {"http://example.com:80",
  452. "http://example.com/",
  453. 0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
  454. {"tp://example.com/path/html",
  455. "",
  456. 0, 0, CURLUE_UNSUPPORTED_SCHEME},
  457. {"http://hello:[email protected]",
  458. "",
  459. CURLU_DISALLOW_USER, 0, CURLUE_USER_NOT_ALLOWED},
  460. {"http:/@example.com:123",
  461. "http://example.com:123/",
  462. 0, 0, CURLUE_OK},
  463. {"http:/:[email protected]",
  464. "http://:[email protected]/",
  465. 0, 0, CURLUE_OK},
  466. {"http://[email protected]?#",
  467. "http://[email protected]/",
  468. 0, 0, CURLUE_OK},
  469. {"http://[email protected]?",
  470. "http://[email protected]/",
  471. 0, 0, CURLUE_OK},
  472. {"http://[email protected]#anchor",
  473. "http://[email protected]/#anchor",
  474. 0, 0, CURLUE_OK},
  475. {"example.com/path/html",
  476. "https://example.com/path/html",
  477. CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
  478. {"example.com/path/html",
  479. "",
  480. 0, 0, CURLUE_MALFORMED_INPUT},
  481. {"http://user:[email protected]:1234/path/html?query=name#anchor",
  482. "http://user:[email protected]:1234/path/html?query=name#anchor",
  483. 0, 0, CURLUE_OK},
  484. {"http://example.com:1234/path/html?query=name#anchor",
  485. "http://example.com:1234/path/html?query=name#anchor",
  486. 0, 0, CURLUE_OK},
  487. {"http://example.com/path/html?query=name#anchor",
  488. "http://example.com/path/html?query=name#anchor",
  489. 0, 0, CURLUE_OK},
  490. {"http://example.com/path/html?query=name",
  491. "http://example.com/path/html?query=name",
  492. 0, 0, CURLUE_OK},
  493. {"http://example.com/path/html",
  494. "http://example.com/path/html",
  495. 0, 0, CURLUE_OK},
  496. {"tp://example.com/path/html",
  497. "tp://example.com/path/html",
  498. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  499. {"custom-scheme://host?expected=test-good",
  500. "custom-scheme://host/?expected=test-good",
  501. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
  502. {"custom-scheme://?expected=test-bad",
  503. "",
  504. CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
  505. {"custom-scheme://?expected=test-new-good",
  506. "custom-scheme:///?expected=test-new-good",
  507. CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
  508. {"custom-scheme://host?expected=test-still-good",
  509. "custom-scheme://host/?expected=test-still-good",
  510. CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
  511. {NULL, NULL, 0, 0, 0}
  512. };
  513. static int checkurl(const char *url, const char *out)
  514. {
  515. if(strcmp(out, url)) {
  516. fprintf(stderr, "Wanted: %s\nGot : %s\n",
  517. out, url);
  518. return 1;
  519. }
  520. return 0;
  521. }
  522. /* !checksrc! disable SPACEBEFORECOMMA 1 */
  523. static const struct setcase set_parts_list[] = {
  524. {"https://example.com/",
  525. "host=++,", /* '++' there's no automatic URL decode when settin this
  526. part */
  527. "https://++/",
  528. 0, /* get */
  529. 0, /* set */
  530. CURLUE_OK, CURLUE_OK},
  531. {"https://example.com/",
  532. "query=Al2cO3tDkcDZ3EWE5Lh+LX8TPHs,", /* contains '+' */
  533. "https://example.com/?Al2cO3tDkcDZ3EWE5Lh%2bLX8TPHs",
  534. CURLU_URLDECODE, /* decode on get */
  535. CURLU_URLENCODE, /* encode on set */
  536. CURLUE_OK, CURLUE_OK},
  537. {"https://example.com/",
  538. /* Set a 41 bytes scheme. That's too long so the old scheme remains set. */
  539. "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbc,",
  540. "https://example.com/",
  541. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_MALFORMED_INPUT},
  542. {"https://example.com/",
  543. /* set a 40 bytes scheme */
  544. "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,",
  545. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb://example.com/",
  546. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  547. {"https://[::1%25fake]:1234/",
  548. "zoneid=NULL,",
  549. "https://[::1]:1234/",
  550. 0, 0, CURLUE_OK, CURLUE_OK},
  551. {"https://host:1234/",
  552. "port=NULL,",
  553. "https://host/",
  554. 0, 0, CURLUE_OK, CURLUE_OK},
  555. {"https://host:1234/",
  556. "port=\"\",",
  557. "https://host:1234/",
  558. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  559. {"https://host:1234/",
  560. "port=56 78,",
  561. "https://host:1234/",
  562. 0, 0, CURLUE_OK, CURLUE_MALFORMED_INPUT},
  563. {"https://host:1234/",
  564. "port=0,",
  565. "https://host:1234/",
  566. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  567. {"https://host:1234/",
  568. "port=65535,",
  569. "https://host:65535/",
  570. 0, 0, CURLUE_OK, CURLUE_OK},
  571. {"https://host:1234/",
  572. "port=65536,",
  573. "https://host:1234/",
  574. 0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
  575. {"https://host/",
  576. "path=%4A%4B%4C,",
  577. "https://host/%4a%4b%4c",
  578. 0, 0, CURLUE_OK, CURLUE_OK},
  579. {"https://host/mooo?q#f",
  580. "path=NULL,query=NULL,fragment=NULL,",
  581. "https://host/",
  582. 0, 0, CURLUE_OK, CURLUE_OK},
  583. {"https://user:secret@host/",
  584. "user=NULL,password=NULL,",
  585. "https://host/",
  586. 0, 0, CURLUE_OK, CURLUE_OK},
  587. {NULL,
  588. "scheme=https,user= @:,host=foobar,",
  589. "https://%20%20%20%40%3a@foobar/",
  590. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  591. /* Setting a host name with spaces is not OK: */
  592. {NULL,
  593. "scheme=https,host= ,path= ,user= ,password= ,query= ,fragment= ,",
  594. "[nothing]",
  595. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_MALFORMED_INPUT},
  596. {NULL,
  597. "scheme=https,host=foobar,path=/this /path /is /here,",
  598. "https://foobar/this%20/path%20/is%20/here",
  599. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  600. {NULL,
  601. "scheme=https,host=foobar,path=\xc3\xa4\xc3\xb6\xc3\xbc,",
  602. "https://foobar/%c3%a4%c3%b6%c3%bc",
  603. 0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
  604. {"imap://user:secret;opt@host/",
  605. "options=updated,scheme=imaps,password=p4ssw0rd,",
  606. "imaps://user:p4ssw0rd;updated@host/",
  607. 0, 0, CURLUE_NO_HOST, CURLUE_OK},
  608. {"imap://user:secret;optit@host/",
  609. "scheme=https,",
  610. "https://user:secret@host/",
  611. 0, 0, CURLUE_NO_HOST, CURLUE_OK},
  612. {"file:///file#anchor",
  613. "scheme=https,host=example,",
  614. "https://example/file#anchor",
  615. 0, 0, CURLUE_NO_HOST, CURLUE_OK},
  616. {NULL, /* start fresh! */
  617. "scheme=file,host=127.0.0.1,path=/no,user=anonymous,",
  618. "file:///no",
  619. 0, 0, CURLUE_OK, CURLUE_OK},
  620. {NULL, /* start fresh! */
  621. "scheme=ftp,host=127.0.0.1,path=/no,user=anonymous,",
  622. "ftp://[email protected]/no",
  623. 0, 0, CURLUE_OK, CURLUE_OK},
  624. {NULL, /* start fresh! */
  625. "scheme=https,host=example.com,",
  626. "https://example.com/",
  627. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  628. {"http://user:[email protected]/path?query#frag",
  629. "fragment=changed,",
  630. "http://user:[email protected]/path?query#changed",
  631. 0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
  632. {"http://example.com/",
  633. "scheme=foo,", /* not accepted */
  634. "http://example.com/",
  635. 0, 0, CURLUE_OK, CURLUE_UNSUPPORTED_SCHEME},
  636. {"http://example.com/",
  637. "scheme=https,path=/hello,fragment=snippet,",
  638. "https://example.com/hello#snippet",
  639. 0, 0, CURLUE_OK, CURLUE_OK},
  640. {"http://example.com:80",
  641. "user=foo,port=1922,",
  642. "http://[email protected]:1922/",
  643. 0, 0, CURLUE_OK, CURLUE_OK},
  644. {"http://example.com:80",
  645. "user=foo,password=bar,",
  646. "http://foo:[email protected]:80/",
  647. 0, 0, CURLUE_OK, CURLUE_OK},
  648. {"http://example.com:80",
  649. "user=foo,",
  650. "http://[email protected]:80/",
  651. 0, 0, CURLUE_OK, CURLUE_OK},
  652. {"http://example.com",
  653. "host=www.example.com,",
  654. "http://www.example.com/",
  655. 0, 0, CURLUE_OK, CURLUE_OK},
  656. {"http://example.com:80",
  657. "scheme=ftp,",
  658. "ftp://example.com:80/",
  659. 0, 0, CURLUE_OK, CURLUE_OK},
  660. {"custom-scheme://host",
  661. "host=\"\",",
  662. "custom-scheme://host/",
  663. CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK,
  664. CURLUE_MALFORMED_INPUT},
  665. {"custom-scheme://host",
  666. "host=\"\",",
  667. "custom-scheme:///",
  668. CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY,
  669. CURLUE_OK, CURLUE_OK},
  670. {NULL, NULL, NULL, 0, 0, 0, 0}
  671. };
  672. static CURLUPart part2id(char *part)
  673. {
  674. if(!strcmp("url", part))
  675. return CURLUPART_URL;
  676. if(!strcmp("scheme", part))
  677. return CURLUPART_SCHEME;
  678. if(!strcmp("user", part))
  679. return CURLUPART_USER;
  680. if(!strcmp("password", part))
  681. return CURLUPART_PASSWORD;
  682. if(!strcmp("options", part))
  683. return CURLUPART_OPTIONS;
  684. if(!strcmp("host", part))
  685. return CURLUPART_HOST;
  686. if(!strcmp("port", part))
  687. return CURLUPART_PORT;
  688. if(!strcmp("path", part))
  689. return CURLUPART_PATH;
  690. if(!strcmp("query", part))
  691. return CURLUPART_QUERY;
  692. if(!strcmp("fragment", part))
  693. return CURLUPART_FRAGMENT;
  694. if(!strcmp("zoneid", part))
  695. return CURLUPART_ZONEID;
  696. return (CURLUPart)9999; /* bad input => bad output */
  697. }
  698. static CURLUcode updateurl(CURLU *u, const char *cmd, unsigned int setflags)
  699. {
  700. const char *p = cmd;
  701. CURLUcode uc;
  702. /* make sure the last command ends with a comma too! */
  703. while(p) {
  704. char *e = strchr(p, ',');
  705. if(e) {
  706. size_t n = e-p;
  707. char buf[80];
  708. char part[80];
  709. char value[80];
  710. memset(part, 0, sizeof(part)); /* Avoid valgrind false positive. */
  711. memset(value, 0, sizeof(value)); /* Avoid valgrind false positive. */
  712. memcpy(buf, p, n);
  713. buf[n] = 0;
  714. if(2 == sscanf(buf, "%79[^=]=%79[^,]", part, value)) {
  715. CURLUPart what = part2id(part);
  716. #if 0
  717. /* for debugging this */
  718. fprintf(stderr, "%s = \"%s\" [%d]\n", part, value, (int)what);
  719. #endif
  720. if(what > CURLUPART_ZONEID)
  721. fprintf(stderr, "UNKNOWN part '%s'\n", part);
  722. if(!strcmp("NULL", value))
  723. uc = curl_url_set(u, what, NULL, setflags);
  724. else if(!strcmp("\"\"", value))
  725. uc = curl_url_set(u, what, "", setflags);
  726. else
  727. uc = curl_url_set(u, what, value, setflags);
  728. if(uc)
  729. return uc;
  730. }
  731. p = e + 1;
  732. continue;
  733. }
  734. break;
  735. }
  736. return CURLUE_OK;
  737. }
  738. static const struct redircase set_url_list[] = {
  739. {"http://example.org/static/favicon/wikipedia.ico",
  740. "//fake.example.com/licenses/by-sa/3.0/",
  741. "http://fake.example.com/licenses/by-sa/3.0/",
  742. 0, 0, 0},
  743. {"https://example.org/static/favicon/wikipedia.ico",
  744. "//fake.example.com/licenses/by-sa/3.0/",
  745. "https://fake.example.com/licenses/by-sa/3.0/",
  746. 0, 0, 0},
  747. {"file://localhost/path?query#frag",
  748. "foo#another",
  749. "file:///foo#another",
  750. 0, 0, 0},
  751. {"http://example.com/path?query#frag",
  752. "https://two.example.com/bradnew",
  753. "https://two.example.com/bradnew",
  754. 0, 0, 0},
  755. {"http://example.com/path?query#frag",
  756. "../../newpage#foo",
  757. "http://example.com/newpage#foo",
  758. 0, 0, 0},
  759. {"http://user:[email protected]/path?query#frag",
  760. "../../newpage",
  761. "http://user:[email protected]/newpage",
  762. 0, 0, 0},
  763. {"http://user:[email protected]/path?query#frag",
  764. "../newpage",
  765. "http://user:[email protected]/newpage",
  766. 0, 0, 0},
  767. {NULL, NULL, NULL, 0, 0, 0}
  768. };
  769. static int set_url(void)
  770. {
  771. int i;
  772. int error = 0;
  773. for(i = 0; set_url_list[i].in && !error; i++) {
  774. CURLUcode rc;
  775. CURLU *urlp = curl_url();
  776. if(!urlp)
  777. break;
  778. rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].in,
  779. set_url_list[i].urlflags);
  780. if(!rc) {
  781. rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].set,
  782. set_url_list[i].setflags);
  783. if(rc) {
  784. fprintf(stderr, "%s:%d Set URL %s returned %d (%s)\n",
  785. __FILE__, __LINE__, set_url_list[i].set,
  786. (int)rc, curl_url_strerror(rc));
  787. error++;
  788. }
  789. else {
  790. char *url = NULL;
  791. rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
  792. if(rc) {
  793. fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
  794. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  795. error++;
  796. }
  797. else {
  798. if(checkurl(url, set_url_list[i].out)) {
  799. error++;
  800. }
  801. }
  802. curl_free(url);
  803. }
  804. }
  805. else if(rc != set_url_list[i].ucode) {
  806. fprintf(stderr, "Set URL\nin: %s\nreturned %d (expected %d)\n",
  807. set_url_list[i].in, (int)rc, set_url_list[i].ucode);
  808. error++;
  809. }
  810. curl_url_cleanup(urlp);
  811. }
  812. return error;
  813. }
  814. static int set_parts(void)
  815. {
  816. int i;
  817. int error = 0;
  818. for(i = 0; set_parts_list[i].set && !error; i++) {
  819. CURLUcode rc;
  820. CURLU *urlp = curl_url();
  821. if(!urlp) {
  822. error++;
  823. break;
  824. }
  825. if(set_parts_list[i].in)
  826. rc = curl_url_set(urlp, CURLUPART_URL, set_parts_list[i].in,
  827. set_parts_list[i].urlflags);
  828. else
  829. rc = CURLUE_OK;
  830. if(!rc) {
  831. char *url = NULL;
  832. CURLUcode uc = updateurl(urlp, set_parts_list[i].set,
  833. set_parts_list[i].setflags);
  834. if(uc != set_parts_list[i].pcode) {
  835. fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n",
  836. set_parts_list[i].set, (int)uc, set_parts_list[i].pcode);
  837. error++;
  838. }
  839. if(!uc) {
  840. /* only do this if it worked */
  841. rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
  842. if(rc) {
  843. fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
  844. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  845. error++;
  846. }
  847. else if(checkurl(url, set_parts_list[i].out)) {
  848. error++;
  849. }
  850. }
  851. curl_free(url);
  852. }
  853. else if(rc != set_parts_list[i].ucode) {
  854. fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
  855. set_parts_list[i].in, (int)rc, set_parts_list[i].ucode);
  856. error++;
  857. }
  858. curl_url_cleanup(urlp);
  859. }
  860. return error;
  861. }
  862. static int get_url(void)
  863. {
  864. int i;
  865. int error = 0;
  866. for(i = 0; get_url_list[i].in && !error; i++) {
  867. CURLUcode rc;
  868. CURLU *urlp = curl_url();
  869. if(!urlp) {
  870. error++;
  871. break;
  872. }
  873. rc = curl_url_set(urlp, CURLUPART_URL, get_url_list[i].in,
  874. get_url_list[i].urlflags);
  875. if(!rc) {
  876. char *url = NULL;
  877. rc = curl_url_get(urlp, CURLUPART_URL, &url, get_url_list[i].getflags);
  878. if(rc) {
  879. fprintf(stderr, "%s:%d returned %d (%s). URL: '%s'\n",
  880. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc),
  881. get_url_list[i].in);
  882. error++;
  883. }
  884. else {
  885. if(checkurl(url, get_url_list[i].out)) {
  886. error++;
  887. }
  888. }
  889. curl_free(url);
  890. }
  891. else if(rc != get_url_list[i].ucode) {
  892. fprintf(stderr, "Get URL\nin: %s\nreturned %d (expected %d)\n",
  893. get_url_list[i].in, (int)rc, get_url_list[i].ucode);
  894. error++;
  895. }
  896. curl_url_cleanup(urlp);
  897. }
  898. return error;
  899. }
  900. static int get_parts(void)
  901. {
  902. int i;
  903. int error = 0;
  904. for(i = 0; get_parts_list[i].in && !error; i++) {
  905. CURLUcode rc;
  906. CURLU *urlp = curl_url();
  907. if(!urlp) {
  908. error++;
  909. break;
  910. }
  911. rc = curl_url_set(urlp, CURLUPART_URL,
  912. get_parts_list[i].in,
  913. get_parts_list[i].urlflags);
  914. if(rc != get_parts_list[i].ucode) {
  915. fprintf(stderr, "Get parts\nin: %s\nreturned %d (expected %d)\n",
  916. get_parts_list[i].in, (int)rc, get_parts_list[i].ucode);
  917. error++;
  918. }
  919. else if(get_parts_list[i].ucode) {
  920. /* the expected error happened */
  921. }
  922. else if(checkparts(urlp, get_parts_list[i].in, get_parts_list[i].out,
  923. get_parts_list[i].getflags))
  924. error++;
  925. curl_url_cleanup(urlp);
  926. }
  927. return error;
  928. }
  929. static const struct querycase append_list[] = {
  930. {"HTTP://test/?s", "name=joe\x02", "http://test/?s&name=joe%02",
  931. 0, CURLU_URLENCODE, CURLUE_OK},
  932. {"HTTP://test/?size=2#f", "name=joe=", "http://test/?size=2&name=joe%3d#f",
  933. 0, CURLU_URLENCODE, CURLUE_OK},
  934. {"HTTP://test/?size=2#f", "name=joe doe",
  935. "http://test/?size=2&name=joe+doe#f",
  936. 0, CURLU_URLENCODE, CURLUE_OK},
  937. {"HTTP://test/", "name=joe", "http://test/?name=joe", 0, 0, CURLUE_OK},
  938. {"HTTP://test/?size=2", "name=joe", "http://test/?size=2&name=joe",
  939. 0, 0, CURLUE_OK},
  940. {"HTTP://test/?size=2&", "name=joe", "http://test/?size=2&name=joe",
  941. 0, 0, CURLUE_OK},
  942. {"HTTP://test/?size=2#f", "name=joe", "http://test/?size=2&name=joe#f",
  943. 0, 0, CURLUE_OK},
  944. {NULL, NULL, NULL, 0, 0, 0}
  945. };
  946. static int append(void)
  947. {
  948. int i;
  949. int error = 0;
  950. for(i = 0; append_list[i].in && !error; i++) {
  951. CURLUcode rc;
  952. CURLU *urlp = curl_url();
  953. if(!urlp) {
  954. error++;
  955. break;
  956. }
  957. rc = curl_url_set(urlp, CURLUPART_URL,
  958. append_list[i].in,
  959. append_list[i].urlflags);
  960. if(rc)
  961. error++;
  962. else
  963. rc = curl_url_set(urlp, CURLUPART_QUERY,
  964. append_list[i].q,
  965. append_list[i].qflags | CURLU_APPENDQUERY);
  966. if(error)
  967. ;
  968. else if(rc != append_list[i].ucode) {
  969. fprintf(stderr, "Append\nin: %s\nreturned %d (expected %d)\n",
  970. append_list[i].in, (int)rc, append_list[i].ucode);
  971. error++;
  972. }
  973. else if(append_list[i].ucode) {
  974. /* the expected error happened */
  975. }
  976. else {
  977. char *url;
  978. rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
  979. if(rc) {
  980. fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
  981. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  982. error++;
  983. }
  984. else {
  985. if(checkurl(url, append_list[i].out)) {
  986. error++;
  987. }
  988. curl_free(url);
  989. }
  990. }
  991. curl_url_cleanup(urlp);
  992. }
  993. return error;
  994. }
  995. static int scopeid(void)
  996. {
  997. CURLU *u = curl_url();
  998. int error = 0;
  999. CURLUcode rc;
  1000. char *url;
  1001. rc = curl_url_set(u, CURLUPART_URL,
  1002. "https://[fe80::20c:29ff:fe9c:409b%25eth0]/hello.html", 0);
  1003. if(rc != CURLUE_OK) {
  1004. fprintf(stderr, "%s:%d curl_url_set returned %d (%s)\n",
  1005. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1006. error++;
  1007. }
  1008. rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
  1009. if(rc != CURLUE_OK) {
  1010. fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d (%s)\n",
  1011. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1012. error++;
  1013. }
  1014. else {
  1015. printf("we got %s\n", url);
  1016. curl_free(url);
  1017. }
  1018. rc = curl_url_set(u, CURLUPART_HOST, "[::1]", 0);
  1019. if(rc != CURLUE_OK) {
  1020. fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
  1021. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1022. error++;
  1023. }
  1024. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1025. if(rc != CURLUE_OK) {
  1026. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1027. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1028. error++;
  1029. }
  1030. else {
  1031. printf("we got %s\n", url);
  1032. curl_free(url);
  1033. }
  1034. rc = curl_url_set(u, CURLUPART_HOST, "example.com", 0);
  1035. if(rc != CURLUE_OK) {
  1036. fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
  1037. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1038. error++;
  1039. }
  1040. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1041. if(rc != CURLUE_OK) {
  1042. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1043. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1044. error++;
  1045. }
  1046. else {
  1047. printf("we got %s\n", url);
  1048. curl_free(url);
  1049. }
  1050. rc = curl_url_set(u, CURLUPART_HOST,
  1051. "[fe80::20c:29ff:fe9c:409b%25eth0]", 0);
  1052. if(rc != CURLUE_OK) {
  1053. fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
  1054. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1055. error++;
  1056. }
  1057. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1058. if(rc != CURLUE_OK) {
  1059. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1060. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1061. error++;
  1062. }
  1063. else {
  1064. printf("we got %s\n", url);
  1065. curl_free(url);
  1066. }
  1067. rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
  1068. if(rc != CURLUE_OK) {
  1069. fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d (%s)\n",
  1070. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1071. error++;
  1072. }
  1073. else {
  1074. printf("we got %s\n", url);
  1075. curl_free(url);
  1076. }
  1077. rc = curl_url_get(u, CURLUPART_ZONEID, &url, 0);
  1078. if(rc != CURLUE_OK) {
  1079. fprintf(stderr, "%s:%d curl_url_get CURLUPART_ZONEID returned %d (%s)\n",
  1080. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1081. error++;
  1082. }
  1083. else {
  1084. printf("we got %s\n", url);
  1085. curl_free(url);
  1086. }
  1087. rc = curl_url_set(u, CURLUPART_ZONEID, "clown", 0);
  1088. if(rc != CURLUE_OK) {
  1089. fprintf(stderr, "%s:%d curl_url_set CURLUPART_ZONEID returned %d (%s)\n",
  1090. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1091. error++;
  1092. }
  1093. rc = curl_url_get(u, CURLUPART_URL, &url, 0);
  1094. if(rc != CURLUE_OK) {
  1095. fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
  1096. __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
  1097. error++;
  1098. }
  1099. else {
  1100. printf("we got %s\n", url);
  1101. curl_free(url);
  1102. }
  1103. curl_url_cleanup(u);
  1104. return error;
  1105. }
  1106. int test(char *URL)
  1107. {
  1108. (void)URL; /* not used */
  1109. if(scopeid())
  1110. return 6;
  1111. if(append())
  1112. return 5;
  1113. if(set_url())
  1114. return 1;
  1115. if(set_parts())
  1116. return 2;
  1117. if(get_url())
  1118. return 3;
  1119. if(get_parts())
  1120. return 4;
  1121. printf("success\n");
  1122. return 0;
  1123. }