diff options
author | Ms2ger <Ms2ger@gmail.com> | 2016-06-07 14:01:26 +0200 |
---|---|---|
committer | Ms2ger <Ms2ger@gmail.com> | 2016-06-07 14:20:08 +0200 |
commit | 80fc66673473910ec325eb35d1e2e040f9e5277a (patch) | |
tree | f12c34fc14f94493d8cdf1724a9ede3b51c6365c | |
parent | 6a1722e18d926aa0a177e4b9a672a90bf4c541f5 (diff) | |
download | servo-80fc66673473910ec325eb35d1e2e040f9e5277a.tar.gz servo-80fc66673473910ec325eb35d1e2e040f9e5277a.zip |
Move trim_http_whitespace to net_traits.
-rw-r--r-- | components/net_traits/lib.rs | 24 | ||||
-rw-r--r-- | components/script/dom/xmlhttprequest.rs | 25 | ||||
-rw-r--r-- | tests/unit/net_traits/lib.rs | 22 | ||||
-rw-r--r-- | tests/unit/script/dom/xmlhttprequest.rs | 27 | ||||
-rw-r--r-- | tests/unit/script/lib.rs | 1 |
5 files changed, 47 insertions, 52 deletions
diff --git a/components/net_traits/lib.rs b/components/net_traits/lib.rs index 4eab3e725e9..be6cbf4ee95 100644 --- a/components/net_traits/lib.rs +++ b/components/net_traits/lib.rs @@ -573,3 +573,27 @@ pub enum NetworkError { /// SSL validation error that has to be handled in the HTML parser SslValidation(Url), } + +/// Normalize `slice`, as defined by +/// [the Fetch Spec](https://fetch.spec.whatwg.org/#concept-header-value-normalize). +pub fn trim_http_whitespace(mut slice: &[u8]) -> &[u8] { + const HTTP_WS_BYTES: &'static [u8] = b"\x09\x0A\x0D\x20"; + + loop { + match slice.split_first() { + Some((first, remainder)) if HTTP_WS_BYTES.contains(first) => + slice = remainder, + _ => break, + } + } + + loop { + match slice.split_last() { + Some((last, remainder)) if HTTP_WS_BYTES.contains(last) => + slice = remainder, + _ => break, + } + } + + slice +} diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs index 18131bd5fd8..318d3bbc1ec 100644 --- a/components/script/dom/xmlhttprequest.rs +++ b/components/script/dom/xmlhttprequest.rs @@ -46,6 +46,7 @@ use js::jsapi::{JSContext, JS_ParseJSON, RootedValue}; use js::jsval::{JSVal, NullValue, UndefinedValue}; use msg::constellation_msg::{PipelineId, ReferrerPolicy}; use net_traits::CoreResourceMsg::Load; +use net_traits::trim_http_whitespace; use net_traits::{AsyncResponseListener, AsyncResponseTarget, Metadata, NetworkError, RequestSource}; use net_traits::{LoadConsumer, LoadContext, LoadData, ResourceCORSData, CoreResourceThread, LoadOrigin}; use network_listener::{NetworkListener, PreInvoke}; @@ -1506,27 +1507,3 @@ pub fn is_field_value(slice: &[u8]) -> bool { } }) } - -/// Normalize `self`, as defined by -/// [the Fetch Spec](https://fetch.spec.whatwg.org/#concept-header-value-normalize). -pub fn trim_http_whitespace(mut slice: &[u8]) -> &[u8] { - const HTTP_WS_BYTES: &'static [u8] = b"\x09\x0A\x0D\x20"; - - loop { - match slice.split_first() { - Some((first, remainder)) if HTTP_WS_BYTES.contains(first) => - slice = remainder, - _ => break, - } - } - - loop { - match slice.split_last() { - Some((last, remainder)) if HTTP_WS_BYTES.contains(last) => - slice = remainder, - _ => break, - } - } - - slice -} diff --git a/tests/unit/net_traits/lib.rs b/tests/unit/net_traits/lib.rs index 89bded99c22..0ea8e4cfe78 100644 --- a/tests/unit/net_traits/lib.rs +++ b/tests/unit/net_traits/lib.rs @@ -5,3 +5,25 @@ extern crate net_traits; #[cfg(test)] mod image; + +#[test] +fn test_trim_http_whitespace() { + fn test_trim(in_: &[u8], out: &[u8]) { + let b = net_traits::trim_http_whitespace(in_); + assert_eq!(b, out); + } + + test_trim(b"", b""); + + test_trim(b" ", b""); + test_trim(b"a", b"a"); + test_trim(b" a", b"a"); + test_trim(b"a ", b"a"); + test_trim(b" a ", b"a"); + + test_trim(b"\t", b""); + test_trim(b"a", b"a"); + test_trim(b"\ta", b"a"); + test_trim(b"a\t", b"a"); + test_trim(b"\ta\t", b"a"); +} diff --git a/tests/unit/script/dom/xmlhttprequest.rs b/tests/unit/script/dom/xmlhttprequest.rs deleted file mode 100644 index f9d4ebc25e8..00000000000 --- a/tests/unit/script/dom/xmlhttprequest.rs +++ /dev/null @@ -1,27 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -use script::dom::xmlhttprequest::trim_http_whitespace; - -#[test] -fn test_trim_http_whitespace() { - fn test_trim(in_: &[u8], out: &[u8]) { - let b = trim_http_whitespace(in_); - assert_eq!(b, out); - } - - test_trim(b"", b""); - - test_trim(b" ", b""); - test_trim(b"a", b"a"); - test_trim(b" a", b"a"); - test_trim(b"a ", b"a"); - test_trim(b" a ", b"a"); - - test_trim(b"\t", b""); - test_trim(b"a", b"a"); - test_trim(b"\ta", b"a"); - test_trim(b"a\t", b"a"); - test_trim(b"\ta\t", b"a"); -} diff --git a/tests/unit/script/lib.rs b/tests/unit/script/lib.rs index cab595e0500..c2829292bb9 100644 --- a/tests/unit/script/lib.rs +++ b/tests/unit/script/lib.rs @@ -15,5 +15,4 @@ extern crate url; #[cfg(test)] mod dom { mod bindings; mod blob; - mod xmlhttprequest; } |