diff options
| author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-07-10 20:51:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-10 15:51:24 -0400 |
| commit | 69e0886362623e1998f192dda72567b4e66b4117 (patch) | |
| tree | 95d83f8a2e288f3203f4bfe952ad6969fdf71b50 /cli/ops | |
| parent | 39dba12a061e464fb06bc6a763c84b36b5d1a915 (diff) | |
fix(URL): Implement spec-compliant host parsing (#6689)
Diffstat (limited to 'cli/ops')
| -rw-r--r-- | cli/ops/idna.rs | 43 | ||||
| -rw-r--r-- | cli/ops/mod.rs | 1 |
2 files changed, 44 insertions, 0 deletions
diff --git a/cli/ops/idna.rs b/cli/ops/idna.rs new file mode 100644 index 000000000..8ecef4862 --- /dev/null +++ b/cli/ops/idna.rs @@ -0,0 +1,43 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +//! https://url.spec.whatwg.org/#idna + +use super::dispatch_json::{Deserialize, JsonOp, Value}; +use crate::op_error::{ErrorKind, OpError}; +use crate::state::State; +use deno_core::CoreIsolate; +use deno_core::ZeroCopyBuf; +use idna::{domain_to_ascii, domain_to_ascii_strict}; + +pub fn init(i: &mut CoreIsolate, s: &State) { + i.register_op("op_domain_to_ascii", s.stateful_json_op(op_domain_to_ascii)); +} + +fn invalid_domain_error() -> OpError { + OpError { + kind: ErrorKind::TypeError, + msg: "Invalid domain.".to_string(), + } +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct DomainToAscii { + domain: String, + be_strict: bool, +} + +fn op_domain_to_ascii( + _state: &State, + args: Value, + _zero_copy: &mut [ZeroCopyBuf], +) -> Result<JsonOp, OpError> { + let args: DomainToAscii = serde_json::from_value(args)?; + let domain = if args.be_strict { + domain_to_ascii_strict(args.domain.as_str()) + .map_err(|_| invalid_domain_error())? + } else { + domain_to_ascii(args.domain.as_str()).map_err(|_| invalid_domain_error())? + }; + Ok(JsonOp::Sync(json!(domain))) +} diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index a53e5ac16..ef8c3bd0f 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -13,6 +13,7 @@ pub mod errors; pub mod fetch; pub mod fs; pub mod fs_events; +pub mod idna; pub mod io; pub mod net; #[cfg(unix)] |
