goish 0.14.0

Write Rust using Go idioms — a Go-flavored standard library for Rust
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// url: Go's net/url package — parse and manipulate URLs.
//
//   Go                                  goish
//   ─────────────────────────────────   ──────────────────────────────────
//   u, err := url.Parse(s)              let (u, err) = url::Parse(s);
//   u.Scheme                            u.Scheme
//   u.Host                              u.Host
//   u.Path                              u.Path
//   u.Query()                           u.Query()
//   v := url.Values{}                   let mut v = url::Values::new();
//   v.Set("k", "v")                     v.Set("k", "v");
//   v.Encode()                          v.Encode();
//   url.QueryEscape(s)                  url::QueryEscape(s)
//   url.PathEscape(s)                   url::PathEscape(s)
//
// Implements the common subset. For the full WHATWG URL algorithm, reach
// for a dedicated crate; Go's net/url and this port are both "best effort
// for typical URLs".

use crate::errors::{error, nil, New};
use crate::types::{int, map, slice, string};
use std::collections::HashMap;

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct URL {
    pub Scheme: string,
    pub Opaque: string,
    pub User: Option<Userinfo>,
    pub Host: string,
    pub Path: string,
    pub RawPath: string,
    pub RawQuery: string,
    pub Fragment: string,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Userinfo {
    pub username: string,
    pub password: Option<string>,
}

impl Userinfo {
    pub fn Username(&self) -> string { self.username.clone() }
    pub fn Password(&self) -> (string, bool) {
        match &self.password {
            Some(p) => (p.clone(), true),
            None => (String::new(), false),
        }
    }
    pub fn String(&self) -> string {
        match &self.password {
            Some(p) => format!("{}:{}", QueryEscape(&self.username), QueryEscape(p)),
            None => QueryEscape(&self.username),
        }
    }
}

impl URL {
    pub fn String(&self) -> string {
        let mut out = String::new();
        if !self.Scheme.is_empty() {
            out.push_str(&self.Scheme);
            out.push(':');
        }
        if !self.Opaque.is_empty() {
            out.push_str(&self.Opaque);
        } else {
            if !self.Host.is_empty() || self.User.is_some()
                || self.Scheme == "http" || self.Scheme == "https"
                || self.Scheme == "ws" || self.Scheme == "wss" || self.Scheme == "ftp"
            {
                out.push_str("//");
                if let Some(u) = &self.User {
                    out.push_str(&u.String());
                    out.push('@');
                }
                out.push_str(&self.Host);
            }
            out.push_str(&self.Path);
        }
        if !self.RawQuery.is_empty() {
            out.push('?');
            out.push_str(&self.RawQuery);
        }
        if !self.Fragment.is_empty() {
            out.push('#');
            out.push_str(&self.Fragment);
        }
        out
    }

    pub fn Query(&self) -> Values {
        ParseQuery(&self.RawQuery).0
    }

    pub fn IsAbs(&self) -> bool { !self.Scheme.is_empty() }

    pub fn Hostname(&self) -> string {
        let h = &self.Host;
        if h.starts_with('[') {
            if let Some(end) = h.find(']') { return h[1..end].to_string(); }
        }
        match h.rsplit_once(':') {
            Some((host, _)) => host.to_string(),
            None => h.to_string(),
        }
    }

    /// URL.RequestURI — the path?query portion, suitable for an HTTP request.
    pub fn RequestURI(&self) -> string {
        let mut out = String::new();
        if !self.Opaque.is_empty() {
            out.push_str(&self.Opaque);
        } else {
            if self.Path.is_empty() {
                out.push('/');
            } else {
                out.push_str(&self.Path);
            }
        }
        if !self.RawQuery.is_empty() {
            out.push('?');
            out.push_str(&self.RawQuery);
        }
        out
    }

    /// URL.JoinPath — returns a new URL with the given path components appended.
    pub fn JoinPath(&self, elem: &[impl AsRef<str>]) -> URL {
        let mut joined = self.Path.clone();
        for e in elem {
            let s = e.as_ref();
            if s.is_empty() { continue; }
            if !joined.ends_with('/') && !s.starts_with('/') { joined.push('/'); }
            joined.push_str(s);
        }
        // Normalise ./ and ../ like path.Clean would do for a pure path.
        let cleaned = path_clean(&joined);
        let mut u = self.clone();
        u.Path = cleaned;
        u.RawPath = String::new();
        u
    }

    pub fn Port(&self) -> string {
        let h = &self.Host;
        if h.starts_with('[') {
            if let Some(end) = h.find(']') {
                if h.len() > end + 1 && &h[end + 1..end + 2] == ":" {
                    return h[end + 2..].to_string();
                }
                return String::new();
            }
        }
        match h.rsplit_once(':') {
            Some((_, p)) => p.to_string(),
            None => String::new(),
        }
    }
}

#[allow(non_snake_case)]
pub fn Parse(raw: impl AsRef<str>) -> (URL, error) {
    let s = raw.as_ref();
    let mut u = URL::default();
    let mut rest = s;

    // Fragment.
    if let Some(i) = rest.find('#') {
        u.Fragment = rest[i + 1..].to_string();
        rest = &rest[..i];
    }

    // Query.
    if let Some(i) = rest.find('?') {
        u.RawQuery = rest[i + 1..].to_string();
        rest = &rest[..i];
    }

    // Scheme.
    if let Some(i) = find_scheme_end(rest) {
        u.Scheme = rest[..i].to_ascii_lowercase();
        rest = &rest[i + 1..];
    }

    // Authority (//host...)?
    if rest.starts_with("//") {
        rest = &rest[2..];
        let authority_end = rest.find('/').unwrap_or(rest.len());
        let authority = &rest[..authority_end];
        rest = &rest[authority_end..];
        let (userinfo_opt, host) = match authority.rfind('@') {
            Some(i) => (Some(&authority[..i]), &authority[i + 1..]),
            None => (None, authority),
        };
        if let Some(ui) = userinfo_opt {
            let (user, pwd) = match ui.find(':') {
                Some(i) => {
                    let (u, p) = ui.split_at(i);
                    (QueryUnescape(u).0, Some(QueryUnescape(&p[1..]).0))
                }
                None => (QueryUnescape(ui).0, None),
            };
            u.User = Some(Userinfo { username: user, password: pwd });
        }
        u.Host = host.to_string();
    } else if !u.Scheme.is_empty() && !rest.starts_with('/') {
        // Opaque URL: e.g. "mailto:foo@bar".
        u.Opaque = rest.to_string();
        return (u, nil);
    }

    u.RawPath = rest.to_string();
    let (decoded, err) = PathUnescape(rest);
    if err != nil {
        return (u, New(&format!("parse {:?}: invalid URL escape", s)));
    }
    u.Path = decoded;
    (u, nil)
}

fn find_scheme_end(s: &str) -> Option<usize> {
    let bytes = s.as_bytes();
    if bytes.is_empty() || !bytes[0].is_ascii_alphabetic() { return None; }
    for (i, &b) in bytes.iter().enumerate() {
        if b == b':' { return Some(i); }
        if i == 0 {
            if !b.is_ascii_alphabetic() { return None; }
        } else if !(b.is_ascii_alphanumeric() || b == b'+' || b == b'-' || b == b'.') {
            return None;
        }
    }
    None
}

// ── Values (query string) ─────────────────────────────────────────────

#[derive(Debug, Clone, Default)]
pub struct Values {
    inner: map<string, slice<string>>,
}

impl Values {
    pub fn new() -> Self { Values { inner: HashMap::new() } }

    pub fn Get(&self, key: impl AsRef<str>) -> string {
        self.inner.get(key.as_ref())
            .and_then(|v| v.first())
            .cloned()
            .unwrap_or_default()
    }

    pub fn Set(&mut self, key: impl AsRef<str>, value: impl AsRef<str>) {
        self.inner.insert(key.as_ref().to_string(), vec![value.as_ref().to_string()]);
    }

    pub fn Add(&mut self, key: impl AsRef<str>, value: impl AsRef<str>) {
        self.inner.entry(key.as_ref().to_string()).or_default()
            .push(value.as_ref().to_string());
    }

    pub fn Del(&mut self, key: impl AsRef<str>) {
        self.inner.remove(key.as_ref());
    }

    pub fn Has(&self, key: impl AsRef<str>) -> bool {
        self.inner.contains_key(key.as_ref())
    }

    pub fn Encode(&self) -> string {
        let mut keys: Vec<&String> = self.inner.keys().collect();
        keys.sort();
        let mut out = String::new();
        for k in keys {
            let enc_k = QueryEscape(k);
            for v in &self.inner[k] {
                if !out.is_empty() { out.push('&'); }
                out.push_str(&enc_k);
                out.push('=');
                out.push_str(&QueryEscape(v));
            }
        }
        out
    }

    pub fn Len(&self) -> int { self.inner.len() as int }

    pub fn Values(&self, key: impl AsRef<str>) -> slice<string> {
        self.inner.get(key.as_ref()).cloned().unwrap_or_default()
    }
}

#[allow(non_snake_case)]
pub fn ParseQuery(s: impl AsRef<str>) -> (Values, error) {
    let mut v = Values::new();
    let mut err: error = nil;
    for part in s.as_ref().split('&') {
        if part.is_empty() { continue; }
        let (k, val) = match part.find('=') {
            Some(i) => (&part[..i], &part[i + 1..]),
            None => (part, ""),
        };
        let (key, e1) = QueryUnescape(k);
        let (value, e2) = QueryUnescape(val);
        if e1 != nil { err = e1; }
        if e2 != nil { err = e2; }
        v.Add(key, value);
    }
    (v, err)
}

// ── Escape / unescape ────────────────────────────────────────────────

#[allow(non_snake_case)]
pub fn QueryEscape(s: impl AsRef<str>) -> string {
    escape(s.as_ref(), true)
}

#[allow(non_snake_case)]
pub fn PathEscape(s: impl AsRef<str>) -> string {
    escape(s.as_ref(), false)
}

fn escape(s: &str, is_query: bool) -> string {
    let mut out = String::with_capacity(s.len());
    for &b in s.as_bytes() {
        if should_not_escape(b, is_query) {
            out.push(b as char);
        } else if b == b' ' && is_query {
            out.push('+');
        } else {
            out.push('%');
            out.push(hex_digit(b >> 4));
            out.push(hex_digit(b & 0xf));
        }
    }
    out
}

fn should_not_escape(b: u8, is_query: bool) -> bool {
    if b.is_ascii_alphanumeric() { return true; }
    match b {
        b'-' | b'.' | b'_' | b'~' => true,
        b'$' | b'&' | b'+' | b',' | b'/' | b':' | b';' | b'=' | b'?' | b'@' => !is_query && b != b'?',
        _ => false,
    }
}

fn hex_digit(n: u8) -> char {
    match n {
        0..=9 => (b'0' + n) as char,
        10..=15 => (b'A' + (n - 10)) as char,
        _ => '?',
    }
}

#[allow(non_snake_case)]
pub fn QueryUnescape(s: impl AsRef<str>) -> (string, error) {
    unescape(s.as_ref(), true)
}

#[allow(non_snake_case)]
pub fn PathUnescape(s: impl AsRef<str>) -> (string, error) {
    unescape(s.as_ref(), false)
}

fn unescape(s: &str, is_query: bool) -> (string, error) {
    let mut out = Vec::<u8>::with_capacity(s.len());
    let bytes = s.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if b == b'%' {
            if i + 2 >= bytes.len() {
                return (String::new(), New("invalid URL escape"));
            }
            let hi = hex_val(bytes[i + 1]);
            let lo = hex_val(bytes[i + 2]);
            if hi < 0 || lo < 0 {
                return (String::new(), New("invalid URL escape"));
            }
            out.push(((hi as u8) << 4) | (lo as u8));
            i += 3;
        } else if b == b'+' && is_query {
            out.push(b' ');
            i += 1;
        } else {
            out.push(b);
            i += 1;
        }
    }
    (String::from_utf8_lossy(&out).into_owned(), nil)
}

