lib1560.c 40 KB

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