1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
// file : tests/url/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <cassert>
#ifndef __cpp_lib_modules_ts
#include <string>
#include <utility> // move()
#include <iostream>
#include <iterator> // back_inserter
#include <stdexcept> // invalid_argument
#endif
// Other includes.
#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.url;
import butl.utility; // operator<<(ostream, exception)
#else
#include <libbutl/url.mxx>
#include <libbutl/utility.mxx>
#endif
using namespace std;
using namespace butl;
enum class scheme
{
http,
https,
file,
pkcs11
};
namespace butl
{
template <>
struct url_traits<scheme>
{
using string_type = wstring;
using path_type = wstring;
using scheme_type = scheme;
using authority_type = basic_url_authority<string_type>;
static optional<scheme_type>
translate_scheme (const string_type& url,
string_type&& scheme,
optional<authority_type>& authority,
optional<path_type>& path,
optional<string_type>& query,
optional<string_type>& /*fragment*/,
bool& rootless)
{
if (scheme.empty ())
{
// If the URL looks like an absolute filesystem path, then translate it
// to the file URL. If it is not, then return nullopt to fail with a
// proper exception description.
//
wchar_t c;
if ((c = url[0]) == '/' ||
(url.size () > 2 && alpha (c) && url[1] == ':' && url[2] == '/'))
{
path = url;
rootless = false;
return scheme_type::file;
}
return nullopt;
}
scheme_type t;
// Note that we must compare case-insensitive in the real program.
//
if (scheme == L"http")
t = scheme_type::http;
else if (scheme == L"https")
t = scheme_type::https;
else if (scheme == L"file")
t = scheme_type::file;
else if (scheme == L"pkcs11")
t = scheme_type::pkcs11;
else
throw invalid_argument ("unknown scheme");
if (t != scheme_type::pkcs11 && !authority && !path && !query)
throw invalid_argument ("no authority, path or query");
if (path)
{
if (t == scheme_type::pkcs11)
{
if (!rootless || path->find (L'/') != string_type::npos)
throw invalid_argument ("unexpected slash");
}
else if (rootless)
throw invalid_argument ("rootless path");
}
return t;
}
// Translate scheme type back to its string representation.
//
static string_type
translate_scheme (string_type&, /*url*/
const scheme_type& scheme,
const optional<authority_type>& /*authority*/,
const optional<path_type>& /*path*/,
const optional<string_type>& /*query*/,
const optional<string_type>& /*fragment*/,
bool /*rootless*/)
{
switch (scheme)
{
case scheme_type::http: return L"http";
case scheme_type::https: return L"https";
case scheme_type::file: return L"file";
case scheme_type::pkcs11: return L"pkcs11";
}
assert (false); // Can't be here.
return L"";
}
static path_type
translate_path (string_type&& path)
{
// Note that a real pkcs11-supporting URL most likely would keep the
// path URL-encoded as its components can contain binary data. Or, it
// would split the path into components before decoding them.
//
return path_type (basic_url<string_type>::decode (path));
}
static string_type
translate_path (const path_type& path)
{
using url = basic_url<string_type>;
return url::encode (path, [] (wchar_t& c) {return !url::path_char (c);});
}
};
}
// Usages:
//
// argv[0]
// argv[0] [-c|-s|-w] <url>
//
// Perform some basic tests if no URL is provided. Otherwise round-trip the URL
// to STDOUT. URL must contain only ASCII characters. Exit with zero code on
// success. Exit with code one on parsing failure, printing error description
// to STDERR.
//
// -c
// Print the URL components one per line. Print the special '[null]' string
// for an absent components. This is the default option if URL is provided.
//
// -s
// Print stringified url object representation.
//
// -w
// Same as above, but use the custom wstring-based url_traits
// implementation for the basic_url template.
//
int
main (int argc, const char* argv[])
try
{
using butl::nullopt; // Disambiguate with std::nullopt.
using wurl = basic_url<scheme>;
using wurl_authority = wurl::authority_type;
using wurl_host = wurl::host_type;
enum class print_mode
{
str,
wstr,
comp
} mode (print_mode::comp);
int i (1);
for (; i != argc; ++i)
{
string o (argv[i]);
if (o == "-s")
mode = print_mode::str;
else if (o == "-w")
mode = print_mode::wstr;
else if (o == "-c")
mode = print_mode::comp;
else
break; // End of options.
}
if (i == argc)
{
// Test ctors and operators.
//
{
wurl u0;
assert (u0.empty ());
wurl u1 (scheme::http,
wurl_authority {wstring (), wurl_host (L"[123]"), 0},
wstring (L"login"),
wstring (L"q="),
wstring (L"f"));
assert (!u1.empty ());
assert (u1 != u0);
wurl u2 (scheme::http,
wurl_host (L"123", url_host_kind::ipv6),
wstring (L"login"),
wstring (L"q="),
wstring (L"f"));
assert (u2 == u1);
wurl u3 (scheme::http,
wurl_host (L"123", url_host_kind::ipv6),
0,
wstring (L"login"),
wstring (L"q="),
wstring (L"f"));
assert (u3 == u2);
wurl u4 (scheme::http,
L"[123]",
wstring (L"login"),
wstring (L"q="),
wstring (L"f"));
assert (u4 == u3);
wurl u5 (scheme::http,
L"[123]",
0,
wstring (L"login"),
wstring (L"q="),
wstring (L"f"));
assert (u5 == u4);
}
// Test encode and decode.
//
{
const char* s ("ABC +");
string es (url::encode (s));
assert (es == "ABC%20%2B");
string ds (url::decode (es));
assert (ds == s);
}
{
const char* s ("ABC +");
string es (url::encode (s,
[] (char& c) -> bool
{
if (c == ' ')
{
c = '+';
return false;
}
return !url::unreserved (c);
}));
assert (es == "ABC+%2B");
string ds (url::decode (es.c_str (),
[] (char& c)
{
if (c == '+')
c = ' ';
}));
assert (ds == s);
}
{
const wchar_t s[] = L"ABC ";
wstring es;
wurl::encode (s, s + 4,
back_inserter (es),
[] (wchar_t& c) -> bool
{
if (!alnum (c))
return true;
++c;
return false;
});
assert (es == L"BCD%20");
wstring ds (wurl::decode (es,
[] (wchar_t& c)
{
if (alnum (c))
--c;
}));
assert (ds == s);
}
}
else // Round-trip the URL.
{
assert (i + 1 == argc);
const char* ua (argv[i]);
switch (mode)
{
case print_mode::str:
{
cout << (*ua != '\0' ? url (ua) : url ()) << endl;
break;
}
case print_mode::wstr:
{
// Convert ASCII string to wstring.
//
wstring s (ua, ua + strlen (ua));
wcout << (!s.empty () ? wurl (s) : wurl ()) << endl;
break;
}
case print_mode::comp:
{
// Convert ASCII string to wstring.
//
wstring s (ua, ua + strlen (ua));
wurl u;
if (!s.empty ())
u = wurl (s);
if (!u.empty ())
{
wstring s;
wcout << wurl::traits_type::translate_scheme (s,
u.scheme,
nullopt,
nullopt,
nullopt,
nullopt,
false) << endl;
}
else
wcout << L"[null]" << endl;
if (u.authority)
{
const wchar_t* kinds[] = {L"ipv4", L"ipv6", L"name"};
const wurl_authority& a (*u.authority);
wcout << a.user << L'@' << a.host.value << L':' << a.port
<< " " << kinds[static_cast<size_t> (a.host.kind)] << endl;
}
else
wcout << L"[null]" << endl;
wcout << (u.path ? *u.path : L"[null]") << endl
<< (u.query ? *u.query : L"[null]") << endl
<< (u.fragment ? *u.fragment : L"[null]") << endl;
break;
}
}
}
return 0;
}
catch (const invalid_argument& e)
{
cerr << e << endl;
return 1;
}
|