fn path_clean(p: &str) -> String {
    if p.is_empty() { return ".".to_string(); }
    let absolute = p.starts_with('/');
    let mut stack: Vec<&str> = Vec::new();
    for part in p.split('/') {
        match part {
            "" | "." => continue,
            ".." => {
                if stack.last().map_or(false, |t| *t != "..") && !stack.is_empty() {
                    stack.pop();
                } else if !absolute {
                    stack.push("..");
                }
            }
            other => stack.push(other),
        }
    }
    let joined = stack.join("/");
    if absolute { format!("/{}", joined) }
    else if joined.is_empty() { ".".to_string() }
    else { joined }
}

/// url.JoinPath(base, elem...) — returns base with elem joined to its path.
#[allow(non_snake_case)]
pub fn JoinPath(base: impl AsRef<str>, elem: &[impl AsRef<str>]) -> (string, error) {
    let (u, err) = Parse(base);
    if err != nil { return (String::new(), err); }
    let out = u.JoinPath(elem);
    (out.String(), nil)
}

/// url.ParseRequestURI parses rawurl as absolute (either URL or path-only) for server Request.URI.
#[allow(non_snake_case)]
pub fn ParseRequestURI(raw: impl AsRef<str>) -> (URL, error) {
    Parse(raw)
}

