diff options
author | Nick Wilson <nijolas.wilson@gmail.com> | 2024-06-14 17:37:03 +1000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-07-17 13:07:16 +0100 |
commit | f31f5ef4239e4418eaa21bfc8c118b3cf40fceda (patch) | |
tree | a483ee0d8cb0ee64d032427e06782c790ec22b1e /src | |
parent | 2bd61406c2c215cfcbecab1a0ce20543a0eca267 (diff) | |
download | gleam_stdlib-f31f5ef4239e4418eaa21bfc8c118b3cf40fceda.tar.gz gleam_stdlib-f31f5ef4239e4418eaa21bfc8c118b3cf40fceda.zip |
Correct implementation of uri.origin and add tests for default ports
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/uri.gleam | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index 05dfb9d..508cfef 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -361,20 +361,18 @@ pub fn to_string(uri: Uri) -> String { /// pub fn origin(uri: Uri) -> Result(String, Nil) { let Uri(scheme: scheme, host: host, port: port, ..) = uri - case scheme { - Some("https") if port == Some(443) -> { - let origin = Uri(scheme, None, host, None, "", None, None) - Ok(to_string(origin)) - } - Some("http") if port == Some(80) -> { - let origin = Uri(scheme, None, host, None, "", None, None) - Ok(to_string(origin)) - } - Some(s) if s == "http" || s == "https" -> { - let origin = Uri(scheme, None, host, port, "", None, None) - Ok(to_string(origin)) + case host, scheme { + Some(h), Some("https") if port == Some(443) -> + Ok(string.concat(["https://", h])) + Some(h), Some("http") if port == Some(80) -> + Ok(string.concat(["http://", h])) + Some(h), Some(s) if s == "http" || s == "https" -> { + case port { + Some(p) -> Ok(string.concat([s, "://", h, ":", int.to_string(p)])) + None -> Ok(string.concat([s, "://", h])) + } } - _ -> Error(Nil) + _, _ -> Error(Nil) } } |