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