fn hex_val(b: u8) -> i32 {
    match b {
        b'0'..=b'9' => (b - b'0') as i32,
        b'a'..=b'f' => (b - b'a' + 10) as i32,
        b'A'..=b'F' => (b - b'A' + 10) as i32,
        _ => -1,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_simple_http() {
        let (u, err) = Parse("http://example.com/foo/bar");
        assert_eq!(err, nil);
        assert_eq!(u.Scheme, "http");
        assert_eq!(u.Host, "example.com");
        assert_eq!(u.Path, "/foo/bar");
    }

    #[test]
    fn parse_with_userinfo_and_port() {
        let (u, err) = Parse("https://alice:s3cret@api.example.com:8443/v1/ping?x=1&y=2#section");
        assert_eq!(err, nil);
        assert_eq!(u.Scheme, "https");
        assert_eq!(u.Host, "api.example.com:8443");
        assert_eq!(u.Hostname(), "api.example.com");
        assert_eq!(u.Port(), "8443");
        assert_eq!(u.Path, "/v1/ping");
        assert_eq!(u.RawQuery, "x=1&y=2");
        assert_eq!(u.Fragment, "section");
        let ui = u.User.as_ref().unwrap();
        assert_eq!(ui.username, "alice");
        assert_eq!(ui.password, Some("s3cret".to_string()));
    }

    #[test]
    fn parse_ipv6_host() {
        let (u, err) = Parse("http://[::1]:8080/path");
        assert_eq!(err, nil);
        assert_eq!(u.Hostname(), "::1");
        assert_eq!(u.Port(), "8080");
    }

    #[test]
    fn parse_opaque_mailto() {
        let (u, err) = Parse("mailto:alice@example.com");
        assert_eq!(err, nil);
        assert_eq!(u.Scheme, "mailto");
        assert_eq!(u.Opaque, "alice@example.com");
    }

    #[test]
    fn url_string_roundtrip() {
        let (u, _) = Parse("http://example.com/foo?bar=1#x");
        assert_eq!(u.String(), "http://example.com/foo?bar=1#x");
    }

    #[test]
    fn query_escape_and_unescape() {
        assert_eq!(QueryEscape("hello world/+?"), "hello+world%2F%2B%3F");
        let (v, err) = QueryUnescape("hello+world%2F");
        assert_eq!(err, nil);
        assert_eq!(v, "hello world/");
    }

    #[test]
    fn path_escape_preserves_slashes() {
        assert_eq!(PathEscape("a b/c"), "a%20b/c");
    }

    #[test]
    fn values_encode_sorts_keys() {
        let mut v = Values::new();
        v.Set("name", "alice smith");
        v.Add("tag", "a");
        v.Add("tag", "b");
        assert_eq!(v.Encode(), "name=alice+smith&tag=a&tag=b");
    }

    #[test]
    fn parse_query_round_trip() {
        let (v, err) = ParseQuery("a=1&a=2&b=three");
        assert_eq!(err, nil);
        assert_eq!(v.Values("a"), vec!["1", "2"]);
        assert_eq!(v.Get("b"), "three");
    }

    #[test]
    fn bad_escape_is_error() {
        let (_, err) = QueryUnescape("%ZZ");
        assert!(err != nil);
    }
}