xref: /curl/tests/libtest/lib1560.c (revision fbf5d507)
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 #ifdef USE_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   {"https://[::%25fakeit];80/moo",
569    "",
570    0, 0, CURLUE_BAD_PORT_NUMBER},
571   {"https://[fe80::20c:29ff:fe9c:409b]-80/moo",
572    "",
573    0, 0, CURLUE_BAD_PORT_NUMBER},
574 #ifdef USE_IDN
575   {"https://räksmörgås.se/path?q#frag",
576    "https://xn--rksmrgs-5wao1o.se/path?q#frag", 0, CURLU_PUNYCODE, CURLUE_OK},
577 #endif
578   /* unsupported schemes with no guessing enabled */
579   {"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIEhUTUw+PG1ldGEgY",
580    "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
581   {"d:anything-really", "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
582   {"about:config", "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
583   {"example://foo", "", 0, 0, CURLUE_UNSUPPORTED_SCHEME},
584   {"mailto:infobot@example.com?body=send%20current-issue", "", 0, 0,
585    CURLUE_UNSUPPORTED_SCHEME},
586   {"about:80", "https://about:80/", CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
587   /* percent encoded host names */
588   {"http://example.com%40127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
589   {"http://example.com%21127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
590   {"http://example.com%3f127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
591   {"http://example.com%23127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
592   {"http://example.com%3a127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
593   {"http://example.com%09127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
594   {"http://example.com%2F127.0.0.1/", "", 0, 0, CURLUE_BAD_HOSTNAME},
595   {"https://%41", "https://A/", 0, 0, CURLUE_OK},
596   {"https://%20", "", 0, 0, CURLUE_BAD_HOSTNAME},
597   {"https://%41%0d", "", 0, 0, CURLUE_BAD_HOSTNAME},
598   {"https://%25", "", 0, 0, CURLUE_BAD_HOSTNAME},
599   {"https://_%c0_", "https://_\xC0_/", 0, 0, CURLUE_OK},
600   {"https://_%c0_", "https://_%C0_/", 0, CURLU_URLENCODE, CURLUE_OK},
601 
602   /* IPv4 trickeries */
603   {"https://16843009", "https://1.1.1.1/", 0, 0, CURLUE_OK},
604   {"https://0177.1", "https://127.0.0.1/", 0, 0, CURLUE_OK},
605   {"https://0111.02.0x3", "https://73.2.0.3/", 0, 0, CURLUE_OK},
606   {"https://0111.02.0x3.", "https://0111.02.0x3./", 0, 0, CURLUE_OK},
607   {"https://0111.02.030", "https://73.2.0.24/", 0, 0, CURLUE_OK},
608   {"https://0111.02.030.", "https://0111.02.030./", 0, 0, CURLUE_OK},
609   {"https://0xff.0xff.0377.255", "https://255.255.255.255/", 0, 0, CURLUE_OK},
610   {"https://1.0xffffff", "https://1.255.255.255/", 0, 0, CURLUE_OK},
611   /* IPv4 numerical overflows or syntax errors will not normalize */
612   {"https://a127.0.0.1", "https://a127.0.0.1/", 0, 0, CURLUE_OK},
613   {"https://\xff.127.0.0.1", "https://%FF.127.0.0.1/", 0, CURLU_URLENCODE,
614    CURLUE_OK},
615   {"https://127.-0.0.1", "https://127.-0.0.1/", 0, 0, CURLUE_OK},
616   {"https://127.0. 1", "https://127.0.0.1/", 0, 0, CURLUE_MALFORMED_INPUT},
617   {"https://1.2.3.256", "https://1.2.3.256/", 0, 0, CURLUE_OK},
618   {"https://1.2.3.256.", "https://1.2.3.256./", 0, 0, CURLUE_OK},
619   {"https://1.2.3.4.5", "https://1.2.3.4.5/", 0, 0, CURLUE_OK},
620   {"https://1.2.0x100.3", "https://1.2.0x100.3/", 0, 0, CURLUE_OK},
621   {"https://4294967296", "https://4294967296/", 0, 0, CURLUE_OK},
622   {"https://123host", "https://123host/", 0, 0, CURLUE_OK},
623   /* 40 bytes scheme is the max allowed */
624   {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
625    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa://hostname/path",
626    CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
627   /* 41 bytes scheme is not allowed */
628   {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA://hostname/path",
629    "",
630    CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_BAD_SCHEME},
631   {"https://[fe80::20c:29ff:fe9c:409b%]:1234",
632    "",
633    0, 0, CURLUE_BAD_IPV6},
634   {"https://[fe80::20c:29ff:fe9c:409b%25]:1234",
635    "https://[fe80::20c:29ff:fe9c:409b%2525]:1234/",
636    0, 0, CURLUE_OK},
637   {"https://[fe80::20c:29ff:fe9c:409b%eth0]:1234",
638    "https://[fe80::20c:29ff:fe9c:409b%25eth0]:1234/",
639    0, 0, CURLUE_OK},
640   {"https://[::%25fakeit]/moo",
641    "https://[::%25fakeit]/moo",
642    0, 0, CURLUE_OK},
643   {"smtp.example.com/path/html",
644    "smtp://smtp.example.com/path/html",
645    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
646   {"https.example.com/path/html",
647    "http://https.example.com/path/html",
648    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
649   {"dict.example.com/path/html",
650    "dict://dict.example.com/path/html",
651    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
652   {"pop3.example.com/path/html",
653    "pop3://pop3.example.com/path/html",
654    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
655   {"ldap.example.com/path/html",
656    "ldap://ldap.example.com/path/html",
657    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
658   {"imap.example.com/path/html",
659    "imap://imap.example.com/path/html",
660    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
661   {"ftp.example.com/path/html",
662    "ftp://ftp.example.com/path/html",
663    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
664   {"example.com/path/html",
665    "http://example.com/path/html",
666    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
667   {"smtp.com/path/html",
668    "smtp://smtp.com/path/html",
669    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
670   {"dict.com/path/html",
671    "dict://dict.com/path/html",
672    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
673   {"pop3.com/path/html",
674    "pop3://pop3.com/path/html",
675    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
676   {"ldap.com/path/html",
677    "ldap://ldap.com/path/html",
678    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
679   {"imap.com/path/html",
680    "imap://imap.com/path/html",
681    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
682   {"ftp.com/path/html",
683    "ftp://ftp.com/path/html",
684    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
685   {"smtp/path/html",
686    "http://smtp/path/html",
687    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
688   {"dict/path/html",
689    "http://dict/path/html",
690    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
691   {"pop3/path/html",
692    "http://pop3/path/html",
693    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
694   {"ldap/path/html",
695    "http://ldap/path/html",
696    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
697   {"imap/path/html",
698    "http://imap/path/html",
699    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
700   {"ftp/path/html",
701    "http://ftp/path/html",
702    CURLU_GUESS_SCHEME, 0, CURLUE_OK},
703   {"HTTP://test/", "http://test/", 0, 0, CURLUE_OK},
704   {"http://HO0_-st..~./", "http://HO0_-st..~./", 0, 0, CURLUE_OK},
705   {"http:/@example.com: 123/", "", 0, 0, CURLUE_MALFORMED_INPUT},
706   {"http:/@example.com:123 /", "", 0, 0, CURLUE_MALFORMED_INPUT},
707   {"http:/@example.com:123a/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
708   {"http://host/file\r", "", 0, 0, CURLUE_MALFORMED_INPUT},
709   {"http://host/file\n\x03", "", 0, 0, CURLUE_MALFORMED_INPUT},
710   {"htt\x02://host/file", "",
711    CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
712   {" http://host/file", "", 0, 0, CURLUE_MALFORMED_INPUT},
713   /* here the password ends at the semicolon and options is 'word' */
714   {"imap://user:pass;word@host/file",
715    "imap://user:pass;word@host/file",
716    0, 0, CURLUE_OK},
717   /* here the password has the semicolon */
718   {"http://user:pass;word@host/file",
719    "http://user:pass;word@host/file", 0, 0, CURLUE_OK},
720   {"file:///file.txt#moo", "file:///file.txt#moo", 0, 0, CURLUE_OK},
721   {"file:////file.txt", "file:////file.txt", 0, 0, CURLUE_OK},
722   {"file:///file.txt", "file:///file.txt", 0, 0, CURLUE_OK},
723   {"file:./", "file://", 0, 0, CURLUE_OK},
724   {"http://example.com/hello/../here",
725    "http://example.com/hello/../here",
726    CURLU_PATH_AS_IS, 0, CURLUE_OK},
727   {"http://example.com/hello/../here",
728    "http://example.com/here",
729    0, 0, CURLUE_OK},
730   {"http://example.com:80",
731    "http://example.com/",
732    0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
733   {"tp://example.com/path/html",
734    "",
735    0, 0, CURLUE_UNSUPPORTED_SCHEME},
736   {"http://hello:fool@example.com",
737    "",
738    CURLU_DISALLOW_USER, 0, CURLUE_USER_NOT_ALLOWED},
739   {"http:/@example.com:123",
740    "http://@example.com:123/",
741    0, 0, CURLUE_OK},
742   {"http:/:password@example.com",
743    "http://:password@example.com/",
744    0, 0, CURLUE_OK},
745   {"http://user@example.com?#",
746    "http://user@example.com/",
747    0, 0, CURLUE_OK},
748   {"http://user@example.com?",
749    "http://user@example.com/",
750    0, 0, CURLUE_OK},
751   {"http://user@example.com#anchor",
752    "http://user@example.com/#anchor",
753    0, 0, CURLUE_OK},
754   {"example.com/path/html",
755    "https://example.com/path/html",
756    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
757   {"example.com/path/html",
758    "",
759    0, 0, CURLUE_BAD_SCHEME},
760   {"http://user:password@example.com:1234/path/html?query=name#anchor",
761    "http://user:password@example.com:1234/path/html?query=name#anchor",
762    0, 0, CURLUE_OK},
763   {"http://example.com:1234/path/html?query=name#anchor",
764    "http://example.com:1234/path/html?query=name#anchor",
765    0, 0, CURLUE_OK},
766   {"http://example.com/path/html?query=name#anchor",
767    "http://example.com/path/html?query=name#anchor",
768    0, 0, CURLUE_OK},
769   {"http://example.com/path/html?query=name",
770    "http://example.com/path/html?query=name",
771    0, 0, CURLUE_OK},
772   {"http://example.com/path/html",
773    "http://example.com/path/html",
774    0, 0, CURLUE_OK},
775   {"tp://example.com/path/html",
776    "tp://example.com/path/html",
777    CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
778   {"custom-scheme://host?expected=test-good",
779    "custom-scheme://host/?expected=test-good",
780    CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
781   {"custom-scheme://?expected=test-bad",
782    "",
783    CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_NO_HOST},
784   {"custom-scheme://?expected=test-new-good",
785    "custom-scheme:///?expected=test-new-good",
786    CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
787   {"custom-scheme://host?expected=test-still-good",
788    "custom-scheme://host/?expected=test-still-good",
789    CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY, 0, CURLUE_OK},
790   {NULL, NULL, 0, 0, CURLUE_OK}
791 };
792 
checkurl(const char * org,const char * url,const char * out)793 static int checkurl(const char *org, const char *url, const char *out)
794 {
795   if(strcmp(out, url)) {
796     fprintf(stderr,
797             "Org:    %s\n"
798             "Wanted: %s\n"
799             "Got   : %s\n",
800             org, out, url);
801     return 1;
802   }
803   return 0;
804 }
805 
806 /* 1. Set the URL
807    2. Set components
808    3. Extract all components (not URL)
809 */
810 static const struct setgetcase setget_parts_list[] = {
811   {"https://example.com/",
812    "query=\"\",",
813    "https | [11] | [12] | [13] | example.com | [15] | / |  | [17]",
814    0, 0, CURLU_GET_EMPTY, CURLUE_OK},
815   {"https://example.com/",
816    "fragment=\"\",",
817    "https | [11] | [12] | [13] | example.com | [15] | / | [16] | ",
818    0, 0, CURLU_GET_EMPTY, CURLUE_OK},
819   {"https://example.com/",
820    "query=\"\",",
821    "https | [11] | [12] | [13] | example.com | [15] | / | [16] | [17]",
822    0, 0, 0, CURLUE_OK},
823   {"https://example.com",
824    "path=get,",
825    "https | [11] | [12] | [13] | example.com | [15] | /get | [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=g e t,",
833    "https | [11] | [12] | [13] | example.com | [15] | /g%20e%20t | "
834    "[16] | [17]",
835    0, CURLU_URLENCODE, 0, CURLUE_OK},
836   {NULL, NULL, NULL, 0, 0, 0, CURLUE_OK}
837 };
838 
839 /* !checksrc! disable SPACEBEFORECOMMA 1 */
840 static const struct setcase set_parts_list[] = {
841   {"https://example.com/",
842    "host=%43url.se,",
843    "https://%43url.se/",
844    0, 0, CURLUE_OK, CURLUE_OK},
845   {"https://example.com/",
846    "host=%25url.se,",
847    "",
848    0, 0, CURLUE_OK, CURLUE_BAD_HOSTNAME},
849   {"https://example.com/?param=value",
850    "query=\"\",",
851    "https://example.com/",
852    0, CURLU_APPENDQUERY | CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
853   {"https://example.com/",
854    "host=\"\",",
855    "https://example.com/",
856    0, CURLU_URLENCODE, CURLUE_OK, CURLUE_BAD_HOSTNAME},
857   {"https://example.com/",
858    "host=\"\",",
859    "https://example.com/",
860    0, 0, CURLUE_OK, CURLUE_BAD_HOSTNAME},
861   {"https://example.com",
862    "path=get,",
863    "https://example.com/get",
864    0, 0, CURLUE_OK, CURLUE_OK},
865   {"https://example.com/",
866    "scheme=ftp+-.123,",
867    "ftp+-.123://example.com/",
868    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
869   {"https://example.com/",
870    "scheme=1234,",
871    "https://example.com/",
872    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
873   {"https://example.com/",
874    "scheme=1http,",
875    "https://example.com/",
876    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
877   {"https://example.com/",
878    "scheme=-ftp,",
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    "host=example.com%2fmoo,",
891    "",
892    0, /* get */
893    0, /* set */
894    CURLUE_OK, CURLUE_BAD_HOSTNAME},
895   {"https://example.com/",
896    "host=http://fake,",
897    "",
898    0, /* get */
899    0, /* set */
900    CURLUE_OK, CURLUE_BAD_HOSTNAME},
901   {"https://example.com/",
902    "host=test%,",
903    "",
904    0, /* get */
905    0, /* set */
906    CURLUE_OK, CURLUE_BAD_HOSTNAME},
907   {"https://example.com/",
908    "host=te st,",
909    "",
910    0, /* get */
911    0, /* set */
912    CURLUE_OK, CURLUE_BAD_HOSTNAME},
913   {"https://example.com/",
914    "host=0xff,", /* '++' there's no automatic URL decode when setting this
915                   part */
916    "https://0xff/",
917    0, /* get */
918    0, /* set */
919    CURLUE_OK, CURLUE_OK},
920 
921   {"https://example.com/",
922    "query=Al2cO3tDkcDZ3EWE5Lh+LX8TPHs,", /* contains '+' */
923    "https://example.com/?Al2cO3tDkcDZ3EWE5Lh%2bLX8TPHs",
924    CURLU_URLDECODE, /* decode on get */
925    CURLU_URLENCODE, /* encode on set */
926    CURLUE_OK, CURLUE_OK},
927 
928   {"https://example.com/",
929    /* Set a bad scheme *including* :// */
930    "scheme=https://,",
931    "https://example.com/",
932    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
933   {"https://example.com/",
934    /* Set a 41 bytes scheme. That's too long so the old scheme remains set. */
935    "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbc,",
936    "https://example.com/",
937    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_BAD_SCHEME},
938   {"https://example.com/",
939    /* set a 40 bytes scheme */
940    "scheme=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,",
941    "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb://example.com/",
942    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
943   {"https://[::1%25fake]:1234/",
944    "zoneid=NULL,",
945    "https://[::1]:1234/",
946    0, 0, CURLUE_OK, CURLUE_OK},
947   {"https://host:1234/",
948    "port=NULL,",
949    "https://host/",
950    0, 0, CURLUE_OK, CURLUE_OK},
951   {"https://host:1234/",
952    "port=\"\",",
953    "https://host:1234/",
954    0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
955   {"https://host:1234/",
956    "port=56 78,",
957    "https://host:1234/",
958    0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
959   {"https://host:1234/",
960    "port=0,",
961    "https://host:0/",
962    0, 0, CURLUE_OK, CURLUE_OK},
963   {"https://host:1234/",
964    "port=65535,",
965    "https://host:65535/",
966    0, 0, CURLUE_OK, CURLUE_OK},
967   {"https://host:1234/",
968    "port=65536,",
969    "https://host:1234/",
970    0, 0, CURLUE_OK, CURLUE_BAD_PORT_NUMBER},
971   {"https://host/",
972    "path=%4A%4B%4C,",
973    "https://host/%4a%4b%4c",
974    0, 0, CURLUE_OK, CURLUE_OK},
975   {"https://host/mooo?q#f",
976    "path=NULL,query=NULL,fragment=NULL,",
977    "https://host/",
978    0, 0, CURLUE_OK, CURLUE_OK},
979   {"https://user:secret@host/",
980    "user=NULL,password=NULL,",
981    "https://host/",
982    0, 0, CURLUE_OK, CURLUE_OK},
983   {NULL,
984    "scheme=https,user=   @:,host=foobar,",
985    "https://%20%20%20%40%3a@foobar/",
986    0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
987   /* Setting a host name with spaces is not OK: */
988   {NULL,
989    "scheme=https,host=  ,path= ,user= ,password= ,query= ,fragment= ,",
990    "[nothing]",
991    0, CURLU_URLENCODE, CURLUE_OK, CURLUE_BAD_HOSTNAME},
992   {NULL,
993    "scheme=https,host=foobar,path=/this /path /is /here,",
994    "https://foobar/this%20/path%20/is%20/here",
995    0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
996   {NULL,
997    "scheme=https,host=foobar,path=\xc3\xa4\xc3\xb6\xc3\xbc,",
998    "https://foobar/%c3%a4%c3%b6%c3%bc",
999    0, CURLU_URLENCODE, CURLUE_OK, CURLUE_OK},
1000   {"imap://user:secret;opt@host/",
1001    "options=updated,scheme=imaps,password=p4ssw0rd,",
1002    "imaps://user:p4ssw0rd;updated@host/",
1003    0, 0, CURLUE_NO_HOST, CURLUE_OK},
1004   {"imap://user:secret;optit@host/",
1005    "scheme=https,",
1006    "https://user:secret@host/",
1007    0, 0, CURLUE_NO_HOST, CURLUE_OK},
1008   {"file:///file#anchor",
1009    "scheme=https,host=example,",
1010    "https://example/file#anchor",
1011    0, 0, CURLUE_NO_HOST, CURLUE_OK},
1012   {NULL, /* start fresh! */
1013    "scheme=file,host=127.0.0.1,path=/no,user=anonymous,",
1014    "file:///no",
1015    0, 0, CURLUE_OK, CURLUE_OK},
1016   {NULL, /* start fresh! */
1017    "scheme=ftp,host=127.0.0.1,path=/no,user=anonymous,",
1018    "ftp://anonymous@127.0.0.1/no",
1019    0, 0, CURLUE_OK, CURLUE_OK},
1020   {NULL, /* start fresh! */
1021    "scheme=https,host=example.com,",
1022    "https://example.com/",
1023    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
1024   {"http://user:foo@example.com/path?query#frag",
1025    "fragment=changed,",
1026    "http://user:foo@example.com/path?query#changed",
1027    0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK, CURLUE_OK},
1028   {"http://example.com/",
1029    "scheme=foo,", /* not accepted */
1030    "http://example.com/",
1031    0, 0, CURLUE_OK, CURLUE_UNSUPPORTED_SCHEME},
1032   {"http://example.com/",
1033    "scheme=https,path=/hello,fragment=snippet,",
1034    "https://example.com/hello#snippet",
1035    0, 0, CURLUE_OK, CURLUE_OK},
1036   {"http://example.com:80",
1037    "user=foo,port=1922,",
1038    "http://foo@example.com:1922/",
1039    0, 0, CURLUE_OK, CURLUE_OK},
1040   {"http://example.com:80",
1041    "user=foo,password=bar,",
1042    "http://foo:bar@example.com:80/",
1043    0, 0, CURLUE_OK, CURLUE_OK},
1044   {"http://example.com:80",
1045    "user=foo,",
1046    "http://foo@example.com:80/",
1047    0, 0, CURLUE_OK, CURLUE_OK},
1048   {"http://example.com",
1049    "host=www.example.com,",
1050    "http://www.example.com/",
1051    0, 0, CURLUE_OK, CURLUE_OK},
1052   {"http://example.com:80",
1053    "scheme=ftp,",
1054    "ftp://example.com:80/",
1055    0, 0, CURLUE_OK, CURLUE_OK},
1056   {"custom-scheme://host",
1057    "host=\"\",",
1058    "custom-scheme://host/",
1059    CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK,
1060    CURLUE_BAD_HOSTNAME},
1061   {"custom-scheme://host",
1062    "host=\"\",",
1063    "custom-scheme:///",
1064    CURLU_NON_SUPPORT_SCHEME, CURLU_NON_SUPPORT_SCHEME | CURLU_NO_AUTHORITY,
1065    CURLUE_OK, CURLUE_OK},
1066 
1067   {NULL, NULL, NULL, 0, 0, CURLUE_OK, CURLUE_OK}
1068 };
1069 
part2id(char * part)1070 static CURLUPart part2id(char *part)
1071 {
1072   if(!strcmp("url", part))
1073     return CURLUPART_URL;
1074   if(!strcmp("scheme", part))
1075     return CURLUPART_SCHEME;
1076   if(!strcmp("user", part))
1077     return CURLUPART_USER;
1078   if(!strcmp("password", part))
1079     return CURLUPART_PASSWORD;
1080   if(!strcmp("options", part))
1081     return CURLUPART_OPTIONS;
1082   if(!strcmp("host", part))
1083     return CURLUPART_HOST;
1084   if(!strcmp("port", part))
1085     return CURLUPART_PORT;
1086   if(!strcmp("path", part))
1087     return CURLUPART_PATH;
1088   if(!strcmp("query", part))
1089     return CURLUPART_QUERY;
1090   if(!strcmp("fragment", part))
1091     return CURLUPART_FRAGMENT;
1092   if(!strcmp("zoneid", part))
1093     return CURLUPART_ZONEID;
1094   return (CURLUPart)9999; /* bad input => bad output */
1095 }
1096 
updateurl(CURLU * u,const char * cmd,unsigned int setflags)1097 static CURLUcode updateurl(CURLU *u, const char *cmd, unsigned int setflags)
1098 {
1099   const char *p = cmd;
1100   CURLUcode uc;
1101 
1102   /* make sure the last command ends with a comma too! */
1103   while(p) {
1104     char *e = strchr(p, ',');
1105     if(e) {
1106       size_t n = (size_t)(e - p);
1107       char buf[80];
1108       char part[80];
1109       char value[80];
1110 
1111       memset(part, 0, sizeof(part)); /* Avoid valgrind false positive. */
1112       memset(value, 0, sizeof(value)); /* Avoid valgrind false positive. */
1113       memcpy(buf, p, n);
1114       buf[n] = 0;
1115       if(2 == sscanf(buf, "%79[^=]=%79[^,]", part, value)) {
1116         CURLUPart what = part2id(part);
1117 #if 0
1118         /* for debugging this */
1119         fprintf(stderr, "%s = \"%s\" [%d]\n", part, value, (int)what);
1120 #endif
1121         if(what > CURLUPART_ZONEID)
1122           fprintf(stderr, "UNKNOWN part '%s'\n", part);
1123 
1124         if(!strcmp("NULL", value))
1125           uc = curl_url_set(u, what, NULL, setflags);
1126         else if(!strcmp("\"\"", value))
1127           uc = curl_url_set(u, what, "", setflags);
1128         else
1129           uc = curl_url_set(u, what, value, setflags);
1130         if(uc)
1131           return uc;
1132       }
1133       p = e + 1;
1134       continue;
1135     }
1136     break;
1137   }
1138   return CURLUE_OK;
1139 }
1140 
1141 static const struct redircase set_url_list[] = {
1142   {"http://example.org/",
1143    "../path/././../../moo",
1144    "http://example.org/moo",
1145    0, 0, CURLUE_OK},
1146   {"http://example.org/",
1147    "//example.org/../path/../../",
1148    "http://example.org/",
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/foo/bar",
1155    ":23",
1156    "http://example.org/foo/:23",
1157    0, 0, CURLUE_OK},
1158   {"http://example.org/foo/bar",
1159    "\\x",
1160    "http://example.org/foo/\\x",
1161    /* WHATWG disagrees */
1162    0, 0, CURLUE_OK},
1163   {"http://example.org/foo/bar",
1164    "#/",
1165    "http://example.org/foo/bar#/",
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    /* This happens because the parser removes empty fragments */
1179    0, 0, CURLUE_OK},
1180   {"http://example.org/foo/bar",
1181    "?",
1182    "http://example.org/foo/bar",
1183    /* This happens because the parser removes empty queries */
1184    0, 0, CURLUE_OK},
1185   {"http://example.org/foo/bar",
1186    "?#",
1187    "http://example.org/foo/bar",
1188    /* This happens because the parser removes empty queries and fragments */
1189    0, 0, CURLUE_OK},
1190   {"http://example.com/please/../gimme/%TESTNUMBER?foobar#hello",
1191    "http://example.net/there/it/is/../../tes t case=/%TESTNUMBER0002? yes no",
1192    "http://example.net/there/tes%20t%20case=/%TESTNUMBER0002?+yes+no",
1193    0, CURLU_URLENCODE|CURLU_ALLOW_SPACE, CURLUE_OK},
1194   {"http://local.test?redirect=http://local.test:80?-321",
1195    "http://local.test:80?-123",
1196    "http://local.test:80/?-123",
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, 0, CURLUE_OK},
1202   {"http://example.org/static/favicon/wikipedia.ico",
1203    "//fake.example.com/licenses/by-sa/3.0/",
1204    "http://fake.example.com/licenses/by-sa/3.0/",
1205    0, 0, CURLUE_OK},
1206   {"https://example.org/static/favicon/wikipedia.ico",
1207    "//fake.example.com/licenses/by-sa/3.0/",
1208    "https://fake.example.com/licenses/by-sa/3.0/",
1209    0, 0, CURLUE_OK},
1210   {"file://localhost/path?query#frag",
1211    "foo#another",
1212    "file:///foo#another",
1213    0, 0, CURLUE_OK},
1214   {"http://example.com/path?query#frag",
1215    "https://two.example.com/bradnew",
1216    "https://two.example.com/bradnew",
1217    0, 0, CURLUE_OK},
1218   {"http://example.com/path?query#frag",
1219    "../../newpage#foo",
1220    "http://example.com/newpage#foo",
1221    0, 0, CURLUE_OK},
1222   {"http://user:foo@example.com/path?query#frag",
1223    "../../newpage",
1224    "http://user:foo@example.com/newpage",
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    "http://?hi",
1232    "http:///?hi",
1233    0, CURLU_NO_AUTHORITY, CURLUE_OK},
1234   {NULL, NULL, NULL, 0, 0, CURLUE_OK}
1235 };
1236 
set_url(void)1237 static int set_url(void)
1238 {
1239   int i;
1240   int error = 0;
1241 
1242   for(i = 0; set_url_list[i].in && !error; i++) {
1243     CURLUcode rc;
1244     CURLU *urlp = curl_url();
1245     if(!urlp)
1246       break;
1247     rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].in,
1248                       set_url_list[i].urlflags);
1249     if(!rc) {
1250       rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].set,
1251                         set_url_list[i].setflags);
1252       if(rc) {
1253         fprintf(stderr, "%s:%d Set URL %s returned %d (%s)\n",
1254                 __FILE__, __LINE__, set_url_list[i].set,
1255                 (int)rc, curl_url_strerror(rc));
1256         error++;
1257       }
1258       else {
1259         char *url = NULL;
1260         rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
1261         if(rc) {
1262           fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
1263                   __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1264           error++;
1265         }
1266         else {
1267           if(checkurl(set_url_list[i].in, url, set_url_list[i].out)) {
1268             error++;
1269           }
1270         }
1271         curl_free(url);
1272       }
1273     }
1274     else if(rc != set_url_list[i].ucode) {
1275       fprintf(stderr, "Set URL\nin: %s\nreturned %d (expected %d)\n",
1276               set_url_list[i].in, (int)rc, set_url_list[i].ucode);
1277       error++;
1278     }
1279     curl_url_cleanup(urlp);
1280   }
1281   return error;
1282 }
1283 
1284 /* 1. Set a URL
1285    2. Set one or more parts
1286    3. Extract and compare all parts - not the URL
1287 */
setget_parts(void)1288 static int setget_parts(void)
1289 {
1290   int i;
1291   int error = 0;
1292 
1293   for(i = 0; setget_parts_list[i].set && !error; i++) {
1294     CURLUcode rc;
1295     CURLU *urlp = curl_url();
1296     if(!urlp) {
1297       error++;
1298       break;
1299     }
1300     if(setget_parts_list[i].in)
1301       rc = curl_url_set(urlp, CURLUPART_URL, setget_parts_list[i].in,
1302                         setget_parts_list[i].urlflags);
1303     else
1304       rc = CURLUE_OK;
1305     if(!rc) {
1306       char *url = NULL;
1307       CURLUcode uc = updateurl(urlp, setget_parts_list[i].set,
1308                                setget_parts_list[i].setflags);
1309 
1310       if(uc != setget_parts_list[i].pcode) {
1311         fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n",
1312                 setget_parts_list[i].set, (int)uc, setget_parts_list[i].pcode);
1313         error++;
1314       }
1315       if(!uc) {
1316         if(checkparts(urlp, setget_parts_list[i].set, setget_parts_list[i].out,
1317                       setget_parts_list[i].getflags))
1318           error++;        /* add */
1319       }
1320       curl_free(url);
1321     }
1322     else if(rc != CURLUE_OK) {
1323       fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
1324               setget_parts_list[i].in, (int)rc, 0);
1325       error++;
1326     }
1327     curl_url_cleanup(urlp);
1328   }
1329   return error;
1330 }
1331 
set_parts(void)1332 static int set_parts(void)
1333 {
1334   int i;
1335   int error = 0;
1336 
1337   for(i = 0; set_parts_list[i].set && !error; i++) {
1338     CURLUcode rc;
1339     CURLU *urlp = curl_url();
1340     if(!urlp) {
1341       error++;
1342       break;
1343     }
1344     if(set_parts_list[i].in)
1345       rc = curl_url_set(urlp, CURLUPART_URL, set_parts_list[i].in,
1346                         set_parts_list[i].urlflags);
1347     else
1348       rc = CURLUE_OK;
1349     if(!rc) {
1350       char *url = NULL;
1351       CURLUcode uc = updateurl(urlp, set_parts_list[i].set,
1352                                set_parts_list[i].setflags);
1353 
1354       if(uc != set_parts_list[i].pcode) {
1355         fprintf(stderr, "updateurl\nin: %s\nreturned %d (expected %d)\n",
1356                 set_parts_list[i].set, (int)uc, set_parts_list[i].pcode);
1357         error++;
1358       }
1359       if(!uc) {
1360         /* only do this if it worked */
1361         rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
1362 
1363         if(rc) {
1364           fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
1365                   __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1366           error++;
1367         }
1368         else if(checkurl(set_parts_list[i].in, url, set_parts_list[i].out)) {
1369           error++;
1370         }
1371       }
1372       curl_free(url);
1373     }
1374     else if(rc != set_parts_list[i].ucode) {
1375       fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
1376               set_parts_list[i].in, (int)rc, set_parts_list[i].ucode);
1377       error++;
1378     }
1379     curl_url_cleanup(urlp);
1380   }
1381   return error;
1382 }
1383 
get_url(void)1384 static int get_url(void)
1385 {
1386   int i;
1387   int error = 0;
1388   for(i = 0; get_url_list[i].in && !error; i++) {
1389     CURLUcode rc;
1390     CURLU *urlp = curl_url();
1391     if(!urlp) {
1392       error++;
1393       break;
1394     }
1395     rc = curl_url_set(urlp, CURLUPART_URL, get_url_list[i].in,
1396                       get_url_list[i].urlflags);
1397     if(!rc) {
1398       char *url = NULL;
1399       rc = curl_url_get(urlp, CURLUPART_URL, &url, get_url_list[i].getflags);
1400 
1401       if(rc) {
1402         fprintf(stderr, "%s:%d returned %d (%s). URL: '%s'\n",
1403                 __FILE__, __LINE__, (int)rc, curl_url_strerror(rc),
1404                 get_url_list[i].in);
1405         error++;
1406       }
1407       else {
1408         if(checkurl(get_url_list[i].in, url, get_url_list[i].out)) {
1409           error++;
1410         }
1411       }
1412       curl_free(url);
1413     }
1414     if(rc != get_url_list[i].ucode) {
1415       fprintf(stderr, "Get URL\nin: %s\nreturned %d (expected %d)\n",
1416               get_url_list[i].in, (int)rc, get_url_list[i].ucode);
1417       error++;
1418     }
1419     curl_url_cleanup(urlp);
1420   }
1421   return error;
1422 }
1423 
get_parts(void)1424 static int get_parts(void)
1425 {
1426   int i;
1427   int error = 0;
1428   for(i = 0; get_parts_list[i].in && !error; i++) {
1429     CURLUcode rc;
1430     CURLU *urlp = curl_url();
1431     if(!urlp) {
1432       error++;
1433       break;
1434     }
1435     rc = curl_url_set(urlp, CURLUPART_URL,
1436                       get_parts_list[i].in,
1437                       get_parts_list[i].urlflags);
1438     if(rc != get_parts_list[i].ucode) {
1439       fprintf(stderr, "Get parts\nin: %s\nreturned %d (expected %d)\n",
1440               get_parts_list[i].in, (int)rc, get_parts_list[i].ucode);
1441       error++;
1442     }
1443     else if(get_parts_list[i].ucode) {
1444       /* the expected error happened */
1445     }
1446     else if(checkparts(urlp, get_parts_list[i].in, get_parts_list[i].out,
1447                        get_parts_list[i].getflags))
1448       error++;
1449     curl_url_cleanup(urlp);
1450   }
1451   return error;
1452 }
1453 
1454 static const struct querycase append_list[] = {
1455   {"HTTP://test/?s", "name=joe\x02", "http://test/?s&name=joe%02",
1456    0, CURLU_URLENCODE, CURLUE_OK},
1457   {"HTTP://test/?size=2#f", "name=joe=", "http://test/?size=2&name=joe%3d#f",
1458    0, CURLU_URLENCODE, CURLUE_OK},
1459   {"HTTP://test/?size=2#f", "name=joe doe",
1460    "http://test/?size=2&name=joe+doe#f",
1461    0, CURLU_URLENCODE, CURLUE_OK},
1462   {"HTTP://test/", "name=joe", "http://test/?name=joe", 0, 0, CURLUE_OK},
1463   {"HTTP://test/?size=2", "name=joe", "http://test/?size=2&name=joe",
1464    0, 0, CURLUE_OK},
1465   {"HTTP://test/?size=2&", "name=joe", "http://test/?size=2&name=joe",
1466    0, 0, CURLUE_OK},
1467   {"HTTP://test/?size=2#f", "name=joe", "http://test/?size=2&name=joe#f",
1468    0, 0, CURLUE_OK},
1469   {NULL, NULL, NULL, 0, 0, CURLUE_OK}
1470 };
1471 
append(void)1472 static int append(void)
1473 {
1474   int i;
1475   int error = 0;
1476   for(i = 0; append_list[i].in && !error; i++) {
1477     CURLUcode rc;
1478     CURLU *urlp = curl_url();
1479     if(!urlp) {
1480       error++;
1481       break;
1482     }
1483     rc = curl_url_set(urlp, CURLUPART_URL,
1484                       append_list[i].in,
1485                       append_list[i].urlflags);
1486     if(rc)
1487       error++;
1488     else
1489       rc = curl_url_set(urlp, CURLUPART_QUERY,
1490                         append_list[i].q,
1491                         append_list[i].qflags | CURLU_APPENDQUERY);
1492     if(error)
1493       ;
1494     else if(rc != append_list[i].ucode) {
1495       fprintf(stderr, "Append\nin: %s\nreturned %d (expected %d)\n",
1496               append_list[i].in, (int)rc, append_list[i].ucode);
1497       error++;
1498     }
1499     else if(append_list[i].ucode) {
1500       /* the expected error happened */
1501     }
1502     else {
1503       char *url;
1504       rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
1505       if(rc) {
1506         fprintf(stderr, "%s:%d Get URL returned %d (%s)\n",
1507                 __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1508         error++;
1509       }
1510       else {
1511         if(checkurl(append_list[i].in, url, append_list[i].out)) {
1512           error++;
1513         }
1514         curl_free(url);
1515       }
1516     }
1517     curl_url_cleanup(urlp);
1518   }
1519   return error;
1520 }
1521 
scopeid(void)1522 static int scopeid(void)
1523 {
1524   CURLU *u = curl_url();
1525   int error = 0;
1526   CURLUcode rc;
1527   char *url;
1528 
1529   rc = curl_url_set(u, CURLUPART_URL,
1530                     "https://[fe80::20c:29ff:fe9c:409b%25eth0]/hello.html", 0);
1531   if(rc != CURLUE_OK) {
1532     fprintf(stderr, "%s:%d curl_url_set returned %d (%s)\n",
1533             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1534     error++;
1535   }
1536 
1537   rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
1538   if(rc != CURLUE_OK) {
1539     fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d (%s)\n",
1540             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1541     error++;
1542   }
1543   else {
1544     curl_free(url);
1545   }
1546 
1547   rc = curl_url_set(u, CURLUPART_HOST, "[::1]", 0);
1548   if(rc != CURLUE_OK) {
1549     fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
1550             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1551     error++;
1552   }
1553 
1554   rc = curl_url_get(u, CURLUPART_URL, &url, 0);
1555   if(rc != CURLUE_OK) {
1556     fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
1557             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1558     error++;
1559   }
1560   else {
1561     curl_free(url);
1562   }
1563 
1564   rc = curl_url_set(u, CURLUPART_HOST, "example.com", 0);
1565   if(rc != CURLUE_OK) {
1566     fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
1567             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1568     error++;
1569   }
1570 
1571   rc = curl_url_get(u, CURLUPART_URL, &url, 0);
1572   if(rc != CURLUE_OK) {
1573     fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
1574             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1575     error++;
1576   }
1577   else {
1578     curl_free(url);
1579   }
1580 
1581   rc = curl_url_set(u, CURLUPART_HOST,
1582                     "[fe80::20c:29ff:fe9c:409b%25eth0]", 0);
1583   if(rc != CURLUE_OK) {
1584     fprintf(stderr, "%s:%d curl_url_set CURLUPART_HOST returned %d (%s)\n",
1585             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1586     error++;
1587   }
1588 
1589   rc = curl_url_get(u, CURLUPART_URL, &url, 0);
1590   if(rc != CURLUE_OK) {
1591     fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
1592             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1593     error++;
1594   }
1595   else {
1596     curl_free(url);
1597   }
1598 
1599   rc = curl_url_get(u, CURLUPART_HOST, &url, 0);
1600   if(rc != CURLUE_OK) {
1601     fprintf(stderr, "%s:%d curl_url_get CURLUPART_HOST returned %d (%s)\n",
1602             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1603     error++;
1604   }
1605   else {
1606     curl_free(url);
1607   }
1608 
1609   rc = curl_url_get(u, CURLUPART_ZONEID, &url, 0);
1610   if(rc != CURLUE_OK) {
1611     fprintf(stderr, "%s:%d curl_url_get CURLUPART_ZONEID returned %d (%s)\n",
1612             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1613     error++;
1614   }
1615   else {
1616     curl_free(url);
1617   }
1618 
1619   rc = curl_url_set(u, CURLUPART_ZONEID, "clown", 0);
1620   if(rc != CURLUE_OK) {
1621     fprintf(stderr, "%s:%d curl_url_set CURLUPART_ZONEID returned %d (%s)\n",
1622             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1623     error++;
1624   }
1625 
1626   rc = curl_url_get(u, CURLUPART_URL, &url, 0);
1627   if(rc != CURLUE_OK) {
1628     fprintf(stderr, "%s:%d curl_url_get CURLUPART_URL returned %d (%s)\n",
1629             __FILE__, __LINE__, (int)rc, curl_url_strerror(rc));
1630     error++;
1631   }
1632   else {
1633     curl_free(url);
1634   }
1635 
1636   curl_url_cleanup(u);
1637 
1638   return error;
1639 }
1640 
get_nothing(void)1641 static int get_nothing(void)
1642 {
1643   CURLU *u = curl_url();
1644   if(u) {
1645     char *p;
1646     CURLUcode rc;
1647 
1648     rc = curl_url_get(u, CURLUPART_SCHEME, &p, 0);
1649     if(rc != CURLUE_NO_SCHEME)
1650       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1651 
1652     rc = curl_url_get(u, CURLUPART_HOST, &p, 0);
1653     if(rc != CURLUE_NO_HOST)
1654       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1655 
1656     rc = curl_url_get(u, CURLUPART_USER, &p, 0);
1657     if(rc != CURLUE_NO_USER)
1658       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1659 
1660     rc = curl_url_get(u, CURLUPART_PASSWORD, &p, 0);
1661     if(rc != CURLUE_NO_PASSWORD)
1662       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1663 
1664     rc = curl_url_get(u, CURLUPART_OPTIONS, &p, 0);
1665     if(rc != CURLUE_NO_OPTIONS)
1666       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1667 
1668     rc = curl_url_get(u, CURLUPART_PATH, &p, 0);
1669     if(rc != CURLUE_OK)
1670       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1671     else
1672       curl_free(p);
1673 
1674     rc = curl_url_get(u, CURLUPART_QUERY, &p, 0);
1675     if(rc != CURLUE_NO_QUERY)
1676       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1677 
1678     rc = curl_url_get(u, CURLUPART_FRAGMENT, &p, 0);
1679     if(rc != CURLUE_NO_FRAGMENT)
1680       fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1681 
1682     rc = curl_url_get(u, CURLUPART_ZONEID, &p, 0);
1683     if(rc != CURLUE_NO_ZONEID)
1684       fprintf(stderr, "unexpected return code %u on line %u\n", (int)rc,
1685               __LINE__);
1686 
1687     curl_url_cleanup(u);
1688   }
1689   return 0;
1690 }
1691 
1692 static const struct clearurlcase clear_url_list[] ={
1693   {CURLUPART_SCHEME, "http", NULL, CURLUE_NO_SCHEME},
1694   {CURLUPART_USER, "user", NULL, CURLUE_NO_USER},
1695   {CURLUPART_PASSWORD, "password", NULL, CURLUE_NO_PASSWORD},
1696   {CURLUPART_OPTIONS, "options", NULL, CURLUE_NO_OPTIONS},
1697   {CURLUPART_HOST, "host", NULL, CURLUE_NO_HOST},
1698   {CURLUPART_ZONEID, "eth0", NULL, CURLUE_NO_ZONEID},
1699   {CURLUPART_PORT, "1234", NULL, CURLUE_NO_PORT},
1700   {CURLUPART_PATH, "/hello", "/", CURLUE_OK},
1701   {CURLUPART_QUERY, "a=b", NULL, CURLUE_NO_QUERY},
1702   {CURLUPART_FRAGMENT, "anchor", NULL, CURLUE_NO_FRAGMENT},
1703   {CURLUPART_URL, NULL, NULL, CURLUE_OK},
1704 };
1705 
clear_url(void)1706 static int clear_url(void)
1707 {
1708   CURLU *u = curl_url();
1709   int i, error = 0;
1710   if(u) {
1711     char *p = NULL;
1712     CURLUcode rc;
1713 
1714     for(i = 0; clear_url_list[i].in && !error; i++) {
1715       rc = curl_url_set(u, clear_url_list[i].part, clear_url_list[i].in, 0);
1716       if(rc != CURLUE_OK)
1717         fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1718 
1719       rc = curl_url_set(u, CURLUPART_URL, NULL, 0);
1720       if(rc != CURLUE_OK)
1721         fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1722 
1723       rc = curl_url_get(u, clear_url_list[i].part, &p, 0);
1724       if(rc != clear_url_list[i].ucode || (clear_url_list[i].out &&
1725          0 != strcmp(p, clear_url_list[i].out))) {
1726 
1727         fprintf(stderr, "unexpected return code line %u\n", __LINE__);
1728         error++;
1729       }
1730       if(rc == CURLUE_OK)
1731         curl_free(p);
1732     }
1733   }
1734 
1735   curl_url_cleanup(u);
1736 
1737   return error;
1738 }
1739 
1740 static char total[128000];
1741 static char bigpart[120000];
1742 
1743 /*
1744  * verify ridiculous URL part sizes
1745  */
huge(void)1746 static int huge(void)
1747 {
1748   const char *smallpart = "c";
1749   int i;
1750   CURLU *urlp = curl_url();
1751   CURLUcode rc;
1752   CURLUPart part[]= {
1753     CURLUPART_SCHEME,
1754     CURLUPART_USER,
1755     CURLUPART_PASSWORD,
1756     CURLUPART_HOST,
1757     CURLUPART_PATH,
1758     CURLUPART_QUERY,
1759     CURLUPART_FRAGMENT
1760   };
1761   int error = 0;
1762   if(!urlp)
1763     return 1;
1764   bigpart[0] = '/'; /* for the path */
1765   memset(&bigpart[1], 'a', sizeof(bigpart) - 2);
1766   bigpart[sizeof(bigpart) - 1] = 0;
1767 
1768   for(i = 0; i <  7; i++) {
1769     char *partp;
1770     msnprintf(total, sizeof(total),
1771               "%s://%s:%s@%s/%s?%s#%s",
1772               (i == 0) ? &bigpart[1] : smallpart,
1773               (i == 1) ? &bigpart[1] : smallpart,
1774               (i == 2) ? &bigpart[1] : smallpart,
1775               (i == 3) ? &bigpart[1] : smallpart,
1776               (i == 4) ? &bigpart[1] : smallpart,
1777               (i == 5) ? &bigpart[1] : smallpart,
1778               (i == 6) ? &bigpart[1] : smallpart);
1779     rc = curl_url_set(urlp, CURLUPART_URL, total, CURLU_NON_SUPPORT_SCHEME);
1780     if((!i && (rc != CURLUE_BAD_SCHEME)) ||
1781        (i && rc)) {
1782       printf("URL %u: failed to parse [%s]\n", i, total);
1783       error++;
1784     }
1785 
1786     /* only extract if the parse worked */
1787     if(!rc) {
1788       curl_url_get(urlp, part[i], &partp, 0);
1789       if(!partp || strcmp(partp, &bigpart[1 - (i == 4)])) {
1790         printf("URL %u part %u: failure\n", i, part[i]);
1791         error++;
1792       }
1793       curl_free(partp);
1794     }
1795   }
1796   curl_url_cleanup(urlp);
1797   return error;
1798 }
1799 
urldup(void)1800 static int urldup(void)
1801 {
1802   const char *url[] = {
1803     "http://"
1804     "user:pwd@"
1805     "[2a04:4e42:e00::347%25eth0]"
1806     ":80"
1807     "/path"
1808     "?query"
1809     "#fraggie",
1810     "https://example.com",
1811     "https://user@example.com",
1812     "https://user.pwd@example.com",
1813     "https://user.pwd@example.com:1234",
1814     "https://example.com:1234",
1815     "example.com:1234",
1816     "https://user.pwd@example.com:1234/path?query#frag",
1817     NULL
1818   };
1819   CURLU *copy = NULL;
1820   char *h_str = NULL, *copy_str = NULL;
1821   CURLU *h = curl_url();
1822   int i;
1823 
1824   if(!h)
1825     goto err;
1826 
1827   for(i = 0; url[i]; i++) {
1828     CURLUcode rc = curl_url_set(h, CURLUPART_URL, url[i],
1829                                 CURLU_GUESS_SCHEME);
1830     if(rc)
1831       goto err;
1832     copy = curl_url_dup(h);
1833 
1834     rc = curl_url_get(h, CURLUPART_URL, &h_str, 0);
1835     if(rc)
1836       goto err;
1837 
1838     rc = curl_url_get(copy, CURLUPART_URL, &copy_str, 0);
1839     if(rc)
1840       goto err;
1841 
1842     if(strcmp(h_str, copy_str)) {
1843       printf("Original:  %s\nParsed:    %s\nCopy:      %s\n",
1844              url[i], h_str, copy_str);
1845       goto err;
1846     }
1847     curl_free(copy_str);
1848     curl_free(h_str);
1849     curl_url_cleanup(copy);
1850     copy_str = NULL;
1851     h_str = NULL;
1852     copy = NULL;
1853   }
1854   curl_url_cleanup(h);
1855   return 0;
1856 err:
1857   curl_free(copy_str);
1858   curl_free(h_str);
1859   curl_url_cleanup(copy);
1860   curl_url_cleanup(h);
1861   return 1;
1862 }
1863 
test(char * URL)1864 CURLcode test(char *URL)
1865 {
1866   (void)URL; /* not used */
1867 
1868   if(urldup())
1869     return (CURLcode)11;
1870 
1871   if(setget_parts())
1872     return (CURLcode)10;
1873 
1874   if(get_url())
1875     return (CURLcode)3;
1876 
1877   if(huge())
1878     return (CURLcode)9;
1879 
1880   if(get_nothing())
1881     return (CURLcode)7;
1882 
1883   if(scopeid())
1884     return (CURLcode)6;
1885 
1886   if(append())
1887     return (CURLcode)5;
1888 
1889   if(set_url())
1890     return (CURLcode)1;
1891 
1892   if(set_parts())
1893     return (CURLcode)2;
1894 
1895   if(get_parts())
1896     return (CURLcode)4;
1897 
1898   if(clear_url())
1899     return (CURLcode)8;
1900 
1901   printf("success\n");
1902   return CURLE_OK;
1903 }
1904