From 8e957422f7867a9bb01f71801bc91efdb551d981 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Tue, 12 Dec 2023 16:13:25 -0800 Subject: [PATCH 1/8] vendor third_party rust crate http 1.0.0 This is the new stable release and should be preferred over earlier versions. --- third_party/rust/Cargo.lock | 4 +- third_party/rust/Cargo.toml | 10 +- third_party/rust/http/v1/BUILD.gn | 61 + third_party/rust/http/v1/README.chromium | 8 + .../rust/http/v1/crate/.cargo_vcs_info.json | 6 + .../http/v1/crate/.github/workflows/ci.yml | 97 + third_party/rust/http/v1/crate/.gitignore | 2 + third_party/rust/http/v1/crate/CHANGELOG.md | 217 + third_party/rust/http/v1/crate/Cargo.toml | 64 + .../rust/http/v1/crate/Cargo.toml.orig | 51 + third_party/rust/http/v1/crate/LICENSE-APACHE | 201 + third_party/rust/http/v1/crate/LICENSE-MIT | 25 + third_party/rust/http/v1/crate/README.md | 80 + .../rust/http/v1/crate/src/byte_str.rs | 85 + third_party/rust/http/v1/crate/src/convert.rs | 17 + third_party/rust/http/v1/crate/src/error.rs | 149 + .../rust/http/v1/crate/src/extensions.rs | 289 ++ .../rust/http/v1/crate/src/header/map.rs | 3545 +++++++++++++++++ .../rust/http/v1/crate/src/header/mod.rs | 174 + .../rust/http/v1/crate/src/header/name.rs | 1868 +++++++++ .../rust/http/v1/crate/src/header/value.rs | 795 ++++ third_party/rust/http/v1/crate/src/lib.rs | 214 + third_party/rust/http/v1/crate/src/method.rs | 473 +++ third_party/rust/http/v1/crate/src/request.rs | 1089 +++++ .../rust/http/v1/crate/src/response.rs | 795 ++++ third_party/rust/http/v1/crate/src/status.rs | 588 +++ .../rust/http/v1/crate/src/uri/authority.rs | 684 ++++ .../rust/http/v1/crate/src/uri/builder.rs | 197 + third_party/rust/http/v1/crate/src/uri/mod.rs | 1118 ++++++ .../rust/http/v1/crate/src/uri/path.rs | 564 +++ .../rust/http/v1/crate/src/uri/port.rs | 151 + .../rust/http/v1/crate/src/uri/scheme.rs | 363 ++ .../rust/http/v1/crate/src/uri/tests.rs | 519 +++ third_party/rust/http/v1/crate/src/version.rs | 75 + .../rust/http/v1/crate/tests/header_map.rs | 650 +++ .../http/v1/crate/tests/header_map_fuzz.rs | 376 ++ .../rust/http/v1/crate/tests/status_code.rs | 82 + third_party/rust/third_party.toml | 2 +- 38 files changed, 15682 insertions(+), 6 deletions(-) create mode 100644 third_party/rust/http/v1/BUILD.gn create mode 100644 third_party/rust/http/v1/README.chromium create mode 100644 third_party/rust/http/v1/crate/.cargo_vcs_info.json create mode 100644 third_party/rust/http/v1/crate/.github/workflows/ci.yml create mode 100644 third_party/rust/http/v1/crate/.gitignore create mode 100644 third_party/rust/http/v1/crate/CHANGELOG.md create mode 100644 third_party/rust/http/v1/crate/Cargo.toml create mode 100644 third_party/rust/http/v1/crate/Cargo.toml.orig create mode 100644 third_party/rust/http/v1/crate/LICENSE-APACHE create mode 100644 third_party/rust/http/v1/crate/LICENSE-MIT create mode 100644 third_party/rust/http/v1/crate/README.md create mode 100644 third_party/rust/http/v1/crate/src/byte_str.rs create mode 100644 third_party/rust/http/v1/crate/src/convert.rs create mode 100644 third_party/rust/http/v1/crate/src/error.rs create mode 100644 third_party/rust/http/v1/crate/src/extensions.rs create mode 100644 third_party/rust/http/v1/crate/src/header/map.rs create mode 100644 third_party/rust/http/v1/crate/src/header/mod.rs create mode 100644 third_party/rust/http/v1/crate/src/header/name.rs create mode 100644 third_party/rust/http/v1/crate/src/header/value.rs create mode 100644 third_party/rust/http/v1/crate/src/lib.rs create mode 100644 third_party/rust/http/v1/crate/src/method.rs create mode 100644 third_party/rust/http/v1/crate/src/request.rs create mode 100644 third_party/rust/http/v1/crate/src/response.rs create mode 100644 third_party/rust/http/v1/crate/src/status.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/authority.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/builder.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/mod.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/path.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/port.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/scheme.rs create mode 100644 third_party/rust/http/v1/crate/src/uri/tests.rs create mode 100644 third_party/rust/http/v1/crate/src/version.rs create mode 100644 third_party/rust/http/v1/crate/tests/header_map.rs create mode 100644 third_party/rust/http/v1/crate/tests/header_map_fuzz.rs create mode 100644 third_party/rust/http/v1/crate/tests/status_code.rs diff --git a/third_party/rust/Cargo.lock b/third_party/rust/Cargo.lock index 1595d65b9f48..f6d567313ac3 100644 --- a/third_party/rust/Cargo.lock +++ b/third_party/rust/Cargo.lock @@ -1051,11 +1051,11 @@ dependencies = [ [[package]] name = "http" -version = "0.1.21" +version = "1.0.0" dependencies = [ "bytes", "fnv", - "itoa 0.4.8", + "itoa", ] [[package]] diff --git a/third_party/rust/Cargo.toml b/third_party/rust/Cargo.toml index 027be69a2d1e..890e594535f7 100644 --- a/third_party/rust/Cargo.toml +++ b/third_party/rust/Cargo.toml @@ -113,7 +113,7 @@ build-script-outputs = ["rules.rs"] gn-variables-lib = "cargo_manifest_dir =\n rebase_path(\"//brave/third_party/rust/html5ever/v0_25/crate\")\nrustenv = [ \"CARGO_MANIFEST_DIR=$cargo_manifest_dir\" ]\n" [dependencies.http] -version = "0.1" +version = "1" [dependencies.iana-time-zone] version = "0.1" @@ -312,6 +312,10 @@ package = "byteorder" path = "bytes/v0_4/crate" package = "bytes" +[patch.crates-io.bytes_v1] +path = "bytes/v1/crate" +package = "bytes" + [patch.crates-io.camino_v1] path = "../../../third_party/rust/camino/v1/crate" package = "camino" @@ -656,8 +660,8 @@ package = "hmac" path = "html5ever/v0_25/crate" package = "html5ever" -[patch.crates-io.http_v0_1] -path = "http/v0_1/crate" +[patch.crates-io.http_v1] +path = "http/v1/crate" package = "http" [patch.crates-io.iana-time-zone_v0_1] diff --git a/third_party/rust/http/v1/BUILD.gn b/third_party/rust/http/v1/BUILD.gn new file mode 100644 index 000000000000..4bafb8c45d62 --- /dev/null +++ b/third_party/rust/http/v1/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright 2023 The Chromium Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/rust/cargo_crate.gni") + +cargo_crate("lib") { + crate_name = "http" + epoch = "1" + crate_type = "rlib" + crate_root = "crate/src/lib.rs" + sources = [ + "//brave/third_party/rust/http/v1/crate/src/byte_str.rs", + "//brave/third_party/rust/http/v1/crate/src/convert.rs", + "//brave/third_party/rust/http/v1/crate/src/error.rs", + "//brave/third_party/rust/http/v1/crate/src/extensions.rs", + "//brave/third_party/rust/http/v1/crate/src/header/map.rs", + "//brave/third_party/rust/http/v1/crate/src/header/mod.rs", + "//brave/third_party/rust/http/v1/crate/src/header/name.rs", + "//brave/third_party/rust/http/v1/crate/src/header/value.rs", + "//brave/third_party/rust/http/v1/crate/src/lib.rs", + "//brave/third_party/rust/http/v1/crate/src/method.rs", + "//brave/third_party/rust/http/v1/crate/src/request.rs", + "//brave/third_party/rust/http/v1/crate/src/response.rs", + "//brave/third_party/rust/http/v1/crate/src/status.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/authority.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/builder.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/mod.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/path.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/port.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/scheme.rs", + "//brave/third_party/rust/http/v1/crate/src/uri/tests.rs", + "//brave/third_party/rust/http/v1/crate/src/version.rs", + "//brave/third_party/rust/http/v1/crate/tests/header_map.rs", + "//brave/third_party/rust/http/v1/crate/tests/header_map_fuzz.rs", + "//brave/third_party/rust/http/v1/crate/tests/status_code.rs", + ] + inputs = [ + "//brave/third_party/rust/http/v1/crate/CHANGELOG.md", + "//brave/third_party/rust/http/v1/crate/README.md", + ] + + # Unit tests skipped. Generate with --with-tests to include them. + build_native_rust_unit_tests = false + edition = "2018" + cargo_pkg_version = "1.0.0" + cargo_pkg_authors = "Alex Crichton , Carl Lerche , Sean McArthur " + cargo_pkg_name = "http" + cargo_pkg_description = + "A set of types for representing HTTP requests and responses." + library_configs -= [ "//build/config/compiler:chromium_code" ] + library_configs += [ "//build/config/compiler:no_chromium_code" ] + executable_configs -= [ "//build/config/compiler:chromium_code" ] + executable_configs += [ "//build/config/compiler:no_chromium_code" ] + deps = [ + "//brave/third_party/rust/bytes/v0_4:lib", + "//brave/third_party/rust/fnv/v1:lib", + "//third_party/rust/itoa/v1:lib", + ] + features = [ "std" ] +} diff --git a/third_party/rust/http/v1/README.chromium b/third_party/rust/http/v1/README.chromium new file mode 100644 index 000000000000..06df695e105e --- /dev/null +++ b/third_party/rust/http/v1/README.chromium @@ -0,0 +1,8 @@ +Name: http +URL: https://crates.io/crates/http +Description: A set of types for representing HTTP requests and responses. + +Version: 1.0.0 +Security Critical: yes +License: Apache 2.0 +Revision: 1c06623c9d97d42ac02c351ab4872d6481c793f7 diff --git a/third_party/rust/http/v1/crate/.cargo_vcs_info.json b/third_party/rust/http/v1/crate/.cargo_vcs_info.json new file mode 100644 index 000000000000..b343d197e396 --- /dev/null +++ b/third_party/rust/http/v1/crate/.cargo_vcs_info.json @@ -0,0 +1,6 @@ +{ + "git": { + "sha1": "1c06623c9d97d42ac02c351ab4872d6481c793f7" + }, + "path_in_vcs": "" +} \ No newline at end of file diff --git a/third_party/rust/http/v1/crate/.github/workflows/ci.yml b/third_party/rust/http/v1/crate/.github/workflows/ci.yml new file mode 100644 index 000000000000..6049236edb9a --- /dev/null +++ b/third_party/rust/http/v1/crate/.github/workflows/ci.yml @@ -0,0 +1,97 @@ +name: CI +on: + pull_request: + push: + branches: + - master + +env: + RUST_BACKTRACE: 1 + +jobs: + test: + name: Test ${{ matrix.rust }} + #needs: [style] + strategy: + matrix: + rust: + - stable + - beta + - nightly + + include: + - rust: nightly + benches: true + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install Rust (${{ matrix.rust }}) + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + + - name: Test + run: cargo test + + - name: Test all benches + if: matrix.benches + run: cargo test --benches ${{ matrix.features }} + + msrv: + name: Check MSRV + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Get MSRV from package metadata + id: metadata + run: | + cargo metadata --no-deps --format-version 1 | + jq -r '"msrv=" + (.packages[] | select(.name == "http")).rust_version' >> $GITHUB_OUTPUT + + - name: Install Rust (${{ steps.metadata.outputs.msrv }}) + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ steps.metadata.outputs.msrv }} + + - name: Test + run: cargo check -p http + + wasm: + name: WASM + #needs: [style] + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + targets: wasm32-unknown-unknown + + - name: Check + run: cargo check --target wasm32-unknown-unknown + + miri: + name: Miri + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install Rust + uses: dtolnay/rust-toolchain@nightly + with: + components: miri + + - name: Test + run: MIRIFLAGS="-Zmiri-disable-isolation -Zmiri-ignore-leaks" cargo miri test diff --git a/third_party/rust/http/v1/crate/.gitignore b/third_party/rust/http/v1/crate/.gitignore new file mode 100644 index 000000000000..a9d37c560c6a --- /dev/null +++ b/third_party/rust/http/v1/crate/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/third_party/rust/http/v1/crate/CHANGELOG.md b/third_party/rust/http/v1/crate/CHANGELOG.md new file mode 100644 index 000000000000..94aa956f03d8 --- /dev/null +++ b/third_party/rust/http/v1/crate/CHANGELOG.md @@ -0,0 +1,217 @@ +# 1.0.0 (November 15, 2023) + +- Implement `Clone` for `Request`, `Response`, and `Extensions`. This breaking change requires + that all extensions now implement `Clone`. +- Add a default-on `std` feature. Disabling it currently is not supported. +- Fix MIRI warnings in `HeaderMap::iter()`. + +# 0.2.10 (November 10, 2023) + +* Fix parsing of `Authority` to handle square brackets in incorrect order. +* Fix `HeaderMap::with_capacity()` to handle arithmetic overflow. + +# 0.2.9 (February 17, 2023) + +* Add `HeaderName` constants for `cache-status` and `cdn-cache-control`. +* Implement `Hash` for `PathAndQuery`. +* Re-export `HeaderName` at crate root. + +# 0.2.8 (June 6, 2022) + +* Fix internal usage of uninitialized memory to use `MaybeUninit` inside `HeaderName`. + +# 0.2.7 (April 28, 2022) + +* MSRV bumped to `1.49`. +* Add `extend()` method to `Extensions`. +* Add `From` and `From` impls for `Uri`. +* Make `HeaderName::from_static` a `const fn`. + +# 0.2.6 (December 30, 2021) + +* Upgrade internal `itoa` dependency to 1.0. + +# 0.2.5 (September 21, 2021) + +* Add `is_empty()` and `len()` methods to `Extensions`. +* Add `version_ref()` method to `request::Builder`. +* Implement `TryFrom>` and `TryFrom` for `Authority`, `Uri`, `PathAndQuery`, and `HeaderName`. +* Make `HeaderValue::from_static` a `const fn`. + +# 0.2.4 (April 4, 2021) + +* Fix `Uri` parsing to allow `{`, `"`, and `}` in paths. + +# 0.2.3 (January 7, 2021) + +* Upgrade internal (private) `bytes` dependency to 1.0. + +# 0.2.2 (December 14, 2020) + +* Fix (potential double) panic of (`HeaderMap`) `OccupiedEntry::remove_entry` and + `remove_entry_mult` when multiple values are present. ([#446], [#449] dekellum) +* Safety audits of (priv) `ByteStr` and refactor of `Authority` ([#408], [#414] sbosnick) +* Fix `HeaderName` to error instead of panic when input is too long ([#432] [#433] acfoltzer) +* Allow `StatusCode` to encode values 100-999 without error. Use of the + unclassified range 600-999 remains discouraged. ([#144], [#438], [#443] quininer dekellum) +* Add `String` and `&String` fallible conversions to `PathAndQuery` ([#450] mkindahl) +* Fix `Authority` (and `Uri`) to error instead of panic on unbalanced brackets + ([#435], [#445] aeryz) + +# 0.2.1 (March 25, 2020) + +* Add `extensions_ref` and `extensions_mut` to `request::Builder` and `response::Builder`. + +# 0.2.0 (December 2, 2019) + +* Add `Version::HTTP_3` constant. +* Add `HeaderValue::from_maybe_shared`, `HeaderValue::from_maybe_shared_unchecked`, `Uri::from_maybe_shared`, `Authority::from_maybe_shared`, and `PathAndQuery::from_maybe_shared`. +* Change `request::Builder`, `response::Builder`, and `uri::Builder` to use by-value methods instead of by-ref. +* Change from `HttpTryFrom` trait to `std::convert::TryFrom`. +* Change `HeaderMap::entry` to no longer return a `Result`. +* Change `HeaderMap::drain` iterator to match the behavior of `IntoIter`. +* Change `Authority::port` to return an `Option` instead of `Option`. +* Change `Uri::scheme` to return `Option<&Scheme>` instead of `Option<&str>`. +* Change `Uri::authority` to return `Option<&Authority>` instead of `Option<&str>`. +* Remove `InvalidUriBytes`, `InvalidHeaderNameBytes`, and `InvalidHeaderValueBytes` error types. +* Remove `HeaderValue::from_shared`, `HeaderValue::from_shared_unchecked`, `Uri::from_shared`, `Authority::from_shared`, `Scheme::from_shared`, and `PathAndQuery::from_shared`. +* Remove `Authority::port_part`. +* Remove `Uri::scheme_part` and `Uri::authority_part`. + +# 0.1.20 (November 26, 2019) + +* Fix possible double-free if `header::Drain` iterator is `std::mem::forgot`en (#357). +* Fix possible data race if multiple `header::ValueDrain`s are iterated on different threads (#362). +* Fix `HeaderMap::reserve` capacity overflows (#360). +* Fix parsing long authority-form `Uri`s (#351). + +# 0.1.19 (October 15, 2019) + +* Allow `%` in IPv6 addresses in `Uri` (#343). + +# 0.1.18 (July 26, 2019) + +* Fix compilation of `HeaderName` parsing on WASM targets (#324). +* Implement `HttpTryFrom` for `HeaderMap` (#326). +* Export `http::header::HeaderValue` as `http::HeaderValue`. + +# 0.1.17 (April 5, 2019) + +* Add `Error::inner_ref()` to view the kind of error (#303) +* Add `headers_ref()` and `headers_mut()` methods to `request::Builder` and `response::Builder` (#293) + +# 0.1.16 (February 19, 2019) + +* Fix `Uri` to permit more characters in the `path` (#296) + +# 0.1.15 (January 22, 2019) + +* Fix `Uri::host()` to include brackets of IPv6 literals (#292) +* Add `scheme_str` and `port_u16` methods to `Uri` (#287) +* Add `method_ref`, `uri_ref`, and `headers_ref` to `request::Builder` (#284) + +# 0.1.14 (November 21, 2018) + +* Add `Port` struct (#252, #255, #265) +* Introduce `Uri` builder (#219) +* Empty `Method` no longer considered valid (#262) +* Fix `Uri` equality when terminating question mark is present (#270) +* Allow % character in userinfo (#269) +* Support additional tokens for header names (#271) +* Export `http::headers::{IterMut, ValuesMut}` (#278) + +# 0.1.13 (September 14, 2018) + +* impl `fmt::Display` for `HeaderName` (#249) +* Fix `uri::Authority` parsing when there is no host after an `@` (#248) +* Fix `Uri` parsing to allow more characters in query strings (#247) + +# 0.1.12 (September 7, 2018) + +* Fix `HeaderValue` parsing to allow HTABs (#244) + +# 0.1.11 (September 5, 2018) + +* Add `From<&Self>` for `HeaderValue`, `Method`, and `StatusCode` (#238) +* Add `Uri::from_static` (#240) + +# 0.1.10 (August 8, 2018) + +* `impl HttpTryFrom` for HeaderValue (#236) + +# 0.1.9 (August 7, 2018) + +* Fix double percent encoding (#233) +* Add additional HttpTryFrom impls (#234) + +# 0.1.8 (July 23, 2018) + +* Add fuller set of `PartialEq` for `Method` (#221) +* Reduce size of `HeaderMap` by using `Box<[Entry]>` instea of `Vec` (#224) +* Reduce size of `Extensions` by storing as `Option>` (#227) +* Implement `Iterator::size_hint` for most iterators in `header` (#226) + +# 0.1.7 (June 22, 2018) + +* Add `From for HeaderValue` for most integer types (#218). +* Add `Uri::into_parts()` inherent method (same as `Parts::from(uri)`) (#214). +* Fix converting `Uri`s in authority-form to `Parts` and then back into `Uri` (#216). +* Fix `Authority` parsing to reject multiple port sections (#215). +* Fix parsing 1 character authority-form `Uri`s into illegal forms (#220). + +# 0.1.6 (June 13, 2018) + +* Add `HeaderName::from_static()` constructor (#195). +* Add `Authority::from_static()` constructor (#186). +* Implement `From` for `HeaderValue` (#184). +* Fix duplicate keys when iterating over `header::Keys` (#201). + +# 0.1.5 (February 28, 2018) + +* Add websocket handshake related header constants (#162). +* Parsing `Authority` with an empty string now returns an error (#164). +* Implement `PartialEq` for `StatusCode` (#153). +* Implement `HttpTryFrom<&Uri>` for `Uri` (#165). +* Implement `FromStr` for `Method` (#167). +* Implement `HttpTryFrom` for `Uri` (#171). +* Add `into_body` fns to `Request` and `Response` (#172). +* Fix `Request::options` (#177). + +# 0.1.4 (January 4, 2018) + +* Add PathAndQuery::from_static (#148). +* Impl PartialOrd / PartialEq for Authority and PathAndQuery (#150). +* Add `map` fn to `Request` and `Response` (#151). + +# 0.1.3 (December 11, 2017) + +* Add `Scheme` associated consts for common protos. + +# 0.1.2 (November 29, 2017) + +* Add Uri accessor for scheme part. +* Fix Uri parsing bug (#134) + +# 0.1.1 (October 9, 2017) + +* Provide Uri accessors for parts (#129) +* Add Request builder helpers. (#123) +* Misc performance improvements (#126) + +# 0.1.0 (September 8, 2017) + +* Initial release. + +[#144]: https://github.com/hyperium/http/issues/144 +[#408]: https://github.com/hyperium/http/pull/408 +[#414]: https://github.com/hyperium/http/pull/414 +[#432]: https://github.com/hyperium/http/issues/432 +[#433]: https://github.com/hyperium/http/pull/433 +[#438]: https://github.com/hyperium/http/pull/438 +[#443]: https://github.com/hyperium/http/pull/443 +[#446]: https://github.com/hyperium/http/issues/446 +[#449]: https://github.com/hyperium/http/pull/449 +[#450]: https://github.com/hyperium/http/pull/450 +[#435]: https://github.com/hyperium/http/issues/435 +[#445]: https://github.com/hyperium/http/pull/445 + diff --git a/third_party/rust/http/v1/crate/Cargo.toml b/third_party/rust/http/v1/crate/Cargo.toml new file mode 100644 index 000000000000..d1dbad88b6f5 --- /dev/null +++ b/third_party/rust/http/v1/crate/Cargo.toml @@ -0,0 +1,64 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +rust-version = "1.49.0" +name = "http" +version = "1.0.0" +authors = [ + "Alex Crichton ", + "Carl Lerche ", + "Sean McArthur ", +] +description = """ +A set of types for representing HTTP requests and responses. +""" +documentation = "https://docs.rs/http" +readme = "README.md" +keywords = ["http"] +categories = ["web-programming"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/hyperium/http" + +[dependencies.bytes] +version = "1" + +[dependencies.fnv] +version = "1.0.5" + +[dependencies.itoa] +version = "1" + +[dev-dependencies.doc-comment] +version = "0.3" + +[dev-dependencies.indexmap] +version = "<=1.8" + +[dev-dependencies.quickcheck] +version = "0.9.0" + +[dev-dependencies.rand] +version = "0.7.0" + +[dev-dependencies.seahash] +version = "3.0.5" + +[dev-dependencies.serde] +version = "1.0" + +[dev-dependencies.serde_json] +version = "1.0" + +[features] +default = ["std"] +std = [] diff --git a/third_party/rust/http/v1/crate/Cargo.toml.orig b/third_party/rust/http/v1/crate/Cargo.toml.orig new file mode 100644 index 000000000000..7f20e71f5ef9 --- /dev/null +++ b/third_party/rust/http/v1/crate/Cargo.toml.orig @@ -0,0 +1,51 @@ +[package] +name = "http" +# When releasing to crates.io: +# - Update html_root_url in lib.rs. +# - Update CHANGELOG.md. +# - Create git tag +version = "1.0.0" +readme = "README.md" +documentation = "https://docs.rs/http" +repository = "https://github.com/hyperium/http" +license = "MIT OR Apache-2.0" +authors = [ + "Alex Crichton ", + "Carl Lerche ", + "Sean McArthur ", +] +description = """ +A set of types for representing HTTP requests and responses. +""" +keywords = ["http"] +categories = ["web-programming"] +edition = "2018" +# When updating this value, don't forget to also adjust the GitHub Actions config. +rust-version = "1.49.0" + +[workspace] +members = [ + ".", +] +exclude = [ + "fuzz", + "benches" +] + +[features] +default = ["std"] +std = [] + +[dependencies] +bytes = "1" +fnv = "1.0.5" +itoa = "1" + +[dev-dependencies] +indexmap = "<=1.8" +quickcheck = "0.9.0" +rand = "0.7.0" +seahash = "3.0.5" +serde = "1.0" +serde_json = "1.0" +doc-comment = "0.3" diff --git a/third_party/rust/http/v1/crate/LICENSE-APACHE b/third_party/rust/http/v1/crate/LICENSE-APACHE new file mode 100644 index 000000000000..80176c2b2399 --- /dev/null +++ b/third_party/rust/http/v1/crate/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright 2017 http-rs authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/rust/http/v1/crate/LICENSE-MIT b/third_party/rust/http/v1/crate/LICENSE-MIT new file mode 100644 index 000000000000..0cbc55049230 --- /dev/null +++ b/third_party/rust/http/v1/crate/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2017 http-rs authors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/http/v1/crate/README.md b/third_party/rust/http/v1/crate/README.md new file mode 100644 index 000000000000..2ae8d56cd3a5 --- /dev/null +++ b/third_party/rust/http/v1/crate/README.md @@ -0,0 +1,80 @@ +# HTTP + +A general purpose library of common HTTP types + +[![CI](https://github.com/hyperium/http/workflows/CI/badge.svg)](https://github.com/hyperium/http/actions?query=workflow%3ACI) +[![Crates.io](https://img.shields.io/crates/v/http.svg)](https://crates.io/crates/http) +[![Documentation](https://docs.rs/http/badge.svg)][dox] + +More information about this crate can be found in the [crate +documentation][dox]. + +[dox]: https://docs.rs/http + +## Usage + +To use `http`, first add this to your `Cargo.toml`: + +```toml +[dependencies] +http = "0.2" +``` + +Next, add this to your crate: + +```rust +use http::{Request, Response}; + +fn main() { + // ... +} +``` + +## Examples + +Create an HTTP request: + +```rust +use http::Request; + +fn main() { + let request = Request::builder() + .uri("https://www.rust-lang.org/") + .header("User-Agent", "awesome/1.0") + .body(()) + .unwrap(); +} +``` + +Create an HTTP response: + +```rust +use http::{Response, StatusCode}; + +fn main() { + let response = Response::builder() + .status(StatusCode::MOVED_PERMANENTLY) + .header("Location", "https://www.rust-lang.org/install.html") + .body(()) + .unwrap(); +} +``` + +# Supported Rust Versions + +This project follows the [Tokio MSRV][msrv] and is currently set to `1.49`. + +[msrv]: https://github.com/tokio-rs/tokio/#supported-rust-versions + +# License + +Licensed under either of + +- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or https://apache.org/licenses/LICENSE-2.0) +- MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT) + +# Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. diff --git a/third_party/rust/http/v1/crate/src/byte_str.rs b/third_party/rust/http/v1/crate/src/byte_str.rs new file mode 100644 index 000000000000..dec222b5f45b --- /dev/null +++ b/third_party/rust/http/v1/crate/src/byte_str.rs @@ -0,0 +1,85 @@ +use bytes::Bytes; + +use std::{ops, str}; + +#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub(crate) struct ByteStr { + // Invariant: bytes contains valid UTF-8 + bytes: Bytes, +} + +impl ByteStr { + #[inline] + pub fn new() -> ByteStr { + ByteStr { + // Invariant: the empty slice is trivially valid UTF-8. + bytes: Bytes::new(), + } + } + + #[inline] + pub const fn from_static(val: &'static str) -> ByteStr { + ByteStr { + // Invariant: val is a str so contains valid UTF-8. + bytes: Bytes::from_static(val.as_bytes()), + } + } + + #[inline] + /// ## Panics + /// In a debug build this will panic if `bytes` is not valid UTF-8. + /// + /// ## Safety + /// `bytes` must contain valid UTF-8. In a release build it is undefined + /// behaviour to call this with `bytes` that is not valid UTF-8. + pub unsafe fn from_utf8_unchecked(bytes: Bytes) -> ByteStr { + if cfg!(debug_assertions) { + match str::from_utf8(&bytes) { + Ok(_) => (), + Err(err) => panic!( + "ByteStr::from_utf8_unchecked() with invalid bytes; error = {}, bytes = {:?}", + err, bytes + ), + } + } + // Invariant: assumed by the safety requirements of this function. + ByteStr { bytes: bytes } + } +} + +impl ops::Deref for ByteStr { + type Target = str; + + #[inline] + fn deref(&self) -> &str { + let b: &[u8] = self.bytes.as_ref(); + // Safety: the invariant of `bytes` is that it contains valid UTF-8. + unsafe { str::from_utf8_unchecked(b) } + } +} + +impl From for ByteStr { + #[inline] + fn from(src: String) -> ByteStr { + ByteStr { + // Invariant: src is a String so contains valid UTF-8. + bytes: Bytes::from(src), + } + } +} + +impl<'a> From<&'a str> for ByteStr { + #[inline] + fn from(src: &'a str) -> ByteStr { + ByteStr { + // Invariant: src is a str so contains valid UTF-8. + bytes: Bytes::copy_from_slice(src.as_bytes()), + } + } +} + +impl From for Bytes { + fn from(src: ByteStr) -> Self { + src.bytes + } +} diff --git a/third_party/rust/http/v1/crate/src/convert.rs b/third_party/rust/http/v1/crate/src/convert.rs new file mode 100644 index 000000000000..aeee2219a965 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/convert.rs @@ -0,0 +1,17 @@ +macro_rules! if_downcast_into { + ($in_ty:ty, $out_ty:ty, $val:ident, $body:expr) => ({ + if std::any::TypeId::of::<$in_ty>() == std::any::TypeId::of::<$out_ty>() { + // Store the value in an `Option` so we can `take` + // it after casting to `&mut dyn Any`. + let mut slot = Some($val); + // Re-write the `$val` ident with the downcasted value. + let $val = (&mut slot as &mut dyn std::any::Any) + .downcast_mut::>() + .unwrap() + .take() + .unwrap(); + // Run the $body in scope of the replaced val. + $body + } + }) +} diff --git a/third_party/rust/http/v1/crate/src/error.rs b/third_party/rust/http/v1/crate/src/error.rs new file mode 100644 index 000000000000..ba690841b697 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/error.rs @@ -0,0 +1,149 @@ +use std::error; +use std::fmt; +use std::result; + +use crate::header; +use crate::method; +use crate::status; +use crate::uri; + +/// A generic "error" for HTTP connections +/// +/// This error type is less specific than the error returned from other +/// functions in this crate, but all other errors can be converted to this +/// error. Consumers of this crate can typically consume and work with this form +/// of error for conversions with the `?` operator. +pub struct Error { + inner: ErrorKind, +} + +/// A `Result` typedef to use with the `http::Error` type +pub type Result = result::Result; + +enum ErrorKind { + StatusCode(status::InvalidStatusCode), + Method(method::InvalidMethod), + Uri(uri::InvalidUri), + UriParts(uri::InvalidUriParts), + HeaderName(header::InvalidHeaderName), + HeaderValue(header::InvalidHeaderValue), +} + +impl fmt::Debug for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("http::Error") + // Skip the noise of the ErrorKind enum + .field(&self.get_ref()) + .finish() + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.get_ref(), f) + } +} + +impl Error { + /// Return true if the underlying error has the same type as T. + pub fn is(&self) -> bool { + self.get_ref().is::() + } + + /// Return a reference to the lower level, inner error. + pub fn get_ref(&self) -> &(dyn error::Error + 'static) { + use self::ErrorKind::*; + + match self.inner { + StatusCode(ref e) => e, + Method(ref e) => e, + Uri(ref e) => e, + UriParts(ref e) => e, + HeaderName(ref e) => e, + HeaderValue(ref e) => e, + } + } +} + +impl error::Error for Error { + // Return any available cause from the inner error. Note the inner error is + // not itself the cause. + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + self.get_ref().source() + } +} + +impl From for Error { + fn from(err: status::InvalidStatusCode) -> Error { + Error { + inner: ErrorKind::StatusCode(err), + } + } +} + +impl From for Error { + fn from(err: method::InvalidMethod) -> Error { + Error { + inner: ErrorKind::Method(err), + } + } +} + +impl From for Error { + fn from(err: uri::InvalidUri) -> Error { + Error { + inner: ErrorKind::Uri(err), + } + } +} + +impl From for Error { + fn from(err: uri::InvalidUriParts) -> Error { + Error { + inner: ErrorKind::UriParts(err), + } + } +} + +impl From for Error { + fn from(err: header::InvalidHeaderName) -> Error { + Error { + inner: ErrorKind::HeaderName(err), + } + } +} + +impl From for Error { + fn from(err: header::InvalidHeaderValue) -> Error { + Error { + inner: ErrorKind::HeaderValue(err), + } + } +} + +impl From for Error { + fn from(err: std::convert::Infallible) -> Error { + match err {} + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn inner_error_is_invalid_status_code() { + if let Err(e) = status::StatusCode::from_u16(6666) { + let err: Error = e.into(); + let ie = err.get_ref(); + assert!(!ie.is::()); + assert!(ie.is::()); + ie.downcast_ref::().unwrap(); + + assert!(!err.is::()); + assert!(err.is::()); + } else { + panic!("Bad status allowed!"); + } + } +} diff --git a/third_party/rust/http/v1/crate/src/extensions.rs b/third_party/rust/http/v1/crate/src/extensions.rs new file mode 100644 index 000000000000..71a72134fbcb --- /dev/null +++ b/third_party/rust/http/v1/crate/src/extensions.rs @@ -0,0 +1,289 @@ +use std::any::{Any, TypeId}; +use std::collections::HashMap; +use std::fmt; +use std::hash::{BuildHasherDefault, Hasher}; + +type AnyMap = HashMap, BuildHasherDefault>; + +// With TypeIds as keys, there's no need to hash them. They are already hashes +// themselves, coming from the compiler. The IdHasher just holds the u64 of +// the TypeId, and then returns it, instead of doing any bit fiddling. +#[derive(Default)] +struct IdHasher(u64); + +impl Hasher for IdHasher { + fn write(&mut self, _: &[u8]) { + unreachable!("TypeId calls write_u64"); + } + + #[inline] + fn write_u64(&mut self, id: u64) { + self.0 = id; + } + + #[inline] + fn finish(&self) -> u64 { + self.0 + } +} + +/// A type map of protocol extensions. +/// +/// `Extensions` can be used by `Request` and `Response` to store +/// extra data derived from the underlying protocol. +#[derive(Clone, Default)] +pub struct Extensions { + // If extensions are never used, no need to carry around an empty HashMap. + // That's 3 words. Instead, this is only 1 word. + map: Option>, +} + +impl Extensions { + /// Create an empty `Extensions`. + #[inline] + pub fn new() -> Extensions { + Extensions { map: None } + } + + /// Insert a type into this `Extensions`. + /// + /// If a extension of this type already existed, it will + /// be returned. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// assert!(ext.insert(5i32).is_none()); + /// assert!(ext.insert(4u8).is_none()); + /// assert_eq!(ext.insert(9i32), Some(5i32)); + /// ``` + pub fn insert(&mut self, val: T) -> Option { + self.map + .get_or_insert_with(|| Box::new(HashMap::default())) + .insert(TypeId::of::(), Box::new(val)) + .and_then(|boxed| { + boxed.into_any() + .downcast() + .ok() + .map(|boxed| *boxed) + }) + } + + /// Get a reference to a type previously inserted on this `Extensions`. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// assert!(ext.get::().is_none()); + /// ext.insert(5i32); + /// + /// assert_eq!(ext.get::(), Some(&5i32)); + /// ``` + pub fn get(&self) -> Option<&T> { + self.map + .as_ref() + .and_then(|map| map.get(&TypeId::of::())) + .and_then(|boxed| (&**boxed).as_any().downcast_ref()) + } + + /// Get a mutable reference to a type previously inserted on this `Extensions`. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// ext.insert(String::from("Hello")); + /// ext.get_mut::().unwrap().push_str(" World"); + /// + /// assert_eq!(ext.get::().unwrap(), "Hello World"); + /// ``` + pub fn get_mut(&mut self) -> Option<&mut T> { + self.map + .as_mut() + .and_then(|map| map.get_mut(&TypeId::of::())) + .and_then(|boxed| (&mut **boxed).as_any_mut().downcast_mut()) + } + + /// Remove a type from this `Extensions`. + /// + /// If a extension of this type existed, it will be returned. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// ext.insert(5i32); + /// assert_eq!(ext.remove::(), Some(5i32)); + /// assert!(ext.get::().is_none()); + /// ``` + pub fn remove(&mut self) -> Option { + self.map + .as_mut() + .and_then(|map| map.remove(&TypeId::of::())) + .and_then(|boxed| { + boxed.into_any() + .downcast() + .ok() + .map(|boxed| *boxed) + }) + } + + /// Clear the `Extensions` of all inserted extensions. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// ext.insert(5i32); + /// ext.clear(); + /// + /// assert!(ext.get::().is_none()); + /// ``` + #[inline] + pub fn clear(&mut self) { + if let Some(ref mut map) = self.map { + map.clear(); + } + } + + /// Check whether the extension set is empty or not. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// assert!(ext.is_empty()); + /// ext.insert(5i32); + /// assert!(!ext.is_empty()); + /// ``` + #[inline] + pub fn is_empty(&self) -> bool { + self.map + .as_ref() + .map_or(true, |map| map.is_empty()) + } + + /// Get the numer of extensions available. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext = Extensions::new(); + /// assert_eq!(ext.len(), 0); + /// ext.insert(5i32); + /// assert_eq!(ext.len(), 1); + /// ``` + #[inline] + pub fn len(&self) -> usize { + self.map + .as_ref() + .map_or(0, |map| map.len()) + } + + /// Extends `self` with another `Extensions`. + /// + /// If an instance of a specific type exists in both, the one in `self` is overwritten with the + /// one from `other`. + /// + /// # Example + /// + /// ``` + /// # use http::Extensions; + /// let mut ext_a = Extensions::new(); + /// ext_a.insert(8u8); + /// ext_a.insert(16u16); + /// + /// let mut ext_b = Extensions::new(); + /// ext_b.insert(4u8); + /// ext_b.insert("hello"); + /// + /// ext_a.extend(ext_b); + /// assert_eq!(ext_a.len(), 3); + /// assert_eq!(ext_a.get::(), Some(&4u8)); + /// assert_eq!(ext_a.get::(), Some(&16u16)); + /// assert_eq!(ext_a.get::<&'static str>().copied(), Some("hello")); + /// ``` + pub fn extend(&mut self, other: Self) { + if let Some(other) = other.map { + if let Some(map) = &mut self.map { + map.extend(*other); + } else { + self.map = Some(other); + } + } + } +} + +impl fmt::Debug for Extensions { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Extensions").finish() + } +} + +trait AnyClone: Any { + fn clone_box(&self) -> Box; + fn as_any(&self) -> &dyn Any; + fn as_any_mut(&mut self) -> &mut dyn Any; + fn into_any(self: Box) -> Box; +} + +impl AnyClone for T { + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } + + fn into_any(self: Box) -> Box { + self + } +} + +impl Clone for Box { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + + + +#[test] +fn test_extensions() { + #[derive(Clone, Debug, PartialEq)] + struct MyType(i32); + + let mut extensions = Extensions::new(); + + extensions.insert(5i32); + extensions.insert(MyType(10)); + + assert_eq!(extensions.get(), Some(&5i32)); + assert_eq!(extensions.get_mut(), Some(&mut 5i32)); + + let ext2 = extensions.clone(); + + assert_eq!(extensions.remove::(), Some(5i32)); + assert!(extensions.get::().is_none()); + + // clone still has it + assert_eq!(ext2.get(), Some(&5i32)); + assert_eq!(ext2.get(), Some(&MyType(10))); + + assert_eq!(extensions.get::(), None); + assert_eq!(extensions.get(), Some(&MyType(10))); +} diff --git a/third_party/rust/http/v1/crate/src/header/map.rs b/third_party/rust/http/v1/crate/src/header/map.rs new file mode 100644 index 000000000000..0290160e74d0 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/header/map.rs @@ -0,0 +1,3545 @@ +use std::collections::HashMap; +use std::collections::hash_map::RandomState; +use std::convert::TryFrom; +use std::hash::{BuildHasher, Hash, Hasher}; +use std::iter::{FromIterator, FusedIterator}; +use std::marker::PhantomData; +use std::{fmt, mem, ops, ptr, vec}; + +use crate::Error; + +use super::HeaderValue; +use super::name::{HdrName, HeaderName, InvalidHeaderName}; + +pub use self::as_header_name::AsHeaderName; +pub use self::into_header_name::IntoHeaderName; + +/// A set of HTTP headers +/// +/// `HeaderMap` is an multimap of [`HeaderName`] to values. +/// +/// [`HeaderName`]: struct.HeaderName.html +/// +/// # Examples +/// +/// Basic usage +/// +/// ``` +/// # use http::HeaderMap; +/// # use http::header::{CONTENT_LENGTH, HOST, LOCATION}; +/// let mut headers = HeaderMap::new(); +/// +/// headers.insert(HOST, "example.com".parse().unwrap()); +/// headers.insert(CONTENT_LENGTH, "123".parse().unwrap()); +/// +/// assert!(headers.contains_key(HOST)); +/// assert!(!headers.contains_key(LOCATION)); +/// +/// assert_eq!(headers[HOST], "example.com"); +/// +/// headers.remove(HOST); +/// +/// assert!(!headers.contains_key(HOST)); +/// ``` +#[derive(Clone)] +pub struct HeaderMap { + // Used to mask values to get an index + mask: Size, + indices: Box<[Pos]>, + entries: Vec>, + extra_values: Vec>, + danger: Danger, +} + +// # Implementation notes +// +// Below, you will find a fairly large amount of code. Most of this is to +// provide the necessary functions to efficiently manipulate the header +// multimap. The core hashing table is based on robin hood hashing [1]. While +// this is the same hashing algorithm used as part of Rust's `HashMap` in +// stdlib, many implementation details are different. The two primary reasons +// for this divergence are that `HeaderMap` is a multimap and the structure has +// been optimized to take advantage of the characteristics of HTTP headers. +// +// ## Structure Layout +// +// Most of the data contained by `HeaderMap` is *not* stored in the hash table. +// Instead, pairs of header name and *first* associated header value are stored +// in the `entries` vector. If the header name has more than one associated +// header value, then additional values are stored in `extra_values`. The actual +// hash table (`indices`) only maps hash codes to indices in `entries`. This +// means that, when an eviction happens, the actual header name and value stay +// put and only a tiny amount of memory has to be copied. +// +// Extra values associated with a header name are tracked using a linked list. +// Links are formed with offsets into `extra_values` and not pointers. +// +// [1]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing + +/// `HeaderMap` entry iterator. +/// +/// Yields `(&HeaderName, &value)` tuples. The same header name may be yielded +/// more than once if it has more than one associated value. +#[derive(Debug)] +pub struct Iter<'a, T> { + map: &'a HeaderMap, + entry: usize, + cursor: Option, +} + +/// `HeaderMap` mutable entry iterator +/// +/// Yields `(&HeaderName, &mut value)` tuples. The same header name may be +/// yielded more than once if it has more than one associated value. +#[derive(Debug)] +pub struct IterMut<'a, T> { + map: *mut HeaderMap, + entry: usize, + cursor: Option, + lt: PhantomData<&'a mut HeaderMap>, +} + +/// An owning iterator over the entries of a `HeaderMap`. +/// +/// This struct is created by the `into_iter` method on `HeaderMap`. +#[derive(Debug)] +pub struct IntoIter { + // If None, pull from `entries` + next: Option, + entries: vec::IntoIter>, + extra_values: Vec>, +} + +/// An iterator over `HeaderMap` keys. +/// +/// Each header name is yielded only once, even if it has more than one +/// associated value. +#[derive(Debug)] +pub struct Keys<'a, T> { + inner: ::std::slice::Iter<'a, Bucket>, +} + +/// `HeaderMap` value iterator. +/// +/// Each value contained in the `HeaderMap` will be yielded. +#[derive(Debug)] +pub struct Values<'a, T> { + inner: Iter<'a, T>, +} + +/// `HeaderMap` mutable value iterator +#[derive(Debug)] +pub struct ValuesMut<'a, T> { + inner: IterMut<'a, T>, +} + +/// A drain iterator for `HeaderMap`. +#[derive(Debug)] +pub struct Drain<'a, T> { + idx: usize, + len: usize, + entries: *mut [Bucket], + // If None, pull from `entries` + next: Option, + extra_values: *mut Vec>, + lt: PhantomData<&'a mut HeaderMap>, +} + +/// A view to all values stored in a single entry. +/// +/// This struct is returned by `HeaderMap::get_all`. +#[derive(Debug)] +pub struct GetAll<'a, T> { + map: &'a HeaderMap, + index: Option, +} + +/// A view into a single location in a `HeaderMap`, which may be vacant or occupied. +#[derive(Debug)] +pub enum Entry<'a, T: 'a> { + /// An occupied entry + Occupied(OccupiedEntry<'a, T>), + + /// A vacant entry + Vacant(VacantEntry<'a, T>), +} + +/// A view into a single empty location in a `HeaderMap`. +/// +/// This struct is returned as part of the `Entry` enum. +#[derive(Debug)] +pub struct VacantEntry<'a, T> { + map: &'a mut HeaderMap, + key: HeaderName, + hash: HashValue, + probe: usize, + danger: bool, +} + +/// A view into a single occupied location in a `HeaderMap`. +/// +/// This struct is returned as part of the `Entry` enum. +#[derive(Debug)] +pub struct OccupiedEntry<'a, T> { + map: &'a mut HeaderMap, + probe: usize, + index: usize, +} + +/// An iterator of all values associated with a single header name. +#[derive(Debug)] +pub struct ValueIter<'a, T> { + map: &'a HeaderMap, + index: usize, + front: Option, + back: Option, +} + +/// A mutable iterator of all values associated with a single header name. +#[derive(Debug)] +pub struct ValueIterMut<'a, T> { + map: *mut HeaderMap, + index: usize, + front: Option, + back: Option, + lt: PhantomData<&'a mut HeaderMap>, +} + +/// An drain iterator of all values associated with a single header name. +#[derive(Debug)] +pub struct ValueDrain<'a, T> { + first: Option, + next: Option<::std::vec::IntoIter>, + lt: PhantomData<&'a mut HeaderMap>, +} + +/// Tracks the value iterator state +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +enum Cursor { + Head, + Values(usize), +} + +/// Type used for representing the size of a HeaderMap value. +/// +/// 32,768 is more than enough entries for a single header map. Setting this +/// limit enables using `u16` to represent all offsets, which takes 2 bytes +/// instead of 8 on 64 bit processors. +/// +/// Setting this limit is especially beneficial for `indices`, making it more +/// cache friendly. More hash codes can fit in a cache line. +/// +/// You may notice that `u16` may represent more than 32,768 values. This is +/// true, but 32,768 should be plenty and it allows us to reserve the top bit +/// for future usage. +type Size = u16; + +/// This limit falls out from above. +const MAX_SIZE: usize = 1 << 15; + +/// An entry in the hash table. This represents the full hash code for an entry +/// as well as the position of the entry in the `entries` vector. +#[derive(Copy, Clone)] +struct Pos { + // Index in the `entries` vec + index: Size, + // Full hash value for the entry. + hash: HashValue, +} + +/// Hash values are limited to u16 as well. While `fast_hash` and `Hasher` +/// return `usize` hash codes, limiting the effective hash code to the lower 16 +/// bits is fine since we know that the `indices` vector will never grow beyond +/// that size. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +struct HashValue(u16); + +/// Stores the data associated with a `HeaderMap` entry. Only the first value is +/// included in this struct. If a header name has more than one associated +/// value, all extra values are stored in the `extra_values` vector. A doubly +/// linked list of entries is maintained. The doubly linked list is used so that +/// removing a value is constant time. This also has the nice property of +/// enabling double ended iteration. +#[derive(Debug, Clone)] +struct Bucket { + hash: HashValue, + key: HeaderName, + value: T, + links: Option, +} + +/// The head and tail of the value linked list. +#[derive(Debug, Copy, Clone)] +struct Links { + next: usize, + tail: usize, +} + +/// Access to the `links` value in a slice of buckets. +/// +/// It's important that no other field is accessed, since it may have been +/// freed in a `Drain` iterator. +#[derive(Debug)] +struct RawLinks(*mut [Bucket]); + +/// Node in doubly-linked list of header value entries +#[derive(Debug, Clone)] +struct ExtraValue { + value: T, + prev: Link, + next: Link, +} + +/// A header value node is either linked to another node in the `extra_values` +/// list or it points to an entry in `entries`. The entry in `entries` is the +/// start of the list and holds the associated header name. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +enum Link { + Entry(usize), + Extra(usize), +} + +/// Tracks the header map danger level! This relates to the adaptive hashing +/// algorithm. A HeaderMap starts in the "green" state, when a large number of +/// collisions are detected, it transitions to the yellow state. At this point, +/// the header map will either grow and switch back to the green state OR it +/// will transition to the red state. +/// +/// When in the red state, a safe hashing algorithm is used and all values in +/// the header map have to be rehashed. +#[derive(Clone)] +enum Danger { + Green, + Yellow, + Red(RandomState), +} + +// Constants related to detecting DOS attacks. +// +// Displacement is the number of entries that get shifted when inserting a new +// value. Forward shift is how far the entry gets stored from the ideal +// position. +// +// The current constant values were picked from another implementation. It could +// be that there are different values better suited to the header map case. +const DISPLACEMENT_THRESHOLD: usize = 128; +const FORWARD_SHIFT_THRESHOLD: usize = 512; + +// The default strategy for handling the yellow danger state is to increase the +// header map capacity in order to (hopefully) reduce the number of collisions. +// If growing the hash map would cause the load factor to drop bellow this +// threshold, then instead of growing, the headermap is switched to the red +// danger state and safe hashing is used instead. +const LOAD_FACTOR_THRESHOLD: f32 = 0.2; + +// Macro used to iterate the hash table starting at a given point, looping when +// the end is hit. +macro_rules! probe_loop { + ($label:tt: $probe_var: ident < $len: expr, $body: expr) => { + debug_assert!($len > 0); + $label: + loop { + if $probe_var < $len { + $body + $probe_var += 1; + } else { + $probe_var = 0; + } + } + }; + ($probe_var: ident < $len: expr, $body: expr) => { + debug_assert!($len > 0); + loop { + if $probe_var < $len { + $body + $probe_var += 1; + } else { + $probe_var = 0; + } + } + }; +} + +// First part of the robinhood algorithm. Given a key, find the slot in which it +// will be inserted. This is done by starting at the "ideal" spot. Then scanning +// until the destination slot is found. A destination slot is either the next +// empty slot or the next slot that is occupied by an entry that has a lower +// displacement (displacement is the distance from the ideal spot). +// +// This is implemented as a macro instead of a function that takes a closure in +// order to guarantee that it is "inlined". There is no way to annotate closures +// to guarantee inlining. +macro_rules! insert_phase_one { + ($map:ident, + $key:expr, + $probe:ident, + $pos:ident, + $hash:ident, + $danger:ident, + $vacant:expr, + $occupied:expr, + $robinhood:expr) => + {{ + let $hash = hash_elem_using(&$map.danger, &$key); + let mut $probe = desired_pos($map.mask, $hash); + let mut dist = 0; + let ret; + + // Start at the ideal position, checking all slots + probe_loop!('probe: $probe < $map.indices.len(), { + if let Some(($pos, entry_hash)) = $map.indices[$probe].resolve() { + // The slot is already occupied, but check if it has a lower + // displacement. + let their_dist = probe_distance($map.mask, entry_hash, $probe); + + if their_dist < dist { + // The new key's distance is larger, so claim this spot and + // displace the current entry. + // + // Check if this insertion is above the danger threshold. + let $danger = + dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red(); + + ret = $robinhood; + break 'probe; + } else if entry_hash == $hash && $map.entries[$pos].key == $key { + // There already is an entry with the same key. + ret = $occupied; + break 'probe; + } + } else { + // The entry is vacant, use it for this key. + let $danger = + dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red(); + + ret = $vacant; + break 'probe; + } + + dist += 1; + }); + + ret + }} +} + +// ===== impl HeaderMap ===== + +impl HeaderMap { + /// Create an empty `HeaderMap`. + /// + /// The map will be created without any capacity. This function will not + /// allocate. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// let map = HeaderMap::new(); + /// + /// assert!(map.is_empty()); + /// assert_eq!(0, map.capacity()); + /// ``` + pub fn new() -> Self { + HeaderMap::with_capacity(0) + } +} + +impl HeaderMap { + /// Create an empty `HeaderMap` with the specified capacity. + /// + /// The returned map will allocate internal storage in order to hold about + /// `capacity` elements without reallocating. However, this is a "best + /// effort" as there are usage patterns that could cause additional + /// allocations before `capacity` headers are stored in the map. + /// + /// More capacity than requested may be allocated. + /// + /// # Panics + /// + /// Requested capacity too large: would overflow `usize`. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// let map: HeaderMap = HeaderMap::with_capacity(10); + /// + /// assert!(map.is_empty()); + /// assert_eq!(12, map.capacity()); + /// ``` + pub fn with_capacity(capacity: usize) -> HeaderMap { + if capacity == 0 { + HeaderMap { + mask: 0, + indices: Box::new([]), // as a ZST, this doesn't actually allocate anything + entries: Vec::new(), + extra_values: Vec::new(), + danger: Danger::Green, + } + } else { + let raw_cap = match to_raw_capacity(capacity).checked_next_power_of_two() { + Some(c) => c, + None => panic!( + "requested capacity {} too large: next power of two would overflow `usize`", + capacity + ), + }; + assert!(raw_cap <= MAX_SIZE, "requested capacity too large"); + debug_assert!(raw_cap > 0); + + HeaderMap { + mask: (raw_cap - 1) as Size, + indices: vec![Pos::none(); raw_cap].into_boxed_slice(), + entries: Vec::with_capacity(raw_cap), + extra_values: Vec::new(), + danger: Danger::Green, + } + } + } + + /// Returns the number of headers stored in the map. + /// + /// This number represents the total number of **values** stored in the map. + /// This number can be greater than or equal to the number of **keys** + /// stored given that a single key may have more than one associated value. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{ACCEPT, HOST}; + /// let mut map = HeaderMap::new(); + /// + /// assert_eq!(0, map.len()); + /// + /// map.insert(ACCEPT, "text/plain".parse().unwrap()); + /// map.insert(HOST, "localhost".parse().unwrap()); + /// + /// assert_eq!(2, map.len()); + /// + /// map.append(ACCEPT, "text/html".parse().unwrap()); + /// + /// assert_eq!(3, map.len()); + /// ``` + pub fn len(&self) -> usize { + self.entries.len() + self.extra_values.len() + } + + /// Returns the number of keys stored in the map. + /// + /// This number will be less than or equal to `len()` as each key may have + /// more than one associated value. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{ACCEPT, HOST}; + /// let mut map = HeaderMap::new(); + /// + /// assert_eq!(0, map.keys_len()); + /// + /// map.insert(ACCEPT, "text/plain".parse().unwrap()); + /// map.insert(HOST, "localhost".parse().unwrap()); + /// + /// assert_eq!(2, map.keys_len()); + /// + /// map.insert(ACCEPT, "text/html".parse().unwrap()); + /// + /// assert_eq!(2, map.keys_len()); + /// ``` + pub fn keys_len(&self) -> usize { + self.entries.len() + } + + /// Returns true if the map contains no elements. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// + /// assert!(map.is_empty()); + /// + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// + /// assert!(!map.is_empty()); + /// ``` + pub fn is_empty(&self) -> bool { + self.entries.len() == 0 + } + + /// Clears the map, removing all key-value pairs. Keeps the allocated memory + /// for reuse. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// + /// map.clear(); + /// assert!(map.is_empty()); + /// assert!(map.capacity() > 0); + /// ``` + pub fn clear(&mut self) { + self.entries.clear(); + self.extra_values.clear(); + self.danger = Danger::Green; + + for e in self.indices.iter_mut() { + *e = Pos::none(); + } + } + + /// Returns the number of headers the map can hold without reallocating. + /// + /// This number is an approximation as certain usage patterns could cause + /// additional allocations before the returned capacity is filled. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// + /// assert_eq!(0, map.capacity()); + /// + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// assert_eq!(6, map.capacity()); + /// ``` + pub fn capacity(&self) -> usize { + usable_capacity(self.indices.len()) + } + + /// Reserves capacity for at least `additional` more headers to be inserted + /// into the `HeaderMap`. + /// + /// The header map may reserve more space to avoid frequent reallocations. + /// Like with `with_capacity`, this will be a "best effort" to avoid + /// allocations until `additional` more headers are inserted. Certain usage + /// patterns could cause additional allocations before the number is + /// reached. + /// + /// # Panics + /// + /// Panics if the new allocation size overflows `usize`. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// map.reserve(10); + /// # map.insert(HOST, "bar".parse().unwrap()); + /// ``` + pub fn reserve(&mut self, additional: usize) { + // TODO: This can't overflow if done properly... since the max # of + // elements is u16::MAX. + let cap = self + .entries + .len() + .checked_add(additional) + .expect("reserve overflow"); + + if cap > self.indices.len() { + let cap = cap.next_power_of_two(); + assert!(cap <= MAX_SIZE, "header map reserve over max capacity"); + assert!(cap != 0, "header map reserve overflowed"); + + if self.entries.len() == 0 { + self.mask = cap as Size - 1; + self.indices = vec![Pos::none(); cap].into_boxed_slice(); + self.entries = Vec::with_capacity(usable_capacity(cap)); + } else { + self.grow(cap); + } + } + } + + /// Returns a reference to the value associated with the key. + /// + /// If there are multiple values associated with the key, then the first one + /// is returned. Use `get_all` to get all values associated with a given + /// key. Returns `None` if there are no values associated with the key. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// assert!(map.get("host").is_none()); + /// + /// map.insert(HOST, "hello".parse().unwrap()); + /// assert_eq!(map.get(HOST).unwrap(), &"hello"); + /// assert_eq!(map.get("host").unwrap(), &"hello"); + /// + /// map.append(HOST, "world".parse().unwrap()); + /// assert_eq!(map.get("host").unwrap(), &"hello"); + /// ``` + pub fn get(&self, key: K) -> Option<&T> + where + K: AsHeaderName, + { + self.get2(&key) + } + + fn get2(&self, key: &K) -> Option<&T> + where + K: AsHeaderName, + { + match key.find(self) { + Some((_, found)) => { + let entry = &self.entries[found]; + Some(&entry.value) + } + None => None, + } + } + + /// Returns a mutable reference to the value associated with the key. + /// + /// If there are multiple values associated with the key, then the first one + /// is returned. Use `entry` to get all values associated with a given + /// key. Returns `None` if there are no values associated with the key. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::default(); + /// map.insert(HOST, "hello".to_string()); + /// map.get_mut("host").unwrap().push_str("-world"); + /// + /// assert_eq!(map.get(HOST).unwrap(), &"hello-world"); + /// ``` + pub fn get_mut(&mut self, key: K) -> Option<&mut T> + where + K: AsHeaderName, + { + match key.find(self) { + Some((_, found)) => { + let entry = &mut self.entries[found]; + Some(&mut entry.value) + } + None => None, + } + } + + /// Returns a view of all values associated with a key. + /// + /// The returned view does not incur any allocations and allows iterating + /// the values associated with the key. See [`GetAll`] for more details. + /// Returns `None` if there are no values associated with the key. + /// + /// [`GetAll`]: struct.GetAll.html + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// + /// map.insert(HOST, "hello".parse().unwrap()); + /// map.append(HOST, "goodbye".parse().unwrap()); + /// + /// let view = map.get_all("host"); + /// + /// let mut iter = view.iter(); + /// assert_eq!(&"hello", iter.next().unwrap()); + /// assert_eq!(&"goodbye", iter.next().unwrap()); + /// assert!(iter.next().is_none()); + /// ``` + pub fn get_all(&self, key: K) -> GetAll<'_, T> + where + K: AsHeaderName, + { + GetAll { + map: self, + index: key.find(self).map(|(_, i)| i), + } + } + + /// Returns true if the map contains a value for the specified key. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// assert!(!map.contains_key(HOST)); + /// + /// map.insert(HOST, "world".parse().unwrap()); + /// assert!(map.contains_key("host")); + /// ``` + pub fn contains_key(&self, key: K) -> bool + where + K: AsHeaderName, + { + key.find(self).is_some() + } + + /// An iterator visiting all key-value pairs. + /// + /// The iteration order is arbitrary, but consistent across platforms for + /// the same crate version. Each key will be yielded once per associated + /// value. So, if a key has 3 associated values, it will be yielded 3 times. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{CONTENT_LENGTH, HOST}; + /// let mut map = HeaderMap::new(); + /// + /// map.insert(HOST, "hello".parse().unwrap()); + /// map.append(HOST, "goodbye".parse().unwrap()); + /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); + /// + /// for (key, value) in map.iter() { + /// println!("{:?}: {:?}", key, value); + /// } + /// ``` + pub fn iter(&self) -> Iter<'_, T> { + Iter { + map: self, + entry: 0, + cursor: self.entries.first().map(|_| Cursor::Head), + } + } + + /// An iterator visiting all key-value pairs, with mutable value references. + /// + /// The iterator order is arbitrary, but consistent across platforms for the + /// same crate version. Each key will be yielded once per associated value, + /// so if a key has 3 associated values, it will be yielded 3 times. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{CONTENT_LENGTH, HOST}; + /// let mut map = HeaderMap::default(); + /// + /// map.insert(HOST, "hello".to_string()); + /// map.append(HOST, "goodbye".to_string()); + /// map.insert(CONTENT_LENGTH, "123".to_string()); + /// + /// for (key, value) in map.iter_mut() { + /// value.push_str("-boop"); + /// } + /// ``` + pub fn iter_mut(&mut self) -> IterMut<'_, T> { + IterMut { + map: self as *mut _, + entry: 0, + cursor: self.entries.first().map(|_| Cursor::Head), + lt: PhantomData, + } + } + + /// An iterator visiting all keys. + /// + /// The iteration order is arbitrary, but consistent across platforms for + /// the same crate version. Each key will be yielded only once even if it + /// has multiple associated values. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{CONTENT_LENGTH, HOST}; + /// let mut map = HeaderMap::new(); + /// + /// map.insert(HOST, "hello".parse().unwrap()); + /// map.append(HOST, "goodbye".parse().unwrap()); + /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); + /// + /// for key in map.keys() { + /// println!("{:?}", key); + /// } + /// ``` + pub fn keys(&self) -> Keys<'_, T> { + Keys { + inner: self.entries.iter(), + } + } + + /// An iterator visiting all values. + /// + /// The iteration order is arbitrary, but consistent across platforms for + /// the same crate version. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{CONTENT_LENGTH, HOST}; + /// let mut map = HeaderMap::new(); + /// + /// map.insert(HOST, "hello".parse().unwrap()); + /// map.append(HOST, "goodbye".parse().unwrap()); + /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); + /// + /// for value in map.values() { + /// println!("{:?}", value); + /// } + /// ``` + pub fn values(&self) -> Values<'_, T> { + Values { inner: self.iter() } + } + + /// An iterator visiting all values mutably. + /// + /// The iteration order is arbitrary, but consistent across platforms for + /// the same crate version. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{CONTENT_LENGTH, HOST}; + /// let mut map = HeaderMap::default(); + /// + /// map.insert(HOST, "hello".to_string()); + /// map.append(HOST, "goodbye".to_string()); + /// map.insert(CONTENT_LENGTH, "123".to_string()); + /// + /// for value in map.values_mut() { + /// value.push_str("-boop"); + /// } + /// ``` + pub fn values_mut(&mut self) -> ValuesMut<'_, T> { + ValuesMut { + inner: self.iter_mut(), + } + } + + /// Clears the map, returning all entries as an iterator. + /// + /// The internal memory is kept for reuse. + /// + /// For each yielded item that has `None` provided for the `HeaderName`, + /// then the associated header name is the same as that of the previously + /// yielded item. The first yielded item will have `HeaderName` set. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::{CONTENT_LENGTH, HOST}; + /// let mut map = HeaderMap::new(); + /// + /// map.insert(HOST, "hello".parse().unwrap()); + /// map.append(HOST, "goodbye".parse().unwrap()); + /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); + /// + /// let mut drain = map.drain(); + /// + /// + /// assert_eq!(drain.next(), Some((Some(HOST), "hello".parse().unwrap()))); + /// assert_eq!(drain.next(), Some((None, "goodbye".parse().unwrap()))); + /// + /// assert_eq!(drain.next(), Some((Some(CONTENT_LENGTH), "123".parse().unwrap()))); + /// + /// assert_eq!(drain.next(), None); + /// ``` + pub fn drain(&mut self) -> Drain<'_, T> { + for i in self.indices.iter_mut() { + *i = Pos::none(); + } + + // Memory safety + // + // When the Drain is first created, it shortens the length of + // the source vector to make sure no uninitialized or moved-from + // elements are accessible at all if the Drain's destructor never + // gets to run. + + let entries = &mut self.entries[..] as *mut _; + let extra_values = &mut self.extra_values as *mut _; + let len = self.entries.len(); + unsafe { self.entries.set_len(0); } + + Drain { + idx: 0, + len, + entries, + extra_values, + next: None, + lt: PhantomData, + } + } + + fn value_iter(&self, idx: Option) -> ValueIter<'_, T> { + use self::Cursor::*; + + if let Some(idx) = idx { + let back = { + let entry = &self.entries[idx]; + + entry.links.map(|l| Values(l.tail)).unwrap_or(Head) + }; + + ValueIter { + map: self, + index: idx, + front: Some(Head), + back: Some(back), + } + } else { + ValueIter { + map: self, + index: ::std::usize::MAX, + front: None, + back: None, + } + } + } + + fn value_iter_mut(&mut self, idx: usize) -> ValueIterMut<'_, T> { + use self::Cursor::*; + + let back = { + let entry = &self.entries[idx]; + + entry.links.map(|l| Values(l.tail)).unwrap_or(Head) + }; + + ValueIterMut { + map: self as *mut _, + index: idx, + front: Some(Head), + back: Some(back), + lt: PhantomData, + } + } + + /// Gets the given key's corresponding entry in the map for in-place + /// manipulation. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// let mut map: HeaderMap = HeaderMap::default(); + /// + /// let headers = &[ + /// "content-length", + /// "x-hello", + /// "Content-Length", + /// "x-world", + /// ]; + /// + /// for &header in headers { + /// let counter = map.entry(header).or_insert(0); + /// *counter += 1; + /// } + /// + /// assert_eq!(map["content-length"], 2); + /// assert_eq!(map["x-hello"], 1); + /// ``` + pub fn entry(&mut self, key: K) -> Entry<'_, T> + where + K: IntoHeaderName, + { + key.entry(self) + } + + /// Gets the given key's corresponding entry in the map for in-place + /// manipulation. + /// + /// # Errors + /// + /// This method differs from `entry` by allowing types that may not be + /// valid `HeaderName`s to passed as the key (such as `String`). If they + /// do not parse as a valid `HeaderName`, this returns an + /// `InvalidHeaderName` error. + pub fn try_entry(&mut self, key: K) -> Result, InvalidHeaderName> + where + K: AsHeaderName, + { + key.try_entry(self) + } + + fn entry2(&mut self, key: K) -> Entry<'_, T> + where + K: Hash + Into, + HeaderName: PartialEq, + { + // Ensure that there is space in the map + self.reserve_one(); + + insert_phase_one!( + self, + key, + probe, + pos, + hash, + danger, + Entry::Vacant(VacantEntry { + map: self, + hash: hash, + key: key.into(), + probe: probe, + danger: danger, + }), + Entry::Occupied(OccupiedEntry { + map: self, + index: pos, + probe: probe, + }), + Entry::Vacant(VacantEntry { + map: self, + hash: hash, + key: key.into(), + probe: probe, + danger: danger, + }) + ) + } + + /// Inserts a key-value pair into the map. + /// + /// If the map did not previously have this key present, then `None` is + /// returned. + /// + /// If the map did have this key present, the new value is associated with + /// the key and all previous values are removed. **Note** that only a single + /// one of the previous values is returned. If there are multiple values + /// that have been previously associated with the key, then the first one is + /// returned. See `insert_mult` on `OccupiedEntry` for an API that returns + /// all values. + /// + /// The key is not updated, though; this matters for types that can be `==` + /// without being identical. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none()); + /// assert!(!map.is_empty()); + /// + /// let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap(); + /// assert_eq!("world", prev); + /// ``` + pub fn insert(&mut self, key: K, val: T) -> Option + where + K: IntoHeaderName, + { + key.insert(self, val) + } + + #[inline] + fn insert2(&mut self, key: K, value: T) -> Option + where + K: Hash + Into, + HeaderName: PartialEq, + { + self.reserve_one(); + + insert_phase_one!( + self, + key, + probe, + pos, + hash, + danger, + // Vacant + { + let _ = danger; // Make lint happy + let index = self.entries.len(); + self.insert_entry(hash, key.into(), value); + self.indices[probe] = Pos::new(index, hash); + None + }, + // Occupied + Some(self.insert_occupied(pos, value)), + // Robinhood + { + self.insert_phase_two(key.into(), value, hash, probe, danger); + None + } + ) + } + + /// Set an occupied bucket to the given value + #[inline] + fn insert_occupied(&mut self, index: usize, value: T) -> T { + if let Some(links) = self.entries[index].links { + self.remove_all_extra_values(links.next); + } + + let entry = &mut self.entries[index]; + mem::replace(&mut entry.value, value) + } + + fn insert_occupied_mult(&mut self, index: usize, value: T) -> ValueDrain<'_, T> { + let old; + let links; + + { + let entry = &mut self.entries[index]; + + old = mem::replace(&mut entry.value, value); + links = entry.links.take(); + } + + let raw_links = self.raw_links(); + let extra_values = &mut self.extra_values; + + let next = links.map(|l| { + drain_all_extra_values(raw_links, extra_values, l.next) + .into_iter() + }); + + ValueDrain { + first: Some(old), + next: next, + lt: PhantomData, + } + } + + /// Inserts a key-value pair into the map. + /// + /// If the map did not previously have this key present, then `false` is + /// returned. + /// + /// If the map did have this key present, the new value is pushed to the end + /// of the list of values currently associated with the key. The key is not + /// updated, though; this matters for types that can be `==` without being + /// identical. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none()); + /// assert!(!map.is_empty()); + /// + /// map.append(HOST, "earth".parse().unwrap()); + /// + /// let values = map.get_all("host"); + /// let mut i = values.iter(); + /// assert_eq!("world", *i.next().unwrap()); + /// assert_eq!("earth", *i.next().unwrap()); + /// ``` + pub fn append(&mut self, key: K, value: T) -> bool + where + K: IntoHeaderName, + { + key.append(self, value) + } + + #[inline] + fn append2(&mut self, key: K, value: T) -> bool + where + K: Hash + Into, + HeaderName: PartialEq, + { + self.reserve_one(); + + insert_phase_one!( + self, + key, + probe, + pos, + hash, + danger, + // Vacant + { + let _ = danger; + let index = self.entries.len(); + self.insert_entry(hash, key.into(), value); + self.indices[probe] = Pos::new(index, hash); + false + }, + // Occupied + { + append_value(pos, &mut self.entries[pos], &mut self.extra_values, value); + true + }, + // Robinhood + { + self.insert_phase_two(key.into(), value, hash, probe, danger); + + false + } + ) + } + + #[inline] + fn find(&self, key: &K) -> Option<(usize, usize)> + where + K: Hash + Into, + HeaderName: PartialEq, + { + if self.entries.is_empty() { + return None; + } + + let hash = hash_elem_using(&self.danger, key); + let mask = self.mask; + let mut probe = desired_pos(mask, hash); + let mut dist = 0; + + probe_loop!(probe < self.indices.len(), { + if let Some((i, entry_hash)) = self.indices[probe].resolve() { + if dist > probe_distance(mask, entry_hash, probe) { + // give up when probe distance is too long + return None; + } else if entry_hash == hash && self.entries[i].key == *key { + return Some((probe, i)); + } + } else { + return None; + } + + dist += 1; + }); + } + + /// phase 2 is post-insert where we forward-shift `Pos` in the indices. + #[inline] + fn insert_phase_two( + &mut self, + key: HeaderName, + value: T, + hash: HashValue, + probe: usize, + danger: bool, + ) -> usize { + // Push the value and get the index + let index = self.entries.len(); + self.insert_entry(hash, key, value); + + let num_displaced = do_insert_phase_two(&mut self.indices, probe, Pos::new(index, hash)); + + if danger || num_displaced >= DISPLACEMENT_THRESHOLD { + // Increase danger level + self.danger.to_yellow(); + } + + index + } + + /// Removes a key from the map, returning the value associated with the key. + /// + /// Returns `None` if the map does not contain the key. If there are + /// multiple values associated with the key, then the first one is returned. + /// See `remove_entry_mult` on `OccupiedEntry` for an API that yields all + /// values. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// + /// let prev = map.remove(HOST).unwrap(); + /// assert_eq!("hello.world", prev); + /// + /// assert!(map.remove(HOST).is_none()); + /// ``` + pub fn remove(&mut self, key: K) -> Option + where + K: AsHeaderName, + { + match key.find(self) { + Some((probe, idx)) => { + if let Some(links) = self.entries[idx].links { + self.remove_all_extra_values(links.next); + } + + let entry = self.remove_found(probe, idx); + + Some(entry.value) + } + None => None, + } + } + + /// Remove an entry from the map. + /// + /// Warning: To avoid inconsistent state, extra values _must_ be removed + /// for the `found` index (via `remove_all_extra_values` or similar) + /// _before_ this method is called. + #[inline] + fn remove_found(&mut self, probe: usize, found: usize) -> Bucket { + // index `probe` and entry `found` is to be removed + // use swap_remove, but then we need to update the index that points + // to the other entry that has to move + self.indices[probe] = Pos::none(); + let entry = self.entries.swap_remove(found); + + // correct index that points to the entry that had to swap places + if let Some(entry) = self.entries.get(found) { + // was not last element + // examine new element in `found` and find it in indices + let mut probe = desired_pos(self.mask, entry.hash); + + probe_loop!(probe < self.indices.len(), { + if let Some((i, _)) = self.indices[probe].resolve() { + if i >= self.entries.len() { + // found it + self.indices[probe] = Pos::new(found, entry.hash); + break; + } + } + }); + + // Update links + if let Some(links) = entry.links { + self.extra_values[links.next].prev = Link::Entry(found); + self.extra_values[links.tail].next = Link::Entry(found); + } + } + + // backward shift deletion in self.indices + // after probe, shift all non-ideally placed indices backward + if self.entries.len() > 0 { + let mut last_probe = probe; + let mut probe = probe + 1; + + probe_loop!(probe < self.indices.len(), { + if let Some((_, entry_hash)) = self.indices[probe].resolve() { + if probe_distance(self.mask, entry_hash, probe) > 0 { + self.indices[last_probe] = self.indices[probe]; + self.indices[probe] = Pos::none(); + } else { + break; + } + } else { + break; + } + + last_probe = probe; + }); + } + + entry + } + + /// Removes the `ExtraValue` at the given index. + #[inline] + fn remove_extra_value(&mut self, idx: usize) -> ExtraValue { + let raw_links = self.raw_links(); + remove_extra_value(raw_links, &mut self.extra_values, idx) + } + + fn remove_all_extra_values(&mut self, mut head: usize) { + loop { + let extra = self.remove_extra_value(head); + + if let Link::Extra(idx) = extra.next { + head = idx; + } else { + break; + } + } + } + + #[inline] + fn insert_entry(&mut self, hash: HashValue, key: HeaderName, value: T) { + assert!(self.entries.len() < MAX_SIZE, "header map at capacity"); + + self.entries.push(Bucket { + hash: hash, + key: key, + value: value, + links: None, + }); + } + + fn rebuild(&mut self) { + // Loop over all entries and re-insert them into the map + 'outer: for (index, entry) in self.entries.iter_mut().enumerate() { + let hash = hash_elem_using(&self.danger, &entry.key); + let mut probe = desired_pos(self.mask, hash); + let mut dist = 0; + + // Update the entry's hash code + entry.hash = hash; + + probe_loop!(probe < self.indices.len(), { + if let Some((_, entry_hash)) = self.indices[probe].resolve() { + // if existing element probed less than us, swap + let their_dist = probe_distance(self.mask, entry_hash, probe); + + if their_dist < dist { + // Robinhood + break; + } + } else { + // Vacant slot + self.indices[probe] = Pos::new(index, hash); + continue 'outer; + } + + dist += 1; + }); + + do_insert_phase_two(&mut self.indices, probe, Pos::new(index, hash)); + } + } + + fn reinsert_entry_in_order(&mut self, pos: Pos) { + if let Some((_, entry_hash)) = pos.resolve() { + // Find first empty bucket and insert there + let mut probe = desired_pos(self.mask, entry_hash); + + probe_loop!(probe < self.indices.len(), { + if self.indices[probe].resolve().is_none() { + // empty bucket, insert here + self.indices[probe] = pos; + return; + } + }); + } + } + + fn reserve_one(&mut self) { + let len = self.entries.len(); + + if self.danger.is_yellow() { + let load_factor = self.entries.len() as f32 / self.indices.len() as f32; + + if load_factor >= LOAD_FACTOR_THRESHOLD { + // Transition back to green danger level + self.danger.to_green(); + + // Double the capacity + let new_cap = self.indices.len() * 2; + + // Grow the capacity + self.grow(new_cap); + } else { + self.danger.to_red(); + + // Rebuild hash table + for index in self.indices.iter_mut() { + *index = Pos::none(); + } + + self.rebuild(); + } + } else if len == self.capacity() { + if len == 0 { + let new_raw_cap = 8; + self.mask = 8 - 1; + self.indices = vec![Pos::none(); new_raw_cap].into_boxed_slice(); + self.entries = Vec::with_capacity(usable_capacity(new_raw_cap)); + } else { + let raw_cap = self.indices.len(); + self.grow(raw_cap << 1); + } + } + } + + #[inline] + fn grow(&mut self, new_raw_cap: usize) { + assert!(new_raw_cap <= MAX_SIZE, "requested capacity too large"); + // This path can never be reached when handling the first allocation in + // the map. + + // find first ideally placed element -- start of cluster + let mut first_ideal = 0; + + for (i, pos) in self.indices.iter().enumerate() { + if let Some((_, entry_hash)) = pos.resolve() { + if 0 == probe_distance(self.mask, entry_hash, i) { + first_ideal = i; + break; + } + } + } + + // visit the entries in an order where we can simply reinsert them + // into self.indices without any bucket stealing. + let old_indices = mem::replace( + &mut self.indices, + vec![Pos::none(); new_raw_cap].into_boxed_slice(), + ); + self.mask = new_raw_cap.wrapping_sub(1) as Size; + + for &pos in &old_indices[first_ideal..] { + self.reinsert_entry_in_order(pos); + } + + for &pos in &old_indices[..first_ideal] { + self.reinsert_entry_in_order(pos); + } + + // Reserve additional entry slots + let more = self.capacity() - self.entries.len(); + self.entries.reserve_exact(more); + } + + #[inline] + fn raw_links(&mut self) -> RawLinks { + RawLinks(&mut self.entries[..] as *mut _) + } +} + +/// Removes the `ExtraValue` at the given index. +#[inline] +fn remove_extra_value( + mut raw_links: RawLinks, + extra_values: &mut Vec>, + idx: usize) + -> ExtraValue +{ + let prev; + let next; + + { + debug_assert!(extra_values.len() > idx); + let extra = &extra_values[idx]; + prev = extra.prev; + next = extra.next; + } + + // First unlink the extra value + match (prev, next) { + (Link::Entry(prev), Link::Entry(next)) => { + debug_assert_eq!(prev, next); + + raw_links[prev] = None; + } + (Link::Entry(prev), Link::Extra(next)) => { + debug_assert!(raw_links[prev].is_some()); + + raw_links[prev].as_mut().unwrap() + .next = next; + + debug_assert!(extra_values.len() > next); + extra_values[next].prev = Link::Entry(prev); + } + (Link::Extra(prev), Link::Entry(next)) => { + debug_assert!(raw_links[next].is_some()); + + raw_links[next].as_mut().unwrap() + .tail = prev; + + debug_assert!(extra_values.len() > prev); + extra_values[prev].next = Link::Entry(next); + } + (Link::Extra(prev), Link::Extra(next)) => { + debug_assert!(extra_values.len() > next); + debug_assert!(extra_values.len() > prev); + + extra_values[prev].next = Link::Extra(next); + extra_values[next].prev = Link::Extra(prev); + } + } + + // Remove the extra value + let mut extra = extra_values.swap_remove(idx); + + // This is the index of the value that was moved (possibly `extra`) + let old_idx = extra_values.len(); + + // Update the links + if extra.prev == Link::Extra(old_idx) { + extra.prev = Link::Extra(idx); + } + + if extra.next == Link::Extra(old_idx) { + extra.next = Link::Extra(idx); + } + + // Check if another entry was displaced. If it was, then the links + // need to be fixed. + if idx != old_idx { + let next; + let prev; + + { + debug_assert!(extra_values.len() > idx); + let moved = &extra_values[idx]; + next = moved.next; + prev = moved.prev; + } + + // An entry was moved, we have to the links + match prev { + Link::Entry(entry_idx) => { + // It is critical that we do not attempt to read the + // header name or value as that memory may have been + // "released" already. + debug_assert!(raw_links[entry_idx].is_some()); + + let links = raw_links[entry_idx].as_mut().unwrap(); + links.next = idx; + } + Link::Extra(extra_idx) => { + debug_assert!(extra_values.len() > extra_idx); + extra_values[extra_idx].next = Link::Extra(idx); + } + } + + match next { + Link::Entry(entry_idx) => { + debug_assert!(raw_links[entry_idx].is_some()); + + let links = raw_links[entry_idx].as_mut().unwrap(); + links.tail = idx; + } + Link::Extra(extra_idx) => { + debug_assert!(extra_values.len() > extra_idx); + extra_values[extra_idx].prev = Link::Extra(idx); + } + } + } + + debug_assert!({ + for v in &*extra_values { + assert!(v.next != Link::Extra(old_idx)); + assert!(v.prev != Link::Extra(old_idx)); + } + + true + }); + + extra +} + +fn drain_all_extra_values( + raw_links: RawLinks, + extra_values: &mut Vec>, + mut head: usize) + -> Vec +{ + let mut vec = Vec::new(); + loop { + let extra = remove_extra_value(raw_links, extra_values, head); + vec.push(extra.value); + + if let Link::Extra(idx) = extra.next { + head = idx; + } else { + break; + } + } + vec +} + +impl<'a, T> IntoIterator for &'a HeaderMap { + type Item = (&'a HeaderName, &'a T); + type IntoIter = Iter<'a, T>; + + fn into_iter(self) -> Iter<'a, T> { + self.iter() + } +} + +impl<'a, T> IntoIterator for &'a mut HeaderMap { + type Item = (&'a HeaderName, &'a mut T); + type IntoIter = IterMut<'a, T>; + + fn into_iter(self) -> IterMut<'a, T> { + self.iter_mut() + } +} + +impl IntoIterator for HeaderMap { + type Item = (Option, T); + type IntoIter = IntoIter; + + /// Creates a consuming iterator, that is, one that moves keys and values + /// out of the map in arbitrary order. The map cannot be used after calling + /// this. + /// + /// For each yielded item that has `None` provided for the `HeaderName`, + /// then the associated header name is the same as that of the previously + /// yielded item. The first yielded item will have `HeaderName` set. + /// + /// # Examples + /// + /// Basic usage. + /// + /// ``` + /// # use http::header; + /// # use http::header::*; + /// let mut map = HeaderMap::new(); + /// map.insert(header::CONTENT_LENGTH, "123".parse().unwrap()); + /// map.insert(header::CONTENT_TYPE, "json".parse().unwrap()); + /// + /// let mut iter = map.into_iter(); + /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap()))); + /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap()))); + /// assert!(iter.next().is_none()); + /// ``` + /// + /// Multiple values per key. + /// + /// ``` + /// # use http::header; + /// # use http::header::*; + /// let mut map = HeaderMap::new(); + /// + /// map.append(header::CONTENT_LENGTH, "123".parse().unwrap()); + /// map.append(header::CONTENT_LENGTH, "456".parse().unwrap()); + /// + /// map.append(header::CONTENT_TYPE, "json".parse().unwrap()); + /// map.append(header::CONTENT_TYPE, "html".parse().unwrap()); + /// map.append(header::CONTENT_TYPE, "xml".parse().unwrap()); + /// + /// let mut iter = map.into_iter(); + /// + /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap()))); + /// assert_eq!(iter.next(), Some((None, "456".parse().unwrap()))); + /// + /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap()))); + /// assert_eq!(iter.next(), Some((None, "html".parse().unwrap()))); + /// assert_eq!(iter.next(), Some((None, "xml".parse().unwrap()))); + /// assert!(iter.next().is_none()); + /// ``` + fn into_iter(self) -> IntoIter { + IntoIter { + next: None, + entries: self.entries.into_iter(), + extra_values: self.extra_values, + } + } +} + +impl FromIterator<(HeaderName, T)> for HeaderMap { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + let mut map = HeaderMap::default(); + map.extend(iter); + map + } +} + +/// Try to convert a `HashMap` into a `HeaderMap`. +/// +/// # Examples +/// +/// ``` +/// use std::collections::HashMap; +/// use std::convert::TryInto; +/// use http::HeaderMap; +/// +/// let mut map = HashMap::new(); +/// map.insert("X-Custom-Header".to_string(), "my value".to_string()); +/// +/// let headers: HeaderMap = (&map).try_into().expect("valid headers"); +/// assert_eq!(headers["X-Custom-Header"], "my value"); +/// ``` +impl<'a, K, V, T> TryFrom<&'a HashMap> for HeaderMap + where + K: Eq + Hash, + HeaderName: TryFrom<&'a K>, + >::Error: Into, + T: TryFrom<&'a V>, + T::Error: Into, +{ + type Error = Error; + + fn try_from(c: &'a HashMap) -> Result { + c.into_iter() + .map(|(k, v)| -> crate::Result<(HeaderName, T)> { + let name = TryFrom::try_from(k).map_err(Into::into)?; + let value = TryFrom::try_from(v).map_err(Into::into)?; + Ok((name, value)) + }) + .collect() + } +} + +impl Extend<(Option, T)> for HeaderMap { + /// Extend a `HeaderMap` with the contents of another `HeaderMap`. + /// + /// This function expects the yielded items to follow the same structure as + /// `IntoIter`. + /// + /// # Panics + /// + /// This panics if the first yielded item does not have a `HeaderName`. + /// + /// # Examples + /// + /// ``` + /// # use http::header::*; + /// let mut map = HeaderMap::new(); + /// + /// map.insert(ACCEPT, "text/plain".parse().unwrap()); + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// + /// let mut extra = HeaderMap::new(); + /// + /// extra.insert(HOST, "foo.bar".parse().unwrap()); + /// extra.insert(COOKIE, "hello".parse().unwrap()); + /// extra.append(COOKIE, "world".parse().unwrap()); + /// + /// map.extend(extra); + /// + /// assert_eq!(map["host"], "foo.bar"); + /// assert_eq!(map["accept"], "text/plain"); + /// assert_eq!(map["cookie"], "hello"); + /// + /// let v = map.get_all("host"); + /// assert_eq!(1, v.iter().count()); + /// + /// let v = map.get_all("cookie"); + /// assert_eq!(2, v.iter().count()); + /// ``` + fn extend, T)>>(&mut self, iter: I) { + let mut iter = iter.into_iter(); + + // The structure of this is a bit weird, but it is mostly to make the + // borrow checker happy. + let (mut key, mut val) = match iter.next() { + Some((Some(key), val)) => (key, val), + Some((None, _)) => panic!("expected a header name, but got None"), + None => return, + }; + + 'outer: loop { + let mut entry = match self.entry2(key) { + Entry::Occupied(mut e) => { + // Replace all previous values while maintaining a handle to + // the entry. + e.insert(val); + e + } + Entry::Vacant(e) => e.insert_entry(val), + }; + + // As long as `HeaderName` is none, keep inserting the value into + // the current entry + loop { + match iter.next() { + Some((Some(k), v)) => { + key = k; + val = v; + continue 'outer; + } + Some((None, v)) => { + entry.append(v); + } + None => { + return; + } + } + } + } + } +} + +impl Extend<(HeaderName, T)> for HeaderMap { + fn extend>(&mut self, iter: I) { + // Keys may be already present or show multiple times in the iterator. + // Reserve the entire hint lower bound if the map is empty. + // Otherwise reserve half the hint (rounded up), so the map + // will only resize twice in the worst case. + let iter = iter.into_iter(); + + let reserve = if self.is_empty() { + iter.size_hint().0 + } else { + (iter.size_hint().0 + 1) / 2 + }; + + self.reserve(reserve); + + for (k, v) in iter { + self.append(k, v); + } + } +} + +impl PartialEq for HeaderMap { + fn eq(&self, other: &HeaderMap) -> bool { + if self.len() != other.len() { + return false; + } + + self.keys() + .all(|key| self.get_all(key) == other.get_all(key)) + } +} + +impl Eq for HeaderMap {} + +impl fmt::Debug for HeaderMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_map().entries(self.iter()).finish() + } +} + +impl Default for HeaderMap { + fn default() -> Self { + HeaderMap::with_capacity(0) + } +} + +impl<'a, K, T> ops::Index for HeaderMap +where + K: AsHeaderName, +{ + type Output = T; + + /// # Panics + /// Using the index operator will cause a panic if the header you're querying isn't set. + #[inline] + fn index(&self, index: K) -> &T { + match self.get2(&index) { + Some(val) => val, + None => panic!("no entry found for key {:?}", index.as_str()), + } + } +} + +/// phase 2 is post-insert where we forward-shift `Pos` in the indices. +/// +/// returns the number of displaced elements +#[inline] +fn do_insert_phase_two(indices: &mut [Pos], mut probe: usize, mut old_pos: Pos) -> usize { + let mut num_displaced = 0; + + probe_loop!(probe < indices.len(), { + let pos = &mut indices[probe]; + + if pos.is_none() { + *pos = old_pos; + break; + } else { + num_displaced += 1; + old_pos = mem::replace(pos, old_pos); + } + }); + + num_displaced +} + +#[inline] +fn append_value( + entry_idx: usize, + entry: &mut Bucket, + extra: &mut Vec>, + value: T, +) { + match entry.links { + Some(links) => { + let idx = extra.len(); + extra.push(ExtraValue { + value: value, + prev: Link::Extra(links.tail), + next: Link::Entry(entry_idx), + }); + + extra[links.tail].next = Link::Extra(idx); + + entry.links = Some(Links { tail: idx, ..links }); + } + None => { + let idx = extra.len(); + extra.push(ExtraValue { + value: value, + prev: Link::Entry(entry_idx), + next: Link::Entry(entry_idx), + }); + + entry.links = Some(Links { + next: idx, + tail: idx, + }); + } + } +} + +// ===== impl Iter ===== + +impl<'a, T> Iterator for Iter<'a, T> { + type Item = (&'a HeaderName, &'a T); + + fn next(&mut self) -> Option { + use self::Cursor::*; + + if self.cursor.is_none() { + if (self.entry + 1) >= self.map.entries.len() { + return None; + } + + self.entry += 1; + self.cursor = Some(Cursor::Head); + } + + let entry = &self.map.entries[self.entry]; + + match self.cursor.unwrap() { + Head => { + self.cursor = entry.links.map(|l| Values(l.next)); + Some((&entry.key, &entry.value)) + } + Values(idx) => { + let extra = &self.map.extra_values[idx]; + + match extra.next { + Link::Entry(_) => self.cursor = None, + Link::Extra(i) => self.cursor = Some(Values(i)), + } + + Some((&entry.key, &extra.value)) + } + } + } + + fn size_hint(&self) -> (usize, Option) { + let map = self.map; + debug_assert!(map.entries.len() >= self.entry); + + let lower = map.entries.len() - self.entry; + // We could pessimistically guess at the upper bound, saying + // that its lower + map.extra_values.len(). That could be + // way over though, such as if we're near the end, and have + // already gone through several extra values... + (lower, None) + } +} + +impl<'a, T> FusedIterator for Iter<'a, T> {} + +unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {} +unsafe impl<'a, T: Sync> Send for Iter<'a, T> {} + +// ===== impl IterMut ===== + +impl<'a, T> IterMut<'a, T> { + fn next_unsafe(&mut self) -> Option<(&'a HeaderName, *mut T)> { + use self::Cursor::*; + + if self.cursor.is_none() { + if (self.entry + 1) >= unsafe { &*self.map }.entries.len() { + return None; + } + + self.entry += 1; + self.cursor = Some(Cursor::Head); + } + + let entry = unsafe { &mut (*self.map).entries[self.entry] }; + + match self.cursor.unwrap() { + Head => { + self.cursor = entry.links.map(|l| Values(l.next)); + Some((&entry.key, &mut entry.value as *mut _)) + } + Values(idx) => { + let extra = unsafe { &mut (*self.map).extra_values[idx] }; + + match extra.next { + Link::Entry(_) => self.cursor = None, + Link::Extra(i) => self.cursor = Some(Values(i)), + } + + Some((&entry.key, &mut extra.value as *mut _)) + } + } + } +} + +impl<'a, T> Iterator for IterMut<'a, T> { + type Item = (&'a HeaderName, &'a mut T); + + fn next(&mut self) -> Option { + self.next_unsafe() + .map(|(key, ptr)| (key, unsafe { &mut *ptr })) + } + + fn size_hint(&self) -> (usize, Option) { + let map = unsafe { &*self.map }; + debug_assert!(map.entries.len() >= self.entry); + + let lower = map.entries.len() - self.entry; + // We could pessimistically guess at the upper bound, saying + // that its lower + map.extra_values.len(). That could be + // way over though, such as if we're near the end, and have + // already gone through several extra values... + (lower, None) + } +} + +impl<'a, T> FusedIterator for IterMut<'a, T> {} + +unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {} +unsafe impl<'a, T: Send> Send for IterMut<'a, T> {} + +// ===== impl Keys ===== + +impl<'a, T> Iterator for Keys<'a, T> { + type Item = &'a HeaderName; + + fn next(&mut self) -> Option { + self.inner.next().map(|b| &b.key) + } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl<'a, T> ExactSizeIterator for Keys<'a, T> {} +impl<'a, T> FusedIterator for Keys<'a, T> {} + +// ===== impl Values ==== + +impl<'a, T> Iterator for Values<'a, T> { + type Item = &'a T; + + fn next(&mut self) -> Option { + self.inner.next().map(|(_, v)| v) + } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl<'a, T> FusedIterator for Values<'a, T> {} + +// ===== impl ValuesMut ==== + +impl<'a, T> Iterator for ValuesMut<'a, T> { + type Item = &'a mut T; + + fn next(&mut self) -> Option { + self.inner.next().map(|(_, v)| v) + } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl<'a, T> FusedIterator for ValuesMut<'a, T> {} + +// ===== impl Drain ===== + +impl<'a, T> Iterator for Drain<'a, T> { + type Item = (Option, T); + + fn next(&mut self) -> Option { + if let Some(next) = self.next { + // Remove the extra value + + let raw_links = RawLinks(self.entries); + let extra = unsafe { + remove_extra_value(raw_links, &mut *self.extra_values, next) + }; + + match extra.next { + Link::Extra(idx) => self.next = Some(idx), + Link::Entry(_) => self.next = None, + } + + return Some((None, extra.value)); + } + + let idx = self.idx; + + if idx == self.len { + return None; + } + + self.idx += 1; + + unsafe { + let entry = &(*self.entries)[idx]; + + // Read the header name + let key = ptr::read(&entry.key as *const _); + let value = ptr::read(&entry.value as *const _); + self.next = entry.links.map(|l| l.next); + + Some((Some(key), value)) + } + } + + fn size_hint(&self) -> (usize, Option) { + // At least this many names... It's unknown if the user wants + // to count the extra_values on top. + // + // For instance, extending a new `HeaderMap` wouldn't need to + // reserve the upper-bound in `entries`, only the lower-bound. + let lower = self.len - self.idx; + let upper = unsafe { (*self.extra_values).len() } + lower; + (lower, Some(upper)) + } +} + +impl<'a, T> FusedIterator for Drain<'a, T> {} + +impl<'a, T> Drop for Drain<'a, T> { + fn drop(&mut self) { + for _ in self {} + } +} + +unsafe impl<'a, T: Sync> Sync for Drain<'a, T> {} +unsafe impl<'a, T: Send> Send for Drain<'a, T> {} + +// ===== impl Entry ===== + +impl<'a, T> Entry<'a, T> { + /// Ensures a value is in the entry by inserting the default if empty. + /// + /// Returns a mutable reference to the **first** value in the entry. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// let mut map: HeaderMap = HeaderMap::default(); + /// + /// let headers = &[ + /// "content-length", + /// "x-hello", + /// "Content-Length", + /// "x-world", + /// ]; + /// + /// for &header in headers { + /// let counter = map.entry(header) + /// .or_insert(0); + /// *counter += 1; + /// } + /// + /// assert_eq!(map["content-length"], 2); + /// assert_eq!(map["x-hello"], 1); + /// ``` + pub fn or_insert(self, default: T) -> &'a mut T { + use self::Entry::*; + + match self { + Occupied(e) => e.into_mut(), + Vacant(e) => e.insert(default), + } + } + + /// Ensures a value is in the entry by inserting the result of the default + /// function if empty. + /// + /// The default function is not called if the entry exists in the map. + /// Returns a mutable reference to the **first** value in the entry. + /// + /// # Examples + /// + /// Basic usage. + /// + /// ``` + /// # use http::HeaderMap; + /// let mut map = HeaderMap::new(); + /// + /// let res = map.entry("x-hello") + /// .or_insert_with(|| "world".parse().unwrap()); + /// + /// assert_eq!(res, "world"); + /// ``` + /// + /// The default function is not called if the entry exists in the map. + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// + /// let res = map.entry("host") + /// .or_insert_with(|| unreachable!()); + /// + /// + /// assert_eq!(res, "world"); + /// ``` + pub fn or_insert_with T>(self, default: F) -> &'a mut T { + use self::Entry::*; + + match self { + Occupied(e) => e.into_mut(), + Vacant(e) => e.insert(default()), + } + } + + /// Returns a reference to the entry's key + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// let mut map = HeaderMap::new(); + /// + /// assert_eq!(map.entry("x-hello").key(), "x-hello"); + /// ``` + pub fn key(&self) -> &HeaderName { + use self::Entry::*; + + match *self { + Vacant(ref e) => e.key(), + Occupied(ref e) => e.key(), + } + } +} + +// ===== impl VacantEntry ===== + +impl<'a, T> VacantEntry<'a, T> { + /// Returns a reference to the entry's key + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// let mut map = HeaderMap::new(); + /// + /// assert_eq!(map.entry("x-hello").key().as_str(), "x-hello"); + /// ``` + pub fn key(&self) -> &HeaderName { + &self.key + } + + /// Take ownership of the key + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry}; + /// let mut map = HeaderMap::new(); + /// + /// if let Entry::Vacant(v) = map.entry("x-hello") { + /// assert_eq!(v.into_key().as_str(), "x-hello"); + /// } + /// ``` + pub fn into_key(self) -> HeaderName { + self.key + } + + /// Insert the value into the entry. + /// + /// The value will be associated with this entry's key. A mutable reference + /// to the inserted value will be returned. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry}; + /// let mut map = HeaderMap::new(); + /// + /// if let Entry::Vacant(v) = map.entry("x-hello") { + /// v.insert("world".parse().unwrap()); + /// } + /// + /// assert_eq!(map["x-hello"], "world"); + /// ``` + pub fn insert(self, value: T) -> &'a mut T { + // Ensure that there is space in the map + let index = + self.map + .insert_phase_two(self.key, value.into(), self.hash, self.probe, self.danger); + + &mut self.map.entries[index].value + } + + /// Insert the value into the entry. + /// + /// The value will be associated with this entry's key. The new + /// `OccupiedEntry` is returned, allowing for further manipulation. + /// + /// # Examples + /// + /// ``` + /// # use http::header::*; + /// let mut map = HeaderMap::new(); + /// + /// if let Entry::Vacant(v) = map.entry("x-hello") { + /// let mut e = v.insert_entry("world".parse().unwrap()); + /// e.insert("world2".parse().unwrap()); + /// } + /// + /// assert_eq!(map["x-hello"], "world2"); + /// ``` + pub fn insert_entry(self, value: T) -> OccupiedEntry<'a, T> { + // Ensure that there is space in the map + let index = + self.map + .insert_phase_two(self.key, value.into(), self.hash, self.probe, self.danger); + + OccupiedEntry { + map: self.map, + index: index, + probe: self.probe, + } + } +} + +// ===== impl GetAll ===== + +impl<'a, T: 'a> GetAll<'a, T> { + /// Returns an iterator visiting all values associated with the entry. + /// + /// Values are iterated in insertion order. + /// + /// # Examples + /// + /// ``` + /// # use http::HeaderMap; + /// # use http::header::HOST; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// map.append(HOST, "hello.earth".parse().unwrap()); + /// + /// let values = map.get_all("host"); + /// let mut iter = values.iter(); + /// assert_eq!(&"hello.world", iter.next().unwrap()); + /// assert_eq!(&"hello.earth", iter.next().unwrap()); + /// assert!(iter.next().is_none()); + /// ``` + pub fn iter(&self) -> ValueIter<'a, T> { + // This creates a new GetAll struct so that the lifetime + // isn't bound to &self. + GetAll { + map: self.map, + index: self.index, + } + .into_iter() + } +} + +impl<'a, T: PartialEq> PartialEq for GetAll<'a, T> { + fn eq(&self, other: &Self) -> bool { + self.iter().eq(other.iter()) + } +} + +impl<'a, T> IntoIterator for GetAll<'a, T> { + type Item = &'a T; + type IntoIter = ValueIter<'a, T>; + + fn into_iter(self) -> ValueIter<'a, T> { + self.map.value_iter(self.index) + } +} + +impl<'a, 'b: 'a, T> IntoIterator for &'b GetAll<'a, T> { + type Item = &'a T; + type IntoIter = ValueIter<'a, T>; + + fn into_iter(self) -> ValueIter<'a, T> { + self.map.value_iter(self.index) + } +} + +// ===== impl ValueIter ===== + +impl<'a, T: 'a> Iterator for ValueIter<'a, T> { + type Item = &'a T; + + fn next(&mut self) -> Option { + use self::Cursor::*; + + match self.front { + Some(Head) => { + let entry = &self.map.entries[self.index]; + + if self.back == Some(Head) { + self.front = None; + self.back = None; + } else { + // Update the iterator state + match entry.links { + Some(links) => { + self.front = Some(Values(links.next)); + } + None => unreachable!(), + } + } + + Some(&entry.value) + } + Some(Values(idx)) => { + let extra = &self.map.extra_values[idx]; + + if self.front == self.back { + self.front = None; + self.back = None; + } else { + match extra.next { + Link::Entry(_) => self.front = None, + Link::Extra(i) => self.front = Some(Values(i)), + } + } + + Some(&extra.value) + } + None => None, + } + } + + fn size_hint(&self) -> (usize, Option) { + match (self.front, self.back) { + // Exactly 1 value... + (Some(Cursor::Head), Some(Cursor::Head)) => (1, Some(1)), + // At least 1... + (Some(_), _) => (1, None), + // No more values... + (None, _) => (0, Some(0)), + } + } +} + +impl<'a, T: 'a> DoubleEndedIterator for ValueIter<'a, T> { + fn next_back(&mut self) -> Option { + use self::Cursor::*; + + match self.back { + Some(Head) => { + self.front = None; + self.back = None; + Some(&self.map.entries[self.index].value) + } + Some(Values(idx)) => { + let extra = &self.map.extra_values[idx]; + + if self.front == self.back { + self.front = None; + self.back = None; + } else { + match extra.prev { + Link::Entry(_) => self.back = Some(Head), + Link::Extra(idx) => self.back = Some(Values(idx)), + } + } + + Some(&extra.value) + } + None => None, + } + } +} + +impl<'a, T> FusedIterator for ValueIter<'a, T> {} + +// ===== impl ValueIterMut ===== + +impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> { + type Item = &'a mut T; + + fn next(&mut self) -> Option { + use self::Cursor::*; + + let entry = unsafe { &mut (*self.map).entries[self.index] }; + + match self.front { + Some(Head) => { + if self.back == Some(Head) { + self.front = None; + self.back = None; + } else { + // Update the iterator state + match entry.links { + Some(links) => { + self.front = Some(Values(links.next)); + } + None => unreachable!(), + } + } + + Some(&mut entry.value) + } + Some(Values(idx)) => { + let extra = unsafe { &mut (*self.map).extra_values[idx] }; + + if self.front == self.back { + self.front = None; + self.back = None; + } else { + match extra.next { + Link::Entry(_) => self.front = None, + Link::Extra(i) => self.front = Some(Values(i)), + } + } + + Some(&mut extra.value) + } + None => None, + } + } +} + +impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> { + fn next_back(&mut self) -> Option { + use self::Cursor::*; + + let entry = unsafe { &mut (*self.map).entries[self.index] }; + + match self.back { + Some(Head) => { + self.front = None; + self.back = None; + Some(&mut entry.value) + } + Some(Values(idx)) => { + let extra = unsafe { &mut (*self.map).extra_values[idx] }; + + if self.front == self.back { + self.front = None; + self.back = None; + } else { + match extra.prev { + Link::Entry(_) => self.back = Some(Head), + Link::Extra(idx) => self.back = Some(Values(idx)), + } + } + + Some(&mut extra.value) + } + None => None, + } + } +} + +impl<'a, T> FusedIterator for ValueIterMut<'a, T> {} + +unsafe impl<'a, T: Sync> Sync for ValueIterMut<'a, T> {} +unsafe impl<'a, T: Send> Send for ValueIterMut<'a, T> {} + +// ===== impl IntoIter ===== + +impl Iterator for IntoIter { + type Item = (Option, T); + + fn next(&mut self) -> Option { + if let Some(next) = self.next { + self.next = match self.extra_values[next].next { + Link::Entry(_) => None, + Link::Extra(v) => Some(v), + }; + + let value = unsafe { ptr::read(&self.extra_values[next].value) }; + + return Some((None, value)); + } + + if let Some(bucket) = self.entries.next() { + self.next = bucket.links.map(|l| l.next); + let name = Some(bucket.key); + let value = bucket.value; + + return Some((name, value)); + } + + None + } + + fn size_hint(&self) -> (usize, Option) { + let (lower, _) = self.entries.size_hint(); + // There could be more than just the entries upper, as there + // could be items in the `extra_values`. We could guess, saying + // `upper + extra_values.len()`, but that could overestimate by a lot. + (lower, None) + } +} + +impl FusedIterator for IntoIter {} + +impl Drop for IntoIter { + fn drop(&mut self) { + // Ensure the iterator is consumed + for _ in self.by_ref() {} + + // All the values have already been yielded out. + unsafe { + self.extra_values.set_len(0); + } + } +} + +// ===== impl OccupiedEntry ===== + +impl<'a, T> OccupiedEntry<'a, T> { + /// Returns a reference to the entry's key. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// + /// if let Entry::Occupied(e) = map.entry("host") { + /// assert_eq!("host", e.key()); + /// } + /// ``` + pub fn key(&self) -> &HeaderName { + &self.map.entries[self.index].key + } + + /// Get a reference to the first value in the entry. + /// + /// Values are stored in insertion order. + /// + /// # Panics + /// + /// `get` panics if there are no values associated with the entry. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// + /// if let Entry::Occupied(mut e) = map.entry("host") { + /// assert_eq!(e.get(), &"hello.world"); + /// + /// e.append("hello.earth".parse().unwrap()); + /// + /// assert_eq!(e.get(), &"hello.world"); + /// } + /// ``` + pub fn get(&self) -> &T { + &self.map.entries[self.index].value + } + + /// Get a mutable reference to the first value in the entry. + /// + /// Values are stored in insertion order. + /// + /// # Panics + /// + /// `get_mut` panics if there are no values associated with the entry. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::default(); + /// map.insert(HOST, "hello.world".to_string()); + /// + /// if let Entry::Occupied(mut e) = map.entry("host") { + /// e.get_mut().push_str("-2"); + /// assert_eq!(e.get(), &"hello.world-2"); + /// } + /// ``` + pub fn get_mut(&mut self) -> &mut T { + &mut self.map.entries[self.index].value + } + + /// Converts the `OccupiedEntry` into a mutable reference to the **first** + /// value. + /// + /// The lifetime of the returned reference is bound to the original map. + /// + /// # Panics + /// + /// `into_mut` panics if there are no values associated with the entry. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::default(); + /// map.insert(HOST, "hello.world".to_string()); + /// map.append(HOST, "hello.earth".to_string()); + /// + /// if let Entry::Occupied(e) = map.entry("host") { + /// e.into_mut().push_str("-2"); + /// } + /// + /// assert_eq!("hello.world-2", map["host"]); + /// ``` + pub fn into_mut(self) -> &'a mut T { + &mut self.map.entries[self.index].value + } + + /// Sets the value of the entry. + /// + /// All previous values associated with the entry are removed and the first + /// one is returned. See `insert_mult` for an API that returns all values. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "hello.world".parse().unwrap()); + /// + /// if let Entry::Occupied(mut e) = map.entry("host") { + /// let mut prev = e.insert("earth".parse().unwrap()); + /// assert_eq!("hello.world", prev); + /// } + /// + /// assert_eq!("earth", map["host"]); + /// ``` + pub fn insert(&mut self, value: T) -> T { + self.map.insert_occupied(self.index, value.into()) + } + + /// Sets the value of the entry. + /// + /// This function does the same as `insert` except it returns an iterator + /// that yields all values previously associated with the key. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// map.append(HOST, "world2".parse().unwrap()); + /// + /// if let Entry::Occupied(mut e) = map.entry("host") { + /// let mut prev = e.insert_mult("earth".parse().unwrap()); + /// assert_eq!("world", prev.next().unwrap()); + /// assert_eq!("world2", prev.next().unwrap()); + /// assert!(prev.next().is_none()); + /// } + /// + /// assert_eq!("earth", map["host"]); + /// ``` + pub fn insert_mult(&mut self, value: T) -> ValueDrain<'_, T> { + self.map.insert_occupied_mult(self.index, value.into()) + } + + /// Insert the value into the entry. + /// + /// The new value is appended to the end of the entry's value list. All + /// previous values associated with the entry are retained. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// + /// if let Entry::Occupied(mut e) = map.entry("host") { + /// e.append("earth".parse().unwrap()); + /// } + /// + /// let values = map.get_all("host"); + /// let mut i = values.iter(); + /// assert_eq!("world", *i.next().unwrap()); + /// assert_eq!("earth", *i.next().unwrap()); + /// ``` + pub fn append(&mut self, value: T) { + let idx = self.index; + let entry = &mut self.map.entries[idx]; + append_value(idx, entry, &mut self.map.extra_values, value.into()); + } + + /// Remove the entry from the map. + /// + /// All values associated with the entry are removed and the first one is + /// returned. See `remove_entry_mult` for an API that returns all values. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// + /// if let Entry::Occupied(e) = map.entry("host") { + /// let mut prev = e.remove(); + /// assert_eq!("world", prev); + /// } + /// + /// assert!(!map.contains_key("host")); + /// ``` + pub fn remove(self) -> T { + self.remove_entry().1 + } + + /// Remove the entry from the map. + /// + /// The key and all values associated with the entry are removed and the + /// first one is returned. See `remove_entry_mult` for an API that returns + /// all values. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// + /// if let Entry::Occupied(e) = map.entry("host") { + /// let (key, mut prev) = e.remove_entry(); + /// assert_eq!("host", key.as_str()); + /// assert_eq!("world", prev); + /// } + /// + /// assert!(!map.contains_key("host")); + /// ``` + pub fn remove_entry(self) -> (HeaderName, T) { + if let Some(links) = self.map.entries[self.index].links { + self.map.remove_all_extra_values(links.next); + } + + let entry = self.map.remove_found(self.probe, self.index); + + (entry.key, entry.value) + } + + /// Remove the entry from the map. + /// + /// The key and all values associated with the entry are removed and + /// returned. + pub fn remove_entry_mult(self) -> (HeaderName, ValueDrain<'a, T>) { + let raw_links = self.map.raw_links(); + let extra_values = &mut self.map.extra_values; + + let next = self.map.entries[self.index].links.map(|l| { + drain_all_extra_values(raw_links, extra_values, l.next) + .into_iter() + }); + + let entry = self.map.remove_found(self.probe, self.index); + + let drain = ValueDrain { + first: Some(entry.value), + next, + lt: PhantomData, + }; + (entry.key, drain) + } + + /// Returns an iterator visiting all values associated with the entry. + /// + /// Values are iterated in insertion order. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::new(); + /// map.insert(HOST, "world".parse().unwrap()); + /// map.append(HOST, "earth".parse().unwrap()); + /// + /// if let Entry::Occupied(e) = map.entry("host") { + /// let mut iter = e.iter(); + /// assert_eq!(&"world", iter.next().unwrap()); + /// assert_eq!(&"earth", iter.next().unwrap()); + /// assert!(iter.next().is_none()); + /// } + /// ``` + pub fn iter(&self) -> ValueIter<'_, T> { + self.map.value_iter(Some(self.index)) + } + + /// Returns an iterator mutably visiting all values associated with the + /// entry. + /// + /// Values are iterated in insertion order. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderMap, Entry, HOST}; + /// let mut map = HeaderMap::default(); + /// map.insert(HOST, "world".to_string()); + /// map.append(HOST, "earth".to_string()); + /// + /// if let Entry::Occupied(mut e) = map.entry("host") { + /// for e in e.iter_mut() { + /// e.push_str("-boop"); + /// } + /// } + /// + /// let mut values = map.get_all("host"); + /// let mut i = values.iter(); + /// assert_eq!(&"world-boop", i.next().unwrap()); + /// assert_eq!(&"earth-boop", i.next().unwrap()); + /// ``` + pub fn iter_mut(&mut self) -> ValueIterMut<'_, T> { + self.map.value_iter_mut(self.index) + } +} + +impl<'a, T> IntoIterator for OccupiedEntry<'a, T> { + type Item = &'a mut T; + type IntoIter = ValueIterMut<'a, T>; + + fn into_iter(self) -> ValueIterMut<'a, T> { + self.map.value_iter_mut(self.index) + } +} + +impl<'a, 'b: 'a, T> IntoIterator for &'b OccupiedEntry<'a, T> { + type Item = &'a T; + type IntoIter = ValueIter<'a, T>; + + fn into_iter(self) -> ValueIter<'a, T> { + self.iter() + } +} + +impl<'a, 'b: 'a, T> IntoIterator for &'b mut OccupiedEntry<'a, T> { + type Item = &'a mut T; + type IntoIter = ValueIterMut<'a, T>; + + fn into_iter(self) -> ValueIterMut<'a, T> { + self.iter_mut() + } +} + +// ===== impl ValueDrain ===== + +impl<'a, T> Iterator for ValueDrain<'a, T> { + type Item = T; + + fn next(&mut self) -> Option { + if self.first.is_some() { + self.first.take() + } else if let Some(ref mut extras) = self.next { + extras.next() + } else { + None + } + } + + fn size_hint(&self) -> (usize, Option) { + match (&self.first, &self.next) { + // Exactly 1 + (&Some(_), &None) => (1, Some(1)), + // 1 + extras + (&Some(_), &Some(ref extras)) => { + let (l, u) = extras.size_hint(); + (l + 1, u.map(|u| u + 1)) + }, + // Extras only + (&None, &Some(ref extras)) => extras.size_hint(), + // No more + (&None, &None) => (0, Some(0)), + } + } +} + +impl<'a, T> FusedIterator for ValueDrain<'a, T> {} + +impl<'a, T> Drop for ValueDrain<'a, T> { + fn drop(&mut self) { + while let Some(_) = self.next() {} + } +} + +unsafe impl<'a, T: Sync> Sync for ValueDrain<'a, T> {} +unsafe impl<'a, T: Send> Send for ValueDrain<'a, T> {} + +// ===== impl RawLinks ===== + +impl Clone for RawLinks { + fn clone(&self) -> RawLinks { + *self + } +} + +impl Copy for RawLinks {} + +impl ops::Index for RawLinks { + type Output = Option; + + fn index(&self, idx: usize) -> &Self::Output { + unsafe { + &(*self.0)[idx].links + } + } +} + +impl ops::IndexMut for RawLinks { + fn index_mut(&mut self, idx: usize) -> &mut Self::Output { + unsafe { + &mut (*self.0)[idx].links + } + } +} + +// ===== impl Pos ===== + +impl Pos { + #[inline] + fn new(index: usize, hash: HashValue) -> Self { + debug_assert!(index < MAX_SIZE); + Pos { + index: index as Size, + hash: hash, + } + } + + #[inline] + fn none() -> Self { + Pos { + index: !0, + hash: HashValue(0), + } + } + + #[inline] + fn is_some(&self) -> bool { + !self.is_none() + } + + #[inline] + fn is_none(&self) -> bool { + self.index == !0 + } + + #[inline] + fn resolve(&self) -> Option<(usize, HashValue)> { + if self.is_some() { + Some((self.index as usize, self.hash)) + } else { + None + } + } +} + +impl Danger { + fn is_red(&self) -> bool { + match *self { + Danger::Red(_) => true, + _ => false, + } + } + + fn to_red(&mut self) { + debug_assert!(self.is_yellow()); + *self = Danger::Red(RandomState::new()); + } + + fn is_yellow(&self) -> bool { + match *self { + Danger::Yellow => true, + _ => false, + } + } + + fn to_yellow(&mut self) { + match *self { + Danger::Green => { + *self = Danger::Yellow; + } + _ => {} + } + } + + fn to_green(&mut self) { + debug_assert!(self.is_yellow()); + *self = Danger::Green; + } +} + +// ===== impl Utils ===== + +#[inline] +fn usable_capacity(cap: usize) -> usize { + cap - cap / 4 +} + +#[inline] +fn to_raw_capacity(n: usize) -> usize { + match n.checked_add(n / 3) { + Some(n) => n, + None => panic!( + "requested capacity {} too large: overflow while converting to raw capacity", + n + ), + } +} + +#[inline] +fn desired_pos(mask: Size, hash: HashValue) -> usize { + (hash.0 & mask) as usize +} + +/// The number of steps that `current` is forward of the desired position for hash +#[inline] +fn probe_distance(mask: Size, hash: HashValue, current: usize) -> usize { + current.wrapping_sub(desired_pos(mask, hash)) & mask as usize +} + +fn hash_elem_using(danger: &Danger, k: &K) -> HashValue +where + K: Hash, +{ + use fnv::FnvHasher; + + const MASK: u64 = (MAX_SIZE as u64) - 1; + + let hash = match *danger { + // Safe hash + Danger::Red(ref hasher) => { + let mut h = hasher.build_hasher(); + k.hash(&mut h); + h.finish() + } + // Fast hash + _ => { + let mut h = FnvHasher::default(); + k.hash(&mut h); + h.finish() + } + }; + + HashValue((hash & MASK) as u16) +} + +/* + * + * ===== impl IntoHeaderName / AsHeaderName ===== + * + */ + +mod into_header_name { + use super::{Entry, HdrName, HeaderMap, HeaderName}; + + /// A marker trait used to identify values that can be used as insert keys + /// to a `HeaderMap`. + pub trait IntoHeaderName: Sealed {} + + // All methods are on this pub(super) trait, instead of `IntoHeaderName`, + // so that they aren't publicly exposed to the world. + // + // Being on the `IntoHeaderName` trait would mean users could call + // `"host".insert(&mut map, "localhost")`. + // + // Ultimately, this allows us to adjust the signatures of these methods + // without breaking any external crate. + pub trait Sealed { + #[doc(hidden)] + fn insert(self, map: &mut HeaderMap, val: T) -> Option; + + #[doc(hidden)] + fn append(self, map: &mut HeaderMap, val: T) -> bool; + + #[doc(hidden)] + fn entry(self, map: &mut HeaderMap) -> Entry<'_, T>; + } + + // ==== impls ==== + + impl Sealed for HeaderName { + #[inline] + fn insert(self, map: &mut HeaderMap, val: T) -> Option { + map.insert2(self, val) + } + + #[inline] + fn append(self, map: &mut HeaderMap, val: T) -> bool { + map.append2(self, val) + } + + #[inline] + fn entry(self, map: &mut HeaderMap) -> Entry<'_, T> { + map.entry2(self) + } + } + + impl IntoHeaderName for HeaderName {} + + impl<'a> Sealed for &'a HeaderName { + #[inline] + fn insert(self, map: &mut HeaderMap, val: T) -> Option { + map.insert2(self, val) + } + #[inline] + fn append(self, map: &mut HeaderMap, val: T) -> bool { + map.append2(self, val) + } + + #[inline] + fn entry(self, map: &mut HeaderMap) -> Entry<'_, T> { + map.entry2(self) + } + } + + impl<'a> IntoHeaderName for &'a HeaderName {} + + impl Sealed for &'static str { + #[inline] + fn insert(self, map: &mut HeaderMap, val: T) -> Option { + HdrName::from_static(self, move |hdr| map.insert2(hdr, val)) + } + #[inline] + fn append(self, map: &mut HeaderMap, val: T) -> bool { + HdrName::from_static(self, move |hdr| map.append2(hdr, val)) + } + + #[inline] + fn entry(self, map: &mut HeaderMap) -> Entry<'_, T> { + HdrName::from_static(self, move |hdr| map.entry2(hdr)) + } + } + + impl IntoHeaderName for &'static str {} +} + +mod as_header_name { + use super::{Entry, HdrName, HeaderMap, HeaderName, InvalidHeaderName}; + + /// A marker trait used to identify values that can be used as search keys + /// to a `HeaderMap`. + pub trait AsHeaderName: Sealed {} + + // All methods are on this pub(super) trait, instead of `AsHeaderName`, + // so that they aren't publicly exposed to the world. + // + // Being on the `AsHeaderName` trait would mean users could call + // `"host".find(&map)`. + // + // Ultimately, this allows us to adjust the signatures of these methods + // without breaking any external crate. + pub trait Sealed { + #[doc(hidden)] + fn try_entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName>; + + #[doc(hidden)] + fn find(&self, map: &HeaderMap) -> Option<(usize, usize)>; + + #[doc(hidden)] + fn as_str(&self) -> &str; + } + + // ==== impls ==== + + impl Sealed for HeaderName { + #[inline] + fn try_entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { + Ok(map.entry2(self)) + } + + #[inline] + fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { + map.find(self) + } + + fn as_str(&self) -> &str { + ::as_str(self) + } + } + + impl AsHeaderName for HeaderName {} + + impl<'a> Sealed for &'a HeaderName { + #[inline] + fn try_entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { + Ok(map.entry2(self)) + } + + #[inline] + fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { + map.find(*self) + } + + fn as_str(&self) -> &str { + ::as_str(*self) + } + } + + impl<'a> AsHeaderName for &'a HeaderName {} + + impl<'a> Sealed for &'a str { + #[inline] + fn try_entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { + HdrName::from_bytes(self.as_bytes(), move |hdr| map.entry2(hdr)) + } + + #[inline] + fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { + HdrName::from_bytes(self.as_bytes(), move |hdr| map.find(&hdr)).unwrap_or(None) + } + + fn as_str(&self) -> &str { + self + } + } + + impl<'a> AsHeaderName for &'a str {} + + impl Sealed for String { + #[inline] + fn try_entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { + self.as_str().try_entry(map) + } + + #[inline] + fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { + Sealed::find(&self.as_str(), map) + } + + fn as_str(&self) -> &str { + self + } + } + + impl AsHeaderName for String {} + + impl<'a> Sealed for &'a String { + #[inline] + fn try_entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { + self.as_str().try_entry(map) + } + + #[inline] + fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { + Sealed::find(*self, map) + } + + fn as_str(&self) -> &str { + *self + } + } + + impl<'a> AsHeaderName for &'a String {} +} + +#[test] +fn test_bounds() { + fn check_bounds() {} + + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); + check_bounds::>(); +} + +#[test] +fn skip_duplicates_during_key_iteration() { + let mut map = HeaderMap::new(); + map.append("a", HeaderValue::from_static("a")); + map.append("a", HeaderValue::from_static("b")); + assert_eq!(map.keys().count(), map.keys_len()); +} diff --git a/third_party/rust/http/v1/crate/src/header/mod.rs b/third_party/rust/http/v1/crate/src/header/mod.rs new file mode 100644 index 000000000000..1ca494509499 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/header/mod.rs @@ -0,0 +1,174 @@ +//! HTTP header types +//! +//! The module provides [`HeaderName`], [`HeaderMap`], and a number of types +//! used for interacting with `HeaderMap`. These types allow representing both +//! HTTP/1 and HTTP/2 headers. +//! +//! # `HeaderName` +//! +//! The `HeaderName` type represents both standard header names as well as +//! custom header names. The type handles the case insensitive nature of header +//! names and is used as the key portion of `HeaderMap`. Header names are +//! normalized to lower case. In other words, when creating a `HeaderName` with +//! a string, even if upper case characters are included, when getting a string +//! representation of the `HeaderName`, it will be all lower case. This allows +//! for faster `HeaderMap` comparison operations. +//! +//! The internal representation is optimized to efficiently handle the cases +//! most commonly encountered when working with HTTP. Standard header names are +//! special cased and are represented internally as an enum. Short custom +//! headers will be stored directly in the `HeaderName` struct and will not +//! incur any allocation overhead, however longer strings will require an +//! allocation for storage. +//! +//! ## Limitations +//! +//! `HeaderName` has a max length of 32,768 for header names. Attempting to +//! parse longer names will result in a panic. +//! +//! # `HeaderMap` +//! +//! `HeaderMap` is a map structure of header names highly optimized for use +//! cases common with HTTP. It is a [multimap] structure, where each header name +//! may have multiple associated header values. Given this, some of the APIs +//! diverge from [`HashMap`]. +//! +//! ## Overview +//! +//! Just like `HashMap` in Rust's stdlib, `HeaderMap` is based on [Robin Hood +//! hashing]. This algorithm tends to reduce the worst case search times in the +//! table and enables high load factors without seriously affecting performance. +//! Internally, keys and values are stored in vectors. As such, each insertion +//! will not incur allocation overhead. However, once the underlying vector +//! storage is full, a larger vector must be allocated and all values copied. +//! +//! ## Deterministic ordering +//! +//! Unlike Rust's `HashMap`, values in `HeaderMap` are deterministically +//! ordered. Roughly, values are ordered by insertion. This means that a +//! function that deterministically operates on a header map can rely on the +//! iteration order to remain consistent across processes and platforms. +//! +//! ## Adaptive hashing +//! +//! `HeaderMap` uses an adaptive hashing strategy in order to efficiently handle +//! most common cases. All standard headers have statically computed hash values +//! which removes the need to perform any hashing of these headers at runtime. +//! The default hash function emphasizes performance over robustness. However, +//! `HeaderMap` detects high collision rates and switches to a secure hash +//! function in those events. The threshold is set such that only denial of +//! service attacks should trigger it. +//! +//! ## Limitations +//! +//! `HeaderMap` can store a maximum of 32,768 headers (header name / value +//! pairs). Attempting to insert more will result in a panic. +//! +//! [`HeaderName`]: struct.HeaderName.html +//! [`HeaderMap`]: struct.HeaderMap.html +//! [multimap]: https://en.wikipedia.org/wiki/Multimap +//! [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html +//! [Robin Hood hashing]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing + +mod map; +mod name; +mod value; + +pub use self::map::{ + AsHeaderName, Drain, Entry, GetAll, HeaderMap, IntoHeaderName, IntoIter, Iter, IterMut, Keys, + OccupiedEntry, VacantEntry, ValueDrain, ValueIter, ValueIterMut, Values, ValuesMut, +}; +pub use self::name::{HeaderName, InvalidHeaderName}; +pub use self::value::{HeaderValue, InvalidHeaderValue, ToStrError}; + +// Use header name constants +pub use self::name::{ + ACCEPT, + ACCEPT_CHARSET, + ACCEPT_ENCODING, + ACCEPT_LANGUAGE, + ACCEPT_RANGES, + ACCESS_CONTROL_ALLOW_CREDENTIALS, + ACCESS_CONTROL_ALLOW_HEADERS, + ACCESS_CONTROL_ALLOW_METHODS, + ACCESS_CONTROL_ALLOW_ORIGIN, + ACCESS_CONTROL_EXPOSE_HEADERS, + ACCESS_CONTROL_MAX_AGE, + ACCESS_CONTROL_REQUEST_HEADERS, + ACCESS_CONTROL_REQUEST_METHOD, + AGE, + ALLOW, + ALT_SVC, + AUTHORIZATION, + CACHE_CONTROL, + CACHE_STATUS, + CDN_CACHE_CONTROL, + CONNECTION, + CONTENT_DISPOSITION, + CONTENT_ENCODING, + CONTENT_LANGUAGE, + CONTENT_LENGTH, + CONTENT_LOCATION, + CONTENT_RANGE, + CONTENT_SECURITY_POLICY, + CONTENT_SECURITY_POLICY_REPORT_ONLY, + CONTENT_TYPE, + COOKIE, + DNT, + DATE, + ETAG, + EXPECT, + EXPIRES, + FORWARDED, + FROM, + HOST, + IF_MATCH, + IF_MODIFIED_SINCE, + IF_NONE_MATCH, + IF_RANGE, + IF_UNMODIFIED_SINCE, + LAST_MODIFIED, + LINK, + LOCATION, + MAX_FORWARDS, + ORIGIN, + PRAGMA, + PROXY_AUTHENTICATE, + PROXY_AUTHORIZATION, + PUBLIC_KEY_PINS, + PUBLIC_KEY_PINS_REPORT_ONLY, + RANGE, + REFERER, + REFERRER_POLICY, + REFRESH, + RETRY_AFTER, + SEC_WEBSOCKET_ACCEPT, + SEC_WEBSOCKET_EXTENSIONS, + SEC_WEBSOCKET_KEY, + SEC_WEBSOCKET_PROTOCOL, + SEC_WEBSOCKET_VERSION, + SERVER, + SET_COOKIE, + STRICT_TRANSPORT_SECURITY, + TE, + TRAILER, + TRANSFER_ENCODING, + UPGRADE, + UPGRADE_INSECURE_REQUESTS, + USER_AGENT, + VARY, + VIA, + WARNING, + WWW_AUTHENTICATE, + X_CONTENT_TYPE_OPTIONS, + X_DNS_PREFETCH_CONTROL, + X_FRAME_OPTIONS, + X_XSS_PROTECTION, +}; + +/// Maximum length of a header name +/// +/// Generally, 64kb for a header name is WAY too much than would ever be needed +/// in practice. Restricting it to this size enables using `u16` values to +/// represent offsets when dealing with header names. +const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1; diff --git a/third_party/rust/http/v1/crate/src/header/name.rs b/third_party/rust/http/v1/crate/src/header/name.rs new file mode 100644 index 000000000000..6080cf088b66 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/header/name.rs @@ -0,0 +1,1868 @@ +use crate::byte_str::ByteStr; +use bytes::{Bytes, BytesMut}; + +use std::borrow::Borrow; +use std::error::Error; +use std::convert::{TryFrom}; +use std::hash::{Hash, Hasher}; +use std::mem::MaybeUninit; +use std::str::FromStr; +use std::fmt; + +/// Represents an HTTP header field name +/// +/// Header field names identify the header. Header sets may include multiple +/// headers with the same name. The HTTP specification defines a number of +/// standard headers, but HTTP messages may include non-standard header names as +/// well as long as they adhere to the specification. +/// +/// `HeaderName` is used as the [`HeaderMap`] key. Constants are available for +/// all standard header names in the [`header`] module. +/// +/// # Representation +/// +/// `HeaderName` represents standard header names using an `enum`, as such they +/// will not require an allocation for storage. All custom header names are +/// lower cased upon conversion to a `HeaderName` value. This avoids the +/// overhead of dynamically doing lower case conversion during the hash code +/// computation and the comparison operation. +/// +/// [`HeaderMap`]: struct.HeaderMap.html +/// [`header`]: index.html +#[derive(Clone, Eq, PartialEq, Hash)] +pub struct HeaderName { + inner: Repr, +} + +// Almost a full `HeaderName` +#[derive(Debug, Hash)] +pub struct HdrName<'a> { + inner: Repr>, +} + +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +enum Repr { + Standard(StandardHeader), + Custom(T), +} + +// Used to hijack the Hash impl +#[derive(Debug, Clone, Eq, PartialEq)] +struct Custom(ByteStr); + +#[derive(Debug, Clone)] +// Invariant: If lower then buf is valid UTF-8. +struct MaybeLower<'a> { + buf: &'a [u8], + lower: bool, +} + +/// A possible error when converting a `HeaderName` from another type. +pub struct InvalidHeaderName { + _priv: (), +} + +macro_rules! standard_headers { + ( + $( + $(#[$docs:meta])* + ($konst:ident, $upcase:ident, $name_bytes:literal); + )+ + ) => { + #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] + enum StandardHeader { + $( + $konst, + )+ + } + + $( + $(#[$docs])* + pub const $upcase: HeaderName = HeaderName { + inner: Repr::Standard(StandardHeader::$konst), + }; + )+ + + impl StandardHeader { + #[inline] + fn as_str(&self) -> &'static str { + match *self { + // Safety: test_parse_standard_headers ensures these &[u8]s are &str-safe. + $( + StandardHeader::$konst => unsafe { std::str::from_utf8_unchecked( $name_bytes ) }, + )+ + } + } + + const fn from_bytes(name_bytes: &[u8]) -> Option { + match name_bytes { + $( + $name_bytes => Some(StandardHeader::$konst), + )+ + _ => None, + } + } + } + + #[cfg(test)] + const TEST_HEADERS: &'static [(StandardHeader, &'static [u8])] = &[ + $( + (StandardHeader::$konst, $name_bytes), + )+ + ]; + + #[test] + fn test_parse_standard_headers() { + for &(std, name_bytes) in TEST_HEADERS { + // Test lower case + assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), HeaderName::from(std)); + + // Test upper case + let upper = std::str::from_utf8(name_bytes).expect("byte string constants are all utf-8").to_uppercase(); + assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(), HeaderName::from(std)); + } + } + + #[test] + fn test_standard_headers_into_bytes() { + for &(std, name_bytes) in TEST_HEADERS { + let name = std::str::from_utf8(name_bytes).unwrap(); + let std = HeaderName::from(std); + // Test lower case + let bytes: Bytes = + HeaderName::from_bytes(name_bytes).unwrap().inner.into(); + assert_eq!(bytes, name); + assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std); + + // Test upper case + let upper = name.to_uppercase(); + let bytes: Bytes = + HeaderName::from_bytes(upper.as_bytes()).unwrap().inner.into(); + assert_eq!(bytes, name_bytes); + assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(), + std); + } + + } + } +} + +// Generate constants for all standard HTTP headers. This includes a static hash +// code for the "fast hash" path. The hash code for static headers *do not* have +// to match the text representation of those headers. This is because header +// strings are always converted to the static values (when they match) before +// being hashed. This means that it is impossible to compare the static hash +// code of CONTENT_LENGTH with "content-length". +standard_headers! { + /// Advertises which content types the client is able to understand. + /// + /// The Accept request HTTP header advertises which content types, expressed + /// as MIME types, the client is able to understand. Using content + /// negotiation, the server then selects one of the proposals, uses it and + /// informs the client of its choice with the Content-Type response header. + /// Browsers set adequate values for this header depending of the context + /// where the request is done: when fetching a CSS stylesheet a different + /// value is set for the request than when fetching an image, video or a + /// script. + (Accept, ACCEPT, b"accept"); + + /// Advertises which character set the client is able to understand. + /// + /// The Accept-Charset request HTTP header advertises which character set + /// the client is able to understand. Using content negotiation, the server + /// then selects one of the proposals, uses it and informs the client of its + /// choice within the Content-Type response header. Browsers usually don't + /// set this header as the default value for each content type is usually + /// correct and transmitting it would allow easier fingerprinting. + /// + /// If the server cannot serve any matching character set, it can + /// theoretically send back a 406 (Not Acceptable) error code. But, for a + /// better user experience, this is rarely done and the more common way is + /// to ignore the Accept-Charset header in this case. + (AcceptCharset, ACCEPT_CHARSET, b"accept-charset"); + + /// Advertises which content encoding the client is able to understand. + /// + /// The Accept-Encoding request HTTP header advertises which content + /// encoding, usually a compression algorithm, the client is able to + /// understand. Using content negotiation, the server selects one of the + /// proposals, uses it and informs the client of its choice with the + /// Content-Encoding response header. + /// + /// Even if both the client and the server supports the same compression + /// algorithms, the server may choose not to compress the body of a + /// response, if the identity value is also acceptable. Two common cases + /// lead to this: + /// + /// * The data to be sent is already compressed and a second compression + /// won't lead to smaller data to be transmitted. This may the case with + /// some image formats; + /// + /// * The server is overloaded and cannot afford the computational overhead + /// induced by the compression requirement. Typically, Microsoft recommends + /// not to compress if a server use more than 80 % of its computational + /// power. + /// + /// As long as the identity value, meaning no encryption, is not explicitly + /// forbidden, by an identity;q=0 or a *;q=0 without another explicitly set + /// value for identity, the server must never send back a 406 Not Acceptable + /// error. + (AcceptEncoding, ACCEPT_ENCODING, b"accept-encoding"); + + /// Advertises which languages the client is able to understand. + /// + /// The Accept-Language request HTTP header advertises which languages the + /// client is able to understand, and which locale variant is preferred. + /// Using content negotiation, the server then selects one of the proposals, + /// uses it and informs the client of its choice with the Content-Language + /// response header. Browsers set adequate values for this header according + /// their user interface language and even if a user can change it, this + /// happens rarely (and is frown upon as it leads to fingerprinting). + /// + /// This header is a hint to be used when the server has no way of + /// determining the language via another way, like a specific URL, that is + /// controlled by an explicit user decision. It is recommended that the + /// server never overrides an explicit decision. The content of the + /// Accept-Language is often out of the control of the user (like when + /// traveling and using an Internet Cafe in a different country); the user + /// may also want to visit a page in another language than the locale of + /// their user interface. + /// + /// If the server cannot serve any matching language, it can theoretically + /// send back a 406 (Not Acceptable) error code. But, for a better user + /// experience, this is rarely done and more common way is to ignore the + /// Accept-Language header in this case. + (AcceptLanguage, ACCEPT_LANGUAGE, b"accept-language"); + + /// Marker used by the server to advertise partial request support. + /// + /// The Accept-Ranges response HTTP header is a marker used by the server to + /// advertise its support of partial requests. The value of this field + /// indicates the unit that can be used to define a range. + /// + /// In presence of an Accept-Ranges header, the browser may try to resume an + /// interrupted download, rather than to start it from the start again. + (AcceptRanges, ACCEPT_RANGES, b"accept-ranges"); + + /// Preflight response indicating if the response to the request can be + /// exposed to the page. + /// + /// The Access-Control-Allow-Credentials response header indicates whether + /// or not the response to the request can be exposed to the page. It can be + /// exposed when the true value is returned; it can't in other cases. + /// + /// Credentials are cookies, authorization headers or TLS client + /// certificates. + /// + /// When used as part of a response to a preflight request, this indicates + /// whether or not the actual request can be made using credentials. Note + /// that simple GET requests are not preflighted, and so if a request is + /// made for a resource with credentials, if this header is not returned + /// with the resource, the response is ignored by the browser and not + /// returned to web content. + /// + /// The Access-Control-Allow-Credentials header works in conjunction with + /// the XMLHttpRequest.withCredentials property or with the credentials + /// option in the Request() constructor of the Fetch API. Credentials must + /// be set on both sides (the Access-Control-Allow-Credentials header and in + /// the XHR or Fetch request) in order for the CORS request with credentials + /// to succeed. + (AccessControlAllowCredentials, ACCESS_CONTROL_ALLOW_CREDENTIALS, b"access-control-allow-credentials"); + + /// Preflight response indicating permitted HTTP headers. + /// + /// The Access-Control-Allow-Headers response header is used in response to + /// a preflight request to indicate which HTTP headers will be available via + /// Access-Control-Expose-Headers when making the actual request. + /// + /// The simple headers, Accept, Accept-Language, Content-Language, + /// Content-Type (but only with a MIME type of its parsed value (ignoring + /// parameters) of either application/x-www-form-urlencoded, + /// multipart/form-data, or text/plain), are always available and don't need + /// to be listed by this header. + /// + /// This header is required if the request has an + /// Access-Control-Request-Headers header. + (AccessControlAllowHeaders, ACCESS_CONTROL_ALLOW_HEADERS, b"access-control-allow-headers"); + + /// Preflight header response indicating permitted access methods. + /// + /// The Access-Control-Allow-Methods response header specifies the method or + /// methods allowed when accessing the resource in response to a preflight + /// request. + (AccessControlAllowMethods, ACCESS_CONTROL_ALLOW_METHODS, b"access-control-allow-methods"); + + /// Indicates whether the response can be shared with resources with the + /// given origin. + (AccessControlAllowOrigin, ACCESS_CONTROL_ALLOW_ORIGIN, b"access-control-allow-origin"); + + /// Indicates which headers can be exposed as part of the response by + /// listing their names. + (AccessControlExposeHeaders, ACCESS_CONTROL_EXPOSE_HEADERS, b"access-control-expose-headers"); + + /// Indicates how long the results of a preflight request can be cached. + (AccessControlMaxAge, ACCESS_CONTROL_MAX_AGE, b"access-control-max-age"); + + /// Informs the server which HTTP headers will be used when an actual + /// request is made. + (AccessControlRequestHeaders, ACCESS_CONTROL_REQUEST_HEADERS, b"access-control-request-headers"); + + /// Informs the server know which HTTP method will be used when the actual + /// request is made. + (AccessControlRequestMethod, ACCESS_CONTROL_REQUEST_METHOD, b"access-control-request-method"); + + /// Indicates the time in seconds the object has been in a proxy cache. + /// + /// The Age header is usually close to zero. If it is Age: 0, it was + /// probably just fetched from the origin server; otherwise It is usually + /// calculated as a difference between the proxy's current date and the Date + /// general header included in the HTTP response. + (Age, AGE, b"age"); + + /// Lists the set of methods support by a resource. + /// + /// This header must be sent if the server responds with a 405 Method Not + /// Allowed status code to indicate which request methods can be used. An + /// empty Allow header indicates that the resource allows no request + /// methods, which might occur temporarily for a given resource, for + /// example. + (Allow, ALLOW, b"allow"); + + /// Advertises the availability of alternate services to clients. + (AltSvc, ALT_SVC, b"alt-svc"); + + /// Contains the credentials to authenticate a user agent with a server. + /// + /// Usually this header is included after the server has responded with a + /// 401 Unauthorized status and the WWW-Authenticate header. + (Authorization, AUTHORIZATION, b"authorization"); + + /// Specifies directives for caching mechanisms in both requests and + /// responses. + /// + /// Caching directives are unidirectional, meaning that a given directive in + /// a request is not implying that the same directive is to be given in the + /// response. + (CacheControl, CACHE_CONTROL, b"cache-control"); + + /// Indicates how caches have handled a response and its corresponding request. + /// + /// See [RFC 9211](https://www.rfc-editor.org/rfc/rfc9211.html). + (CacheStatus, CACHE_STATUS, b"cache-status"); + + /// Specifies directives that allow origin servers to control the behavior of CDN caches + /// interposed between them and clients separately from other caches that might handle the + /// response. + /// + /// See [RFC 9213](https://www.rfc-editor.org/rfc/rfc9213.html). + (CdnCacheControl, CDN_CACHE_CONTROL, b"cdn-cache-control"); + + /// Controls whether or not the network connection stays open after the + /// current transaction finishes. + /// + /// If the value sent is keep-alive, the connection is persistent and not + /// closed, allowing for subsequent requests to the same server to be done. + /// + /// Except for the standard hop-by-hop headers (Keep-Alive, + /// Transfer-Encoding, TE, Connection, Trailer, Upgrade, Proxy-Authorization + /// and Proxy-Authenticate), any hop-by-hop headers used by the message must + /// be listed in the Connection header, so that the first proxy knows he has + /// to consume them and not to forward them further. Standard hop-by-hop + /// headers can be listed too (it is often the case of Keep-Alive, but this + /// is not mandatory. + (Connection, CONNECTION, b"connection"); + + /// Indicates if the content is expected to be displayed inline. + /// + /// In a regular HTTP response, the Content-Disposition response header is a + /// header indicating if the content is expected to be displayed inline in + /// the browser, that is, as a Web page or as part of a Web page, or as an + /// attachment, that is downloaded and saved locally. + /// + /// In a multipart/form-data body, the HTTP Content-Disposition general + /// header is a header that can be used on the subpart of a multipart body + /// to give information about the field it applies to. The subpart is + /// delimited by the boundary defined in the Content-Type header. Used on + /// the body itself, Content-Disposition has no effect. + /// + /// The Content-Disposition header is defined in the larger context of MIME + /// messages for e-mail, but only a subset of the possible parameters apply + /// to HTTP forms and POST requests. Only the value form-data, as well as + /// the optional directive name and filename, can be used in the HTTP + /// context. + (ContentDisposition, CONTENT_DISPOSITION, b"content-disposition"); + + /// Used to compress the media-type. + /// + /// When present, its value indicates what additional content encoding has + /// been applied to the entity-body. It lets the client know, how to decode + /// in order to obtain the media-type referenced by the Content-Type header. + /// + /// It is recommended to compress data as much as possible and therefore to + /// use this field, but some types of resources, like jpeg images, are + /// already compressed. Sometimes using additional compression doesn't + /// reduce payload size and can even make the payload longer. + (ContentEncoding, CONTENT_ENCODING, b"content-encoding"); + + /// Used to describe the languages intended for the audience. + /// + /// This header allows a user to differentiate according to the users' own + /// preferred language. For example, if "Content-Language: de-DE" is set, it + /// says that the document is intended for German language speakers + /// (however, it doesn't indicate the document is written in German. For + /// example, it might be written in English as part of a language course for + /// German speakers). + /// + /// If no Content-Language is specified, the default is that the content is + /// intended for all language audiences. Multiple language tags are also + /// possible, as well as applying the Content-Language header to various + /// media types and not only to textual documents. + (ContentLanguage, CONTENT_LANGUAGE, b"content-language"); + + /// Indicates the size of the entity-body. + /// + /// The header value must be a decimal indicating the number of octets sent + /// to the recipient. + (ContentLength, CONTENT_LENGTH, b"content-length"); + + /// Indicates an alternate location for the returned data. + /// + /// The principal use case is to indicate the URL of the resource + /// transmitted as the result of content negotiation. + /// + /// Location and Content-Location are different: Location indicates the + /// target of a redirection (or the URL of a newly created document), while + /// Content-Location indicates the direct URL to use to access the resource, + /// without the need of further content negotiation. Location is a header + /// associated with the response, while Content-Location is associated with + /// the entity returned. + (ContentLocation, CONTENT_LOCATION, b"content-location"); + + /// Indicates where in a full body message a partial message belongs. + (ContentRange, CONTENT_RANGE, b"content-range"); + + /// Allows controlling resources the user agent is allowed to load for a + /// given page. + /// + /// With a few exceptions, policies mostly involve specifying server origins + /// and script endpoints. This helps guard against cross-site scripting + /// attacks (XSS). + (ContentSecurityPolicy, CONTENT_SECURITY_POLICY, b"content-security-policy"); + + /// Allows experimenting with policies by monitoring their effects. + /// + /// The HTTP Content-Security-Policy-Report-Only response header allows web + /// developers to experiment with policies by monitoring (but not enforcing) + /// their effects. These violation reports consist of JSON documents sent + /// via an HTTP POST request to the specified URI. + (ContentSecurityPolicyReportOnly, CONTENT_SECURITY_POLICY_REPORT_ONLY, b"content-security-policy-report-only"); + + /// Used to indicate the media type of the resource. + /// + /// In responses, a Content-Type header tells the client what the content + /// type of the returned content actually is. Browsers will do MIME sniffing + /// in some cases and will not necessarily follow the value of this header; + /// to prevent this behavior, the header X-Content-Type-Options can be set + /// to nosniff. + /// + /// In requests, (such as POST or PUT), the client tells the server what + /// type of data is actually sent. + (ContentType, CONTENT_TYPE, b"content-type"); + + /// Contains stored HTTP cookies previously sent by the server with the + /// Set-Cookie header. + /// + /// The Cookie header might be omitted entirely, if the privacy setting of + /// the browser are set to block them, for example. + (Cookie, COOKIE, b"cookie"); + + /// Indicates the client's tracking preference. + /// + /// This header lets users indicate whether they would prefer privacy rather + /// than personalized content. + (Dnt, DNT, b"dnt"); + + /// Contains the date and time at which the message was originated. + (Date, DATE, b"date"); + + /// Identifier for a specific version of a resource. + /// + /// This header allows caches to be more efficient, and saves bandwidth, as + /// a web server does not need to send a full response if the content has + /// not changed. On the other side, if the content has changed, etags are + /// useful to help prevent simultaneous updates of a resource from + /// overwriting each other ("mid-air collisions"). + /// + /// If the resource at a given URL changes, a new Etag value must be + /// generated. Etags are therefore similar to fingerprints and might also be + /// used for tracking purposes by some servers. A comparison of them allows + /// to quickly determine whether two representations of a resource are the + /// same, but they might also be set to persist indefinitely by a tracking + /// server. + (Etag, ETAG, b"etag"); + + /// Indicates expectations that need to be fulfilled by the server in order + /// to properly handle the request. + /// + /// The only expectation defined in the specification is Expect: + /// 100-continue, to which the server shall respond with: + /// + /// * 100 if the information contained in the header is sufficient to cause + /// an immediate success, + /// + /// * 417 (Expectation Failed) if it cannot meet the expectation; or any + /// other 4xx status otherwise. + /// + /// For example, the server may reject a request if its Content-Length is + /// too large. + /// + /// No common browsers send the Expect header, but some other clients such + /// as cURL do so by default. + (Expect, EXPECT, b"expect"); + + /// Contains the date/time after which the response is considered stale. + /// + /// Invalid dates, like the value 0, represent a date in the past and mean + /// that the resource is already expired. + /// + /// If there is a Cache-Control header with the "max-age" or "s-max-age" + /// directive in the response, the Expires header is ignored. + (Expires, EXPIRES, b"expires"); + + /// Contains information from the client-facing side of proxy servers that + /// is altered or lost when a proxy is involved in the path of the request. + /// + /// The alternative and de-facto standard versions of this header are the + /// X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto headers. + /// + /// This header is used for debugging, statistics, and generating + /// location-dependent content and by design it exposes privacy sensitive + /// information, such as the IP address of the client. Therefore the user's + /// privacy must be kept in mind when deploying this header. + (Forwarded, FORWARDED, b"forwarded"); + + /// Contains an Internet email address for a human user who controls the + /// requesting user agent. + /// + /// If you are running a robotic user agent (e.g. a crawler), the From + /// header should be sent, so you can be contacted if problems occur on + /// servers, such as if the robot is sending excessive, unwanted, or invalid + /// requests. + (From, FROM, b"from"); + + /// Specifies the domain name of the server and (optionally) the TCP port + /// number on which the server is listening. + /// + /// If no port is given, the default port for the service requested (e.g., + /// "80" for an HTTP URL) is implied. + /// + /// A Host header field must be sent in all HTTP/1.1 request messages. A 400 + /// (Bad Request) status code will be sent to any HTTP/1.1 request message + /// that lacks a Host header field or contains more than one. + (Host, HOST, b"host"); + + /// Makes a request conditional based on the E-Tag. + /// + /// For GET and HEAD methods, the server will send back the requested + /// resource only if it matches one of the listed ETags. For PUT and other + /// non-safe methods, it will only upload the resource in this case. + /// + /// The comparison with the stored ETag uses the strong comparison + /// algorithm, meaning two files are considered identical byte to byte only. + /// This is weakened when the W/ prefix is used in front of the ETag. + /// + /// There are two common use cases: + /// + /// * For GET and HEAD methods, used in combination with an Range header, it + /// can guarantee that the new ranges requested comes from the same resource + /// than the previous one. If it doesn't match, then a 416 (Range Not + /// Satisfiable) response is returned. + /// + /// * For other methods, and in particular for PUT, If-Match can be used to + /// prevent the lost update problem. It can check if the modification of a + /// resource that the user wants to upload will not override another change + /// that has been done since the original resource was fetched. If the + /// request cannot be fulfilled, the 412 (Precondition Failed) response is + /// returned. + (IfMatch, IF_MATCH, b"if-match"); + + /// Makes a request conditional based on the modification date. + /// + /// The If-Modified-Since request HTTP header makes the request conditional: + /// the server will send back the requested resource, with a 200 status, + /// only if it has been last modified after the given date. If the request + /// has not been modified since, the response will be a 304 without any + /// body; the Last-Modified header will contain the date of last + /// modification. Unlike If-Unmodified-Since, If-Modified-Since can only be + /// used with a GET or HEAD. + /// + /// When used in combination with If-None-Match, it is ignored, unless the + /// server doesn't support If-None-Match. + /// + /// The most common use case is to update a cached entity that has no + /// associated ETag. + (IfModifiedSince, IF_MODIFIED_SINCE, b"if-modified-since"); + + /// Makes a request conditional based on the E-Tag. + /// + /// The If-None-Match HTTP request header makes the request conditional. For + /// GET and HEAD methods, the server will send back the requested resource, + /// with a 200 status, only if it doesn't have an ETag matching the given + /// ones. For other methods, the request will be processed only if the + /// eventually existing resource's ETag doesn't match any of the values + /// listed. + /// + /// When the condition fails for GET and HEAD methods, then the server must + /// return HTTP status code 304 (Not Modified). For methods that apply + /// server-side changes, the status code 412 (Precondition Failed) is used. + /// Note that the server generating a 304 response MUST generate any of the + /// following header fields that would have been sent in a 200 (OK) response + /// to the same request: Cache-Control, Content-Location, Date, ETag, + /// Expires, and Vary. + /// + /// The comparison with the stored ETag uses the weak comparison algorithm, + /// meaning two files are considered identical not only if they are + /// identical byte to byte, but if the content is equivalent. For example, + /// two pages that would differ only by the date of generation in the footer + /// would be considered as identical. + /// + /// When used in combination with If-Modified-Since, it has precedence (if + /// the server supports it). + /// + /// There are two common use cases: + /// + /// * For `GET` and `HEAD` methods, to update a cached entity that has an associated ETag. + /// * For other methods, and in particular for `PUT`, `If-None-Match` used with + /// the `*` value can be used to save a file not known to exist, + /// guaranteeing that another upload didn't happen before, losing the data + /// of the previous put; this problems is the variation of the lost update + /// problem. + (IfNoneMatch, IF_NONE_MATCH, b"if-none-match"); + + /// Makes a request conditional based on range. + /// + /// The If-Range HTTP request header makes a range request conditional: if + /// the condition is fulfilled, the range request will be issued and the + /// server sends back a 206 Partial Content answer with the appropriate + /// body. If the condition is not fulfilled, the full resource is sent back, + /// with a 200 OK status. + /// + /// This header can be used either with a Last-Modified validator, or with + /// an ETag, but not with both. + /// + /// The most common use case is to resume a download, to guarantee that the + /// stored resource has not been modified since the last fragment has been + /// received. + (IfRange, IF_RANGE, b"if-range"); + + /// Makes the request conditional based on the last modification date. + /// + /// The If-Unmodified-Since request HTTP header makes the request + /// conditional: the server will send back the requested resource, or accept + /// it in the case of a POST or another non-safe method, only if it has not + /// been last modified after the given date. If the request has been + /// modified after the given date, the response will be a 412 (Precondition + /// Failed) error. + /// + /// There are two common use cases: + /// + /// * In conjunction non-safe methods, like POST, it can be used to + /// implement an optimistic concurrency control, like done by some wikis: + /// editions are rejected if the stored document has been modified since the + /// original has been retrieved. + /// + /// * In conjunction with a range request with a If-Range header, it can be + /// used to ensure that the new fragment requested comes from an unmodified + /// document. + (IfUnmodifiedSince, IF_UNMODIFIED_SINCE, b"if-unmodified-since"); + + /// Content-Types that are acceptable for the response. + (LastModified, LAST_MODIFIED, b"last-modified"); + + /// Allows the server to point an interested client to another resource + /// containing metadata about the requested resource. + (Link, LINK, b"link"); + + /// Indicates the URL to redirect a page to. + /// + /// The Location response header indicates the URL to redirect a page to. It + /// only provides a meaning when served with a 3xx status response. + /// + /// The HTTP method used to make the new request to fetch the page pointed + /// to by Location depends of the original method and of the kind of + /// redirection: + /// + /// * If 303 (See Also) responses always lead to the use of a GET method, + /// 307 (Temporary Redirect) and 308 (Permanent Redirect) don't change the + /// method used in the original request; + /// + /// * 301 (Permanent Redirect) and 302 (Found) doesn't change the method + /// most of the time, though older user-agents may (so you basically don't + /// know). + /// + /// All responses with one of these status codes send a Location header. + /// + /// Beside redirect response, messages with 201 (Created) status also + /// include the Location header. It indicates the URL to the newly created + /// resource. + /// + /// Location and Content-Location are different: Location indicates the + /// target of a redirection (or the URL of a newly created resource), while + /// Content-Location indicates the direct URL to use to access the resource + /// when content negotiation happened, without the need of further content + /// negotiation. Location is a header associated with the response, while + /// Content-Location is associated with the entity returned. + (Location, LOCATION, b"location"); + + /// Indicates the max number of intermediaries the request should be sent + /// through. + (MaxForwards, MAX_FORWARDS, b"max-forwards"); + + /// Indicates where a fetch originates from. + /// + /// It doesn't include any path information, but only the server name. It is + /// sent with CORS requests, as well as with POST requests. It is similar to + /// the Referer header, but, unlike this header, it doesn't disclose the + /// whole path. + (Origin, ORIGIN, b"origin"); + + /// HTTP/1.0 header usually used for backwards compatibility. + /// + /// The Pragma HTTP/1.0 general header is an implementation-specific header + /// that may have various effects along the request-response chain. It is + /// used for backwards compatibility with HTTP/1.0 caches where the + /// Cache-Control HTTP/1.1 header is not yet present. + (Pragma, PRAGMA, b"pragma"); + + /// Defines the authentication method that should be used to gain access to + /// a proxy. + /// + /// Unlike `www-authenticate`, the `proxy-authenticate` header field applies + /// only to the next outbound client on the response chain. This is because + /// only the client that chose a given proxy is likely to have the + /// credentials necessary for authentication. However, when multiple proxies + /// are used within the same administrative domain, such as office and + /// regional caching proxies within a large corporate network, it is common + /// for credentials to be generated by the user agent and passed through the + /// hierarchy until consumed. Hence, in such a configuration, it will appear + /// as if Proxy-Authenticate is being forwarded because each proxy will send + /// the same challenge set. + /// + /// The `proxy-authenticate` header is sent along with a `407 Proxy + /// Authentication Required`. + (ProxyAuthenticate, PROXY_AUTHENTICATE, b"proxy-authenticate"); + + /// Contains the credentials to authenticate a user agent to a proxy server. + /// + /// This header is usually included after the server has responded with a + /// 407 Proxy Authentication Required status and the Proxy-Authenticate + /// header. + (ProxyAuthorization, PROXY_AUTHORIZATION, b"proxy-authorization"); + + /// Associates a specific cryptographic public key with a certain server. + /// + /// This decreases the risk of MITM attacks with forged certificates. If one + /// or several keys are pinned and none of them are used by the server, the + /// browser will not accept the response as legitimate, and will not display + /// it. + (PublicKeyPins, PUBLIC_KEY_PINS, b"public-key-pins"); + + /// Sends reports of pinning violation to the report-uri specified in the + /// header. + /// + /// Unlike `Public-Key-Pins`, this header still allows browsers to connect + /// to the server if the pinning is violated. + (PublicKeyPinsReportOnly, PUBLIC_KEY_PINS_REPORT_ONLY, b"public-key-pins-report-only"); + + /// Indicates the part of a document that the server should return. + /// + /// Several parts can be requested with one Range header at once, and the + /// server may send back these ranges in a multipart document. If the server + /// sends back ranges, it uses the 206 Partial Content for the response. If + /// the ranges are invalid, the server returns the 416 Range Not Satisfiable + /// error. The server can also ignore the Range header and return the whole + /// document with a 200 status code. + (Range, RANGE, b"range"); + + /// Contains the address of the previous web page from which a link to the + /// currently requested page was followed. + /// + /// The Referer header allows servers to identify where people are visiting + /// them from and may use that data for analytics, logging, or optimized + /// caching, for example. + (Referer, REFERER, b"referer"); + + /// Governs which referrer information should be included with requests + /// made. + (ReferrerPolicy, REFERRER_POLICY, b"referrer-policy"); + + /// Informs the web browser that the current page or frame should be + /// refreshed. + (Refresh, REFRESH, b"refresh"); + + /// The Retry-After response HTTP header indicates how long the user agent + /// should wait before making a follow-up request. There are two main cases + /// this header is used: + /// + /// * When sent with a 503 (Service Unavailable) response, it indicates how + /// long the service is expected to be unavailable. + /// + /// * When sent with a redirect response, such as 301 (Moved Permanently), + /// it indicates the minimum time that the user agent is asked to wait + /// before issuing the redirected request. + (RetryAfter, RETRY_AFTER, b"retry-after"); + + /// The |Sec-WebSocket-Accept| header field is used in the WebSocket + /// opening handshake. It is sent from the server to the client to + /// confirm that the server is willing to initiate the WebSocket + /// connection. + (SecWebSocketAccept, SEC_WEBSOCKET_ACCEPT, b"sec-websocket-accept"); + + /// The |Sec-WebSocket-Extensions| header field is used in the WebSocket + /// opening handshake. It is initially sent from the client to the + /// server, and then subsequently sent from the server to the client, to + /// agree on a set of protocol-level extensions to use for the duration + /// of the connection. + (SecWebSocketExtensions, SEC_WEBSOCKET_EXTENSIONS, b"sec-websocket-extensions"); + + /// The |Sec-WebSocket-Key| header field is used in the WebSocket opening + /// handshake. It is sent from the client to the server to provide part + /// of the information used by the server to prove that it received a + /// valid WebSocket opening handshake. This helps ensure that the server + /// does not accept connections from non-WebSocket clients (e.g., HTTP + /// clients) that are being abused to send data to unsuspecting WebSocket + /// servers. + (SecWebSocketKey, SEC_WEBSOCKET_KEY, b"sec-websocket-key"); + + /// The |Sec-WebSocket-Protocol| header field is used in the WebSocket + /// opening handshake. It is sent from the client to the server and back + /// from the server to the client to confirm the subprotocol of the + /// connection. This enables scripts to both select a subprotocol and be + /// sure that the server agreed to serve that subprotocol. + (SecWebSocketProtocol, SEC_WEBSOCKET_PROTOCOL, b"sec-websocket-protocol"); + + /// The |Sec-WebSocket-Version| header field is used in the WebSocket + /// opening handshake. It is sent from the client to the server to + /// indicate the protocol version of the connection. This enables + /// servers to correctly interpret the opening handshake and subsequent + /// data being sent from the data, and close the connection if the server + /// cannot interpret that data in a safe manner. + (SecWebSocketVersion, SEC_WEBSOCKET_VERSION, b"sec-websocket-version"); + + /// Contains information about the software used by the origin server to + /// handle the request. + /// + /// Overly long and detailed Server values should be avoided as they + /// potentially reveal internal implementation details that might make it + /// (slightly) easier for attackers to find and exploit known security + /// holes. + (Server, SERVER, b"server"); + + /// Used to send cookies from the server to the user agent. + (SetCookie, SET_COOKIE, b"set-cookie"); + + /// Tells the client to communicate with HTTPS instead of using HTTP. + (StrictTransportSecurity, STRICT_TRANSPORT_SECURITY, b"strict-transport-security"); + + /// Informs the server of transfer encodings willing to be accepted as part + /// of the response. + /// + /// See also the Transfer-Encoding response header for more details on + /// transfer encodings. Note that chunked is always acceptable for HTTP/1.1 + /// recipients and you that don't have to specify "chunked" using the TE + /// header. However, it is useful for setting if the client is accepting + /// trailer fields in a chunked transfer coding using the "trailers" value. + (Te, TE, b"te"); + + /// Allows the sender to include additional fields at the end of chunked + /// messages. + (Trailer, TRAILER, b"trailer"); + + /// Specifies the form of encoding used to safely transfer the entity to the + /// client. + /// + /// `transfer-encoding` is a hop-by-hop header, that is applying to a + /// message between two nodes, not to a resource itself. Each segment of a + /// multi-node connection can use different `transfer-encoding` values. If + /// you want to compress data over the whole connection, use the end-to-end + /// header `content-encoding` header instead. + /// + /// When present on a response to a `HEAD` request that has no body, it + /// indicates the value that would have applied to the corresponding `GET` + /// message. + (TransferEncoding, TRANSFER_ENCODING, b"transfer-encoding"); + + /// Contains a string that allows identifying the requesting client's + /// software. + (UserAgent, USER_AGENT, b"user-agent"); + + /// Used as part of the exchange to upgrade the protocol. + (Upgrade, UPGRADE, b"upgrade"); + + /// Sends a signal to the server expressing the client’s preference for an + /// encrypted and authenticated response. + (UpgradeInsecureRequests, UPGRADE_INSECURE_REQUESTS, b"upgrade-insecure-requests"); + + /// Determines how to match future requests with cached responses. + /// + /// The `vary` HTTP response header determines how to match future request + /// headers to decide whether a cached response can be used rather than + /// requesting a fresh one from the origin server. It is used by the server + /// to indicate which headers it used when selecting a representation of a + /// resource in a content negotiation algorithm. + /// + /// The `vary` header should be set on a 304 Not Modified response exactly + /// like it would have been set on an equivalent 200 OK response. + (Vary, VARY, b"vary"); + + /// Added by proxies to track routing. + /// + /// The `via` general header is added by proxies, both forward and reverse + /// proxies, and can appear in the request headers and the response headers. + /// It is used for tracking message forwards, avoiding request loops, and + /// identifying the protocol capabilities of senders along the + /// request/response chain. + (Via, VIA, b"via"); + + /// General HTTP header contains information about possible problems with + /// the status of the message. + /// + /// More than one `warning` header may appear in a response. Warning header + /// fields can in general be applied to any message, however some warn-codes + /// are specific to caches and can only be applied to response messages. + (Warning, WARNING, b"warning"); + + /// Defines the authentication method that should be used to gain access to + /// a resource. + (WwwAuthenticate, WWW_AUTHENTICATE, b"www-authenticate"); + + /// Marker used by the server to indicate that the MIME types advertised in + /// the `content-type` headers should not be changed and be followed. + /// + /// This allows to opt-out of MIME type sniffing, or, in other words, it is + /// a way to say that the webmasters knew what they were doing. + /// + /// This header was introduced by Microsoft in IE 8 as a way for webmasters + /// to block content sniffing that was happening and could transform + /// non-executable MIME types into executable MIME types. Since then, other + /// browsers have introduced it, even if their MIME sniffing algorithms were + /// less aggressive. + /// + /// Site security testers usually expect this header to be set. + (XContentTypeOptions, X_CONTENT_TYPE_OPTIONS, b"x-content-type-options"); + + /// Controls DNS prefetching. + /// + /// The `x-dns-prefetch-control` HTTP response header controls DNS + /// prefetching, a feature by which browsers proactively perform domain name + /// resolution on both links that the user may choose to follow as well as + /// URLs for items referenced by the document, including images, CSS, + /// JavaScript, and so forth. + /// + /// This prefetching is performed in the background, so that the DNS is + /// likely to have been resolved by the time the referenced items are + /// needed. This reduces latency when the user clicks a link. + (XDnsPrefetchControl, X_DNS_PREFETCH_CONTROL, b"x-dns-prefetch-control"); + + /// Indicates whether or not a browser should be allowed to render a page in + /// a frame. + /// + /// Sites can use this to avoid clickjacking attacks, by ensuring that their + /// content is not embedded into other sites. + /// + /// The added security is only provided if the user accessing the document + /// is using a browser supporting `x-frame-options`. + (XFrameOptions, X_FRAME_OPTIONS, b"x-frame-options"); + + /// Stop pages from loading when an XSS attack is detected. + /// + /// The HTTP X-XSS-Protection response header is a feature of Internet + /// Explorer, Chrome and Safari that stops pages from loading when they + /// detect reflected cross-site scripting (XSS) attacks. Although these + /// protections are largely unnecessary in modern browsers when sites + /// implement a strong Content-Security-Policy that disables the use of + /// inline JavaScript ('unsafe-inline'), they can still provide protections + /// for users of older web browsers that don't yet support CSP. + (XXssProtection, X_XSS_PROTECTION, b"x-xss-protection"); +} + +/// Valid header name characters +/// +/// ```not_rust +/// field-name = token +/// separators = "(" | ")" | "<" | ">" | "@" +/// | "," | ";" | ":" | "\" | <"> +/// | "/" | "[" | "]" | "?" | "=" +/// | "{" | "}" | SP | HT +/// token = 1*tchar +/// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" +/// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" +/// / DIGIT / ALPHA +/// ; any VCHAR, except delimiters +/// ``` +// HEADER_CHARS maps every byte that is 128 or larger to 0 so everything that is +// mapped by HEADER_CHARS, maps to a valid single-byte UTF-8 codepoint. +const HEADER_CHARS: [u8; 256] = [ + // 0 1 2 3 4 5 6 7 8 9 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 0, 0, 0, b'!', b'"', b'#', b'$', b'%', b'&', b'\'', // 3x + 0, 0, b'*', b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x + b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0, // 5x + 0, 0, 0, 0, 0, b'a', b'b', b'c', b'd', b'e', // 6x + b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', // 7x + b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', // 8x + b'z', 0, 0, 0, b'^', b'_', b'`', b'a', b'b', b'c', // 9x + b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x + b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x + b'x', b'y', b'z', 0, b'|', 0, b'~', 0, 0, 0, // 12x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x + 0, 0, 0, 0, 0, 0 // 25x +]; + +/// Valid header name characters for HTTP/2.0 and HTTP/3.0 +// HEADER_CHARS_H2 maps every byte that is 128 or larger to 0 so everything that is +// mapped by HEADER_CHARS_H2, maps to a valid single-byte UTF-8 codepoint. +const HEADER_CHARS_H2: [u8; 256] = [ + // 0 1 2 3 4 5 6 7 8 9 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 0, 0, 0, b'!', b'"', b'#', b'$', b'%', b'&', b'\'', // 3x + 0, 0, b'*', b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x + b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0, // 5x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, b'^', b'_', b'`', b'a', b'b', b'c', // 9x + b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x + b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x + b'x', b'y', b'z', 0, b'|', 0, b'~', 0, 0, 0, // 12x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x + 0, 0, 0, 0, 0, 0 // 25x +]; + +fn parse_hdr<'a>( + data: &'a [u8], + b: &'a mut [MaybeUninit; SCRATCH_BUF_SIZE], + table: &[u8; 256], +) -> Result, InvalidHeaderName> { + match data.len() { + 0 => Err(InvalidHeaderName::new()), + len @ 1..=SCRATCH_BUF_SIZE => { + // Read from data into the buffer - transforming using `table` as we go + data.iter() + .zip(b.iter_mut()) + .for_each(|(index, out)| *out = MaybeUninit::new(table[*index as usize])); + // Safety: len bytes of b were just initialized. + let name: &'a [u8] = unsafe { slice_assume_init(&b[0..len]) }; + match StandardHeader::from_bytes(name) { + Some(sh) => Ok(sh.into()), + None => { + if name.contains(&0) { + Err(InvalidHeaderName::new()) + } else { + Ok(HdrName::custom(name, true)) + } + } + } + } + SCRATCH_BUF_OVERFLOW..=super::MAX_HEADER_NAME_LEN => Ok(HdrName::custom(data, false)), + _ => Err(InvalidHeaderName::new()), + } +} + + + +impl<'a> From for HdrName<'a> { + fn from(hdr: StandardHeader) -> HdrName<'a> { + HdrName { inner: Repr::Standard(hdr) } + } +} + +impl HeaderName { + /// Converts a slice of bytes to an HTTP header name. + /// + /// This function normalizes the input. + pub fn from_bytes(src: &[u8]) -> Result { + let mut buf = uninit_u8_array(); + // Precondition: HEADER_CHARS is a valid table for parse_hdr(). + match parse_hdr(src, &mut buf, &HEADER_CHARS)?.inner { + Repr::Standard(std) => Ok(std.into()), + Repr::Custom(MaybeLower { buf, lower: true }) => { + let buf = Bytes::copy_from_slice(buf); + // Safety: the invariant on MaybeLower ensures buf is valid UTF-8. + let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; + Ok(Custom(val).into()) + } + Repr::Custom(MaybeLower { buf, lower: false }) => { + use bytes::{BufMut}; + let mut dst = BytesMut::with_capacity(buf.len()); + + for b in buf.iter() { + // HEADER_CHARS maps all bytes to valid single-byte UTF-8 + let b = HEADER_CHARS[*b as usize]; + + if b == 0 { + return Err(InvalidHeaderName::new()); + } + + dst.put_u8(b); + } + + // Safety: the loop above maps all bytes in buf to valid single byte + // UTF-8 before copying them into dst. This means that dst (and hence + // dst.freeze()) is valid UTF-8. + let val = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) }; + + Ok(Custom(val).into()) + } + } + } + + /// Converts a slice of bytes to an HTTP header name. + /// + /// This function expects the input to only contain lowercase characters. + /// This is useful when decoding HTTP/2.0 or HTTP/3.0 headers. Both + /// require that all headers be represented in lower case. + /// + /// # Examples + /// + /// ``` + /// # use http::header::*; + /// + /// // Parsing a lower case header + /// let hdr = HeaderName::from_lowercase(b"content-length").unwrap(); + /// assert_eq!(CONTENT_LENGTH, hdr); + /// + /// // Parsing a header that contains uppercase characters + /// assert!(HeaderName::from_lowercase(b"Content-Length").is_err()); + /// ``` + pub fn from_lowercase(src: &[u8]) -> Result { + let mut buf = uninit_u8_array(); + // Precondition: HEADER_CHARS_H2 is a valid table for parse_hdr() + match parse_hdr(src, &mut buf, &HEADER_CHARS_H2)?.inner { + Repr::Standard(std) => Ok(std.into()), + Repr::Custom(MaybeLower { buf, lower: true }) => { + let buf = Bytes::copy_from_slice(buf); + // Safety: the invariant on MaybeLower ensures buf is valid UTF-8. + let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; + Ok(Custom(val).into()) + } + Repr::Custom(MaybeLower { buf, lower: false }) => { + for &b in buf.iter() { + // HEADER_CHARS maps all bytes that are not valid single-byte + // UTF-8 to 0 so this check returns an error for invalid UTF-8. + if b != HEADER_CHARS[b as usize] { + return Err(InvalidHeaderName::new()); + } + } + + let buf = Bytes::copy_from_slice(buf); + // Safety: the loop above checks that each byte of buf (either + // version) is valid UTF-8. + let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; + Ok(Custom(val).into()) + } + } + } + + /// Converts a static string to a HTTP header name. + /// + /// This function requires the static string to only contain lowercase + /// characters, numerals and symbols, as per the HTTP/2.0 specification + /// and header names internal representation within this library. + /// + /// # Panics + /// + /// This function panics when the static string is a invalid header. + /// + /// Until [Allow panicking in constants](https://github.com/rust-lang/rfcs/pull/2345) + /// makes its way into stable, the panic message at compile-time is + /// going to look cryptic, but should at least point at your header value: + /// + /// ```text + /// error: any use of this value will cause an error + /// --> http/src/header/name.rs:1241:13 + /// | + /// 1241 | ([] as [u8; 0])[0]; // Invalid header name + /// | ^^^^^^^^^^^^^^^^^^ + /// | | + /// | index out of bounds: the length is 0 but the index is 0 + /// | inside `http::HeaderName::from_static` at http/src/header/name.rs:1241:13 + /// | inside `INVALID_NAME` at src/main.rs:3:34 + /// | + /// ::: src/main.rs:3:1 + /// | + /// 3 | const INVALID_NAME: HeaderName = HeaderName::from_static("Capitalized"); + /// | ------------------------------------------------------------------------ + /// ``` + /// + /// # Examples + /// + /// ``` + /// # use http::header::*; + /// // Parsing a standard header + /// let hdr = HeaderName::from_static("content-length"); + /// assert_eq!(CONTENT_LENGTH, hdr); + /// + /// // Parsing a custom header + /// let CUSTOM_HEADER: &'static str = "custom-header"; + /// + /// let a = HeaderName::from_lowercase(b"custom-header").unwrap(); + /// let b = HeaderName::from_static(CUSTOM_HEADER); + /// assert_eq!(a, b); + /// ``` + /// + /// ```should_panic + /// # use http::header::*; + /// # + /// // Parsing a header that contains invalid symbols(s): + /// HeaderName::from_static("content{}{}length"); // This line panics! + /// + /// // Parsing a header that contains invalid uppercase characters. + /// let a = HeaderName::from_static("foobar"); + /// let b = HeaderName::from_static("FOOBAR"); // This line panics! + /// ``` + #[allow(unconditional_panic)] // required for the panic circumvention + pub const fn from_static(src: &'static str) -> HeaderName { + let name_bytes = src.as_bytes(); + if let Some(standard) = StandardHeader::from_bytes(name_bytes) { + return HeaderName{ + inner: Repr::Standard(standard), + }; + } + + if name_bytes.len() == 0 || name_bytes.len() > super::MAX_HEADER_NAME_LEN || { + let mut i = 0; + loop { + if i >= name_bytes.len() { + break false; + } else if HEADER_CHARS_H2[name_bytes[i] as usize] == 0 { + break true; + } + i += 1; + } + } { + ([] as [u8; 0])[0]; // Invalid header name + } + + HeaderName { + inner: Repr::Custom(Custom(ByteStr::from_static(src))) + } + } + + /// Returns a `str` representation of the header. + /// + /// The returned string will always be lower case. + #[inline] + pub fn as_str(&self) -> &str { + match self.inner { + Repr::Standard(v) => v.as_str(), + Repr::Custom(ref v) => &*v.0, + } + } + + pub(super) fn into_bytes(self) -> Bytes { + self.inner.into() + } +} + +impl FromStr for HeaderName { + type Err = InvalidHeaderName; + + fn from_str(s: &str) -> Result { + HeaderName::from_bytes(s.as_bytes()).map_err(|_| InvalidHeaderName { _priv: () }) + } +} + +impl AsRef for HeaderName { + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl AsRef<[u8]> for HeaderName { + fn as_ref(&self) -> &[u8] { + self.as_str().as_bytes() + } +} + +impl Borrow for HeaderName { + fn borrow(&self) -> &str { + self.as_str() + } +} + +impl fmt::Debug for HeaderName { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self.as_str(), fmt) + } +} + +impl fmt::Display for HeaderName { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self.as_str(), fmt) + } +} + +impl InvalidHeaderName { + fn new() -> InvalidHeaderName { + InvalidHeaderName { _priv: () } + } +} + +impl<'a> From<&'a HeaderName> for HeaderName { + fn from(src: &'a HeaderName) -> HeaderName { + src.clone() + } +} + +#[doc(hidden)] +impl From> for Bytes +where + T: Into, +{ + fn from(repr: Repr) -> Bytes { + match repr { + Repr::Standard(header) => Bytes::from_static(header.as_str().as_bytes()), + Repr::Custom(header) => header.into(), + } + } +} + +impl From for Bytes { + #[inline] + fn from(Custom(inner): Custom) -> Bytes { + Bytes::from(inner) + } +} + +impl<'a> TryFrom<&'a str> for HeaderName { + type Error = InvalidHeaderName; + #[inline] + fn try_from(s: &'a str) -> Result { + Self::from_bytes(s.as_bytes()) + } +} + +impl<'a> TryFrom<&'a String> for HeaderName { + type Error = InvalidHeaderName; + #[inline] + fn try_from(s: &'a String) -> Result { + Self::from_bytes(s.as_bytes()) + } +} + +impl<'a> TryFrom<&'a [u8]> for HeaderName { + type Error = InvalidHeaderName; + #[inline] + fn try_from(s: &'a [u8]) -> Result { + Self::from_bytes(s) + } +} + +impl TryFrom for HeaderName { + type Error = InvalidHeaderName; + + #[inline] + fn try_from(s: String) -> Result { + Self::from_bytes(s.as_bytes()) + } +} + +impl TryFrom> for HeaderName { + type Error = InvalidHeaderName; + + #[inline] + fn try_from(vec: Vec) -> Result { + Self::from_bytes(&vec) + } +} + +#[doc(hidden)] +impl From for HeaderName { + fn from(src: StandardHeader) -> HeaderName { + HeaderName { + inner: Repr::Standard(src), + } + } +} + +#[doc(hidden)] +impl From for HeaderName { + fn from(src: Custom) -> HeaderName { + HeaderName { + inner: Repr::Custom(src), + } + } +} + +impl<'a> PartialEq<&'a HeaderName> for HeaderName { + #[inline] + fn eq(&self, other: &&'a HeaderName) -> bool { + *self == **other + } +} + +impl<'a> PartialEq for &'a HeaderName { + #[inline] + fn eq(&self, other: &HeaderName) -> bool { + *other == *self + } +} + +impl PartialEq for HeaderName { + /// Performs a case-insensitive comparison of the string against the header + /// name + /// + /// # Examples + /// + /// ``` + /// use http::header::CONTENT_LENGTH; + /// + /// assert_eq!(CONTENT_LENGTH, "content-length"); + /// assert_eq!(CONTENT_LENGTH, "Content-Length"); + /// assert_ne!(CONTENT_LENGTH, "content length"); + /// ``` + #[inline] + fn eq(&self, other: &str) -> bool { + eq_ignore_ascii_case(self.as_ref(), other.as_bytes()) + } +} + +impl PartialEq for str { + /// Performs a case-insensitive comparison of the string against the header + /// name + /// + /// # Examples + /// + /// ``` + /// use http::header::CONTENT_LENGTH; + /// + /// assert_eq!(CONTENT_LENGTH, "content-length"); + /// assert_eq!(CONTENT_LENGTH, "Content-Length"); + /// assert_ne!(CONTENT_LENGTH, "content length"); + /// ``` + #[inline] + fn eq(&self, other: &HeaderName) -> bool { + *other == *self + } +} + +impl<'a> PartialEq<&'a str> for HeaderName { + /// Performs a case-insensitive comparison of the string against the header + /// name + #[inline] + fn eq(&self, other: &&'a str) -> bool { + *self == **other + } +} + +impl<'a> PartialEq for &'a str { + /// Performs a case-insensitive comparison of the string against the header + /// name + #[inline] + fn eq(&self, other: &HeaderName) -> bool { + *other == *self + } +} + +impl fmt::Debug for InvalidHeaderName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("InvalidHeaderName") + // skip _priv noise + .finish() + } +} + +impl fmt::Display for InvalidHeaderName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("invalid HTTP header name") + } +} + +impl Error for InvalidHeaderName {} + +// ===== HdrName ===== + +impl<'a> HdrName<'a> { + // Precondition: if lower then buf is valid UTF-8 + fn custom(buf: &'a [u8], lower: bool) -> HdrName<'a> { + HdrName { + // Invariant (on MaybeLower): follows from the precondition + inner: Repr::Custom(MaybeLower { + buf: buf, + lower: lower, + }), + } + } + + pub fn from_bytes(hdr: &[u8], f: F) -> Result + where F: FnOnce(HdrName<'_>) -> U, + { + let mut buf = uninit_u8_array(); + // Precondition: HEADER_CHARS is a valid table for parse_hdr(). + let hdr = parse_hdr(hdr, &mut buf, &HEADER_CHARS)?; + Ok(f(hdr)) + } + + pub fn from_static(hdr: &'static str, f: F) -> U + where + F: FnOnce(HdrName<'_>) -> U, + { + let mut buf = uninit_u8_array(); + let hdr = + // Precondition: HEADER_CHARS is a valid table for parse_hdr(). + parse_hdr(hdr.as_bytes(), &mut buf, &HEADER_CHARS).expect("static str is invalid name"); + f(hdr) + } +} + +#[doc(hidden)] +impl<'a> From> for HeaderName { + fn from(src: HdrName<'a>) -> HeaderName { + match src.inner { + Repr::Standard(s) => HeaderName { + inner: Repr::Standard(s), + }, + Repr::Custom(maybe_lower) => { + if maybe_lower.lower { + let buf = Bytes::copy_from_slice(&maybe_lower.buf[..]); + // Safety: the invariant on MaybeLower ensures buf is valid UTF-8. + let byte_str = unsafe { ByteStr::from_utf8_unchecked(buf) }; + + HeaderName { + inner: Repr::Custom(Custom(byte_str)), + } + } else { + use bytes::BufMut; + let mut dst = BytesMut::with_capacity(maybe_lower.buf.len()); + + for b in maybe_lower.buf.iter() { + // HEADER_CHARS maps each byte to a valid single-byte UTF-8 + // codepoint. + dst.put_u8(HEADER_CHARS[*b as usize]); + } + + // Safety: the loop above maps each byte of maybe_lower.buf to a + // valid single-byte UTF-8 codepoint before copying it into dst. + // dst (and hence dst.freeze()) is thus valid UTF-8. + let buf = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) }; + + HeaderName { + inner: Repr::Custom(Custom(buf)), + } + } + } + } + } +} + +#[doc(hidden)] +impl<'a> PartialEq> for HeaderName { + #[inline] + fn eq(&self, other: &HdrName<'a>) -> bool { + match self.inner { + Repr::Standard(a) => match other.inner { + Repr::Standard(b) => a == b, + _ => false, + }, + Repr::Custom(Custom(ref a)) => match other.inner { + Repr::Custom(ref b) => { + if b.lower { + a.as_bytes() == b.buf + } else { + eq_ignore_ascii_case(a.as_bytes(), b.buf) + } + } + _ => false, + }, + } + } +} + +// ===== Custom ===== + +impl Hash for Custom { + #[inline] + fn hash(&self, hasher: &mut H) { + hasher.write(self.0.as_bytes()) + } +} + +// ===== MaybeLower ===== + +impl<'a> Hash for MaybeLower<'a> { + #[inline] + fn hash(&self, hasher: &mut H) { + if self.lower { + hasher.write(self.buf); + } else { + for &b in self.buf { + hasher.write(&[HEADER_CHARS[b as usize]]); + } + } + } +} + +// Assumes that the left hand side is already lower case +#[inline] +fn eq_ignore_ascii_case(lower: &[u8], s: &[u8]) -> bool { + if lower.len() != s.len() { + return false; + } + + lower.iter().zip(s).all(|(a, b)| { + *a == HEADER_CHARS[*b as usize] + }) +} + +// Utility functions for MaybeUninit<>. These are drawn from unstable API's on +// MaybeUninit<> itself. +const SCRATCH_BUF_SIZE: usize = 64; +const SCRATCH_BUF_OVERFLOW: usize = SCRATCH_BUF_SIZE + 1; + +fn uninit_u8_array() -> [MaybeUninit; SCRATCH_BUF_SIZE] { + let arr = MaybeUninit::<[MaybeUninit; SCRATCH_BUF_SIZE]>::uninit(); + // Safety: assume_init() is claiming that an array of MaybeUninit<> + // has been initilized, but MaybeUninit<>'s do not require initilizaton. + unsafe { arr.assume_init() } +} + +// Assuming all the elements are initilized, get a slice of them. +// +// Safety: All elements of `slice` must be initilized to prevent +// undefined behavior. +unsafe fn slice_assume_init(slice: &[MaybeUninit]) -> &[T] { + &*(slice as *const [MaybeUninit] as *const [T]) +} + +#[cfg(test)] +mod tests { + use super::*; + use self::StandardHeader::Vary; + + #[test] + fn test_bounds() { + fn check_bounds() {} + check_bounds::(); + } + + #[test] + fn test_parse_invalid_headers() { + for i in 0..128 { + let hdr = vec![1u8; i]; + assert!(HeaderName::from_bytes(&hdr).is_err(), "{} invalid header chars did not fail", i); + } + } + + const ONE_TOO_LONG: &[u8] = &[b'a'; super::super::MAX_HEADER_NAME_LEN+1]; + + #[test] + fn test_invalid_name_lengths() { + assert!( + HeaderName::from_bytes(&[]).is_err(), + "zero-length header name is an error", + ); + + let long = &ONE_TOO_LONG[0..super::super::MAX_HEADER_NAME_LEN]; + + let long_str = std::str::from_utf8(long).unwrap(); + assert_eq!(HeaderName::from_static(long_str), long_str); // shouldn't panic! + + assert!( + HeaderName::from_bytes(long).is_ok(), + "max header name length is ok", + ); + assert!( + HeaderName::from_bytes(ONE_TOO_LONG).is_err(), + "longer than max header name length is an error", + ); + } + + #[test] + #[should_panic] + fn test_static_invalid_name_lengths() { + // Safety: ONE_TOO_LONG contains only the UTF-8 safe, single-byte codepoint b'a'. + let _ = HeaderName::from_static(unsafe { std::str::from_utf8_unchecked(ONE_TOO_LONG) }); + } + + #[test] + fn test_from_hdr_name() { + use self::StandardHeader::Vary; + + let name = HeaderName::from(HdrName { + inner: Repr::Standard(Vary), + }); + + assert_eq!(name.inner, Repr::Standard(Vary)); + + let name = HeaderName::from(HdrName { + inner: Repr::Custom(MaybeLower { + buf: b"hello-world", + lower: true, + }), + }); + + assert_eq!(name.inner, Repr::Custom(Custom(ByteStr::from_static("hello-world")))); + + let name = HeaderName::from(HdrName { + inner: Repr::Custom(MaybeLower { + buf: b"Hello-World", + lower: false, + }), + }); + + assert_eq!(name.inner, Repr::Custom(Custom(ByteStr::from_static("hello-world")))); + } + + #[test] + fn test_eq_hdr_name() { + use self::StandardHeader::Vary; + + let a = HeaderName { inner: Repr::Standard(Vary) }; + let b = HdrName { inner: Repr::Standard(Vary) }; + + assert_eq!(a, b); + + let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("vaary"))) }; + assert_ne!(a, b); + + let b = HdrName { inner: Repr::Custom(MaybeLower { + buf: b"vaary", + lower: true, + })}; + + assert_eq!(a, b); + + let b = HdrName { inner: Repr::Custom(MaybeLower { + buf: b"vaary", + lower: false, + })}; + + assert_eq!(a, b); + + let b = HdrName { inner: Repr::Custom(MaybeLower { + buf: b"VAARY", + lower: false, + })}; + + assert_eq!(a, b); + + let a = HeaderName { inner: Repr::Standard(Vary) }; + assert_ne!(a, b); + } + + #[test] + fn test_from_static_std() { + let a = HeaderName { inner: Repr::Standard(Vary) }; + + let b = HeaderName::from_static("vary"); + assert_eq!(a, b); + + let b = HeaderName::from_static("vaary"); + assert_ne!(a, b); + } + + #[test] + #[should_panic] + fn test_from_static_std_uppercase() { + HeaderName::from_static("Vary"); + } + + #[test] + #[should_panic] + fn test_from_static_std_symbol() { + HeaderName::from_static("vary{}"); + } + + // MaybeLower { lower: true } + #[test] + fn test_from_static_custom_short() { + let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("customheader"))) }; + let b = HeaderName::from_static("customheader"); + assert_eq!(a, b); + } + + #[test] + #[should_panic] + fn test_from_static_custom_short_uppercase() { + HeaderName::from_static("custom header"); + } + + #[test] + #[should_panic] + fn test_from_static_custom_short_symbol() { + HeaderName::from_static("CustomHeader"); + } + + // MaybeLower { lower: false } + #[test] + fn test_from_static_custom_long() { + let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static( + "longer-than-63--thisheaderislongerthansixtythreecharactersandthushandleddifferent" + ))) }; + let b = HeaderName::from_static( + "longer-than-63--thisheaderislongerthansixtythreecharactersandthushandleddifferent" + ); + assert_eq!(a, b); + } + + #[test] + #[should_panic] + fn test_from_static_custom_long_uppercase() { + HeaderName::from_static( + "Longer-Than-63--ThisHeaderIsLongerThanSixtyThreeCharactersAndThusHandledDifferent" + ); + } + + #[test] + #[should_panic] + fn test_from_static_custom_long_symbol() { + HeaderName::from_static( + "longer-than-63--thisheader{}{}{}{}islongerthansixtythreecharactersandthushandleddifferent" + ); + } + + #[test] + fn test_from_static_custom_single_char() { + let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("a"))) }; + let b = HeaderName::from_static("a"); + assert_eq!(a, b); + } + + #[test] + #[should_panic] + fn test_from_static_empty() { + HeaderName::from_static(""); + } + + #[test] + fn test_all_tokens() { + HeaderName::from_static("!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyz"); + } +} diff --git a/third_party/rust/http/v1/crate/src/header/value.rs b/third_party/rust/http/v1/crate/src/header/value.rs new file mode 100644 index 000000000000..bf05f16f4eaf --- /dev/null +++ b/third_party/rust/http/v1/crate/src/header/value.rs @@ -0,0 +1,795 @@ +use bytes::{Bytes, BytesMut}; + +use std::convert::TryFrom; +use std::error::Error; +use std::fmt::Write; +use std::str::FromStr; +use std::{cmp, fmt, mem, str}; + +use crate::header::name::HeaderName; + +/// Represents an HTTP header field value. +/// +/// In practice, HTTP header field values are usually valid ASCII. However, the +/// HTTP spec allows for a header value to contain opaque bytes as well. In this +/// case, the header field value is not able to be represented as a string. +/// +/// To handle this, the `HeaderValue` is useable as a type and can be compared +/// with strings and implements `Debug`. A `to_str` fn is provided that returns +/// an `Err` if the header value contains non visible ascii characters. +#[derive(Clone, Hash)] +pub struct HeaderValue { + inner: Bytes, + is_sensitive: bool, +} + +/// A possible error when converting a `HeaderValue` from a string or byte +/// slice. +pub struct InvalidHeaderValue { + _priv: (), +} + +/// A possible error when converting a `HeaderValue` to a string representation. +/// +/// Header field values may contain opaque bytes, in which case it is not +/// possible to represent the value as a string. +#[derive(Debug)] +pub struct ToStrError { + _priv: (), +} + +impl HeaderValue { + /// Convert a static string to a `HeaderValue`. + /// + /// This function will not perform any copying, however the string is + /// checked to ensure that no invalid characters are present. Only visible + /// ASCII characters (32-127) are permitted. + /// + /// # Panics + /// + /// This function panics if the argument contains invalid header value + /// characters. + /// + /// Until [Allow panicking in constants](https://github.com/rust-lang/rfcs/pull/2345) + /// makes its way into stable, the panic message at compile-time is + /// going to look cryptic, but should at least point at your header value: + /// + /// ```text + /// error: any use of this value will cause an error + /// --> http/src/header/value.rs:67:17 + /// | + /// 67 | ([] as [u8; 0])[0]; // Invalid header value + /// | ^^^^^^^^^^^^^^^^^^ + /// | | + /// | index out of bounds: the length is 0 but the index is 0 + /// | inside `HeaderValue::from_static` at http/src/header/value.rs:67:17 + /// | inside `INVALID_HEADER` at src/main.rs:73:33 + /// | + /// ::: src/main.rs:73:1 + /// | + /// 73 | const INVALID_HEADER: HeaderValue = HeaderValue::from_static("жsome value"); + /// | ---------------------------------------------------------------------------- + /// ``` + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_static("hello"); + /// assert_eq!(val, "hello"); + /// ``` + #[inline] + #[allow(unconditional_panic)] // required for the panic circumvention + pub const fn from_static(src: &'static str) -> HeaderValue { + let bytes = src.as_bytes(); + let mut i = 0; + while i < bytes.len() { + if !is_visible_ascii(bytes[i]) { + ([] as [u8; 0])[0]; // Invalid header value + } + i += 1; + } + + HeaderValue { + inner: Bytes::from_static(bytes), + is_sensitive: false, + } + } + + /// Attempt to convert a string to a `HeaderValue`. + /// + /// If the argument contains invalid header value characters, an error is + /// returned. Only visible ASCII characters (32-127) are permitted. Use + /// `from_bytes` to create a `HeaderValue` that includes opaque octets + /// (128-255). + /// + /// This function is intended to be replaced in the future by a `TryFrom` + /// implementation once the trait is stabilized in std. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_str("hello").unwrap(); + /// assert_eq!(val, "hello"); + /// ``` + /// + /// An invalid value + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_str("\n"); + /// assert!(val.is_err()); + /// ``` + #[inline] + pub fn from_str(src: &str) -> Result { + HeaderValue::try_from_generic(src, |s| Bytes::copy_from_slice(s.as_bytes())) + } + + /// Converts a HeaderName into a HeaderValue + /// + /// Since every valid HeaderName is a valid HeaderValue this is done infallibly. + /// + /// # Examples + /// + /// ``` + /// # use http::header::{HeaderValue, HeaderName}; + /// # use http::header::ACCEPT; + /// let val = HeaderValue::from_name(ACCEPT); + /// assert_eq!(val, HeaderValue::from_bytes(b"accept").unwrap()); + /// ``` + #[inline] + pub fn from_name(name: HeaderName) -> HeaderValue { + name.into() + } + + /// Attempt to convert a byte slice to a `HeaderValue`. + /// + /// If the argument contains invalid header value bytes, an error is + /// returned. Only byte values between 32 and 255 (inclusive) are permitted, + /// excluding byte 127 (DEL). + /// + /// This function is intended to be replaced in the future by a `TryFrom` + /// implementation once the trait is stabilized in std. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_bytes(b"hello\xfa").unwrap(); + /// assert_eq!(val, &b"hello\xfa"[..]); + /// ``` + /// + /// An invalid value + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_bytes(b"\n"); + /// assert!(val.is_err()); + /// ``` + #[inline] + pub fn from_bytes(src: &[u8]) -> Result { + HeaderValue::try_from_generic(src, Bytes::copy_from_slice) + } + + /// Attempt to convert a `Bytes` buffer to a `HeaderValue`. + /// + /// This will try to prevent a copy if the type passed is the type used + /// internally, and will copy the data if it is not. + pub fn from_maybe_shared(src: T) -> Result + where + T: AsRef<[u8]> + 'static, + { + if_downcast_into!(T, Bytes, src, { + return HeaderValue::from_shared(src); + }); + + HeaderValue::from_bytes(src.as_ref()) + } + + /// Convert a `Bytes` directly into a `HeaderValue` without validating. + /// + /// This function does NOT validate that illegal bytes are not contained + /// within the buffer. + pub unsafe fn from_maybe_shared_unchecked(src: T) -> HeaderValue + where + T: AsRef<[u8]> + 'static, + { + if cfg!(debug_assertions) { + match HeaderValue::from_maybe_shared(src) { + Ok(val) => val, + Err(_err) => { + panic!("HeaderValue::from_maybe_shared_unchecked() with invalid bytes"); + } + } + } else { + + if_downcast_into!(T, Bytes, src, { + return HeaderValue { + inner: src, + is_sensitive: false, + }; + }); + + let src = Bytes::copy_from_slice(src.as_ref()); + HeaderValue { + inner: src, + is_sensitive: false, + } + } + } + + fn from_shared(src: Bytes) -> Result { + HeaderValue::try_from_generic(src, std::convert::identity) + } + + fn try_from_generic, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result { + for &b in src.as_ref() { + if !is_valid(b) { + return Err(InvalidHeaderValue { _priv: () }); + } + } + Ok(HeaderValue { + inner: into(src), + is_sensitive: false, + }) + } + + /// Yields a `&str` slice if the `HeaderValue` only contains visible ASCII + /// chars. + /// + /// This function will perform a scan of the header value, checking all the + /// characters. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_static("hello"); + /// assert_eq!(val.to_str().unwrap(), "hello"); + /// ``` + pub fn to_str(&self) -> Result<&str, ToStrError> { + let bytes = self.as_ref(); + + for &b in bytes { + if !is_visible_ascii(b) { + return Err(ToStrError { _priv: () }); + } + } + + unsafe { Ok(str::from_utf8_unchecked(bytes)) } + } + + /// Returns the length of `self`. + /// + /// This length is in bytes. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_static("hello"); + /// assert_eq!(val.len(), 5); + /// ``` + #[inline] + pub fn len(&self) -> usize { + self.as_ref().len() + } + + /// Returns true if the `HeaderValue` has a length of zero bytes. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_static(""); + /// assert!(val.is_empty()); + /// + /// let val = HeaderValue::from_static("hello"); + /// assert!(!val.is_empty()); + /// ``` + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Converts a `HeaderValue` to a byte slice. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let val = HeaderValue::from_static("hello"); + /// assert_eq!(val.as_bytes(), b"hello"); + /// ``` + #[inline] + pub fn as_bytes(&self) -> &[u8] { + self.as_ref() + } + + /// Mark that the header value represents sensitive information. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let mut val = HeaderValue::from_static("my secret"); + /// + /// val.set_sensitive(true); + /// assert!(val.is_sensitive()); + /// + /// val.set_sensitive(false); + /// assert!(!val.is_sensitive()); + /// ``` + #[inline] + pub fn set_sensitive(&mut self, val: bool) { + self.is_sensitive = val; + } + + /// Returns `true` if the value represents sensitive data. + /// + /// Sensitive data could represent passwords or other data that should not + /// be stored on disk or in memory. By marking header values as sensitive, + /// components using this crate can be instructed to treat them with special + /// care for security reasons. For example, caches can avoid storing + /// sensitive values, and HPACK encoders used by HTTP/2.0 implementations + /// can choose not to compress them. + /// + /// Additionally, sensitive values will be masked by the `Debug` + /// implementation of `HeaderValue`. + /// + /// Note that sensitivity is not factored into equality or ordering. + /// + /// # Examples + /// + /// ``` + /// # use http::header::HeaderValue; + /// let mut val = HeaderValue::from_static("my secret"); + /// + /// val.set_sensitive(true); + /// assert!(val.is_sensitive()); + /// + /// val.set_sensitive(false); + /// assert!(!val.is_sensitive()); + /// ``` + #[inline] + pub fn is_sensitive(&self) -> bool { + self.is_sensitive + } +} + +impl AsRef<[u8]> for HeaderValue { + #[inline] + fn as_ref(&self) -> &[u8] { + self.inner.as_ref() + } +} + +impl fmt::Debug for HeaderValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_sensitive { + f.write_str("Sensitive") + } else { + f.write_str("\"")?; + let mut from = 0; + let bytes = self.as_bytes(); + for (i, &b) in bytes.iter().enumerate() { + if !is_visible_ascii(b) || b == b'"' { + if from != i { + f.write_str(unsafe { str::from_utf8_unchecked(&bytes[from..i]) })?; + } + if b == b'"' { + f.write_str("\\\"")?; + } else { + write!(f, "\\x{:x}", b)?; + } + from = i + 1; + } + } + + f.write_str(unsafe { str::from_utf8_unchecked(&bytes[from..]) })?; + f.write_str("\"") + } + } +} + +impl From for HeaderValue { + #[inline] + fn from(h: HeaderName) -> HeaderValue { + HeaderValue { + inner: h.into_bytes(), + is_sensitive: false, + } + } +} + +macro_rules! from_integers { + ($($name:ident: $t:ident => $max_len:expr),*) => {$( + impl From<$t> for HeaderValue { + fn from(num: $t) -> HeaderValue { + let mut buf = if mem::size_of::() - 1 < $max_len { + // On 32bit platforms, BytesMut max inline size + // is 15 bytes, but the $max_len could be bigger. + // + // The likelihood of the number *actually* being + // that big is very small, so only allocate + // if the number needs that space. + // + // The largest decimal number in 15 digits: + // It wold be 10.pow(15) - 1, but this is a constant + // version. + if num as u64 > 999_999_999_999_999_999 { + BytesMut::with_capacity($max_len) + } else { + // fits inline... + BytesMut::new() + } + } else { + // full value fits inline, so don't allocate! + BytesMut::new() + }; + let _ = buf.write_str(::itoa::Buffer::new().format(num)); + HeaderValue { + inner: buf.freeze(), + is_sensitive: false, + } + } + } + + #[test] + fn $name() { + let n: $t = 55; + let val = HeaderValue::from(n); + assert_eq!(val, &n.to_string()); + + let n = ::std::$t::MAX; + let val = HeaderValue::from(n); + assert_eq!(val, &n.to_string()); + } + )*}; +} + +from_integers! { + // integer type => maximum decimal length + + // u8 purposely left off... HeaderValue::from(b'3') could be confusing + from_u16: u16 => 5, + from_i16: i16 => 6, + from_u32: u32 => 10, + from_i32: i32 => 11, + from_u64: u64 => 20, + from_i64: i64 => 20 +} + +#[cfg(target_pointer_width = "16")] +from_integers! { + from_usize: usize => 5, + from_isize: isize => 6 +} + +#[cfg(target_pointer_width = "32")] +from_integers! { + from_usize: usize => 10, + from_isize: isize => 11 +} + +#[cfg(target_pointer_width = "64")] +from_integers! { + from_usize: usize => 20, + from_isize: isize => 20 +} + +#[cfg(test)] +mod from_header_name_tests { + use super::*; + use crate::header::map::HeaderMap; + use crate::header::name; + + #[test] + fn it_can_insert_header_name_as_header_value() { + let mut map = HeaderMap::new(); + map.insert(name::UPGRADE, name::SEC_WEBSOCKET_PROTOCOL.into()); + map.insert( + name::ACCEPT, + name::HeaderName::from_bytes(b"hello-world").unwrap().into(), + ); + + assert_eq!( + map.get(name::UPGRADE).unwrap(), + HeaderValue::from_bytes(b"sec-websocket-protocol").unwrap() + ); + + assert_eq!( + map.get(name::ACCEPT).unwrap(), + HeaderValue::from_bytes(b"hello-world").unwrap() + ); + } +} + +impl FromStr for HeaderValue { + type Err = InvalidHeaderValue; + + #[inline] + fn from_str(s: &str) -> Result { + HeaderValue::from_str(s) + } +} + +impl<'a> From<&'a HeaderValue> for HeaderValue { + #[inline] + fn from(t: &'a HeaderValue) -> Self { + t.clone() + } +} + +impl<'a> TryFrom<&'a str> for HeaderValue { + type Error = InvalidHeaderValue; + + #[inline] + fn try_from(t: &'a str) -> Result { + t.parse() + } +} + +impl<'a> TryFrom<&'a String> for HeaderValue { + type Error = InvalidHeaderValue; + #[inline] + fn try_from(s: &'a String) -> Result { + Self::from_bytes(s.as_bytes()) + } +} + +impl<'a> TryFrom<&'a [u8]> for HeaderValue { + type Error = InvalidHeaderValue; + + #[inline] + fn try_from(t: &'a [u8]) -> Result { + HeaderValue::from_bytes(t) + } +} + +impl TryFrom for HeaderValue { + type Error = InvalidHeaderValue; + + #[inline] + fn try_from(t: String) -> Result { + HeaderValue::from_shared(t.into()) + } +} + +impl TryFrom> for HeaderValue { + type Error = InvalidHeaderValue; + + #[inline] + fn try_from(vec: Vec) -> Result { + HeaderValue::from_shared(vec.into()) + } +} + +#[cfg(test)] +mod try_from_header_name_tests { + use super::*; + use crate::header::name; + + #[test] + fn it_converts_using_try_from() { + assert_eq!( + HeaderValue::try_from(name::UPGRADE).unwrap(), + HeaderValue::from_bytes(b"upgrade").unwrap() + ); + } +} + +const fn is_visible_ascii(b: u8) -> bool { + b >= 32 && b < 127 || b == b'\t' +} + +#[inline] +fn is_valid(b: u8) -> bool { + b >= 32 && b != 127 || b == b'\t' +} + +impl fmt::Debug for InvalidHeaderValue { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("InvalidHeaderValue") + // skip _priv noise + .finish() + } +} + +impl fmt::Display for InvalidHeaderValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("failed to parse header value") + } +} + +impl Error for InvalidHeaderValue {} + +impl fmt::Display for ToStrError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("failed to convert header to a str") + } +} + +impl Error for ToStrError {} + +// ===== PartialEq / PartialOrd ===== + +impl PartialEq for HeaderValue { + #[inline] + fn eq(&self, other: &HeaderValue) -> bool { + self.inner == other.inner + } +} + +impl Eq for HeaderValue {} + +impl PartialOrd for HeaderValue { + #[inline] + fn partial_cmp(&self, other: &HeaderValue) -> Option { + self.inner.partial_cmp(&other.inner) + } +} + +impl Ord for HeaderValue { + #[inline] + fn cmp(&self, other: &Self) -> cmp::Ordering { + self.inner.cmp(&other.inner) + } +} + +impl PartialEq for HeaderValue { + #[inline] + fn eq(&self, other: &str) -> bool { + self.inner == other.as_bytes() + } +} + +impl PartialEq<[u8]> for HeaderValue { + #[inline] + fn eq(&self, other: &[u8]) -> bool { + self.inner == other + } +} + +impl PartialOrd for HeaderValue { + #[inline] + fn partial_cmp(&self, other: &str) -> Option { + (*self.inner).partial_cmp(other.as_bytes()) + } +} + +impl PartialOrd<[u8]> for HeaderValue { + #[inline] + fn partial_cmp(&self, other: &[u8]) -> Option { + (*self.inner).partial_cmp(other) + } +} + +impl PartialEq for str { + #[inline] + fn eq(&self, other: &HeaderValue) -> bool { + *other == *self + } +} + +impl PartialEq for [u8] { + #[inline] + fn eq(&self, other: &HeaderValue) -> bool { + *other == *self + } +} + +impl PartialOrd for str { + #[inline] + fn partial_cmp(&self, other: &HeaderValue) -> Option { + self.as_bytes().partial_cmp(other.as_bytes()) + } +} + +impl PartialOrd for [u8] { + #[inline] + fn partial_cmp(&self, other: &HeaderValue) -> Option { + self.partial_cmp(other.as_bytes()) + } +} + +impl PartialEq for HeaderValue { + #[inline] + fn eq(&self, other: &String) -> bool { + *self == &other[..] + } +} + +impl PartialOrd for HeaderValue { + #[inline] + fn partial_cmp(&self, other: &String) -> Option { + self.inner.partial_cmp(other.as_bytes()) + } +} + +impl PartialEq for String { + #[inline] + fn eq(&self, other: &HeaderValue) -> bool { + *other == *self + } +} + +impl PartialOrd for String { + #[inline] + fn partial_cmp(&self, other: &HeaderValue) -> Option { + self.as_bytes().partial_cmp(other.as_bytes()) + } +} + +impl<'a> PartialEq for &'a HeaderValue { + #[inline] + fn eq(&self, other: &HeaderValue) -> bool { + **self == *other + } +} + +impl<'a> PartialOrd for &'a HeaderValue { + #[inline] + fn partial_cmp(&self, other: &HeaderValue) -> Option { + (**self).partial_cmp(other) + } +} + +impl<'a, T: ?Sized> PartialEq<&'a T> for HeaderValue +where + HeaderValue: PartialEq, +{ + #[inline] + fn eq(&self, other: &&'a T) -> bool { + *self == **other + } +} + +impl<'a, T: ?Sized> PartialOrd<&'a T> for HeaderValue +where + HeaderValue: PartialOrd, +{ + #[inline] + fn partial_cmp(&self, other: &&'a T) -> Option { + self.partial_cmp(*other) + } +} + +impl<'a> PartialEq for &'a str { + #[inline] + fn eq(&self, other: &HeaderValue) -> bool { + *other == *self + } +} + +impl<'a> PartialOrd for &'a str { + #[inline] + fn partial_cmp(&self, other: &HeaderValue) -> Option { + self.as_bytes().partial_cmp(other.as_bytes()) + } +} + +#[test] +fn test_try_from() { + HeaderValue::try_from(vec![127]).unwrap_err(); +} + +#[test] +fn test_debug() { + let cases = &[ + ("hello", "\"hello\""), + ("hello \"world\"", "\"hello \\\"world\\\"\""), + ("\u{7FFF}hello", "\"\\xe7\\xbf\\xbfhello\""), + ]; + + for &(value, expected) in cases { + let val = HeaderValue::from_bytes(value.as_bytes()).unwrap(); + let actual = format!("{:?}", val); + assert_eq!(expected, actual); + } + + let mut sensitive = HeaderValue::from_static("password"); + sensitive.set_sensitive(true); + assert_eq!("Sensitive", format!("{:?}", sensitive)); +} diff --git a/third_party/rust/http/v1/crate/src/lib.rs b/third_party/rust/http/v1/crate/src/lib.rs new file mode 100644 index 000000000000..54714ba7e912 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/lib.rs @@ -0,0 +1,214 @@ +//! A general purpose library of common HTTP types +//! +//! This crate is a general purpose library for common types found when working +//! with the HTTP protocol. You'll find `Request` and `Response` types for +//! working as either a client or a server as well as all of their components. +//! Notably you'll find `Uri` for what a `Request` is requesting, a `Method` +//! for how it's being requested, a `StatusCode` for what sort of response came +//! back, a `Version` for how this was communicated, and +//! `HeaderName`/`HeaderValue` definitions to get grouped in a `HeaderMap` to +//! work with request/response headers. +//! +//! You will notably *not* find an implementation of sending requests or +//! spinning up a server in this crate. It's intended that this crate is the +//! "standard library" for HTTP clients and servers without dictating any +//! particular implementation. Note that this crate is still early on in its +//! lifecycle so the support libraries that integrate with the `http` crate are +//! a work in progress! Stay tuned and we'll be sure to highlight crates here +//! in the future. +//! +//! ## Requests and Responses +//! +//! Perhaps the main two types in this crate are the `Request` and `Response` +//! types. A `Request` could either be constructed to get sent off as a client +//! or it can also be received to generate a `Response` for a server. Similarly +//! as a client a `Response` is what you get after sending a `Request`, whereas +//! on a server you'll be manufacturing a `Response` to send back to the client. +//! +//! Each type has a number of accessors for the component fields. For as a +//! server you might want to inspect a requests URI to dispatch it: +//! +//! ``` +//! use http::{Request, Response}; +//! +//! fn response(req: Request<()>) -> http::Result> { +//! match req.uri().path() { +//! "/" => index(req), +//! "/foo" => foo(req), +//! "/bar" => bar(req), +//! _ => not_found(req), +//! } +//! } +//! # fn index(_req: Request<()>) -> http::Result> { panic!() } +//! # fn foo(_req: Request<()>) -> http::Result> { panic!() } +//! # fn bar(_req: Request<()>) -> http::Result> { panic!() } +//! # fn not_found(_req: Request<()>) -> http::Result> { panic!() } +//! ``` +//! +//! On a `Request` you'll also find accessors like `method` to return a +//! `Method` and `headers` to inspect the various headers. A `Response` +//! has similar methods for headers, the status code, etc. +//! +//! In addition to getters, request/response types also have mutable accessors +//! to edit the request/response: +//! +//! ``` +//! use http::{HeaderValue, Response, StatusCode}; +//! use http::header::CONTENT_TYPE; +//! +//! fn add_server_headers(response: &mut Response) { +//! response.headers_mut() +//! .insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); +//! *response.status_mut() = StatusCode::OK; +//! } +//! ``` +//! +//! And finally, one of the most important aspects of requests/responses, the +//! body! The `Request` and `Response` types in this crate are *generic* in +//! what their body is. This allows downstream libraries to use different +//! representations such as `Request>`, `Response`, +//! `Request, Error = _>>`, or even +//! `Response` where the custom type was deserialized from JSON. +//! +//! The body representation is intentionally flexible to give downstream +//! libraries maximal flexibility in implementing the body as appropriate. +//! +//! ## HTTP Headers +//! +//! Another major piece of functionality in this library is HTTP header +//! interpretation and generation. The `HeaderName` type serves as a way to +//! define header *names*, or what's to the left of the colon. A `HeaderValue` +//! conversely is the header *value*, or what's to the right of a colon. +//! +//! For example, if you have an HTTP request that looks like: +//! +//! ```http +//! GET /foo HTTP/1.1 +//! Accept: text/html +//! ``` +//! +//! Then `"Accept"` is a `HeaderName` while `"text/html"` is a `HeaderValue`. +//! Each of these is a dedicated type to allow for a number of interesting +//! optimizations and to also encode the static guarantees of each type. For +//! example a `HeaderName` is always a valid `&str`, but a `HeaderValue` may +//! not be valid UTF-8. +//! +//! The most common header names are already defined for you as constant values +//! in the `header` module of this crate. For example: +//! +//! ``` +//! use http::header::{self, HeaderName}; +//! +//! let name: HeaderName = header::ACCEPT; +//! assert_eq!(name.as_str(), "accept"); +//! ``` +//! +//! You can, however, also parse header names from strings: +//! +//! ``` +//! use http::header::{self, HeaderName}; +//! +//! let name = "Accept".parse::().unwrap(); +//! assert_eq!(name, header::ACCEPT); +//! ``` +//! +//! Header values can be created from string literals through the `from_static` +//! function: +//! +//! ``` +//! use http::HeaderValue; +//! +//! let value = HeaderValue::from_static("text/html"); +//! assert_eq!(value.as_bytes(), b"text/html"); +//! ``` +//! +//! And header values can also be parsed like names: +//! +//! ``` +//! use http::HeaderValue; +//! +//! let value = "text/html"; +//! let value = value.parse::().unwrap(); +//! ``` +//! +//! Most HTTP requests and responses tend to come with more than one header, so +//! it's not too useful to just work with names and values only! This crate also +//! provides a `HeaderMap` type which is a specialized hash map for keys as +//! `HeaderName` and generic values. This type, like header names, is optimized +//! for common usage but should continue to scale with your needs over time. +//! +//! # URIs +//! +//! Each HTTP `Request` has an associated URI with it. This may just be a path +//! like `/index.html` but it could also be an absolute URL such as +//! `https://www.rust-lang.org/index.html`. A `URI` has a number of accessors to +//! interpret it: +//! +//! ``` +//! use http::Uri; +//! use http::uri::Scheme; +//! +//! let uri = "https://www.rust-lang.org/index.html".parse::().unwrap(); +//! +//! assert_eq!(uri.scheme(), Some(&Scheme::HTTPS)); +//! assert_eq!(uri.host(), Some("www.rust-lang.org")); +//! assert_eq!(uri.path(), "/index.html"); +//! assert_eq!(uri.query(), None); +//! ``` + +#![deny(warnings, missing_docs, missing_debug_implementations)] + +//#![cfg_attr(not(feature = "std"), no_std)] +#[cfg(not(feature = "std"))] +compile_error!("`std` feature currently required, support for `no_std` may be added later"); + + +#[cfg(test)] +#[macro_use] +extern crate doc_comment; + +#[cfg(test)] +doctest!("../README.md"); + +#[macro_use] +mod convert; + +pub mod header; +pub mod method; +pub mod request; +pub mod response; +pub mod status; +pub mod uri; +pub mod version; + +mod byte_str; +mod error; +mod extensions; + +pub use crate::error::{Error, Result}; +pub use crate::extensions::Extensions; +#[doc(no_inline)] +pub use crate::header::{HeaderMap, HeaderName, HeaderValue}; +pub use crate::method::Method; +pub use crate::request::Request; +pub use crate::response::Response; +pub use crate::status::StatusCode; +pub use crate::uri::Uri; +pub use crate::version::Version; + +fn _assert_types() { + fn assert_send() {} + fn assert_sync() {} + + assert_send::>(); + assert_send::>(); + + assert_sync::>(); + assert_sync::>(); +} + +mod sealed { + /// Private trait to this crate to prevent traits from being implemented in + /// downstream crates. + pub trait Sealed {} +} diff --git a/third_party/rust/http/v1/crate/src/method.rs b/third_party/rust/http/v1/crate/src/method.rs new file mode 100644 index 000000000000..04261a37ab00 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/method.rs @@ -0,0 +1,473 @@ +//! The HTTP request method +//! +//! This module contains HTTP-method related structs and errors and such. The +//! main type of this module, `Method`, is also reexported at the root of the +//! crate as `http::Method` and is intended for import through that location +//! primarily. +//! +//! # Examples +//! +//! ``` +//! use http::Method; +//! +//! assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); +//! assert!(Method::GET.is_idempotent()); +//! assert_eq!(Method::POST.as_str(), "POST"); +//! ``` + +use self::Inner::*; +use self::extension::{InlineExtension, AllocatedExtension}; + +use std::convert::AsRef; +use std::error::Error; +use std::str::FromStr; +use std::convert::TryFrom; +use std::{fmt, str}; + +/// The Request Method (VERB) +/// +/// This type also contains constants for a number of common HTTP methods such +/// as GET, POST, etc. +/// +/// Currently includes 8 variants representing the 8 methods defined in +/// [RFC 7230](https://tools.ietf.org/html/rfc7231#section-4.1), plus PATCH, +/// and an Extension variant for all extensions. +/// +/// # Examples +/// +/// ``` +/// use http::Method; +/// +/// assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); +/// assert!(Method::GET.is_idempotent()); +/// assert_eq!(Method::POST.as_str(), "POST"); +/// ``` +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct Method(Inner); + +/// A possible error value when converting `Method` from bytes. +pub struct InvalidMethod { + _priv: (), +} + +#[derive(Clone, PartialEq, Eq, Hash)] +enum Inner { + Options, + Get, + Post, + Put, + Delete, + Head, + Trace, + Connect, + Patch, + // If the extension is short enough, store it inline + ExtensionInline(InlineExtension), + // Otherwise, allocate it + ExtensionAllocated(AllocatedExtension), +} + + +impl Method { + /// GET + pub const GET: Method = Method(Get); + + /// POST + pub const POST: Method = Method(Post); + + /// PUT + pub const PUT: Method = Method(Put); + + /// DELETE + pub const DELETE: Method = Method(Delete); + + /// HEAD + pub const HEAD: Method = Method(Head); + + /// OPTIONS + pub const OPTIONS: Method = Method(Options); + + /// CONNECT + pub const CONNECT: Method = Method(Connect); + + /// PATCH + pub const PATCH: Method = Method(Patch); + + /// TRACE + pub const TRACE: Method = Method(Trace); + + /// Converts a slice of bytes to an HTTP method. + pub fn from_bytes(src: &[u8]) -> Result { + match src.len() { + 0 => Err(InvalidMethod::new()), + 3 => match src { + b"GET" => Ok(Method(Get)), + b"PUT" => Ok(Method(Put)), + _ => Method::extension_inline(src), + }, + 4 => match src { + b"POST" => Ok(Method(Post)), + b"HEAD" => Ok(Method(Head)), + _ => Method::extension_inline(src), + }, + 5 => match src { + b"PATCH" => Ok(Method(Patch)), + b"TRACE" => Ok(Method(Trace)), + _ => Method::extension_inline(src), + }, + 6 => match src { + b"DELETE" => Ok(Method(Delete)), + _ => Method::extension_inline(src), + }, + 7 => match src { + b"OPTIONS" => Ok(Method(Options)), + b"CONNECT" => Ok(Method(Connect)), + _ => Method::extension_inline(src), + }, + _ => { + if src.len() < InlineExtension::MAX { + Method::extension_inline(src) + } else { + let allocated = AllocatedExtension::new(src)?; + + Ok(Method(ExtensionAllocated(allocated))) + } + } + } + } + + fn extension_inline(src: &[u8]) -> Result { + let inline = InlineExtension::new(src)?; + + Ok(Method(ExtensionInline(inline))) + } + + /// Whether a method is considered "safe", meaning the request is + /// essentially read-only. + /// + /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.1) + /// for more words. + pub fn is_safe(&self) -> bool { + match self.0 { + Get | Head | Options | Trace => true, + _ => false, + } + } + + /// Whether a method is considered "idempotent", meaning the request has + /// the same result if executed multiple times. + /// + /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.2) for + /// more words. + pub fn is_idempotent(&self) -> bool { + match self.0 { + Put | Delete => true, + _ => self.is_safe(), + } + } + + /// Return a &str representation of the HTTP method + #[inline] + pub fn as_str(&self) -> &str { + match self.0 { + Options => "OPTIONS", + Get => "GET", + Post => "POST", + Put => "PUT", + Delete => "DELETE", + Head => "HEAD", + Trace => "TRACE", + Connect => "CONNECT", + Patch => "PATCH", + ExtensionInline(ref inline) => inline.as_str(), + ExtensionAllocated(ref allocated) => allocated.as_str(), + } + } +} + +impl AsRef for Method { + #[inline] + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl<'a> PartialEq<&'a Method> for Method { + #[inline] + fn eq(&self, other: &&'a Method) -> bool { + self == *other + } +} + +impl<'a> PartialEq for &'a Method { + #[inline] + fn eq(&self, other: &Method) -> bool { + *self == other + } +} + +impl PartialEq for Method { + #[inline] + fn eq(&self, other: &str) -> bool { + self.as_ref() == other + } +} + +impl PartialEq for str { + #[inline] + fn eq(&self, other: &Method) -> bool { + self == other.as_ref() + } +} + +impl<'a> PartialEq<&'a str> for Method { + #[inline] + fn eq(&self, other: &&'a str) -> bool { + self.as_ref() == *other + } +} + +impl<'a> PartialEq for &'a str { + #[inline] + fn eq(&self, other: &Method) -> bool { + *self == other.as_ref() + } +} + +impl fmt::Debug for Method { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_ref()) + } +} + +impl fmt::Display for Method { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(self.as_ref()) + } +} + +impl Default for Method { + #[inline] + fn default() -> Method { + Method::GET + } +} + +impl<'a> From<&'a Method> for Method { + #[inline] + fn from(t: &'a Method) -> Self { + t.clone() + } +} + +impl<'a> TryFrom<&'a [u8]> for Method { + type Error = InvalidMethod; + + #[inline] + fn try_from(t: &'a [u8]) -> Result { + Method::from_bytes(t) + } +} + +impl<'a> TryFrom<&'a str> for Method { + type Error = InvalidMethod; + + #[inline] + fn try_from(t: &'a str) -> Result { + TryFrom::try_from(t.as_bytes()) + } +} + +impl FromStr for Method { + type Err = InvalidMethod; + + #[inline] + fn from_str(t: &str) -> Result { + TryFrom::try_from(t) + } +} + +impl InvalidMethod { + fn new() -> InvalidMethod { + InvalidMethod { _priv: () } + } +} + +impl fmt::Debug for InvalidMethod { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("InvalidMethod") + // skip _priv noise + .finish() + } +} + +impl fmt::Display for InvalidMethod { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("invalid HTTP method") + } +} + +impl Error for InvalidMethod {} + +mod extension { + use super::InvalidMethod; + use std::str; + + #[derive(Clone, PartialEq, Eq, Hash)] + // Invariant: the first self.1 bytes of self.0 are valid UTF-8. + pub struct InlineExtension([u8; InlineExtension::MAX], u8); + + #[derive(Clone, PartialEq, Eq, Hash)] + // Invariant: self.0 contains valid UTF-8. + pub struct AllocatedExtension(Box<[u8]>); + + impl InlineExtension { + // Method::from_bytes() assumes this is at least 7 + pub const MAX: usize = 15; + + pub fn new(src: &[u8]) -> Result { + let mut data: [u8; InlineExtension::MAX] = Default::default(); + + write_checked(src, &mut data)?; + + // Invariant: write_checked ensures that the first src.len() bytes + // of data are valid UTF-8. + Ok(InlineExtension(data, src.len() as u8)) + } + + pub fn as_str(&self) -> &str { + let InlineExtension(ref data, len) = self; + // Safety: the invariant of InlineExtension ensures that the first + // len bytes of data contain valid UTF-8. + unsafe {str::from_utf8_unchecked(&data[..*len as usize])} + } + } + + impl AllocatedExtension { + pub fn new(src: &[u8]) -> Result { + let mut data: Vec = vec![0; src.len()]; + + write_checked(src, &mut data)?; + + // Invariant: data is exactly src.len() long and write_checked + // ensures that the first src.len() bytes of data are valid UTF-8. + Ok(AllocatedExtension(data.into_boxed_slice())) + } + + pub fn as_str(&self) -> &str { + // Safety: the invariant of AllocatedExtension ensures that self.0 + // contains valid UTF-8. + unsafe {str::from_utf8_unchecked(&self.0)} + } + } + + // From the HTTP spec section 5.1.1, the HTTP method is case-sensitive and can + // contain the following characters: + // + // ``` + // method = token + // token = 1*tchar + // tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / + // "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA + // ``` + // + // https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01#Method + // + // Note that this definition means that any &[u8] that consists solely of valid + // characters is also valid UTF-8 because the valid method characters are a + // subset of the valid 1 byte UTF-8 encoding. + const METHOD_CHARS: [u8; 256] = [ + // 0 1 2 3 4 5 6 7 8 9 + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 1x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 2x + b'\0', b'\0', b'\0', b'!', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 3x + b'\0', b'\0', b'*', b'+', b'\0', b'-', b'.', b'\0', b'0', b'1', // 4x + b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'\0', b'\0', // 5x + b'\0', b'\0', b'\0', b'\0', b'\0', b'A', b'B', b'C', b'D', b'E', // 6x + b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x + b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x + b'Z', b'\0', b'\0', b'\0', b'^', b'_', b'`', b'a', b'b', b'c', // 9x + b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x + b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x + b'x', b'y', b'z', b'\0', b'|', b'\0', b'~', b'\0', b'\0', b'\0', // 12x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 13x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 14x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 15x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 16x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 17x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 18x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 19x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 20x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 21x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 22x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 23x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 24x + b'\0', b'\0', b'\0', b'\0', b'\0', b'\0' // 25x + ]; + + // write_checked ensures (among other things) that the first src.len() bytes + // of dst are valid UTF-8 + fn write_checked(src: &[u8], dst: &mut [u8]) -> Result<(), InvalidMethod> { + for (i, &b) in src.iter().enumerate() { + let b = METHOD_CHARS[b as usize]; + + if b == 0 { + return Err(InvalidMethod::new()); + } + + dst[i] = b; + } + + Ok(()) + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_method_eq() { + assert_eq!(Method::GET, Method::GET); + assert_eq!(Method::GET, "GET"); + assert_eq!(&Method::GET, "GET"); + + assert_eq!("GET", Method::GET); + assert_eq!("GET", &Method::GET); + + assert_eq!(&Method::GET, Method::GET); + assert_eq!(Method::GET, &Method::GET); + } + + #[test] + fn test_invalid_method() { + assert!(Method::from_str("").is_err()); + assert!(Method::from_bytes(b"").is_err()); + assert!(Method::from_bytes(&[0xC0]).is_err()); // invalid utf-8 + assert!(Method::from_bytes(&[0x10]).is_err()); // invalid method characters + } + + #[test] + fn test_is_idempotent() { + assert!(Method::OPTIONS.is_idempotent()); + assert!(Method::GET.is_idempotent()); + assert!(Method::PUT.is_idempotent()); + assert!(Method::DELETE.is_idempotent()); + assert!(Method::HEAD.is_idempotent()); + assert!(Method::TRACE.is_idempotent()); + + assert!(!Method::POST.is_idempotent()); + assert!(!Method::CONNECT.is_idempotent()); + assert!(!Method::PATCH.is_idempotent()); + } + + #[test] + fn test_extension_method() { + assert_eq!(Method::from_str("WOW").unwrap(), "WOW"); + assert_eq!(Method::from_str("wOw!!").unwrap(), "wOw!!"); + + let long_method = "This_is_a_very_long_method.It_is_valid_but_unlikely."; + assert_eq!(Method::from_str(&long_method).unwrap(), long_method); + } +} diff --git a/third_party/rust/http/v1/crate/src/request.rs b/third_party/rust/http/v1/crate/src/request.rs new file mode 100644 index 000000000000..ed0f673cca4d --- /dev/null +++ b/third_party/rust/http/v1/crate/src/request.rs @@ -0,0 +1,1089 @@ +//! HTTP request types. +//! +//! This module contains structs related to HTTP requests, notably the +//! `Request` type itself as well as a builder to create requests. Typically +//! you'll import the `http::Request` type rather than reaching into this +//! module itself. +//! +//! # Examples +//! +//! Creating a `Request` to send +//! +//! ```no_run +//! use http::{Request, Response}; +//! +//! let mut request = Request::builder() +//! .uri("https://www.rust-lang.org/") +//! .header("User-Agent", "my-awesome-agent/1.0"); +//! +//! if needs_awesome_header() { +//! request = request.header("Awesome", "yes"); +//! } +//! +//! let response = send(request.body(()).unwrap()); +//! +//! # fn needs_awesome_header() -> bool { +//! # true +//! # } +//! # +//! fn send(req: Request<()>) -> Response<()> { +//! // ... +//! # panic!() +//! } +//! ``` +//! +//! Inspecting a request to see what was sent. +//! +//! ``` +//! use http::{Request, Response, StatusCode}; +//! +//! fn respond_to(req: Request<()>) -> http::Result> { +//! if req.uri() != "/awesome-url" { +//! return Response::builder() +//! .status(StatusCode::NOT_FOUND) +//! .body(()) +//! } +//! +//! let has_awesome_header = req.headers().contains_key("Awesome"); +//! let body = req.body(); +//! +//! // ... +//! # panic!() +//! } +//! ``` + +use std::any::Any; +use std::convert::{TryFrom}; +use std::fmt; + +use crate::header::{HeaderMap, HeaderName, HeaderValue}; +use crate::method::Method; +use crate::version::Version; +use crate::{Extensions, Result, Uri}; + +/// Represents an HTTP request. +/// +/// An HTTP request consists of a head and a potentially optional body. The body +/// component is generic, enabling arbitrary types to represent the HTTP body. +/// For example, the body could be `Vec`, a `Stream` of byte chunks, or a +/// value that has been deserialized. +/// +/// # Examples +/// +/// Creating a `Request` to send +/// +/// ```no_run +/// use http::{Request, Response}; +/// +/// let mut request = Request::builder() +/// .uri("https://www.rust-lang.org/") +/// .header("User-Agent", "my-awesome-agent/1.0"); +/// +/// if needs_awesome_header() { +/// request = request.header("Awesome", "yes"); +/// } +/// +/// let response = send(request.body(()).unwrap()); +/// +/// # fn needs_awesome_header() -> bool { +/// # true +/// # } +/// # +/// fn send(req: Request<()>) -> Response<()> { +/// // ... +/// # panic!() +/// } +/// ``` +/// +/// Inspecting a request to see what was sent. +/// +/// ``` +/// use http::{Request, Response, StatusCode}; +/// +/// fn respond_to(req: Request<()>) -> http::Result> { +/// if req.uri() != "/awesome-url" { +/// return Response::builder() +/// .status(StatusCode::NOT_FOUND) +/// .body(()) +/// } +/// +/// let has_awesome_header = req.headers().contains_key("Awesome"); +/// let body = req.body(); +/// +/// // ... +/// # panic!() +/// } +/// ``` +/// +/// Deserialize a request of bytes via json: +/// +/// ``` +/// # extern crate serde; +/// # extern crate serde_json; +/// # extern crate http; +/// use http::Request; +/// use serde::de; +/// +/// fn deserialize(req: Request>) -> serde_json::Result> +/// where for<'de> T: de::Deserialize<'de>, +/// { +/// let (parts, body) = req.into_parts(); +/// let body = serde_json::from_slice(&body)?; +/// Ok(Request::from_parts(parts, body)) +/// } +/// # +/// # fn main() {} +/// ``` +/// +/// Or alternatively, serialize the body of a request to json +/// +/// ``` +/// # extern crate serde; +/// # extern crate serde_json; +/// # extern crate http; +/// use http::Request; +/// use serde::ser; +/// +/// fn serialize(req: Request) -> serde_json::Result>> +/// where T: ser::Serialize, +/// { +/// let (parts, body) = req.into_parts(); +/// let body = serde_json::to_vec(&body)?; +/// Ok(Request::from_parts(parts, body)) +/// } +/// # +/// # fn main() {} +/// ``` +#[derive(Clone)] +pub struct Request { + head: Parts, + body: T, +} + +/// Component parts of an HTTP `Request` +/// +/// The HTTP request head consists of a method, uri, version, and a set of +/// header fields. +#[derive(Clone)] +pub struct Parts { + /// The request's method + pub method: Method, + + /// The request's URI + pub uri: Uri, + + /// The request's version + pub version: Version, + + /// The request's headers + pub headers: HeaderMap, + + /// The request's extensions + pub extensions: Extensions, + + _priv: (), +} + +/// An HTTP request builder +/// +/// This type can be used to construct an instance or `Request` +/// through a builder-like pattern. +#[derive(Debug)] +pub struct Builder { + inner: Result, +} + +impl Request<()> { + /// Creates a new builder-style object to manufacture a `Request` + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request = Request::builder() + /// .method("GET") + /// .uri("https://www.rust-lang.org/") + /// .header("X-Custom-Foo", "Bar") + /// .body(()) + /// .unwrap(); + /// ``` + #[inline] + pub fn builder() -> Builder { + Builder::new() + } + + /// Creates a new `Builder` initialized with a GET method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::get("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn get(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::GET).uri(uri) + } + + /// Creates a new `Builder` initialized with a PUT method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::put("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn put(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::PUT).uri(uri) + } + + /// Creates a new `Builder` initialized with a POST method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::post("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn post(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::POST).uri(uri) + } + + /// Creates a new `Builder` initialized with a DELETE method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::delete("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn delete(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::DELETE).uri(uri) + } + + /// Creates a new `Builder` initialized with an OPTIONS method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::options("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// # assert_eq!(*request.method(), Method::OPTIONS); + /// ``` + pub fn options(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::OPTIONS).uri(uri) + } + + /// Creates a new `Builder` initialized with a HEAD method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::head("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn head(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::HEAD).uri(uri) + } + + /// Creates a new `Builder` initialized with a CONNECT method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::connect("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn connect(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + + { + Builder::new().method(Method::CONNECT).uri(uri) + } + + /// Creates a new `Builder` initialized with a PATCH method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::patch("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn patch(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + { + Builder::new().method(Method::PATCH).uri(uri) + } + + /// Creates a new `Builder` initialized with a TRACE method and the given URI. + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Request`. + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::trace("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn trace(uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + { + Builder::new().method(Method::TRACE).uri(uri) + } +} + +impl Request { + /// Creates a new blank `Request` with the body + /// + /// The component parts of this request will be set to their default, e.g. + /// the GET method, no headers, etc. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request = Request::new("hello world"); + /// + /// assert_eq!(*request.method(), Method::GET); + /// assert_eq!(*request.body(), "hello world"); + /// ``` + #[inline] + pub fn new(body: T) -> Request { + Request { + head: Parts::new(), + body: body, + } + } + + /// Creates a new `Request` with the given components parts and body. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request = Request::new("hello world"); + /// let (mut parts, body) = request.into_parts(); + /// parts.method = Method::POST; + /// + /// let request = Request::from_parts(parts, body); + /// ``` + #[inline] + pub fn from_parts(parts: Parts, body: T) -> Request { + Request { + head: parts, + body: body, + } + } + + /// Returns a reference to the associated HTTP method. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request: Request<()> = Request::default(); + /// assert_eq!(*request.method(), Method::GET); + /// ``` + #[inline] + pub fn method(&self) -> &Method { + &self.head.method + } + + /// Returns a mutable reference to the associated HTTP method. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut request: Request<()> = Request::default(); + /// *request.method_mut() = Method::PUT; + /// assert_eq!(*request.method(), Method::PUT); + /// ``` + #[inline] + pub fn method_mut(&mut self) -> &mut Method { + &mut self.head.method + } + + /// Returns a reference to the associated URI. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request: Request<()> = Request::default(); + /// assert_eq!(*request.uri(), *"/"); + /// ``` + #[inline] + pub fn uri(&self) -> &Uri { + &self.head.uri + } + + /// Returns a mutable reference to the associated URI. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut request: Request<()> = Request::default(); + /// *request.uri_mut() = "/hello".parse().unwrap(); + /// assert_eq!(*request.uri(), *"/hello"); + /// ``` + #[inline] + pub fn uri_mut(&mut self) -> &mut Uri { + &mut self.head.uri + } + + /// Returns the associated version. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request: Request<()> = Request::default(); + /// assert_eq!(request.version(), Version::HTTP_11); + /// ``` + #[inline] + pub fn version(&self) -> Version { + self.head.version + } + + /// Returns a mutable reference to the associated version. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut request: Request<()> = Request::default(); + /// *request.version_mut() = Version::HTTP_2; + /// assert_eq!(request.version(), Version::HTTP_2); + /// ``` + #[inline] + pub fn version_mut(&mut self) -> &mut Version { + &mut self.head.version + } + + /// Returns a reference to the associated header field map. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request: Request<()> = Request::default(); + /// assert!(request.headers().is_empty()); + /// ``` + #[inline] + pub fn headers(&self) -> &HeaderMap { + &self.head.headers + } + + /// Returns a mutable reference to the associated header field map. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// # use http::header::*; + /// let mut request: Request<()> = Request::default(); + /// request.headers_mut().insert(HOST, HeaderValue::from_static("world")); + /// assert!(!request.headers().is_empty()); + /// ``` + #[inline] + pub fn headers_mut(&mut self) -> &mut HeaderMap { + &mut self.head.headers + } + + /// Returns a reference to the associated extensions. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request: Request<()> = Request::default(); + /// assert!(request.extensions().get::().is_none()); + /// ``` + #[inline] + pub fn extensions(&self) -> &Extensions { + &self.head.extensions + } + + /// Returns a mutable reference to the associated extensions. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// # use http::header::*; + /// let mut request: Request<()> = Request::default(); + /// request.extensions_mut().insert("hello"); + /// assert_eq!(request.extensions().get(), Some(&"hello")); + /// ``` + #[inline] + pub fn extensions_mut(&mut self) -> &mut Extensions { + &mut self.head.extensions + } + + /// Returns a reference to the associated HTTP body. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request: Request = Request::default(); + /// assert!(request.body().is_empty()); + /// ``` + #[inline] + pub fn body(&self) -> &T { + &self.body + } + + /// Returns a mutable reference to the associated HTTP body. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut request: Request = Request::default(); + /// request.body_mut().push_str("hello world"); + /// assert!(!request.body().is_empty()); + /// ``` + #[inline] + pub fn body_mut(&mut self) -> &mut T { + &mut self.body + } + + /// Consumes the request, returning just the body. + /// + /// # Examples + /// + /// ``` + /// # use http::Request; + /// let request = Request::new(10); + /// let body = request.into_body(); + /// assert_eq!(body, 10); + /// ``` + #[inline] + pub fn into_body(self) -> T { + self.body + } + + /// Consumes the request returning the head and body parts. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request = Request::new(()); + /// let (parts, body) = request.into_parts(); + /// assert_eq!(parts.method, Method::GET); + /// ``` + #[inline] + pub fn into_parts(self) -> (Parts, T) { + (self.head, self.body) + } + + /// Consumes the request returning a new request with body mapped to the + /// return type of the passed in function. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request = Request::builder().body("some string").unwrap(); + /// let mapped_request: Request<&[u8]> = request.map(|b| { + /// assert_eq!(b, "some string"); + /// b.as_bytes() + /// }); + /// assert_eq!(mapped_request.body(), &"some string".as_bytes()); + /// ``` + #[inline] + pub fn map(self, f: F) -> Request + where + F: FnOnce(T) -> U, + { + Request { + body: f(self.body), + head: self.head, + } + } +} + +impl Default for Request { + fn default() -> Request { + Request::new(T::default()) + } +} + +impl fmt::Debug for Request { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Request") + .field("method", self.method()) + .field("uri", self.uri()) + .field("version", &self.version()) + .field("headers", self.headers()) + // omits Extensions because not useful + .field("body", self.body()) + .finish() + } +} + +impl Parts { + /// Creates a new default instance of `Parts` + fn new() -> Parts { + Parts { + method: Method::default(), + uri: Uri::default(), + version: Version::default(), + headers: HeaderMap::default(), + extensions: Extensions::default(), + _priv: (), + } + } +} + +impl fmt::Debug for Parts { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Parts") + .field("method", &self.method) + .field("uri", &self.uri) + .field("version", &self.version) + .field("headers", &self.headers) + // omits Extensions because not useful + // omits _priv because not useful + .finish() + } +} + +impl Builder { + /// Creates a new default instance of `Builder` to construct a `Request`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let req = request::Builder::new() + /// .method("POST") + /// .body(()) + /// .unwrap(); + /// ``` + #[inline] + pub fn new() -> Builder { + Builder::default() + } + + /// Set the HTTP method for this request. + /// + /// By default this is `GET`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let req = Request::builder() + /// .method("POST") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn method(self, method: T) -> Builder + where + Method: TryFrom, + >::Error: Into, + { + self.and_then(move |mut head| { + let method = TryFrom::try_from(method).map_err(Into::into)?; + head.method = method; + Ok(head) + }) + } + + /// Get the HTTP Method for this request. + /// + /// By default this is `GET`. If builder has error, returns None. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let mut req = Request::builder(); + /// assert_eq!(req.method_ref(),Some(&Method::GET)); + /// + /// req = req.method("POST"); + /// assert_eq!(req.method_ref(),Some(&Method::POST)); + /// ``` + pub fn method_ref(&self) -> Option<&Method> { + self.inner.as_ref().ok().map(|h| &h.method) + } + + /// Set the URI for this request. + /// + /// By default this is `/`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let req = Request::builder() + /// .uri("https://www.rust-lang.org/") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn uri(self, uri: T) -> Builder + where + Uri: TryFrom, + >::Error: Into, + { + self.and_then(move |mut head| { + head.uri = TryFrom::try_from(uri).map_err(Into::into)?; + Ok(head) + }) + } + + /// Get the URI for this request + /// + /// By default this is `/`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let mut req = Request::builder(); + /// assert_eq!(req.uri_ref().unwrap(), "/" ); + /// + /// req = req.uri("https://www.rust-lang.org/"); + /// assert_eq!(req.uri_ref().unwrap(), "https://www.rust-lang.org/" ); + /// ``` + pub fn uri_ref(&self) -> Option<&Uri> { + self.inner.as_ref().ok().map(|h| &h.uri) + } + + /// Set the HTTP version for this request. + /// + /// By default this is HTTP/1.1 + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let req = Request::builder() + /// .version(Version::HTTP_2) + /// .body(()) + /// .unwrap(); + /// ``` + pub fn version(self, version: Version) -> Builder { + self.and_then(move |mut head| { + head.version = version; + Ok(head) + }) + } + + /// Get the HTTP version for this request + /// + /// By default this is HTTP/1.1. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let mut req = Request::builder(); + /// assert_eq!(req.version_ref().unwrap(), &Version::HTTP_11 ); + /// + /// req = req.version(Version::HTTP_2); + /// assert_eq!(req.version_ref().unwrap(), &Version::HTTP_2 ); + /// ``` + pub fn version_ref(&self) -> Option<&Version> { + self.inner.as_ref().ok().map(|h| &h.version) + } + + /// Appends a header to this request builder. + /// + /// This function will append the provided key/value as a header to the + /// internal `HeaderMap` being constructed. Essentially this is equivalent + /// to calling `HeaderMap::append`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// # use http::header::HeaderValue; + /// + /// let req = Request::builder() + /// .header("Accept", "text/html") + /// .header("X-Custom-Foo", "bar") + /// .body(()) + /// .unwrap(); + /// ``` + pub fn header(self, key: K, value: V) -> Builder + where + HeaderName: TryFrom, + >::Error: Into, + HeaderValue: TryFrom, + >::Error: Into, + { + self.and_then(move |mut head| { + let name = >::try_from(key).map_err(Into::into)?; + let value = >::try_from(value).map_err(Into::into)?; + head.headers.append(name, value); + Ok(head) + }) + } + + /// Get header on this request builder. + /// when builder has error returns None + /// + /// # Example + /// + /// ``` + /// # use http::Request; + /// let req = Request::builder() + /// .header("Accept", "text/html") + /// .header("X-Custom-Foo", "bar"); + /// let headers = req.headers_ref().unwrap(); + /// assert_eq!( headers["Accept"], "text/html" ); + /// assert_eq!( headers["X-Custom-Foo"], "bar" ); + /// ``` + pub fn headers_ref(&self) -> Option<&HeaderMap> { + self.inner.as_ref().ok().map(|h| &h.headers) + } + + /// Get headers on this request builder. + /// + /// When builder has error returns None. + /// + /// # Example + /// + /// ``` + /// # use http::{header::HeaderValue, Request}; + /// let mut req = Request::builder(); + /// { + /// let headers = req.headers_mut().unwrap(); + /// headers.insert("Accept", HeaderValue::from_static("text/html")); + /// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar")); + /// } + /// let headers = req.headers_ref().unwrap(); + /// assert_eq!( headers["Accept"], "text/html" ); + /// assert_eq!( headers["X-Custom-Foo"], "bar" ); + /// ``` + pub fn headers_mut(&mut self) -> Option<&mut HeaderMap> { + self.inner.as_mut().ok().map(|h| &mut h.headers) + } + + /// Adds an extension to this builder + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let req = Request::builder() + /// .extension("My Extension") + /// .body(()) + /// .unwrap(); + /// + /// assert_eq!(req.extensions().get::<&'static str>(), + /// Some(&"My Extension")); + /// ``` + pub fn extension(self, extension: T) -> Builder + where + T: Clone + Any + Send + Sync + 'static, + { + self.and_then(move |mut head| { + head.extensions.insert(extension); + Ok(head) + }) + } + + /// Get a reference to the extensions for this request builder. + /// + /// If the builder has an error, this returns `None`. + /// + /// # Example + /// + /// ``` + /// # use http::Request; + /// let req = Request::builder().extension("My Extension").extension(5u32); + /// let extensions = req.extensions_ref().unwrap(); + /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension")); + /// assert_eq!(extensions.get::(), Some(&5u32)); + /// ``` + pub fn extensions_ref(&self) -> Option<&Extensions> { + self.inner.as_ref().ok().map(|h| &h.extensions) + } + + /// Get a mutable reference to the extensions for this request builder. + /// + /// If the builder has an error, this returns `None`. + /// + /// # Example + /// + /// ``` + /// # use http::Request; + /// let mut req = Request::builder().extension("My Extension"); + /// let mut extensions = req.extensions_mut().unwrap(); + /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension")); + /// extensions.insert(5u32); + /// assert_eq!(extensions.get::(), Some(&5u32)); + /// ``` + pub fn extensions_mut(&mut self) -> Option<&mut Extensions> { + self.inner.as_mut().ok().map(|h| &mut h.extensions) + } + + /// "Consumes" this builder, using the provided `body` to return a + /// constructed `Request`. + /// + /// # Errors + /// + /// This function may return an error if any previously configured argument + /// failed to parse or get converted to the internal representation. For + /// example if an invalid `head` was specified via `header("Foo", + /// "Bar\r\n")` the error will be returned when this function is called + /// rather than when `header` was called. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let request = Request::builder() + /// .body(()) + /// .unwrap(); + /// ``` + pub fn body(self, body: T) -> Result> { + self.inner.map(move |head| { + Request { + head, + body, + } + }) + } + + // private + + fn and_then(self, func: F) -> Self + where + F: FnOnce(Parts) -> Result + { + Builder { + inner: self.inner.and_then(func), + } + } +} + +impl Default for Builder { + #[inline] + fn default() -> Builder { + Builder { + inner: Ok(Parts::new()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_can_map_a_body_from_one_type_to_another() { + let request = Request::builder().body("some string").unwrap(); + let mapped_request = request.map(|s| { + assert_eq!(s, "some string"); + 123u32 + }); + assert_eq!(mapped_request.body(), &123u32); + } +} diff --git a/third_party/rust/http/v1/crate/src/response.rs b/third_party/rust/http/v1/crate/src/response.rs new file mode 100644 index 000000000000..e0136a0f2c55 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/response.rs @@ -0,0 +1,795 @@ +//! HTTP response types. +//! +//! This module contains structs related to HTTP responses, notably the +//! `Response` type itself as well as a builder to create responses. Typically +//! you'll import the `http::Response` type rather than reaching into this +//! module itself. +//! +//! # Examples +//! +//! Creating a `Response` to return +//! +//! ``` +//! use http::{Request, Response, StatusCode}; +//! +//! fn respond_to(req: Request<()>) -> http::Result> { +//! let mut builder = Response::builder() +//! .header("Foo", "Bar") +//! .status(StatusCode::OK); +//! +//! if req.headers().contains_key("Another-Header") { +//! builder = builder.header("Another-Header", "Ack"); +//! } +//! +//! builder.body(()) +//! } +//! ``` +//! +//! A simple 404 handler +//! +//! ``` +//! use http::{Request, Response, StatusCode}; +//! +//! fn not_found(_req: Request<()>) -> http::Result> { +//! Response::builder() +//! .status(StatusCode::NOT_FOUND) +//! .body(()) +//! } +//! ``` +//! +//! Or otherwise inspecting the result of a request: +//! +//! ```no_run +//! use http::{Request, Response}; +//! +//! fn get(url: &str) -> http::Result> { +//! // ... +//! # panic!() +//! } +//! +//! let response = get("https://www.rust-lang.org/").unwrap(); +//! +//! if !response.status().is_success() { +//! panic!("failed to get a successful response status!"); +//! } +//! +//! if let Some(date) = response.headers().get("Date") { +//! // we've got a `Date` header! +//! } +//! +//! let body = response.body(); +//! // ... +//! ``` + +use std::any::Any; +use std::convert::TryFrom; +use std::fmt; + +use crate::header::{HeaderMap, HeaderName, HeaderValue}; +use crate::status::StatusCode; +use crate::version::Version; +use crate::{Extensions, Result}; + +/// Represents an HTTP response +/// +/// An HTTP response consists of a head and a potentially optional body. The body +/// component is generic, enabling arbitrary types to represent the HTTP body. +/// For example, the body could be `Vec`, a `Stream` of byte chunks, or a +/// value that has been deserialized. +/// +/// Typically you'll work with responses on the client side as the result of +/// sending a `Request` and on the server you'll be generating a `Response` to +/// send back to the client. +/// +/// # Examples +/// +/// Creating a `Response` to return +/// +/// ``` +/// use http::{Request, Response, StatusCode}; +/// +/// fn respond_to(req: Request<()>) -> http::Result> { +/// let mut builder = Response::builder() +/// .header("Foo", "Bar") +/// .status(StatusCode::OK); +/// +/// if req.headers().contains_key("Another-Header") { +/// builder = builder.header("Another-Header", "Ack"); +/// } +/// +/// builder.body(()) +/// } +/// ``` +/// +/// A simple 404 handler +/// +/// ``` +/// use http::{Request, Response, StatusCode}; +/// +/// fn not_found(_req: Request<()>) -> http::Result> { +/// Response::builder() +/// .status(StatusCode::NOT_FOUND) +/// .body(()) +/// } +/// ``` +/// +/// Or otherwise inspecting the result of a request: +/// +/// ```no_run +/// use http::{Request, Response}; +/// +/// fn get(url: &str) -> http::Result> { +/// // ... +/// # panic!() +/// } +/// +/// let response = get("https://www.rust-lang.org/").unwrap(); +/// +/// if !response.status().is_success() { +/// panic!("failed to get a successful response status!"); +/// } +/// +/// if let Some(date) = response.headers().get("Date") { +/// // we've got a `Date` header! +/// } +/// +/// let body = response.body(); +/// // ... +/// ``` +/// +/// Deserialize a response of bytes via json: +/// +/// ``` +/// # extern crate serde; +/// # extern crate serde_json; +/// # extern crate http; +/// use http::Response; +/// use serde::de; +/// +/// fn deserialize(res: Response>) -> serde_json::Result> +/// where for<'de> T: de::Deserialize<'de>, +/// { +/// let (parts, body) = res.into_parts(); +/// let body = serde_json::from_slice(&body)?; +/// Ok(Response::from_parts(parts, body)) +/// } +/// # +/// # fn main() {} +/// ``` +/// +/// Or alternatively, serialize the body of a response to json +/// +/// ``` +/// # extern crate serde; +/// # extern crate serde_json; +/// # extern crate http; +/// use http::Response; +/// use serde::ser; +/// +/// fn serialize(res: Response) -> serde_json::Result>> +/// where T: ser::Serialize, +/// { +/// let (parts, body) = res.into_parts(); +/// let body = serde_json::to_vec(&body)?; +/// Ok(Response::from_parts(parts, body)) +/// } +/// # +/// # fn main() {} +/// ``` +#[derive(Clone)] +pub struct Response { + head: Parts, + body: T, +} + +/// Component parts of an HTTP `Response` +/// +/// The HTTP response head consists of a status, version, and a set of +/// header fields. +#[derive(Clone)] +pub struct Parts { + /// The response's status + pub status: StatusCode, + + /// The response's version + pub version: Version, + + /// The response's headers + pub headers: HeaderMap, + + /// The response's extensions + pub extensions: Extensions, + + _priv: (), +} + +/// An HTTP response builder +/// +/// This type can be used to construct an instance of `Response` through a +/// builder-like pattern. +#[derive(Debug)] +pub struct Builder { + inner: Result, +} + +impl Response<()> { + /// Creates a new builder-style object to manufacture a `Response` + /// + /// This method returns an instance of `Builder` which can be used to + /// create a `Response`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response = Response::builder() + /// .status(200) + /// .header("X-Custom-Foo", "Bar") + /// .body(()) + /// .unwrap(); + /// ``` + #[inline] + pub fn builder() -> Builder { + Builder::new() + } +} + +impl Response { + /// Creates a new blank `Response` with the body + /// + /// The component ports of this response will be set to their default, e.g. + /// the ok status, no headers, etc. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response = Response::new("hello world"); + /// + /// assert_eq!(response.status(), StatusCode::OK); + /// assert_eq!(*response.body(), "hello world"); + /// ``` + #[inline] + pub fn new(body: T) -> Response { + Response { + head: Parts::new(), + body: body, + } + } + + /// Creates a new `Response` with the given head and body + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response = Response::new("hello world"); + /// let (mut parts, body) = response.into_parts(); + /// + /// parts.status = StatusCode::BAD_REQUEST; + /// let response = Response::from_parts(parts, body); + /// + /// assert_eq!(response.status(), StatusCode::BAD_REQUEST); + /// assert_eq!(*response.body(), "hello world"); + /// ``` + #[inline] + pub fn from_parts(parts: Parts, body: T) -> Response { + Response { + head: parts, + body: body, + } + } + + /// Returns the `StatusCode`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response: Response<()> = Response::default(); + /// assert_eq!(response.status(), StatusCode::OK); + /// ``` + #[inline] + pub fn status(&self) -> StatusCode { + self.head.status + } + + /// Returns a mutable reference to the associated `StatusCode`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut response: Response<()> = Response::default(); + /// *response.status_mut() = StatusCode::CREATED; + /// assert_eq!(response.status(), StatusCode::CREATED); + /// ``` + #[inline] + pub fn status_mut(&mut self) -> &mut StatusCode { + &mut self.head.status + } + + /// Returns a reference to the associated version. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response: Response<()> = Response::default(); + /// assert_eq!(response.version(), Version::HTTP_11); + /// ``` + #[inline] + pub fn version(&self) -> Version { + self.head.version + } + + /// Returns a mutable reference to the associated version. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut response: Response<()> = Response::default(); + /// *response.version_mut() = Version::HTTP_2; + /// assert_eq!(response.version(), Version::HTTP_2); + /// ``` + #[inline] + pub fn version_mut(&mut self) -> &mut Version { + &mut self.head.version + } + + /// Returns a reference to the associated header field map. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response: Response<()> = Response::default(); + /// assert!(response.headers().is_empty()); + /// ``` + #[inline] + pub fn headers(&self) -> &HeaderMap { + &self.head.headers + } + + /// Returns a mutable reference to the associated header field map. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// # use http::header::*; + /// let mut response: Response<()> = Response::default(); + /// response.headers_mut().insert(HOST, HeaderValue::from_static("world")); + /// assert!(!response.headers().is_empty()); + /// ``` + #[inline] + pub fn headers_mut(&mut self) -> &mut HeaderMap { + &mut self.head.headers + } + + /// Returns a reference to the associated extensions. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response: Response<()> = Response::default(); + /// assert!(response.extensions().get::().is_none()); + /// ``` + #[inline] + pub fn extensions(&self) -> &Extensions { + &self.head.extensions + } + + /// Returns a mutable reference to the associated extensions. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// # use http::header::*; + /// let mut response: Response<()> = Response::default(); + /// response.extensions_mut().insert("hello"); + /// assert_eq!(response.extensions().get(), Some(&"hello")); + /// ``` + #[inline] + pub fn extensions_mut(&mut self) -> &mut Extensions { + &mut self.head.extensions + } + + /// Returns a reference to the associated HTTP body. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response: Response = Response::default(); + /// assert!(response.body().is_empty()); + /// ``` + #[inline] + pub fn body(&self) -> &T { + &self.body + } + + /// Returns a mutable reference to the associated HTTP body. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let mut response: Response = Response::default(); + /// response.body_mut().push_str("hello world"); + /// assert!(!response.body().is_empty()); + /// ``` + #[inline] + pub fn body_mut(&mut self) -> &mut T { + &mut self.body + } + + /// Consumes the response, returning just the body. + /// + /// # Examples + /// + /// ``` + /// # use http::Response; + /// let response = Response::new(10); + /// let body = response.into_body(); + /// assert_eq!(body, 10); + /// ``` + #[inline] + pub fn into_body(self) -> T { + self.body + } + + /// Consumes the response returning the head and body parts. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response: Response<()> = Response::default(); + /// let (parts, body) = response.into_parts(); + /// assert_eq!(parts.status, StatusCode::OK); + /// ``` + #[inline] + pub fn into_parts(self) -> (Parts, T) { + (self.head, self.body) + } + + /// Consumes the response returning a new response with body mapped to the + /// return type of the passed in function. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response = Response::builder().body("some string").unwrap(); + /// let mapped_response: Response<&[u8]> = response.map(|b| { + /// assert_eq!(b, "some string"); + /// b.as_bytes() + /// }); + /// assert_eq!(mapped_response.body(), &"some string".as_bytes()); + /// ``` + #[inline] + pub fn map(self, f: F) -> Response + where + F: FnOnce(T) -> U, + { + Response { + body: f(self.body), + head: self.head, + } + } +} + +impl Default for Response { + #[inline] + fn default() -> Response { + Response::new(T::default()) + } +} + +impl fmt::Debug for Response { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Response") + .field("status", &self.status()) + .field("version", &self.version()) + .field("headers", self.headers()) + // omits Extensions because not useful + .field("body", self.body()) + .finish() + } +} + +impl Parts { + /// Creates a new default instance of `Parts` + fn new() -> Parts { + Parts { + status: StatusCode::default(), + version: Version::default(), + headers: HeaderMap::default(), + extensions: Extensions::default(), + _priv: (), + } + } +} + +impl fmt::Debug for Parts { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Parts") + .field("status", &self.status) + .field("version", &self.version) + .field("headers", &self.headers) + // omits Extensions because not useful + // omits _priv because not useful + .finish() + } +} + +impl Builder { + /// Creates a new default instance of `Builder` to construct either a + /// `Head` or a `Response`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let response = response::Builder::new() + /// .status(200) + /// .body(()) + /// .unwrap(); + /// ``` + #[inline] + pub fn new() -> Builder { + Builder::default() + } + + /// Set the HTTP status for this response. + /// + /// By default this is `200`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let response = Response::builder() + /// .status(200) + /// .body(()) + /// .unwrap(); + /// ``` + pub fn status(self, status: T) -> Builder + where + StatusCode: TryFrom, + >::Error: Into, + { + self.and_then(move |mut head| { + head.status = TryFrom::try_from(status).map_err(Into::into)?; + Ok(head) + }) + } + + /// Set the HTTP version for this response. + /// + /// By default this is HTTP/1.1 + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let response = Response::builder() + /// .version(Version::HTTP_2) + /// .body(()) + /// .unwrap(); + /// ``` + pub fn version(self, version: Version) -> Builder { + self.and_then(move |mut head| { + head.version = version; + Ok(head) + }) + } + + /// Appends a header to this response builder. + /// + /// This function will append the provided key/value as a header to the + /// internal `HeaderMap` being constructed. Essentially this is equivalent + /// to calling `HeaderMap::append`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// # use http::header::HeaderValue; + /// + /// let response = Response::builder() + /// .header("Content-Type", "text/html") + /// .header("X-Custom-Foo", "bar") + /// .header("content-length", 0) + /// .body(()) + /// .unwrap(); + /// ``` + pub fn header(self, key: K, value: V) -> Builder + where + HeaderName: TryFrom, + >::Error: Into, + HeaderValue: TryFrom, + >::Error: Into, + { + self.and_then(move |mut head| { + let name = >::try_from(key).map_err(Into::into)?; + let value = >::try_from(value).map_err(Into::into)?; + head.headers.append(name, value); + Ok(head) + }) + } + + /// Get header on this response builder. + /// + /// When builder has error returns None. + /// + /// # Example + /// + /// ``` + /// # use http::Response; + /// # use http::header::HeaderValue; + /// let res = Response::builder() + /// .header("Accept", "text/html") + /// .header("X-Custom-Foo", "bar"); + /// let headers = res.headers_ref().unwrap(); + /// assert_eq!( headers["Accept"], "text/html" ); + /// assert_eq!( headers["X-Custom-Foo"], "bar" ); + /// ``` + pub fn headers_ref(&self) -> Option<&HeaderMap> { + self.inner.as_ref().ok().map(|h| &h.headers) + } + + /// Get header on this response builder. + /// when builder has error returns None + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// # use http::header::HeaderValue; + /// # use http::response::Builder; + /// let mut res = Response::builder(); + /// { + /// let headers = res.headers_mut().unwrap(); + /// headers.insert("Accept", HeaderValue::from_static("text/html")); + /// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar")); + /// } + /// let headers = res.headers_ref().unwrap(); + /// assert_eq!( headers["Accept"], "text/html" ); + /// assert_eq!( headers["X-Custom-Foo"], "bar" ); + /// ``` + pub fn headers_mut(&mut self) -> Option<&mut HeaderMap> { + self.inner.as_mut().ok().map(|h| &mut h.headers) + } + + /// Adds an extension to this builder + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let response = Response::builder() + /// .extension("My Extension") + /// .body(()) + /// .unwrap(); + /// + /// assert_eq!(response.extensions().get::<&'static str>(), + /// Some(&"My Extension")); + /// ``` + pub fn extension(self, extension: T) -> Builder + where + T: Clone + Any + Send + Sync + 'static, + { + self.and_then(move |mut head| { + head.extensions.insert(extension); + Ok(head) + }) + } + + /// Get a reference to the extensions for this response builder. + /// + /// If the builder has an error, this returns `None`. + /// + /// # Example + /// + /// ``` + /// # use http::Response; + /// let res = Response::builder().extension("My Extension").extension(5u32); + /// let extensions = res.extensions_ref().unwrap(); + /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension")); + /// assert_eq!(extensions.get::(), Some(&5u32)); + /// ``` + pub fn extensions_ref(&self) -> Option<&Extensions> { + self.inner.as_ref().ok().map(|h| &h.extensions) + } + + /// Get a mutable reference to the extensions for this response builder. + /// + /// If the builder has an error, this returns `None`. + /// + /// # Example + /// + /// ``` + /// # use http::Response; + /// let mut res = Response::builder().extension("My Extension"); + /// let mut extensions = res.extensions_mut().unwrap(); + /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension")); + /// extensions.insert(5u32); + /// assert_eq!(extensions.get::(), Some(&5u32)); + /// ``` + pub fn extensions_mut(&mut self) -> Option<&mut Extensions> { + self.inner.as_mut().ok().map(|h| &mut h.extensions) + } + + /// "Consumes" this builder, using the provided `body` to return a + /// constructed `Response`. + /// + /// # Errors + /// + /// This function may return an error if any previously configured argument + /// failed to parse or get converted to the internal representation. For + /// example if an invalid `head` was specified via `header("Foo", + /// "Bar\r\n")` the error will be returned when this function is called + /// rather than when `header` was called. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let response = Response::builder() + /// .body(()) + /// .unwrap(); + /// ``` + pub fn body(self, body: T) -> Result> { + self.inner.map(move |head| { + Response { + head, + body, + } + }) + } + + // private + + fn and_then(self, func: F) -> Self + where + F: FnOnce(Parts) -> Result + { + Builder { + inner: self.inner.and_then(func), + } + } +} + +impl Default for Builder { + #[inline] + fn default() -> Builder { + Builder { + inner: Ok(Parts::new()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_can_map_a_body_from_one_type_to_another() { + let response = Response::builder().body("some string").unwrap(); + let mapped_response = response.map(|s| { + assert_eq!(s, "some string"); + 123u32 + }); + assert_eq!(mapped_response.body(), &123u32); + } +} diff --git a/third_party/rust/http/v1/crate/src/status.rs b/third_party/rust/http/v1/crate/src/status.rs new file mode 100644 index 000000000000..d98d24c3d930 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/status.rs @@ -0,0 +1,588 @@ +//! HTTP status codes +//! +//! This module contains HTTP-status code related structs an errors. The main +//! type in this module is `StatusCode` which is not intended to be used through +//! this module but rather the `http::StatusCode` type. +//! +//! # Examples +//! +//! ``` +//! use http::StatusCode; +//! +//! assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); +//! assert_eq!(StatusCode::NOT_FOUND, 404); +//! assert!(StatusCode::OK.is_success()); +//! ``` + +use std::convert::TryFrom; +use std::num::NonZeroU16; +use std::error::Error; +use std::fmt; +use std::str::FromStr; + +/// An HTTP status code (`status-code` in RFC 7230 et al.). +/// +/// Constants are provided for known status codes, including those in the IANA +/// [HTTP Status Code Registry]( +/// https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml). +/// +/// Status code values in the range 100-999 (inclusive) are supported by this +/// type. Values in the range 100-599 are semantically classified by the most +/// significant digit. See [`StatusCode::is_success`], etc. Values above 599 +/// are unclassified but allowed for legacy compatibility, though their use is +/// discouraged. Applications may interpret such values as protocol errors. +/// +/// # Examples +/// +/// ``` +/// use http::StatusCode; +/// +/// assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); +/// assert_eq!(StatusCode::NOT_FOUND.as_u16(), 404); +/// assert!(StatusCode::OK.is_success()); +/// ``` +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct StatusCode(NonZeroU16); + +/// A possible error value when converting a `StatusCode` from a `u16` or `&str` +/// +/// This error indicates that the supplied input was not a valid number, was less +/// than 100, or was greater than 999. +pub struct InvalidStatusCode { + _priv: (), +} + +impl StatusCode { + /// Converts a u16 to a status code. + /// + /// The function validates the correctness of the supplied u16. It must be + /// greater or equal to 100 and less than 1000. + /// + /// # Example + /// + /// ``` + /// use http::StatusCode; + /// + /// let ok = StatusCode::from_u16(200).unwrap(); + /// assert_eq!(ok, StatusCode::OK); + /// + /// let err = StatusCode::from_u16(99); + /// assert!(err.is_err()); + /// ``` + #[inline] + pub fn from_u16(src: u16) -> Result { + if src < 100 || src >= 1000 { + return Err(InvalidStatusCode::new()); + } + + NonZeroU16::new(src) + .map(StatusCode) + .ok_or_else(InvalidStatusCode::new) + } + + /// Converts a &[u8] to a status code + pub fn from_bytes(src: &[u8]) -> Result { + if src.len() != 3 { + return Err(InvalidStatusCode::new()); + } + + let a = src[0].wrapping_sub(b'0') as u16; + let b = src[1].wrapping_sub(b'0') as u16; + let c = src[2].wrapping_sub(b'0') as u16; + + if a == 0 || a > 9 || b > 9 || c > 9 { + return Err(InvalidStatusCode::new()); + } + + let status = (a * 100) + (b * 10) + c; + NonZeroU16::new(status) + .map(StatusCode) + .ok_or_else(InvalidStatusCode::new) + } + + /// Returns the `u16` corresponding to this `StatusCode`. + /// + /// # Note + /// + /// This is the same as the `From` implementation, but + /// included as an inherent method because that implementation doesn't + /// appear in rustdocs, as well as a way to force the type instead of + /// relying on inference. + /// + /// # Example + /// + /// ``` + /// let status = http::StatusCode::OK; + /// assert_eq!(status.as_u16(), 200); + /// ``` + #[inline] + pub fn as_u16(&self) -> u16 { + (*self).into() + } + + /// Returns a &str representation of the `StatusCode` + /// + /// The return value only includes a numerical representation of the + /// status code. The canonical reason is not included. + /// + /// # Example + /// + /// ``` + /// let status = http::StatusCode::OK; + /// assert_eq!(status.as_str(), "200"); + /// ``` + #[inline] + pub fn as_str(&self) -> &str { + let offset = (self.0.get() - 100) as usize; + let offset = offset * 3; + + // Invariant: self has checked range [100, 999] and CODE_DIGITS is + // ASCII-only, of length 900 * 3 = 2700 bytes + + #[cfg(debug_assertions)] + { &CODE_DIGITS[offset..offset+3] } + + #[cfg(not(debug_assertions))] + unsafe { CODE_DIGITS.get_unchecked(offset..offset+3) } + } + + /// Get the standardised `reason-phrase` for this status code. + /// + /// This is mostly here for servers writing responses, but could potentially have application + /// at other times. + /// + /// The reason phrase is defined as being exclusively for human readers. You should avoid + /// deriving any meaning from it at all costs. + /// + /// Bear in mind also that in HTTP/2.0 and HTTP/3.0 the reason phrase is abolished from + /// transmission, and so this canonical reason phrase really is the only reason phrase you’ll + /// find. + /// + /// # Example + /// + /// ``` + /// let status = http::StatusCode::OK; + /// assert_eq!(status.canonical_reason(), Some("OK")); + /// ``` + pub fn canonical_reason(&self) -> Option<&'static str> { + canonical_reason(self.0.get()) + } + + /// Check if status is within 100-199. + #[inline] + pub fn is_informational(&self) -> bool { + 200 > self.0.get() && self.0.get() >= 100 + } + + /// Check if status is within 200-299. + #[inline] + pub fn is_success(&self) -> bool { + 300 > self.0.get() && self.0.get() >= 200 + } + + /// Check if status is within 300-399. + #[inline] + pub fn is_redirection(&self) -> bool { + 400 > self.0.get() && self.0.get() >= 300 + } + + /// Check if status is within 400-499. + #[inline] + pub fn is_client_error(&self) -> bool { + 500 > self.0.get() && self.0.get() >= 400 + } + + /// Check if status is within 500-599. + #[inline] + pub fn is_server_error(&self) -> bool { + 600 > self.0.get() && self.0.get() >= 500 + } +} + +impl fmt::Debug for StatusCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f) + } +} + +/// Formats the status code, *including* the canonical reason. +/// +/// # Example +/// +/// ``` +/// # use http::StatusCode; +/// assert_eq!(format!("{}", StatusCode::OK), "200 OK"); +/// ``` +impl fmt::Display for StatusCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} {}", + u16::from(*self), + self.canonical_reason().unwrap_or("") + ) + } +} + +impl Default for StatusCode { + #[inline] + fn default() -> StatusCode { + StatusCode::OK + } +} + +impl PartialEq for StatusCode { + #[inline] + fn eq(&self, other: &u16) -> bool { + self.as_u16() == *other + } +} + +impl PartialEq for u16 { + #[inline] + fn eq(&self, other: &StatusCode) -> bool { + *self == other.as_u16() + } +} + +impl From for u16 { + #[inline] + fn from(status: StatusCode) -> u16 { + status.0.get() + } +} + +impl FromStr for StatusCode { + type Err = InvalidStatusCode; + + fn from_str(s: &str) -> Result { + StatusCode::from_bytes(s.as_ref()) + } +} + +impl<'a> From<&'a StatusCode> for StatusCode { + #[inline] + fn from(t: &'a StatusCode) -> Self { + t.clone() + } +} + +impl<'a> TryFrom<&'a [u8]> for StatusCode { + type Error = InvalidStatusCode; + + #[inline] + fn try_from(t: &'a [u8]) -> Result { + StatusCode::from_bytes(t) + } +} + +impl<'a> TryFrom<&'a str> for StatusCode { + type Error = InvalidStatusCode; + + #[inline] + fn try_from(t: &'a str) -> Result { + t.parse() + } +} + +impl TryFrom for StatusCode { + type Error = InvalidStatusCode; + + #[inline] + fn try_from(t: u16) -> Result { + StatusCode::from_u16(t) + } +} + +macro_rules! status_codes { + ( + $( + $(#[$docs:meta])* + ($num:expr, $konst:ident, $phrase:expr); + )+ + ) => { + impl StatusCode { + $( + $(#[$docs])* + pub const $konst: StatusCode = StatusCode(unsafe { NonZeroU16::new_unchecked($num) }); + )+ + + } + + fn canonical_reason(num: u16) -> Option<&'static str> { + match num { + $( + $num => Some($phrase), + )+ + _ => None + } + } + } +} + +status_codes! { + /// 100 Continue + /// [[RFC7231, Section 6.2.1](https://tools.ietf.org/html/rfc7231#section-6.2.1)] + (100, CONTINUE, "Continue"); + /// 101 Switching Protocols + /// [[RFC7231, Section 6.2.2](https://tools.ietf.org/html/rfc7231#section-6.2.2)] + (101, SWITCHING_PROTOCOLS, "Switching Protocols"); + /// 102 Processing + /// [[RFC2518](https://tools.ietf.org/html/rfc2518)] + (102, PROCESSING, "Processing"); + + /// 200 OK + /// [[RFC7231, Section 6.3.1](https://tools.ietf.org/html/rfc7231#section-6.3.1)] + (200, OK, "OK"); + /// 201 Created + /// [[RFC7231, Section 6.3.2](https://tools.ietf.org/html/rfc7231#section-6.3.2)] + (201, CREATED, "Created"); + /// 202 Accepted + /// [[RFC7231, Section 6.3.3](https://tools.ietf.org/html/rfc7231#section-6.3.3)] + (202, ACCEPTED, "Accepted"); + /// 203 Non-Authoritative Information + /// [[RFC7231, Section 6.3.4](https://tools.ietf.org/html/rfc7231#section-6.3.4)] + (203, NON_AUTHORITATIVE_INFORMATION, "Non Authoritative Information"); + /// 204 No Content + /// [[RFC7231, Section 6.3.5](https://tools.ietf.org/html/rfc7231#section-6.3.5)] + (204, NO_CONTENT, "No Content"); + /// 205 Reset Content + /// [[RFC7231, Section 6.3.6](https://tools.ietf.org/html/rfc7231#section-6.3.6)] + (205, RESET_CONTENT, "Reset Content"); + /// 206 Partial Content + /// [[RFC7233, Section 4.1](https://tools.ietf.org/html/rfc7233#section-4.1)] + (206, PARTIAL_CONTENT, "Partial Content"); + /// 207 Multi-Status + /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] + (207, MULTI_STATUS, "Multi-Status"); + /// 208 Already Reported + /// [[RFC5842](https://tools.ietf.org/html/rfc5842)] + (208, ALREADY_REPORTED, "Already Reported"); + + /// 226 IM Used + /// [[RFC3229](https://tools.ietf.org/html/rfc3229)] + (226, IM_USED, "IM Used"); + + /// 300 Multiple Choices + /// [[RFC7231, Section 6.4.1](https://tools.ietf.org/html/rfc7231#section-6.4.1)] + (300, MULTIPLE_CHOICES, "Multiple Choices"); + /// 301 Moved Permanently + /// [[RFC7231, Section 6.4.2](https://tools.ietf.org/html/rfc7231#section-6.4.2)] + (301, MOVED_PERMANENTLY, "Moved Permanently"); + /// 302 Found + /// [[RFC7231, Section 6.4.3](https://tools.ietf.org/html/rfc7231#section-6.4.3)] + (302, FOUND, "Found"); + /// 303 See Other + /// [[RFC7231, Section 6.4.4](https://tools.ietf.org/html/rfc7231#section-6.4.4)] + (303, SEE_OTHER, "See Other"); + /// 304 Not Modified + /// [[RFC7232, Section 4.1](https://tools.ietf.org/html/rfc7232#section-4.1)] + (304, NOT_MODIFIED, "Not Modified"); + /// 305 Use Proxy + /// [[RFC7231, Section 6.4.5](https://tools.ietf.org/html/rfc7231#section-6.4.5)] + (305, USE_PROXY, "Use Proxy"); + /// 307 Temporary Redirect + /// [[RFC7231, Section 6.4.7](https://tools.ietf.org/html/rfc7231#section-6.4.7)] + (307, TEMPORARY_REDIRECT, "Temporary Redirect"); + /// 308 Permanent Redirect + /// [[RFC7238](https://tools.ietf.org/html/rfc7238)] + (308, PERMANENT_REDIRECT, "Permanent Redirect"); + + /// 400 Bad Request + /// [[RFC7231, Section 6.5.1](https://tools.ietf.org/html/rfc7231#section-6.5.1)] + (400, BAD_REQUEST, "Bad Request"); + /// 401 Unauthorized + /// [[RFC7235, Section 3.1](https://tools.ietf.org/html/rfc7235#section-3.1)] + (401, UNAUTHORIZED, "Unauthorized"); + /// 402 Payment Required + /// [[RFC7231, Section 6.5.2](https://tools.ietf.org/html/rfc7231#section-6.5.2)] + (402, PAYMENT_REQUIRED, "Payment Required"); + /// 403 Forbidden + /// [[RFC7231, Section 6.5.3](https://tools.ietf.org/html/rfc7231#section-6.5.3)] + (403, FORBIDDEN, "Forbidden"); + /// 404 Not Found + /// [[RFC7231, Section 6.5.4](https://tools.ietf.org/html/rfc7231#section-6.5.4)] + (404, NOT_FOUND, "Not Found"); + /// 405 Method Not Allowed + /// [[RFC7231, Section 6.5.5](https://tools.ietf.org/html/rfc7231#section-6.5.5)] + (405, METHOD_NOT_ALLOWED, "Method Not Allowed"); + /// 406 Not Acceptable + /// [[RFC7231, Section 6.5.6](https://tools.ietf.org/html/rfc7231#section-6.5.6)] + (406, NOT_ACCEPTABLE, "Not Acceptable"); + /// 407 Proxy Authentication Required + /// [[RFC7235, Section 3.2](https://tools.ietf.org/html/rfc7235#section-3.2)] + (407, PROXY_AUTHENTICATION_REQUIRED, "Proxy Authentication Required"); + /// 408 Request Timeout + /// [[RFC7231, Section 6.5.7](https://tools.ietf.org/html/rfc7231#section-6.5.7)] + (408, REQUEST_TIMEOUT, "Request Timeout"); + /// 409 Conflict + /// [[RFC7231, Section 6.5.8](https://tools.ietf.org/html/rfc7231#section-6.5.8)] + (409, CONFLICT, "Conflict"); + /// 410 Gone + /// [[RFC7231, Section 6.5.9](https://tools.ietf.org/html/rfc7231#section-6.5.9)] + (410, GONE, "Gone"); + /// 411 Length Required + /// [[RFC7231, Section 6.5.10](https://tools.ietf.org/html/rfc7231#section-6.5.10)] + (411, LENGTH_REQUIRED, "Length Required"); + /// 412 Precondition Failed + /// [[RFC7232, Section 4.2](https://tools.ietf.org/html/rfc7232#section-4.2)] + (412, PRECONDITION_FAILED, "Precondition Failed"); + /// 413 Payload Too Large + /// [[RFC7231, Section 6.5.11](https://tools.ietf.org/html/rfc7231#section-6.5.11)] + (413, PAYLOAD_TOO_LARGE, "Payload Too Large"); + /// 414 URI Too Long + /// [[RFC7231, Section 6.5.12](https://tools.ietf.org/html/rfc7231#section-6.5.12)] + (414, URI_TOO_LONG, "URI Too Long"); + /// 415 Unsupported Media Type + /// [[RFC7231, Section 6.5.13](https://tools.ietf.org/html/rfc7231#section-6.5.13)] + (415, UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type"); + /// 416 Range Not Satisfiable + /// [[RFC7233, Section 4.4](https://tools.ietf.org/html/rfc7233#section-4.4)] + (416, RANGE_NOT_SATISFIABLE, "Range Not Satisfiable"); + /// 417 Expectation Failed + /// [[RFC7231, Section 6.5.14](https://tools.ietf.org/html/rfc7231#section-6.5.14)] + (417, EXPECTATION_FAILED, "Expectation Failed"); + /// 418 I'm a teapot + /// [curiously not registered by IANA but [RFC2324](https://tools.ietf.org/html/rfc2324)] + (418, IM_A_TEAPOT, "I'm a teapot"); + + /// 421 Misdirected Request + /// [RFC7540, Section 9.1.2](http://tools.ietf.org/html/rfc7540#section-9.1.2) + (421, MISDIRECTED_REQUEST, "Misdirected Request"); + /// 422 Unprocessable Entity + /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] + (422, UNPROCESSABLE_ENTITY, "Unprocessable Entity"); + /// 423 Locked + /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] + (423, LOCKED, "Locked"); + /// 424 Failed Dependency + /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] + (424, FAILED_DEPENDENCY, "Failed Dependency"); + + /// 426 Upgrade Required + /// [[RFC7231, Section 6.5.15](https://tools.ietf.org/html/rfc7231#section-6.5.15)] + (426, UPGRADE_REQUIRED, "Upgrade Required"); + + /// 428 Precondition Required + /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] + (428, PRECONDITION_REQUIRED, "Precondition Required"); + /// 429 Too Many Requests + /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] + (429, TOO_MANY_REQUESTS, "Too Many Requests"); + + /// 431 Request Header Fields Too Large + /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] + (431, REQUEST_HEADER_FIELDS_TOO_LARGE, "Request Header Fields Too Large"); + + /// 451 Unavailable For Legal Reasons + /// [[RFC7725](http://tools.ietf.org/html/rfc7725)] + (451, UNAVAILABLE_FOR_LEGAL_REASONS, "Unavailable For Legal Reasons"); + + /// 500 Internal Server Error + /// [[RFC7231, Section 6.6.1](https://tools.ietf.org/html/rfc7231#section-6.6.1)] + (500, INTERNAL_SERVER_ERROR, "Internal Server Error"); + /// 501 Not Implemented + /// [[RFC7231, Section 6.6.2](https://tools.ietf.org/html/rfc7231#section-6.6.2)] + (501, NOT_IMPLEMENTED, "Not Implemented"); + /// 502 Bad Gateway + /// [[RFC7231, Section 6.6.3](https://tools.ietf.org/html/rfc7231#section-6.6.3)] + (502, BAD_GATEWAY, "Bad Gateway"); + /// 503 Service Unavailable + /// [[RFC7231, Section 6.6.4](https://tools.ietf.org/html/rfc7231#section-6.6.4)] + (503, SERVICE_UNAVAILABLE, "Service Unavailable"); + /// 504 Gateway Timeout + /// [[RFC7231, Section 6.6.5](https://tools.ietf.org/html/rfc7231#section-6.6.5)] + (504, GATEWAY_TIMEOUT, "Gateway Timeout"); + /// 505 HTTP Version Not Supported + /// [[RFC7231, Section 6.6.6](https://tools.ietf.org/html/rfc7231#section-6.6.6)] + (505, HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported"); + /// 506 Variant Also Negotiates + /// [[RFC2295](https://tools.ietf.org/html/rfc2295)] + (506, VARIANT_ALSO_NEGOTIATES, "Variant Also Negotiates"); + /// 507 Insufficient Storage + /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] + (507, INSUFFICIENT_STORAGE, "Insufficient Storage"); + /// 508 Loop Detected + /// [[RFC5842](https://tools.ietf.org/html/rfc5842)] + (508, LOOP_DETECTED, "Loop Detected"); + + /// 510 Not Extended + /// [[RFC2774](https://tools.ietf.org/html/rfc2774)] + (510, NOT_EXTENDED, "Not Extended"); + /// 511 Network Authentication Required + /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] + (511, NETWORK_AUTHENTICATION_REQUIRED, "Network Authentication Required"); +} + +impl InvalidStatusCode { + fn new() -> InvalidStatusCode { + InvalidStatusCode { + _priv: (), + } + } +} + +impl fmt::Debug for InvalidStatusCode { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("InvalidStatusCode") + // skip _priv noise + .finish() + } +} + +impl fmt::Display for InvalidStatusCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("invalid status code") + } +} + +impl Error for InvalidStatusCode {} + +// A string of packed 3-ASCII-digit status code values for the supported range +// of [100, 999] (900 codes, 2700 bytes). +const CODE_DIGITS: &'static str = "\ +100101102103104105106107108109110111112113114115116117118119\ +120121122123124125126127128129130131132133134135136137138139\ +140141142143144145146147148149150151152153154155156157158159\ +160161162163164165166167168169170171172173174175176177178179\ +180181182183184185186187188189190191192193194195196197198199\ +200201202203204205206207208209210211212213214215216217218219\ +220221222223224225226227228229230231232233234235236237238239\ +240241242243244245246247248249250251252253254255256257258259\ +260261262263264265266267268269270271272273274275276277278279\ +280281282283284285286287288289290291292293294295296297298299\ +300301302303304305306307308309310311312313314315316317318319\ +320321322323324325326327328329330331332333334335336337338339\ +340341342343344345346347348349350351352353354355356357358359\ +360361362363364365366367368369370371372373374375376377378379\ +380381382383384385386387388389390391392393394395396397398399\ +400401402403404405406407408409410411412413414415416417418419\ +420421422423424425426427428429430431432433434435436437438439\ +440441442443444445446447448449450451452453454455456457458459\ +460461462463464465466467468469470471472473474475476477478479\ +480481482483484485486487488489490491492493494495496497498499\ +500501502503504505506507508509510511512513514515516517518519\ +520521522523524525526527528529530531532533534535536537538539\ +540541542543544545546547548549550551552553554555556557558559\ +560561562563564565566567568569570571572573574575576577578579\ +580581582583584585586587588589590591592593594595596597598599\ +600601602603604605606607608609610611612613614615616617618619\ +620621622623624625626627628629630631632633634635636637638639\ +640641642643644645646647648649650651652653654655656657658659\ +660661662663664665666667668669670671672673674675676677678679\ +680681682683684685686687688689690691692693694695696697698699\ +700701702703704705706707708709710711712713714715716717718719\ +720721722723724725726727728729730731732733734735736737738739\ +740741742743744745746747748749750751752753754755756757758759\ +760761762763764765766767768769770771772773774775776777778779\ +780781782783784785786787788789790791792793794795796797798799\ +800801802803804805806807808809810811812813814815816817818819\ +820821822823824825826827828829830831832833834835836837838839\ +840841842843844845846847848849850851852853854855856857858859\ +860861862863864865866867868869870871872873874875876877878879\ +880881882883884885886887888889890891892893894895896897898899\ +900901902903904905906907908909910911912913914915916917918919\ +920921922923924925926927928929930931932933934935936937938939\ +940941942943944945946947948949950951952953954955956957958959\ +960961962963964965966967968969970971972973974975976977978979\ +980981982983984985986987988989990991992993994995996997998999"; diff --git a/third_party/rust/http/v1/crate/src/uri/authority.rs b/third_party/rust/http/v1/crate/src/uri/authority.rs new file mode 100644 index 000000000000..f41ddd19cb05 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/authority.rs @@ -0,0 +1,684 @@ +use std::convert::TryFrom; +use std::hash::{Hash, Hasher}; +use std::str::FromStr; +use std::{cmp, fmt, str}; + +use bytes::Bytes; + +use super::{ErrorKind, InvalidUri, Port, URI_CHARS}; +use crate::byte_str::ByteStr; + +/// Represents the authority component of a URI. +#[derive(Clone)] +pub struct Authority { + pub(super) data: ByteStr, +} + +impl Authority { + pub(super) fn empty() -> Self { + Authority { + data: ByteStr::new(), + } + } + + // Not public while `bytes` is unstable. + pub(super) fn from_shared(s: Bytes) -> Result { + // Precondition on create_authority: trivially satisfied by the + // identity clousre + create_authority(s, |s| s) + } + + /// Attempt to convert an `Authority` from a static string. + /// + /// This function will not perform any copying, and the string will be + /// checked if it is empty or contains an invalid character. + /// + /// # Panics + /// + /// This function panics if the argument contains invalid characters or + /// is empty. + /// + /// # Examples + /// + /// ``` + /// # use http::uri::Authority; + /// let authority = Authority::from_static("example.com"); + /// assert_eq!(authority.host(), "example.com"); + /// ``` + pub fn from_static(src: &'static str) -> Self { + Authority::from_shared(Bytes::from_static(src.as_bytes())) + .expect("static str is not valid authority") + } + + /// Attempt to convert a `Bytes` buffer to a `Authority`. + /// + /// This will try to prevent a copy if the type passed is the type used + /// internally, and will copy the data if it is not. + pub fn from_maybe_shared(src: T) -> Result + where + T: AsRef<[u8]> + 'static, + { + if_downcast_into!(T, Bytes, src, { + return Authority::from_shared(src); + }); + + Authority::try_from(src.as_ref()) + } + + // Note: this may return an *empty* Authority. You might want `parse_non_empty`. + // Postcondition: for all Ok() returns, s[..ret.unwrap()] is valid UTF-8 where + // ret is the return value. + pub(super) fn parse(s: &[u8]) -> Result { + let mut colon_cnt = 0u32; + let mut start_bracket = false; + let mut end_bracket = false; + let mut has_percent = false; + let mut end = s.len(); + let mut at_sign_pos = None; + const MAX_COLONS: u32 = 8; // e.g., [FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80 + + // Among other things, this loop checks that every byte in s up to the + // first '/', '?', or '#' is a valid URI character (or in some contexts, + // a '%'). This means that each such byte is a valid single-byte UTF-8 + // code point. + for (i, &b) in s.iter().enumerate() { + match URI_CHARS[b as usize] { + b'/' | b'?' | b'#' => { + end = i; + break; + } + b':' => { + if colon_cnt >= MAX_COLONS { + return Err(ErrorKind::InvalidAuthority.into()); + } + colon_cnt += 1; + } + b'[' => { + if has_percent || start_bracket { + // Something other than the userinfo has a `%`, so reject it. + return Err(ErrorKind::InvalidAuthority.into()); + } + start_bracket = true; + } + b']' => { + if (!start_bracket) || end_bracket { + return Err(ErrorKind::InvalidAuthority.into()); + } + end_bracket = true; + + // Those were part of an IPv6 hostname, so forget them... + colon_cnt = 0; + has_percent = false; + } + b'@' => { + at_sign_pos = Some(i); + + // Those weren't a port colon, but part of the + // userinfo, so it needs to be forgotten. + colon_cnt = 0; + has_percent = false; + } + 0 if b == b'%' => { + // Per https://tools.ietf.org/html/rfc3986#section-3.2.1 and + // https://url.spec.whatwg.org/#authority-state + // the userinfo can have a percent-encoded username and password, + // so record that a `%` was found. If this turns out to be + // part of the userinfo, this flag will be cleared. + // Also per https://tools.ietf.org/html/rfc6874, percent-encoding can + // be used to indicate a zone identifier. + // If the flag hasn't been cleared at the end, that means this + // was part of the hostname (and not part of an IPv6 address), and + // will fail with an error. + has_percent = true; + } + 0 => { + return Err(ErrorKind::InvalidUriChar.into()); + } + _ => {} + } + } + + if start_bracket ^ end_bracket { + return Err(ErrorKind::InvalidAuthority.into()); + } + + if colon_cnt > 1 { + // Things like 'localhost:8080:3030' are rejected. + return Err(ErrorKind::InvalidAuthority.into()); + } + + if end > 0 && at_sign_pos == Some(end - 1) { + // If there's nothing after an `@`, this is bonkers. + return Err(ErrorKind::InvalidAuthority.into()); + } + + if has_percent { + // Something after the userinfo has a `%`, so reject it. + return Err(ErrorKind::InvalidAuthority.into()); + } + + Ok(end) + } + + // Parse bytes as an Authority, not allowing an empty string. + // + // This should be used by functions that allow a user to parse + // an `Authority` by itself. + // + // Postcondition: for all Ok() returns, s[..ret.unwrap()] is valid UTF-8 where + // ret is the return value. + fn parse_non_empty(s: &[u8]) -> Result { + if s.is_empty() { + return Err(ErrorKind::Empty.into()); + } + Authority::parse(s) + } + + /// Get the host of this `Authority`. + /// + /// The host subcomponent of authority is identified by an IP literal + /// encapsulated within square brackets, an IPv4 address in dotted- decimal + /// form, or a registered name. The host subcomponent is **case-insensitive**. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |---------| + /// | + /// host + /// ``` + /// + /// # Examples + /// + /// ``` + /// # use http::uri::*; + /// let authority: Authority = "example.org:80".parse().unwrap(); + /// + /// assert_eq!(authority.host(), "example.org"); + /// ``` + #[inline] + pub fn host(&self) -> &str { + host(self.as_str()) + } + + /// Get the port part of this `Authority`. + /// + /// The port subcomponent of authority is designated by an optional port + /// number following the host and delimited from it by a single colon (":") + /// character. It can be turned into a decimal port number with the `as_u16` + /// method or as a `str` with the `as_str` method. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |-| + /// | + /// port + /// ``` + /// + /// # Examples + /// + /// Authority with port + /// + /// ``` + /// # use http::uri::Authority; + /// let authority: Authority = "example.org:80".parse().unwrap(); + /// + /// let port = authority.port().unwrap(); + /// assert_eq!(port.as_u16(), 80); + /// assert_eq!(port.as_str(), "80"); + /// ``` + /// + /// Authority without port + /// + /// ``` + /// # use http::uri::Authority; + /// let authority: Authority = "example.org".parse().unwrap(); + /// + /// assert!(authority.port().is_none()); + /// ``` + pub fn port(&self) -> Option> { + let bytes = self.as_str(); + bytes + .rfind(":") + .and_then(|i| Port::from_str(&bytes[i + 1..]).ok()) + } + + /// Get the port of this `Authority` as a `u16`. + /// + /// # Example + /// + /// ``` + /// # use http::uri::Authority; + /// let authority: Authority = "example.org:80".parse().unwrap(); + /// + /// assert_eq!(authority.port_u16(), Some(80)); + /// ``` + pub fn port_u16(&self) -> Option { + self.port().and_then(|p| Some(p.as_u16())) + } + + /// Return a str representation of the authority + #[inline] + pub fn as_str(&self) -> &str { + &self.data[..] + } +} + +// Purposefully not public while `bytes` is unstable. +// impl TryFrom for Authority + +impl AsRef for Authority { + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl PartialEq for Authority { + fn eq(&self, other: &Authority) -> bool { + self.data.eq_ignore_ascii_case(&other.data) + } +} + +impl Eq for Authority {} + +/// Case-insensitive equality +/// +/// # Examples +/// +/// ``` +/// # use http::uri::Authority; +/// let authority: Authority = "HELLO.com".parse().unwrap(); +/// assert_eq!(authority, "hello.coM"); +/// assert_eq!("hello.com", authority); +/// ``` +impl PartialEq for Authority { + fn eq(&self, other: &str) -> bool { + self.data.eq_ignore_ascii_case(other) + } +} + +impl PartialEq for str { + fn eq(&self, other: &Authority) -> bool { + self.eq_ignore_ascii_case(other.as_str()) + } +} + +impl<'a> PartialEq for &'a str { + fn eq(&self, other: &Authority) -> bool { + self.eq_ignore_ascii_case(other.as_str()) + } +} + +impl<'a> PartialEq<&'a str> for Authority { + fn eq(&self, other: &&'a str) -> bool { + self.data.eq_ignore_ascii_case(other) + } +} + +impl PartialEq for Authority { + fn eq(&self, other: &String) -> bool { + self.data.eq_ignore_ascii_case(other.as_str()) + } +} + +impl PartialEq for String { + fn eq(&self, other: &Authority) -> bool { + self.as_str().eq_ignore_ascii_case(other.as_str()) + } +} + +/// Case-insensitive ordering +/// +/// # Examples +/// +/// ``` +/// # use http::uri::Authority; +/// let authority: Authority = "DEF.com".parse().unwrap(); +/// assert!(authority < "ghi.com"); +/// assert!(authority > "abc.com"); +/// ``` +impl PartialOrd for Authority { + fn partial_cmp(&self, other: &Authority) -> Option { + let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +impl PartialOrd for Authority { + fn partial_cmp(&self, other: &str) -> Option { + let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +impl PartialOrd for str { + fn partial_cmp(&self, other: &Authority) -> Option { + let left = self.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +impl<'a> PartialOrd for &'a str { + fn partial_cmp(&self, other: &Authority) -> Option { + let left = self.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +impl<'a> PartialOrd<&'a str> for Authority { + fn partial_cmp(&self, other: &&'a str) -> Option { + let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +impl PartialOrd for Authority { + fn partial_cmp(&self, other: &String) -> Option { + let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +impl PartialOrd for String { + fn partial_cmp(&self, other: &Authority) -> Option { + let left = self.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); + left.partial_cmp(right) + } +} + +/// Case-insensitive hashing +/// +/// # Examples +/// +/// ``` +/// # use http::uri::Authority; +/// # use std::hash::{Hash, Hasher}; +/// # use std::collections::hash_map::DefaultHasher; +/// +/// let a: Authority = "HELLO.com".parse().unwrap(); +/// let b: Authority = "hello.coM".parse().unwrap(); +/// +/// let mut s = DefaultHasher::new(); +/// a.hash(&mut s); +/// let a = s.finish(); +/// +/// let mut s = DefaultHasher::new(); +/// b.hash(&mut s); +/// let b = s.finish(); +/// +/// assert_eq!(a, b); +/// ``` +impl Hash for Authority { + fn hash(&self, state: &mut H) + where + H: Hasher, + { + self.data.len().hash(state); + for &b in self.data.as_bytes() { + state.write_u8(b.to_ascii_lowercase()); + } + } +} + +impl<'a> TryFrom<&'a [u8]> for Authority { + type Error = InvalidUri; + #[inline] + fn try_from(s: &'a [u8]) -> Result { + // parse first, and only turn into Bytes if valid + + // Preconditon on create_authority: copy_from_slice() copies all of + // bytes from the [u8] parameter into a new Bytes + create_authority(s, |s| Bytes::copy_from_slice(s)) + } +} + +impl<'a> TryFrom<&'a str> for Authority { + type Error = InvalidUri; + #[inline] + fn try_from(s: &'a str) -> Result { + TryFrom::try_from(s.as_bytes()) + } +} + +impl TryFrom> for Authority { + type Error = InvalidUri; + + #[inline] + fn try_from(vec: Vec) -> Result { + Authority::from_shared(vec.into()) + } +} + +impl TryFrom for Authority { + type Error = InvalidUri; + + #[inline] + fn try_from(t: String) -> Result { + Authority::from_shared(t.into()) + } +} + +impl FromStr for Authority { + type Err = InvalidUri; + + fn from_str(s: &str) -> Result { + TryFrom::try_from(s) + } +} + +impl fmt::Debug for Authority { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + +impl fmt::Display for Authority { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + +fn host(auth: &str) -> &str { + let host_port = auth + .rsplitn(2, '@') + .next() + .expect("split always has at least 1 item"); + + if host_port.as_bytes()[0] == b'[' { + let i = host_port + .find(']') + .expect("parsing should validate brackets"); + // ..= ranges aren't available in 1.20, our minimum Rust version... + &host_port[0..i + 1] + } else { + host_port + .split(':') + .next() + .expect("split always has at least 1 item") + } +} + +// Precondition: f converts all of the bytes in the passed in B into the +// returned Bytes. +fn create_authority(b: B, f: F) -> Result +where + B: AsRef<[u8]>, + F: FnOnce(B) -> Bytes, +{ + let s = b.as_ref(); + let authority_end = Authority::parse_non_empty(s)?; + + if authority_end != s.len() { + return Err(ErrorKind::InvalidUriChar.into()); + } + + let bytes = f(b); + + Ok(Authority { + // Safety: the postcondition on parse_non_empty() and the check against + // s.len() ensure that b is valid UTF-8. The precondition on f ensures + // that this is carried through to bytes. + data: unsafe { ByteStr::from_utf8_unchecked(bytes) }, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_empty_string_is_error() { + let err = Authority::parse_non_empty(b"").unwrap_err(); + assert_eq!(err.0, ErrorKind::Empty); + } + + #[test] + fn equal_to_self_of_same_authority() { + let authority1: Authority = "example.com".parse().unwrap(); + let authority2: Authority = "EXAMPLE.COM".parse().unwrap(); + assert_eq!(authority1, authority2); + assert_eq!(authority2, authority1); + } + + #[test] + fn not_equal_to_self_of_different_authority() { + let authority1: Authority = "example.com".parse().unwrap(); + let authority2: Authority = "test.com".parse().unwrap(); + assert_ne!(authority1, authority2); + assert_ne!(authority2, authority1); + } + + #[test] + fn equates_with_a_str() { + let authority: Authority = "example.com".parse().unwrap(); + assert_eq!(&authority, "EXAMPLE.com"); + assert_eq!("EXAMPLE.com", &authority); + assert_eq!(authority, "EXAMPLE.com"); + assert_eq!("EXAMPLE.com", authority); + } + + #[test] + fn from_static_equates_with_a_str() { + let authority = Authority::from_static("example.com"); + assert_eq!(authority, "example.com"); + } + + #[test] + fn not_equal_with_a_str_of_a_different_authority() { + let authority: Authority = "example.com".parse().unwrap(); + assert_ne!(&authority, "test.com"); + assert_ne!("test.com", &authority); + assert_ne!(authority, "test.com"); + assert_ne!("test.com", authority); + } + + #[test] + fn equates_with_a_string() { + let authority: Authority = "example.com".parse().unwrap(); + assert_eq!(authority, "EXAMPLE.com".to_string()); + assert_eq!("EXAMPLE.com".to_string(), authority); + } + + #[test] + fn equates_with_a_string_of_a_different_authority() { + let authority: Authority = "example.com".parse().unwrap(); + assert_ne!(authority, "test.com".to_string()); + assert_ne!("test.com".to_string(), authority); + } + + #[test] + fn compares_to_self() { + let authority1: Authority = "abc.com".parse().unwrap(); + let authority2: Authority = "def.com".parse().unwrap(); + assert!(authority1 < authority2); + assert!(authority2 > authority1); + } + + #[test] + fn compares_with_a_str() { + let authority: Authority = "def.com".parse().unwrap(); + // with ref + assert!(&authority < "ghi.com"); + assert!("ghi.com" > &authority); + assert!(&authority > "abc.com"); + assert!("abc.com" < &authority); + + // no ref + assert!(authority < "ghi.com"); + assert!("ghi.com" > authority); + assert!(authority > "abc.com"); + assert!("abc.com" < authority); + } + + #[test] + fn compares_with_a_string() { + let authority: Authority = "def.com".parse().unwrap(); + assert!(authority < "ghi.com".to_string()); + assert!("ghi.com".to_string() > authority); + assert!(authority > "abc.com".to_string()); + assert!("abc.com".to_string() < authority); + } + + #[test] + fn allows_percent_in_userinfo() { + let authority_str = "a%2f:b%2f@example.com"; + let authority: Authority = authority_str.parse().unwrap(); + assert_eq!(authority, authority_str); + } + + #[test] + fn rejects_percent_in_hostname() { + let err = Authority::parse_non_empty(b"example%2f.com").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + + let err = Authority::parse_non_empty(b"a%2f:b%2f@example%2f.com").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + } + + #[test] + fn allows_percent_in_ipv6_address() { + let authority_str = "[fe80::1:2:3:4%25eth0]"; + let result: Authority = authority_str.parse().unwrap(); + assert_eq!(result, authority_str); + } + + #[test] + fn reject_obviously_invalid_ipv6_address() { + let err = Authority::parse_non_empty(b"[0:1:2:3:4:5:6:7:8:9:10:11:12:13:14]").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + } + + #[test] + fn rejects_percent_outside_ipv6_address() { + let err = Authority::parse_non_empty(b"1234%20[fe80::1:2:3:4]").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + + let err = Authority::parse_non_empty(b"[fe80::1:2:3:4]%20").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + } + + #[test] + fn rejects_invalid_utf8() { + let err = Authority::try_from([0xc0u8].as_ref()).unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidUriChar); + + let err = Authority::from_shared(Bytes::from_static([0xc0u8].as_ref())).unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidUriChar); + } + + #[test] + fn rejects_invalid_use_of_brackets() { + let err = Authority::parse_non_empty(b"[]@[").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + + // reject tie-fighter + let err = Authority::parse_non_empty(b"]o[").unwrap_err(); + assert_eq!(err.0, ErrorKind::InvalidAuthority); + } +} diff --git a/third_party/rust/http/v1/crate/src/uri/builder.rs b/third_party/rust/http/v1/crate/src/uri/builder.rs new file mode 100644 index 000000000000..825c0fafcca1 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/builder.rs @@ -0,0 +1,197 @@ +use std::convert::{TryFrom, TryInto}; + +use super::{Authority, Parts, PathAndQuery, Scheme}; +use crate::Uri; + +/// A builder for `Uri`s. +/// +/// This type can be used to construct an instance of `Uri` +/// through a builder pattern. +#[derive(Debug)] +pub struct Builder { + parts: Result, +} + +impl Builder { + /// Creates a new default instance of `Builder` to construct a `Uri`. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let uri = uri::Builder::new() + /// .scheme("https") + /// .authority("hyper.rs") + /// .path_and_query("/") + /// .build() + /// .unwrap(); + /// ``` + #[inline] + pub fn new() -> Builder { + Builder::default() + } + + /// Set the `Scheme` for this URI. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let mut builder = uri::Builder::new(); + /// builder.scheme("https"); + /// ``` + pub fn scheme(self, scheme: T) -> Self + where + Scheme: TryFrom, + >::Error: Into, + { + self.map(move |mut parts| { + let scheme = scheme.try_into().map_err(Into::into)?; + parts.scheme = Some(scheme); + Ok(parts) + }) + } + + /// Set the `Authority` for this URI. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let uri = uri::Builder::new() + /// .authority("tokio.rs") + /// .build() + /// .unwrap(); + /// ``` + pub fn authority(self, auth: T) -> Self + where + Authority: TryFrom, + >::Error: Into, + { + self.map(move |mut parts| { + let auth = auth.try_into().map_err(Into::into)?; + parts.authority = Some(auth); + Ok(parts) + }) + } + + /// Set the `PathAndQuery` for this URI. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let uri = uri::Builder::new() + /// .path_and_query("/hello?foo=bar") + /// .build() + /// .unwrap(); + /// ``` + pub fn path_and_query(self, p_and_q: T) -> Self + where + PathAndQuery: TryFrom, + >::Error: Into, + { + self.map(move |mut parts| { + let p_and_q = p_and_q.try_into().map_err(Into::into)?; + parts.path_and_query = Some(p_and_q); + Ok(parts) + }) + } + + /// Consumes this builder, and tries to construct a valid `Uri` from + /// the configured pieces. + /// + /// # Errors + /// + /// This function may return an error if any previously configured argument + /// failed to parse or get converted to the internal representation. For + /// example if an invalid `scheme` was specified via `scheme("!@#%/^")` + /// the error will be returned when this function is called rather than + /// when `scheme` was called. + /// + /// Additionally, the various forms of URI require certain combinations of + /// parts to be set to be valid. If the parts don't fit into any of the + /// valid forms of URI, a new error is returned. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let uri = Uri::builder() + /// .build() + /// .unwrap(); + /// ``` + pub fn build(self) -> Result { + let parts = self.parts?; + Uri::from_parts(parts).map_err(Into::into) + } + + // private + + fn map(self, func: F) -> Self + where + F: FnOnce(Parts) -> Result, + { + + Builder { + parts: self.parts.and_then(func), + } + } +} + +impl Default for Builder { + #[inline] + fn default() -> Builder { + Builder { + parts: Ok(Parts::default()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn build_from_str() { + let uri = Builder::new() + .scheme(Scheme::HTTP) + .authority("hyper.rs") + .path_and_query("/foo?a=1") + .build() + .unwrap(); + assert_eq!(uri.scheme_str(), Some("http")); + assert_eq!(uri.authority().unwrap().host(), "hyper.rs"); + assert_eq!(uri.path(), "/foo"); + assert_eq!(uri.query(), Some("a=1")); + } + + #[test] + fn build_from_string() { + for i in 1..10 { + let uri = Builder::new() + .path_and_query(format!("/foo?a={}", i)) + .build() + .unwrap(); + let expected_query = format!("a={}", i); + assert_eq!(uri.path(), "/foo"); + assert_eq!(uri.query(), Some(expected_query.as_str())); + } + } + + #[test] + fn build_from_string_ref() { + for i in 1..10 { + let p_a_q = format!("/foo?a={}", i); + let uri = Builder::new().path_and_query(&p_a_q).build().unwrap(); + let expected_query = format!("a={}", i); + assert_eq!(uri.path(), "/foo"); + assert_eq!(uri.query(), Some(expected_query.as_str())); + } + } +} diff --git a/third_party/rust/http/v1/crate/src/uri/mod.rs b/third_party/rust/http/v1/crate/src/uri/mod.rs new file mode 100644 index 000000000000..5ebd47b6f580 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/mod.rs @@ -0,0 +1,1118 @@ +//! URI component of request and response lines +//! +//! This module primarily contains the `Uri` type which is a component of all +//! HTTP requests and also reexports this type at the root of the crate. A URI +//! is not always a "full URL" in the sense of something you'd type into a web +//! browser, but HTTP requests may only have paths on servers but may have full +//! schemes and hostnames on clients. +//! +//! # Examples +//! +//! ``` +//! use http::Uri; +//! +//! let uri = "/foo/bar?baz".parse::().unwrap(); +//! assert_eq!(uri.path(), "/foo/bar"); +//! assert_eq!(uri.query(), Some("baz")); +//! assert_eq!(uri.host(), None); +//! +//! let uri = "https://www.rust-lang.org/install.html".parse::().unwrap(); +//! assert_eq!(uri.scheme_str(), Some("https")); +//! assert_eq!(uri.host(), Some("www.rust-lang.org")); +//! assert_eq!(uri.path(), "/install.html"); +//! ``` + +use crate::byte_str::ByteStr; +use std::convert::TryFrom; + +use bytes::Bytes; + +use std::error::Error; +use std::hash::{Hash, Hasher}; +use std::str::{self, FromStr}; +use std::{fmt, u16, u8}; + +use self::scheme::Scheme2; + +pub use self::authority::Authority; +pub use self::builder::Builder; +pub use self::path::PathAndQuery; +pub use self::port::Port; +pub use self::scheme::Scheme; + +mod authority; +mod builder; +mod path; +mod port; +mod scheme; +#[cfg(test)] +mod tests; + +/// The URI component of a request. +/// +/// For HTTP 1, this is included as part of the request line. From Section 5.3, +/// Request Target: +/// +/// > Once an inbound connection is obtained, the client sends an HTTP +/// > request message (Section 3) with a request-target derived from the +/// > target URI. There are four distinct formats for the request-target, +/// > depending on both the method being requested and whether the request +/// > is to a proxy. +/// > +/// > ```notrust +/// > request-target = origin-form +/// > / absolute-form +/// > / authority-form +/// > / asterisk-form +/// > ``` +/// +/// The URI is structured as follows: +/// +/// ```notrust +/// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 +/// |-| |-------------------------------||--------| |-------------------| |-----| +/// | | | | | +/// scheme authority path query fragment +/// ``` +/// +/// For HTTP 2.0, the URI is encoded using pseudoheaders. +/// +/// # Examples +/// +/// ``` +/// use http::Uri; +/// +/// let uri = "/foo/bar?baz".parse::().unwrap(); +/// assert_eq!(uri.path(), "/foo/bar"); +/// assert_eq!(uri.query(), Some("baz")); +/// assert_eq!(uri.host(), None); +/// +/// let uri = "https://www.rust-lang.org/install.html".parse::().unwrap(); +/// assert_eq!(uri.scheme_str(), Some("https")); +/// assert_eq!(uri.host(), Some("www.rust-lang.org")); +/// assert_eq!(uri.path(), "/install.html"); +/// ``` +#[derive(Clone)] +pub struct Uri { + scheme: Scheme, + authority: Authority, + path_and_query: PathAndQuery, +} + +/// The various parts of a URI. +/// +/// This struct is used to provide to and retrieve from a URI. +#[derive(Debug, Default)] +pub struct Parts { + /// The scheme component of a URI + pub scheme: Option, + + /// The authority component of a URI + pub authority: Option, + + /// The origin-form component of a URI + pub path_and_query: Option, + + /// Allow extending in the future + _priv: (), +} + +/// An error resulting from a failed attempt to construct a URI. +#[derive(Debug)] +pub struct InvalidUri(ErrorKind); + +/// An error resulting from a failed attempt to construct a URI. +#[derive(Debug)] +pub struct InvalidUriParts(InvalidUri); + +#[derive(Debug, Eq, PartialEq)] +enum ErrorKind { + InvalidUriChar, + InvalidScheme, + InvalidAuthority, + InvalidPort, + InvalidFormat, + SchemeMissing, + AuthorityMissing, + PathAndQueryMissing, + TooLong, + Empty, + SchemeTooLong, +} + +// u16::MAX is reserved for None +const MAX_LEN: usize = (u16::MAX - 1) as usize; + +// URI_CHARS is a table of valid characters in a URI. An entry in the table is +// 0 for invalid characters. For valid characters the entry is itself (i.e. +// the entry for 33 is b'!' because b'!' == 33u8). An important characteristic +// of this table is that all entries above 127 are invalid. This makes all of the +// valid entries a valid single-byte UTF-8 code point. This means that a slice +// of such valid entries is valid UTF-8. +const URI_CHARS: [u8; 256] = [ + // 0 1 2 3 4 5 6 7 8 9 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 0, 0, 0, b'!', 0, b'#', b'$', 0, b'&', b'\'', // 3x + b'(', b')', b'*', b'+', b',', b'-', b'.', b'/', b'0', b'1', // 4x + b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', b';', // 5x + 0, b'=', 0, b'?', b'@', b'A', b'B', b'C', b'D', b'E', // 6x + b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x + b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x + b'Z', b'[', 0, b']', 0, b'_', 0, b'a', b'b', b'c', // 9x + b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x + b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x + b'x', b'y', b'z', 0, 0, 0, b'~', 0, 0, 0, // 12x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x + 0, 0, 0, 0, 0, 0 // 25x +]; + +impl Uri { + /// Creates a new builder-style object to manufacture a `Uri`. + /// + /// This method returns an instance of `Builder` which can be usd to + /// create a `Uri`. + /// + /// # Examples + /// + /// ``` + /// use http::Uri; + /// + /// let uri = Uri::builder() + /// .scheme("https") + /// .authority("hyper.rs") + /// .path_and_query("/") + /// .build() + /// .unwrap(); + /// ``` + pub fn builder() -> Builder { + Builder::new() + } + + /// Attempt to convert a `Parts` into a `Uri`. + /// + /// # Examples + /// + /// Relative URI + /// + /// ``` + /// # use http::uri::*; + /// let mut parts = Parts::default(); + /// parts.path_and_query = Some("/foo".parse().unwrap()); + /// + /// let uri = Uri::from_parts(parts).unwrap(); + /// + /// assert_eq!(uri.path(), "/foo"); + /// + /// assert!(uri.scheme().is_none()); + /// assert!(uri.authority().is_none()); + /// ``` + /// + /// Absolute URI + /// + /// ``` + /// # use http::uri::*; + /// let mut parts = Parts::default(); + /// parts.scheme = Some("http".parse().unwrap()); + /// parts.authority = Some("foo.com".parse().unwrap()); + /// parts.path_and_query = Some("/foo".parse().unwrap()); + /// + /// let uri = Uri::from_parts(parts).unwrap(); + /// + /// assert_eq!(uri.scheme().unwrap().as_str(), "http"); + /// assert_eq!(uri.authority().unwrap(), "foo.com"); + /// assert_eq!(uri.path(), "/foo"); + /// ``` + pub fn from_parts(src: Parts) -> Result { + if src.scheme.is_some() { + if src.authority.is_none() { + return Err(ErrorKind::AuthorityMissing.into()); + } + + if src.path_and_query.is_none() { + return Err(ErrorKind::PathAndQueryMissing.into()); + } + } else { + if src.authority.is_some() && src.path_and_query.is_some() { + return Err(ErrorKind::SchemeMissing.into()); + } + } + + let scheme = match src.scheme { + Some(scheme) => scheme, + None => Scheme { + inner: Scheme2::None, + }, + }; + + let authority = match src.authority { + Some(authority) => authority, + None => Authority::empty(), + }; + + let path_and_query = match src.path_and_query { + Some(path_and_query) => path_and_query, + None => PathAndQuery::empty(), + }; + + Ok(Uri { + scheme: scheme, + authority: authority, + path_and_query: path_and_query, + }) + } + + /// Attempt to convert a `Bytes` buffer to a `Uri`. + /// + /// This will try to prevent a copy if the type passed is the type used + /// internally, and will copy the data if it is not. + pub fn from_maybe_shared(src: T) -> Result + where + T: AsRef<[u8]> + 'static, + { + if_downcast_into!(T, Bytes, src, { + return Uri::from_shared(src); + }); + + Uri::try_from(src.as_ref()) + } + + // Not public while `bytes` is unstable. + fn from_shared(s: Bytes) -> Result { + use self::ErrorKind::*; + + if s.len() > MAX_LEN { + return Err(TooLong.into()); + } + + match s.len() { + 0 => { + return Err(Empty.into()); + } + 1 => match s[0] { + b'/' => { + return Ok(Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::slash(), + }); + } + b'*' => { + return Ok(Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::star(), + }); + } + _ => { + let authority = Authority::from_shared(s)?; + + return Ok(Uri { + scheme: Scheme::empty(), + authority: authority, + path_and_query: PathAndQuery::empty(), + }); + } + }, + _ => {} + } + + if s[0] == b'/' { + return Ok(Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::from_shared(s)?, + }); + } + + parse_full(s) + } + + /// Convert a `Uri` from a static string. + /// + /// This function will not perform any copying, however the string is + /// checked to ensure that it is valid. + /// + /// # Panics + /// + /// This function panics if the argument is an invalid URI. + /// + /// # Examples + /// + /// ``` + /// # use http::uri::Uri; + /// let uri = Uri::from_static("http://example.com/foo"); + /// + /// assert_eq!(uri.host().unwrap(), "example.com"); + /// assert_eq!(uri.path(), "/foo"); + /// ``` + pub fn from_static(src: &'static str) -> Self { + let s = Bytes::from_static(src.as_bytes()); + match Uri::from_shared(s) { + Ok(uri) => uri, + Err(e) => panic!("static str is not valid URI: {}", e), + } + } + + /// Convert a `Uri` into `Parts`. + /// + /// # Note + /// + /// This is just an inherent method providing the same functionality as + /// `let parts: Parts = uri.into()` + /// + /// # Examples + /// + /// ``` + /// # use http::uri::*; + /// let uri: Uri = "/foo".parse().unwrap(); + /// + /// let parts = uri.into_parts(); + /// + /// assert_eq!(parts.path_and_query.unwrap(), "/foo"); + /// + /// assert!(parts.scheme.is_none()); + /// assert!(parts.authority.is_none()); + /// ``` + #[inline] + pub fn into_parts(self) -> Parts { + self.into() + } + + /// Returns the path & query components of the Uri + #[inline] + pub fn path_and_query(&self) -> Option<&PathAndQuery> { + if !self.scheme.inner.is_none() || self.authority.data.is_empty() { + Some(&self.path_and_query) + } else { + None + } + } + + /// Get the path of this `Uri`. + /// + /// Both relative and absolute URIs contain a path component, though it + /// might be the empty string. The path component is **case sensitive**. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |--------| + /// | + /// path + /// ``` + /// + /// If the URI is `*` then the path component is equal to `*`. + /// + /// # Examples + /// + /// A relative URI + /// + /// ``` + /// # use http::Uri; + /// + /// let uri: Uri = "/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.path(), "/hello/world"); + /// ``` + /// + /// An absolute URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.path(), "/hello/world"); + /// ``` + #[inline] + pub fn path(&self) -> &str { + if self.has_path() { + self.path_and_query.path() + } else { + "" + } + } + + /// Get the scheme of this `Uri`. + /// + /// The URI scheme refers to a specification for assigning identifiers + /// within that scheme. Only absolute URIs contain a scheme component, but + /// not all absolute URIs will contain a scheme component. Although scheme + /// names are case-insensitive, the canonical form is lowercase. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |-| + /// | + /// scheme + /// ``` + /// + /// # Examples + /// + /// Absolute URI + /// + /// ``` + /// use http::uri::{Scheme, Uri}; + /// + /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.scheme(), Some(&Scheme::HTTP)); + /// ``` + /// + /// + /// Relative URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "/hello/world".parse().unwrap(); + /// + /// assert!(uri.scheme().is_none()); + /// ``` + #[inline] + pub fn scheme(&self) -> Option<&Scheme> { + if self.scheme.inner.is_none() { + None + } else { + Some(&self.scheme) + } + } + + /// Get the scheme of this `Uri` as a `&str`. + /// + /// # Example + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.scheme_str(), Some("http")); + /// ``` + #[inline] + pub fn scheme_str(&self) -> Option<&str> { + if self.scheme.inner.is_none() { + None + } else { + Some(self.scheme.as_str()) + } + } + + /// Get the authority of this `Uri`. + /// + /// The authority is a hierarchical element for naming authority such that + /// the remainder of the URI is delegated to that authority. For HTTP, the + /// authority consists of the host and port. The host portion of the + /// authority is **case-insensitive**. + /// + /// The authority also includes a `username:password` component, however + /// the use of this is deprecated and should be avoided. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |-------------------------------| + /// | + /// authority + /// ``` + /// + /// # Examples + /// + /// Absolute URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.authority().map(|a| a.as_str()), Some("example.org:80")); + /// ``` + /// + /// + /// Relative URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "/hello/world".parse().unwrap(); + /// + /// assert!(uri.authority().is_none()); + /// ``` + #[inline] + pub fn authority(&self) -> Option<&Authority> { + if self.authority.data.is_empty() { + None + } else { + Some(&self.authority) + } + } + + /// Get the host of this `Uri`. + /// + /// The host subcomponent of authority is identified by an IP literal + /// encapsulated within square brackets, an IPv4 address in dotted- decimal + /// form, or a registered name. The host subcomponent is **case-insensitive**. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |---------| + /// | + /// host + /// ``` + /// + /// # Examples + /// + /// Absolute URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.host(), Some("example.org")); + /// ``` + /// + /// + /// Relative URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "/hello/world".parse().unwrap(); + /// + /// assert!(uri.host().is_none()); + /// ``` + #[inline] + pub fn host(&self) -> Option<&str> { + self.authority().map(|a| a.host()) + } + + /// Get the port part of this `Uri`. + /// + /// The port subcomponent of authority is designated by an optional port + /// number following the host and delimited from it by a single colon (":") + /// character. It can be turned into a decimal port number with the `as_u16` + /// method or as a `str` with the `as_str` method. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |-| + /// | + /// port + /// ``` + /// + /// # Examples + /// + /// Absolute URI with port + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); + /// + /// let port = uri.port().unwrap(); + /// assert_eq!(port.as_u16(), 80); + /// ``` + /// + /// Absolute URI without port + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); + /// + /// assert!(uri.port().is_none()); + /// ``` + /// + /// Relative URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "/hello/world".parse().unwrap(); + /// + /// assert!(uri.port().is_none()); + /// ``` + pub fn port(&self) -> Option> { + self.authority().and_then(|a| a.port()) + } + + /// Get the port of this `Uri` as a `u16`. + /// + /// + /// # Example + /// + /// ``` + /// # use http::{Uri, uri::Port}; + /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); + /// + /// assert_eq!(uri.port_u16(), Some(80)); + /// ``` + pub fn port_u16(&self) -> Option { + self.port().and_then(|p| Some(p.as_u16())) + } + + /// Get the query string of this `Uri`, starting after the `?`. + /// + /// The query component contains non-hierarchical data that, along with data + /// in the path component, serves to identify a resource within the scope of + /// the URI's scheme and naming authority (if any). The query component is + /// indicated by the first question mark ("?") character and terminated by a + /// number sign ("#") character or by the end of the URI. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |-------------------| + /// | + /// query + /// ``` + /// + /// # Examples + /// + /// Absolute URI + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "http://example.org/hello/world?key=value".parse().unwrap(); + /// + /// assert_eq!(uri.query(), Some("key=value")); + /// ``` + /// + /// Relative URI with a query string component + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "/hello/world?key=value&foo=bar".parse().unwrap(); + /// + /// assert_eq!(uri.query(), Some("key=value&foo=bar")); + /// ``` + /// + /// Relative URI without a query string component + /// + /// ``` + /// # use http::Uri; + /// let uri: Uri = "/hello/world".parse().unwrap(); + /// + /// assert!(uri.query().is_none()); + /// ``` + #[inline] + pub fn query(&self) -> Option<&str> { + self.path_and_query.query() + } + + fn has_path(&self) -> bool { + !self.path_and_query.data.is_empty() || !self.scheme.inner.is_none() + } +} + +impl<'a> TryFrom<&'a [u8]> for Uri { + type Error = InvalidUri; + + #[inline] + fn try_from(t: &'a [u8]) -> Result { + Uri::from_shared(Bytes::copy_from_slice(t)) + } +} + +impl<'a> TryFrom<&'a str> for Uri { + type Error = InvalidUri; + + #[inline] + fn try_from(t: &'a str) -> Result { + t.parse() + } +} + +impl<'a> TryFrom<&'a String> for Uri { + type Error = InvalidUri; + + #[inline] + fn try_from(t: &'a String) -> Result { + t.parse() + } +} + +impl TryFrom for Uri { + type Error = InvalidUri; + + #[inline] + fn try_from(t: String) -> Result { + Uri::from_shared(Bytes::from(t)) + } +} + +impl<'a> TryFrom> for Uri { + type Error = InvalidUri; + + #[inline] + fn try_from(vec: Vec) -> Result { + Uri::from_shared(Bytes::from(vec)) + } +} + +impl TryFrom for Uri { + type Error = InvalidUriParts; + + #[inline] + fn try_from(src: Parts) -> Result { + Uri::from_parts(src) + } +} + +impl<'a> TryFrom<&'a Uri> for Uri { + type Error = crate::Error; + + #[inline] + fn try_from(src: &'a Uri) -> Result { + Ok(src.clone()) + } +} + +/// Convert an `Authority` into a `Uri`. +impl From for Uri { + fn from(authority: Authority) -> Self { + Self { + scheme: Scheme::empty(), + authority, + path_and_query: PathAndQuery::empty(), + } + } +} + +/// Convert a `PathAndQuery` into a `Uri`. +impl From for Uri { + fn from(path_and_query: PathAndQuery) -> Self { + Self { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query, + } + } +} + +/// Convert a `Uri` into `Parts` +impl From for Parts { + fn from(src: Uri) -> Self { + let path_and_query = if src.has_path() { + Some(src.path_and_query) + } else { + None + }; + + let scheme = match src.scheme.inner { + Scheme2::None => None, + _ => Some(src.scheme), + }; + + let authority = if src.authority.data.is_empty() { + None + } else { + Some(src.authority) + }; + + Parts { + scheme: scheme, + authority: authority, + path_and_query: path_and_query, + _priv: (), + } + } +} + +fn parse_full(mut s: Bytes) -> Result { + // Parse the scheme + let scheme = match Scheme2::parse(&s[..])? { + Scheme2::None => Scheme2::None, + Scheme2::Standard(p) => { + // TODO: use truncate + let _ = s.split_to(p.len() + 3); + Scheme2::Standard(p) + } + Scheme2::Other(n) => { + // Grab the protocol + let mut scheme = s.split_to(n + 3); + + // Strip ://, TODO: truncate + let _ = scheme.split_off(n); + + // Allocate the ByteStr + let val = unsafe { ByteStr::from_utf8_unchecked(scheme) }; + + Scheme2::Other(Box::new(val)) + } + }; + + // Find the end of the authority. The scheme will already have been + // extracted. + let authority_end = Authority::parse(&s[..])?; + + if scheme.is_none() { + if authority_end != s.len() { + return Err(ErrorKind::InvalidFormat.into()); + } + + let authority = Authority { + data: unsafe { ByteStr::from_utf8_unchecked(s) }, + }; + + return Ok(Uri { + scheme: scheme.into(), + authority: authority, + path_and_query: PathAndQuery::empty(), + }); + } + + // Authority is required when absolute + if authority_end == 0 { + return Err(ErrorKind::InvalidFormat.into()); + } + + let authority = s.split_to(authority_end); + let authority = Authority { + data: unsafe { ByteStr::from_utf8_unchecked(authority) }, + }; + + Ok(Uri { + scheme: scheme.into(), + authority: authority, + path_and_query: PathAndQuery::from_shared(s)?, + }) +} + +impl FromStr for Uri { + type Err = InvalidUri; + + #[inline] + fn from_str(s: &str) -> Result { + Uri::try_from(s.as_bytes()) + } +} + +impl PartialEq for Uri { + fn eq(&self, other: &Uri) -> bool { + if self.scheme() != other.scheme() { + return false; + } + + if self.authority() != other.authority() { + return false; + } + + if self.path() != other.path() { + return false; + } + + if self.query() != other.query() { + return false; + } + + true + } +} + +impl PartialEq for Uri { + fn eq(&self, other: &str) -> bool { + let mut other = other.as_bytes(); + let mut absolute = false; + + if let Some(scheme) = self.scheme() { + let scheme = scheme.as_str().as_bytes(); + absolute = true; + + if other.len() < scheme.len() + 3 { + return false; + } + + if !scheme.eq_ignore_ascii_case(&other[..scheme.len()]) { + return false; + } + + other = &other[scheme.len()..]; + + if &other[..3] != b"://" { + return false; + } + + other = &other[3..]; + } + + if let Some(auth) = self.authority() { + let len = auth.data.len(); + absolute = true; + + if other.len() < len { + return false; + } + + if !auth.data.as_bytes().eq_ignore_ascii_case(&other[..len]) { + return false; + } + + other = &other[len..]; + } + + let path = self.path(); + + if other.len() < path.len() || path.as_bytes() != &other[..path.len()] { + if absolute && path == "/" { + // PathAndQuery can be omitted, fall through + } else { + return false; + } + } else { + other = &other[path.len()..]; + } + + if let Some(query) = self.query() { + if other.len() == 0 { + return query.len() == 0; + } + + if other[0] != b'?' { + return false; + } + + other = &other[1..]; + + if other.len() < query.len() { + return false; + } + + if query.as_bytes() != &other[..query.len()] { + return false; + } + + other = &other[query.len()..]; + } + + other.is_empty() || other[0] == b'#' + } +} + +impl PartialEq for str { + fn eq(&self, uri: &Uri) -> bool { + uri == self + } +} + +impl<'a> PartialEq<&'a str> for Uri { + fn eq(&self, other: &&'a str) -> bool { + self == *other + } +} + +impl<'a> PartialEq for &'a str { + fn eq(&self, uri: &Uri) -> bool { + uri == *self + } +} + +impl Eq for Uri {} + +/// Returns a `Uri` representing `/` +impl Default for Uri { + #[inline] + fn default() -> Uri { + Uri { + scheme: Scheme::empty(), + authority: Authority::empty(), + path_and_query: PathAndQuery::slash(), + } + } +} + +impl fmt::Display for Uri { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(scheme) = self.scheme() { + write!(f, "{}://", scheme)?; + } + + if let Some(authority) = self.authority() { + write!(f, "{}", authority)?; + } + + write!(f, "{}", self.path())?; + + if let Some(query) = self.query() { + write!(f, "?{}", query)?; + } + + Ok(()) + } +} + +impl fmt::Debug for Uri { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl From for InvalidUri { + fn from(src: ErrorKind) -> InvalidUri { + InvalidUri(src) + } +} + +impl From for InvalidUriParts { + fn from(src: ErrorKind) -> InvalidUriParts { + InvalidUriParts(src.into()) + } +} + +impl InvalidUri { + fn s(&self) -> &str { + match self.0 { + ErrorKind::InvalidUriChar => "invalid uri character", + ErrorKind::InvalidScheme => "invalid scheme", + ErrorKind::InvalidAuthority => "invalid authority", + ErrorKind::InvalidPort => "invalid port", + ErrorKind::InvalidFormat => "invalid format", + ErrorKind::SchemeMissing => "scheme missing", + ErrorKind::AuthorityMissing => "authority missing", + ErrorKind::PathAndQueryMissing => "path missing", + ErrorKind::TooLong => "uri too long", + ErrorKind::Empty => "empty string", + ErrorKind::SchemeTooLong => "scheme too long", + } + } +} + +impl fmt::Display for InvalidUri { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.s().fmt(f) + } +} + +impl Error for InvalidUri {} + +impl fmt::Display for InvalidUriParts { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl Error for InvalidUriParts {} + +impl Hash for Uri { + fn hash(&self, state: &mut H) + where + H: Hasher, + { + if !self.scheme.inner.is_none() { + self.scheme.hash(state); + state.write_u8(0xff); + } + + if let Some(auth) = self.authority() { + auth.hash(state); + } + + Hash::hash_slice(self.path().as_bytes(), state); + + if let Some(query) = self.query() { + b'?'.hash(state); + Hash::hash_slice(query.as_bytes(), state); + } + } +} diff --git a/third_party/rust/http/v1/crate/src/uri/path.rs b/third_party/rust/http/v1/crate/src/uri/path.rs new file mode 100644 index 000000000000..be2cb65c1b64 --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/path.rs @@ -0,0 +1,564 @@ +use std::convert::TryFrom; +use std::str::FromStr; +use std::{cmp, fmt, hash, str}; + +use bytes::Bytes; + +use super::{ErrorKind, InvalidUri}; +use crate::byte_str::ByteStr; + +/// Represents the path component of a URI +#[derive(Clone)] +pub struct PathAndQuery { + pub(super) data: ByteStr, + pub(super) query: u16, +} + +const NONE: u16 = ::std::u16::MAX; + +impl PathAndQuery { + // Not public while `bytes` is unstable. + pub(super) fn from_shared(mut src: Bytes) -> Result { + let mut query = NONE; + let mut fragment = None; + + // block for iterator borrow + { + let mut iter = src.as_ref().iter().enumerate(); + + // path ... + for (i, &b) in &mut iter { + // See https://url.spec.whatwg.org/#path-state + match b { + b'?' => { + debug_assert_eq!(query, NONE); + query = i as u16; + break; + } + b'#' => { + fragment = Some(i); + break; + } + + // This is the range of bytes that don't need to be + // percent-encoded in the path. If it should have been + // percent-encoded, then error. + 0x21 | + 0x24..=0x3B | + 0x3D | + 0x40..=0x5F | + 0x61..=0x7A | + 0x7C | + 0x7E => {}, + + // These are code points that are supposed to be + // percent-encoded in the path but there are clients + // out there sending them as is and httparse accepts + // to parse those requests, so they are allowed here + // for parity. + // + // For reference, those are code points that are used + // to send requests with JSON directly embedded in + // the URI path. Yes, those things happen for real. + b'"' | + b'{' | b'}' => {}, + + _ => return Err(ErrorKind::InvalidUriChar.into()), + } + } + + // query ... + if query != NONE { + for (i, &b) in iter { + match b { + // While queries *should* be percent-encoded, most + // bytes are actually allowed... + // See https://url.spec.whatwg.org/#query-state + // + // Allowed: 0x21 / 0x24 - 0x3B / 0x3D / 0x3F - 0x7E + 0x21 | + 0x24..=0x3B | + 0x3D | + 0x3F..=0x7E => {}, + + b'#' => { + fragment = Some(i); + break; + } + + _ => return Err(ErrorKind::InvalidUriChar.into()), + } + } + } + } + + if let Some(i) = fragment { + src.truncate(i); + } + + Ok(PathAndQuery { + data: unsafe { ByteStr::from_utf8_unchecked(src) }, + query: query, + }) + } + + /// Convert a `PathAndQuery` from a static string. + /// + /// This function will not perform any copying, however the string is + /// checked to ensure that it is valid. + /// + /// # Panics + /// + /// This function panics if the argument is an invalid path and query. + /// + /// # Examples + /// + /// ``` + /// # use http::uri::*; + /// let v = PathAndQuery::from_static("/hello?world"); + /// + /// assert_eq!(v.path(), "/hello"); + /// assert_eq!(v.query(), Some("world")); + /// ``` + #[inline] + pub fn from_static(src: &'static str) -> Self { + let src = Bytes::from_static(src.as_bytes()); + + PathAndQuery::from_shared(src).unwrap() + } + + /// Attempt to convert a `Bytes` buffer to a `PathAndQuery`. + /// + /// This will try to prevent a copy if the type passed is the type used + /// internally, and will copy the data if it is not. + pub fn from_maybe_shared(src: T) -> Result + where + T: AsRef<[u8]> + 'static, + { + if_downcast_into!(T, Bytes, src, { + return PathAndQuery::from_shared(src); + }); + + PathAndQuery::try_from(src.as_ref()) + } + + pub(super) fn empty() -> Self { + PathAndQuery { + data: ByteStr::new(), + query: NONE, + } + } + + pub(super) fn slash() -> Self { + PathAndQuery { + data: ByteStr::from_static("/"), + query: NONE, + } + } + + pub(super) fn star() -> Self { + PathAndQuery { + data: ByteStr::from_static("*"), + query: NONE, + } + } + + /// Returns the path component + /// + /// The path component is **case sensitive**. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |--------| + /// | + /// path + /// ``` + /// + /// If the URI is `*` then the path component is equal to `*`. + /// + /// # Examples + /// + /// ``` + /// # use http::uri::*; + /// + /// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap(); + /// + /// assert_eq!(path_and_query.path(), "/hello/world"); + /// ``` + #[inline] + pub fn path(&self) -> &str { + let ret = if self.query == NONE { + &self.data[..] + } else { + &self.data[..self.query as usize] + }; + + if ret.is_empty() { + return "/"; + } + + ret + } + + /// Returns the query string component + /// + /// The query component contains non-hierarchical data that, along with data + /// in the path component, serves to identify a resource within the scope of + /// the URI's scheme and naming authority (if any). The query component is + /// indicated by the first question mark ("?") character and terminated by a + /// number sign ("#") character or by the end of the URI. + /// + /// ```notrust + /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 + /// |-------------------| + /// | + /// query + /// ``` + /// + /// # Examples + /// + /// With a query string component + /// + /// ``` + /// # use http::uri::*; + /// let path_and_query: PathAndQuery = "/hello/world?key=value&foo=bar".parse().unwrap(); + /// + /// assert_eq!(path_and_query.query(), Some("key=value&foo=bar")); + /// ``` + /// + /// Without a query string component + /// + /// ``` + /// # use http::uri::*; + /// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap(); + /// + /// assert!(path_and_query.query().is_none()); + /// ``` + #[inline] + pub fn query(&self) -> Option<&str> { + if self.query == NONE { + None + } else { + let i = self.query + 1; + Some(&self.data[i as usize..]) + } + } + + /// Returns the path and query as a string component. + /// + /// # Examples + /// + /// With a query string component + /// + /// ``` + /// # use http::uri::*; + /// let path_and_query: PathAndQuery = "/hello/world?key=value&foo=bar".parse().unwrap(); + /// + /// assert_eq!(path_and_query.as_str(), "/hello/world?key=value&foo=bar"); + /// ``` + /// + /// Without a query string component + /// + /// ``` + /// # use http::uri::*; + /// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap(); + /// + /// assert_eq!(path_and_query.as_str(), "/hello/world"); + /// ``` + #[inline] + pub fn as_str(&self) -> &str { + let ret = &self.data[..]; + if ret.is_empty() { + return "/"; + } + ret + } +} + +impl<'a> TryFrom<&'a [u8]> for PathAndQuery { + type Error = InvalidUri; + #[inline] + fn try_from(s: &'a [u8]) -> Result { + PathAndQuery::from_shared(Bytes::copy_from_slice(s)) + } +} + +impl<'a> TryFrom<&'a str> for PathAndQuery { + type Error = InvalidUri; + #[inline] + fn try_from(s: &'a str) -> Result { + TryFrom::try_from(s.as_bytes()) + } +} + +impl<'a> TryFrom> for PathAndQuery { + type Error = InvalidUri; + #[inline] + fn try_from(vec: Vec) -> Result { + PathAndQuery::from_shared(vec.into()) + } +} + +impl TryFrom for PathAndQuery { + type Error = InvalidUri; + #[inline] + fn try_from(s: String) -> Result { + PathAndQuery::from_shared(s.into()) + } +} + +impl TryFrom<&String> for PathAndQuery { + type Error = InvalidUri; + #[inline] + fn try_from(s: &String) -> Result { + TryFrom::try_from(s.as_bytes()) + } +} + +impl FromStr for PathAndQuery { + type Err = InvalidUri; + #[inline] + fn from_str(s: &str) -> Result { + TryFrom::try_from(s) + } +} + +impl fmt::Debug for PathAndQuery { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for PathAndQuery { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + if !self.data.is_empty() { + match self.data.as_bytes()[0] { + b'/' | b'*' => write!(fmt, "{}", &self.data[..]), + _ => write!(fmt, "/{}", &self.data[..]), + } + } else { + write!(fmt, "/") + } + } +} + +impl hash::Hash for PathAndQuery { + fn hash(&self, state: &mut H) { + self.data.hash(state); + } +} + +// ===== PartialEq / PartialOrd ===== + +impl PartialEq for PathAndQuery { + #[inline] + fn eq(&self, other: &PathAndQuery) -> bool { + self.data == other.data + } +} + +impl Eq for PathAndQuery {} + +impl PartialEq for PathAndQuery { + #[inline] + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} + +impl<'a> PartialEq for &'a str { + #[inline] + fn eq(&self, other: &PathAndQuery) -> bool { + self == &other.as_str() + } +} + +impl<'a> PartialEq<&'a str> for PathAndQuery { + #[inline] + fn eq(&self, other: &&'a str) -> bool { + self.as_str() == *other + } +} + +impl PartialEq for str { + #[inline] + fn eq(&self, other: &PathAndQuery) -> bool { + self == other.as_str() + } +} + +impl PartialEq for PathAndQuery { + #[inline] + fn eq(&self, other: &String) -> bool { + self.as_str() == other.as_str() + } +} + +impl PartialEq for String { + #[inline] + fn eq(&self, other: &PathAndQuery) -> bool { + self.as_str() == other.as_str() + } +} + +impl PartialOrd for PathAndQuery { + #[inline] + fn partial_cmp(&self, other: &PathAndQuery) -> Option { + self.as_str().partial_cmp(other.as_str()) + } +} + +impl PartialOrd for PathAndQuery { + #[inline] + fn partial_cmp(&self, other: &str) -> Option { + self.as_str().partial_cmp(other) + } +} + +impl PartialOrd for str { + #[inline] + fn partial_cmp(&self, other: &PathAndQuery) -> Option { + self.partial_cmp(other.as_str()) + } +} + +impl<'a> PartialOrd<&'a str> for PathAndQuery { + #[inline] + fn partial_cmp(&self, other: &&'a str) -> Option { + self.as_str().partial_cmp(*other) + } +} + +impl<'a> PartialOrd for &'a str { + #[inline] + fn partial_cmp(&self, other: &PathAndQuery) -> Option { + self.partial_cmp(&other.as_str()) + } +} + +impl PartialOrd for PathAndQuery { + #[inline] + fn partial_cmp(&self, other: &String) -> Option { + self.as_str().partial_cmp(other.as_str()) + } +} + +impl PartialOrd for String { + #[inline] + fn partial_cmp(&self, other: &PathAndQuery) -> Option { + self.as_str().partial_cmp(other.as_str()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn equal_to_self_of_same_path() { + let p1: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + let p2: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + assert_eq!(p1, p2); + assert_eq!(p2, p1); + } + + #[test] + fn not_equal_to_self_of_different_path() { + let p1: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + let p2: PathAndQuery = "/world&foo=bar".parse().unwrap(); + assert_ne!(p1, p2); + assert_ne!(p2, p1); + } + + #[test] + fn equates_with_a_str() { + let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + assert_eq!(&path_and_query, "/hello/world&foo=bar"); + assert_eq!("/hello/world&foo=bar", &path_and_query); + assert_eq!(path_and_query, "/hello/world&foo=bar"); + assert_eq!("/hello/world&foo=bar", path_and_query); + } + + #[test] + fn not_equal_with_a_str_of_a_different_path() { + let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + // as a reference + assert_ne!(&path_and_query, "/hello&foo=bar"); + assert_ne!("/hello&foo=bar", &path_and_query); + // without reference + assert_ne!(path_and_query, "/hello&foo=bar"); + assert_ne!("/hello&foo=bar", path_and_query); + } + + #[test] + fn equates_with_a_string() { + let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + assert_eq!(path_and_query, "/hello/world&foo=bar".to_string()); + assert_eq!("/hello/world&foo=bar".to_string(), path_and_query); + } + + #[test] + fn not_equal_with_a_string_of_a_different_path() { + let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); + assert_ne!(path_and_query, "/hello&foo=bar".to_string()); + assert_ne!("/hello&foo=bar".to_string(), path_and_query); + } + + #[test] + fn compares_to_self() { + let p1: PathAndQuery = "/a/world&foo=bar".parse().unwrap(); + let p2: PathAndQuery = "/b/world&foo=bar".parse().unwrap(); + assert!(p1 < p2); + assert!(p2 > p1); + } + + #[test] + fn compares_with_a_str() { + let path_and_query: PathAndQuery = "/b/world&foo=bar".parse().unwrap(); + // by ref + assert!(&path_and_query < "/c/world&foo=bar"); + assert!("/c/world&foo=bar" > &path_and_query); + assert!(&path_and_query > "/a/world&foo=bar"); + assert!("/a/world&foo=bar" < &path_and_query); + + // by val + assert!(path_and_query < "/c/world&foo=bar"); + assert!("/c/world&foo=bar" > path_and_query); + assert!(path_and_query > "/a/world&foo=bar"); + assert!("/a/world&foo=bar" < path_and_query); + } + + #[test] + fn compares_with_a_string() { + let path_and_query: PathAndQuery = "/b/world&foo=bar".parse().unwrap(); + assert!(path_and_query < "/c/world&foo=bar".to_string()); + assert!("/c/world&foo=bar".to_string() > path_and_query); + assert!(path_and_query > "/a/world&foo=bar".to_string()); + assert!("/a/world&foo=bar".to_string() < path_and_query); + } + + #[test] + fn ignores_valid_percent_encodings() { + assert_eq!("/a%20b", pq("/a%20b?r=1").path()); + assert_eq!("qr=%31", pq("/a/b?qr=%31").query().unwrap()); + } + + #[test] + fn ignores_invalid_percent_encodings() { + assert_eq!("/a%%b", pq("/a%%b?r=1").path()); + assert_eq!("/aaa%", pq("/aaa%").path()); + assert_eq!("/aaa%", pq("/aaa%?r=1").path()); + assert_eq!("/aa%2", pq("/aa%2").path()); + assert_eq!("/aa%2", pq("/aa%2?r=1").path()); + assert_eq!("qr=%3", pq("/a/b?qr=%3").query().unwrap()); + } + + #[test] + fn json_is_fine() { + assert_eq!(r#"/{"bread":"baguette"}"#, pq(r#"/{"bread":"baguette"}"#).path()); + } + + fn pq(s: &str) -> PathAndQuery { + s.parse().expect(&format!("parsing {}", s)) + } +} diff --git a/third_party/rust/http/v1/crate/src/uri/port.rs b/third_party/rust/http/v1/crate/src/uri/port.rs new file mode 100644 index 000000000000..8f5c5f3f7d0b --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/port.rs @@ -0,0 +1,151 @@ +use std::fmt; + +use super::{ErrorKind, InvalidUri}; + +/// The port component of a URI. +pub struct Port { + port: u16, + repr: T, +} + +impl Port { + /// Returns the port number as a `u16`. + /// + /// # Examples + /// + /// Port as `u16`. + /// + /// ``` + /// # use http::uri::Authority; + /// let authority: Authority = "example.org:80".parse().unwrap(); + /// + /// let port = authority.port().unwrap(); + /// assert_eq!(port.as_u16(), 80); + /// ``` + pub fn as_u16(&self) -> u16 { + self.port + } +} + +impl Port +where + T: AsRef, +{ + /// Converts a `str` to a port number. + /// + /// The supplied `str` must be a valid u16. + pub(crate) fn from_str(bytes: T) -> Result { + bytes + .as_ref() + .parse::() + .map(|port| Port { port, repr: bytes }) + .map_err(|_| ErrorKind::InvalidPort.into()) + } + + /// Returns the port number as a `str`. + /// + /// # Examples + /// + /// Port as `str`. + /// + /// ``` + /// # use http::uri::Authority; + /// let authority: Authority = "example.org:80".parse().unwrap(); + /// + /// let port = authority.port().unwrap(); + /// assert_eq!(port.as_str(), "80"); + /// ``` + pub fn as_str(&self) -> &str { + self.repr.as_ref() + } +} + +impl fmt::Debug for Port +where + T: fmt::Debug, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Port").field(&self.port).finish() + } +} + +impl fmt::Display for Port { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Use `u16::fmt` so that it respects any formatting flags that + // may have been set (like padding, align, etc). + fmt::Display::fmt(&self.port, f) + } +} + +impl From> for u16 { + fn from(port: Port) -> Self { + port.as_u16() + } +} + +impl AsRef for Port +where + T: AsRef, +{ + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl PartialEq> for Port { + fn eq(&self, other: &Port) -> bool { + self.port == other.port + } +} + +impl PartialEq for Port { + fn eq(&self, other: &u16) -> bool { + self.port == *other + } +} + +impl PartialEq> for u16 { + fn eq(&self, other: &Port) -> bool { + other.port == *self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn partialeq_port() { + let port_a = Port::from_str("8080").unwrap(); + let port_b = Port::from_str("8080").unwrap(); + assert_eq!(port_a, port_b); + } + + #[test] + fn partialeq_port_different_reprs() { + let port_a = Port { + repr: "8081", + port: 8081, + }; + let port_b = Port { + repr: String::from("8081"), + port: 8081, + }; + assert_eq!(port_a, port_b); + assert_eq!(port_b, port_a); + } + + #[test] + fn partialeq_u16() { + let port = Port::from_str("8080").unwrap(); + // test equals in both directions + assert_eq!(port, 8080); + assert_eq!(8080, port); + } + + #[test] + fn u16_from_port() { + let port = Port::from_str("8080").unwrap(); + assert_eq!(8080, u16::from(port)); + } +} diff --git a/third_party/rust/http/v1/crate/src/uri/scheme.rs b/third_party/rust/http/v1/crate/src/uri/scheme.rs new file mode 100644 index 000000000000..682b11eeeacd --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/scheme.rs @@ -0,0 +1,363 @@ +use std::convert::TryFrom; +use std::fmt; +use std::hash::{Hash, Hasher}; +use std::str::FromStr; + +use bytes::Bytes; + +use super::{ErrorKind, InvalidUri}; +use crate::byte_str::ByteStr; + +/// Represents the scheme component of a URI +#[derive(Clone)] +pub struct Scheme { + pub(super) inner: Scheme2, +} + +#[derive(Clone, Debug)] +pub(super) enum Scheme2> { + None, + Standard(Protocol), + Other(T), +} + +#[derive(Copy, Clone, Debug)] +pub(super) enum Protocol { + Http, + Https, +} + +impl Scheme { + /// HTTP protocol scheme + pub const HTTP: Scheme = Scheme { + inner: Scheme2::Standard(Protocol::Http), + }; + + /// HTTP protocol over TLS. + pub const HTTPS: Scheme = Scheme { + inner: Scheme2::Standard(Protocol::Https), + }; + + pub(super) fn empty() -> Self { + Scheme { + inner: Scheme2::None, + } + } + + /// Return a str representation of the scheme + /// + /// # Examples + /// + /// ``` + /// # use http::uri::*; + /// let scheme: Scheme = "http".parse().unwrap(); + /// assert_eq!(scheme.as_str(), "http"); + /// ``` + #[inline] + pub fn as_str(&self) -> &str { + use self::Protocol::*; + use self::Scheme2::*; + + match self.inner { + Standard(Http) => "http", + Standard(Https) => "https", + Other(ref v) => &v[..], + None => unreachable!(), + } + } +} + +impl<'a> TryFrom<&'a [u8]> for Scheme { + type Error = InvalidUri; + #[inline] + fn try_from(s: &'a [u8]) -> Result { + use self::Scheme2::*; + + match Scheme2::parse_exact(s)? { + None => Err(ErrorKind::InvalidScheme.into()), + Standard(p) => Ok(Standard(p).into()), + Other(_) => { + let bytes = Bytes::copy_from_slice(s); + + // Safety: postcondition on parse_exact() means that s and + // hence bytes are valid UTF-8. + let string = unsafe { ByteStr::from_utf8_unchecked(bytes) }; + + Ok(Other(Box::new(string)).into()) + } + } + } +} + +impl<'a> TryFrom<&'a str> for Scheme { + type Error = InvalidUri; + #[inline] + fn try_from(s: &'a str) -> Result { + TryFrom::try_from(s.as_bytes()) + } +} + +impl FromStr for Scheme { + type Err = InvalidUri; + + fn from_str(s: &str) -> Result { + TryFrom::try_from(s) + } +} + +impl fmt::Debug for Scheme { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self.as_str(), f) + } +} + +impl fmt::Display for Scheme { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) + } +} + +impl AsRef for Scheme { + #[inline] + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl PartialEq for Scheme { + fn eq(&self, other: &Scheme) -> bool { + use self::Protocol::*; + use self::Scheme2::*; + + match (&self.inner, &other.inner) { + (&Standard(Http), &Standard(Http)) => true, + (&Standard(Https), &Standard(Https)) => true, + (&Other(ref a), &Other(ref b)) => a.eq_ignore_ascii_case(b), + (&None, _) | (_, &None) => unreachable!(), + _ => false, + } + } +} + +impl Eq for Scheme {} + +/// Case-insensitive equality +/// +/// # Examples +/// +/// ``` +/// # use http::uri::Scheme; +/// let scheme: Scheme = "HTTP".parse().unwrap(); +/// assert_eq!(scheme, *"http"); +/// ``` +impl PartialEq for Scheme { + fn eq(&self, other: &str) -> bool { + self.as_str().eq_ignore_ascii_case(other) + } +} + +/// Case-insensitive equality +impl PartialEq for str { + fn eq(&self, other: &Scheme) -> bool { + other == self + } +} + +/// Case-insensitive hashing +impl Hash for Scheme { + fn hash(&self, state: &mut H) + where + H: Hasher, + { + match self.inner { + Scheme2::None => (), + Scheme2::Standard(Protocol::Http) => state.write_u8(1), + Scheme2::Standard(Protocol::Https) => state.write_u8(2), + Scheme2::Other(ref other) => { + other.len().hash(state); + for &b in other.as_bytes() { + state.write_u8(b.to_ascii_lowercase()); + } + } + } + } +} + +impl Scheme2 { + pub(super) fn is_none(&self) -> bool { + match *self { + Scheme2::None => true, + _ => false, + } + } +} + +// Require the scheme to not be too long in order to enable further +// optimizations later. +const MAX_SCHEME_LEN: usize = 64; + +// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) +// +// SCHEME_CHARS is a table of valid characters in the scheme part of a URI. An +// entry in the table is 0 for invalid characters. For valid characters the +// entry is itself (i.e. the entry for 43 is b'+' because b'+' == 43u8). An +// important characteristic of this table is that all entries above 127 are +// invalid. This makes all of the valid entries a valid single-byte UTF-8 code +// point. This means that a slice of such valid entries is valid UTF-8. +const SCHEME_CHARS: [u8; 256] = [ + // 0 1 2 3 4 5 6 7 8 9 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3x + 0, 0, 0, b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x + b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', 0, // 5x + 0, 0, 0, 0, 0, b'A', b'B', b'C', b'D', b'E', // 6x + b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x + b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x + b'Z', 0, 0, 0, 0, 0, 0, b'a', b'b', b'c', // 9x + b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x + b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x + b'x', b'y', b'z', 0, 0, 0, b'~', 0, 0, 0, // 12x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x + 0, 0, 0, 0, 0, 0 // 25x +]; + +impl Scheme2 { + // Postcondition: On all Ok() returns, s is valid UTF-8 + fn parse_exact(s: &[u8]) -> Result, InvalidUri> { + match s { + b"http" => Ok(Protocol::Http.into()), + b"https" => Ok(Protocol::Https.into()), + _ => { + if s.len() > MAX_SCHEME_LEN { + return Err(ErrorKind::SchemeTooLong.into()); + } + + // check that each byte in s is a SCHEME_CHARS which implies + // that it is a valid single byte UTF-8 code point. + for &b in s { + match SCHEME_CHARS[b as usize] { + b':' => { + // Don't want :// here + return Err(ErrorKind::InvalidScheme.into()); + } + 0 => { + return Err(ErrorKind::InvalidScheme.into()); + } + _ => {} + } + } + + Ok(Scheme2::Other(())) + } + } + } + + pub(super) fn parse(s: &[u8]) -> Result, InvalidUri> { + if s.len() >= 7 { + // Check for HTTP + if s[..7].eq_ignore_ascii_case(b"http://") { + // Prefix will be striped + return Ok(Protocol::Http.into()); + } + } + + if s.len() >= 8 { + // Check for HTTPs + if s[..8].eq_ignore_ascii_case(b"https://") { + return Ok(Protocol::Https.into()); + } + } + + if s.len() > 3 { + for i in 0..s.len() { + let b = s[i]; + + match SCHEME_CHARS[b as usize] { + b':' => { + // Not enough data remaining + if s.len() < i + 3 { + break; + } + + // Not a scheme + if &s[i + 1..i + 3] != b"//" { + break; + } + + if i > MAX_SCHEME_LEN { + return Err(ErrorKind::SchemeTooLong.into()); + } + + // Return scheme + return Ok(Scheme2::Other(i)); + } + // Invald scheme character, abort + 0 => break, + _ => {} + } + } + } + + Ok(Scheme2::None) + } +} + +impl Protocol { + pub(super) fn len(&self) -> usize { + match *self { + Protocol::Http => 4, + Protocol::Https => 5, + } + } +} + +impl From for Scheme2 { + fn from(src: Protocol) -> Self { + Scheme2::Standard(src) + } +} + +#[doc(hidden)] +impl From for Scheme { + fn from(src: Scheme2) -> Self { + Scheme { inner: src } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn scheme_eq_to_str() { + assert_eq!(&scheme("http"), "http"); + assert_eq!(&scheme("https"), "https"); + assert_eq!(&scheme("ftp"), "ftp"); + assert_eq!(&scheme("my+funky+scheme"), "my+funky+scheme"); + } + + #[test] + fn invalid_scheme_is_error() { + Scheme::try_from("my_funky_scheme").expect_err("Unexpectly valid Scheme"); + + // Invalid UTF-8 + Scheme::try_from([0xC0].as_ref()).expect_err("Unexpectly valid Scheme"); + } + + fn scheme(s: &str) -> Scheme { + s.parse().expect(&format!("Invalid scheme: {}", s)) + } +} diff --git a/third_party/rust/http/v1/crate/src/uri/tests.rs b/third_party/rust/http/v1/crate/src/uri/tests.rs new file mode 100644 index 000000000000..719cb94ee35f --- /dev/null +++ b/third_party/rust/http/v1/crate/src/uri/tests.rs @@ -0,0 +1,519 @@ +use std::str::FromStr; + +use super::{ErrorKind, InvalidUri, Port, Uri, URI_CHARS}; + +#[test] +fn test_char_table() { + for (i, &v) in URI_CHARS.iter().enumerate() { + if v != 0 { + assert_eq!(i, v as usize); + } + } +} + +macro_rules! part { + ($s:expr) => { + Some(&$s.parse().unwrap()) + }; +} + +macro_rules! test_parse { + ( + $test_name:ident, + $str:expr, + $alt:expr, + $($method:ident = $value:expr,)* + ) => ( + #[test] + fn $test_name() { + let orig_str = $str; + let uri = match Uri::from_str(orig_str) { + Ok(uri) => uri, + Err(err) => { + panic!("parse error {:?} from {:?}", err, orig_str); + }, + }; + $( + assert_eq!(uri.$method(), $value, "{}: uri = {:?}", stringify!($method), uri); + )+ + assert_eq!(uri, orig_str, "partial eq to original str"); + assert_eq!(uri, uri.clone(), "clones are equal"); + + let new_str = uri.to_string(); + let new_uri = Uri::from_str(&new_str).expect("to_string output parses again as a Uri"); + assert_eq!(new_uri, orig_str, "round trip still equals original str"); + + const ALT: &'static [&'static str] = &$alt; + + for &alt in ALT.iter() { + let other: Uri = alt.parse().unwrap(); + assert_eq!(uri, *alt); + assert_eq!(uri, other); + } + } + ); +} + +test_parse! { + test_uri_parse_path_and_query, + "/some/path/here?and=then&hello#and-bye", + [], + + scheme = None, + authority = None, + path = "/some/path/here", + query = Some("and=then&hello"), + host = None, +} + +test_parse! { + test_uri_parse_absolute_form, + "http://127.0.0.1:61761/chunks", + [], + + scheme = part!("http"), + authority = part!("127.0.0.1:61761"), + path = "/chunks", + query = None, + host = Some("127.0.0.1"), + port = Port::from_str("61761").ok(), +} + +test_parse! { + test_uri_parse_absolute_form_without_path, + "https://127.0.0.1:61761", + ["https://127.0.0.1:61761/"], + + scheme = part!("https"), + authority = part!("127.0.0.1:61761"), + path = "/", + query = None, + host = Some("127.0.0.1"), + port = Port::from_str("61761").ok(), +} + +test_parse! { + test_uri_parse_asterisk_form, + "*", + [], + + scheme = None, + authority = None, + path = "*", + query = None, + host = None, +} + +test_parse! { + test_uri_parse_authority_no_port, + "localhost", + ["LOCALHOST", "LocaLHOSt"], + + scheme = None, + authority = part!("localhost"), + path = "", + query = None, + port = None, + host = Some("localhost"), +} + +test_parse! { + test_uri_authority_only_one_character_issue_197, + "S", + [], + + scheme = None, + authority = part!("S"), + path = "", + query = None, + port = None, + host = Some("S"), +} + +test_parse! { + test_uri_parse_authority_form, + "localhost:3000", + ["localhosT:3000"], + + scheme = None, + authority = part!("localhost:3000"), + path = "", + query = None, + host = Some("localhost"), + port = Port::from_str("3000").ok(), +} + +test_parse! { + test_uri_parse_absolute_with_default_port_http, + "http://127.0.0.1:80", + ["http://127.0.0.1:80/"], + + scheme = part!("http"), + authority = part!("127.0.0.1:80"), + host = Some("127.0.0.1"), + path = "/", + query = None, + port = Port::from_str("80").ok(), +} + +test_parse! { + test_uri_parse_absolute_with_default_port_https, + "https://127.0.0.1:443", + ["https://127.0.0.1:443/"], + + scheme = part!("https"), + authority = part!("127.0.0.1:443"), + host = Some("127.0.0.1"), + path = "/", + query = None, + port = Port::from_str("443").ok(), +} + +test_parse! { + test_uri_parse_fragment_questionmark, + "http://127.0.0.1/#?", + [], + + scheme = part!("http"), + authority = part!("127.0.0.1"), + host = Some("127.0.0.1"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_uri_parse_path_with_terminating_questionmark, + "http://127.0.0.1/path?", + [], + + scheme = part!("http"), + authority = part!("127.0.0.1"), + path = "/path", + query = Some(""), + port = None, +} + +test_parse! { + test_uri_parse_absolute_form_with_empty_path_and_nonempty_query, + "http://127.0.0.1?foo=bar", + [], + + scheme = part!("http"), + authority = part!("127.0.0.1"), + path = "/", + query = Some("foo=bar"), + port = None, +} + +test_parse! { + test_uri_parse_absolute_form_with_empty_path_and_fragment_with_slash, + "http://127.0.0.1#foo/bar", + [], + + scheme = part!("http"), + authority = part!("127.0.0.1"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_uri_parse_absolute_form_with_empty_path_and_fragment_with_questionmark, + "http://127.0.0.1#foo?bar", + [], + + scheme = part!("http"), + authority = part!("127.0.0.1"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_uri_parse_long_host_with_no_scheme, + "thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost", + [], + + scheme = None, + authority = part!("thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost"), + path = "", + query = None, + port = None, +} + +test_parse! { + test_uri_parse_long_host_with_port_and_no_scheme, + "thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost:1234", + [], + + scheme = None, + authority = part!("thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost:1234"), + path = "", + query = None, + port = Port::from_str("1234").ok(), +} + +test_parse! { + test_userinfo1, + "http://a:b@127.0.0.1:1234/", + [], + + scheme = part!("http"), + authority = part!("a:b@127.0.0.1:1234"), + host = Some("127.0.0.1"), + path = "/", + query = None, + port = Port::from_str("1234").ok(), +} + +test_parse! { + test_userinfo2, + "http://a:b@127.0.0.1/", + [], + + scheme = part!("http"), + authority = part!("a:b@127.0.0.1"), + host = Some("127.0.0.1"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_userinfo3, + "http://a@127.0.0.1/", + [], + + scheme = part!("http"), + authority = part!("a@127.0.0.1"), + host = Some("127.0.0.1"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_userinfo_with_port, + "user@localhost:3000", + [], + + scheme = None, + authority = part!("user@localhost:3000"), + path = "", + query = None, + host = Some("localhost"), + port = Port::from_str("3000").ok(), +} + +test_parse! { + test_userinfo_pass_with_port, + "user:pass@localhost:3000", + [], + + scheme = None, + authority = part!("user:pass@localhost:3000"), + path = "", + query = None, + host = Some("localhost"), + port = Port::from_str("3000").ok(), +} + +test_parse! { + test_ipv6, + "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/", + [], + + scheme = part!("http"), + authority = part!("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"), + host = Some("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_ipv6_shorthand, + "http://[::1]/", + [], + + scheme = part!("http"), + authority = part!("[::1]"), + host = Some("[::1]"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_ipv6_shorthand2, + "http://[::]/", + [], + + scheme = part!("http"), + authority = part!("[::]"), + host = Some("[::]"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_ipv6_shorthand3, + "http://[2001:db8::2:1]/", + [], + + scheme = part!("http"), + authority = part!("[2001:db8::2:1]"), + host = Some("[2001:db8::2:1]"), + path = "/", + query = None, + port = None, +} + +test_parse! { + test_ipv6_with_port, + "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8008/", + [], + + scheme = part!("http"), + authority = part!("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8008"), + host = Some("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"), + path = "/", + query = None, + port = Port::from_str("8008").ok(), +} + +test_parse! { + test_percentage_encoded_path, + "/echo/abcdefgh_i-j%20/abcdefg_i-j%20478", + [], + + scheme = None, + authority = None, + host = None, + path = "/echo/abcdefgh_i-j%20/abcdefg_i-j%20478", + query = None, + port = None, +} + +test_parse! { + test_path_permissive, + "/foo=bar|baz\\^~%", + [], + + path = "/foo=bar|baz\\^~%", +} + +test_parse! { + test_query_permissive, + "/?foo={bar|baz}\\^`", + [], + + query = Some("foo={bar|baz}\\^`"), +} + +#[test] +fn test_uri_parse_error() { + fn err(s: &str) { + Uri::from_str(s).unwrap_err(); + } + + err("http://"); + err("htt:p//host"); + err("hyper.rs/"); + err("hyper.rs?key=val"); + err("?key=val"); + err("localhost/"); + err("localhost?key=val"); + err("\0"); + err("http://[::1"); + err("http://::1]"); + err("localhost:8080:3030"); + err("@"); + err("http://username:password@/wut"); + + // illegal queries + err("/?foo\rbar"); + err("/?foo\nbar"); + err("/?<"); + err("/?>"); +} + +#[test] +fn test_max_uri_len() { + let mut uri = vec![]; + uri.extend(b"http://localhost/"); + uri.extend(vec![b'a'; 70 * 1024]); + + let uri = String::from_utf8(uri).unwrap(); + let res: Result = uri.parse(); + + assert_eq!(res.unwrap_err().0, ErrorKind::TooLong); +} + +#[test] +fn test_overflowing_scheme() { + let mut uri = vec![]; + uri.extend(vec![b'a'; 256]); + uri.extend(b"://localhost/"); + + let uri = String::from_utf8(uri).unwrap(); + let res: Result = uri.parse(); + + assert_eq!(res.unwrap_err().0, ErrorKind::SchemeTooLong); +} + +#[test] +fn test_max_length_scheme() { + let mut uri = vec![]; + uri.extend(vec![b'a'; 64]); + uri.extend(b"://localhost/"); + + let uri = String::from_utf8(uri).unwrap(); + let uri: Uri = uri.parse().unwrap(); + + assert_eq!(uri.scheme_str().unwrap().len(), 64); +} + +#[test] +fn test_uri_to_path_and_query() { + let cases = vec![ + ("/", "/"), + ("/foo?bar", "/foo?bar"), + ("/foo?bar#nope", "/foo?bar"), + ("http://hyper.rs", "/"), + ("http://hyper.rs/", "/"), + ("http://hyper.rs/path", "/path"), + ("http://hyper.rs?query", "/?query"), + ("*", "*"), + ]; + + for case in cases { + let uri = Uri::from_str(case.0).unwrap(); + let s = uri.path_and_query().unwrap().to_string(); + + assert_eq!(s, case.1); + } +} + +#[test] +fn test_authority_uri_parts_round_trip() { + let s = "hyper.rs"; + let uri = Uri::from_str(s).expect("first parse"); + assert_eq!(uri, s); + assert_eq!(uri.to_string(), s); + + let parts = uri.into_parts(); + let uri2 = Uri::from_parts(parts).expect("from_parts"); + assert_eq!(uri2, s); + assert_eq!(uri2.to_string(), s); +} + +#[test] +fn test_partial_eq_path_with_terminating_questionmark() { + let a = "/path"; + let uri = Uri::from_str("/path?").expect("first parse"); + + assert_eq!(uri, a); +} diff --git a/third_party/rust/http/v1/crate/src/version.rs b/third_party/rust/http/v1/crate/src/version.rs new file mode 100644 index 000000000000..d8b713061e3c --- /dev/null +++ b/third_party/rust/http/v1/crate/src/version.rs @@ -0,0 +1,75 @@ +//! HTTP version +//! +//! This module contains a definition of the `Version` type. The `Version` +//! type is intended to be accessed through the root of the crate +//! (`http::Version`) rather than this module. +//! +//! The `Version` type contains constants that represent the various versions +//! of the HTTP protocol. +//! +//! # Examples +//! +//! ``` +//! use http::Version; +//! +//! let http11 = Version::HTTP_11; +//! let http2 = Version::HTTP_2; +//! assert!(http11 != http2); +//! +//! println!("{:?}", http2); +//! ``` + +use std::fmt; + +/// Represents a version of the HTTP spec. +#[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)] +pub struct Version(Http); + +impl Version { + /// `HTTP/0.9` + pub const HTTP_09: Version = Version(Http::Http09); + + /// `HTTP/1.0` + pub const HTTP_10: Version = Version(Http::Http10); + + /// `HTTP/1.1` + pub const HTTP_11: Version = Version(Http::Http11); + + /// `HTTP/2.0` + pub const HTTP_2: Version = Version(Http::H2); + + /// `HTTP/3.0` + pub const HTTP_3: Version = Version(Http::H3); +} + +#[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)] +enum Http { + Http09, + Http10, + Http11, + H2, + H3, + __NonExhaustive, +} + +impl Default for Version { + #[inline] + fn default() -> Version { + Version::HTTP_11 + } +} + +impl fmt::Debug for Version { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use self::Http::*; + + f.write_str(match self.0 { + Http09 => "HTTP/0.9", + Http10 => "HTTP/1.0", + Http11 => "HTTP/1.1", + H2 => "HTTP/2.0", + H3 => "HTTP/3.0", + __NonExhaustive => unreachable!(), + }) + } +} diff --git a/third_party/rust/http/v1/crate/tests/header_map.rs b/third_party/rust/http/v1/crate/tests/header_map.rs new file mode 100644 index 000000000000..9859b0a832f7 --- /dev/null +++ b/third_party/rust/http/v1/crate/tests/header_map.rs @@ -0,0 +1,650 @@ +use http::header::*; +use http::*; + +#[test] +fn smoke() { + let mut headers = HeaderMap::new(); + + assert!(headers.get("hello").is_none()); + + let name: HeaderName = "hello".parse().unwrap(); + + match headers.entry(&name) { + Entry::Vacant(e) => { + e.insert("world".parse().unwrap()); + } + _ => panic!(), + } + + assert!(headers.get("hello").is_some()); + + match headers.entry(&name) { + Entry::Occupied(mut e) => { + assert_eq!(e.get(), &"world"); + + // Push another value + e.append("zomg".parse().unwrap()); + + let mut i = e.iter(); + + assert_eq!(*i.next().unwrap(), "world"); + assert_eq!(*i.next().unwrap(), "zomg"); + assert!(i.next().is_none()); + } + _ => panic!(), + } +} + +#[test] +#[should_panic] +fn reserve_over_capacity() { + // See https://github.com/hyperium/http/issues/352 + let mut headers = HeaderMap::::with_capacity(32); + headers.reserve(50_000); // over MAX_SIZE +} + +#[test] +fn with_capacity_max() { + // The largest capacity such that (cap + cap / 3) < MAX_SIZE. + HeaderMap::::with_capacity(24_576); +} + +#[test] +#[should_panic] +fn with_capacity_overflow() { + HeaderMap::::with_capacity(24_577); +} + +#[test] +#[should_panic] +fn reserve_overflow() { + // See https://github.com/hyperium/http/issues/352 + let mut headers = HeaderMap::::with_capacity(0); + headers.reserve(std::usize::MAX); // next_power_of_two overflows +} + +#[test] +fn drain() { + let mut headers = HeaderMap::new(); + + // Insert a single value + let name: HeaderName = "hello".parse().unwrap(); + headers.insert(name, "world".parse().unwrap()); + + { + let mut iter = headers.drain(); + let (name, value) = iter.next().unwrap(); + assert_eq!(name.unwrap().as_str(), "hello"); + + assert_eq!(value, "world"); + + assert!(iter.next().is_none()); + } + + assert!(headers.is_empty()); + + // Insert two sequential values + headers.insert( + "hello".parse::().unwrap(), + "world".parse().unwrap(), + ); + headers.insert( + "zomg".parse::().unwrap(), + "bar".parse().unwrap(), + ); + headers.append( + "hello".parse::().unwrap(), + "world2".parse().unwrap(), + ); + + // Drain... + { + let mut iter = headers.drain(); + + let (name, value) = iter.next().unwrap(); + assert_eq!(name.unwrap().as_str(), "hello"); + assert_eq!(value, "world"); + + let (name, value) = iter.next().unwrap(); + assert_eq!(name, None); + assert_eq!(value, "world2"); + + let (name, value) = iter.next().unwrap(); + assert_eq!(name.unwrap().as_str(), "zomg"); + assert_eq!(value, "bar"); + + assert!(iter.next().is_none()); + } +} + +#[test] +fn drain_drop_immediately() { + // test mem::forgetting does not double-free + + let mut headers = HeaderMap::new(); + headers.insert("hello", "world".parse().unwrap()); + headers.insert("zomg", "bar".parse().unwrap()); + headers.append("hello", "world2".parse().unwrap()); + + let iter = headers.drain(); + assert_eq!(iter.size_hint(), (2, Some(3))); + // not consuming `iter` +} + +#[test] +fn drain_forget() { + // test mem::forgetting does not double-free + + let mut headers = HeaderMap::::new(); + headers.insert("hello", "world".parse().unwrap()); + headers.insert("zomg", "bar".parse().unwrap()); + + assert_eq!(headers.len(), 2); + + { + let mut iter = headers.drain(); + assert_eq!(iter.size_hint(), (2, Some(2))); + let _ = iter.next().unwrap(); + std::mem::forget(iter); + } + + assert_eq!(headers.len(), 0); +} + +#[test] +fn drain_entry() { + let mut headers = HeaderMap::new(); + + headers.insert( + "hello".parse::().unwrap(), + "world".parse().unwrap(), + ); + headers.insert( + "zomg".parse::().unwrap(), + "foo".parse().unwrap(), + ); + headers.append( + "hello".parse::().unwrap(), + "world2".parse().unwrap(), + ); + headers.insert( + "more".parse::().unwrap(), + "words".parse().unwrap(), + ); + headers.append( + "more".parse::().unwrap(), + "insertions".parse().unwrap(), + ); + assert_eq!(5, headers.len()); + + // Using insert_mult + { + let mut e = match headers.entry("hello") { + Entry::Occupied(e) => e, + _ => panic!(), + }; + + let vals: Vec<_> = e.insert_mult("wat".parse().unwrap()).collect(); + assert_eq!(2, vals.len()); + assert_eq!(vals[0], "world"); + assert_eq!(vals[1], "world2"); + } + + assert_eq!(5 - 2 + 1, headers.len()); +} + +#[test] +fn eq() { + let mut a = HeaderMap::new(); + let mut b = HeaderMap::new(); + + assert_eq!(a, b); + + a.insert( + "hello".parse::().unwrap(), + "world".parse().unwrap(), + ); + assert_ne!(a, b); + + b.insert( + "hello".parse::().unwrap(), + "world".parse().unwrap(), + ); + assert_eq!(a, b); + + a.insert("foo".parse::().unwrap(), "bar".parse().unwrap()); + a.append("foo".parse::().unwrap(), "baz".parse().unwrap()); + assert_ne!(a, b); + + b.insert("foo".parse::().unwrap(), "bar".parse().unwrap()); + assert_ne!(a, b); + + b.append("foo".parse::().unwrap(), "baz".parse().unwrap()); + assert_eq!(a, b); + + a.append("a".parse::().unwrap(), "a".parse().unwrap()); + a.append("a".parse::().unwrap(), "b".parse().unwrap()); + b.append("a".parse::().unwrap(), "b".parse().unwrap()); + b.append("a".parse::().unwrap(), "a".parse().unwrap()); + + assert_ne!(a, b); +} + +#[test] +fn into_header_name() { + let mut m = HeaderMap::new(); + m.insert(HOST, "localhost".parse().unwrap()); + m.insert(&ACCEPT, "*/*".parse().unwrap()); + m.insert("connection", "keep-alive".parse().unwrap()); + + m.append(LOCATION, "/".parse().unwrap()); + m.append(&VIA, "bob".parse().unwrap()); + m.append("transfer-encoding", "chunked".parse().unwrap()); + + assert_eq!(m.len(), 6); +} + +#[test] +fn as_header_name() { + let mut m = HeaderMap::new(); + let v: HeaderValue = "localhost".parse().unwrap(); + m.insert(HOST, v.clone()); + + let expected = Some(&v); + + assert_eq!(m.get("host"), expected); + assert_eq!(m.get(&HOST), expected); + + let s = String::from("host"); + assert_eq!(m.get(&s), expected); + assert_eq!(m.get(s.as_str()), expected); +} + +#[test] +fn insert_all_std_headers() { + let mut m = HeaderMap::new(); + + for (i, hdr) in STD.iter().enumerate() { + m.insert(hdr.clone(), hdr.as_str().parse().unwrap()); + + for j in 0..(i + 1) { + assert_eq!(m[&STD[j]], STD[j].as_str()); + } + + if i != 0 { + for j in (i + 1)..STD.len() { + assert!( + m.get(&STD[j]).is_none(), + "contained {}; j={}", + STD[j].as_str(), + j + ); + } + } + } +} + +#[test] +fn insert_79_custom_std_headers() { + let mut h = HeaderMap::new(); + let hdrs = custom_std(79); + + for (i, hdr) in hdrs.iter().enumerate() { + h.insert(hdr.clone(), hdr.as_str().parse().unwrap()); + + for j in 0..(i + 1) { + assert_eq!(h[&hdrs[j]], hdrs[j].as_str()); + } + + for j in (i + 1)..hdrs.len() { + assert!(h.get(&hdrs[j]).is_none()); + } + } +} + +#[test] +fn append_multiple_values() { + let mut map = HeaderMap::new(); + + map.append(header::CONTENT_TYPE, "json".parse().unwrap()); + map.append(header::CONTENT_TYPE, "html".parse().unwrap()); + map.append(header::CONTENT_TYPE, "xml".parse().unwrap()); + + let vals = map + .get_all(&header::CONTENT_TYPE) + .iter() + .collect::>(); + + assert_eq!(&vals, &[&"json", &"html", &"xml"]); +} + +fn custom_std(n: usize) -> Vec { + (0..n) + .map(|i| { + let s = format!("{}-{}", STD[i % STD.len()].as_str(), i); + s.parse().unwrap() + }) + .collect() +} + +const STD: &'static [HeaderName] = &[ + ACCEPT, + ACCEPT_CHARSET, + ACCEPT_ENCODING, + ACCEPT_LANGUAGE, + ACCEPT_RANGES, + ACCESS_CONTROL_ALLOW_CREDENTIALS, + ACCESS_CONTROL_ALLOW_HEADERS, + ACCESS_CONTROL_ALLOW_METHODS, + ACCESS_CONTROL_ALLOW_ORIGIN, + ACCESS_CONTROL_EXPOSE_HEADERS, + ACCESS_CONTROL_MAX_AGE, + ACCESS_CONTROL_REQUEST_HEADERS, + ACCESS_CONTROL_REQUEST_METHOD, + AGE, + ALLOW, + ALT_SVC, + AUTHORIZATION, + CACHE_CONTROL, + CACHE_STATUS, + CDN_CACHE_CONTROL, + CONNECTION, + CONTENT_DISPOSITION, + CONTENT_ENCODING, + CONTENT_LANGUAGE, + CONTENT_LENGTH, + CONTENT_LOCATION, + CONTENT_RANGE, + CONTENT_SECURITY_POLICY, + CONTENT_SECURITY_POLICY_REPORT_ONLY, + CONTENT_TYPE, + COOKIE, + DNT, + DATE, + ETAG, + EXPECT, + EXPIRES, + FORWARDED, + FROM, + HOST, + IF_MATCH, + IF_MODIFIED_SINCE, + IF_NONE_MATCH, + IF_RANGE, + IF_UNMODIFIED_SINCE, + LAST_MODIFIED, + LINK, + LOCATION, + MAX_FORWARDS, + ORIGIN, + PRAGMA, + PROXY_AUTHENTICATE, + PROXY_AUTHORIZATION, + PUBLIC_KEY_PINS, + PUBLIC_KEY_PINS_REPORT_ONLY, + RANGE, + REFERER, + REFERRER_POLICY, + RETRY_AFTER, + SERVER, + SET_COOKIE, + STRICT_TRANSPORT_SECURITY, + TE, + TRAILER, + TRANSFER_ENCODING, + USER_AGENT, + UPGRADE, + UPGRADE_INSECURE_REQUESTS, + VARY, + VIA, + WARNING, + WWW_AUTHENTICATE, + X_CONTENT_TYPE_OPTIONS, + X_DNS_PREFETCH_CONTROL, + X_FRAME_OPTIONS, + X_XSS_PROTECTION, +]; + +#[test] +fn get_invalid() { + let mut headers = HeaderMap::new(); + headers.insert("foo", "bar".parse().unwrap()); + assert!(headers.get("Evil\r\nKey").is_none()); +} + +#[test] +#[should_panic] +fn insert_invalid() { + let mut headers = HeaderMap::new(); + headers.insert("evil\r\nfoo", "bar".parse().unwrap()); +} + +#[test] +fn value_htab() { + // RFC 7230 Section 3.2: + // > field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] + HeaderValue::from_static("hello\tworld"); + HeaderValue::from_str("hello\tworld").unwrap(); +} + +#[test] +fn remove_multiple_a() { + let mut headers = HeaderMap::new(); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + headers.append(VIA, "1.1 other.com".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_3=value 3".parse().unwrap()); + headers.insert(VARY, "*".parse().unwrap()); + + assert_eq!(headers.len(), 6); + + let cookie = headers.remove(SET_COOKIE); + assert_eq!(cookie, Some("cookie_1=value 1".parse().unwrap())); + assert_eq!(headers.len(), 3); + + let via = headers.remove(VIA); + assert_eq!(via, Some("1.1 example.com".parse().unwrap())); + assert_eq!(headers.len(), 1); + + let vary = headers.remove(VARY); + assert_eq!(vary, Some("*".parse().unwrap())); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_multiple_b() { + let mut headers = HeaderMap::new(); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + headers.append(VIA, "1.1 other.com".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_3=value 3".parse().unwrap()); + headers.insert(VARY, "*".parse().unwrap()); + + assert_eq!(headers.len(), 6); + + let vary = headers.remove(VARY); + assert_eq!(vary, Some("*".parse().unwrap())); + assert_eq!(headers.len(), 5); + + let via = headers.remove(VIA); + assert_eq!(via, Some("1.1 example.com".parse().unwrap())); + assert_eq!(headers.len(), 3); + + let cookie = headers.remove(SET_COOKIE); + assert_eq!(cookie, Some("cookie_1=value 1".parse().unwrap())); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_entry_multi_0() { + let mut headers = HeaderMap::new(); + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 0); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_entry_multi_0_others() { + let mut headers = HeaderMap::new(); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + headers.append(VIA, "1.1 other.com".parse().unwrap()); + + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 0); + assert_eq!(headers.len(), 2); +} + +#[test] +fn remove_entry_multi_1() { + let mut headers = HeaderMap::new(); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 1); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_entry_multi_1_other() { + let mut headers = HeaderMap::new(); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 1); + assert_eq!(headers.len(), 1); + + let vias = remove_all_values(&mut headers, VIA); + assert_eq!(vias.len(), 1); + assert_eq!(headers.len(), 0); +} + +// For issue hyperimum/http#446 +#[test] +fn remove_entry_multi_2() { + let mut headers = HeaderMap::new(); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 2); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_entry_multi_3() { + let mut headers = HeaderMap::new(); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_3=value 3".parse().unwrap()); + + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 3); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_entry_multi_3_others() { + let mut headers = HeaderMap::new(); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + headers.append(VIA, "1.1 other.com".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_3=value 3".parse().unwrap()); + headers.insert(VARY, "*".parse().unwrap()); + + let cookies = remove_all_values(&mut headers, SET_COOKIE); + assert_eq!(cookies.len(), 3); + assert_eq!(headers.len(), 3); + + let vias = remove_all_values(&mut headers, VIA); + assert_eq!(vias.len(), 2); + assert_eq!(headers.len(), 1); + + let varies = remove_all_values(&mut headers, VARY); + assert_eq!(varies.len(), 1); + assert_eq!(headers.len(), 0); +} + +fn remove_all_values(headers: &mut HeaderMap, key: K) -> Vec +where + K: IntoHeaderName, +{ + match headers.entry(key) { + Entry::Occupied(e) => e.remove_entry_mult().1.collect(), + Entry::Vacant(_) => vec![], + } +} + +#[test] +fn remove_entry_3_others_a() { + let mut headers = HeaderMap::new(); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + headers.append(VIA, "1.1 other.com".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_3=value 3".parse().unwrap()); + headers.insert(VARY, "*".parse().unwrap()); + + assert_eq!(headers.len(), 6); + + let cookie = remove_values(&mut headers, SET_COOKIE); + assert_eq!(cookie, Some("cookie_1=value 1".parse().unwrap())); + assert_eq!(headers.len(), 3); + + let via = remove_values(&mut headers, VIA); + assert_eq!(via, Some("1.1 example.com".parse().unwrap())); + assert_eq!(headers.len(), 1); + + let vary = remove_values(&mut headers, VARY); + assert_eq!(vary, Some("*".parse().unwrap())); + assert_eq!(headers.len(), 0); +} + +#[test] +fn remove_entry_3_others_b() { + let mut headers = HeaderMap::new(); + headers.insert(VIA, "1.1 example.com".parse().unwrap()); + headers.insert(SET_COOKIE, "cookie_1=value 1".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_2=value 2".parse().unwrap()); + headers.append(VIA, "1.1 other.com".parse().unwrap()); + headers.append(SET_COOKIE, "cookie_3=value 3".parse().unwrap()); + headers.insert(VARY, "*".parse().unwrap()); + + assert_eq!(headers.len(), 6); + + let vary = remove_values(&mut headers, VARY); + assert_eq!(vary, Some("*".parse().unwrap())); + assert_eq!(headers.len(), 5); + + let via = remove_values(&mut headers, VIA); + assert_eq!(via, Some("1.1 example.com".parse().unwrap())); + assert_eq!(headers.len(), 3); + + let cookie = remove_values(&mut headers, SET_COOKIE); + assert_eq!(cookie, Some("cookie_1=value 1".parse().unwrap())); + assert_eq!(headers.len(), 0); +} + +fn remove_values(headers: &mut HeaderMap, key: K) -> Option +where + K: IntoHeaderName, +{ + match headers.entry(key) { + Entry::Occupied(e) => Some(e.remove_entry().1), + Entry::Vacant(_) => None, + } +} + +#[test] +fn ensure_miri_sharedreadonly_not_violated() { + let mut headers = HeaderMap::new(); + headers.insert( + HeaderName::from_static("chunky-trailer"), + HeaderValue::from_static("header data"), + ); + + let _foo = &headers.iter().next(); +} diff --git a/third_party/rust/http/v1/crate/tests/header_map_fuzz.rs b/third_party/rust/http/v1/crate/tests/header_map_fuzz.rs new file mode 100644 index 000000000000..c3af2e52e702 --- /dev/null +++ b/third_party/rust/http/v1/crate/tests/header_map_fuzz.rs @@ -0,0 +1,376 @@ +use http::header::*; +use http::*; + +use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult}; +use rand::rngs::StdRng; +use rand::seq::SliceRandom; +use rand::{Rng, SeedableRng}; + +use std::collections::HashMap; + +#[cfg(not(miri))] +#[test] +fn header_map_fuzz() { + fn prop(fuzz: Fuzz) -> TestResult { + fuzz.run(); + TestResult::from_bool(true) + } + + QuickCheck::new().quickcheck(prop as fn(Fuzz) -> TestResult) +} + +#[derive(Debug, Clone)] +#[allow(dead_code)] +struct Fuzz { + // The magic seed that makes the test case reproducible + seed: [u8; 32], + + // Actions to perform + steps: Vec, + + // Number of steps to drop + reduce: usize, +} + +#[derive(Debug)] +struct Weight { + insert: usize, + remove: usize, + append: usize, +} + +#[derive(Debug, Clone)] +struct Step { + action: Action, + expect: AltMap, +} + +#[derive(Debug, Clone)] +enum Action { + Insert { + name: HeaderName, // Name to insert + val: HeaderValue, // Value to insert + old: Option, // Old value + }, + Append { + name: HeaderName, + val: HeaderValue, + ret: bool, + }, + Remove { + name: HeaderName, // Name to remove + val: Option, // Value to get + }, +} + +// An alternate implementation of HeaderMap backed by HashMap +#[derive(Debug, Clone, Default)] +struct AltMap { + map: HashMap>, +} + +impl Fuzz { + fn new(seed: [u8; 32]) -> Fuzz { + // Seed the RNG + let mut rng = StdRng::from_seed(seed); + + let mut steps = vec![]; + let mut expect = AltMap::default(); + let num = rng.gen_range(5, 500); + + let weight = Weight { + insert: rng.gen_range(1, 10), + remove: rng.gen_range(1, 10), + append: rng.gen_range(1, 10), + }; + + while steps.len() < num { + steps.push(expect.gen_step(&weight, &mut rng)); + } + + Fuzz { + seed: seed, + steps: steps, + reduce: 0, + } + } + + fn run(self) { + // Create a new header map + let mut map = HeaderMap::new(); + + // Number of steps to perform + let take = self.steps.len() - self.reduce; + + for step in self.steps.into_iter().take(take) { + step.action.apply(&mut map); + + step.expect.assert_identical(&map); + } + } +} + +impl Arbitrary for Fuzz { + fn arbitrary(g: &mut G) -> Self { + Fuzz::new(Rng::gen(g)) + } +} + +impl AltMap { + fn gen_step(&mut self, weight: &Weight, rng: &mut StdRng) -> Step { + let action = self.gen_action(weight, rng); + + Step { + action: action, + expect: self.clone(), + } + } + + /// This will also apply the action against `self` + fn gen_action(&mut self, weight: &Weight, rng: &mut StdRng) -> Action { + let sum = weight.insert + weight.remove + weight.append; + + let mut num = rng.gen_range(0, sum); + + if num < weight.insert { + return self.gen_insert(rng); + } + + num -= weight.insert; + + if num < weight.remove { + return self.gen_remove(rng); + } + + num -= weight.remove; + + if num < weight.append { + return self.gen_append(rng); + } + + unreachable!(); + } + + fn gen_insert(&mut self, rng: &mut StdRng) -> Action { + let name = self.gen_name(4, rng); + let val = gen_header_value(rng); + let old = self.insert(name.clone(), val.clone()); + + Action::Insert { + name: name, + val: val, + old: old, + } + } + + fn gen_remove(&mut self, rng: &mut StdRng) -> Action { + let name = self.gen_name(-4, rng); + let val = self.remove(&name); + + Action::Remove { + name: name, + val: val, + } + } + + fn gen_append(&mut self, rng: &mut StdRng) -> Action { + let name = self.gen_name(-5, rng); + let val = gen_header_value(rng); + + let vals = self.map.entry(name.clone()).or_insert(vec![]); + + let ret = !vals.is_empty(); + vals.push(val.clone()); + + Action::Append { + name: name, + val: val, + ret: ret, + } + } + + /// Negative numbers weigh finding an existing header higher + fn gen_name(&self, weight: i32, rng: &mut StdRng) -> HeaderName { + let mut existing = rng.gen_ratio(1, weight.abs() as u32); + + if weight < 0 { + existing = !existing; + } + + if existing { + // Existing header + if let Some(name) = self.find_random_name(rng) { + name + } else { + gen_header_name(rng) + } + } else { + gen_header_name(rng) + } + } + + fn find_random_name(&self, rng: &mut StdRng) -> Option { + if self.map.is_empty() { + None + } else { + let n = rng.gen_range(0, self.map.len()); + self.map.keys().nth(n).map(Clone::clone) + } + } + + fn insert(&mut self, name: HeaderName, val: HeaderValue) -> Option { + let old = self.map.insert(name, vec![val]); + old.and_then(|v| v.into_iter().next()) + } + + fn remove(&mut self, name: &HeaderName) -> Option { + self.map.remove(name).and_then(|v| v.into_iter().next()) + } + + fn assert_identical(&self, other: &HeaderMap) { + assert_eq!(self.map.len(), other.keys_len()); + + for (key, val) in &self.map { + // Test get + assert_eq!(other.get(key), val.get(0)); + + // Test get_all + let vals = other.get_all(key); + let actual: Vec<_> = vals.iter().collect(); + assert_eq!(&actual[..], &val[..]); + } + } +} + +impl Action { + fn apply(self, map: &mut HeaderMap) { + match self { + Action::Insert { name, val, old } => { + let actual = map.insert(name, val); + assert_eq!(actual, old); + } + Action::Remove { name, val } => { + // Just to help track the state, load all associated values. + let _ = map.get_all(&name).iter().collect::>(); + + let actual = map.remove(&name); + assert_eq!(actual, val); + } + Action::Append { name, val, ret } => { + assert_eq!(ret, map.append(name, val)); + } + } + } +} + +fn gen_header_name(g: &mut StdRng) -> HeaderName { + const STANDARD_HEADERS: &'static [HeaderName] = &[ + header::ACCEPT, + header::ACCEPT_CHARSET, + header::ACCEPT_ENCODING, + header::ACCEPT_LANGUAGE, + header::ACCEPT_RANGES, + header::ACCESS_CONTROL_ALLOW_CREDENTIALS, + header::ACCESS_CONTROL_ALLOW_HEADERS, + header::ACCESS_CONTROL_ALLOW_METHODS, + header::ACCESS_CONTROL_ALLOW_ORIGIN, + header::ACCESS_CONTROL_EXPOSE_HEADERS, + header::ACCESS_CONTROL_MAX_AGE, + header::ACCESS_CONTROL_REQUEST_HEADERS, + header::ACCESS_CONTROL_REQUEST_METHOD, + header::AGE, + header::ALLOW, + header::ALT_SVC, + header::AUTHORIZATION, + header::CACHE_CONTROL, + header::CACHE_STATUS, + header::CDN_CACHE_CONTROL, + header::CONNECTION, + header::CONTENT_DISPOSITION, + header::CONTENT_ENCODING, + header::CONTENT_LANGUAGE, + header::CONTENT_LENGTH, + header::CONTENT_LOCATION, + header::CONTENT_RANGE, + header::CONTENT_SECURITY_POLICY, + header::CONTENT_SECURITY_POLICY_REPORT_ONLY, + header::CONTENT_TYPE, + header::COOKIE, + header::DNT, + header::DATE, + header::ETAG, + header::EXPECT, + header::EXPIRES, + header::FORWARDED, + header::FROM, + header::HOST, + header::IF_MATCH, + header::IF_MODIFIED_SINCE, + header::IF_NONE_MATCH, + header::IF_RANGE, + header::IF_UNMODIFIED_SINCE, + header::LAST_MODIFIED, + header::LINK, + header::LOCATION, + header::MAX_FORWARDS, + header::ORIGIN, + header::PRAGMA, + header::PROXY_AUTHENTICATE, + header::PROXY_AUTHORIZATION, + header::PUBLIC_KEY_PINS, + header::PUBLIC_KEY_PINS_REPORT_ONLY, + header::RANGE, + header::REFERER, + header::REFERRER_POLICY, + header::REFRESH, + header::RETRY_AFTER, + header::SEC_WEBSOCKET_ACCEPT, + header::SEC_WEBSOCKET_EXTENSIONS, + header::SEC_WEBSOCKET_KEY, + header::SEC_WEBSOCKET_PROTOCOL, + header::SEC_WEBSOCKET_VERSION, + header::SERVER, + header::SET_COOKIE, + header::STRICT_TRANSPORT_SECURITY, + header::TE, + header::TRAILER, + header::TRANSFER_ENCODING, + header::UPGRADE, + header::UPGRADE_INSECURE_REQUESTS, + header::USER_AGENT, + header::VARY, + header::VIA, + header::WARNING, + header::WWW_AUTHENTICATE, + header::X_CONTENT_TYPE_OPTIONS, + header::X_DNS_PREFETCH_CONTROL, + header::X_FRAME_OPTIONS, + header::X_XSS_PROTECTION, + ]; + + if g.gen_ratio(1, 2) { + STANDARD_HEADERS.choose(g).unwrap().clone() + } else { + let value = gen_string(g, 1, 25); + HeaderName::from_bytes(value.as_bytes()).unwrap() + } +} + +fn gen_header_value(g: &mut StdRng) -> HeaderValue { + let value = gen_string(g, 0, 70); + HeaderValue::from_bytes(value.as_bytes()).unwrap() +} + +fn gen_string(g: &mut StdRng, min: usize, max: usize) -> String { + let bytes: Vec<_> = (min..max) + .map(|_| { + // Chars to pick from + b"ABCDEFGHIJKLMNOPQRSTUVabcdefghilpqrstuvwxyz----" + .choose(g) + .unwrap() + .clone() + }) + .collect(); + + String::from_utf8(bytes).unwrap() +} diff --git a/third_party/rust/http/v1/crate/tests/status_code.rs b/third_party/rust/http/v1/crate/tests/status_code.rs new file mode 100644 index 000000000000..160df6bad5d3 --- /dev/null +++ b/third_party/rust/http/v1/crate/tests/status_code.rs @@ -0,0 +1,82 @@ +use http::*; + +#[test] +fn from_bytes() { + for ok in &[ + "100", "101", "199", "200", "250", "299", "321", "399", "499", "599", "600", "999" + ] { + assert!(StatusCode::from_bytes(ok.as_bytes()).is_ok()); + } + + for not_ok in &[ + "0", "00", "10", "40", "99", "000", "010", "099", "1000", "1999", + ] { + assert!(StatusCode::from_bytes(not_ok.as_bytes()).is_err()); + } +} + +#[test] +fn equates_with_u16() { + let status = StatusCode::from_u16(200u16).unwrap(); + assert_eq!(200u16, status); + assert_eq!(status, 200u16); +} + +#[test] +fn roundtrip() { + for s in 100..1000 { + let sstr = s.to_string(); + let status = StatusCode::from_bytes(sstr.as_bytes()).unwrap(); + assert_eq!(s, u16::from(status)); + assert_eq!(sstr, status.as_str()); + } +} + +#[test] +fn is_informational() { + assert!(status_code(100).is_informational()); + assert!(status_code(199).is_informational()); + + assert!(!status_code(200).is_informational()); +} + +#[test] +fn is_success() { + assert!(status_code(200).is_success()); + assert!(status_code(299).is_success()); + + assert!(!status_code(199).is_success()); + assert!(!status_code(300).is_success()); +} + +#[test] +fn is_redirection() { + assert!(status_code(300).is_redirection()); + assert!(status_code(399).is_redirection()); + + assert!(!status_code(299).is_redirection()); + assert!(!status_code(400).is_redirection()); +} + +#[test] +fn is_client_error() { + assert!(status_code(400).is_client_error()); + assert!(status_code(499).is_client_error()); + + assert!(!status_code(399).is_client_error()); + assert!(!status_code(500).is_client_error()); +} + +#[test] +fn is_server_error() { + assert!(status_code(500).is_server_error()); + assert!(status_code(599).is_server_error()); + + assert!(!status_code(499).is_server_error()); + assert!(!status_code(600).is_server_error()); +} + +/// Helper method for readability +fn status_code(status_code: u16) -> StatusCode { + StatusCode::from_u16(status_code).unwrap() +} diff --git a/third_party/rust/third_party.toml b/third_party/rust/third_party.toml index 28e063135b32..b67c0e378944 100644 --- a/third_party/rust/third_party.toml +++ b/third_party/rust/third_party.toml @@ -98,7 +98,7 @@ tracing-subscriber = { version = "0.2.0", default-features = false, features = [ # tracing = "0.1" #skus -http = { version = "0.1" } +http = { version = "1" } async-trait = "0.1.64" rand = { version = "0.7" } # serde_json = "1.0" From 834d013d57ed94f8f5e477affa20e4cc18e16711 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Wed, 13 Dec 2023 08:26:41 -0800 Subject: [PATCH 2/8] Add third-party rust crate bytes v1.5.0 This is needed by the stable version of the `http` crate used by the skus component. --- third_party/rust/bytes/v1/BUILD.gn | 70 + third_party/rust/bytes/v1/README.chromium | 7 + .../rust/bytes/v1/crate/.cargo_vcs_info.json | 6 + .../bytes/v1/crate/.github/workflows/ci.yml | 172 ++ third_party/rust/bytes/v1/crate/.gitignore | 2 + third_party/rust/bytes/v1/crate/CHANGELOG.md | 294 +++ third_party/rust/bytes/v1/crate/Cargo.toml | 54 + .../rust/bytes/v1/crate/Cargo.toml.orig | 34 + third_party/rust/bytes/v1/crate/LICENSE | 25 + third_party/rust/bytes/v1/crate/README.md | 56 + .../rust/bytes/v1/crate/benches/buf.rs | 186 ++ .../rust/bytes/v1/crate/benches/bytes.rs | 120 ++ .../rust/bytes/v1/crate/benches/bytes_mut.rs | 266 +++ third_party/rust/bytes/v1/crate/ci/miri.sh | 11 + .../rust/bytes/v1/crate/ci/test-stable.sh | 28 + third_party/rust/bytes/v1/crate/ci/tsan.sh | 13 + third_party/rust/bytes/v1/crate/clippy.toml | 1 + .../rust/bytes/v1/crate/src/buf/buf_impl.rs | 1394 +++++++++++++ .../rust/bytes/v1/crate/src/buf/buf_mut.rs | 1528 ++++++++++++++ .../rust/bytes/v1/crate/src/buf/chain.rs | 242 +++ .../rust/bytes/v1/crate/src/buf/iter.rs | 130 ++ .../rust/bytes/v1/crate/src/buf/limit.rs | 75 + .../rust/bytes/v1/crate/src/buf/mod.rs | 41 + .../rust/bytes/v1/crate/src/buf/reader.rs | 81 + .../rust/bytes/v1/crate/src/buf/take.rs | 155 ++ .../bytes/v1/crate/src/buf/uninit_slice.rs | 257 +++ .../rust/bytes/v1/crate/src/buf/vec_deque.rs | 22 + .../rust/bytes/v1/crate/src/buf/writer.rs | 88 + third_party/rust/bytes/v1/crate/src/bytes.rs | 1304 ++++++++++++ .../rust/bytes/v1/crate/src/bytes_mut.rs | 1813 +++++++++++++++++ .../rust/bytes/v1/crate/src/fmt/debug.rs | 49 + .../rust/bytes/v1/crate/src/fmt/hex.rs | 37 + .../rust/bytes/v1/crate/src/fmt/mod.rs | 5 + third_party/rust/bytes/v1/crate/src/lib.rs | 117 ++ third_party/rust/bytes/v1/crate/src/loom.rs | 30 + third_party/rust/bytes/v1/crate/src/serde.rs | 89 + .../rust/bytes/v1/crate/tests/test_buf.rs | 121 ++ .../rust/bytes/v1/crate/tests/test_buf_mut.rs | 276 +++ .../rust/bytes/v1/crate/tests/test_bytes.rs | 1210 +++++++++++ .../v1/crate/tests/test_bytes_odd_alloc.rs | 97 + .../v1/crate/tests/test_bytes_vec_alloc.rs | 143 ++ .../rust/bytes/v1/crate/tests/test_chain.rs | 177 ++ .../rust/bytes/v1/crate/tests/test_debug.rs | 35 + .../rust/bytes/v1/crate/tests/test_iter.rs | 21 + .../rust/bytes/v1/crate/tests/test_reader.rs | 29 + .../rust/bytes/v1/crate/tests/test_serde.rs | 20 + .../rust/bytes/v1/crate/tests/test_take.rs | 32 + third_party/rust/http/v1/BUILD.gn | 2 +- 48 files changed, 10964 insertions(+), 1 deletion(-) create mode 100644 third_party/rust/bytes/v1/BUILD.gn create mode 100644 third_party/rust/bytes/v1/README.chromium create mode 100644 third_party/rust/bytes/v1/crate/.cargo_vcs_info.json create mode 100644 third_party/rust/bytes/v1/crate/.github/workflows/ci.yml create mode 100644 third_party/rust/bytes/v1/crate/.gitignore create mode 100644 third_party/rust/bytes/v1/crate/CHANGELOG.md create mode 100644 third_party/rust/bytes/v1/crate/Cargo.toml create mode 100644 third_party/rust/bytes/v1/crate/Cargo.toml.orig create mode 100644 third_party/rust/bytes/v1/crate/LICENSE create mode 100644 third_party/rust/bytes/v1/crate/README.md create mode 100644 third_party/rust/bytes/v1/crate/benches/buf.rs create mode 100644 third_party/rust/bytes/v1/crate/benches/bytes.rs create mode 100644 third_party/rust/bytes/v1/crate/benches/bytes_mut.rs create mode 100755 third_party/rust/bytes/v1/crate/ci/miri.sh create mode 100644 third_party/rust/bytes/v1/crate/ci/test-stable.sh create mode 100644 third_party/rust/bytes/v1/crate/ci/tsan.sh create mode 100644 third_party/rust/bytes/v1/crate/clippy.toml create mode 100644 third_party/rust/bytes/v1/crate/src/buf/buf_impl.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/buf_mut.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/chain.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/iter.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/limit.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/mod.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/reader.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/take.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/uninit_slice.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/vec_deque.rs create mode 100644 third_party/rust/bytes/v1/crate/src/buf/writer.rs create mode 100644 third_party/rust/bytes/v1/crate/src/bytes.rs create mode 100644 third_party/rust/bytes/v1/crate/src/bytes_mut.rs create mode 100644 third_party/rust/bytes/v1/crate/src/fmt/debug.rs create mode 100644 third_party/rust/bytes/v1/crate/src/fmt/hex.rs create mode 100644 third_party/rust/bytes/v1/crate/src/fmt/mod.rs create mode 100644 third_party/rust/bytes/v1/crate/src/lib.rs create mode 100644 third_party/rust/bytes/v1/crate/src/loom.rs create mode 100644 third_party/rust/bytes/v1/crate/src/serde.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_buf.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_buf_mut.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_bytes.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_bytes_odd_alloc.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_bytes_vec_alloc.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_chain.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_debug.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_iter.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_reader.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_serde.rs create mode 100644 third_party/rust/bytes/v1/crate/tests/test_take.rs diff --git a/third_party/rust/bytes/v1/BUILD.gn b/third_party/rust/bytes/v1/BUILD.gn new file mode 100644 index 000000000000..9c28859c0fed --- /dev/null +++ b/third_party/rust/bytes/v1/BUILD.gn @@ -0,0 +1,70 @@ +# Copyright 2023 The Chromium Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/rust/cargo_crate.gni") + +cargo_crate("lib") { + crate_name = "bytes" + epoch = "1" + crate_type = "rlib" + + # Only for usage from third-party crates. Add the crate to + # third_party.toml to use it from first-party code. + visibility = [ "//brave/third_party/rust/*" ] + crate_root = "crate/src/lib.rs" + sources = [ + "//brave/third_party/rust/bytes/v1/crate/benches/buf.rs", + "//brave/third_party/rust/bytes/v1/crate/benches/bytes.rs", + "//brave/third_party/rust/bytes/v1/crate/benches/bytes_mut.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/buf_impl.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/buf_mut.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/chain.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/iter.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/limit.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/mod.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/reader.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/take.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/uninit_slice.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/vec_deque.rs", + "//brave/third_party/rust/bytes/v1/crate/src/buf/writer.rs", + "//brave/third_party/rust/bytes/v1/crate/src/bytes.rs", + "//brave/third_party/rust/bytes/v1/crate/src/bytes_mut.rs", + "//brave/third_party/rust/bytes/v1/crate/src/fmt/debug.rs", + "//brave/third_party/rust/bytes/v1/crate/src/fmt/hex.rs", + "//brave/third_party/rust/bytes/v1/crate/src/fmt/mod.rs", + "//brave/third_party/rust/bytes/v1/crate/src/lib.rs", + "//brave/third_party/rust/bytes/v1/crate/src/loom.rs", + "//brave/third_party/rust/bytes/v1/crate/src/serde.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_buf.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_buf_mut.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_bytes.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_bytes_odd_alloc.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_bytes_vec_alloc.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_chain.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_debug.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_iter.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_reader.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_serde.rs", + "//brave/third_party/rust/bytes/v1/crate/tests/test_take.rs", + ] + inputs = [ + "//brave/third_party/rust/bytes/v1/crate/CHANGELOG.md", + "//brave/third_party/rust/bytes/v1/crate/README.md", + ] + + # Unit tests skipped. Generate with --with-tests to include them. + build_native_rust_unit_tests = false + edition = "2018" + cargo_pkg_version = "1.5.0" + cargo_pkg_authors = [ + "Carl Lerche ", + "Sean McArthur ", + ] + cargo_pkg_name = "bytes" + cargo_pkg_description = "Types and traits for working with bytes" + library_configs -= [ "//build/config/compiler:chromium_code" ] + library_configs += [ "//build/config/compiler:no_chromium_code" ] + executable_configs -= [ "//build/config/compiler:chromium_code" ] + executable_configs += [ "//build/config/compiler:no_chromium_code" ] +} diff --git a/third_party/rust/bytes/v1/README.chromium b/third_party/rust/bytes/v1/README.chromium new file mode 100644 index 000000000000..439ce32a30bd --- /dev/null +++ b/third_party/rust/bytes/v1/README.chromium @@ -0,0 +1,7 @@ +Name: bytes +URL: https://crates.io/crates/bytes +Description: Types and traits for working with bytes +Version: 1.5.0 +Security Critical: yes +License: MIT +Revision: 74e6e200fd671340d4d4a874f83776def04f6c7b diff --git a/third_party/rust/bytes/v1/crate/.cargo_vcs_info.json b/third_party/rust/bytes/v1/crate/.cargo_vcs_info.json new file mode 100644 index 000000000000..ebd71a0539f1 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/.cargo_vcs_info.json @@ -0,0 +1,6 @@ +{ + "git": { + "sha1": "74e6e200fd671340d4d4a874f83776def04f6c7b" + }, + "path_in_vcs": "" +} \ No newline at end of file diff --git a/third_party/rust/bytes/v1/crate/.github/workflows/ci.yml b/third_party/rust/bytes/v1/crate/.github/workflows/ci.yml new file mode 100644 index 000000000000..a4f7b1d937bb --- /dev/null +++ b/third_party/rust/bytes/v1/crate/.github/workflows/ci.yml @@ -0,0 +1,172 @@ +name: CI + +on: + pull_request: + branches: + - master + push: + branches: + - master + +env: + RUSTFLAGS: -Dwarnings + RUST_BACKTRACE: 1 + nightly: nightly-2022-11-12 + +defaults: + run: + shell: bash + +jobs: + # Check formatting + rustfmt: + name: rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update stable && rustup default stable + - name: Check formatting + run: cargo fmt --all -- --check + + # TODO + # # Apply clippy lints + # clippy: + # name: clippy + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - name: Apply clippy lints + # run: cargo clippy --all-features + + # This represents the minimum Rust version supported by + # Bytes. Updating this should be done in a dedicated PR. + # + # Tests are not run as tests may require newer versions of + # rust. + minrust: + name: minrust + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update 1.39.0 && rustup default 1.39.0 + - name: Check + run: . ci/test-stable.sh check + + # Stable + stable: + name: stable + strategy: + matrix: + os: + - ubuntu-latest + - macos-latest + - windows-latest + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: Install Rust + # --no-self-update is necessary because the windows environment cannot self-update rustup.exe. + run: rustup update stable --no-self-update && rustup default stable + - name: Test + run: . ci/test-stable.sh test + + # Nightly + nightly: + name: nightly + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update $nightly && rustup default $nightly + - name: Test + run: . ci/test-stable.sh test + + # Run tests on some extra platforms + cross: + name: cross + strategy: + matrix: + target: + - i686-unknown-linux-gnu + - armv7-unknown-linux-gnueabihf + - powerpc-unknown-linux-gnu + - powerpc64-unknown-linux-gnu + - wasm32-unknown-unknown + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update stable && rustup default stable + - name: cross build --target ${{ matrix.target }} + run: | + cargo install cross + cross build --target ${{ matrix.target }} + if: matrix.target != 'wasm32-unknown-unknown' + # WASM support + - name: cargo build --target ${{ matrix.target }} + run: | + rustup target add ${{ matrix.target }} + cargo build --target ${{ matrix.target }} + if: matrix.target == 'wasm32-unknown-unknown' + + # Sanitizers + tsan: + name: tsan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update $nightly && rustup default $nightly + - name: Install rust-src + run: rustup component add rust-src + - name: ASAN / TSAN + run: . ci/tsan.sh + miri: + name: miri + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Miri + run: ci/miri.sh + + # Loom + loom: + name: loom + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update $nightly && rustup default $nightly + - name: Loom tests + run: RUSTFLAGS="--cfg loom -Dwarnings" cargo test --lib + + publish_docs: + name: Publish Documentation + needs: + - rustfmt + # - clippy + - stable + - nightly + - minrust + - cross + - tsan + - loom + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Rust + run: rustup update $nightly && rustup default $nightly + - name: Build documentation + run: cargo doc --no-deps --all-features + env: + RUSTDOCFLAGS: --cfg docsrs + - name: Publish documentation + run: | + cd target/doc + git init + git add . + git -c user.name='ci' -c user.email='ci' commit -m 'Deploy Bytes API documentation' + git push -f -q https://git:${{ secrets.github_token }}@github.com/${{ github.repository }} HEAD:gh-pages + if: github.event_name == 'push' && github.event.ref == 'refs/heads/master' && github.repository == 'tokio-rs/bytes' diff --git a/third_party/rust/bytes/v1/crate/.gitignore b/third_party/rust/bytes/v1/crate/.gitignore new file mode 100644 index 000000000000..4fffb2f89cbd --- /dev/null +++ b/third_party/rust/bytes/v1/crate/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/third_party/rust/bytes/v1/crate/CHANGELOG.md b/third_party/rust/bytes/v1/crate/CHANGELOG.md new file mode 100644 index 000000000000..47e48802ca49 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/CHANGELOG.md @@ -0,0 +1,294 @@ +# 1.5.0 (September 7, 2023) + +### Added + +- Add `UninitSlice::{new,init}` (#598, #599) +- Implement `BufMut` for `&mut [MaybeUninit]` (#597) + +### Changed + +- Mark `BytesMut::extend_from_slice` as inline (#595) + +# 1.4.0 (January 31, 2023) + +### Added + +- Make `IntoIter` constructor public (#581) + +### Fixed + +- Avoid large reallocations when freezing `BytesMut` (#592) + +### Documented + +- Document which functions require `std` (#591) +- Fix duplicate "the the" typos (#585) + +# 1.3.0 (November 20, 2022) + +### Added + +- Rename and expose `BytesMut::spare_capacity_mut` (#572) +- Implement native-endian get and put functions for `Buf` and `BufMut` (#576) + +### Fixed + +- Don't have important data in unused capacity when calling reserve (#563) + +### Documented + +- `Bytes::new` etc should return `Self` not `Bytes` (#568) + +# 1.2.1 (July 30, 2022) + +### Fixed + +- Fix unbounded memory growth when using `reserve` (#560) + +# 1.2.0 (July 19, 2022) + +### Added + +- Add `BytesMut::zeroed` (#517) +- Implement `Extend` for `BytesMut` (#527) +- Add conversion from `BytesMut` to `Vec` (#543, #554) +- Add conversion from `Bytes` to `Vec` (#547) +- Add `UninitSlice::as_uninit_slice_mut()` (#548) +- Add const to `Bytes::{len,is_empty}` (#514) + +### Changed + +- Reuse vector in `BytesMut::reserve` (#539, #544) + +### Fixed + +- Make miri happy (#515, #523, #542, #545, #553) +- Make tsan happy (#541) +- Fix `remaining_mut()` on chain (#488) +- Fix amortized asymptotics of `BytesMut` (#555) + +### Documented + +- Redraw layout diagram with box drawing characters (#539) +- Clarify `BytesMut::unsplit` docs (#535) + +# 1.1.0 (August 25, 2021) + +### Added + +- `BufMut::put_bytes(self, val, cnt)` (#487) +- Implement `From>` for `Bytes` (#504) + +### Changed + +- Override `put_slice` for `&mut [u8]` (#483) +- Panic on integer overflow in `Chain::remaining` (#482) +- Add inline tags to `UninitSlice` methods (#443) +- Override `copy_to_bytes` for Chain and Take (#481) +- Keep capacity when unsplit on empty other buf (#502) + +### Documented + +- Clarify `BufMut` allocation guarantees (#501) +- Clarify `BufMut::put_int` behavior (#486) +- Clarify actions of `clear` and `truncate`. (#508) + +# 1.0.1 (January 11, 2021) + +### Changed +- mark `Vec::put_slice` with `#[inline]` (#459) + +### Fixed +- Fix deprecation warning (#457) +- use `Box::into_raw` instead of `mem::forget`-in-disguise (#458) + +# 1.0.0 (December 22, 2020) + +### Changed +- Rename `Buf`/`BufMut` methods `bytes()` and `bytes_mut()` to `chunk()` and `chunk_mut()` (#450) + +### Removed +- remove unused Buf implementation. (#449) + +# 0.6.0 (October 21, 2020) + +API polish in preparation for a 1.0 release. + +### Changed +- `BufMut` is now an `unsafe` trait (#432). +- `BufMut::bytes_mut()` returns `&mut UninitSlice`, a type owned by `bytes` to + avoid undefined behavior (#433). +- `Buf::copy_to_bytes(len)` replaces `Buf::into_bytes()` (#439). +- `Buf`/`BufMut` utility methods are moved onto the trait and `*Ext` traits are + removed (#431). + +### Removed +- `BufMut::bytes_vectored_mut()` (#430). +- `new` methods on combinator types (#434). + +# 0.5.6 (July 13, 2020) + +- Improve `BytesMut` to reuse buffer when fully `advance`d. +- Mark `BytesMut::{as_mut, set_len}` with `#[inline]`. +- Relax synchronization when cloning in shared vtable of `Bytes`. +- Move `loom` to `dev-dependencies`. + +# 0.5.5 (June 18, 2020) + +### Added +- Allow using the `serde` feature in `no_std` environments (#385). + +### Fix +- Fix `BufMut::advance_mut` to panic if advanced passed the capacity (#354).. +- Fix `BytesMut::freeze` ignoring amount previously `advance`d (#352). + +# 0.5.4 (January 23, 2020) + +### Added +- Make `Bytes::new` a `const fn`. +- Add `From` for `Bytes`. + +### Fix +- Fix reversed arguments in `PartialOrd` for `Bytes`. +- Fix `Bytes::truncate` losing original capacity when repr is an unshared `Vec`. +- Fix `Bytes::from(Vec)` when allocator gave `Vec` a pointer with LSB set. +- Fix panic in `Bytes::slice_ref` if argument is an empty slice. + +# 0.5.3 (December 12, 2019) + +### Added +- `must_use` attributes to `split`, `split_off`, and `split_to` methods (#337). + +### Fix +- Potential freeing of a null pointer in `Bytes` when constructed with an empty `Vec` (#341, #342). +- Calling `Bytes::truncate` with a size large than the length will no longer clear the `Bytes` (#333). + +# 0.5.2 (November 27, 2019) + +### Added +- `Limit` methods `into_inner`, `get_ref`, `get_mut`, `limit`, and `set_limit` (#325). + +# 0.5.1 (November 25, 2019) + +### Fix +- Growth documentation for `BytesMut` (#321) + +# 0.5.0 (November 25, 2019) + +### Fix +- Potential overflow in `copy_to_slice` + +### Changed +- Increased minimum supported Rust version to 1.39. +- `Bytes` is now a "trait object", allowing for custom allocation strategies (#298) +- `BytesMut` implicitly grows internal storage. `remaining_mut()` returns + `usize::MAX` (#316). +- `BufMut::bytes_mut` returns `&mut [MaybeUninit]` to reflect the unknown + initialization state (#305). +- `Buf` / `BufMut` implementations for `&[u8]` and `&mut [u8]` + respectively (#261). +- Move `Buf` / `BufMut` "extra" functions to an extension trait (#306). +- `BufMutExt::limit` (#309). +- `Bytes::slice` takes a `RangeBounds` argument (#265). +- `Bytes::from_static` is now a `const fn` (#311). +- A multitude of smaller performance optimizations. + +### Added +- `no_std` support (#281). +- `get_*`, `put_*`, `get_*_le`, and `put_*le` accessors for handling byte order. +- `BorrowMut` implementation for `BytesMut` (#185). + +### Removed +- `IntoBuf` (#288). +- `Buf` implementation for `&str` (#301). +- `byteorder` dependency (#280). +- `iovec` dependency, use `std::IoSlice` instead (#263). +- optional `either` dependency (#315). +- optional `i128` feature -- now available on stable. (#276). + +# 0.4.12 (March 6, 2019) + +### Added +- Implement `FromIterator<&'a u8>` for `BytesMut`/`Bytes` (#244). +- Implement `Buf` for `VecDeque` (#249). + +# 0.4.11 (November 17, 2018) + +* Use raw pointers for potentially racy loads (#233). +* Implement `BufRead` for `buf::Reader` (#232). +* Documentation tweaks (#234). + +# 0.4.10 (September 4, 2018) + +* impl `Buf` and `BufMut` for `Either` (#225). +* Add `Bytes::slice_ref` (#208). + +# 0.4.9 (July 12, 2018) + +* Add 128 bit number support behind a feature flag (#209). +* Implement `IntoBuf` for `&mut [u8]` + +# 0.4.8 (May 25, 2018) + +* Fix panic in `BytesMut` `FromIterator` implementation. +* Bytes: Recycle space when reserving space in vec mode (#197). +* Bytes: Add resize fn (#203). + +# 0.4.7 (April 27, 2018) + +* Make `Buf` and `BufMut` usable as trait objects (#186). +* impl BorrowMut for BytesMut (#185). +* Improve accessor performance (#195). + +# 0.4.6 (Janary 8, 2018) + +* Implement FromIterator for Bytes/BytesMut (#148). +* Add `advance` fn to Bytes/BytesMut (#166). +* Add `unsplit` fn to `BytesMut` (#162, #173). +* Improvements to Bytes split fns (#92). + +# 0.4.5 (August 12, 2017) + +* Fix range bug in `Take::bytes` +* Misc performance improvements +* Add extra `PartialEq` implementations. +* Add `Bytes::with_capacity` +* Implement `AsMut[u8]` for `BytesMut` + +# 0.4.4 (May 26, 2017) + +* Add serde support behind feature flag +* Add `extend_from_slice` on `Bytes` and `BytesMut` +* Add `truncate` and `clear` on `Bytes` +* Misc additional std trait implementations +* Misc performance improvements + +# 0.4.3 (April 30, 2017) + +* Fix Vec::advance_mut bug +* Bump minimum Rust version to 1.15 +* Misc performance tweaks + +# 0.4.2 (April 5, 2017) + +* Misc performance tweaks +* Improved `Debug` implementation for `Bytes` +* Avoid some incorrect assert panics + +# 0.4.1 (March 15, 2017) + +* Expose `buf` module and have most types available from there vs. root. +* Implement `IntoBuf` for `T: Buf`. +* Add `FromBuf` and `Buf::collect`. +* Add iterator adapter for `Buf`. +* Add scatter/gather support to `Buf` and `BufMut`. +* Add `Buf::chain`. +* Reduce allocations on repeated calls to `BytesMut::reserve`. +* Implement `Debug` for more types. +* Remove `Source` in favor of `IntoBuf`. +* Implement `Extend` for `BytesMut`. + + +# 0.4.0 (February 24, 2017) + +* Initial release diff --git a/third_party/rust/bytes/v1/crate/Cargo.toml b/third_party/rust/bytes/v1/crate/Cargo.toml new file mode 100644 index 000000000000..2c163aa0fcbf --- /dev/null +++ b/third_party/rust/bytes/v1/crate/Cargo.toml @@ -0,0 +1,54 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "bytes" +version = "1.5.0" +authors = [ + "Carl Lerche ", + "Sean McArthur ", +] +description = "Types and traits for working with bytes" +readme = "README.md" +keywords = [ + "buffers", + "zero-copy", + "io", +] +categories = [ + "network-programming", + "data-structures", +] +license = "MIT" +repository = "https://github.com/tokio-rs/bytes" + +[package.metadata.docs.rs] +rustdoc-args = [ + "--cfg", + "docsrs", +] + +[dependencies.serde] +version = "1.0.60" +features = ["alloc"] +optional = true +default-features = false + +[dev-dependencies.serde_test] +version = "1.0" + +[features] +default = ["std"] +std = [] + +[target."cfg(loom)".dev-dependencies.loom] +version = "0.5" diff --git a/third_party/rust/bytes/v1/crate/Cargo.toml.orig b/third_party/rust/bytes/v1/crate/Cargo.toml.orig new file mode 100644 index 000000000000..06b19e6712a0 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/Cargo.toml.orig @@ -0,0 +1,34 @@ +[package] + +name = "bytes" +# When releasing to crates.io: +# - Update CHANGELOG.md. +# - Create "v1.x.y" git tag. +version = "1.5.0" +license = "MIT" +authors = [ + "Carl Lerche ", + "Sean McArthur ", +] +description = "Types and traits for working with bytes" +repository = "https://github.com/tokio-rs/bytes" +readme = "README.md" +keywords = ["buffers", "zero-copy", "io"] +categories = ["network-programming", "data-structures"] +edition = "2018" + +[features] +default = ["std"] +std = [] + +[dependencies] +serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] } + +[dev-dependencies] +serde_test = "1.0" + +[target.'cfg(loom)'.dev-dependencies] +loom = "0.5" + +[package.metadata.docs.rs] +rustdoc-args = ["--cfg", "docsrs"] diff --git a/third_party/rust/bytes/v1/crate/LICENSE b/third_party/rust/bytes/v1/crate/LICENSE new file mode 100644 index 000000000000..58fb29a12384 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2018 Carl Lerche + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/bytes/v1/crate/README.md b/third_party/rust/bytes/v1/crate/README.md new file mode 100644 index 000000000000..be46642a4b83 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/README.md @@ -0,0 +1,56 @@ +# Bytes + +A utility library for working with bytes. + +[![Crates.io][crates-badge]][crates-url] +[![Build Status][ci-badge]][ci-url] + +[crates-badge]: https://img.shields.io/crates/v/bytes.svg +[crates-url]: https://crates.io/crates/bytes +[ci-badge]: https://github.com/tokio-rs/bytes/workflows/CI/badge.svg +[ci-url]: https://github.com/tokio-rs/bytes/actions + +[Documentation](https://docs.rs/bytes) + +## Usage + +To use `bytes`, first add this to your `Cargo.toml`: + +```toml +[dependencies] +bytes = "1" +``` + +Next, add this to your crate: + +```rust +use bytes::{Bytes, BytesMut, Buf, BufMut}; +``` + +## Serde support + +Serde support is optional and disabled by default. To enable use the feature `serde`. + +```toml +[dependencies] +bytes = { version = "1", features = ["serde"] } +``` + +## Building documentation + +When building the `bytes` documentation the `docsrs` option should be used, otherwise +feature gates will not be shown. This requires a nightly toolchain: + +``` +RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc +``` + +## License + +This project is licensed under the [MIT license](LICENSE). + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in `bytes` by you, shall be licensed as MIT, without any additional +terms or conditions. diff --git a/third_party/rust/bytes/v1/crate/benches/buf.rs b/third_party/rust/bytes/v1/crate/benches/buf.rs new file mode 100644 index 000000000000..616d18748839 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/benches/buf.rs @@ -0,0 +1,186 @@ +#![feature(test)] +#![warn(rust_2018_idioms)] + +extern crate test; + +use bytes::Buf; +use test::Bencher; + +/// Dummy Buf implementation +struct TestBuf { + buf: &'static [u8], + readlens: &'static [usize], + init_pos: usize, + pos: usize, + readlen_pos: usize, + readlen: usize, +} +impl TestBuf { + fn new(buf: &'static [u8], readlens: &'static [usize], init_pos: usize) -> TestBuf { + let mut buf = TestBuf { + buf, + readlens, + init_pos, + pos: 0, + readlen_pos: 0, + readlen: 0, + }; + buf.reset(); + buf + } + fn reset(&mut self) { + self.pos = self.init_pos; + self.readlen_pos = 0; + self.next_readlen(); + } + /// Compute the length of the next read : + /// - use the next value specified in readlens (capped by remaining) if any + /// - else the remaining + fn next_readlen(&mut self) { + self.readlen = self.buf.len() - self.pos; + if let Some(readlen) = self.readlens.get(self.readlen_pos) { + self.readlen = std::cmp::min(self.readlen, *readlen); + self.readlen_pos += 1; + } + } +} +impl Buf for TestBuf { + fn remaining(&self) -> usize { + self.buf.len() - self.pos + } + fn advance(&mut self, cnt: usize) { + self.pos += cnt; + assert!(self.pos <= self.buf.len()); + self.next_readlen(); + } + fn chunk(&self) -> &[u8] { + if self.readlen == 0 { + Default::default() + } else { + &self.buf[self.pos..self.pos + self.readlen] + } + } +} + +/// Dummy Buf implementation +/// version with methods forced to not be inlined (to simulate costly calls) +struct TestBufC { + inner: TestBuf, +} +impl TestBufC { + fn new(buf: &'static [u8], readlens: &'static [usize], init_pos: usize) -> TestBufC { + TestBufC { + inner: TestBuf::new(buf, readlens, init_pos), + } + } + fn reset(&mut self) { + self.inner.reset() + } +} +impl Buf for TestBufC { + #[inline(never)] + fn remaining(&self) -> usize { + self.inner.remaining() + } + #[inline(never)] + fn advance(&mut self, cnt: usize) { + self.inner.advance(cnt) + } + #[inline(never)] + fn chunk(&self) -> &[u8] { + self.inner.chunk() + } +} + +macro_rules! bench { + ($fname:ident, testbuf $testbuf:ident $readlens:expr, $method:ident $(,$arg:expr)*) => ( + #[bench] + fn $fname(b: &mut Bencher) { + let mut bufs = [ + $testbuf::new(&[1u8; 8+0], $readlens, 0), + $testbuf::new(&[1u8; 8+1], $readlens, 1), + $testbuf::new(&[1u8; 8+2], $readlens, 2), + $testbuf::new(&[1u8; 8+3], $readlens, 3), + $testbuf::new(&[1u8; 8+4], $readlens, 4), + $testbuf::new(&[1u8; 8+5], $readlens, 5), + $testbuf::new(&[1u8; 8+6], $readlens, 6), + $testbuf::new(&[1u8; 8+7], $readlens, 7), + ]; + b.iter(|| { + for i in 0..8 { + bufs[i].reset(); + let buf: &mut dyn Buf = &mut bufs[i]; // type erasure + test::black_box(buf.$method($($arg,)*)); + } + }) + } + ); + ($fname:ident, slice, $method:ident $(,$arg:expr)*) => ( + #[bench] + fn $fname(b: &mut Bencher) { + // buf must be long enough for one read of 8 bytes starting at pos 7 + let arr = [1u8; 8+7]; + b.iter(|| { + for i in 0..8 { + let mut buf = &arr[i..]; + let buf = &mut buf as &mut dyn Buf; // type erasure + test::black_box(buf.$method($($arg,)*)); + } + }) + } + ); + ($fname:ident, option) => ( + #[bench] + fn $fname(b: &mut Bencher) { + let data = [1u8; 1]; + b.iter(|| { + for _ in 0..8 { + let mut buf = Some(data); + let buf = &mut buf as &mut dyn Buf; // type erasure + test::black_box(buf.get_u8()); + } + }) + } + ); +} + +macro_rules! bench_group { + ($method:ident $(,$arg:expr)*) => ( + bench!(slice, slice, $method $(,$arg)*); + bench!(tbuf_1, testbuf TestBuf &[], $method $(,$arg)*); + bench!(tbuf_1_costly, testbuf TestBufC &[], $method $(,$arg)*); + bench!(tbuf_2, testbuf TestBuf &[1], $method $(,$arg)*); + bench!(tbuf_2_costly, testbuf TestBufC &[1], $method $(,$arg)*); + // bench!(tbuf_onebyone, testbuf TestBuf &[1,1,1,1,1,1,1,1], $method $(,$arg)*); + // bench!(tbuf_onebyone_costly, testbuf TestBufC &[1,1,1,1,1,1,1,1], $method $(,$arg)*); + ); +} + +mod get_u8 { + use super::*; + bench_group!(get_u8); +} +mod get_u16 { + use super::*; + bench_group!(get_u16); +} +mod get_u32 { + use super::*; + bench_group!(get_u32); +} +mod get_u64 { + use super::*; + bench_group!(get_u64); +} +mod get_f32 { + use super::*; + bench_group!(get_f32); +} +mod get_f64 { + use super::*; + bench_group!(get_f64); +} +mod get_uint24 { + use super::*; + bench_group!(get_uint, 3); +} diff --git a/third_party/rust/bytes/v1/crate/benches/bytes.rs b/third_party/rust/bytes/v1/crate/benches/bytes.rs new file mode 100644 index 000000000000..61d1e832a28b --- /dev/null +++ b/third_party/rust/bytes/v1/crate/benches/bytes.rs @@ -0,0 +1,120 @@ +#![feature(test)] +#![warn(rust_2018_idioms)] + +extern crate test; + +use bytes::Bytes; +use test::Bencher; + +#[bench] +fn deref_unique(b: &mut Bencher) { + let buf = Bytes::from(vec![0; 1024]); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_shared(b: &mut Bencher) { + let buf = Bytes::from(vec![0; 1024]); + let _b2 = buf.clone(); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_static(b: &mut Bencher) { + let buf = Bytes::from_static(b"hello world"); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn clone_static(b: &mut Bencher) { + let bytes = + Bytes::from_static("hello world 1234567890 and have a good byte 0987654321".as_bytes()); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&bytes.clone()); + } + }) +} + +#[bench] +fn clone_shared(b: &mut Bencher) { + let bytes = Bytes::from(b"hello world 1234567890 and have a good byte 0987654321".to_vec()); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&bytes.clone()); + } + }) +} + +#[bench] +fn clone_arc_vec(b: &mut Bencher) { + use std::sync::Arc; + let bytes = Arc::new(b"hello world 1234567890 and have a good byte 0987654321".to_vec()); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&bytes.clone()); + } + }) +} + +#[bench] +fn from_long_slice(b: &mut Bencher) { + let data = [0u8; 128]; + b.bytes = data.len() as u64; + b.iter(|| { + let buf = Bytes::copy_from_slice(&data[..]); + test::black_box(buf); + }) +} + +#[bench] +fn slice_empty(b: &mut Bencher) { + b.iter(|| { + // `clone` is to convert to ARC + let b = Bytes::from(vec![17; 1024]).clone(); + for i in 0..1000 { + test::black_box(b.slice(i % 100..i % 100)); + } + }) +} + +#[bench] +fn slice_short_from_arc(b: &mut Bencher) { + b.iter(|| { + // `clone` is to convert to ARC + let b = Bytes::from(vec![17; 1024]).clone(); + for i in 0..1000 { + test::black_box(b.slice(1..2 + i % 10)); + } + }) +} + +#[bench] +fn split_off_and_drop(b: &mut Bencher) { + b.iter(|| { + for _ in 0..1024 { + let v = vec![10; 200]; + let mut b = Bytes::from(v); + test::black_box(b.split_off(100)); + test::black_box(b); + } + }) +} diff --git a/third_party/rust/bytes/v1/crate/benches/bytes_mut.rs b/third_party/rust/bytes/v1/crate/benches/bytes_mut.rs new file mode 100644 index 000000000000..b06943621005 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/benches/bytes_mut.rs @@ -0,0 +1,266 @@ +#![feature(test)] +#![warn(rust_2018_idioms)] + +extern crate test; + +use bytes::{BufMut, BytesMut}; +use test::Bencher; + +#[bench] +fn alloc_small(b: &mut Bencher) { + b.iter(|| { + for _ in 0..1024 { + test::black_box(BytesMut::with_capacity(12)); + } + }) +} + +#[bench] +fn alloc_mid(b: &mut Bencher) { + b.iter(|| { + test::black_box(BytesMut::with_capacity(128)); + }) +} + +#[bench] +fn alloc_big(b: &mut Bencher) { + b.iter(|| { + test::black_box(BytesMut::with_capacity(4096)); + }) +} + +#[bench] +fn deref_unique(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(4096); + buf.put(&[0u8; 1024][..]); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_unique_unroll(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(4096); + buf.put(&[0u8; 1024][..]); + + b.iter(|| { + for _ in 0..128 { + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_shared(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(4096); + buf.put(&[0u8; 1024][..]); + let _b2 = buf.split_off(1024); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_two(b: &mut Bencher) { + let mut buf1 = BytesMut::with_capacity(8); + buf1.put(&[0u8; 8][..]); + + let mut buf2 = BytesMut::with_capacity(4096); + buf2.put(&[0u8; 1024][..]); + + b.iter(|| { + for _ in 0..512 { + test::black_box(&buf1[..]); + test::black_box(&buf2[..]); + } + }) +} + +#[bench] +fn clone_frozen(b: &mut Bencher) { + let bytes = BytesMut::from(&b"hello world 1234567890 and have a good byte 0987654321"[..]) + .split() + .freeze(); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&bytes.clone()); + } + }) +} + +#[bench] +fn alloc_write_split_to_mid(b: &mut Bencher) { + b.iter(|| { + let mut buf = BytesMut::with_capacity(128); + buf.put_slice(&[0u8; 64]); + test::black_box(buf.split_to(64)); + }) +} + +#[bench] +fn drain_write_drain(b: &mut Bencher) { + let data = [0u8; 128]; + + b.iter(|| { + let mut buf = BytesMut::with_capacity(1024); + let mut parts = Vec::with_capacity(8); + + for _ in 0..8 { + buf.put(&data[..]); + parts.push(buf.split_to(128)); + } + + test::black_box(parts); + }) +} + +#[bench] +fn fmt_write(b: &mut Bencher) { + use std::fmt::Write; + let mut buf = BytesMut::with_capacity(128); + let s = "foo bar baz quux lorem ipsum dolor et"; + + b.bytes = s.len() as u64; + b.iter(|| { + let _ = write!(buf, "{}", s); + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }) +} + +#[bench] +fn bytes_mut_extend(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(256); + let data = [33u8; 32]; + + b.bytes = data.len() as u64 * 4; + b.iter(|| { + for _ in 0..4 { + buf.extend(&data); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} + +// BufMut for BytesMut vs Vec + +#[bench] +fn put_slice_bytes_mut(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(256); + let data = [33u8; 32]; + + b.bytes = data.len() as u64 * 4; + b.iter(|| { + for _ in 0..4 { + buf.put_slice(&data); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} + +#[bench] +fn put_u8_bytes_mut(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(256); + let cnt = 128; + + b.bytes = cnt as u64; + b.iter(|| { + for _ in 0..cnt { + buf.put_u8(b'x'); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} + +#[bench] +fn put_slice_vec(b: &mut Bencher) { + let mut buf = Vec::::with_capacity(256); + let data = [33u8; 32]; + + b.bytes = data.len() as u64 * 4; + b.iter(|| { + for _ in 0..4 { + buf.put_slice(&data); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} + +#[bench] +fn put_u8_vec(b: &mut Bencher) { + let mut buf = Vec::::with_capacity(256); + let cnt = 128; + + b.bytes = cnt as u64; + b.iter(|| { + for _ in 0..cnt { + buf.put_u8(b'x'); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} + +#[bench] +fn put_slice_vec_extend(b: &mut Bencher) { + let mut buf = Vec::::with_capacity(256); + let data = [33u8; 32]; + + b.bytes = data.len() as u64 * 4; + b.iter(|| { + for _ in 0..4 { + buf.extend_from_slice(&data); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} + +#[bench] +fn put_u8_vec_push(b: &mut Bencher) { + let mut buf = Vec::::with_capacity(256); + let cnt = 128; + + b.bytes = cnt as u64; + b.iter(|| { + for _ in 0..cnt { + buf.push(b'x'); + } + test::black_box(&buf); + unsafe { + buf.set_len(0); + } + }); +} diff --git a/third_party/rust/bytes/v1/crate/ci/miri.sh b/third_party/rust/bytes/v1/crate/ci/miri.sh new file mode 100755 index 000000000000..0158756cd396 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/ci/miri.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +rustup toolchain install nightly --component miri +rustup override set nightly +cargo miri setup + +export MIRIFLAGS="-Zmiri-strict-provenance" + +cargo miri test +cargo miri test --target mips64-unknown-linux-gnuabi64 diff --git a/third_party/rust/bytes/v1/crate/ci/test-stable.sh b/third_party/rust/bytes/v1/crate/ci/test-stable.sh new file mode 100644 index 000000000000..4421f3a97903 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/ci/test-stable.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -ex + +cmd="${1:-test}" + +# Install cargo-hack for feature flag test +host=$(rustc -Vv | grep host | sed 's/host: //') +curl -LsSf https://github.com/taiki-e/cargo-hack/releases/latest/download/cargo-hack-$host.tar.gz | tar xzf - -C ~/.cargo/bin + +# Run with each feature +# * --each-feature includes both default/no-default features +# * --optional-deps is needed for serde feature +cargo hack "${cmd}" --each-feature --optional-deps +# Run with all features +cargo "${cmd}" --all-features + +cargo doc --no-deps --all-features + +if [[ "${RUST_VERSION}" == "nightly"* ]]; then + # Check benchmarks + cargo check --benches + + # Check minimal versions + cargo clean + cargo update -Zminimal-versions + cargo check --all-features +fi diff --git a/third_party/rust/bytes/v1/crate/ci/tsan.sh b/third_party/rust/bytes/v1/crate/ci/tsan.sh new file mode 100644 index 000000000000..ca520bd7fd04 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/ci/tsan.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -ex + +export ASAN_OPTIONS="detect_odr_violation=0 detect_leaks=0" + +# Run address sanitizer +RUSTFLAGS="-Z sanitizer=address" \ +cargo test --target x86_64-unknown-linux-gnu --test test_bytes --test test_buf --test test_buf_mut + +# Run thread sanitizer +RUSTFLAGS="-Z sanitizer=thread" \ +cargo -Zbuild-std test --target x86_64-unknown-linux-gnu --test test_bytes --test test_buf --test test_buf_mut diff --git a/third_party/rust/bytes/v1/crate/clippy.toml b/third_party/rust/bytes/v1/crate/clippy.toml new file mode 100644 index 000000000000..53095b15d981 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/clippy.toml @@ -0,0 +1 @@ +msrv = "1.39" diff --git a/third_party/rust/bytes/v1/crate/src/buf/buf_impl.rs b/third_party/rust/bytes/v1/crate/src/buf/buf_impl.rs new file mode 100644 index 000000000000..366cfc98982b --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/buf_impl.rs @@ -0,0 +1,1394 @@ +#[cfg(feature = "std")] +use crate::buf::{reader, Reader}; +use crate::buf::{take, Chain, Take}; + +use core::{cmp, mem, ptr}; + +#[cfg(feature = "std")] +use std::io::IoSlice; + +use alloc::boxed::Box; + +macro_rules! buf_get_impl { + ($this:ident, $typ:tt::$conv:tt) => {{ + const SIZE: usize = mem::size_of::<$typ>(); + // try to convert directly from the bytes + // this Option trick is to avoid keeping a borrow on self + // when advance() is called (mut borrow) and to call bytes() only once + let ret = $this + .chunk() + .get(..SIZE) + .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) }); + + if let Some(ret) = ret { + // if the direct conversion was possible, advance and return + $this.advance(SIZE); + return ret; + } else { + // if not we copy the bytes in a temp buffer then convert + let mut buf = [0; SIZE]; + $this.copy_to_slice(&mut buf); // (do the advance) + return $typ::$conv(buf); + } + }}; + (le => $this:ident, $typ:tt, $len_to_read:expr) => {{ + debug_assert!(mem::size_of::<$typ>() >= $len_to_read); + + // The same trick as above does not improve the best case speed. + // It seems to be linked to the way the method is optimised by the compiler + let mut buf = [0; (mem::size_of::<$typ>())]; + $this.copy_to_slice(&mut buf[..($len_to_read)]); + return $typ::from_le_bytes(buf); + }}; + (be => $this:ident, $typ:tt, $len_to_read:expr) => {{ + debug_assert!(mem::size_of::<$typ>() >= $len_to_read); + + let mut buf = [0; (mem::size_of::<$typ>())]; + $this.copy_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]); + return $typ::from_be_bytes(buf); + }}; +} + +/// Read bytes from a buffer. +/// +/// A buffer stores bytes in memory such that read operations are infallible. +/// The underlying storage may or may not be in contiguous memory. A `Buf` value +/// is a cursor into the buffer. Reading from `Buf` advances the cursor +/// position. It can be thought of as an efficient `Iterator` for collections of +/// bytes. +/// +/// The simplest `Buf` is a `&[u8]`. +/// +/// ``` +/// use bytes::Buf; +/// +/// let mut buf = &b"hello world"[..]; +/// +/// assert_eq!(b'h', buf.get_u8()); +/// assert_eq!(b'e', buf.get_u8()); +/// assert_eq!(b'l', buf.get_u8()); +/// +/// let mut rest = [0; 8]; +/// buf.copy_to_slice(&mut rest); +/// +/// assert_eq!(&rest[..], &b"lo world"[..]); +/// ``` +pub trait Buf { + /// Returns the number of bytes between the current position and the end of + /// the buffer. + /// + /// This value is greater than or equal to the length of the slice returned + /// by `chunk()`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"hello world"[..]; + /// + /// assert_eq!(buf.remaining(), 11); + /// + /// buf.get_u8(); + /// + /// assert_eq!(buf.remaining(), 10); + /// ``` + /// + /// # Implementer notes + /// + /// Implementations of `remaining` should ensure that the return value does + /// not change unless a call is made to `advance` or any other function that + /// is documented to change the `Buf`'s current position. + fn remaining(&self) -> usize; + + /// Returns a slice starting at the current position and of length between 0 + /// and `Buf::remaining()`. Note that this *can* return shorter slice (this allows + /// non-continuous internal representation). + /// + /// This is a lower level function. Most operations are done with other + /// functions. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"hello world"[..]; + /// + /// assert_eq!(buf.chunk(), &b"hello world"[..]); + /// + /// buf.advance(6); + /// + /// assert_eq!(buf.chunk(), &b"world"[..]); + /// ``` + /// + /// # Implementer notes + /// + /// This function should never panic. Once the end of the buffer is reached, + /// i.e., `Buf::remaining` returns 0, calls to `chunk()` should return an + /// empty slice. + // The `chunk` method was previously called `bytes`. This alias makes the rename + // more easily discoverable. + #[cfg_attr(docsrs, doc(alias = "bytes"))] + fn chunk(&self) -> &[u8]; + + /// Fills `dst` with potentially multiple slices starting at `self`'s + /// current position. + /// + /// If the `Buf` is backed by disjoint slices of bytes, `chunk_vectored` enables + /// fetching more than one slice at once. `dst` is a slice of `IoSlice` + /// references, enabling the slice to be directly used with [`writev`] + /// without any further conversion. The sum of the lengths of all the + /// buffers in `dst` will be less than or equal to `Buf::remaining()`. + /// + /// The entries in `dst` will be overwritten, but the data **contained** by + /// the slices **will not** be modified. If `chunk_vectored` does not fill every + /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices + /// in `self. + /// + /// This is a lower level function. Most operations are done with other + /// functions. + /// + /// # Implementer notes + /// + /// This function should never panic. Once the end of the buffer is reached, + /// i.e., `Buf::remaining` returns 0, calls to `chunk_vectored` must return 0 + /// without mutating `dst`. + /// + /// Implementations should also take care to properly handle being called + /// with `dst` being a zero length slice. + /// + /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { + if dst.is_empty() { + return 0; + } + + if self.has_remaining() { + dst[0] = IoSlice::new(self.chunk()); + 1 + } else { + 0 + } + } + + /// Advance the internal cursor of the Buf + /// + /// The next call to `chunk()` will return a slice starting `cnt` bytes + /// further into the underlying buffer. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"hello world"[..]; + /// + /// assert_eq!(buf.chunk(), &b"hello world"[..]); + /// + /// buf.advance(6); + /// + /// assert_eq!(buf.chunk(), &b"world"[..]); + /// ``` + /// + /// # Panics + /// + /// This function **may** panic if `cnt > self.remaining()`. + /// + /// # Implementer notes + /// + /// It is recommended for implementations of `advance` to panic if `cnt > + /// self.remaining()`. If the implementation does not panic, the call must + /// behave as if `cnt == self.remaining()`. + /// + /// A call with `cnt == 0` should never panic and be a no-op. + fn advance(&mut self, cnt: usize); + + /// Returns true if there are any more bytes to consume + /// + /// This is equivalent to `self.remaining() != 0`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"a"[..]; + /// + /// assert!(buf.has_remaining()); + /// + /// buf.get_u8(); + /// + /// assert!(!buf.has_remaining()); + /// ``` + fn has_remaining(&self) -> bool { + self.remaining() > 0 + } + + /// Copies bytes from `self` into `dst`. + /// + /// The cursor is advanced by the number of bytes copied. `self` must have + /// enough remaining bytes to fill `dst`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"hello world"[..]; + /// let mut dst = [0; 5]; + /// + /// buf.copy_to_slice(&mut dst); + /// assert_eq!(&b"hello"[..], &dst); + /// assert_eq!(6, buf.remaining()); + /// ``` + /// + /// # Panics + /// + /// This function panics if `self.remaining() < dst.len()` + fn copy_to_slice(&mut self, dst: &mut [u8]) { + let mut off = 0; + + assert!(self.remaining() >= dst.len()); + + while off < dst.len() { + let cnt; + + unsafe { + let src = self.chunk(); + cnt = cmp::min(src.len(), dst.len() - off); + + ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt); + + off += cnt; + } + + self.advance(cnt); + } + } + + /// Gets an unsigned 8 bit integer from `self`. + /// + /// The current position is advanced by 1. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08 hello"[..]; + /// assert_eq!(8, buf.get_u8()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is no more remaining data in `self`. + fn get_u8(&mut self) -> u8 { + assert!(self.remaining() >= 1); + let ret = self.chunk()[0]; + self.advance(1); + ret + } + + /// Gets a signed 8 bit integer from `self`. + /// + /// The current position is advanced by 1. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08 hello"[..]; + /// assert_eq!(8, buf.get_i8()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is no more remaining data in `self`. + fn get_i8(&mut self) -> i8 { + assert!(self.remaining() >= 1); + let ret = self.chunk()[0] as i8; + self.advance(1); + ret + } + + /// Gets an unsigned 16 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09 hello"[..]; + /// assert_eq!(0x0809, buf.get_u16()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u16(&mut self) -> u16 { + buf_get_impl!(self, u16::from_be_bytes); + } + + /// Gets an unsigned 16 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x09\x08 hello"[..]; + /// assert_eq!(0x0809, buf.get_u16_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u16_le(&mut self) -> u16 { + buf_get_impl!(self, u16::from_le_bytes); + } + + /// Gets an unsigned 16 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09 hello", + /// false => b"\x09\x08 hello", + /// }; + /// assert_eq!(0x0809, buf.get_u16_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u16_ne(&mut self) -> u16 { + buf_get_impl!(self, u16::from_ne_bytes); + } + + /// Gets a signed 16 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09 hello"[..]; + /// assert_eq!(0x0809, buf.get_i16()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i16(&mut self) -> i16 { + buf_get_impl!(self, i16::from_be_bytes); + } + + /// Gets a signed 16 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x09\x08 hello"[..]; + /// assert_eq!(0x0809, buf.get_i16_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i16_le(&mut self) -> i16 { + buf_get_impl!(self, i16::from_le_bytes); + } + + /// Gets a signed 16 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09 hello", + /// false => b"\x09\x08 hello", + /// }; + /// assert_eq!(0x0809, buf.get_i16_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i16_ne(&mut self) -> i16 { + buf_get_impl!(self, i16::from_ne_bytes); + } + + /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; + /// assert_eq!(0x0809A0A1, buf.get_u32()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u32(&mut self) -> u32 { + buf_get_impl!(self, u32::from_be_bytes); + } + + /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; + /// assert_eq!(0x0809A0A1, buf.get_u32_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u32_le(&mut self) -> u32 { + buf_get_impl!(self, u32::from_le_bytes); + } + + /// Gets an unsigned 32 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09\xA0\xA1 hello", + /// false => b"\xA1\xA0\x09\x08 hello", + /// }; + /// assert_eq!(0x0809A0A1, buf.get_u32_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u32_ne(&mut self) -> u32 { + buf_get_impl!(self, u32::from_ne_bytes); + } + + /// Gets a signed 32 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; + /// assert_eq!(0x0809A0A1, buf.get_i32()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i32(&mut self) -> i32 { + buf_get_impl!(self, i32::from_be_bytes); + } + + /// Gets a signed 32 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; + /// assert_eq!(0x0809A0A1, buf.get_i32_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i32_le(&mut self) -> i32 { + buf_get_impl!(self, i32::from_le_bytes); + } + + /// Gets a signed 32 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x08\x09\xA0\xA1 hello", + /// false => b"\xA1\xA0\x09\x08 hello", + /// }; + /// assert_eq!(0x0809A0A1, buf.get_i32_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i32_ne(&mut self) -> i32 { + buf_get_impl!(self, i32::from_ne_bytes); + } + + /// Gets an unsigned 64 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; + /// assert_eq!(0x0102030405060708, buf.get_u64()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u64(&mut self) -> u64 { + buf_get_impl!(self, u64::from_be_bytes); + } + + /// Gets an unsigned 64 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(0x0102030405060708, buf.get_u64_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u64_le(&mut self) -> u64 { + buf_get_impl!(self, u64::from_le_bytes); + } + + /// Gets an unsigned 64 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", + /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(0x0102030405060708, buf.get_u64_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u64_ne(&mut self) -> u64 { + buf_get_impl!(self, u64::from_ne_bytes); + } + + /// Gets a signed 64 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; + /// assert_eq!(0x0102030405060708, buf.get_i64()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i64(&mut self) -> i64 { + buf_get_impl!(self, i64::from_be_bytes); + } + + /// Gets a signed 64 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(0x0102030405060708, buf.get_i64_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i64_le(&mut self) -> i64 { + buf_get_impl!(self, i64::from_le_bytes); + } + + /// Gets a signed 64 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", + /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(0x0102030405060708, buf.get_i64_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i64_ne(&mut self) -> i64 { + buf_get_impl!(self, i64::from_ne_bytes); + } + + /// Gets an unsigned 128 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; + /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u128(&mut self) -> u128 { + buf_get_impl!(self, u128::from_be_bytes); + } + + /// Gets an unsigned 128 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u128_le(&mut self) -> u128 { + buf_get_impl!(self, u128::from_le_bytes); + } + + /// Gets an unsigned 128 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", + /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u128_ne(&mut self) -> u128 { + buf_get_impl!(self, u128::from_ne_bytes); + } + + /// Gets a signed 128 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; + /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i128(&mut self) -> i128 { + buf_get_impl!(self, i128::from_be_bytes); + } + + /// Gets a signed 128 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i128_le(&mut self) -> i128 { + buf_get_impl!(self, i128::from_le_bytes); + } + + /// Gets a signed 128 bit integer from `self` in native-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", + /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", + /// }; + /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i128_ne(&mut self) -> i128 { + buf_get_impl!(self, i128::from_ne_bytes); + } + + /// Gets an unsigned n-byte integer from `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03 hello"[..]; + /// assert_eq!(0x010203, buf.get_uint(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_uint(&mut self, nbytes: usize) -> u64 { + buf_get_impl!(be => self, u64, nbytes); + } + + /// Gets an unsigned n-byte integer from `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x03\x02\x01 hello"[..]; + /// assert_eq!(0x010203, buf.get_uint_le(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_uint_le(&mut self, nbytes: usize) -> u64 { + buf_get_impl!(le => self, u64, nbytes); + } + + /// Gets an unsigned n-byte integer from `self` in native-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03 hello", + /// false => b"\x03\x02\x01 hello", + /// }; + /// assert_eq!(0x010203, buf.get_uint_ne(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_uint_ne(&mut self, nbytes: usize) -> u64 { + if cfg!(target_endian = "big") { + self.get_uint(nbytes) + } else { + self.get_uint_le(nbytes) + } + } + + /// Gets a signed n-byte integer from `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x01\x02\x03 hello"[..]; + /// assert_eq!(0x010203, buf.get_int(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_int(&mut self, nbytes: usize) -> i64 { + buf_get_impl!(be => self, i64, nbytes); + } + + /// Gets a signed n-byte integer from `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x03\x02\x01 hello"[..]; + /// assert_eq!(0x010203, buf.get_int_le(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_int_le(&mut self, nbytes: usize) -> i64 { + buf_get_impl!(le => self, i64, nbytes); + } + + /// Gets a signed n-byte integer from `self` in native-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x01\x02\x03 hello", + /// false => b"\x03\x02\x01 hello", + /// }; + /// assert_eq!(0x010203, buf.get_int_ne(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_int_ne(&mut self, nbytes: usize) -> i64 { + if cfg!(target_endian = "big") { + self.get_int(nbytes) + } else { + self.get_int_le(nbytes) + } + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..]; + /// assert_eq!(1.2f32, buf.get_f32()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f32(&mut self) -> f32 { + f32::from_bits(Self::get_u32(self)) + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..]; + /// assert_eq!(1.2f32, buf.get_f32_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f32_le(&mut self) -> f32 { + f32::from_bits(Self::get_u32_le(self)) + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x3F\x99\x99\x9A hello", + /// false => b"\x9A\x99\x99\x3F hello", + /// }; + /// assert_eq!(1.2f32, buf.get_f32_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f32_ne(&mut self) -> f32 { + f32::from_bits(Self::get_u32_ne(self)) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..]; + /// assert_eq!(1.2f64, buf.get_f64()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f64(&mut self) -> f64 { + f64::from_bits(Self::get_u64(self)) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..]; + /// assert_eq!(1.2f64, buf.get_f64_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f64_le(&mut self) -> f64 { + f64::from_bits(Self::get_u64_le(self)) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf: &[u8] = match cfg!(target_endian = "big") { + /// true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello", + /// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello", + /// }; + /// assert_eq!(1.2f64, buf.get_f64_ne()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f64_ne(&mut self) -> f64 { + f64::from_bits(Self::get_u64_ne(self)) + } + + /// Consumes `len` bytes inside self and returns new instance of `Bytes` + /// with this data. + /// + /// This function may be optimized by the underlying type to avoid actual + /// copies. For example, `Bytes` implementation will do a shallow copy + /// (ref-count increment). + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let bytes = (&b"hello world"[..]).copy_to_bytes(5); + /// assert_eq!(&bytes[..], &b"hello"[..]); + /// ``` + fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { + use super::BufMut; + + assert!(len <= self.remaining(), "`len` greater than remaining"); + + let mut ret = crate::BytesMut::with_capacity(len); + ret.put(self.take(len)); + ret.freeze() + } + + /// Creates an adaptor which will read at most `limit` bytes from `self`. + /// + /// This function returns a new instance of `Buf` which will read at most + /// `limit` bytes. + /// + /// # Examples + /// + /// ``` + /// use bytes::{Buf, BufMut}; + /// + /// let mut buf = b"hello world"[..].take(5); + /// let mut dst = vec![]; + /// + /// dst.put(&mut buf); + /// assert_eq!(dst, b"hello"); + /// + /// let mut buf = buf.into_inner(); + /// dst.clear(); + /// dst.put(&mut buf); + /// assert_eq!(dst, b" world"); + /// ``` + fn take(self, limit: usize) -> Take + where + Self: Sized, + { + take::new(self, limit) + } + + /// Creates an adaptor which will chain this buffer with another. + /// + /// The returned `Buf` instance will first consume all bytes from `self`. + /// Afterwards the output is equivalent to the output of next. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut chain = b"hello "[..].chain(&b"world"[..]); + /// + /// let full = chain.copy_to_bytes(11); + /// assert_eq!(full.chunk(), b"hello world"); + /// ``` + fn chain(self, next: U) -> Chain + where + Self: Sized, + { + Chain::new(self, next) + } + + /// Creates an adaptor which implements the `Read` trait for `self`. + /// + /// This function returns a new value which implements `Read` by adapting + /// the `Read` trait functions to the `Buf` trait functions. Given that + /// `Buf` operations are infallible, none of the `Read` functions will + /// return with `Err`. + /// + /// # Examples + /// + /// ``` + /// use bytes::{Bytes, Buf}; + /// use std::io::Read; + /// + /// let buf = Bytes::from("hello world"); + /// + /// let mut reader = buf.reader(); + /// let mut dst = [0; 1024]; + /// + /// let num = reader.read(&mut dst).unwrap(); + /// + /// assert_eq!(11, num); + /// assert_eq!(&dst[..11], &b"hello world"[..]); + /// ``` + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn reader(self) -> Reader + where + Self: Sized, + { + reader::new(self) + } +} + +macro_rules! deref_forward_buf { + () => { + fn remaining(&self) -> usize { + (**self).remaining() + } + + fn chunk(&self) -> &[u8] { + (**self).chunk() + } + + #[cfg(feature = "std")] + fn chunks_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize { + (**self).chunks_vectored(dst) + } + + fn advance(&mut self, cnt: usize) { + (**self).advance(cnt) + } + + fn has_remaining(&self) -> bool { + (**self).has_remaining() + } + + fn copy_to_slice(&mut self, dst: &mut [u8]) { + (**self).copy_to_slice(dst) + } + + fn get_u8(&mut self) -> u8 { + (**self).get_u8() + } + + fn get_i8(&mut self) -> i8 { + (**self).get_i8() + } + + fn get_u16(&mut self) -> u16 { + (**self).get_u16() + } + + fn get_u16_le(&mut self) -> u16 { + (**self).get_u16_le() + } + + fn get_u16_ne(&mut self) -> u16 { + (**self).get_u16_ne() + } + + fn get_i16(&mut self) -> i16 { + (**self).get_i16() + } + + fn get_i16_le(&mut self) -> i16 { + (**self).get_i16_le() + } + + fn get_i16_ne(&mut self) -> i16 { + (**self).get_i16_ne() + } + + fn get_u32(&mut self) -> u32 { + (**self).get_u32() + } + + fn get_u32_le(&mut self) -> u32 { + (**self).get_u32_le() + } + + fn get_u32_ne(&mut self) -> u32 { + (**self).get_u32_ne() + } + + fn get_i32(&mut self) -> i32 { + (**self).get_i32() + } + + fn get_i32_le(&mut self) -> i32 { + (**self).get_i32_le() + } + + fn get_i32_ne(&mut self) -> i32 { + (**self).get_i32_ne() + } + + fn get_u64(&mut self) -> u64 { + (**self).get_u64() + } + + fn get_u64_le(&mut self) -> u64 { + (**self).get_u64_le() + } + + fn get_u64_ne(&mut self) -> u64 { + (**self).get_u64_ne() + } + + fn get_i64(&mut self) -> i64 { + (**self).get_i64() + } + + fn get_i64_le(&mut self) -> i64 { + (**self).get_i64_le() + } + + fn get_i64_ne(&mut self) -> i64 { + (**self).get_i64_ne() + } + + fn get_uint(&mut self, nbytes: usize) -> u64 { + (**self).get_uint(nbytes) + } + + fn get_uint_le(&mut self, nbytes: usize) -> u64 { + (**self).get_uint_le(nbytes) + } + + fn get_uint_ne(&mut self, nbytes: usize) -> u64 { + (**self).get_uint_ne(nbytes) + } + + fn get_int(&mut self, nbytes: usize) -> i64 { + (**self).get_int(nbytes) + } + + fn get_int_le(&mut self, nbytes: usize) -> i64 { + (**self).get_int_le(nbytes) + } + + fn get_int_ne(&mut self, nbytes: usize) -> i64 { + (**self).get_int_ne(nbytes) + } + + fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { + (**self).copy_to_bytes(len) + } + }; +} + +impl Buf for &mut T { + deref_forward_buf!(); +} + +impl Buf for Box { + deref_forward_buf!(); +} + +impl Buf for &[u8] { + #[inline] + fn remaining(&self) -> usize { + self.len() + } + + #[inline] + fn chunk(&self) -> &[u8] { + self + } + + #[inline] + fn advance(&mut self, cnt: usize) { + *self = &self[cnt..]; + } +} + +#[cfg(feature = "std")] +impl> Buf for std::io::Cursor { + fn remaining(&self) -> usize { + let len = self.get_ref().as_ref().len(); + let pos = self.position(); + + if pos >= len as u64 { + return 0; + } + + len - pos as usize + } + + fn chunk(&self) -> &[u8] { + let len = self.get_ref().as_ref().len(); + let pos = self.position(); + + if pos >= len as u64 { + return &[]; + } + + &self.get_ref().as_ref()[pos as usize..] + } + + fn advance(&mut self, cnt: usize) { + let pos = (self.position() as usize) + .checked_add(cnt) + .expect("overflow"); + + assert!(pos <= self.get_ref().as_ref().len()); + self.set_position(pos as u64); + } +} + +// The existence of this function makes the compiler catch if the Buf +// trait is "object-safe" or not. +fn _assert_trait_object(_b: &dyn Buf) {} diff --git a/third_party/rust/bytes/v1/crate/src/buf/buf_mut.rs b/third_party/rust/bytes/v1/crate/src/buf/buf_mut.rs new file mode 100644 index 000000000000..c2ac39df34b6 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/buf_mut.rs @@ -0,0 +1,1528 @@ +use crate::buf::{limit, Chain, Limit, UninitSlice}; +#[cfg(feature = "std")] +use crate::buf::{writer, Writer}; + +use core::{cmp, mem, ptr, usize}; + +use alloc::{boxed::Box, vec::Vec}; + +/// A trait for values that provide sequential write access to bytes. +/// +/// Write bytes to a buffer +/// +/// A buffer stores bytes in memory such that write operations are infallible. +/// The underlying storage may or may not be in contiguous memory. A `BufMut` +/// value is a cursor into the buffer. Writing to `BufMut` advances the cursor +/// position. +/// +/// The simplest `BufMut` is a `Vec`. +/// +/// ``` +/// use bytes::BufMut; +/// +/// let mut buf = vec![]; +/// +/// buf.put(&b"hello world"[..]); +/// +/// assert_eq!(buf, b"hello world"); +/// ``` +pub unsafe trait BufMut { + /// Returns the number of bytes that can be written from the current + /// position until the end of the buffer is reached. + /// + /// This value is greater than or equal to the length of the slice returned + /// by `chunk_mut()`. + /// + /// Writing to a `BufMut` may involve allocating more memory on the fly. + /// Implementations may fail before reaching the number of bytes indicated + /// by this method if they encounter an allocation failure. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut dst = [0; 10]; + /// let mut buf = &mut dst[..]; + /// + /// let original_remaining = buf.remaining_mut(); + /// buf.put(&b"hello"[..]); + /// + /// assert_eq!(original_remaining - 5, buf.remaining_mut()); + /// ``` + /// + /// # Implementer notes + /// + /// Implementations of `remaining_mut` should ensure that the return value + /// does not change unless a call is made to `advance_mut` or any other + /// function that is documented to change the `BufMut`'s current position. + /// + /// # Note + /// + /// `remaining_mut` may return value smaller than actual available space. + fn remaining_mut(&self) -> usize; + + /// Advance the internal cursor of the BufMut + /// + /// The next call to `chunk_mut` will return a slice starting `cnt` bytes + /// further into the underlying buffer. + /// + /// This function is unsafe because there is no guarantee that the bytes + /// being advanced past have been initialized. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = Vec::with_capacity(16); + /// + /// // Write some data + /// buf.chunk_mut()[0..2].copy_from_slice(b"he"); + /// unsafe { buf.advance_mut(2) }; + /// + /// // write more bytes + /// buf.chunk_mut()[0..3].copy_from_slice(b"llo"); + /// + /// unsafe { buf.advance_mut(3); } + /// + /// assert_eq!(5, buf.len()); + /// assert_eq!(buf, b"hello"); + /// ``` + /// + /// # Panics + /// + /// This function **may** panic if `cnt > self.remaining_mut()`. + /// + /// # Implementer notes + /// + /// It is recommended for implementations of `advance_mut` to panic if + /// `cnt > self.remaining_mut()`. If the implementation does not panic, + /// the call must behave as if `cnt == self.remaining_mut()`. + /// + /// A call with `cnt == 0` should never panic and be a no-op. + unsafe fn advance_mut(&mut self, cnt: usize); + + /// Returns true if there is space in `self` for more bytes. + /// + /// This is equivalent to `self.remaining_mut() != 0`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut dst = [0; 5]; + /// let mut buf = &mut dst[..]; + /// + /// assert!(buf.has_remaining_mut()); + /// + /// buf.put(&b"hello"[..]); + /// + /// assert!(!buf.has_remaining_mut()); + /// ``` + fn has_remaining_mut(&self) -> bool { + self.remaining_mut() > 0 + } + + /// Returns a mutable slice starting at the current BufMut position and of + /// length between 0 and `BufMut::remaining_mut()`. Note that this *can* be shorter than the + /// whole remainder of the buffer (this allows non-continuous implementation). + /// + /// This is a lower level function. Most operations are done with other + /// functions. + /// + /// The returned byte slice may represent uninitialized memory. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = Vec::with_capacity(16); + /// + /// unsafe { + /// // MaybeUninit::as_mut_ptr + /// buf.chunk_mut()[0..].as_mut_ptr().write(b'h'); + /// buf.chunk_mut()[1..].as_mut_ptr().write(b'e'); + /// + /// buf.advance_mut(2); + /// + /// buf.chunk_mut()[0..].as_mut_ptr().write(b'l'); + /// buf.chunk_mut()[1..].as_mut_ptr().write(b'l'); + /// buf.chunk_mut()[2..].as_mut_ptr().write(b'o'); + /// + /// buf.advance_mut(3); + /// } + /// + /// assert_eq!(5, buf.len()); + /// assert_eq!(buf, b"hello"); + /// ``` + /// + /// # Implementer notes + /// + /// This function should never panic. `chunk_mut` should return an empty + /// slice **if and only if** `remaining_mut()` returns 0. In other words, + /// `chunk_mut()` returning an empty slice implies that `remaining_mut()` will + /// return 0 and `remaining_mut()` returning 0 implies that `chunk_mut()` will + /// return an empty slice. + /// + /// This function may trigger an out-of-memory abort if it tries to allocate + /// memory and fails to do so. + // The `chunk_mut` method was previously called `bytes_mut`. This alias makes the + // rename more easily discoverable. + #[cfg_attr(docsrs, doc(alias = "bytes_mut"))] + fn chunk_mut(&mut self) -> &mut UninitSlice; + + /// Transfer bytes into `self` from `src` and advance the cursor by the + /// number of bytes written. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// + /// buf.put_u8(b'h'); + /// buf.put(&b"ello"[..]); + /// buf.put(&b" world"[..]); + /// + /// assert_eq!(buf, b"hello world"); + /// ``` + /// + /// # Panics + /// + /// Panics if `self` does not have enough capacity to contain `src`. + fn put(&mut self, mut src: T) + where + Self: Sized, + { + assert!(self.remaining_mut() >= src.remaining()); + + while src.has_remaining() { + let l; + + unsafe { + let s = src.chunk(); + let d = self.chunk_mut(); + l = cmp::min(s.len(), d.len()); + + ptr::copy_nonoverlapping(s.as_ptr(), d.as_mut_ptr() as *mut u8, l); + } + + src.advance(l); + unsafe { + self.advance_mut(l); + } + } + } + + /// Transfer bytes into `self` from `src` and advance the cursor by the + /// number of bytes written. + /// + /// `self` must have enough remaining capacity to contain all of `src`. + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut dst = [0; 6]; + /// + /// { + /// let mut buf = &mut dst[..]; + /// buf.put_slice(b"hello"); + /// + /// assert_eq!(1, buf.remaining_mut()); + /// } + /// + /// assert_eq!(b"hello\0", &dst); + /// ``` + fn put_slice(&mut self, src: &[u8]) { + let mut off = 0; + + assert!( + self.remaining_mut() >= src.len(), + "buffer overflow; remaining = {}; src = {}", + self.remaining_mut(), + src.len() + ); + + while off < src.len() { + let cnt; + + unsafe { + let dst = self.chunk_mut(); + cnt = cmp::min(dst.len(), src.len() - off); + + ptr::copy_nonoverlapping(src[off..].as_ptr(), dst.as_mut_ptr() as *mut u8, cnt); + + off += cnt; + } + + unsafe { + self.advance_mut(cnt); + } + } + } + + /// Put `cnt` bytes `val` into `self`. + /// + /// Logically equivalent to calling `self.put_u8(val)` `cnt` times, but may work faster. + /// + /// `self` must have at least `cnt` remaining capacity. + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut dst = [0; 6]; + /// + /// { + /// let mut buf = &mut dst[..]; + /// buf.put_bytes(b'a', 4); + /// + /// assert_eq!(2, buf.remaining_mut()); + /// } + /// + /// assert_eq!(b"aaaa\0\0", &dst); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_bytes(&mut self, val: u8, cnt: usize) { + for _ in 0..cnt { + self.put_u8(val); + } + } + + /// Writes an unsigned 8 bit integer to `self`. + /// + /// The current position is advanced by 1. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u8(0x01); + /// assert_eq!(buf, b"\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u8(&mut self, n: u8) { + let src = [n]; + self.put_slice(&src); + } + + /// Writes a signed 8 bit integer to `self`. + /// + /// The current position is advanced by 1. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i8(0x01); + /// assert_eq!(buf, b"\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i8(&mut self, n: i8) { + let src = [n as u8]; + self.put_slice(&src) + } + + /// Writes an unsigned 16 bit integer to `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u16(0x0809); + /// assert_eq!(buf, b"\x08\x09"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u16(&mut self, n: u16) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes an unsigned 16 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u16_le(0x0809); + /// assert_eq!(buf, b"\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u16_le(&mut self, n: u16) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes an unsigned 16 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u16_ne(0x0809); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x08\x09"); + /// } else { + /// assert_eq!(buf, b"\x09\x08"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u16_ne(&mut self, n: u16) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes a signed 16 bit integer to `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i16(0x0809); + /// assert_eq!(buf, b"\x08\x09"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i16(&mut self, n: i16) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes a signed 16 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i16_le(0x0809); + /// assert_eq!(buf, b"\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i16_le(&mut self, n: i16) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes a signed 16 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i16_ne(0x0809); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x08\x09"); + /// } else { + /// assert_eq!(buf, b"\x09\x08"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i16_ne(&mut self, n: i16) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes an unsigned 32 bit integer to `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u32(0x0809A0A1); + /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u32(&mut self, n: u32) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes an unsigned 32 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u32_le(0x0809A0A1); + /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u32_le(&mut self, n: u32) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes an unsigned 32 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u32_ne(0x0809A0A1); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); + /// } else { + /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u32_ne(&mut self, n: u32) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes a signed 32 bit integer to `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i32(0x0809A0A1); + /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i32(&mut self, n: i32) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes a signed 32 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i32_le(0x0809A0A1); + /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i32_le(&mut self, n: i32) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes a signed 32 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i32_ne(0x0809A0A1); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); + /// } else { + /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i32_ne(&mut self, n: i32) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes an unsigned 64 bit integer to `self` in the big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u64(0x0102030405060708); + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u64(&mut self, n: u64) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes an unsigned 64 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u64_le(0x0102030405060708); + /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u64_le(&mut self, n: u64) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes an unsigned 64 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u64_ne(0x0102030405060708); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); + /// } else { + /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u64_ne(&mut self, n: u64) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes a signed 64 bit integer to `self` in the big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i64(0x0102030405060708); + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i64(&mut self, n: i64) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes a signed 64 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i64_le(0x0102030405060708); + /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i64_le(&mut self, n: i64) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes a signed 64 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i64_ne(0x0102030405060708); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); + /// } else { + /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i64_ne(&mut self, n: i64) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes an unsigned 128 bit integer to `self` in the big-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u128(0x01020304050607080910111213141516); + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u128(&mut self, n: u128) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes an unsigned 128 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u128_le(0x01020304050607080910111213141516); + /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u128_le(&mut self, n: u128) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes an unsigned 128 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u128_ne(0x01020304050607080910111213141516); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16"); + /// } else { + /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u128_ne(&mut self, n: u128) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes a signed 128 bit integer to `self` in the big-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i128(0x01020304050607080910111213141516); + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i128(&mut self, n: i128) { + self.put_slice(&n.to_be_bytes()) + } + + /// Writes a signed 128 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i128_le(0x01020304050607080910111213141516); + /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i128_le(&mut self, n: i128) { + self.put_slice(&n.to_le_bytes()) + } + + /// Writes a signed 128 bit integer to `self` in native-endian byte order. + /// + /// The current position is advanced by 16. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i128_ne(0x01020304050607080910111213141516); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16"); + /// } else { + /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i128_ne(&mut self, n: i128) { + self.put_slice(&n.to_ne_bytes()) + } + + /// Writes an unsigned n-byte integer to `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_uint(0x010203, 3); + /// assert_eq!(buf, b"\x01\x02\x03"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_uint(&mut self, n: u64, nbytes: usize) { + self.put_slice(&n.to_be_bytes()[mem::size_of_val(&n) - nbytes..]); + } + + /// Writes an unsigned n-byte integer to `self` in the little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_uint_le(0x010203, 3); + /// assert_eq!(buf, b"\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_uint_le(&mut self, n: u64, nbytes: usize) { + self.put_slice(&n.to_le_bytes()[0..nbytes]); + } + + /// Writes an unsigned n-byte integer to `self` in the native-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_uint_ne(0x010203, 3); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x01\x02\x03"); + /// } else { + /// assert_eq!(buf, b"\x03\x02\x01"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_uint_ne(&mut self, n: u64, nbytes: usize) { + if cfg!(target_endian = "big") { + self.put_uint(n, nbytes) + } else { + self.put_uint_le(n, nbytes) + } + } + + /// Writes low `nbytes` of a signed integer to `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_int(0x0504010203, 3); + /// assert_eq!(buf, b"\x01\x02\x03"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self` or if `nbytes` is greater than 8. + fn put_int(&mut self, n: i64, nbytes: usize) { + self.put_slice(&n.to_be_bytes()[mem::size_of_val(&n) - nbytes..]); + } + + /// Writes low `nbytes` of a signed integer to `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_int_le(0x0504010203, 3); + /// assert_eq!(buf, b"\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self` or if `nbytes` is greater than 8. + fn put_int_le(&mut self, n: i64, nbytes: usize) { + self.put_slice(&n.to_le_bytes()[0..nbytes]); + } + + /// Writes low `nbytes` of a signed integer to `self` in native-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_int_ne(0x010203, 3); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x01\x02\x03"); + /// } else { + /// assert_eq!(buf, b"\x03\x02\x01"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self` or if `nbytes` is greater than 8. + fn put_int_ne(&mut self, n: i64, nbytes: usize) { + if cfg!(target_endian = "big") { + self.put_int(n, nbytes) + } else { + self.put_int_le(n, nbytes) + } + } + + /// Writes an IEEE754 single-precision (4 bytes) floating point number to + /// `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f32(1.2f32); + /// assert_eq!(buf, b"\x3F\x99\x99\x9A"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f32(&mut self, n: f32) { + self.put_u32(n.to_bits()); + } + + /// Writes an IEEE754 single-precision (4 bytes) floating point number to + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f32_le(1.2f32); + /// assert_eq!(buf, b"\x9A\x99\x99\x3F"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f32_le(&mut self, n: f32) { + self.put_u32_le(n.to_bits()); + } + + /// Writes an IEEE754 single-precision (4 bytes) floating point number to + /// `self` in native-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f32_ne(1.2f32); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x3F\x99\x99\x9A"); + /// } else { + /// assert_eq!(buf, b"\x9A\x99\x99\x3F"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f32_ne(&mut self, n: f32) { + self.put_u32_ne(n.to_bits()); + } + + /// Writes an IEEE754 double-precision (8 bytes) floating point number to + /// `self` in big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f64(1.2f64); + /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f64(&mut self, n: f64) { + self.put_u64(n.to_bits()); + } + + /// Writes an IEEE754 double-precision (8 bytes) floating point number to + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f64_le(1.2f64); + /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f64_le(&mut self, n: f64) { + self.put_u64_le(n.to_bits()); + } + + /// Writes an IEEE754 double-precision (8 bytes) floating point number to + /// `self` in native-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f64_ne(1.2f64); + /// if cfg!(target_endian = "big") { + /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33"); + /// } else { + /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F"); + /// } + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f64_ne(&mut self, n: f64) { + self.put_u64_ne(n.to_bits()); + } + + /// Creates an adaptor which can write at most `limit` bytes to `self`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let arr = &mut [0u8; 128][..]; + /// assert_eq!(arr.remaining_mut(), 128); + /// + /// let dst = arr.limit(10); + /// assert_eq!(dst.remaining_mut(), 10); + /// ``` + fn limit(self, limit: usize) -> Limit + where + Self: Sized, + { + limit::new(self, limit) + } + + /// Creates an adaptor which implements the `Write` trait for `self`. + /// + /// This function returns a new value which implements `Write` by adapting + /// the `Write` trait functions to the `BufMut` trait functions. Given that + /// `BufMut` operations are infallible, none of the `Write` functions will + /// return with `Err`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// use std::io::Write; + /// + /// let mut buf = vec![].writer(); + /// + /// let num = buf.write(&b"hello world"[..]).unwrap(); + /// assert_eq!(11, num); + /// + /// let buf = buf.into_inner(); + /// + /// assert_eq!(*buf, b"hello world"[..]); + /// ``` + #[cfg(feature = "std")] + #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + fn writer(self) -> Writer + where + Self: Sized, + { + writer::new(self) + } + + /// Creates an adapter which will chain this buffer with another. + /// + /// The returned `BufMut` instance will first write to all bytes from + /// `self`. Afterwards, it will write to `next`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut a = [0u8; 5]; + /// let mut b = [0u8; 6]; + /// + /// let mut chain = (&mut a[..]).chain_mut(&mut b[..]); + /// + /// chain.put_slice(b"hello world"); + /// + /// assert_eq!(&a[..], b"hello"); + /// assert_eq!(&b[..], b" world"); + /// ``` + fn chain_mut(self, next: U) -> Chain + where + Self: Sized, + { + Chain::new(self, next) + } +} + +macro_rules! deref_forward_bufmut { + () => { + fn remaining_mut(&self) -> usize { + (**self).remaining_mut() + } + + fn chunk_mut(&mut self) -> &mut UninitSlice { + (**self).chunk_mut() + } + + unsafe fn advance_mut(&mut self, cnt: usize) { + (**self).advance_mut(cnt) + } + + fn put_slice(&mut self, src: &[u8]) { + (**self).put_slice(src) + } + + fn put_u8(&mut self, n: u8) { + (**self).put_u8(n) + } + + fn put_i8(&mut self, n: i8) { + (**self).put_i8(n) + } + + fn put_u16(&mut self, n: u16) { + (**self).put_u16(n) + } + + fn put_u16_le(&mut self, n: u16) { + (**self).put_u16_le(n) + } + + fn put_u16_ne(&mut self, n: u16) { + (**self).put_u16_ne(n) + } + + fn put_i16(&mut self, n: i16) { + (**self).put_i16(n) + } + + fn put_i16_le(&mut self, n: i16) { + (**self).put_i16_le(n) + } + + fn put_i16_ne(&mut self, n: i16) { + (**self).put_i16_ne(n) + } + + fn put_u32(&mut self, n: u32) { + (**self).put_u32(n) + } + + fn put_u32_le(&mut self, n: u32) { + (**self).put_u32_le(n) + } + + fn put_u32_ne(&mut self, n: u32) { + (**self).put_u32_ne(n) + } + + fn put_i32(&mut self, n: i32) { + (**self).put_i32(n) + } + + fn put_i32_le(&mut self, n: i32) { + (**self).put_i32_le(n) + } + + fn put_i32_ne(&mut self, n: i32) { + (**self).put_i32_ne(n) + } + + fn put_u64(&mut self, n: u64) { + (**self).put_u64(n) + } + + fn put_u64_le(&mut self, n: u64) { + (**self).put_u64_le(n) + } + + fn put_u64_ne(&mut self, n: u64) { + (**self).put_u64_ne(n) + } + + fn put_i64(&mut self, n: i64) { + (**self).put_i64(n) + } + + fn put_i64_le(&mut self, n: i64) { + (**self).put_i64_le(n) + } + + fn put_i64_ne(&mut self, n: i64) { + (**self).put_i64_ne(n) + } + }; +} + +unsafe impl BufMut for &mut T { + deref_forward_bufmut!(); +} + +unsafe impl BufMut for Box { + deref_forward_bufmut!(); +} + +unsafe impl BufMut for &mut [u8] { + #[inline] + fn remaining_mut(&self) -> usize { + self.len() + } + + #[inline] + fn chunk_mut(&mut self) -> &mut UninitSlice { + // UninitSlice is repr(transparent), so safe to transmute + unsafe { &mut *(*self as *mut [u8] as *mut _) } + } + + #[inline] + unsafe fn advance_mut(&mut self, cnt: usize) { + // Lifetime dance taken from `impl Write for &mut [u8]`. + let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt); + *self = b; + } + + #[inline] + fn put_slice(&mut self, src: &[u8]) { + self[..src.len()].copy_from_slice(src); + unsafe { + self.advance_mut(src.len()); + } + } + + fn put_bytes(&mut self, val: u8, cnt: usize) { + assert!(self.remaining_mut() >= cnt); + unsafe { + ptr::write_bytes(self.as_mut_ptr(), val, cnt); + self.advance_mut(cnt); + } + } +} + +unsafe impl BufMut for &mut [core::mem::MaybeUninit] { + #[inline] + fn remaining_mut(&self) -> usize { + self.len() + } + + #[inline] + fn chunk_mut(&mut self) -> &mut UninitSlice { + UninitSlice::uninit(self) + } + + #[inline] + unsafe fn advance_mut(&mut self, cnt: usize) { + // Lifetime dance taken from `impl Write for &mut [u8]`. + let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt); + *self = b; + } + + #[inline] + fn put_slice(&mut self, src: &[u8]) { + self.chunk_mut()[..src.len()].copy_from_slice(src); + unsafe { + self.advance_mut(src.len()); + } + } + + fn put_bytes(&mut self, val: u8, cnt: usize) { + assert!(self.remaining_mut() >= cnt); + unsafe { + ptr::write_bytes(self.as_mut_ptr() as *mut u8, val, cnt); + self.advance_mut(cnt); + } + } +} + +unsafe impl BufMut for Vec { + #[inline] + fn remaining_mut(&self) -> usize { + // A vector can never have more than isize::MAX bytes + core::isize::MAX as usize - self.len() + } + + #[inline] + unsafe fn advance_mut(&mut self, cnt: usize) { + let len = self.len(); + let remaining = self.capacity() - len; + + assert!( + cnt <= remaining, + "cannot advance past `remaining_mut`: {:?} <= {:?}", + cnt, + remaining + ); + + self.set_len(len + cnt); + } + + #[inline] + fn chunk_mut(&mut self) -> &mut UninitSlice { + if self.capacity() == self.len() { + self.reserve(64); // Grow the vec + } + + let cap = self.capacity(); + let len = self.len(); + + let ptr = self.as_mut_ptr(); + unsafe { &mut UninitSlice::from_raw_parts_mut(ptr, cap)[len..] } + } + + // Specialize these methods so they can skip checking `remaining_mut` + // and `advance_mut`. + fn put(&mut self, mut src: T) + where + Self: Sized, + { + // In case the src isn't contiguous, reserve upfront + self.reserve(src.remaining()); + + while src.has_remaining() { + let l; + + // a block to contain the src.bytes() borrow + { + let s = src.chunk(); + l = s.len(); + self.extend_from_slice(s); + } + + src.advance(l); + } + } + + #[inline] + fn put_slice(&mut self, src: &[u8]) { + self.extend_from_slice(src); + } + + fn put_bytes(&mut self, val: u8, cnt: usize) { + let new_len = self.len().checked_add(cnt).unwrap(); + self.resize(new_len, val); + } +} + +// The existence of this function makes the compiler catch if the BufMut +// trait is "object-safe" or not. +fn _assert_trait_object(_b: &dyn BufMut) {} diff --git a/third_party/rust/bytes/v1/crate/src/buf/chain.rs b/third_party/rust/bytes/v1/crate/src/buf/chain.rs new file mode 100644 index 000000000000..78979a12310d --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/chain.rs @@ -0,0 +1,242 @@ +use crate::buf::{IntoIter, UninitSlice}; +use crate::{Buf, BufMut, Bytes}; + +#[cfg(feature = "std")] +use std::io::IoSlice; + +/// A `Chain` sequences two buffers. +/// +/// `Chain` is an adapter that links two underlying buffers and provides a +/// continuous view across both buffers. It is able to sequence either immutable +/// buffers ([`Buf`] values) or mutable buffers ([`BufMut`] values). +/// +/// This struct is generally created by calling [`Buf::chain`]. Please see that +/// function's documentation for more detail. +/// +/// # Examples +/// +/// ``` +/// use bytes::{Bytes, Buf}; +/// +/// let mut buf = (&b"hello "[..]) +/// .chain(&b"world"[..]); +/// +/// let full: Bytes = buf.copy_to_bytes(11); +/// assert_eq!(full[..], b"hello world"[..]); +/// ``` +/// +/// [`Buf::chain`]: trait.Buf.html#method.chain +/// [`Buf`]: trait.Buf.html +/// [`BufMut`]: trait.BufMut.html +#[derive(Debug)] +pub struct Chain { + a: T, + b: U, +} + +impl Chain { + /// Creates a new `Chain` sequencing the provided values. + pub(crate) fn new(a: T, b: U) -> Chain { + Chain { a, b } + } + + /// Gets a reference to the first underlying `Buf`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let buf = (&b"hello"[..]) + /// .chain(&b"world"[..]); + /// + /// assert_eq!(buf.first_ref()[..], b"hello"[..]); + /// ``` + pub fn first_ref(&self) -> &T { + &self.a + } + + /// Gets a mutable reference to the first underlying `Buf`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = (&b"hello"[..]) + /// .chain(&b"world"[..]); + /// + /// buf.first_mut().advance(1); + /// + /// let full = buf.copy_to_bytes(9); + /// assert_eq!(full, b"elloworld"[..]); + /// ``` + pub fn first_mut(&mut self) -> &mut T { + &mut self.a + } + + /// Gets a reference to the last underlying `Buf`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let buf = (&b"hello"[..]) + /// .chain(&b"world"[..]); + /// + /// assert_eq!(buf.last_ref()[..], b"world"[..]); + /// ``` + pub fn last_ref(&self) -> &U { + &self.b + } + + /// Gets a mutable reference to the last underlying `Buf`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut buf = (&b"hello "[..]) + /// .chain(&b"world"[..]); + /// + /// buf.last_mut().advance(1); + /// + /// let full = buf.copy_to_bytes(10); + /// assert_eq!(full, b"hello orld"[..]); + /// ``` + pub fn last_mut(&mut self) -> &mut U { + &mut self.b + } + + /// Consumes this `Chain`, returning the underlying values. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let chain = (&b"hello"[..]) + /// .chain(&b"world"[..]); + /// + /// let (first, last) = chain.into_inner(); + /// assert_eq!(first[..], b"hello"[..]); + /// assert_eq!(last[..], b"world"[..]); + /// ``` + pub fn into_inner(self) -> (T, U) { + (self.a, self.b) + } +} + +impl Buf for Chain +where + T: Buf, + U: Buf, +{ + fn remaining(&self) -> usize { + self.a.remaining().checked_add(self.b.remaining()).unwrap() + } + + fn chunk(&self) -> &[u8] { + if self.a.has_remaining() { + self.a.chunk() + } else { + self.b.chunk() + } + } + + fn advance(&mut self, mut cnt: usize) { + let a_rem = self.a.remaining(); + + if a_rem != 0 { + if a_rem >= cnt { + self.a.advance(cnt); + return; + } + + // Consume what is left of a + self.a.advance(a_rem); + + cnt -= a_rem; + } + + self.b.advance(cnt); + } + + #[cfg(feature = "std")] + fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { + let mut n = self.a.chunks_vectored(dst); + n += self.b.chunks_vectored(&mut dst[n..]); + n + } + + fn copy_to_bytes(&mut self, len: usize) -> Bytes { + let a_rem = self.a.remaining(); + if a_rem >= len { + self.a.copy_to_bytes(len) + } else if a_rem == 0 { + self.b.copy_to_bytes(len) + } else { + assert!( + len - a_rem <= self.b.remaining(), + "`len` greater than remaining" + ); + let mut ret = crate::BytesMut::with_capacity(len); + ret.put(&mut self.a); + ret.put((&mut self.b).take(len - a_rem)); + ret.freeze() + } + } +} + +unsafe impl BufMut for Chain +where + T: BufMut, + U: BufMut, +{ + fn remaining_mut(&self) -> usize { + self.a + .remaining_mut() + .saturating_add(self.b.remaining_mut()) + } + + fn chunk_mut(&mut self) -> &mut UninitSlice { + if self.a.has_remaining_mut() { + self.a.chunk_mut() + } else { + self.b.chunk_mut() + } + } + + unsafe fn advance_mut(&mut self, mut cnt: usize) { + let a_rem = self.a.remaining_mut(); + + if a_rem != 0 { + if a_rem >= cnt { + self.a.advance_mut(cnt); + return; + } + + // Consume what is left of a + self.a.advance_mut(a_rem); + + cnt -= a_rem; + } + + self.b.advance_mut(cnt); + } +} + +impl IntoIterator for Chain +where + T: Buf, + U: Buf, +{ + type Item = u8; + type IntoIter = IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} diff --git a/third_party/rust/bytes/v1/crate/src/buf/iter.rs b/third_party/rust/bytes/v1/crate/src/buf/iter.rs new file mode 100644 index 000000000000..c694e3d418ed --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/iter.rs @@ -0,0 +1,130 @@ +use crate::Buf; + +/// Iterator over the bytes contained by the buffer. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// use bytes::Bytes; +/// +/// let buf = Bytes::from(&b"abc"[..]); +/// let mut iter = buf.into_iter(); +/// +/// assert_eq!(iter.next(), Some(b'a')); +/// assert_eq!(iter.next(), Some(b'b')); +/// assert_eq!(iter.next(), Some(b'c')); +/// assert_eq!(iter.next(), None); +/// ``` +/// +/// [`iter`]: trait.Buf.html#method.iter +/// [`Buf`]: trait.Buf.html +#[derive(Debug)] +pub struct IntoIter { + inner: T, +} + +impl IntoIter { + /// Creates an iterator over the bytes contained by the buffer. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let buf = Bytes::from_static(b"abc"); + /// let mut iter = buf.into_iter(); + /// + /// assert_eq!(iter.next(), Some(b'a')); + /// assert_eq!(iter.next(), Some(b'b')); + /// assert_eq!(iter.next(), Some(b'c')); + /// assert_eq!(iter.next(), None); + /// ``` + pub fn new(inner: T) -> IntoIter { + IntoIter { inner } + } + + /// Consumes this `IntoIter`, returning the underlying value. + /// + /// # Examples + /// + /// ```rust + /// use bytes::{Buf, Bytes}; + /// + /// let buf = Bytes::from(&b"abc"[..]); + /// let mut iter = buf.into_iter(); + /// + /// assert_eq!(iter.next(), Some(b'a')); + /// + /// let buf = iter.into_inner(); + /// assert_eq!(2, buf.remaining()); + /// ``` + pub fn into_inner(self) -> T { + self.inner + } + + /// Gets a reference to the underlying `Buf`. + /// + /// It is inadvisable to directly read from the underlying `Buf`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::{Buf, Bytes}; + /// + /// let buf = Bytes::from(&b"abc"[..]); + /// let mut iter = buf.into_iter(); + /// + /// assert_eq!(iter.next(), Some(b'a')); + /// + /// assert_eq!(2, iter.get_ref().remaining()); + /// ``` + pub fn get_ref(&self) -> &T { + &self.inner + } + + /// Gets a mutable reference to the underlying `Buf`. + /// + /// It is inadvisable to directly read from the underlying `Buf`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::{Buf, BytesMut}; + /// + /// let buf = BytesMut::from(&b"abc"[..]); + /// let mut iter = buf.into_iter(); + /// + /// assert_eq!(iter.next(), Some(b'a')); + /// + /// iter.get_mut().advance(1); + /// + /// assert_eq!(iter.next(), Some(b'c')); + /// ``` + pub fn get_mut(&mut self) -> &mut T { + &mut self.inner + } +} + +impl Iterator for IntoIter { + type Item = u8; + + fn next(&mut self) -> Option { + if !self.inner.has_remaining() { + return None; + } + + let b = self.inner.chunk()[0]; + self.inner.advance(1); + + Some(b) + } + + fn size_hint(&self) -> (usize, Option) { + let rem = self.inner.remaining(); + (rem, Some(rem)) + } +} + +impl ExactSizeIterator for IntoIter {} diff --git a/third_party/rust/bytes/v1/crate/src/buf/limit.rs b/third_party/rust/bytes/v1/crate/src/buf/limit.rs new file mode 100644 index 000000000000..b422be538333 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/limit.rs @@ -0,0 +1,75 @@ +use crate::buf::UninitSlice; +use crate::BufMut; + +use core::cmp; + +/// A `BufMut` adapter which limits the amount of bytes that can be written +/// to an underlying buffer. +#[derive(Debug)] +pub struct Limit { + inner: T, + limit: usize, +} + +pub(super) fn new(inner: T, limit: usize) -> Limit { + Limit { inner, limit } +} + +impl Limit { + /// Consumes this `Limit`, returning the underlying value. + pub fn into_inner(self) -> T { + self.inner + } + + /// Gets a reference to the underlying `BufMut`. + /// + /// It is inadvisable to directly write to the underlying `BufMut`. + pub fn get_ref(&self) -> &T { + &self.inner + } + + /// Gets a mutable reference to the underlying `BufMut`. + /// + /// It is inadvisable to directly write to the underlying `BufMut`. + pub fn get_mut(&mut self) -> &mut T { + &mut self.inner + } + + /// Returns the maximum number of bytes that can be written + /// + /// # Note + /// + /// If the inner `BufMut` has fewer bytes than indicated by this method then + /// that is the actual number of available bytes. + pub fn limit(&self) -> usize { + self.limit + } + + /// Sets the maximum number of bytes that can be written. + /// + /// # Note + /// + /// If the inner `BufMut` has fewer bytes than `lim` then that is the actual + /// number of available bytes. + pub fn set_limit(&mut self, lim: usize) { + self.limit = lim + } +} + +unsafe impl BufMut for Limit { + fn remaining_mut(&self) -> usize { + cmp::min(self.inner.remaining_mut(), self.limit) + } + + fn chunk_mut(&mut self) -> &mut UninitSlice { + let bytes = self.inner.chunk_mut(); + let end = cmp::min(bytes.len(), self.limit); + &mut bytes[..end] + } + + unsafe fn advance_mut(&mut self, cnt: usize) { + assert!(cnt <= self.limit); + self.inner.advance_mut(cnt); + self.limit -= cnt; + } +} diff --git a/third_party/rust/bytes/v1/crate/src/buf/mod.rs b/third_party/rust/bytes/v1/crate/src/buf/mod.rs new file mode 100644 index 000000000000..c4c0a5724a1c --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/mod.rs @@ -0,0 +1,41 @@ +//! Utilities for working with buffers. +//! +//! A buffer is any structure that contains a sequence of bytes. The bytes may +//! or may not be stored in contiguous memory. This module contains traits used +//! to abstract over buffers as well as utilities for working with buffer types. +//! +//! # `Buf`, `BufMut` +//! +//! These are the two foundational traits for abstractly working with buffers. +//! They can be thought as iterators for byte structures. They offer additional +//! performance over `Iterator` by providing an API optimized for byte slices. +//! +//! See [`Buf`] and [`BufMut`] for more details. +//! +//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure) +//! [`Buf`]: trait.Buf.html +//! [`BufMut`]: trait.BufMut.html + +mod buf_impl; +mod buf_mut; +mod chain; +mod iter; +mod limit; +#[cfg(feature = "std")] +mod reader; +mod take; +mod uninit_slice; +mod vec_deque; +#[cfg(feature = "std")] +mod writer; + +pub use self::buf_impl::Buf; +pub use self::buf_mut::BufMut; +pub use self::chain::Chain; +pub use self::iter::IntoIter; +pub use self::limit::Limit; +pub use self::take::Take; +pub use self::uninit_slice::UninitSlice; + +#[cfg(feature = "std")] +pub use self::{reader::Reader, writer::Writer}; diff --git a/third_party/rust/bytes/v1/crate/src/buf/reader.rs b/third_party/rust/bytes/v1/crate/src/buf/reader.rs new file mode 100644 index 000000000000..f2b4d98f7168 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/reader.rs @@ -0,0 +1,81 @@ +use crate::Buf; + +use std::{cmp, io}; + +/// A `Buf` adapter which implements `io::Read` for the inner value. +/// +/// This struct is generally created by calling `reader()` on `Buf`. See +/// documentation of [`reader()`](trait.Buf.html#method.reader) for more +/// details. +#[derive(Debug)] +pub struct Reader { + buf: B, +} + +pub fn new(buf: B) -> Reader { + Reader { buf } +} + +impl Reader { + /// Gets a reference to the underlying `Buf`. + /// + /// It is inadvisable to directly read from the underlying `Buf`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::Buf; + /// + /// let buf = b"hello world".reader(); + /// + /// assert_eq!(b"hello world", buf.get_ref()); + /// ``` + pub fn get_ref(&self) -> &B { + &self.buf + } + + /// Gets a mutable reference to the underlying `Buf`. + /// + /// It is inadvisable to directly read from the underlying `Buf`. + pub fn get_mut(&mut self) -> &mut B { + &mut self.buf + } + + /// Consumes this `Reader`, returning the underlying value. + /// + /// # Examples + /// + /// ```rust + /// use bytes::Buf; + /// use std::io; + /// + /// let mut buf = b"hello world".reader(); + /// let mut dst = vec![]; + /// + /// io::copy(&mut buf, &mut dst).unwrap(); + /// + /// let buf = buf.into_inner(); + /// assert_eq!(0, buf.remaining()); + /// ``` + pub fn into_inner(self) -> B { + self.buf + } +} + +impl io::Read for Reader { + fn read(&mut self, dst: &mut [u8]) -> io::Result { + let len = cmp::min(self.buf.remaining(), dst.len()); + + Buf::copy_to_slice(&mut self.buf, &mut dst[0..len]); + Ok(len) + } +} + +impl io::BufRead for Reader { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + Ok(self.buf.chunk()) + } + fn consume(&mut self, amt: usize) { + self.buf.advance(amt) + } +} diff --git a/third_party/rust/bytes/v1/crate/src/buf/take.rs b/third_party/rust/bytes/v1/crate/src/buf/take.rs new file mode 100644 index 000000000000..d3cb10ab648b --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/take.rs @@ -0,0 +1,155 @@ +use crate::{Buf, Bytes}; + +use core::cmp; + +/// A `Buf` adapter which limits the bytes read from an underlying buffer. +/// +/// This struct is generally created by calling `take()` on `Buf`. See +/// documentation of [`take()`](trait.Buf.html#method.take) for more details. +#[derive(Debug)] +pub struct Take { + inner: T, + limit: usize, +} + +pub fn new(inner: T, limit: usize) -> Take { + Take { inner, limit } +} + +impl Take { + /// Consumes this `Take`, returning the underlying value. + /// + /// # Examples + /// + /// ```rust + /// use bytes::{Buf, BufMut}; + /// + /// let mut buf = b"hello world".take(2); + /// let mut dst = vec![]; + /// + /// dst.put(&mut buf); + /// assert_eq!(*dst, b"he"[..]); + /// + /// let mut buf = buf.into_inner(); + /// + /// dst.clear(); + /// dst.put(&mut buf); + /// assert_eq!(*dst, b"llo world"[..]); + /// ``` + pub fn into_inner(self) -> T { + self.inner + } + + /// Gets a reference to the underlying `Buf`. + /// + /// It is inadvisable to directly read from the underlying `Buf`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::Buf; + /// + /// let buf = b"hello world".take(2); + /// + /// assert_eq!(11, buf.get_ref().remaining()); + /// ``` + pub fn get_ref(&self) -> &T { + &self.inner + } + + /// Gets a mutable reference to the underlying `Buf`. + /// + /// It is inadvisable to directly read from the underlying `Buf`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::{Buf, BufMut}; + /// + /// let mut buf = b"hello world".take(2); + /// let mut dst = vec![]; + /// + /// buf.get_mut().advance(2); + /// + /// dst.put(&mut buf); + /// assert_eq!(*dst, b"ll"[..]); + /// ``` + pub fn get_mut(&mut self) -> &mut T { + &mut self.inner + } + + /// Returns the maximum number of bytes that can be read. + /// + /// # Note + /// + /// If the inner `Buf` has fewer bytes than indicated by this method then + /// that is the actual number of available bytes. + /// + /// # Examples + /// + /// ```rust + /// use bytes::Buf; + /// + /// let mut buf = b"hello world".take(2); + /// + /// assert_eq!(2, buf.limit()); + /// assert_eq!(b'h', buf.get_u8()); + /// assert_eq!(1, buf.limit()); + /// ``` + pub fn limit(&self) -> usize { + self.limit + } + + /// Sets the maximum number of bytes that can be read. + /// + /// # Note + /// + /// If the inner `Buf` has fewer bytes than `lim` then that is the actual + /// number of available bytes. + /// + /// # Examples + /// + /// ```rust + /// use bytes::{Buf, BufMut}; + /// + /// let mut buf = b"hello world".take(2); + /// let mut dst = vec![]; + /// + /// dst.put(&mut buf); + /// assert_eq!(*dst, b"he"[..]); + /// + /// dst.clear(); + /// + /// buf.set_limit(3); + /// dst.put(&mut buf); + /// assert_eq!(*dst, b"llo"[..]); + /// ``` + pub fn set_limit(&mut self, lim: usize) { + self.limit = lim + } +} + +impl Buf for Take { + fn remaining(&self) -> usize { + cmp::min(self.inner.remaining(), self.limit) + } + + fn chunk(&self) -> &[u8] { + let bytes = self.inner.chunk(); + &bytes[..cmp::min(bytes.len(), self.limit)] + } + + fn advance(&mut self, cnt: usize) { + assert!(cnt <= self.limit); + self.inner.advance(cnt); + self.limit -= cnt; + } + + fn copy_to_bytes(&mut self, len: usize) -> Bytes { + assert!(len <= self.remaining(), "`len` greater than remaining"); + + let r = self.inner.copy_to_bytes(len); + self.limit -= len; + r + } +} diff --git a/third_party/rust/bytes/v1/crate/src/buf/uninit_slice.rs b/third_party/rust/bytes/v1/crate/src/buf/uninit_slice.rs new file mode 100644 index 000000000000..c5d86e85dac8 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/uninit_slice.rs @@ -0,0 +1,257 @@ +use core::fmt; +use core::mem::MaybeUninit; +use core::ops::{ + Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, +}; + +/// Uninitialized byte slice. +/// +/// Returned by `BufMut::chunk_mut()`, the referenced byte slice may be +/// uninitialized. The wrapper provides safe access without introducing +/// undefined behavior. +/// +/// The safety invariants of this wrapper are: +/// +/// 1. Reading from an `UninitSlice` is undefined behavior. +/// 2. Writing uninitialized bytes to an `UninitSlice` is undefined behavior. +/// +/// The difference between `&mut UninitSlice` and `&mut [MaybeUninit]` is +/// that it is possible in safe code to write uninitialized bytes to an +/// `&mut [MaybeUninit]`, which this type prohibits. +#[repr(transparent)] +pub struct UninitSlice([MaybeUninit]); + +impl UninitSlice { + /// Creates a `&mut UninitSlice` wrapping a slice of initialised memory. + /// + /// # Examples + /// + /// ``` + /// use bytes::buf::UninitSlice; + /// + /// let mut buffer = [0u8; 64]; + /// let slice = UninitSlice::new(&mut buffer[..]); + /// ``` + #[inline] + pub fn new(slice: &mut [u8]) -> &mut UninitSlice { + unsafe { &mut *(slice as *mut [u8] as *mut [MaybeUninit] as *mut UninitSlice) } + } + + /// Creates a `&mut UninitSlice` wrapping a slice of uninitialised memory. + /// + /// # Examples + /// + /// ``` + /// use bytes::buf::UninitSlice; + /// use core::mem::MaybeUninit; + /// + /// let mut buffer = [MaybeUninit::uninit(); 64]; + /// let slice = UninitSlice::uninit(&mut buffer[..]); + /// + /// let mut vec = Vec::with_capacity(1024); + /// let spare: &mut UninitSlice = vec.spare_capacity_mut().into(); + /// ``` + #[inline] + pub fn uninit(slice: &mut [MaybeUninit]) -> &mut UninitSlice { + unsafe { &mut *(slice as *mut [MaybeUninit] as *mut UninitSlice) } + } + + fn uninit_ref(slice: &[MaybeUninit]) -> &UninitSlice { + unsafe { &*(slice as *const [MaybeUninit] as *const UninitSlice) } + } + + /// Create a `&mut UninitSlice` from a pointer and a length. + /// + /// # Safety + /// + /// The caller must ensure that `ptr` references a valid memory region owned + /// by the caller representing a byte slice for the duration of `'a`. + /// + /// # Examples + /// + /// ``` + /// use bytes::buf::UninitSlice; + /// + /// let bytes = b"hello world".to_vec(); + /// let ptr = bytes.as_ptr() as *mut _; + /// let len = bytes.len(); + /// + /// let slice = unsafe { UninitSlice::from_raw_parts_mut(ptr, len) }; + /// ``` + #[inline] + pub unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut UninitSlice { + let maybe_init: &mut [MaybeUninit] = + core::slice::from_raw_parts_mut(ptr as *mut _, len); + Self::uninit(maybe_init) + } + + /// Write a single byte at the specified offset. + /// + /// # Panics + /// + /// The function panics if `index` is out of bounds. + /// + /// # Examples + /// + /// ``` + /// use bytes::buf::UninitSlice; + /// + /// let mut data = [b'f', b'o', b'o']; + /// let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; + /// + /// slice.write_byte(0, b'b'); + /// + /// assert_eq!(b"boo", &data[..]); + /// ``` + #[inline] + pub fn write_byte(&mut self, index: usize, byte: u8) { + assert!(index < self.len()); + + unsafe { self[index..].as_mut_ptr().write(byte) } + } + + /// Copies bytes from `src` into `self`. + /// + /// The length of `src` must be the same as `self`. + /// + /// # Panics + /// + /// The function panics if `src` has a different length than `self`. + /// + /// # Examples + /// + /// ``` + /// use bytes::buf::UninitSlice; + /// + /// let mut data = [b'f', b'o', b'o']; + /// let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; + /// + /// slice.copy_from_slice(b"bar"); + /// + /// assert_eq!(b"bar", &data[..]); + /// ``` + #[inline] + pub fn copy_from_slice(&mut self, src: &[u8]) { + use core::ptr; + + assert_eq!(self.len(), src.len()); + + unsafe { + ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr(), self.len()); + } + } + + /// Return a raw pointer to the slice's buffer. + /// + /// # Safety + /// + /// The caller **must not** read from the referenced memory and **must not** + /// write **uninitialized** bytes to the slice either. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut data = [0, 1, 2]; + /// let mut slice = &mut data[..]; + /// let ptr = BufMut::chunk_mut(&mut slice).as_mut_ptr(); + /// ``` + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut u8 { + self.0.as_mut_ptr() as *mut _ + } + + /// Return a `&mut [MaybeUninit]` to this slice's buffer. + /// + /// # Safety + /// + /// The caller **must not** read from the referenced memory and **must not** write + /// **uninitialized** bytes to the slice either. This is because `BufMut` implementation + /// that created the `UninitSlice` knows which parts are initialized. Writing uninitalized + /// bytes to the slice may cause the `BufMut` to read those bytes and trigger undefined + /// behavior. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut data = [0, 1, 2]; + /// let mut slice = &mut data[..]; + /// unsafe { + /// let uninit_slice = BufMut::chunk_mut(&mut slice).as_uninit_slice_mut(); + /// }; + /// ``` + #[inline] + pub unsafe fn as_uninit_slice_mut<'a>(&'a mut self) -> &'a mut [MaybeUninit] { + &mut *(self as *mut _ as *mut [MaybeUninit]) + } + + /// Returns the number of bytes in the slice. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut data = [0, 1, 2]; + /// let mut slice = &mut data[..]; + /// let len = BufMut::chunk_mut(&mut slice).len(); + /// + /// assert_eq!(len, 3); + /// ``` + #[inline] + pub fn len(&self) -> usize { + self.0.len() + } +} + +impl fmt::Debug for UninitSlice { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("UninitSlice[...]").finish() + } +} + +impl<'a> From<&'a mut [u8]> for &'a mut UninitSlice { + fn from(slice: &'a mut [u8]) -> Self { + UninitSlice::new(slice) + } +} + +impl<'a> From<&'a mut [MaybeUninit]> for &'a mut UninitSlice { + fn from(slice: &'a mut [MaybeUninit]) -> Self { + UninitSlice::uninit(slice) + } +} + +macro_rules! impl_index { + ($($t:ty),*) => { + $( + impl Index<$t> for UninitSlice { + type Output = UninitSlice; + + #[inline] + fn index(&self, index: $t) -> &UninitSlice { + UninitSlice::uninit_ref(&self.0[index]) + } + } + + impl IndexMut<$t> for UninitSlice { + #[inline] + fn index_mut(&mut self, index: $t) -> &mut UninitSlice { + UninitSlice::uninit(&mut self.0[index]) + } + } + )* + }; +} + +impl_index!( + Range, + RangeFrom, + RangeFull, + RangeInclusive, + RangeTo, + RangeToInclusive +); diff --git a/third_party/rust/bytes/v1/crate/src/buf/vec_deque.rs b/third_party/rust/bytes/v1/crate/src/buf/vec_deque.rs new file mode 100644 index 000000000000..263167e83cce --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/vec_deque.rs @@ -0,0 +1,22 @@ +use alloc::collections::VecDeque; + +use super::Buf; + +impl Buf for VecDeque { + fn remaining(&self) -> usize { + self.len() + } + + fn chunk(&self) -> &[u8] { + let (s1, s2) = self.as_slices(); + if s1.is_empty() { + s2 + } else { + s1 + } + } + + fn advance(&mut self, cnt: usize) { + self.drain(..cnt); + } +} diff --git a/third_party/rust/bytes/v1/crate/src/buf/writer.rs b/third_party/rust/bytes/v1/crate/src/buf/writer.rs new file mode 100644 index 000000000000..261d7cd09100 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/buf/writer.rs @@ -0,0 +1,88 @@ +use crate::BufMut; + +use std::{cmp, io}; + +/// A `BufMut` adapter which implements `io::Write` for the inner value. +/// +/// This struct is generally created by calling `writer()` on `BufMut`. See +/// documentation of [`writer()`](trait.BufMut.html#method.writer) for more +/// details. +#[derive(Debug)] +pub struct Writer { + buf: B, +} + +pub fn new(buf: B) -> Writer { + Writer { buf } +} + +impl Writer { + /// Gets a reference to the underlying `BufMut`. + /// + /// It is inadvisable to directly write to the underlying `BufMut`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::BufMut; + /// + /// let buf = Vec::with_capacity(1024).writer(); + /// + /// assert_eq!(1024, buf.get_ref().capacity()); + /// ``` + pub fn get_ref(&self) -> &B { + &self.buf + } + + /// Gets a mutable reference to the underlying `BufMut`. + /// + /// It is inadvisable to directly write to the underlying `BufMut`. + /// + /// # Examples + /// + /// ```rust + /// use bytes::BufMut; + /// + /// let mut buf = vec![].writer(); + /// + /// buf.get_mut().reserve(1024); + /// + /// assert_eq!(1024, buf.get_ref().capacity()); + /// ``` + pub fn get_mut(&mut self) -> &mut B { + &mut self.buf + } + + /// Consumes this `Writer`, returning the underlying value. + /// + /// # Examples + /// + /// ```rust + /// use bytes::BufMut; + /// use std::io; + /// + /// let mut buf = vec![].writer(); + /// let mut src = &b"hello world"[..]; + /// + /// io::copy(&mut src, &mut buf).unwrap(); + /// + /// let buf = buf.into_inner(); + /// assert_eq!(*buf, b"hello world"[..]); + /// ``` + pub fn into_inner(self) -> B { + self.buf + } +} + +impl io::Write for Writer { + fn write(&mut self, src: &[u8]) -> io::Result { + let n = cmp::min(self.buf.remaining_mut(), src.len()); + + self.buf.put(&src[0..n]); + Ok(n) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} diff --git a/third_party/rust/bytes/v1/crate/src/bytes.rs b/third_party/rust/bytes/v1/crate/src/bytes.rs new file mode 100644 index 000000000000..0404a72dbaa2 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/bytes.rs @@ -0,0 +1,1304 @@ +use core::iter::FromIterator; +use core::ops::{Deref, RangeBounds}; +use core::{cmp, fmt, hash, mem, ptr, slice, usize}; + +use alloc::{ + alloc::{dealloc, Layout}, + borrow::Borrow, + boxed::Box, + string::String, + vec::Vec, +}; + +use crate::buf::IntoIter; +#[allow(unused)] +use crate::loom::sync::atomic::AtomicMut; +use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; +use crate::Buf; + +/// A cheaply cloneable and sliceable chunk of contiguous memory. +/// +/// `Bytes` is an efficient container for storing and operating on contiguous +/// slices of memory. It is intended for use primarily in networking code, but +/// could have applications elsewhere as well. +/// +/// `Bytes` values facilitate zero-copy network programming by allowing multiple +/// `Bytes` objects to point to the same underlying memory. +/// +/// `Bytes` does not have a single implementation. It is an interface, whose +/// exact behavior is implemented through dynamic dispatch in several underlying +/// implementations of `Bytes`. +/// +/// All `Bytes` implementations must fulfill the following requirements: +/// - They are cheaply cloneable and thereby shareable between an unlimited amount +/// of components, for example by modifying a reference count. +/// - Instances can be sliced to refer to a subset of the original buffer. +/// +/// ``` +/// use bytes::Bytes; +/// +/// let mut mem = Bytes::from("Hello world"); +/// let a = mem.slice(0..5); +/// +/// assert_eq!(a, "Hello"); +/// +/// let b = mem.split_to(6); +/// +/// assert_eq!(mem, "world"); +/// assert_eq!(b, "Hello "); +/// ``` +/// +/// # Memory layout +/// +/// The `Bytes` struct itself is fairly small, limited to 4 `usize` fields used +/// to track information about which segment of the underlying memory the +/// `Bytes` handle has access to. +/// +/// `Bytes` keeps both a pointer to the shared state containing the full memory +/// slice and a pointer to the start of the region visible by the handle. +/// `Bytes` also tracks the length of its view into the memory. +/// +/// # Sharing +/// +/// `Bytes` contains a vtable, which allows implementations of `Bytes` to define +/// how sharing/cloning is implemented in detail. +/// When `Bytes::clone()` is called, `Bytes` will call the vtable function for +/// cloning the backing storage in order to share it behind between multiple +/// `Bytes` instances. +/// +/// For `Bytes` implementations which refer to constant memory (e.g. created +/// via `Bytes::from_static()`) the cloning implementation will be a no-op. +/// +/// For `Bytes` implementations which point to a reference counted shared storage +/// (e.g. an `Arc<[u8]>`), sharing will be implemented by increasing the +/// reference count. +/// +/// Due to this mechanism, multiple `Bytes` instances may point to the same +/// shared memory region. +/// Each `Bytes` instance can point to different sections within that +/// memory region, and `Bytes` instances may or may not have overlapping views +/// into the memory. +/// +/// The following diagram visualizes a scenario where 2 `Bytes` instances make +/// use of an `Arc`-based backing storage, and provide access to different views: +/// +/// ```text +/// +/// Arc ptrs ┌─────────┐ +/// ________________________ / │ Bytes 2 │ +/// / └─────────┘ +/// / ┌───────────┐ | | +/// |_________/ │ Bytes 1 │ | | +/// | └───────────┘ | | +/// | | | ___/ data | tail +/// | data | tail |/ | +/// v v v v +/// ┌─────┬─────┬───────────┬───────────────┬─────┐ +/// │ Arc │ │ │ │ │ +/// └─────┴─────┴───────────┴───────────────┴─────┘ +/// ``` +pub struct Bytes { + ptr: *const u8, + len: usize, + // inlined "trait object" + data: AtomicPtr<()>, + vtable: &'static Vtable, +} + +pub(crate) struct Vtable { + /// fn(data, ptr, len) + pub clone: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> Bytes, + /// fn(data, ptr, len) + /// + /// takes `Bytes` to value + pub to_vec: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> Vec, + /// fn(data, ptr, len) + pub drop: unsafe fn(&mut AtomicPtr<()>, *const u8, usize), +} + +impl Bytes { + /// Creates a new empty `Bytes`. + /// + /// This will not allocate and the returned `Bytes` handle will be empty. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let b = Bytes::new(); + /// assert_eq!(&b[..], b""); + /// ``` + #[inline] + #[cfg(not(all(loom, test)))] + pub const fn new() -> Self { + // Make it a named const to work around + // "unsizing casts are not allowed in const fn" + const EMPTY: &[u8] = &[]; + Bytes::from_static(EMPTY) + } + + #[cfg(all(loom, test))] + pub fn new() -> Self { + const EMPTY: &[u8] = &[]; + Bytes::from_static(EMPTY) + } + + /// Creates a new `Bytes` from a static slice. + /// + /// The returned `Bytes` will point directly to the static slice. There is + /// no allocating or copying. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let b = Bytes::from_static(b"hello"); + /// assert_eq!(&b[..], b"hello"); + /// ``` + #[inline] + #[cfg(not(all(loom, test)))] + pub const fn from_static(bytes: &'static [u8]) -> Self { + Bytes { + ptr: bytes.as_ptr(), + len: bytes.len(), + data: AtomicPtr::new(ptr::null_mut()), + vtable: &STATIC_VTABLE, + } + } + + #[cfg(all(loom, test))] + pub fn from_static(bytes: &'static [u8]) -> Self { + Bytes { + ptr: bytes.as_ptr(), + len: bytes.len(), + data: AtomicPtr::new(ptr::null_mut()), + vtable: &STATIC_VTABLE, + } + } + + /// Returns the number of bytes contained in this `Bytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let b = Bytes::from(&b"hello"[..]); + /// assert_eq!(b.len(), 5); + /// ``` + #[inline] + pub const fn len(&self) -> usize { + self.len + } + + /// Returns true if the `Bytes` has a length of 0. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let b = Bytes::new(); + /// assert!(b.is_empty()); + /// ``` + #[inline] + pub const fn is_empty(&self) -> bool { + self.len == 0 + } + + /// Creates `Bytes` instance from slice, by copying it. + pub fn copy_from_slice(data: &[u8]) -> Self { + data.to_vec().into() + } + + /// Returns a slice of self for the provided range. + /// + /// This will increment the reference count for the underlying memory and + /// return a new `Bytes` handle set to the slice. + /// + /// This operation is `O(1)`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let a = Bytes::from(&b"hello world"[..]); + /// let b = a.slice(2..5); + /// + /// assert_eq!(&b[..], b"llo"); + /// ``` + /// + /// # Panics + /// + /// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing + /// will panic. + pub fn slice(&self, range: impl RangeBounds) -> Self { + use core::ops::Bound; + + let len = self.len(); + + let begin = match range.start_bound() { + Bound::Included(&n) => n, + Bound::Excluded(&n) => n + 1, + Bound::Unbounded => 0, + }; + + let end = match range.end_bound() { + Bound::Included(&n) => n.checked_add(1).expect("out of range"), + Bound::Excluded(&n) => n, + Bound::Unbounded => len, + }; + + assert!( + begin <= end, + "range start must not be greater than end: {:?} <= {:?}", + begin, + end, + ); + assert!( + end <= len, + "range end out of bounds: {:?} <= {:?}", + end, + len, + ); + + if end == begin { + return Bytes::new(); + } + + let mut ret = self.clone(); + + ret.len = end - begin; + ret.ptr = unsafe { ret.ptr.add(begin) }; + + ret + } + + /// Returns a slice of self that is equivalent to the given `subset`. + /// + /// When processing a `Bytes` buffer with other tools, one often gets a + /// `&[u8]` which is in fact a slice of the `Bytes`, i.e. a subset of it. + /// This function turns that `&[u8]` into another `Bytes`, as if one had + /// called `self.slice()` with the offsets that correspond to `subset`. + /// + /// This operation is `O(1)`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let bytes = Bytes::from(&b"012345678"[..]); + /// let as_slice = bytes.as_ref(); + /// let subset = &as_slice[2..6]; + /// let subslice = bytes.slice_ref(&subset); + /// assert_eq!(&subslice[..], b"2345"); + /// ``` + /// + /// # Panics + /// + /// Requires that the given `sub` slice is in fact contained within the + /// `Bytes` buffer; otherwise this function will panic. + pub fn slice_ref(&self, subset: &[u8]) -> Self { + // Empty slice and empty Bytes may have their pointers reset + // so explicitly allow empty slice to be a subslice of any slice. + if subset.is_empty() { + return Bytes::new(); + } + + let bytes_p = self.as_ptr() as usize; + let bytes_len = self.len(); + + let sub_p = subset.as_ptr() as usize; + let sub_len = subset.len(); + + assert!( + sub_p >= bytes_p, + "subset pointer ({:p}) is smaller than self pointer ({:p})", + subset.as_ptr(), + self.as_ptr(), + ); + assert!( + sub_p + sub_len <= bytes_p + bytes_len, + "subset is out of bounds: self = ({:p}, {}), subset = ({:p}, {})", + self.as_ptr(), + bytes_len, + subset.as_ptr(), + sub_len, + ); + + let sub_offset = sub_p - bytes_p; + + self.slice(sub_offset..(sub_offset + sub_len)) + } + + /// Splits the bytes into two at the given index. + /// + /// Afterwards `self` contains elements `[0, at)`, and the returned `Bytes` + /// contains elements `[at, len)`. + /// + /// This is an `O(1)` operation that just increases the reference count and + /// sets a few indices. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let mut a = Bytes::from(&b"hello world"[..]); + /// let b = a.split_off(5); + /// + /// assert_eq!(&a[..], b"hello"); + /// assert_eq!(&b[..], b" world"); + /// ``` + /// + /// # Panics + /// + /// Panics if `at > len`. + #[must_use = "consider Bytes::truncate if you don't need the other half"] + pub fn split_off(&mut self, at: usize) -> Self { + assert!( + at <= self.len(), + "split_off out of bounds: {:?} <= {:?}", + at, + self.len(), + ); + + if at == self.len() { + return Bytes::new(); + } + + if at == 0 { + return mem::replace(self, Bytes::new()); + } + + let mut ret = self.clone(); + + self.len = at; + + unsafe { ret.inc_start(at) }; + + ret + } + + /// Splits the bytes into two at the given index. + /// + /// Afterwards `self` contains elements `[at, len)`, and the returned + /// `Bytes` contains elements `[0, at)`. + /// + /// This is an `O(1)` operation that just increases the reference count and + /// sets a few indices. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let mut a = Bytes::from(&b"hello world"[..]); + /// let b = a.split_to(5); + /// + /// assert_eq!(&a[..], b" world"); + /// assert_eq!(&b[..], b"hello"); + /// ``` + /// + /// # Panics + /// + /// Panics if `at > len`. + #[must_use = "consider Bytes::advance if you don't need the other half"] + pub fn split_to(&mut self, at: usize) -> Self { + assert!( + at <= self.len(), + "split_to out of bounds: {:?} <= {:?}", + at, + self.len(), + ); + + if at == self.len() { + return mem::replace(self, Bytes::new()); + } + + if at == 0 { + return Bytes::new(); + } + + let mut ret = self.clone(); + + unsafe { self.inc_start(at) }; + + ret.len = at; + ret + } + + /// Shortens the buffer, keeping the first `len` bytes and dropping the + /// rest. + /// + /// If `len` is greater than the buffer's current length, this has no + /// effect. + /// + /// The [`split_off`] method can emulate `truncate`, but this causes the + /// excess bytes to be returned instead of dropped. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let mut buf = Bytes::from(&b"hello world"[..]); + /// buf.truncate(5); + /// assert_eq!(buf, b"hello"[..]); + /// ``` + /// + /// [`split_off`]: #method.split_off + #[inline] + pub fn truncate(&mut self, len: usize) { + if len < self.len { + // The Vec "promotable" vtables do not store the capacity, + // so we cannot truncate while using this repr. We *have* to + // promote using `split_off` so the capacity can be stored. + if self.vtable as *const Vtable == &PROMOTABLE_EVEN_VTABLE + || self.vtable as *const Vtable == &PROMOTABLE_ODD_VTABLE + { + drop(self.split_off(len)); + } else { + self.len = len; + } + } + } + + /// Clears the buffer, removing all data. + /// + /// # Examples + /// + /// ``` + /// use bytes::Bytes; + /// + /// let mut buf = Bytes::from(&b"hello world"[..]); + /// buf.clear(); + /// assert!(buf.is_empty()); + /// ``` + #[inline] + pub fn clear(&mut self) { + self.truncate(0); + } + + #[inline] + pub(crate) unsafe fn with_vtable( + ptr: *const u8, + len: usize, + data: AtomicPtr<()>, + vtable: &'static Vtable, + ) -> Bytes { + Bytes { + ptr, + len, + data, + vtable, + } + } + + // private + + #[inline] + fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.ptr, self.len) } + } + + #[inline] + unsafe fn inc_start(&mut self, by: usize) { + // should already be asserted, but debug assert for tests + debug_assert!(self.len >= by, "internal: inc_start out of bounds"); + self.len -= by; + self.ptr = self.ptr.add(by); + } +} + +// Vtable must enforce this behavior +unsafe impl Send for Bytes {} +unsafe impl Sync for Bytes {} + +impl Drop for Bytes { + #[inline] + fn drop(&mut self) { + unsafe { (self.vtable.drop)(&mut self.data, self.ptr, self.len) } + } +} + +impl Clone for Bytes { + #[inline] + fn clone(&self) -> Bytes { + unsafe { (self.vtable.clone)(&self.data, self.ptr, self.len) } + } +} + +impl Buf for Bytes { + #[inline] + fn remaining(&self) -> usize { + self.len() + } + + #[inline] + fn chunk(&self) -> &[u8] { + self.as_slice() + } + + #[inline] + fn advance(&mut self, cnt: usize) { + assert!( + cnt <= self.len(), + "cannot advance past `remaining`: {:?} <= {:?}", + cnt, + self.len(), + ); + + unsafe { + self.inc_start(cnt); + } + } + + fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { + if len == self.remaining() { + core::mem::replace(self, Bytes::new()) + } else { + let ret = self.slice(..len); + self.advance(len); + ret + } + } +} + +impl Deref for Bytes { + type Target = [u8]; + + #[inline] + fn deref(&self) -> &[u8] { + self.as_slice() + } +} + +impl AsRef<[u8]> for Bytes { + #[inline] + fn as_ref(&self) -> &[u8] { + self.as_slice() + } +} + +impl hash::Hash for Bytes { + fn hash(&self, state: &mut H) + where + H: hash::Hasher, + { + self.as_slice().hash(state); + } +} + +impl Borrow<[u8]> for Bytes { + fn borrow(&self) -> &[u8] { + self.as_slice() + } +} + +impl IntoIterator for Bytes { + type Item = u8; + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} + +impl<'a> IntoIterator for &'a Bytes { + type Item = &'a u8; + type IntoIter = core::slice::Iter<'a, u8>; + + fn into_iter(self) -> Self::IntoIter { + self.as_slice().iter() + } +} + +impl FromIterator for Bytes { + fn from_iter>(into_iter: T) -> Self { + Vec::from_iter(into_iter).into() + } +} + +// impl Eq + +impl PartialEq for Bytes { + fn eq(&self, other: &Bytes) -> bool { + self.as_slice() == other.as_slice() + } +} + +impl PartialOrd for Bytes { + fn partial_cmp(&self, other: &Bytes) -> Option { + self.as_slice().partial_cmp(other.as_slice()) + } +} + +impl Ord for Bytes { + fn cmp(&self, other: &Bytes) -> cmp::Ordering { + self.as_slice().cmp(other.as_slice()) + } +} + +impl Eq for Bytes {} + +impl PartialEq<[u8]> for Bytes { + fn eq(&self, other: &[u8]) -> bool { + self.as_slice() == other + } +} + +impl PartialOrd<[u8]> for Bytes { + fn partial_cmp(&self, other: &[u8]) -> Option { + self.as_slice().partial_cmp(other) + } +} + +impl PartialEq for [u8] { + fn eq(&self, other: &Bytes) -> bool { + *other == *self + } +} + +impl PartialOrd for [u8] { + fn partial_cmp(&self, other: &Bytes) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) + } +} + +impl PartialEq for Bytes { + fn eq(&self, other: &str) -> bool { + self.as_slice() == other.as_bytes() + } +} + +impl PartialOrd for Bytes { + fn partial_cmp(&self, other: &str) -> Option { + self.as_slice().partial_cmp(other.as_bytes()) + } +} + +impl PartialEq for str { + fn eq(&self, other: &Bytes) -> bool { + *other == *self + } +} + +impl PartialOrd for str { + fn partial_cmp(&self, other: &Bytes) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) + } +} + +impl PartialEq> for Bytes { + fn eq(&self, other: &Vec) -> bool { + *self == other[..] + } +} + +impl PartialOrd> for Bytes { + fn partial_cmp(&self, other: &Vec) -> Option { + self.as_slice().partial_cmp(&other[..]) + } +} + +impl PartialEq for Vec { + fn eq(&self, other: &Bytes) -> bool { + *other == *self + } +} + +impl PartialOrd for Vec { + fn partial_cmp(&self, other: &Bytes) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) + } +} + +impl PartialEq for Bytes { + fn eq(&self, other: &String) -> bool { + *self == other[..] + } +} + +impl PartialOrd for Bytes { + fn partial_cmp(&self, other: &String) -> Option { + self.as_slice().partial_cmp(other.as_bytes()) + } +} + +impl PartialEq for String { + fn eq(&self, other: &Bytes) -> bool { + *other == *self + } +} + +impl PartialOrd for String { + fn partial_cmp(&self, other: &Bytes) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) + } +} + +impl PartialEq for &[u8] { + fn eq(&self, other: &Bytes) -> bool { + *other == *self + } +} + +impl PartialOrd for &[u8] { + fn partial_cmp(&self, other: &Bytes) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) + } +} + +impl PartialEq for &str { + fn eq(&self, other: &Bytes) -> bool { + *other == *self + } +} + +impl PartialOrd for &str { + fn partial_cmp(&self, other: &Bytes) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) + } +} + +impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes +where + Bytes: PartialEq, +{ + fn eq(&self, other: &&'a T) -> bool { + *self == **other + } +} + +impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes +where + Bytes: PartialOrd, +{ + fn partial_cmp(&self, other: &&'a T) -> Option { + self.partial_cmp(&**other) + } +} + +// impl From + +impl Default for Bytes { + #[inline] + fn default() -> Bytes { + Bytes::new() + } +} + +impl From<&'static [u8]> for Bytes { + fn from(slice: &'static [u8]) -> Bytes { + Bytes::from_static(slice) + } +} + +impl From<&'static str> for Bytes { + fn from(slice: &'static str) -> Bytes { + Bytes::from_static(slice.as_bytes()) + } +} + +impl From> for Bytes { + fn from(vec: Vec) -> Bytes { + let mut vec = vec; + let ptr = vec.as_mut_ptr(); + let len = vec.len(); + let cap = vec.capacity(); + + // Avoid an extra allocation if possible. + if len == cap { + return Bytes::from(vec.into_boxed_slice()); + } + + let shared = Box::new(Shared { + buf: ptr, + cap, + ref_cnt: AtomicUsize::new(1), + }); + mem::forget(vec); + + let shared = Box::into_raw(shared); + // The pointer should be aligned, so this assert should + // always succeed. + debug_assert!( + 0 == (shared as usize & KIND_MASK), + "internal: Box should have an aligned pointer", + ); + Bytes { + ptr, + len, + data: AtomicPtr::new(shared as _), + vtable: &SHARED_VTABLE, + } + } +} + +impl From> for Bytes { + fn from(slice: Box<[u8]>) -> Bytes { + // Box<[u8]> doesn't contain a heap allocation for empty slices, + // so the pointer isn't aligned enough for the KIND_VEC stashing to + // work. + if slice.is_empty() { + return Bytes::new(); + } + + let len = slice.len(); + let ptr = Box::into_raw(slice) as *mut u8; + + if ptr as usize & 0x1 == 0 { + let data = ptr_map(ptr, |addr| addr | KIND_VEC); + Bytes { + ptr, + len, + data: AtomicPtr::new(data.cast()), + vtable: &PROMOTABLE_EVEN_VTABLE, + } + } else { + Bytes { + ptr, + len, + data: AtomicPtr::new(ptr.cast()), + vtable: &PROMOTABLE_ODD_VTABLE, + } + } + } +} + +impl From for Bytes { + fn from(s: String) -> Bytes { + Bytes::from(s.into_bytes()) + } +} + +impl From for Vec { + fn from(bytes: Bytes) -> Vec { + let bytes = mem::ManuallyDrop::new(bytes); + unsafe { (bytes.vtable.to_vec)(&bytes.data, bytes.ptr, bytes.len) } + } +} + +// ===== impl Vtable ===== + +impl fmt::Debug for Vtable { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Vtable") + .field("clone", &(self.clone as *const ())) + .field("drop", &(self.drop as *const ())) + .finish() + } +} + +// ===== impl StaticVtable ===== + +const STATIC_VTABLE: Vtable = Vtable { + clone: static_clone, + to_vec: static_to_vec, + drop: static_drop, +}; + +unsafe fn static_clone(_: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { + let slice = slice::from_raw_parts(ptr, len); + Bytes::from_static(slice) +} + +unsafe fn static_to_vec(_: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Vec { + let slice = slice::from_raw_parts(ptr, len); + slice.to_vec() +} + +unsafe fn static_drop(_: &mut AtomicPtr<()>, _: *const u8, _: usize) { + // nothing to drop for &'static [u8] +} + +// ===== impl PromotableVtable ===== + +static PROMOTABLE_EVEN_VTABLE: Vtable = Vtable { + clone: promotable_even_clone, + to_vec: promotable_even_to_vec, + drop: promotable_even_drop, +}; + +static PROMOTABLE_ODD_VTABLE: Vtable = Vtable { + clone: promotable_odd_clone, + to_vec: promotable_odd_to_vec, + drop: promotable_odd_drop, +}; + +unsafe fn promotable_even_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { + let shared = data.load(Ordering::Acquire); + let kind = shared as usize & KIND_MASK; + + if kind == KIND_ARC { + shallow_clone_arc(shared.cast(), ptr, len) + } else { + debug_assert_eq!(kind, KIND_VEC); + let buf = ptr_map(shared.cast(), |addr| addr & !KIND_MASK); + shallow_clone_vec(data, shared, buf, ptr, len) + } +} + +unsafe fn promotable_to_vec( + data: &AtomicPtr<()>, + ptr: *const u8, + len: usize, + f: fn(*mut ()) -> *mut u8, +) -> Vec { + let shared = data.load(Ordering::Acquire); + let kind = shared as usize & KIND_MASK; + + if kind == KIND_ARC { + shared_to_vec_impl(shared.cast(), ptr, len) + } else { + // If Bytes holds a Vec, then the offset must be 0. + debug_assert_eq!(kind, KIND_VEC); + + let buf = f(shared); + + let cap = (ptr as usize - buf as usize) + len; + + // Copy back buffer + ptr::copy(ptr, buf, len); + + Vec::from_raw_parts(buf, len, cap) + } +} + +unsafe fn promotable_even_to_vec(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Vec { + promotable_to_vec(data, ptr, len, |shared| { + ptr_map(shared.cast(), |addr| addr & !KIND_MASK) + }) +} + +unsafe fn promotable_even_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: usize) { + data.with_mut(|shared| { + let shared = *shared; + let kind = shared as usize & KIND_MASK; + + if kind == KIND_ARC { + release_shared(shared.cast()); + } else { + debug_assert_eq!(kind, KIND_VEC); + let buf = ptr_map(shared.cast(), |addr| addr & !KIND_MASK); + free_boxed_slice(buf, ptr, len); + } + }); +} + +unsafe fn promotable_odd_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { + let shared = data.load(Ordering::Acquire); + let kind = shared as usize & KIND_MASK; + + if kind == KIND_ARC { + shallow_clone_arc(shared as _, ptr, len) + } else { + debug_assert_eq!(kind, KIND_VEC); + shallow_clone_vec(data, shared, shared.cast(), ptr, len) + } +} + +unsafe fn promotable_odd_to_vec(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Vec { + promotable_to_vec(data, ptr, len, |shared| shared.cast()) +} + +unsafe fn promotable_odd_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: usize) { + data.with_mut(|shared| { + let shared = *shared; + let kind = shared as usize & KIND_MASK; + + if kind == KIND_ARC { + release_shared(shared.cast()); + } else { + debug_assert_eq!(kind, KIND_VEC); + + free_boxed_slice(shared.cast(), ptr, len); + } + }); +} + +unsafe fn free_boxed_slice(buf: *mut u8, offset: *const u8, len: usize) { + let cap = (offset as usize - buf as usize) + len; + dealloc(buf, Layout::from_size_align(cap, 1).unwrap()) +} + +// ===== impl SharedVtable ===== + +struct Shared { + // Holds arguments to dealloc upon Drop, but otherwise doesn't use them + buf: *mut u8, + cap: usize, + ref_cnt: AtomicUsize, +} + +impl Drop for Shared { + fn drop(&mut self) { + unsafe { dealloc(self.buf, Layout::from_size_align(self.cap, 1).unwrap()) } + } +} + +// Assert that the alignment of `Shared` is divisible by 2. +// This is a necessary invariant since we depend on allocating `Shared` a +// shared object to implicitly carry the `KIND_ARC` flag in its pointer. +// This flag is set when the LSB is 0. +const _: [(); 0 - mem::align_of::() % 2] = []; // Assert that the alignment of `Shared` is divisible by 2. + +static SHARED_VTABLE: Vtable = Vtable { + clone: shared_clone, + to_vec: shared_to_vec, + drop: shared_drop, +}; + +const KIND_ARC: usize = 0b0; +const KIND_VEC: usize = 0b1; +const KIND_MASK: usize = 0b1; + +unsafe fn shared_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { + let shared = data.load(Ordering::Relaxed); + shallow_clone_arc(shared as _, ptr, len) +} + +unsafe fn shared_to_vec_impl(shared: *mut Shared, ptr: *const u8, len: usize) -> Vec { + // Check that the ref_cnt is 1 (unique). + // + // If it is unique, then it is set to 0 with AcqRel fence for the same + // reason in release_shared. + // + // Otherwise, we take the other branch and call release_shared. + if (*shared) + .ref_cnt + .compare_exchange(1, 0, Ordering::AcqRel, Ordering::Relaxed) + .is_ok() + { + let buf = (*shared).buf; + let cap = (*shared).cap; + + // Deallocate Shared + drop(Box::from_raw(shared as *mut mem::ManuallyDrop)); + + // Copy back buffer + ptr::copy(ptr, buf, len); + + Vec::from_raw_parts(buf, len, cap) + } else { + let v = slice::from_raw_parts(ptr, len).to_vec(); + release_shared(shared); + v + } +} + +unsafe fn shared_to_vec(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Vec { + shared_to_vec_impl(data.load(Ordering::Relaxed).cast(), ptr, len) +} + +unsafe fn shared_drop(data: &mut AtomicPtr<()>, _ptr: *const u8, _len: usize) { + data.with_mut(|shared| { + release_shared(shared.cast()); + }); +} + +unsafe fn shallow_clone_arc(shared: *mut Shared, ptr: *const u8, len: usize) -> Bytes { + let old_size = (*shared).ref_cnt.fetch_add(1, Ordering::Relaxed); + + if old_size > usize::MAX >> 1 { + crate::abort(); + } + + Bytes { + ptr, + len, + data: AtomicPtr::new(shared as _), + vtable: &SHARED_VTABLE, + } +} + +#[cold] +unsafe fn shallow_clone_vec( + atom: &AtomicPtr<()>, + ptr: *const (), + buf: *mut u8, + offset: *const u8, + len: usize, +) -> Bytes { + // If the buffer is still tracked in a `Vec`. It is time to + // promote the vec to an `Arc`. This could potentially be called + // concurrently, so some care must be taken. + + // First, allocate a new `Shared` instance containing the + // `Vec` fields. It's important to note that `ptr`, `len`, + // and `cap` cannot be mutated without having `&mut self`. + // This means that these fields will not be concurrently + // updated and since the buffer hasn't been promoted to an + // `Arc`, those three fields still are the components of the + // vector. + let shared = Box::new(Shared { + buf, + cap: (offset as usize - buf as usize) + len, + // Initialize refcount to 2. One for this reference, and one + // for the new clone that will be returned from + // `shallow_clone`. + ref_cnt: AtomicUsize::new(2), + }); + + let shared = Box::into_raw(shared); + + // The pointer should be aligned, so this assert should + // always succeed. + debug_assert!( + 0 == (shared as usize & KIND_MASK), + "internal: Box should have an aligned pointer", + ); + + // Try compare & swapping the pointer into the `arc` field. + // `Release` is used synchronize with other threads that + // will load the `arc` field. + // + // If the `compare_exchange` fails, then the thread lost the + // race to promote the buffer to shared. The `Acquire` + // ordering will synchronize with the `compare_exchange` + // that happened in the other thread and the `Shared` + // pointed to by `actual` will be visible. + match atom.compare_exchange(ptr as _, shared as _, Ordering::AcqRel, Ordering::Acquire) { + Ok(actual) => { + debug_assert!(actual as usize == ptr as usize); + // The upgrade was successful, the new handle can be + // returned. + Bytes { + ptr: offset, + len, + data: AtomicPtr::new(shared as _), + vtable: &SHARED_VTABLE, + } + } + Err(actual) => { + // The upgrade failed, a concurrent clone happened. Release + // the allocation that was made in this thread, it will not + // be needed. + let shared = Box::from_raw(shared); + mem::forget(*shared); + + // Buffer already promoted to shared storage, so increment ref + // count. + shallow_clone_arc(actual as _, offset, len) + } + } +} + +unsafe fn release_shared(ptr: *mut Shared) { + // `Shared` storage... follow the drop steps from Arc. + if (*ptr).ref_cnt.fetch_sub(1, Ordering::Release) != 1 { + return; + } + + // This fence is needed to prevent reordering of use of the data and + // deletion of the data. Because it is marked `Release`, the decreasing + // of the reference count synchronizes with this `Acquire` fence. This + // means that use of the data happens before decreasing the reference + // count, which happens before this fence, which happens before the + // deletion of the data. + // + // As explained in the [Boost documentation][1], + // + // > It is important to enforce any possible access to the object in one + // > thread (through an existing reference) to *happen before* deleting + // > the object in a different thread. This is achieved by a "release" + // > operation after dropping a reference (any access to the object + // > through this reference must obviously happened before), and an + // > "acquire" operation before deleting the object. + // + // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) + // + // Thread sanitizer does not support atomic fences. Use an atomic load + // instead. + (*ptr).ref_cnt.load(Ordering::Acquire); + + // Drop the data + drop(Box::from_raw(ptr)); +} + +// Ideally we would always use this version of `ptr_map` since it is strict +// provenance compatible, but it results in worse codegen. We will however still +// use it on miri because it gives better diagnostics for people who test bytes +// code with miri. +// +// See https://github.com/tokio-rs/bytes/pull/545 for more info. +#[cfg(miri)] +fn ptr_map(ptr: *mut u8, f: F) -> *mut u8 +where + F: FnOnce(usize) -> usize, +{ + let old_addr = ptr as usize; + let new_addr = f(old_addr); + let diff = new_addr.wrapping_sub(old_addr); + ptr.wrapping_add(diff) +} + +#[cfg(not(miri))] +fn ptr_map(ptr: *mut u8, f: F) -> *mut u8 +where + F: FnOnce(usize) -> usize, +{ + let old_addr = ptr as usize; + let new_addr = f(old_addr); + new_addr as *mut u8 +} + +// compile-fails + +/// ```compile_fail +/// use bytes::Bytes; +/// #[deny(unused_must_use)] +/// { +/// let mut b1 = Bytes::from("hello world"); +/// b1.split_to(6); +/// } +/// ``` +fn _split_to_must_use() {} + +/// ```compile_fail +/// use bytes::Bytes; +/// #[deny(unused_must_use)] +/// { +/// let mut b1 = Bytes::from("hello world"); +/// b1.split_off(6); +/// } +/// ``` +fn _split_off_must_use() {} + +// fuzz tests +#[cfg(all(test, loom))] +mod fuzz { + use loom::sync::Arc; + use loom::thread; + + use super::Bytes; + #[test] + fn bytes_cloning_vec() { + loom::model(|| { + let a = Bytes::from(b"abcdefgh".to_vec()); + let addr = a.as_ptr() as usize; + + // test the Bytes::clone is Sync by putting it in an Arc + let a1 = Arc::new(a); + let a2 = a1.clone(); + + let t1 = thread::spawn(move || { + let b: Bytes = (*a1).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + let t2 = thread::spawn(move || { + let b: Bytes = (*a2).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + t1.join().unwrap(); + t2.join().unwrap(); + }); + } +} diff --git a/third_party/rust/bytes/v1/crate/src/bytes_mut.rs b/third_party/rust/bytes/v1/crate/src/bytes_mut.rs new file mode 100644 index 000000000000..c5c2e52fc243 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/bytes_mut.rs @@ -0,0 +1,1813 @@ +use core::iter::{FromIterator, Iterator}; +use core::mem::{self, ManuallyDrop, MaybeUninit}; +use core::ops::{Deref, DerefMut}; +use core::ptr::{self, NonNull}; +use core::{cmp, fmt, hash, isize, slice, usize}; + +use alloc::{ + borrow::{Borrow, BorrowMut}, + boxed::Box, + string::String, + vec, + vec::Vec, +}; + +use crate::buf::{IntoIter, UninitSlice}; +use crate::bytes::Vtable; +#[allow(unused)] +use crate::loom::sync::atomic::AtomicMut; +use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; +use crate::{Buf, BufMut, Bytes}; + +/// A unique reference to a contiguous slice of memory. +/// +/// `BytesMut` represents a unique view into a potentially shared memory region. +/// Given the uniqueness guarantee, owners of `BytesMut` handles are able to +/// mutate the memory. +/// +/// `BytesMut` can be thought of as containing a `buf: Arc>`, an offset +/// into `buf`, a slice length, and a guarantee that no other `BytesMut` for the +/// same `buf` overlaps with its slice. That guarantee means that a write lock +/// is not required. +/// +/// # Growth +/// +/// `BytesMut`'s `BufMut` implementation will implicitly grow its buffer as +/// necessary. However, explicitly reserving the required space up-front before +/// a series of inserts will be more efficient. +/// +/// # Examples +/// +/// ``` +/// use bytes::{BytesMut, BufMut}; +/// +/// let mut buf = BytesMut::with_capacity(64); +/// +/// buf.put_u8(b'h'); +/// buf.put_u8(b'e'); +/// buf.put(&b"llo"[..]); +/// +/// assert_eq!(&buf[..], b"hello"); +/// +/// // Freeze the buffer so that it can be shared +/// let a = buf.freeze(); +/// +/// // This does not allocate, instead `b` points to the same memory. +/// let b = a.clone(); +/// +/// assert_eq!(&a[..], b"hello"); +/// assert_eq!(&b[..], b"hello"); +/// ``` +pub struct BytesMut { + ptr: NonNull, + len: usize, + cap: usize, + data: *mut Shared, +} + +// Thread-safe reference-counted container for the shared storage. This mostly +// the same as `core::sync::Arc` but without the weak counter. The ref counting +// fns are based on the ones found in `std`. +// +// The main reason to use `Shared` instead of `core::sync::Arc` is that it ends +// up making the overall code simpler and easier to reason about. This is due to +// some of the logic around setting `Inner::arc` and other ways the `arc` field +// is used. Using `Arc` ended up requiring a number of funky transmutes and +// other shenanigans to make it work. +struct Shared { + vec: Vec, + original_capacity_repr: usize, + ref_count: AtomicUsize, +} + +// Buffer storage strategy flags. +const KIND_ARC: usize = 0b0; +const KIND_VEC: usize = 0b1; +const KIND_MASK: usize = 0b1; + +// The max original capacity value. Any `Bytes` allocated with a greater initial +// capacity will default to this. +const MAX_ORIGINAL_CAPACITY_WIDTH: usize = 17; +// The original capacity algorithm will not take effect unless the originally +// allocated capacity was at least 1kb in size. +const MIN_ORIGINAL_CAPACITY_WIDTH: usize = 10; +// The original capacity is stored in powers of 2 starting at 1kb to a max of +// 64kb. Representing it as such requires only 3 bits of storage. +const ORIGINAL_CAPACITY_MASK: usize = 0b11100; +const ORIGINAL_CAPACITY_OFFSET: usize = 2; + +// When the storage is in the `Vec` representation, the pointer can be advanced +// at most this value. This is due to the amount of storage available to track +// the offset is usize - number of KIND bits and number of ORIGINAL_CAPACITY +// bits. +const VEC_POS_OFFSET: usize = 5; +const MAX_VEC_POS: usize = usize::MAX >> VEC_POS_OFFSET; +const NOT_VEC_POS_MASK: usize = 0b11111; + +#[cfg(target_pointer_width = "64")] +const PTR_WIDTH: usize = 64; +#[cfg(target_pointer_width = "32")] +const PTR_WIDTH: usize = 32; + +/* + * + * ===== BytesMut ===== + * + */ + +impl BytesMut { + /// Creates a new `BytesMut` with the specified capacity. + /// + /// The returned `BytesMut` will be able to hold at least `capacity` bytes + /// without reallocating. + /// + /// It is important to note that this function does not specify the length + /// of the returned `BytesMut`, but only the capacity. + /// + /// # Examples + /// + /// ``` + /// use bytes::{BytesMut, BufMut}; + /// + /// let mut bytes = BytesMut::with_capacity(64); + /// + /// // `bytes` contains no data, even though there is capacity + /// assert_eq!(bytes.len(), 0); + /// + /// bytes.put(&b"hello world"[..]); + /// + /// assert_eq!(&bytes[..], b"hello world"); + /// ``` + #[inline] + pub fn with_capacity(capacity: usize) -> BytesMut { + BytesMut::from_vec(Vec::with_capacity(capacity)) + } + + /// Creates a new `BytesMut` with default capacity. + /// + /// Resulting object has length 0 and unspecified capacity. + /// This function does not allocate. + /// + /// # Examples + /// + /// ``` + /// use bytes::{BytesMut, BufMut}; + /// + /// let mut bytes = BytesMut::new(); + /// + /// assert_eq!(0, bytes.len()); + /// + /// bytes.reserve(2); + /// bytes.put_slice(b"xy"); + /// + /// assert_eq!(&b"xy"[..], &bytes[..]); + /// ``` + #[inline] + pub fn new() -> BytesMut { + BytesMut::with_capacity(0) + } + + /// Returns the number of bytes contained in this `BytesMut`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let b = BytesMut::from(&b"hello"[..]); + /// assert_eq!(b.len(), 5); + /// ``` + #[inline] + pub fn len(&self) -> usize { + self.len + } + + /// Returns true if the `BytesMut` has a length of 0. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let b = BytesMut::with_capacity(64); + /// assert!(b.is_empty()); + /// ``` + #[inline] + pub fn is_empty(&self) -> bool { + self.len == 0 + } + + /// Returns the number of bytes the `BytesMut` can hold without reallocating. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let b = BytesMut::with_capacity(64); + /// assert_eq!(b.capacity(), 64); + /// ``` + #[inline] + pub fn capacity(&self) -> usize { + self.cap + } + + /// Converts `self` into an immutable `Bytes`. + /// + /// The conversion is zero cost and is used to indicate that the slice + /// referenced by the handle will no longer be mutated. Once the conversion + /// is done, the handle can be cloned and shared across threads. + /// + /// # Examples + /// + /// ``` + /// use bytes::{BytesMut, BufMut}; + /// use std::thread; + /// + /// let mut b = BytesMut::with_capacity(64); + /// b.put(&b"hello world"[..]); + /// let b1 = b.freeze(); + /// let b2 = b1.clone(); + /// + /// let th = thread::spawn(move || { + /// assert_eq!(&b1[..], b"hello world"); + /// }); + /// + /// assert_eq!(&b2[..], b"hello world"); + /// th.join().unwrap(); + /// ``` + #[inline] + pub fn freeze(mut self) -> Bytes { + if self.kind() == KIND_VEC { + // Just re-use `Bytes` internal Vec vtable + unsafe { + let (off, _) = self.get_vec_pos(); + let vec = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off); + mem::forget(self); + let mut b: Bytes = vec.into(); + b.advance(off); + b + } + } else { + debug_assert_eq!(self.kind(), KIND_ARC); + + let ptr = self.ptr.as_ptr(); + let len = self.len; + let data = AtomicPtr::new(self.data.cast()); + mem::forget(self); + unsafe { Bytes::with_vtable(ptr, len, data, &SHARED_VTABLE) } + } + } + + /// Creates a new `BytesMut`, which is initialized with zero. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let zeros = BytesMut::zeroed(42); + /// + /// assert_eq!(zeros.len(), 42); + /// zeros.into_iter().for_each(|x| assert_eq!(x, 0)); + /// ``` + pub fn zeroed(len: usize) -> BytesMut { + BytesMut::from_vec(vec![0; len]) + } + + /// Splits the bytes into two at the given index. + /// + /// Afterwards `self` contains elements `[0, at)`, and the returned + /// `BytesMut` contains elements `[at, capacity)`. + /// + /// This is an `O(1)` operation that just increases the reference count + /// and sets a few indices. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut a = BytesMut::from(&b"hello world"[..]); + /// let mut b = a.split_off(5); + /// + /// a[0] = b'j'; + /// b[0] = b'!'; + /// + /// assert_eq!(&a[..], b"jello"); + /// assert_eq!(&b[..], b"!world"); + /// ``` + /// + /// # Panics + /// + /// Panics if `at > capacity`. + #[must_use = "consider BytesMut::truncate if you don't need the other half"] + pub fn split_off(&mut self, at: usize) -> BytesMut { + assert!( + at <= self.capacity(), + "split_off out of bounds: {:?} <= {:?}", + at, + self.capacity(), + ); + unsafe { + let mut other = self.shallow_clone(); + other.set_start(at); + self.set_end(at); + other + } + } + + /// Removes the bytes from the current view, returning them in a new + /// `BytesMut` handle. + /// + /// Afterwards, `self` will be empty, but will retain any additional + /// capacity that it had before the operation. This is identical to + /// `self.split_to(self.len())`. + /// + /// This is an `O(1)` operation that just increases the reference count and + /// sets a few indices. + /// + /// # Examples + /// + /// ``` + /// use bytes::{BytesMut, BufMut}; + /// + /// let mut buf = BytesMut::with_capacity(1024); + /// buf.put(&b"hello world"[..]); + /// + /// let other = buf.split(); + /// + /// assert!(buf.is_empty()); + /// assert_eq!(1013, buf.capacity()); + /// + /// assert_eq!(other, b"hello world"[..]); + /// ``` + #[must_use = "consider BytesMut::advance(len()) if you don't need the other half"] + pub fn split(&mut self) -> BytesMut { + let len = self.len(); + self.split_to(len) + } + + /// Splits the buffer into two at the given index. + /// + /// Afterwards `self` contains elements `[at, len)`, and the returned `BytesMut` + /// contains elements `[0, at)`. + /// + /// This is an `O(1)` operation that just increases the reference count and + /// sets a few indices. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut a = BytesMut::from(&b"hello world"[..]); + /// let mut b = a.split_to(5); + /// + /// a[0] = b'!'; + /// b[0] = b'j'; + /// + /// assert_eq!(&a[..], b"!world"); + /// assert_eq!(&b[..], b"jello"); + /// ``` + /// + /// # Panics + /// + /// Panics if `at > len`. + #[must_use = "consider BytesMut::advance if you don't need the other half"] + pub fn split_to(&mut self, at: usize) -> BytesMut { + assert!( + at <= self.len(), + "split_to out of bounds: {:?} <= {:?}", + at, + self.len(), + ); + + unsafe { + let mut other = self.shallow_clone(); + other.set_end(at); + self.set_start(at); + other + } + } + + /// Shortens the buffer, keeping the first `len` bytes and dropping the + /// rest. + /// + /// If `len` is greater than the buffer's current length, this has no + /// effect. + /// + /// Existing underlying capacity is preserved. + /// + /// The [`split_off`] method can emulate `truncate`, but this causes the + /// excess bytes to be returned instead of dropped. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::from(&b"hello world"[..]); + /// buf.truncate(5); + /// assert_eq!(buf, b"hello"[..]); + /// ``` + /// + /// [`split_off`]: #method.split_off + pub fn truncate(&mut self, len: usize) { + if len <= self.len() { + unsafe { + self.set_len(len); + } + } + } + + /// Clears the buffer, removing all data. Existing capacity is preserved. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::from(&b"hello world"[..]); + /// buf.clear(); + /// assert!(buf.is_empty()); + /// ``` + pub fn clear(&mut self) { + self.truncate(0); + } + + /// Resizes the buffer so that `len` is equal to `new_len`. + /// + /// If `new_len` is greater than `len`, the buffer is extended by the + /// difference with each additional byte set to `value`. If `new_len` is + /// less than `len`, the buffer is simply truncated. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::new(); + /// + /// buf.resize(3, 0x1); + /// assert_eq!(&buf[..], &[0x1, 0x1, 0x1]); + /// + /// buf.resize(2, 0x2); + /// assert_eq!(&buf[..], &[0x1, 0x1]); + /// + /// buf.resize(4, 0x3); + /// assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]); + /// ``` + pub fn resize(&mut self, new_len: usize, value: u8) { + let len = self.len(); + if new_len > len { + let additional = new_len - len; + self.reserve(additional); + unsafe { + let dst = self.chunk_mut().as_mut_ptr(); + ptr::write_bytes(dst, value, additional); + self.set_len(new_len); + } + } else { + self.truncate(new_len); + } + } + + /// Sets the length of the buffer. + /// + /// This will explicitly set the size of the buffer without actually + /// modifying the data, so it is up to the caller to ensure that the data + /// has been initialized. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut b = BytesMut::from(&b"hello world"[..]); + /// + /// unsafe { + /// b.set_len(5); + /// } + /// + /// assert_eq!(&b[..], b"hello"); + /// + /// unsafe { + /// b.set_len(11); + /// } + /// + /// assert_eq!(&b[..], b"hello world"); + /// ``` + #[inline] + pub unsafe fn set_len(&mut self, len: usize) { + debug_assert!(len <= self.cap, "set_len out of bounds"); + self.len = len; + } + + /// Reserves capacity for at least `additional` more bytes to be inserted + /// into the given `BytesMut`. + /// + /// More than `additional` bytes may be reserved in order to avoid frequent + /// reallocations. A call to `reserve` may result in an allocation. + /// + /// Before allocating new buffer space, the function will attempt to reclaim + /// space in the existing buffer. If the current handle references a view + /// into a larger original buffer, and all other handles referencing part + /// of the same original buffer have been dropped, then the current view + /// can be copied/shifted to the front of the buffer and the handle can take + /// ownership of the full buffer, provided that the full buffer is large + /// enough to fit the requested additional capacity. + /// + /// This optimization will only happen if shifting the data from the current + /// view to the front of the buffer is not too expensive in terms of the + /// (amortized) time required. The precise condition is subject to change; + /// as of now, the length of the data being shifted needs to be at least as + /// large as the distance that it's shifted by. If the current view is empty + /// and the original buffer is large enough to fit the requested additional + /// capacity, then reallocations will never happen. + /// + /// # Examples + /// + /// In the following example, a new buffer is allocated. + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::from(&b"hello"[..]); + /// buf.reserve(64); + /// assert!(buf.capacity() >= 69); + /// ``` + /// + /// In the following example, the existing buffer is reclaimed. + /// + /// ``` + /// use bytes::{BytesMut, BufMut}; + /// + /// let mut buf = BytesMut::with_capacity(128); + /// buf.put(&[0; 64][..]); + /// + /// let ptr = buf.as_ptr(); + /// let other = buf.split(); + /// + /// assert!(buf.is_empty()); + /// assert_eq!(buf.capacity(), 64); + /// + /// drop(other); + /// buf.reserve(128); + /// + /// assert_eq!(buf.capacity(), 128); + /// assert_eq!(buf.as_ptr(), ptr); + /// ``` + /// + /// # Panics + /// + /// Panics if the new capacity overflows `usize`. + #[inline] + pub fn reserve(&mut self, additional: usize) { + let len = self.len(); + let rem = self.capacity() - len; + + if additional <= rem { + // The handle can already store at least `additional` more bytes, so + // there is no further work needed to be done. + return; + } + + self.reserve_inner(additional); + } + + // In separate function to allow the short-circuits in `reserve` to + // be inline-able. Significant helps performance. + fn reserve_inner(&mut self, additional: usize) { + let len = self.len(); + let kind = self.kind(); + + if kind == KIND_VEC { + // If there's enough free space before the start of the buffer, then + // just copy the data backwards and reuse the already-allocated + // space. + // + // Otherwise, since backed by a vector, use `Vec::reserve` + // + // We need to make sure that this optimization does not kill the + // amortized runtimes of BytesMut's operations. + unsafe { + let (off, prev) = self.get_vec_pos(); + + // Only reuse space if we can satisfy the requested additional space. + // + // Also check if the value of `off` suggests that enough bytes + // have been read to account for the overhead of shifting all + // the data (in an amortized analysis). + // Hence the condition `off >= self.len()`. + // + // This condition also already implies that the buffer is going + // to be (at least) half-empty in the end; so we do not break + // the (amortized) runtime with future resizes of the underlying + // `Vec`. + // + // [For more details check issue #524, and PR #525.] + if self.capacity() - self.len() + off >= additional && off >= self.len() { + // There's enough space, and it's not too much overhead: + // reuse the space! + // + // Just move the pointer back to the start after copying + // data back. + let base_ptr = self.ptr.as_ptr().offset(-(off as isize)); + // Since `off >= self.len()`, the two regions don't overlap. + ptr::copy_nonoverlapping(self.ptr.as_ptr(), base_ptr, self.len); + self.ptr = vptr(base_ptr); + self.set_vec_pos(0, prev); + + // Length stays constant, but since we moved backwards we + // can gain capacity back. + self.cap += off; + } else { + // Not enough space, or reusing might be too much overhead: + // allocate more space! + let mut v = + ManuallyDrop::new(rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off)); + v.reserve(additional); + + // Update the info + self.ptr = vptr(v.as_mut_ptr().add(off)); + self.len = v.len() - off; + self.cap = v.capacity() - off; + } + + return; + } + } + + debug_assert_eq!(kind, KIND_ARC); + let shared: *mut Shared = self.data; + + // Reserving involves abandoning the currently shared buffer and + // allocating a new vector with the requested capacity. + // + // Compute the new capacity + let mut new_cap = len.checked_add(additional).expect("overflow"); + + let original_capacity; + let original_capacity_repr; + + unsafe { + original_capacity_repr = (*shared).original_capacity_repr; + original_capacity = original_capacity_from_repr(original_capacity_repr); + + // First, try to reclaim the buffer. This is possible if the current + // handle is the only outstanding handle pointing to the buffer. + if (*shared).is_unique() { + // This is the only handle to the buffer. It can be reclaimed. + // However, before doing the work of copying data, check to make + // sure that the vector has enough capacity. + let v = &mut (*shared).vec; + + let v_capacity = v.capacity(); + let ptr = v.as_mut_ptr(); + + let offset = offset_from(self.ptr.as_ptr(), ptr); + + // Compare the condition in the `kind == KIND_VEC` case above + // for more details. + if v_capacity >= new_cap + offset { + self.cap = new_cap; + // no copy is necessary + } else if v_capacity >= new_cap && offset >= len { + // The capacity is sufficient, and copying is not too much + // overhead: reclaim the buffer! + + // `offset >= len` means: no overlap + ptr::copy_nonoverlapping(self.ptr.as_ptr(), ptr, len); + + self.ptr = vptr(ptr); + self.cap = v.capacity(); + } else { + // calculate offset + let off = (self.ptr.as_ptr() as usize) - (v.as_ptr() as usize); + + // new_cap is calculated in terms of `BytesMut`, not the underlying + // `Vec`, so it does not take the offset into account. + // + // Thus we have to manually add it here. + new_cap = new_cap.checked_add(off).expect("overflow"); + + // The vector capacity is not sufficient. The reserve request is + // asking for more than the initial buffer capacity. Allocate more + // than requested if `new_cap` is not much bigger than the current + // capacity. + // + // There are some situations, using `reserve_exact` that the + // buffer capacity could be below `original_capacity`, so do a + // check. + let double = v.capacity().checked_shl(1).unwrap_or(new_cap); + + new_cap = cmp::max(double, new_cap); + + // No space - allocate more + // + // The length field of `Shared::vec` is not used by the `BytesMut`; + // instead we use the `len` field in the `BytesMut` itself. However, + // when calling `reserve`, it doesn't guarantee that data stored in + // the unused capacity of the vector is copied over to the new + // allocation, so we need to ensure that we don't have any data we + // care about in the unused capacity before calling `reserve`. + debug_assert!(off + len <= v.capacity()); + v.set_len(off + len); + v.reserve(new_cap - v.len()); + + // Update the info + self.ptr = vptr(v.as_mut_ptr().add(off)); + self.cap = v.capacity() - off; + } + + return; + } else { + new_cap = cmp::max(new_cap, original_capacity); + } + } + + // Create a new vector to store the data + let mut v = ManuallyDrop::new(Vec::with_capacity(new_cap)); + + // Copy the bytes + v.extend_from_slice(self.as_ref()); + + // Release the shared handle. This must be done *after* the bytes are + // copied. + unsafe { release_shared(shared) }; + + // Update self + let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC; + self.data = invalid_ptr(data); + self.ptr = vptr(v.as_mut_ptr()); + self.len = v.len(); + self.cap = v.capacity(); + } + + /// Appends given bytes to this `BytesMut`. + /// + /// If this `BytesMut` object does not have enough capacity, it is resized + /// first. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::with_capacity(0); + /// buf.extend_from_slice(b"aaabbb"); + /// buf.extend_from_slice(b"cccddd"); + /// + /// assert_eq!(b"aaabbbcccddd", &buf[..]); + /// ``` + #[inline] + pub fn extend_from_slice(&mut self, extend: &[u8]) { + let cnt = extend.len(); + self.reserve(cnt); + + unsafe { + let dst = self.spare_capacity_mut(); + // Reserved above + debug_assert!(dst.len() >= cnt); + + ptr::copy_nonoverlapping(extend.as_ptr(), dst.as_mut_ptr().cast(), cnt); + } + + unsafe { + self.advance_mut(cnt); + } + } + + /// Absorbs a `BytesMut` that was previously split off. + /// + /// If the two `BytesMut` objects were previously contiguous and not mutated + /// in a way that causes re-allocation i.e., if `other` was created by + /// calling `split_off` on this `BytesMut`, then this is an `O(1)` operation + /// that just decreases a reference count and sets a few indices. + /// Otherwise this method degenerates to + /// `self.extend_from_slice(other.as_ref())`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// let mut buf = BytesMut::with_capacity(64); + /// buf.extend_from_slice(b"aaabbbcccddd"); + /// + /// let split = buf.split_off(6); + /// assert_eq!(b"aaabbb", &buf[..]); + /// assert_eq!(b"cccddd", &split[..]); + /// + /// buf.unsplit(split); + /// assert_eq!(b"aaabbbcccddd", &buf[..]); + /// ``` + pub fn unsplit(&mut self, other: BytesMut) { + if self.is_empty() { + *self = other; + return; + } + + if let Err(other) = self.try_unsplit(other) { + self.extend_from_slice(other.as_ref()); + } + } + + // private + + // For now, use a `Vec` to manage the memory for us, but we may want to + // change that in the future to some alternate allocator strategy. + // + // Thus, we don't expose an easy way to construct from a `Vec` since an + // internal change could make a simple pattern (`BytesMut::from(vec)`) + // suddenly a lot more expensive. + #[inline] + pub(crate) fn from_vec(mut vec: Vec) -> BytesMut { + let ptr = vptr(vec.as_mut_ptr()); + let len = vec.len(); + let cap = vec.capacity(); + mem::forget(vec); + + let original_capacity_repr = original_capacity_to_repr(cap); + let data = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC; + + BytesMut { + ptr, + len, + cap, + data: invalid_ptr(data), + } + } + + #[inline] + fn as_slice(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) } + } + + #[inline] + fn as_slice_mut(&mut self) -> &mut [u8] { + unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) } + } + + unsafe fn set_start(&mut self, start: usize) { + // Setting the start to 0 is a no-op, so return early if this is the + // case. + if start == 0 { + return; + } + + debug_assert!(start <= self.cap, "internal: set_start out of bounds"); + + let kind = self.kind(); + + if kind == KIND_VEC { + // Setting the start when in vec representation is a little more + // complicated. First, we have to track how far ahead the + // "start" of the byte buffer from the beginning of the vec. We + // also have to ensure that we don't exceed the maximum shift. + let (mut pos, prev) = self.get_vec_pos(); + pos += start; + + if pos <= MAX_VEC_POS { + self.set_vec_pos(pos, prev); + } else { + // The repr must be upgraded to ARC. This will never happen + // on 64 bit systems and will only happen on 32 bit systems + // when shifting past 134,217,727 bytes. As such, we don't + // worry too much about performance here. + self.promote_to_shared(/*ref_count = */ 1); + } + } + + // Updating the start of the view is setting `ptr` to point to the + // new start and updating the `len` field to reflect the new length + // of the view. + self.ptr = vptr(self.ptr.as_ptr().add(start)); + + if self.len >= start { + self.len -= start; + } else { + self.len = 0; + } + + self.cap -= start; + } + + unsafe fn set_end(&mut self, end: usize) { + debug_assert_eq!(self.kind(), KIND_ARC); + assert!(end <= self.cap, "set_end out of bounds"); + + self.cap = end; + self.len = cmp::min(self.len, end); + } + + fn try_unsplit(&mut self, other: BytesMut) -> Result<(), BytesMut> { + if other.capacity() == 0 { + return Ok(()); + } + + let ptr = unsafe { self.ptr.as_ptr().add(self.len) }; + if ptr == other.ptr.as_ptr() + && self.kind() == KIND_ARC + && other.kind() == KIND_ARC + && self.data == other.data + { + // Contiguous blocks, just combine directly + self.len += other.len; + self.cap += other.cap; + Ok(()) + } else { + Err(other) + } + } + + #[inline] + fn kind(&self) -> usize { + self.data as usize & KIND_MASK + } + + unsafe fn promote_to_shared(&mut self, ref_cnt: usize) { + debug_assert_eq!(self.kind(), KIND_VEC); + debug_assert!(ref_cnt == 1 || ref_cnt == 2); + + let original_capacity_repr = + (self.data as usize & ORIGINAL_CAPACITY_MASK) >> ORIGINAL_CAPACITY_OFFSET; + + // The vec offset cannot be concurrently mutated, so there + // should be no danger reading it. + let off = (self.data as usize) >> VEC_POS_OFFSET; + + // First, allocate a new `Shared` instance containing the + // `Vec` fields. It's important to note that `ptr`, `len`, + // and `cap` cannot be mutated without having `&mut self`. + // This means that these fields will not be concurrently + // updated and since the buffer hasn't been promoted to an + // `Arc`, those three fields still are the components of the + // vector. + let shared = Box::new(Shared { + vec: rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off), + original_capacity_repr, + ref_count: AtomicUsize::new(ref_cnt), + }); + + let shared = Box::into_raw(shared); + + // The pointer should be aligned, so this assert should + // always succeed. + debug_assert_eq!(shared as usize & KIND_MASK, KIND_ARC); + + self.data = shared; + } + + /// Makes an exact shallow clone of `self`. + /// + /// The kind of `self` doesn't matter, but this is unsafe + /// because the clone will have the same offsets. You must + /// be sure the returned value to the user doesn't allow + /// two views into the same range. + #[inline] + unsafe fn shallow_clone(&mut self) -> BytesMut { + if self.kind() == KIND_ARC { + increment_shared(self.data); + ptr::read(self) + } else { + self.promote_to_shared(/*ref_count = */ 2); + ptr::read(self) + } + } + + #[inline] + unsafe fn get_vec_pos(&mut self) -> (usize, usize) { + debug_assert_eq!(self.kind(), KIND_VEC); + + let prev = self.data as usize; + (prev >> VEC_POS_OFFSET, prev) + } + + #[inline] + unsafe fn set_vec_pos(&mut self, pos: usize, prev: usize) { + debug_assert_eq!(self.kind(), KIND_VEC); + debug_assert!(pos <= MAX_VEC_POS); + + self.data = invalid_ptr((pos << VEC_POS_OFFSET) | (prev & NOT_VEC_POS_MASK)); + } + + /// Returns the remaining spare capacity of the buffer as a slice of `MaybeUninit`. + /// + /// The returned slice can be used to fill the buffer with data (e.g. by + /// reading from a file) before marking the data as initialized using the + /// [`set_len`] method. + /// + /// [`set_len`]: BytesMut::set_len + /// + /// # Examples + /// + /// ``` + /// use bytes::BytesMut; + /// + /// // Allocate buffer big enough for 10 bytes. + /// let mut buf = BytesMut::with_capacity(10); + /// + /// // Fill in the first 3 elements. + /// let uninit = buf.spare_capacity_mut(); + /// uninit[0].write(0); + /// uninit[1].write(1); + /// uninit[2].write(2); + /// + /// // Mark the first 3 bytes of the buffer as being initialized. + /// unsafe { + /// buf.set_len(3); + /// } + /// + /// assert_eq!(&buf[..], &[0, 1, 2]); + /// ``` + #[inline] + pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit] { + unsafe { + let ptr = self.ptr.as_ptr().add(self.len); + let len = self.cap - self.len; + + slice::from_raw_parts_mut(ptr.cast(), len) + } + } +} + +impl Drop for BytesMut { + fn drop(&mut self) { + let kind = self.kind(); + + if kind == KIND_VEC { + unsafe { + let (off, _) = self.get_vec_pos(); + + // Vector storage, free the vector + let _ = rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off); + } + } else if kind == KIND_ARC { + unsafe { release_shared(self.data) }; + } + } +} + +impl Buf for BytesMut { + #[inline] + fn remaining(&self) -> usize { + self.len() + } + + #[inline] + fn chunk(&self) -> &[u8] { + self.as_slice() + } + + #[inline] + fn advance(&mut self, cnt: usize) { + assert!( + cnt <= self.remaining(), + "cannot advance past `remaining`: {:?} <= {:?}", + cnt, + self.remaining(), + ); + unsafe { + self.set_start(cnt); + } + } + + fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { + self.split_to(len).freeze() + } +} + +unsafe impl BufMut for BytesMut { + #[inline] + fn remaining_mut(&self) -> usize { + usize::MAX - self.len() + } + + #[inline] + unsafe fn advance_mut(&mut self, cnt: usize) { + let new_len = self.len() + cnt; + assert!( + new_len <= self.cap, + "new_len = {}; capacity = {}", + new_len, + self.cap + ); + self.len = new_len; + } + + #[inline] + fn chunk_mut(&mut self) -> &mut UninitSlice { + if self.capacity() == self.len() { + self.reserve(64); + } + self.spare_capacity_mut().into() + } + + // Specialize these methods so they can skip checking `remaining_mut` + // and `advance_mut`. + + fn put(&mut self, mut src: T) + where + Self: Sized, + { + while src.has_remaining() { + let s = src.chunk(); + let l = s.len(); + self.extend_from_slice(s); + src.advance(l); + } + } + + fn put_slice(&mut self, src: &[u8]) { + self.extend_from_slice(src); + } + + fn put_bytes(&mut self, val: u8, cnt: usize) { + self.reserve(cnt); + unsafe { + let dst = self.spare_capacity_mut(); + // Reserved above + debug_assert!(dst.len() >= cnt); + + ptr::write_bytes(dst.as_mut_ptr(), val, cnt); + + self.advance_mut(cnt); + } + } +} + +impl AsRef<[u8]> for BytesMut { + #[inline] + fn as_ref(&self) -> &[u8] { + self.as_slice() + } +} + +impl Deref for BytesMut { + type Target = [u8]; + + #[inline] + fn deref(&self) -> &[u8] { + self.as_ref() + } +} + +impl AsMut<[u8]> for BytesMut { + #[inline] + fn as_mut(&mut self) -> &mut [u8] { + self.as_slice_mut() + } +} + +impl DerefMut for BytesMut { + #[inline] + fn deref_mut(&mut self) -> &mut [u8] { + self.as_mut() + } +} + +impl<'a> From<&'a [u8]> for BytesMut { + fn from(src: &'a [u8]) -> BytesMut { + BytesMut::from_vec(src.to_vec()) + } +} + +impl<'a> From<&'a str> for BytesMut { + fn from(src: &'a str) -> BytesMut { + BytesMut::from(src.as_bytes()) + } +} + +impl From for Bytes { + fn from(src: BytesMut) -> Bytes { + src.freeze() + } +} + +impl PartialEq for BytesMut { + fn eq(&self, other: &BytesMut) -> bool { + self.as_slice() == other.as_slice() + } +} + +impl PartialOrd for BytesMut { + fn partial_cmp(&self, other: &BytesMut) -> Option { + self.as_slice().partial_cmp(other.as_slice()) + } +} + +impl Ord for BytesMut { + fn cmp(&self, other: &BytesMut) -> cmp::Ordering { + self.as_slice().cmp(other.as_slice()) + } +} + +impl Eq for BytesMut {} + +impl Default for BytesMut { + #[inline] + fn default() -> BytesMut { + BytesMut::new() + } +} + +impl hash::Hash for BytesMut { + fn hash(&self, state: &mut H) + where + H: hash::Hasher, + { + let s: &[u8] = self.as_ref(); + s.hash(state); + } +} + +impl Borrow<[u8]> for BytesMut { + fn borrow(&self) -> &[u8] { + self.as_ref() + } +} + +impl BorrowMut<[u8]> for BytesMut { + fn borrow_mut(&mut self) -> &mut [u8] { + self.as_mut() + } +} + +impl fmt::Write for BytesMut { + #[inline] + fn write_str(&mut self, s: &str) -> fmt::Result { + if self.remaining_mut() >= s.len() { + self.put_slice(s.as_bytes()); + Ok(()) + } else { + Err(fmt::Error) + } + } + + #[inline] + fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { + fmt::write(self, args) + } +} + +impl Clone for BytesMut { + fn clone(&self) -> BytesMut { + BytesMut::from(&self[..]) + } +} + +impl IntoIterator for BytesMut { + type Item = u8; + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} + +impl<'a> IntoIterator for &'a BytesMut { + type Item = &'a u8; + type IntoIter = core::slice::Iter<'a, u8>; + + fn into_iter(self) -> Self::IntoIter { + self.as_ref().iter() + } +} + +impl Extend for BytesMut { + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + let iter = iter.into_iter(); + + let (lower, _) = iter.size_hint(); + self.reserve(lower); + + // TODO: optimize + // 1. If self.kind() == KIND_VEC, use Vec::extend + // 2. Make `reserve` inline-able + for b in iter { + self.reserve(1); + self.put_u8(b); + } + } +} + +impl<'a> Extend<&'a u8> for BytesMut { + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + self.extend(iter.into_iter().copied()) + } +} + +impl Extend for BytesMut { + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + for bytes in iter { + self.extend_from_slice(&bytes) + } + } +} + +impl FromIterator for BytesMut { + fn from_iter>(into_iter: T) -> Self { + BytesMut::from_vec(Vec::from_iter(into_iter)) + } +} + +impl<'a> FromIterator<&'a u8> for BytesMut { + fn from_iter>(into_iter: T) -> Self { + BytesMut::from_iter(into_iter.into_iter().copied()) + } +} + +/* + * + * ===== Inner ===== + * + */ + +unsafe fn increment_shared(ptr: *mut Shared) { + let old_size = (*ptr).ref_count.fetch_add(1, Ordering::Relaxed); + + if old_size > isize::MAX as usize { + crate::abort(); + } +} + +unsafe fn release_shared(ptr: *mut Shared) { + // `Shared` storage... follow the drop steps from Arc. + if (*ptr).ref_count.fetch_sub(1, Ordering::Release) != 1 { + return; + } + + // This fence is needed to prevent reordering of use of the data and + // deletion of the data. Because it is marked `Release`, the decreasing + // of the reference count synchronizes with this `Acquire` fence. This + // means that use of the data happens before decreasing the reference + // count, which happens before this fence, which happens before the + // deletion of the data. + // + // As explained in the [Boost documentation][1], + // + // > It is important to enforce any possible access to the object in one + // > thread (through an existing reference) to *happen before* deleting + // > the object in a different thread. This is achieved by a "release" + // > operation after dropping a reference (any access to the object + // > through this reference must obviously happened before), and an + // > "acquire" operation before deleting the object. + // + // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) + // + // Thread sanitizer does not support atomic fences. Use an atomic load + // instead. + (*ptr).ref_count.load(Ordering::Acquire); + + // Drop the data + drop(Box::from_raw(ptr)); +} + +impl Shared { + fn is_unique(&self) -> bool { + // The goal is to check if the current handle is the only handle + // that currently has access to the buffer. This is done by + // checking if the `ref_count` is currently 1. + // + // The `Acquire` ordering synchronizes with the `Release` as + // part of the `fetch_sub` in `release_shared`. The `fetch_sub` + // operation guarantees that any mutations done in other threads + // are ordered before the `ref_count` is decremented. As such, + // this `Acquire` will guarantee that those mutations are + // visible to the current thread. + self.ref_count.load(Ordering::Acquire) == 1 + } +} + +#[inline] +fn original_capacity_to_repr(cap: usize) -> usize { + let width = PTR_WIDTH - ((cap >> MIN_ORIGINAL_CAPACITY_WIDTH).leading_zeros() as usize); + cmp::min( + width, + MAX_ORIGINAL_CAPACITY_WIDTH - MIN_ORIGINAL_CAPACITY_WIDTH, + ) +} + +fn original_capacity_from_repr(repr: usize) -> usize { + if repr == 0 { + return 0; + } + + 1 << (repr + (MIN_ORIGINAL_CAPACITY_WIDTH - 1)) +} + +/* +#[test] +fn test_original_capacity_to_repr() { + assert_eq!(original_capacity_to_repr(0), 0); + + let max_width = 32; + + for width in 1..(max_width + 1) { + let cap = 1 << width - 1; + + let expected = if width < MIN_ORIGINAL_CAPACITY_WIDTH { + 0 + } else if width < MAX_ORIGINAL_CAPACITY_WIDTH { + width - MIN_ORIGINAL_CAPACITY_WIDTH + } else { + MAX_ORIGINAL_CAPACITY_WIDTH - MIN_ORIGINAL_CAPACITY_WIDTH + }; + + assert_eq!(original_capacity_to_repr(cap), expected); + + if width > 1 { + assert_eq!(original_capacity_to_repr(cap + 1), expected); + } + + // MIN_ORIGINAL_CAPACITY_WIDTH must be bigger than 7 to pass tests below + if width == MIN_ORIGINAL_CAPACITY_WIDTH + 1 { + assert_eq!(original_capacity_to_repr(cap - 24), expected - 1); + assert_eq!(original_capacity_to_repr(cap + 76), expected); + } else if width == MIN_ORIGINAL_CAPACITY_WIDTH + 2 { + assert_eq!(original_capacity_to_repr(cap - 1), expected - 1); + assert_eq!(original_capacity_to_repr(cap - 48), expected - 1); + } + } +} + +#[test] +fn test_original_capacity_from_repr() { + assert_eq!(0, original_capacity_from_repr(0)); + + let min_cap = 1 << MIN_ORIGINAL_CAPACITY_WIDTH; + + assert_eq!(min_cap, original_capacity_from_repr(1)); + assert_eq!(min_cap * 2, original_capacity_from_repr(2)); + assert_eq!(min_cap * 4, original_capacity_from_repr(3)); + assert_eq!(min_cap * 8, original_capacity_from_repr(4)); + assert_eq!(min_cap * 16, original_capacity_from_repr(5)); + assert_eq!(min_cap * 32, original_capacity_from_repr(6)); + assert_eq!(min_cap * 64, original_capacity_from_repr(7)); +} +*/ + +unsafe impl Send for BytesMut {} +unsafe impl Sync for BytesMut {} + +/* + * + * ===== PartialEq / PartialOrd ===== + * + */ + +impl PartialEq<[u8]> for BytesMut { + fn eq(&self, other: &[u8]) -> bool { + &**self == other + } +} + +impl PartialOrd<[u8]> for BytesMut { + fn partial_cmp(&self, other: &[u8]) -> Option { + (**self).partial_cmp(other) + } +} + +impl PartialEq for [u8] { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd for [u8] { + fn partial_cmp(&self, other: &BytesMut) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) + } +} + +impl PartialEq for BytesMut { + fn eq(&self, other: &str) -> bool { + &**self == other.as_bytes() + } +} + +impl PartialOrd for BytesMut { + fn partial_cmp(&self, other: &str) -> Option { + (**self).partial_cmp(other.as_bytes()) + } +} + +impl PartialEq for str { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd for str { + fn partial_cmp(&self, other: &BytesMut) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) + } +} + +impl PartialEq> for BytesMut { + fn eq(&self, other: &Vec) -> bool { + *self == other[..] + } +} + +impl PartialOrd> for BytesMut { + fn partial_cmp(&self, other: &Vec) -> Option { + (**self).partial_cmp(&other[..]) + } +} + +impl PartialEq for Vec { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd for Vec { + fn partial_cmp(&self, other: &BytesMut) -> Option { + other.partial_cmp(self) + } +} + +impl PartialEq for BytesMut { + fn eq(&self, other: &String) -> bool { + *self == other[..] + } +} + +impl PartialOrd for BytesMut { + fn partial_cmp(&self, other: &String) -> Option { + (**self).partial_cmp(other.as_bytes()) + } +} + +impl PartialEq for String { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd for String { + fn partial_cmp(&self, other: &BytesMut) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) + } +} + +impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut +where + BytesMut: PartialEq, +{ + fn eq(&self, other: &&'a T) -> bool { + *self == **other + } +} + +impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut +where + BytesMut: PartialOrd, +{ + fn partial_cmp(&self, other: &&'a T) -> Option { + self.partial_cmp(*other) + } +} + +impl PartialEq for &[u8] { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd for &[u8] { + fn partial_cmp(&self, other: &BytesMut) -> Option { + <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) + } +} + +impl PartialEq for &str { + fn eq(&self, other: &BytesMut) -> bool { + *other == *self + } +} + +impl PartialOrd for &str { + fn partial_cmp(&self, other: &BytesMut) -> Option { + other.partial_cmp(self) + } +} + +impl PartialEq for Bytes { + fn eq(&self, other: &BytesMut) -> bool { + other[..] == self[..] + } +} + +impl PartialEq for BytesMut { + fn eq(&self, other: &Bytes) -> bool { + other[..] == self[..] + } +} + +impl From for Vec { + fn from(mut bytes: BytesMut) -> Self { + let kind = bytes.kind(); + + let mut vec = if kind == KIND_VEC { + unsafe { + let (off, _) = bytes.get_vec_pos(); + rebuild_vec(bytes.ptr.as_ptr(), bytes.len, bytes.cap, off) + } + } else if kind == KIND_ARC { + let shared = bytes.data as *mut Shared; + + if unsafe { (*shared).is_unique() } { + let vec = mem::replace(unsafe { &mut (*shared).vec }, Vec::new()); + + unsafe { release_shared(shared) }; + + vec + } else { + return bytes.deref().to_vec(); + } + } else { + return bytes.deref().to_vec(); + }; + + let len = bytes.len; + + unsafe { + ptr::copy(bytes.ptr.as_ptr(), vec.as_mut_ptr(), len); + vec.set_len(len); + } + + mem::forget(bytes); + + vec + } +} + +#[inline] +fn vptr(ptr: *mut u8) -> NonNull { + if cfg!(debug_assertions) { + NonNull::new(ptr).expect("Vec pointer should be non-null") + } else { + unsafe { NonNull::new_unchecked(ptr) } + } +} + +/// Returns a dangling pointer with the given address. This is used to store +/// integer data in pointer fields. +/// +/// It is equivalent to `addr as *mut T`, but this fails on miri when strict +/// provenance checking is enabled. +#[inline] +fn invalid_ptr(addr: usize) -> *mut T { + let ptr = core::ptr::null_mut::().wrapping_add(addr); + debug_assert_eq!(ptr as usize, addr); + ptr.cast::() +} + +/// Precondition: dst >= original +/// +/// The following line is equivalent to: +/// +/// ```rust,ignore +/// self.ptr.as_ptr().offset_from(ptr) as usize; +/// ``` +/// +/// But due to min rust is 1.39 and it is only stablised +/// in 1.47, we cannot use it. +#[inline] +fn offset_from(dst: *mut u8, original: *mut u8) -> usize { + debug_assert!(dst >= original); + + dst as usize - original as usize +} + +unsafe fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) -> Vec { + let ptr = ptr.offset(-(off as isize)); + len += off; + cap += off; + + Vec::from_raw_parts(ptr, len, cap) +} + +// ===== impl SharedVtable ===== + +static SHARED_VTABLE: Vtable = Vtable { + clone: shared_v_clone, + to_vec: shared_v_to_vec, + drop: shared_v_drop, +}; + +unsafe fn shared_v_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { + let shared = data.load(Ordering::Relaxed) as *mut Shared; + increment_shared(shared); + + let data = AtomicPtr::new(shared as *mut ()); + Bytes::with_vtable(ptr, len, data, &SHARED_VTABLE) +} + +unsafe fn shared_v_to_vec(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Vec { + let shared: *mut Shared = data.load(Ordering::Relaxed).cast(); + + if (*shared).is_unique() { + let shared = &mut *shared; + + // Drop shared + let mut vec = mem::replace(&mut shared.vec, Vec::new()); + release_shared(shared); + + // Copy back buffer + ptr::copy(ptr, vec.as_mut_ptr(), len); + vec.set_len(len); + + vec + } else { + let v = slice::from_raw_parts(ptr, len).to_vec(); + release_shared(shared); + v + } +} + +unsafe fn shared_v_drop(data: &mut AtomicPtr<()>, _ptr: *const u8, _len: usize) { + data.with_mut(|shared| { + release_shared(*shared as *mut Shared); + }); +} + +// compile-fails + +/// ```compile_fail +/// use bytes::BytesMut; +/// #[deny(unused_must_use)] +/// { +/// let mut b1 = BytesMut::from("hello world"); +/// b1.split_to(6); +/// } +/// ``` +fn _split_to_must_use() {} + +/// ```compile_fail +/// use bytes::BytesMut; +/// #[deny(unused_must_use)] +/// { +/// let mut b1 = BytesMut::from("hello world"); +/// b1.split_off(6); +/// } +/// ``` +fn _split_off_must_use() {} + +/// ```compile_fail +/// use bytes::BytesMut; +/// #[deny(unused_must_use)] +/// { +/// let mut b1 = BytesMut::from("hello world"); +/// b1.split(); +/// } +/// ``` +fn _split_must_use() {} + +// fuzz tests +#[cfg(all(test, loom))] +mod fuzz { + use loom::sync::Arc; + use loom::thread; + + use super::BytesMut; + use crate::Bytes; + + #[test] + fn bytes_mut_cloning_frozen() { + loom::model(|| { + let a = BytesMut::from(&b"abcdefgh"[..]).split().freeze(); + let addr = a.as_ptr() as usize; + + // test the Bytes::clone is Sync by putting it in an Arc + let a1 = Arc::new(a); + let a2 = a1.clone(); + + let t1 = thread::spawn(move || { + let b: Bytes = (*a1).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + let t2 = thread::spawn(move || { + let b: Bytes = (*a2).clone(); + assert_eq!(b.as_ptr() as usize, addr); + }); + + t1.join().unwrap(); + t2.join().unwrap(); + }); + } +} diff --git a/third_party/rust/bytes/v1/crate/src/fmt/debug.rs b/third_party/rust/bytes/v1/crate/src/fmt/debug.rs new file mode 100644 index 000000000000..83de695dd7a5 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/fmt/debug.rs @@ -0,0 +1,49 @@ +use core::fmt::{Debug, Formatter, Result}; + +use super::BytesRef; +use crate::{Bytes, BytesMut}; + +/// Alternative implementation of `std::fmt::Debug` for byte slice. +/// +/// Standard `Debug` implementation for `[u8]` is comma separated +/// list of numbers. Since large amount of byte strings are in fact +/// ASCII strings or contain a lot of ASCII strings (e. g. HTTP), +/// it is convenient to print strings as ASCII when possible. +impl Debug for BytesRef<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + write!(f, "b\"")?; + for &b in self.0 { + // https://doc.rust-lang.org/reference/tokens.html#byte-escapes + if b == b'\n' { + write!(f, "\\n")?; + } else if b == b'\r' { + write!(f, "\\r")?; + } else if b == b'\t' { + write!(f, "\\t")?; + } else if b == b'\\' || b == b'"' { + write!(f, "\\{}", b as char)?; + } else if b == b'\0' { + write!(f, "\\0")?; + // ASCII printable + } else if (0x20..0x7f).contains(&b) { + write!(f, "{}", b as char)?; + } else { + write!(f, "\\x{:02x}", b)?; + } + } + write!(f, "\"")?; + Ok(()) + } +} + +impl Debug for Bytes { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Debug::fmt(&BytesRef(self.as_ref()), f) + } +} + +impl Debug for BytesMut { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Debug::fmt(&BytesRef(self.as_ref()), f) + } +} diff --git a/third_party/rust/bytes/v1/crate/src/fmt/hex.rs b/third_party/rust/bytes/v1/crate/src/fmt/hex.rs new file mode 100644 index 000000000000..97a749a33682 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/fmt/hex.rs @@ -0,0 +1,37 @@ +use core::fmt::{Formatter, LowerHex, Result, UpperHex}; + +use super::BytesRef; +use crate::{Bytes, BytesMut}; + +impl LowerHex for BytesRef<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + for &b in self.0 { + write!(f, "{:02x}", b)?; + } + Ok(()) + } +} + +impl UpperHex for BytesRef<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + for &b in self.0 { + write!(f, "{:02X}", b)?; + } + Ok(()) + } +} + +macro_rules! hex_impl { + ($tr:ident, $ty:ty) => { + impl $tr for $ty { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + $tr::fmt(&BytesRef(self.as_ref()), f) + } + } + }; +} + +hex_impl!(LowerHex, Bytes); +hex_impl!(LowerHex, BytesMut); +hex_impl!(UpperHex, Bytes); +hex_impl!(UpperHex, BytesMut); diff --git a/third_party/rust/bytes/v1/crate/src/fmt/mod.rs b/third_party/rust/bytes/v1/crate/src/fmt/mod.rs new file mode 100644 index 000000000000..676d15fc21f1 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/fmt/mod.rs @@ -0,0 +1,5 @@ +mod debug; +mod hex; + +/// `BytesRef` is not a part of public API of bytes crate. +struct BytesRef<'a>(&'a [u8]); diff --git a/third_party/rust/bytes/v1/crate/src/lib.rs b/third_party/rust/bytes/v1/crate/src/lib.rs new file mode 100644 index 000000000000..af436b3162c2 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/lib.rs @@ -0,0 +1,117 @@ +#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)] +#![doc(test( + no_crate_inject, + attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) +))] +#![no_std] +#![cfg_attr(docsrs, feature(doc_cfg))] + +//! Provides abstractions for working with bytes. +//! +//! The `bytes` crate provides an efficient byte buffer structure +//! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer +//! implementations ([`Buf`], [`BufMut`]). +//! +//! [`Buf`]: trait.Buf.html +//! [`BufMut`]: trait.BufMut.html +//! +//! # `Bytes` +//! +//! `Bytes` is an efficient container for storing and operating on contiguous +//! slices of memory. It is intended for use primarily in networking code, but +//! could have applications elsewhere as well. +//! +//! `Bytes` values facilitate zero-copy network programming by allowing multiple +//! `Bytes` objects to point to the same underlying memory. This is managed by +//! using a reference count to track when the memory is no longer needed and can +//! be freed. +//! +//! A `Bytes` handle can be created directly from an existing byte store (such as `&[u8]` +//! or `Vec`), but usually a `BytesMut` is used first and written to. For +//! example: +//! +//! ```rust +//! use bytes::{BytesMut, BufMut}; +//! +//! let mut buf = BytesMut::with_capacity(1024); +//! buf.put(&b"hello world"[..]); +//! buf.put_u16(1234); +//! +//! let a = buf.split(); +//! assert_eq!(a, b"hello world\x04\xD2"[..]); +//! +//! buf.put(&b"goodbye world"[..]); +//! +//! let b = buf.split(); +//! assert_eq!(b, b"goodbye world"[..]); +//! +//! assert_eq!(buf.capacity(), 998); +//! ``` +//! +//! In the above example, only a single buffer of 1024 is allocated. The handles +//! `a` and `b` will share the underlying buffer and maintain indices tracking +//! the view into the buffer represented by the handle. +//! +//! See the [struct docs] for more details. +//! +//! [struct docs]: struct.Bytes.html +//! +//! # `Buf`, `BufMut` +//! +//! These two traits provide read and write access to buffers. The underlying +//! storage may or may not be in contiguous memory. For example, `Bytes` is a +//! buffer that guarantees contiguous memory, but a [rope] stores the bytes in +//! disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current +//! position in the underlying byte storage. When bytes are read or written, the +//! cursor is advanced. +//! +//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure) +//! +//! ## Relation with `Read` and `Write` +//! +//! At first glance, it may seem that `Buf` and `BufMut` overlap in +//! functionality with `std::io::Read` and `std::io::Write`. However, they +//! serve different purposes. A buffer is the value that is provided as an +//! argument to `Read::read` and `Write::write`. `Read` and `Write` may then +//! perform a syscall, which has the potential of failing. Operations on `Buf` +//! and `BufMut` are infallible. + +extern crate alloc; + +#[cfg(feature = "std")] +extern crate std; + +pub mod buf; +pub use crate::buf::{Buf, BufMut}; + +mod bytes; +mod bytes_mut; +mod fmt; +mod loom; +pub use crate::bytes::Bytes; +pub use crate::bytes_mut::BytesMut; + +// Optional Serde support +#[cfg(feature = "serde")] +mod serde; + +#[inline(never)] +#[cold] +fn abort() -> ! { + #[cfg(feature = "std")] + { + std::process::abort(); + } + + #[cfg(not(feature = "std"))] + { + struct Abort; + impl Drop for Abort { + fn drop(&mut self) { + panic!(); + } + } + let _a = Abort; + panic!("abort"); + } +} diff --git a/third_party/rust/bytes/v1/crate/src/loom.rs b/third_party/rust/bytes/v1/crate/src/loom.rs new file mode 100644 index 000000000000..9e6b2d5e25a9 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/loom.rs @@ -0,0 +1,30 @@ +#[cfg(not(all(test, loom)))] +pub(crate) mod sync { + pub(crate) mod atomic { + pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; + + pub(crate) trait AtomicMut { + fn with_mut(&mut self, f: F) -> R + where + F: FnOnce(&mut *mut T) -> R; + } + + impl AtomicMut for AtomicPtr { + fn with_mut(&mut self, f: F) -> R + where + F: FnOnce(&mut *mut T) -> R, + { + f(self.get_mut()) + } + } + } +} + +#[cfg(all(test, loom))] +pub(crate) mod sync { + pub(crate) mod atomic { + pub(crate) use loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; + + pub(crate) trait AtomicMut {} + } +} diff --git a/third_party/rust/bytes/v1/crate/src/serde.rs b/third_party/rust/bytes/v1/crate/src/serde.rs new file mode 100644 index 000000000000..0a5bd144a960 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/src/serde.rs @@ -0,0 +1,89 @@ +use super::{Bytes, BytesMut}; +use alloc::string::String; +use alloc::vec::Vec; +use core::{cmp, fmt}; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +macro_rules! serde_impl { + ($ty:ident, $visitor_ty:ident, $from_slice:ident, $from_vec:ident) => { + impl Serialize for $ty { + #[inline] + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(&self) + } + } + + struct $visitor_ty; + + impl<'de> de::Visitor<'de> for $visitor_ty { + type Value = $ty; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("byte array") + } + + #[inline] + fn visit_seq(self, mut seq: V) -> Result + where + V: de::SeqAccess<'de>, + { + let len = cmp::min(seq.size_hint().unwrap_or(0), 4096); + let mut values: Vec = Vec::with_capacity(len); + + while let Some(value) = seq.next_element()? { + values.push(value); + } + + Ok($ty::$from_vec(values)) + } + + #[inline] + fn visit_bytes(self, v: &[u8]) -> Result + where + E: de::Error, + { + Ok($ty::$from_slice(v)) + } + + #[inline] + fn visit_byte_buf(self, v: Vec) -> Result + where + E: de::Error, + { + Ok($ty::$from_vec(v)) + } + + #[inline] + fn visit_str(self, v: &str) -> Result + where + E: de::Error, + { + Ok($ty::$from_slice(v.as_bytes())) + } + + #[inline] + fn visit_string(self, v: String) -> Result + where + E: de::Error, + { + Ok($ty::$from_vec(v.into_bytes())) + } + } + + impl<'de> Deserialize<'de> for $ty { + #[inline] + fn deserialize(deserializer: D) -> Result<$ty, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_byte_buf($visitor_ty) + } + } + }; +} + +serde_impl!(Bytes, BytesVisitor, copy_from_slice, from); +serde_impl!(BytesMut, BytesMutVisitor, from, from_vec); diff --git a/third_party/rust/bytes/v1/crate/tests/test_buf.rs b/third_party/rust/bytes/v1/crate/tests/test_buf.rs new file mode 100644 index 000000000000..3940f9247d36 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_buf.rs @@ -0,0 +1,121 @@ +#![warn(rust_2018_idioms)] + +use bytes::Buf; +#[cfg(feature = "std")] +use std::io::IoSlice; + +#[test] +fn test_fresh_cursor_vec() { + let mut buf = &b"hello"[..]; + + assert_eq!(buf.remaining(), 5); + assert_eq!(buf.chunk(), b"hello"); + + buf.advance(2); + + assert_eq!(buf.remaining(), 3); + assert_eq!(buf.chunk(), b"llo"); + + buf.advance(3); + + assert_eq!(buf.remaining(), 0); + assert_eq!(buf.chunk(), b""); +} + +#[test] +fn test_get_u8() { + let mut buf = &b"\x21zomg"[..]; + assert_eq!(0x21, buf.get_u8()); +} + +#[test] +fn test_get_u16() { + let mut buf = &b"\x21\x54zomg"[..]; + assert_eq!(0x2154, buf.get_u16()); + let mut buf = &b"\x21\x54zomg"[..]; + assert_eq!(0x5421, buf.get_u16_le()); +} + +#[test] +#[should_panic] +fn test_get_u16_buffer_underflow() { + let mut buf = &b"\x21"[..]; + buf.get_u16(); +} + +#[cfg(feature = "std")] +#[test] +fn test_bufs_vec() { + let buf = &b"hello world"[..]; + + let b1: &[u8] = &mut []; + let b2: &[u8] = &mut []; + + let mut dst = [IoSlice::new(b1), IoSlice::new(b2)]; + + assert_eq!(1, buf.chunks_vectored(&mut dst[..])); +} + +#[test] +fn test_vec_deque() { + use std::collections::VecDeque; + + let mut buffer: VecDeque = VecDeque::new(); + buffer.extend(b"hello world"); + assert_eq!(11, buffer.remaining()); + assert_eq!(b"hello world", buffer.chunk()); + buffer.advance(6); + assert_eq!(b"world", buffer.chunk()); + buffer.extend(b" piece"); + let mut out = [0; 11]; + buffer.copy_to_slice(&mut out); + assert_eq!(b"world piece", &out[..]); +} + +#[allow(unused_allocation)] // This is intentional. +#[test] +fn test_deref_buf_forwards() { + struct Special; + + impl Buf for Special { + fn remaining(&self) -> usize { + unreachable!("remaining"); + } + + fn chunk(&self) -> &[u8] { + unreachable!("chunk"); + } + + fn advance(&mut self, _: usize) { + unreachable!("advance"); + } + + fn get_u8(&mut self) -> u8 { + // specialized! + b'x' + } + } + + // these should all use the specialized method + assert_eq!(Special.get_u8(), b'x'); + assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x'); + assert_eq!((Box::new(Special) as Box).get_u8(), b'x'); + assert_eq!(Box::new(Special).get_u8(), b'x'); +} + +#[test] +fn copy_to_bytes_less() { + let mut buf = &b"hello world"[..]; + + let bytes = buf.copy_to_bytes(5); + assert_eq!(bytes, &b"hello"[..]); + assert_eq!(buf, &b" world"[..]) +} + +#[test] +#[should_panic] +fn copy_to_bytes_overflow() { + let mut buf = &b"hello world"[..]; + + let _bytes = buf.copy_to_bytes(12); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_buf_mut.rs b/third_party/rust/bytes/v1/crate/tests/test_buf_mut.rs new file mode 100644 index 000000000000..33aa6803862d --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_buf_mut.rs @@ -0,0 +1,276 @@ +#![warn(rust_2018_idioms)] + +use bytes::buf::UninitSlice; +use bytes::{BufMut, BytesMut}; +use core::fmt::Write; +use core::mem::MaybeUninit; +use core::usize; + +#[test] +fn test_vec_as_mut_buf() { + let mut buf = Vec::with_capacity(64); + + assert_eq!(buf.remaining_mut(), isize::MAX as usize); + + assert!(buf.chunk_mut().len() >= 64); + + buf.put(&b"zomg"[..]); + + assert_eq!(&buf, b"zomg"); + + assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4); + assert_eq!(buf.capacity(), 64); + + for _ in 0..16 { + buf.put(&b"zomg"[..]); + } + + assert_eq!(buf.len(), 68); +} + +#[test] +fn test_vec_put_bytes() { + let mut buf = Vec::new(); + buf.push(17); + buf.put_bytes(19, 2); + assert_eq!([17, 19, 19], &buf[..]); +} + +#[test] +fn test_put_u8() { + let mut buf = Vec::with_capacity(8); + buf.put_u8(33); + assert_eq!(b"\x21", &buf[..]); +} + +#[test] +fn test_put_u16() { + let mut buf = Vec::with_capacity(8); + buf.put_u16(8532); + assert_eq!(b"\x21\x54", &buf[..]); + + buf.clear(); + buf.put_u16_le(8532); + assert_eq!(b"\x54\x21", &buf[..]); +} + +#[test] +fn test_put_int() { + let mut buf = Vec::with_capacity(8); + buf.put_int(0x1020304050607080, 3); + assert_eq!(b"\x60\x70\x80", &buf[..]); +} + +#[test] +#[should_panic] +fn test_put_int_nbytes_overflow() { + let mut buf = Vec::with_capacity(8); + buf.put_int(0x1020304050607080, 9); +} + +#[test] +fn test_put_int_le() { + let mut buf = Vec::with_capacity(8); + buf.put_int_le(0x1020304050607080, 3); + assert_eq!(b"\x80\x70\x60", &buf[..]); +} + +#[test] +#[should_panic] +fn test_put_int_le_nbytes_overflow() { + let mut buf = Vec::with_capacity(8); + buf.put_int_le(0x1020304050607080, 9); +} + +#[test] +#[should_panic(expected = "cannot advance")] +fn test_vec_advance_mut() { + // Verify fix for #354 + let mut buf = Vec::with_capacity(8); + unsafe { + buf.advance_mut(12); + } +} + +#[test] +fn test_clone() { + let mut buf = BytesMut::with_capacity(100); + buf.write_str("this is a test").unwrap(); + let buf2 = buf.clone(); + + buf.write_str(" of our emergency broadcast system").unwrap(); + assert!(buf != buf2); +} + +fn do_test_slice_small(make: impl Fn(&mut [u8]) -> &mut T) +where + for<'r> &'r mut T: BufMut, +{ + let mut buf = [b'X'; 8]; + + let mut slice = make(&mut buf[..]); + slice.put_bytes(b'A', 2); + slice.put_u8(b'B'); + slice.put_slice(b"BCC"); + assert_eq!(2, slice.remaining_mut()); + assert_eq!(b"AABBCCXX", &buf[..]); + + let mut slice = make(&mut buf[..]); + slice.put_u32(0x61626364); + assert_eq!(4, slice.remaining_mut()); + assert_eq!(b"abcdCCXX", &buf[..]); + + let mut slice = make(&mut buf[..]); + slice.put_u32_le(0x30313233); + assert_eq!(4, slice.remaining_mut()); + assert_eq!(b"3210CCXX", &buf[..]); +} + +fn do_test_slice_large(make: impl Fn(&mut [u8]) -> &mut T) +where + for<'r> &'r mut T: BufMut, +{ + const LEN: usize = 100; + const FILL: [u8; LEN] = [b'Y'; LEN]; + + let test = |fill: &dyn Fn(&mut &mut T, usize)| { + for buf_len in 0..LEN { + let mut buf = [b'X'; LEN]; + for fill_len in 0..=buf_len { + let mut slice = make(&mut buf[..buf_len]); + fill(&mut slice, fill_len); + assert_eq!(buf_len - fill_len, slice.remaining_mut()); + let (head, tail) = buf.split_at(fill_len); + assert_eq!(&FILL[..fill_len], head); + assert!(tail.iter().all(|b| *b == b'X')); + } + } + }; + + test(&|slice, fill_len| slice.put_slice(&FILL[..fill_len])); + test(&|slice, fill_len| slice.put_bytes(FILL[0], fill_len)); +} + +fn do_test_slice_put_slice_panics(make: impl Fn(&mut [u8]) -> &mut T) +where + for<'r> &'r mut T: BufMut, +{ + let mut buf = [b'X'; 4]; + let mut slice = make(&mut buf[..]); + slice.put_slice(b"12345"); +} + +fn do_test_slice_put_bytes_panics(make: impl Fn(&mut [u8]) -> &mut T) +where + for<'r> &'r mut T: BufMut, +{ + let mut buf = [b'X'; 4]; + let mut slice = make(&mut buf[..]); + slice.put_bytes(b'1', 5); +} + +#[test] +fn test_slice_buf_mut_small() { + do_test_slice_small(|x| x); +} + +#[test] +fn test_slice_buf_mut_large() { + do_test_slice_large(|x| x); +} + +#[test] +#[should_panic] +fn test_slice_buf_mut_put_slice_overflow() { + do_test_slice_put_slice_panics(|x| x); +} + +#[test] +#[should_panic] +fn test_slice_buf_mut_put_bytes_overflow() { + do_test_slice_put_bytes_panics(|x| x); +} + +fn make_maybe_uninit_slice(slice: &mut [u8]) -> &mut [MaybeUninit] { + // SAFETY: [u8] has the same layout as [MaybeUninit]. + unsafe { core::mem::transmute(slice) } +} + +#[test] +fn test_maybe_uninit_buf_mut_small() { + do_test_slice_small(make_maybe_uninit_slice); +} + +#[test] +fn test_maybe_uninit_buf_mut_large() { + do_test_slice_large(make_maybe_uninit_slice); +} + +#[test] +#[should_panic] +fn test_maybe_uninit_buf_mut_put_slice_overflow() { + do_test_slice_put_slice_panics(make_maybe_uninit_slice); +} + +#[test] +#[should_panic] +fn test_maybe_uninit_buf_mut_put_bytes_overflow() { + do_test_slice_put_bytes_panics(make_maybe_uninit_slice); +} + +#[allow(unused_allocation)] // This is intentional. +#[test] +fn test_deref_bufmut_forwards() { + struct Special; + + unsafe impl BufMut for Special { + fn remaining_mut(&self) -> usize { + unreachable!("remaining_mut"); + } + + fn chunk_mut(&mut self) -> &mut UninitSlice { + unreachable!("chunk_mut"); + } + + unsafe fn advance_mut(&mut self, _: usize) { + unreachable!("advance"); + } + + fn put_u8(&mut self, _: u8) { + // specialized! + } + } + + // these should all use the specialized method + Special.put_u8(b'x'); + (&mut Special as &mut dyn BufMut).put_u8(b'x'); + (Box::new(Special) as Box).put_u8(b'x'); + Box::new(Special).put_u8(b'x'); +} + +#[test] +#[should_panic] +fn write_byte_panics_if_out_of_bounds() { + let mut data = [b'b', b'a', b'r']; + + let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; + slice.write_byte(4, b'f'); +} + +#[test] +#[should_panic] +fn copy_from_slice_panics_if_different_length_1() { + let mut data = [b'b', b'a', b'r']; + + let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; + slice.copy_from_slice(b"a"); +} + +#[test] +#[should_panic] +fn copy_from_slice_panics_if_different_length_2() { + let mut data = [b'b', b'a', b'r']; + + let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) }; + slice.copy_from_slice(b"abcd"); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_bytes.rs b/third_party/rust/bytes/v1/crate/tests/test_bytes.rs new file mode 100644 index 000000000000..5ec60a5b0e0a --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_bytes.rs @@ -0,0 +1,1210 @@ +#![warn(rust_2018_idioms)] + +use bytes::{Buf, BufMut, Bytes, BytesMut}; + +use std::usize; + +const LONG: &[u8] = b"mary had a little lamb, little lamb, little lamb"; +const SHORT: &[u8] = b"hello world"; + +fn is_sync() {} +fn is_send() {} + +#[test] +fn test_bounds() { + is_sync::(); + is_sync::(); + is_send::(); + is_send::(); +} + +#[test] +fn test_layout() { + use std::mem; + + assert_eq!( + mem::size_of::(), + mem::size_of::() * 4, + "Bytes size should be 4 words", + ); + assert_eq!( + mem::size_of::(), + mem::size_of::() * 4, + "BytesMut should be 4 words", + ); + + assert_eq!( + mem::size_of::(), + mem::size_of::>(), + "Bytes should be same size as Option", + ); + + assert_eq!( + mem::size_of::(), + mem::size_of::>(), + "BytesMut should be same size as Option", + ); +} + +#[test] +fn from_slice() { + let a = Bytes::from(&b"abcdefgh"[..]); + assert_eq!(a, b"abcdefgh"[..]); + assert_eq!(a, &b"abcdefgh"[..]); + assert_eq!(a, Vec::from(&b"abcdefgh"[..])); + assert_eq!(b"abcdefgh"[..], a); + assert_eq!(&b"abcdefgh"[..], a); + assert_eq!(Vec::from(&b"abcdefgh"[..]), a); + + let a = BytesMut::from(&b"abcdefgh"[..]); + assert_eq!(a, b"abcdefgh"[..]); + assert_eq!(a, &b"abcdefgh"[..]); + assert_eq!(a, Vec::from(&b"abcdefgh"[..])); + assert_eq!(b"abcdefgh"[..], a); + assert_eq!(&b"abcdefgh"[..], a); + assert_eq!(Vec::from(&b"abcdefgh"[..]), a); +} + +#[test] +fn fmt() { + let a = format!("{:?}", Bytes::from(&b"abcdefg"[..])); + let b = "b\"abcdefg\""; + + assert_eq!(a, b); + + let a = format!("{:?}", BytesMut::from(&b"abcdefg"[..])); + assert_eq!(a, b); +} + +#[test] +fn fmt_write() { + use std::fmt::Write; + use std::iter::FromIterator; + let s = String::from_iter((0..10).map(|_| "abcdefg")); + + let mut a = BytesMut::with_capacity(64); + write!(a, "{}", &s[..64]).unwrap(); + assert_eq!(a, s[..64].as_bytes()); + + let mut b = BytesMut::with_capacity(64); + write!(b, "{}", &s[..32]).unwrap(); + write!(b, "{}", &s[32..64]).unwrap(); + assert_eq!(b, s[..64].as_bytes()); + + let mut c = BytesMut::with_capacity(64); + write!(c, "{}", s).unwrap(); + assert_eq!(c, s[..].as_bytes()); +} + +#[test] +fn len() { + let a = Bytes::from(&b"abcdefg"[..]); + assert_eq!(a.len(), 7); + + let a = BytesMut::from(&b"abcdefg"[..]); + assert_eq!(a.len(), 7); + + let a = Bytes::from(&b""[..]); + assert!(a.is_empty()); + + let a = BytesMut::from(&b""[..]); + assert!(a.is_empty()); +} + +#[test] +fn index() { + let a = Bytes::from(&b"hello world"[..]); + assert_eq!(a[0..5], *b"hello"); +} + +#[test] +fn slice() { + let a = Bytes::from(&b"hello world"[..]); + + let b = a.slice(3..5); + assert_eq!(b, b"lo"[..]); + + let b = a.slice(0..0); + assert_eq!(b, b""[..]); + + let b = a.slice(3..3); + assert_eq!(b, b""[..]); + + let b = a.slice(a.len()..a.len()); + assert_eq!(b, b""[..]); + + let b = a.slice(..5); + assert_eq!(b, b"hello"[..]); + + let b = a.slice(3..); + assert_eq!(b, b"lo world"[..]); +} + +#[test] +#[should_panic] +fn slice_oob_1() { + let a = Bytes::from(&b"hello world"[..]); + a.slice(5..44); +} + +#[test] +#[should_panic] +fn slice_oob_2() { + let a = Bytes::from(&b"hello world"[..]); + a.slice(44..49); +} + +#[test] +fn split_off() { + let mut hello = Bytes::from(&b"helloworld"[..]); + let world = hello.split_off(5); + + assert_eq!(hello, &b"hello"[..]); + assert_eq!(world, &b"world"[..]); + + let mut hello = BytesMut::from(&b"helloworld"[..]); + let world = hello.split_off(5); + + assert_eq!(hello, &b"hello"[..]); + assert_eq!(world, &b"world"[..]); +} + +#[test] +#[should_panic] +fn split_off_oob() { + let mut hello = Bytes::from(&b"helloworld"[..]); + let _ = hello.split_off(44); +} + +#[test] +fn split_off_uninitialized() { + let mut bytes = BytesMut::with_capacity(1024); + let other = bytes.split_off(128); + + assert_eq!(bytes.len(), 0); + assert_eq!(bytes.capacity(), 128); + + assert_eq!(other.len(), 0); + assert_eq!(other.capacity(), 896); +} + +#[test] +fn split_off_to_loop() { + let s = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + for i in 0..(s.len() + 1) { + { + let mut bytes = Bytes::from(&s[..]); + let off = bytes.split_off(i); + assert_eq!(i, bytes.len()); + let mut sum = Vec::new(); + sum.extend(bytes.iter()); + sum.extend(off.iter()); + assert_eq!(&s[..], &sum[..]); + } + { + let mut bytes = BytesMut::from(&s[..]); + let off = bytes.split_off(i); + assert_eq!(i, bytes.len()); + let mut sum = Vec::new(); + sum.extend(&bytes); + sum.extend(&off); + assert_eq!(&s[..], &sum[..]); + } + { + let mut bytes = Bytes::from(&s[..]); + let off = bytes.split_to(i); + assert_eq!(i, off.len()); + let mut sum = Vec::new(); + sum.extend(off.iter()); + sum.extend(bytes.iter()); + assert_eq!(&s[..], &sum[..]); + } + { + let mut bytes = BytesMut::from(&s[..]); + let off = bytes.split_to(i); + assert_eq!(i, off.len()); + let mut sum = Vec::new(); + sum.extend(&off); + sum.extend(&bytes); + assert_eq!(&s[..], &sum[..]); + } + } +} + +#[test] +fn split_to_1() { + // Static + let mut a = Bytes::from_static(SHORT); + let b = a.split_to(4); + + assert_eq!(SHORT[4..], a); + assert_eq!(SHORT[..4], b); + + // Allocated + let mut a = Bytes::copy_from_slice(LONG); + let b = a.split_to(4); + + assert_eq!(LONG[4..], a); + assert_eq!(LONG[..4], b); + + let mut a = Bytes::copy_from_slice(LONG); + let b = a.split_to(30); + + assert_eq!(LONG[30..], a); + assert_eq!(LONG[..30], b); +} + +#[test] +fn split_to_2() { + let mut a = Bytes::from(LONG); + assert_eq!(LONG, a); + + let b = a.split_to(1); + + assert_eq!(LONG[1..], a); + drop(b); +} + +#[test] +#[should_panic] +fn split_to_oob() { + let mut hello = Bytes::from(&b"helloworld"[..]); + let _ = hello.split_to(33); +} + +#[test] +#[should_panic] +fn split_to_oob_mut() { + let mut hello = BytesMut::from(&b"helloworld"[..]); + let _ = hello.split_to(33); +} + +#[test] +#[should_panic] +fn split_to_uninitialized() { + let mut bytes = BytesMut::with_capacity(1024); + let _other = bytes.split_to(128); +} + +#[test] +fn split_off_to_at_gt_len() { + fn make_bytes() -> Bytes { + let mut bytes = BytesMut::with_capacity(100); + bytes.put_slice(&[10, 20, 30, 40]); + bytes.freeze() + } + + use std::panic; + + let _ = make_bytes().split_to(4); + let _ = make_bytes().split_off(4); + + assert!(panic::catch_unwind(move || { + let _ = make_bytes().split_to(5); + }) + .is_err()); + + assert!(panic::catch_unwind(move || { + let _ = make_bytes().split_off(5); + }) + .is_err()); +} + +#[test] +fn truncate() { + let s = &b"helloworld"[..]; + let mut hello = Bytes::from(s); + hello.truncate(15); + assert_eq!(hello, s); + hello.truncate(10); + assert_eq!(hello, s); + hello.truncate(5); + assert_eq!(hello, "hello"); +} + +#[test] +fn freeze_clone_shared() { + let s = &b"abcdefgh"[..]; + let b = BytesMut::from(s).split().freeze(); + assert_eq!(b, s); + let c = b.clone(); + assert_eq!(c, s); +} + +#[test] +fn freeze_clone_unique() { + let s = &b"abcdefgh"[..]; + let b = BytesMut::from(s).freeze(); + assert_eq!(b, s); + let c = b.clone(); + assert_eq!(c, s); +} + +#[test] +fn freeze_after_advance() { + let s = &b"abcdefgh"[..]; + let mut b = BytesMut::from(s); + b.advance(1); + assert_eq!(b, s[1..]); + let b = b.freeze(); + // Verify fix for #352. Previously, freeze would ignore the start offset + // for BytesMuts in Vec mode. + assert_eq!(b, s[1..]); +} + +#[test] +fn freeze_after_advance_arc() { + let s = &b"abcdefgh"[..]; + let mut b = BytesMut::from(s); + // Make b Arc + let _ = b.split_to(0); + b.advance(1); + assert_eq!(b, s[1..]); + let b = b.freeze(); + assert_eq!(b, s[1..]); +} + +#[test] +fn freeze_after_split_to() { + let s = &b"abcdefgh"[..]; + let mut b = BytesMut::from(s); + let _ = b.split_to(1); + assert_eq!(b, s[1..]); + let b = b.freeze(); + assert_eq!(b, s[1..]); +} + +#[test] +fn freeze_after_truncate() { + let s = &b"abcdefgh"[..]; + let mut b = BytesMut::from(s); + b.truncate(7); + assert_eq!(b, s[..7]); + let b = b.freeze(); + assert_eq!(b, s[..7]); +} + +#[test] +fn freeze_after_truncate_arc() { + let s = &b"abcdefgh"[..]; + let mut b = BytesMut::from(s); + // Make b Arc + let _ = b.split_to(0); + b.truncate(7); + assert_eq!(b, s[..7]); + let b = b.freeze(); + assert_eq!(b, s[..7]); +} + +#[test] +fn freeze_after_split_off() { + let s = &b"abcdefgh"[..]; + let mut b = BytesMut::from(s); + let _ = b.split_off(7); + assert_eq!(b, s[..7]); + let b = b.freeze(); + assert_eq!(b, s[..7]); +} + +#[test] +fn fns_defined_for_bytes_mut() { + let mut bytes = BytesMut::from(&b"hello world"[..]); + + let _ = bytes.as_ptr(); + let _ = bytes.as_mut_ptr(); + + // Iterator + let v: Vec = bytes.as_ref().iter().cloned().collect(); + assert_eq!(&v[..], bytes); +} + +#[test] +fn reserve_convert() { + // Vec -> Vec + let mut bytes = BytesMut::from(LONG); + bytes.reserve(64); + assert_eq!(bytes.capacity(), LONG.len() + 64); + + // Arc -> Vec + let mut bytes = BytesMut::from(LONG); + let a = bytes.split_to(30); + + bytes.reserve(128); + assert!(bytes.capacity() >= bytes.len() + 128); + + drop(a); +} + +#[test] +fn reserve_growth() { + let mut bytes = BytesMut::with_capacity(64); + bytes.put("hello world".as_bytes()); + let _ = bytes.split(); + + bytes.reserve(65); + assert_eq!(bytes.capacity(), 117); +} + +#[test] +fn reserve_allocates_at_least_original_capacity() { + let mut bytes = BytesMut::with_capacity(1024); + + for i in 0..1020 { + bytes.put_u8(i as u8); + } + + let _other = bytes.split(); + + bytes.reserve(16); + assert_eq!(bytes.capacity(), 1024); +} + +#[test] +#[cfg_attr(miri, ignore)] // Miri is too slow +fn reserve_max_original_capacity_value() { + const SIZE: usize = 128 * 1024; + + let mut bytes = BytesMut::with_capacity(SIZE); + + for _ in 0..SIZE { + bytes.put_u8(0u8); + } + + let _other = bytes.split(); + + bytes.reserve(16); + assert_eq!(bytes.capacity(), 64 * 1024); +} + +#[test] +fn reserve_vec_recycling() { + let mut bytes = BytesMut::with_capacity(16); + assert_eq!(bytes.capacity(), 16); + let addr = bytes.as_ptr() as usize; + bytes.put("0123456789012345".as_bytes()); + assert_eq!(bytes.as_ptr() as usize, addr); + bytes.advance(10); + assert_eq!(bytes.capacity(), 6); + bytes.reserve(8); + assert_eq!(bytes.capacity(), 16); + assert_eq!(bytes.as_ptr() as usize, addr); +} + +#[test] +fn reserve_in_arc_unique_does_not_overallocate() { + let mut bytes = BytesMut::with_capacity(1000); + let _ = bytes.split(); + + // now bytes is Arc and refcount == 1 + + assert_eq!(1000, bytes.capacity()); + bytes.reserve(2001); + assert_eq!(2001, bytes.capacity()); +} + +#[test] +fn reserve_in_arc_unique_doubles() { + let mut bytes = BytesMut::with_capacity(1000); + let _ = bytes.split(); + + // now bytes is Arc and refcount == 1 + + assert_eq!(1000, bytes.capacity()); + bytes.reserve(1001); + assert_eq!(2000, bytes.capacity()); +} + +#[test] +fn reserve_in_arc_unique_does_not_overallocate_after_split() { + let mut bytes = BytesMut::from(LONG); + let orig_capacity = bytes.capacity(); + drop(bytes.split_off(LONG.len() / 2)); + + // now bytes is Arc and refcount == 1 + + let new_capacity = bytes.capacity(); + bytes.reserve(orig_capacity - new_capacity); + assert_eq!(bytes.capacity(), orig_capacity); +} + +#[test] +fn reserve_in_arc_unique_does_not_overallocate_after_multiple_splits() { + let mut bytes = BytesMut::from(LONG); + let orig_capacity = bytes.capacity(); + for _ in 0..10 { + drop(bytes.split_off(LONG.len() / 2)); + + // now bytes is Arc and refcount == 1 + + let new_capacity = bytes.capacity(); + bytes.reserve(orig_capacity - new_capacity); + } + assert_eq!(bytes.capacity(), orig_capacity); +} + +#[test] +fn reserve_in_arc_nonunique_does_not_overallocate() { + let mut bytes = BytesMut::with_capacity(1000); + let _copy = bytes.split(); + + // now bytes is Arc and refcount == 2 + + assert_eq!(1000, bytes.capacity()); + bytes.reserve(2001); + assert_eq!(2001, bytes.capacity()); +} + +/// This function tests `BytesMut::reserve_inner`, where `BytesMut` holds +/// a unique reference to the shared vector and decide to reuse it +/// by reallocating the `Vec`. +#[test] +fn reserve_shared_reuse() { + let mut bytes = BytesMut::with_capacity(1000); + bytes.put_slice(b"Hello, World!"); + drop(bytes.split()); + + bytes.put_slice(b"!123ex123,sadchELLO,_wORLD!"); + // Use split_off so that v.capacity() - self.cap != off + drop(bytes.split_off(9)); + assert_eq!(&*bytes, b"!123ex123"); + + bytes.reserve(2000); + assert_eq!(&*bytes, b"!123ex123"); + assert_eq!(bytes.capacity(), 2009); +} + +#[test] +fn extend_mut() { + let mut bytes = BytesMut::with_capacity(0); + bytes.extend(LONG); + assert_eq!(*bytes, LONG[..]); +} + +#[test] +fn extend_from_slice_mut() { + for &i in &[3, 34] { + let mut bytes = BytesMut::new(); + bytes.extend_from_slice(&LONG[..i]); + bytes.extend_from_slice(&LONG[i..]); + assert_eq!(LONG[..], *bytes); + } +} + +#[test] +fn extend_mut_from_bytes() { + let mut bytes = BytesMut::with_capacity(0); + bytes.extend([Bytes::from(LONG)]); + assert_eq!(*bytes, LONG[..]); +} + +#[test] +fn extend_mut_without_size_hint() { + let mut bytes = BytesMut::with_capacity(0); + let mut long_iter = LONG.iter(); + + // Use iter::from_fn since it doesn't know a size_hint + bytes.extend(std::iter::from_fn(|| long_iter.next())); + assert_eq!(*bytes, LONG[..]); +} + +#[test] +fn from_static() { + let mut a = Bytes::from_static(b"ab"); + let b = a.split_off(1); + + assert_eq!(a, b"a"[..]); + assert_eq!(b, b"b"[..]); +} + +#[test] +fn advance_static() { + let mut a = Bytes::from_static(b"hello world"); + a.advance(6); + assert_eq!(a, &b"world"[..]); +} + +#[test] +fn advance_vec() { + let mut a = Bytes::from(b"hello world boooo yah world zomg wat wat".to_vec()); + a.advance(16); + assert_eq!(a, b"o yah world zomg wat wat"[..]); + + a.advance(4); + assert_eq!(a, b"h world zomg wat wat"[..]); + + a.advance(6); + assert_eq!(a, b"d zomg wat wat"[..]); +} + +#[test] +fn advance_bytes_mut() { + let mut a = BytesMut::from("hello world boooo yah world zomg wat wat"); + a.advance(16); + assert_eq!(a, b"o yah world zomg wat wat"[..]); + + a.advance(4); + assert_eq!(a, b"h world zomg wat wat"[..]); + + // Reserve some space. + a.reserve(1024); + assert_eq!(a, b"h world zomg wat wat"[..]); + + a.advance(6); + assert_eq!(a, b"d zomg wat wat"[..]); +} + +#[test] +#[should_panic] +fn advance_past_len() { + let mut a = BytesMut::from("hello world"); + a.advance(20); +} + +#[test] +// Only run these tests on little endian systems. CI uses qemu for testing +// big endian... and qemu doesn't really support threading all that well. +#[cfg(any(miri, target_endian = "little"))] +fn stress() { + // Tests promoting a buffer from a vec -> shared in a concurrent situation + use std::sync::{Arc, Barrier}; + use std::thread; + + const THREADS: usize = 8; + const ITERS: usize = if cfg!(miri) { 100 } else { 1_000 }; + + for i in 0..ITERS { + let data = [i as u8; 256]; + let buf = Arc::new(Bytes::copy_from_slice(&data[..])); + + let barrier = Arc::new(Barrier::new(THREADS)); + let mut joins = Vec::with_capacity(THREADS); + + for _ in 0..THREADS { + let c = barrier.clone(); + let buf = buf.clone(); + + joins.push(thread::spawn(move || { + c.wait(); + let buf: Bytes = (*buf).clone(); + drop(buf); + })); + } + + for th in joins { + th.join().unwrap(); + } + + assert_eq!(*buf, data[..]); + } +} + +#[test] +fn partial_eq_bytesmut() { + let bytes = Bytes::from(&b"The quick red fox"[..]); + let bytesmut = BytesMut::from(&b"The quick red fox"[..]); + assert!(bytes == bytesmut); + assert!(bytesmut == bytes); + let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]); + assert!(bytes2 != bytesmut); + assert!(bytesmut != bytes2); +} + +/* +#[test] +fn bytes_unsplit_basic() { + let buf = Bytes::from(&b"aaabbbcccddd"[..]); + + let splitted = buf.split_off(6); + assert_eq!(b"aaabbb", &buf[..]); + assert_eq!(b"cccddd", &splitted[..]); + + buf.unsplit(splitted); + assert_eq!(b"aaabbbcccddd", &buf[..]); +} + +#[test] +fn bytes_unsplit_empty_other() { + let buf = Bytes::from(&b"aaabbbcccddd"[..]); + + // empty other + let other = Bytes::new(); + + buf.unsplit(other); + assert_eq!(b"aaabbbcccddd", &buf[..]); +} + +#[test] +fn bytes_unsplit_empty_self() { + // empty self + let mut buf = Bytes::new(); + + let mut other = Bytes::with_capacity(64); + other.extend_from_slice(b"aaabbbcccddd"); + + buf.unsplit(other); + assert_eq!(b"aaabbbcccddd", &buf[..]); +} + +#[test] +fn bytes_unsplit_arc_different() { + let mut buf = Bytes::with_capacity(64); + buf.extend_from_slice(b"aaaabbbbeeee"); + + buf.split_off(8); //arc + + let mut buf2 = Bytes::with_capacity(64); + buf2.extend_from_slice(b"ccccddddeeee"); + + buf2.split_off(8); //arc + + buf.unsplit(buf2); + assert_eq!(b"aaaabbbbccccdddd", &buf[..]); +} + +#[test] +fn bytes_unsplit_arc_non_contiguous() { + let mut buf = Bytes::with_capacity(64); + buf.extend_from_slice(b"aaaabbbbeeeeccccdddd"); + + let mut buf2 = buf.split_off(8); //arc + + let buf3 = buf2.split_off(4); //arc + + buf.unsplit(buf3); + assert_eq!(b"aaaabbbbccccdddd", &buf[..]); +} + +#[test] +fn bytes_unsplit_two_split_offs() { + let mut buf = Bytes::with_capacity(64); + buf.extend_from_slice(b"aaaabbbbccccdddd"); + + let mut buf2 = buf.split_off(8); //arc + let buf3 = buf2.split_off(4); //arc + + buf2.unsplit(buf3); + buf.unsplit(buf2); + assert_eq!(b"aaaabbbbccccdddd", &buf[..]); +} + +#[test] +fn bytes_unsplit_overlapping_references() { + let mut buf = Bytes::with_capacity(64); + buf.extend_from_slice(b"abcdefghijklmnopqrstuvwxyz"); + let mut buf0010 = buf.slice(0..10); + let buf1020 = buf.slice(10..20); + let buf0515 = buf.slice(5..15); + buf0010.unsplit(buf1020); + assert_eq!(b"abcdefghijklmnopqrst", &buf0010[..]); + assert_eq!(b"fghijklmno", &buf0515[..]); +} +*/ + +#[test] +fn bytes_mut_unsplit_basic() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aaabbbcccddd"); + + let splitted = buf.split_off(6); + assert_eq!(b"aaabbb", &buf[..]); + assert_eq!(b"cccddd", &splitted[..]); + + buf.unsplit(splitted); + assert_eq!(b"aaabbbcccddd", &buf[..]); +} + +#[test] +fn bytes_mut_unsplit_empty_other() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aaabbbcccddd"); + + // empty other + let other = BytesMut::new(); + + buf.unsplit(other); + assert_eq!(b"aaabbbcccddd", &buf[..]); +} + +#[test] +fn bytes_mut_unsplit_empty_self() { + // empty self + let mut buf = BytesMut::new(); + + let mut other = BytesMut::with_capacity(64); + other.extend_from_slice(b"aaabbbcccddd"); + + buf.unsplit(other); + assert_eq!(b"aaabbbcccddd", &buf[..]); +} + +#[test] +fn bytes_mut_unsplit_other_keeps_capacity() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aabb"); + + // non empty other created "from" buf + let mut other = buf.split_off(buf.len()); + other.extend_from_slice(b"ccddee"); + buf.unsplit(other); + + assert_eq!(buf.capacity(), 64); +} + +#[test] +fn bytes_mut_unsplit_empty_other_keeps_capacity() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aabbccddee"); + + // empty other created "from" buf + let other = buf.split_off(buf.len()); + buf.unsplit(other); + + assert_eq!(buf.capacity(), 64); +} + +#[test] +fn bytes_mut_unsplit_arc_different() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aaaabbbbeeee"); + + let _ = buf.split_off(8); //arc + + let mut buf2 = BytesMut::with_capacity(64); + buf2.extend_from_slice(b"ccccddddeeee"); + + let _ = buf2.split_off(8); //arc + + buf.unsplit(buf2); + assert_eq!(b"aaaabbbbccccdddd", &buf[..]); +} + +#[test] +fn bytes_mut_unsplit_arc_non_contiguous() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aaaabbbbeeeeccccdddd"); + + let mut buf2 = buf.split_off(8); //arc + + let buf3 = buf2.split_off(4); //arc + + buf.unsplit(buf3); + assert_eq!(b"aaaabbbbccccdddd", &buf[..]); +} + +#[test] +fn bytes_mut_unsplit_two_split_offs() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aaaabbbbccccdddd"); + + let mut buf2 = buf.split_off(8); //arc + let buf3 = buf2.split_off(4); //arc + + buf2.unsplit(buf3); + buf.unsplit(buf2); + assert_eq!(b"aaaabbbbccccdddd", &buf[..]); +} + +#[test] +fn from_iter_no_size_hint() { + use std::iter; + + let mut expect = vec![]; + + let actual: Bytes = iter::repeat(b'x') + .scan(100, |cnt, item| { + if *cnt >= 1 { + *cnt -= 1; + expect.push(item); + Some(item) + } else { + None + } + }) + .collect(); + + assert_eq!(&actual[..], &expect[..]); +} + +fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) { + let slice = &(bytes.as_ref()[start..end]); + let sub = bytes.slice_ref(slice); + assert_eq!(&sub[..], expected); +} + +#[test] +fn slice_ref_works() { + let bytes = Bytes::from(&b"012345678"[..]); + + test_slice_ref(&bytes, 0, 0, b""); + test_slice_ref(&bytes, 0, 3, b"012"); + test_slice_ref(&bytes, 2, 6, b"2345"); + test_slice_ref(&bytes, 7, 9, b"78"); + test_slice_ref(&bytes, 9, 9, b""); +} + +#[test] +fn slice_ref_empty() { + let bytes = Bytes::from(&b""[..]); + let slice = &(bytes.as_ref()[0..0]); + + let sub = bytes.slice_ref(slice); + assert_eq!(&sub[..], b""); +} + +#[test] +fn slice_ref_empty_subslice() { + let bytes = Bytes::from(&b"abcde"[..]); + let subbytes = bytes.slice(0..0); + let slice = &subbytes[..]; + // The `slice` object is derived from the original `bytes` object + // so `slice_ref` should work. + assert_eq!(Bytes::new(), bytes.slice_ref(slice)); +} + +#[test] +#[should_panic] +fn slice_ref_catches_not_a_subset() { + let bytes = Bytes::from(&b"012345678"[..]); + let slice = &b"012345"[0..4]; + + bytes.slice_ref(slice); +} + +#[test] +fn slice_ref_not_an_empty_subset() { + let bytes = Bytes::from(&b"012345678"[..]); + let slice = &b""[0..0]; + + assert_eq!(Bytes::new(), bytes.slice_ref(slice)); +} + +#[test] +fn empty_slice_ref_not_an_empty_subset() { + let bytes = Bytes::new(); + let slice = &b"some other slice"[0..0]; + + assert_eq!(Bytes::new(), bytes.slice_ref(slice)); +} + +#[test] +fn bytes_buf_mut_advance() { + let mut bytes = BytesMut::with_capacity(1024); + + unsafe { + let ptr = bytes.chunk_mut().as_mut_ptr(); + assert_eq!(1024, bytes.chunk_mut().len()); + + bytes.advance_mut(10); + + let next = bytes.chunk_mut().as_mut_ptr(); + assert_eq!(1024 - 10, bytes.chunk_mut().len()); + assert_eq!(ptr.offset(10), next); + + // advance to the end + bytes.advance_mut(1024 - 10); + + // The buffer size is doubled + assert_eq!(1024, bytes.chunk_mut().len()); + } +} + +#[test] +fn bytes_buf_mut_reuse_when_fully_consumed() { + use bytes::{Buf, BytesMut}; + let mut buf = BytesMut::new(); + buf.reserve(8192); + buf.extend_from_slice(&[0u8; 100][..]); + + let p = &buf[0] as *const u8; + buf.advance(100); + + buf.reserve(8192); + buf.extend_from_slice(b" "); + + assert_eq!(&buf[0] as *const u8, p); +} + +#[test] +#[should_panic] +fn bytes_reserve_overflow() { + let mut bytes = BytesMut::with_capacity(1024); + bytes.put_slice(b"hello world"); + + bytes.reserve(usize::MAX); +} + +#[test] +fn bytes_with_capacity_but_empty() { + // See https://github.com/tokio-rs/bytes/issues/340 + let vec = Vec::with_capacity(1); + let _ = Bytes::from(vec); +} + +#[test] +fn bytes_put_bytes() { + let mut bytes = BytesMut::new(); + bytes.put_u8(17); + bytes.put_bytes(19, 2); + assert_eq!([17, 19, 19], bytes.as_ref()); +} + +#[test] +fn box_slice_empty() { + // See https://github.com/tokio-rs/bytes/issues/340 + let empty: Box<[u8]> = Default::default(); + let b = Bytes::from(empty); + assert!(b.is_empty()); +} + +#[test] +fn bytes_into_vec() { + // Test kind == KIND_VEC + let content = b"helloworld"; + + let mut bytes = BytesMut::new(); + bytes.put_slice(content); + + let vec: Vec = bytes.into(); + assert_eq!(&vec, content); + + // Test kind == KIND_ARC, shared.is_unique() == True + let mut bytes = BytesMut::new(); + bytes.put_slice(b"abcdewe23"); + bytes.put_slice(content); + + // Overwrite the bytes to make sure only one reference to the underlying + // Vec exists. + bytes = bytes.split_off(9); + + let vec: Vec = bytes.into(); + assert_eq!(&vec, content); + + // Test kind == KIND_ARC, shared.is_unique() == False + let prefix = b"abcdewe23"; + + let mut bytes = BytesMut::new(); + bytes.put_slice(prefix); + bytes.put_slice(content); + + let vec: Vec = bytes.split_off(prefix.len()).into(); + assert_eq!(&vec, content); + + let vec: Vec = bytes.into(); + assert_eq!(&vec, prefix); +} + +#[test] +fn test_bytes_into_vec() { + // Test STATIC_VTABLE.to_vec + let bs = b"1b23exfcz3r"; + let vec: Vec = Bytes::from_static(bs).into(); + assert_eq!(&*vec, bs); + + // Test bytes_mut.SHARED_VTABLE.to_vec impl + eprintln!("1"); + let mut bytes_mut: BytesMut = bs[..].into(); + + // Set kind to KIND_ARC so that after freeze, Bytes will use bytes_mut.SHARED_VTABLE + eprintln!("2"); + drop(bytes_mut.split_off(bs.len())); + + eprintln!("3"); + let b1 = bytes_mut.freeze(); + eprintln!("4"); + let b2 = b1.clone(); + + eprintln!("{:#?}", (&*b1).as_ptr()); + + // shared.is_unique() = False + eprintln!("5"); + assert_eq!(&*Vec::from(b2), bs); + + // shared.is_unique() = True + eprintln!("6"); + assert_eq!(&*Vec::from(b1), bs); + + // Test bytes_mut.SHARED_VTABLE.to_vec impl where offset != 0 + let mut bytes_mut1: BytesMut = bs[..].into(); + let bytes_mut2 = bytes_mut1.split_off(9); + + let b1 = bytes_mut1.freeze(); + let b2 = bytes_mut2.freeze(); + + assert_eq!(Vec::from(b2), bs[9..]); + assert_eq!(Vec::from(b1), bs[..9]); +} + +#[test] +fn test_bytes_into_vec_promotable_even() { + let vec = vec![33u8; 1024]; + + // Test cases where kind == KIND_VEC + let b1 = Bytes::from(vec.clone()); + assert_eq!(Vec::from(b1), vec); + + // Test cases where kind == KIND_ARC, ref_cnt == 1 + let b1 = Bytes::from(vec.clone()); + drop(b1.clone()); + assert_eq!(Vec::from(b1), vec); + + // Test cases where kind == KIND_ARC, ref_cnt == 2 + let b1 = Bytes::from(vec.clone()); + let b2 = b1.clone(); + assert_eq!(Vec::from(b1), vec); + + // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1 + assert_eq!(Vec::from(b2), vec); + + // Test cases where offset != 0 + let mut b1 = Bytes::from(vec.clone()); + let b2 = b1.split_off(20); + + assert_eq!(Vec::from(b2), vec[20..]); + assert_eq!(Vec::from(b1), vec[..20]); +} + +#[test] +fn test_bytes_vec_conversion() { + let mut vec = Vec::with_capacity(10); + vec.extend(b"abcdefg"); + let b = Bytes::from(vec); + let v = Vec::from(b); + assert_eq!(v.len(), 7); + assert_eq!(v.capacity(), 10); + + let mut b = Bytes::from(v); + b.advance(1); + let v = Vec::from(b); + assert_eq!(v.len(), 6); + assert_eq!(v.capacity(), 10); + assert_eq!(v.as_slice(), b"bcdefg"); +} + +#[test] +fn test_bytes_mut_conversion() { + let mut b1 = BytesMut::with_capacity(10); + b1.extend(b"abcdefg"); + let b2 = Bytes::from(b1); + let v = Vec::from(b2); + assert_eq!(v.len(), 7); + assert_eq!(v.capacity(), 10); + + let mut b = Bytes::from(v); + b.advance(1); + let v = Vec::from(b); + assert_eq!(v.len(), 6); + assert_eq!(v.capacity(), 10); + assert_eq!(v.as_slice(), b"bcdefg"); +} + +#[test] +fn test_bytes_capacity_len() { + for cap in 0..100 { + for len in 0..=cap { + let mut v = Vec::with_capacity(cap); + v.resize(len, 0); + let _ = Bytes::from(v); + } + } +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_bytes_odd_alloc.rs b/third_party/rust/bytes/v1/crate/tests/test_bytes_odd_alloc.rs new file mode 100644 index 000000000000..27ed87736209 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_bytes_odd_alloc.rs @@ -0,0 +1,97 @@ +//! Test using `Bytes` with an allocator that hands out "odd" pointers for +//! vectors (pointers where the LSB is set). + +#![cfg(not(miri))] // Miri does not support custom allocators (also, Miri is "odd" by default with 50% chance) + +use std::alloc::{GlobalAlloc, Layout, System}; +use std::ptr; + +use bytes::Bytes; + +#[global_allocator] +static ODD: Odd = Odd; + +struct Odd; + +unsafe impl GlobalAlloc for Odd { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + if layout.align() == 1 && layout.size() > 0 { + // Allocate slightly bigger so that we can offset the pointer by 1 + let size = layout.size() + 1; + let new_layout = match Layout::from_size_align(size, 1) { + Ok(layout) => layout, + Err(_err) => return ptr::null_mut(), + }; + let ptr = System.alloc(new_layout); + if !ptr.is_null() { + ptr.offset(1) + } else { + ptr + } + } else { + System.alloc(layout) + } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + if layout.align() == 1 && layout.size() > 0 { + let size = layout.size() + 1; + let new_layout = match Layout::from_size_align(size, 1) { + Ok(layout) => layout, + Err(_err) => std::process::abort(), + }; + System.dealloc(ptr.offset(-1), new_layout); + } else { + System.dealloc(ptr, layout); + } + } +} + +#[test] +fn sanity_check_odd_allocator() { + let vec = vec![33u8; 1024]; + let p = vec.as_ptr() as usize; + assert!(p & 0x1 == 0x1, "{:#b}", p); +} + +#[test] +fn test_bytes_from_vec_drop() { + let vec = vec![33u8; 1024]; + let _b = Bytes::from(vec); +} + +#[test] +fn test_bytes_clone_drop() { + let vec = vec![33u8; 1024]; + let b1 = Bytes::from(vec); + let _b2 = b1.clone(); +} + +#[test] +fn test_bytes_into_vec() { + let vec = vec![33u8; 1024]; + + // Test cases where kind == KIND_VEC + let b1 = Bytes::from(vec.clone()); + assert_eq!(Vec::from(b1), vec); + + // Test cases where kind == KIND_ARC, ref_cnt == 1 + let b1 = Bytes::from(vec.clone()); + drop(b1.clone()); + assert_eq!(Vec::from(b1), vec); + + // Test cases where kind == KIND_ARC, ref_cnt == 2 + let b1 = Bytes::from(vec.clone()); + let b2 = b1.clone(); + assert_eq!(Vec::from(b1), vec); + + // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1 + assert_eq!(Vec::from(b2), vec); + + // Test cases where offset != 0 + let mut b1 = Bytes::from(vec.clone()); + let b2 = b1.split_off(20); + + assert_eq!(Vec::from(b2), vec[20..]); + assert_eq!(Vec::from(b1), vec[..20]); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_bytes_vec_alloc.rs b/third_party/rust/bytes/v1/crate/tests/test_bytes_vec_alloc.rs new file mode 100644 index 000000000000..107e56e58cde --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_bytes_vec_alloc.rs @@ -0,0 +1,143 @@ +use std::alloc::{GlobalAlloc, Layout, System}; +use std::ptr::null_mut; +use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; + +use bytes::{Buf, Bytes}; + +#[global_allocator] +static LEDGER: Ledger = Ledger::new(); + +const LEDGER_LENGTH: usize = 2048; + +struct Ledger { + alloc_table: [(AtomicPtr, AtomicUsize); LEDGER_LENGTH], +} + +impl Ledger { + const fn new() -> Self { + const ELEM: (AtomicPtr, AtomicUsize) = + (AtomicPtr::new(null_mut()), AtomicUsize::new(0)); + let alloc_table = [ELEM; LEDGER_LENGTH]; + + Self { alloc_table } + } + + /// Iterate over our table until we find an open entry, then insert into said entry + fn insert(&self, ptr: *mut u8, size: usize) { + for (entry_ptr, entry_size) in self.alloc_table.iter() { + // SeqCst is good enough here, we don't care about perf, i just want to be correct! + if entry_ptr + .compare_exchange(null_mut(), ptr, Ordering::SeqCst, Ordering::SeqCst) + .is_ok() + { + entry_size.store(size, Ordering::SeqCst); + break; + } + } + } + + fn remove(&self, ptr: *mut u8) -> usize { + for (entry_ptr, entry_size) in self.alloc_table.iter() { + // set the value to be something that will never try and be deallocated, so that we + // don't have any chance of a race condition + // + // dont worry, LEDGER_LENGTH is really long to compensate for us not reclaiming space + if entry_ptr + .compare_exchange( + ptr, + invalid_ptr(usize::MAX), + Ordering::SeqCst, + Ordering::SeqCst, + ) + .is_ok() + { + return entry_size.load(Ordering::SeqCst); + } + } + + panic!("Couldn't find a matching entry for {:x?}", ptr); + } +} + +unsafe impl GlobalAlloc for Ledger { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let size = layout.size(); + let ptr = System.alloc(layout); + self.insert(ptr, size); + ptr + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + let orig_size = self.remove(ptr); + + if orig_size != layout.size() { + panic!( + "bad dealloc: alloc size was {}, dealloc size is {}", + orig_size, + layout.size() + ); + } else { + System.dealloc(ptr, layout); + } + } +} + +#[test] +fn test_bytes_advance() { + let mut bytes = Bytes::from(vec![10, 20, 30]); + bytes.advance(1); + drop(bytes); +} + +#[test] +fn test_bytes_truncate() { + let mut bytes = Bytes::from(vec![10, 20, 30]); + bytes.truncate(2); + drop(bytes); +} + +#[test] +fn test_bytes_truncate_and_advance() { + let mut bytes = Bytes::from(vec![10, 20, 30]); + bytes.truncate(2); + bytes.advance(1); + drop(bytes); +} + +/// Returns a dangling pointer with the given address. This is used to store +/// integer data in pointer fields. +#[inline] +fn invalid_ptr(addr: usize) -> *mut T { + let ptr = std::ptr::null_mut::().wrapping_add(addr); + debug_assert_eq!(ptr as usize, addr); + ptr.cast::() +} + +#[test] +fn test_bytes_into_vec() { + let vec = vec![33u8; 1024]; + + // Test cases where kind == KIND_VEC + let b1 = Bytes::from(vec.clone()); + assert_eq!(Vec::from(b1), vec); + + // Test cases where kind == KIND_ARC, ref_cnt == 1 + let b1 = Bytes::from(vec.clone()); + drop(b1.clone()); + assert_eq!(Vec::from(b1), vec); + + // Test cases where kind == KIND_ARC, ref_cnt == 2 + let b1 = Bytes::from(vec.clone()); + let b2 = b1.clone(); + assert_eq!(Vec::from(b1), vec); + + // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1 + assert_eq!(Vec::from(b2), vec); + + // Test cases where offset != 0 + let mut b1 = Bytes::from(vec.clone()); + let b2 = b1.split_off(20); + + assert_eq!(Vec::from(b2), vec[20..]); + assert_eq!(Vec::from(b1), vec[..20]); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_chain.rs b/third_party/rust/bytes/v1/crate/tests/test_chain.rs new file mode 100644 index 000000000000..cfda6b8dc7ea --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_chain.rs @@ -0,0 +1,177 @@ +#![warn(rust_2018_idioms)] + +use bytes::{Buf, BufMut, Bytes}; +#[cfg(feature = "std")] +use std::io::IoSlice; + +#[test] +fn collect_two_bufs() { + let a = Bytes::from(&b"hello"[..]); + let b = Bytes::from(&b"world"[..]); + + let res = a.chain(b).copy_to_bytes(10); + assert_eq!(res, &b"helloworld"[..]); +} + +#[test] +fn writing_chained() { + let mut a = [0u8; 64]; + let mut b = [0u8; 64]; + + { + let mut buf = (&mut a[..]).chain_mut(&mut b[..]); + + for i in 0u8..128 { + buf.put_u8(i); + } + } + + for i in 0..64 { + let expect = i as u8; + assert_eq!(expect, a[i]); + assert_eq!(expect + 64, b[i]); + } +} + +#[test] +fn iterating_two_bufs() { + let a = Bytes::from(&b"hello"[..]); + let b = Bytes::from(&b"world"[..]); + + let res: Vec = a.chain(b).into_iter().collect(); + assert_eq!(res, &b"helloworld"[..]); +} + +#[cfg(feature = "std")] +#[test] +fn vectored_read() { + let a = Bytes::from(&b"hello"[..]); + let b = Bytes::from(&b"world"[..]); + + let mut buf = a.chain(b); + + { + let b1: &[u8] = &mut []; + let b2: &[u8] = &mut []; + let b3: &[u8] = &mut []; + let b4: &[u8] = &mut []; + let mut iovecs = [ + IoSlice::new(b1), + IoSlice::new(b2), + IoSlice::new(b3), + IoSlice::new(b4), + ]; + + assert_eq!(2, buf.chunks_vectored(&mut iovecs)); + assert_eq!(iovecs[0][..], b"hello"[..]); + assert_eq!(iovecs[1][..], b"world"[..]); + assert_eq!(iovecs[2][..], b""[..]); + assert_eq!(iovecs[3][..], b""[..]); + } + + buf.advance(2); + + { + let b1: &[u8] = &mut []; + let b2: &[u8] = &mut []; + let b3: &[u8] = &mut []; + let b4: &[u8] = &mut []; + let mut iovecs = [ + IoSlice::new(b1), + IoSlice::new(b2), + IoSlice::new(b3), + IoSlice::new(b4), + ]; + + assert_eq!(2, buf.chunks_vectored(&mut iovecs)); + assert_eq!(iovecs[0][..], b"llo"[..]); + assert_eq!(iovecs[1][..], b"world"[..]); + assert_eq!(iovecs[2][..], b""[..]); + assert_eq!(iovecs[3][..], b""[..]); + } + + buf.advance(3); + + { + let b1: &[u8] = &mut []; + let b2: &[u8] = &mut []; + let b3: &[u8] = &mut []; + let b4: &[u8] = &mut []; + let mut iovecs = [ + IoSlice::new(b1), + IoSlice::new(b2), + IoSlice::new(b3), + IoSlice::new(b4), + ]; + + assert_eq!(1, buf.chunks_vectored(&mut iovecs)); + assert_eq!(iovecs[0][..], b"world"[..]); + assert_eq!(iovecs[1][..], b""[..]); + assert_eq!(iovecs[2][..], b""[..]); + assert_eq!(iovecs[3][..], b""[..]); + } + + buf.advance(3); + + { + let b1: &[u8] = &mut []; + let b2: &[u8] = &mut []; + let b3: &[u8] = &mut []; + let b4: &[u8] = &mut []; + let mut iovecs = [ + IoSlice::new(b1), + IoSlice::new(b2), + IoSlice::new(b3), + IoSlice::new(b4), + ]; + + assert_eq!(1, buf.chunks_vectored(&mut iovecs)); + assert_eq!(iovecs[0][..], b"ld"[..]); + assert_eq!(iovecs[1][..], b""[..]); + assert_eq!(iovecs[2][..], b""[..]); + assert_eq!(iovecs[3][..], b""[..]); + } +} + +#[test] +fn chain_growing_buffer() { + let mut buff = [' ' as u8; 10]; + let mut vec = b"wassup".to_vec(); + + let mut chained = (&mut buff[..]).chain_mut(&mut vec).chain_mut(Vec::new()); // Required for potential overflow because remaining_mut for Vec is isize::MAX - vec.len(), but for chain_mut is usize::MAX + + chained.put_slice(b"hey there123123"); + + assert_eq!(&buff, b"hey there1"); + assert_eq!(&vec, b"wassup23123"); +} + +#[test] +fn chain_overflow_remaining_mut() { + let mut chained = Vec::::new().chain_mut(Vec::new()).chain_mut(Vec::new()); + + assert_eq!(chained.remaining_mut(), usize::MAX); + chained.put_slice(&[0; 256]); + assert_eq!(chained.remaining_mut(), usize::MAX); +} + +#[test] +fn chain_get_bytes() { + let mut ab = Bytes::copy_from_slice(b"ab"); + let mut cd = Bytes::copy_from_slice(b"cd"); + let ab_ptr = ab.as_ptr(); + let cd_ptr = cd.as_ptr(); + let mut chain = (&mut ab).chain(&mut cd); + let a = chain.copy_to_bytes(1); + let bc = chain.copy_to_bytes(2); + let d = chain.copy_to_bytes(1); + + assert_eq!(Bytes::copy_from_slice(b"a"), a); + assert_eq!(Bytes::copy_from_slice(b"bc"), bc); + assert_eq!(Bytes::copy_from_slice(b"d"), d); + + // assert `get_bytes` did not allocate + assert_eq!(ab_ptr, a.as_ptr()); + // assert `get_bytes` did not allocate + assert_eq!(cd_ptr.wrapping_offset(1), d.as_ptr()); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_debug.rs b/third_party/rust/bytes/v1/crate/tests/test_debug.rs new file mode 100644 index 000000000000..08d2f254e819 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_debug.rs @@ -0,0 +1,35 @@ +#![warn(rust_2018_idioms)] + +use bytes::Bytes; + +#[test] +fn fmt() { + let vec: Vec<_> = (0..0x100).map(|b| b as u8).collect(); + + let expected = "b\"\ + \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\ + \\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\ + \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\ + \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f\ + \x20!\\\"#$%&'()*+,-./0123456789:;<=>?\ + @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_\ + `abcdefghijklmnopqrstuvwxyz{|}~\\x7f\ + \\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\ + \\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\ + \\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\ + \\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\ + \\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\ + \\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\ + \\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\ + \\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\ + \\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\ + \\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\ + \\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\ + \\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\ + \\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\ + \\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\ + \\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\ + \\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff\""; + + assert_eq!(expected, format!("{:?}", Bytes::from(vec))); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_iter.rs b/third_party/rust/bytes/v1/crate/tests/test_iter.rs new file mode 100644 index 000000000000..a5bfddddf5e9 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_iter.rs @@ -0,0 +1,21 @@ +#![warn(rust_2018_idioms)] + +use bytes::Bytes; + +#[test] +fn iter_len() { + let buf = Bytes::from_static(b"hello world"); + let iter = buf.iter(); + + assert_eq!(iter.size_hint(), (11, Some(11))); + assert_eq!(iter.len(), 11); +} + +#[test] +fn empty_iter_len() { + let buf = Bytes::from_static(b""); + let iter = buf.iter(); + + assert_eq!(iter.size_hint(), (0, Some(0))); + assert_eq!(iter.len(), 0); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_reader.rs b/third_party/rust/bytes/v1/crate/tests/test_reader.rs new file mode 100644 index 000000000000..897aff645551 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_reader.rs @@ -0,0 +1,29 @@ +#![warn(rust_2018_idioms)] +#![cfg(feature = "std")] + +use std::io::{BufRead, Read}; + +use bytes::Buf; + +#[test] +fn read() { + let buf1 = &b"hello "[..]; + let buf2 = &b"world"[..]; + let buf = Buf::chain(buf1, buf2); // Disambiguate with Read::chain + let mut buffer = Vec::new(); + buf.reader().read_to_end(&mut buffer).unwrap(); + assert_eq!(b"hello world", &buffer[..]); +} + +#[test] +fn buf_read() { + let buf1 = &b"hell"[..]; + let buf2 = &b"o\nworld"[..]; + let mut reader = Buf::chain(buf1, buf2).reader(); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + assert_eq!("hello\n", &line); + line.clear(); + reader.read_line(&mut line).unwrap(); + assert_eq!("world", &line); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_serde.rs b/third_party/rust/bytes/v1/crate/tests/test_serde.rs new file mode 100644 index 000000000000..cf4aeffa7854 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_serde.rs @@ -0,0 +1,20 @@ +#![cfg(feature = "serde")] +#![warn(rust_2018_idioms)] + +use serde_test::{assert_tokens, Token}; + +#[test] +fn test_ser_de_empty() { + let b = bytes::Bytes::new(); + assert_tokens(&b, &[Token::Bytes(b"")]); + let b = bytes::BytesMut::with_capacity(0); + assert_tokens(&b, &[Token::Bytes(b"")]); +} + +#[test] +fn test_ser_de() { + let b = bytes::Bytes::from(&b"bytes"[..]); + assert_tokens(&b, &[Token::Bytes(b"bytes")]); + let b = bytes::BytesMut::from(&b"bytes"[..]); + assert_tokens(&b, &[Token::Bytes(b"bytes")]); +} diff --git a/third_party/rust/bytes/v1/crate/tests/test_take.rs b/third_party/rust/bytes/v1/crate/tests/test_take.rs new file mode 100644 index 000000000000..51df91d142f6 --- /dev/null +++ b/third_party/rust/bytes/v1/crate/tests/test_take.rs @@ -0,0 +1,32 @@ +#![warn(rust_2018_idioms)] + +use bytes::buf::Buf; +use bytes::Bytes; + +#[test] +fn long_take() { + // Tests that get a take with a size greater than the buffer length will not + // overrun the buffer. Regression test for #138. + let buf = b"hello world".take(100); + assert_eq!(11, buf.remaining()); + assert_eq!(b"hello world", buf.chunk()); +} + +#[test] +fn take_copy_to_bytes() { + let mut abcd = Bytes::copy_from_slice(b"abcd"); + let abcd_ptr = abcd.as_ptr(); + let mut take = (&mut abcd).take(2); + let a = take.copy_to_bytes(1); + assert_eq!(Bytes::copy_from_slice(b"a"), a); + // assert `to_bytes` did not allocate + assert_eq!(abcd_ptr, a.as_ptr()); + assert_eq!(Bytes::copy_from_slice(b"bcd"), abcd); +} + +#[test] +#[should_panic] +fn take_copy_to_bytes_panics() { + let abcd = Bytes::copy_from_slice(b"abcd"); + abcd.take(2).copy_to_bytes(3); +} diff --git a/third_party/rust/http/v1/BUILD.gn b/third_party/rust/http/v1/BUILD.gn index 4bafb8c45d62..ba6b1bdd9844 100644 --- a/third_party/rust/http/v1/BUILD.gn +++ b/third_party/rust/http/v1/BUILD.gn @@ -53,7 +53,7 @@ cargo_crate("lib") { executable_configs -= [ "//build/config/compiler:chromium_code" ] executable_configs += [ "//build/config/compiler:no_chromium_code" ] deps = [ - "//brave/third_party/rust/bytes/v0_4:lib", + "//brave/third_party/rust/bytes/v1:lib", "//brave/third_party/rust/fnv/v1:lib", "//third_party/rust/itoa/v1:lib", ] From e9203a54c965f30cbf9d86fa2bcfb54a304ef79a Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Tue, 12 Dec 2023 15:57:35 -0800 Subject: [PATCH 3/8] SKUs: port to rust http 1.0 crate Use the new stable release of the `http` crate for creating requests and responses. Update the builder pattern for an incompatible api change. --- components/skus/browser/rs/cxx/src/httpclient.rs | 13 ++++++++++--- components/skus/browser/rs/lib/BUILD.gn | 2 +- components/skus/browser/rs/lib/Cargo.toml | 2 +- .../browser/rs/lib/src/sdk/credentials/fetch.rs | 4 +--- components/skus/browser/rs/lib/src/sdk/orders.rs | 12 ++++++------ 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/components/skus/browser/rs/cxx/src/httpclient.rs b/components/skus/browser/rs/cxx/src/httpclient.rs index a0f7db621418..4a67ff7ddb33 100644 --- a/components/skus/browser/rs/cxx/src/httpclient.rs +++ b/components/skus/browser/rs/cxx/src/httpclient.rs @@ -50,9 +50,8 @@ impl From> for Result>, InternalErr fn from(resp: ffi::HttpResponse<'_>) -> Self { match resp.result { ffi::SkusResult::Ok => { - let mut response = http::Response::builder(); + let mut response = http::Response::builder().status(resp.return_code); - response.status(resp.return_code); for header in resp.headers { let header = header.to_string(); // header: value @@ -63,14 +62,22 @@ impl From> for Result>, InternalErr )) .debug_unwrap()?; let (key, value) = header.split_at(idx); + let key = http::HeaderName::try_from(key).map_err(|_| { + InternalError::InvalidCall("must pass a valid header name".to_string()) + })?; let value = value .get(1..) .ok_or(InternalError::InvalidCall( "must pass headers as `KEY: VALUE`".to_string(), )) .debug_unwrap()?; + let value = http::HeaderValue::try_from(value).map_err(|_| { + InternalError::InvalidCall( + "must pass a valid (ASCII-printable) header value".to_string(), + ) + })?; - response.header(key, value); + response.headers_mut().ok_or(InternalError::BorrowFailed)?.insert(key, value); } response diff --git a/components/skus/browser/rs/lib/BUILD.gn b/components/skus/browser/rs/lib/BUILD.gn index d43df88de345..25391ea1ea54 100644 --- a/components/skus/browser/rs/lib/BUILD.gn +++ b/components/skus/browser/rs/lib/BUILD.gn @@ -31,7 +31,7 @@ rust_static_library("rust_lib") { "//brave/third_party/rust/data_encoding/v2:lib", "//brave/third_party/rust/futures_retry/v0_5:lib", "//brave/third_party/rust/hmac/v0_10:lib", - "//brave/third_party/rust/http/v0_1:lib", + "//brave/third_party/rust/http/v1:lib", "//brave/third_party/rust/rand/v0_7:lib", "//brave/third_party/rust/sha2/v0_9:lib", "//brave/third_party/rust/tracing/v0_1:lib", diff --git a/components/skus/browser/rs/lib/Cargo.toml b/components/skus/browser/rs/lib/Cargo.toml index 3cb9eadcdf95..95dc17673e17 100644 --- a/components/skus/browser/rs/lib/Cargo.toml +++ b/components/skus/browser/rs/lib/Cargo.toml @@ -12,7 +12,7 @@ path = "tests/e2e.rs" required-features = ["e2e_test"] [dependencies] -http = { version = "0.1" } +http = { version = "1" } async-trait = "0.1.64" rand = { version = "0.7" } serde_json = "1.0" diff --git a/components/skus/browser/rs/lib/src/sdk/credentials/fetch.rs b/components/skus/browser/rs/lib/src/sdk/credentials/fetch.rs index b31301f08a8c..f24ee223600f 100644 --- a/components/skus/browser/rs/lib/src/sdk/credentials/fetch.rs +++ b/components/skus/browser/rs/lib/src/sdk/credentials/fetch.rs @@ -336,9 +336,7 @@ where let request_with_retries = FutureRetry::new( || async move { - let mut builder = http::Request::builder(); - builder.method("GET"); - builder.uri(format!( + let builder = http::Request::builder().method("GET").uri(format!( "{}/v1/orders/{}/credentials/items/{}/batches/{}", self.base_url, order_id, item_id, request_id )); diff --git a/components/skus/browser/rs/lib/src/sdk/orders.rs b/components/skus/browser/rs/lib/src/sdk/orders.rs index c65bcb253d77..758913663b2a 100644 --- a/components/skus/browser/rs/lib/src/sdk/orders.rs +++ b/components/skus/browser/rs/lib/src/sdk/orders.rs @@ -210,9 +210,9 @@ where pub async fn fetch_order(&self, order_id: &str) -> Result { let request_with_retries = FutureRetry::new( || async { - let mut builder = http::Request::builder(); - builder.method("GET"); - builder.uri(format!("{}/v1/orders/{}", self.base_url, order_id)); + let builder = http::Request::builder() + .method("GET") + .uri(format!("{}/v1/orders/{}", self.base_url, order_id)); let req = builder.body(vec![]).unwrap(); let resp = self.fetch(req).await?; @@ -239,9 +239,9 @@ where event!(Level::DEBUG, order_id = order_id, "submit_receipt called"); let request_with_retries = FutureRetry::new( || async { - let mut builder = http::Request::builder(); - builder.method("POST"); - builder.uri(format!("{}/v1/orders/{}/submit-receipt", self.base_url, order_id)); + let builder = http::Request::builder() + .method("POST") + .uri(format!("{}/v1/orders/{}/submit-receipt", self.base_url, order_id)); let receipt_bytes = receipt.as_bytes().to_vec(); let req = From dd8ec0d9c977d0ca55aa23c8df9d2e7a792940d5 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Tue, 12 Dec 2023 16:14:18 -0800 Subject: [PATCH 4/8] Remove obsolete rust crate http v0.1 source This is no longer needed now that `skus` has been ported to v1.0. --- third_party/rust/http/v0_1/BUILD.gn | 66 - third_party/rust/http/v0_1/README.chromium | 8 - .../rust/http/v0_1/crate/.cargo_vcs_info.json | 5 - third_party/rust/http/v0_1/crate/.gitignore | 2 - third_party/rust/http/v0_1/crate/.travis.yml | 26 - third_party/rust/http/v0_1/crate/CHANGELOG.md | 127 - third_party/rust/http/v0_1/crate/Cargo.toml | 67 - .../rust/http/v0_1/crate/Cargo.toml.orig | 51 - .../rust/http/v0_1/crate/LICENSE-APACHE | 201 - third_party/rust/http/v0_1/crate/LICENSE-MIT | 25 - third_party/rust/http/v0_1/crate/README.md | 80 - .../v0_1/crate/benches/header_map/basic.rs | 586 --- .../http/v0_1/crate/benches/header_map/mod.rs | 10 - .../v0_1/crate/benches/header_map/vec_map.rs | 103 - .../http/v0_1/crate/benches/header_name.rs | 157 - .../http/v0_1/crate/benches/header_value.rs | 54 - .../rust/http/v0_1/crate/benches/uri.rs | 33 - .../rust/http/v0_1/crate/src/byte_str.rs | 61 - .../rust/http/v0_1/crate/src/convert.rs | 76 - third_party/rust/http/v0_1/crate/src/error.rs | 203 - .../rust/http/v0_1/crate/src/extensions.rs | 209 - .../rust/http/v0_1/crate/src/header/map.rs | 3443 ----------------- .../rust/http/v0_1/crate/src/header/mod.rs | 196 - .../rust/http/v0_1/crate/src/header/name.rs | 2353 ----------- .../rust/http/v0_1/crate/src/header/value.rs | 812 ---- third_party/rust/http/v0_1/crate/src/lib.rs | 213 - .../rust/http/v0_1/crate/src/method.rs | 439 --- .../rust/http/v0_1/crate/src/request.rs | 1069 ----- .../rust/http/v0_1/crate/src/response.rs | 787 ---- .../rust/http/v0_1/crate/src/status.rs | 570 --- .../rust/http/v0_1/crate/src/uri/authority.rs | 639 --- .../rust/http/v0_1/crate/src/uri/builder.rs | 156 - .../rust/http/v0_1/crate/src/uri/mod.rs | 1138 ------ .../rust/http/v0_1/crate/src/uri/path.rs | 552 --- .../rust/http/v0_1/crate/src/uri/port.rs | 158 - .../rust/http/v0_1/crate/src/uri/scheme.rs | 389 -- .../rust/http/v0_1/crate/src/uri/tests.rs | 520 --- .../rust/http/v0_1/crate/src/version.rs | 68 - .../rust/http/v0_1/crate/tests/header_map.rs | 379 -- .../http/v0_1/crate/tests/header_map_fuzz.rs | 365 -- .../rust/http/v0_1/crate/tests/status_code.rs | 67 - 41 files changed, 16463 deletions(-) delete mode 100644 third_party/rust/http/v0_1/BUILD.gn delete mode 100644 third_party/rust/http/v0_1/README.chromium delete mode 100644 third_party/rust/http/v0_1/crate/.cargo_vcs_info.json delete mode 100644 third_party/rust/http/v0_1/crate/.gitignore delete mode 100644 third_party/rust/http/v0_1/crate/.travis.yml delete mode 100644 third_party/rust/http/v0_1/crate/CHANGELOG.md delete mode 100644 third_party/rust/http/v0_1/crate/Cargo.toml delete mode 100644 third_party/rust/http/v0_1/crate/Cargo.toml.orig delete mode 100644 third_party/rust/http/v0_1/crate/LICENSE-APACHE delete mode 100644 third_party/rust/http/v0_1/crate/LICENSE-MIT delete mode 100644 third_party/rust/http/v0_1/crate/README.md delete mode 100644 third_party/rust/http/v0_1/crate/benches/header_map/basic.rs delete mode 100644 third_party/rust/http/v0_1/crate/benches/header_map/mod.rs delete mode 100644 third_party/rust/http/v0_1/crate/benches/header_map/vec_map.rs delete mode 100644 third_party/rust/http/v0_1/crate/benches/header_name.rs delete mode 100644 third_party/rust/http/v0_1/crate/benches/header_value.rs delete mode 100644 third_party/rust/http/v0_1/crate/benches/uri.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/byte_str.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/convert.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/error.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/extensions.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/header/map.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/header/mod.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/header/name.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/header/value.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/lib.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/method.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/request.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/response.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/status.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/authority.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/builder.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/mod.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/path.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/port.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/scheme.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/uri/tests.rs delete mode 100644 third_party/rust/http/v0_1/crate/src/version.rs delete mode 100644 third_party/rust/http/v0_1/crate/tests/header_map.rs delete mode 100644 third_party/rust/http/v0_1/crate/tests/header_map_fuzz.rs delete mode 100644 third_party/rust/http/v0_1/crate/tests/status_code.rs diff --git a/third_party/rust/http/v0_1/BUILD.gn b/third_party/rust/http/v0_1/BUILD.gn deleted file mode 100644 index caf8f5f49887..000000000000 --- a/third_party/rust/http/v0_1/BUILD.gn +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright 2023 The Chromium Authors -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import("//build/rust/cargo_crate.gni") - -cargo_crate("lib") { - crate_name = "http" - epoch = "0.1" - crate_type = "rlib" - crate_root = "crate/src/lib.rs" - sources = [ - "//brave/third_party/rust/http/v0_1/crate/benches/header_map/basic.rs", - "//brave/third_party/rust/http/v0_1/crate/benches/header_map/mod.rs", - "//brave/third_party/rust/http/v0_1/crate/benches/header_map/vec_map.rs", - "//brave/third_party/rust/http/v0_1/crate/benches/header_name.rs", - "//brave/third_party/rust/http/v0_1/crate/benches/header_value.rs", - "//brave/third_party/rust/http/v0_1/crate/benches/uri.rs", - "//brave/third_party/rust/http/v0_1/crate/src/byte_str.rs", - "//brave/third_party/rust/http/v0_1/crate/src/convert.rs", - "//brave/third_party/rust/http/v0_1/crate/src/error.rs", - "//brave/third_party/rust/http/v0_1/crate/src/extensions.rs", - "//brave/third_party/rust/http/v0_1/crate/src/header/map.rs", - "//brave/third_party/rust/http/v0_1/crate/src/header/mod.rs", - "//brave/third_party/rust/http/v0_1/crate/src/header/name.rs", - "//brave/third_party/rust/http/v0_1/crate/src/header/value.rs", - "//brave/third_party/rust/http/v0_1/crate/src/lib.rs", - "//brave/third_party/rust/http/v0_1/crate/src/method.rs", - "//brave/third_party/rust/http/v0_1/crate/src/request.rs", - "//brave/third_party/rust/http/v0_1/crate/src/response.rs", - "//brave/third_party/rust/http/v0_1/crate/src/status.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/authority.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/builder.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/mod.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/path.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/port.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/scheme.rs", - "//brave/third_party/rust/http/v0_1/crate/src/uri/tests.rs", - "//brave/third_party/rust/http/v0_1/crate/src/version.rs", - "//brave/third_party/rust/http/v0_1/crate/tests/header_map.rs", - "//brave/third_party/rust/http/v0_1/crate/tests/header_map_fuzz.rs", - "//brave/third_party/rust/http/v0_1/crate/tests/status_code.rs", - ] - inputs = [ - "//brave/third_party/rust/http/v0_1/crate/CHANGELOG.md", - "//brave/third_party/rust/http/v0_1/crate/README.md", - ] - - # Unit tests skipped. Generate with --with-tests to include them. - build_native_rust_unit_tests = false - edition = "2015" - cargo_pkg_version = "0.1.21" - cargo_pkg_authors = "Alex Crichton , Carl Lerche , Sean McArthur " - cargo_pkg_name = "http" - cargo_pkg_description = - "A set of types for representing HTTP requests and responses." - library_configs -= [ "//build/config/compiler:chromium_code" ] - library_configs += [ "//build/config/compiler:no_chromium_code" ] - executable_configs -= [ "//build/config/compiler:chromium_code" ] - executable_configs += [ "//build/config/compiler:no_chromium_code" ] - deps = [ - "//brave/third_party/rust/bytes/v0_4:lib", - "//brave/third_party/rust/fnv/v1:lib", - "//brave/third_party/rust/itoa/v0_4:lib", - ] -} diff --git a/third_party/rust/http/v0_1/README.chromium b/third_party/rust/http/v0_1/README.chromium deleted file mode 100644 index c413552fd5dd..000000000000 --- a/third_party/rust/http/v0_1/README.chromium +++ /dev/null @@ -1,8 +0,0 @@ -Name: http -URL: https://crates.io/crates/http -Description: A set of types for representing HTTP requests and responses. - -Version: 0.1.21 -Security Critical: yes -License: Apache 2.0 -Revision: 6443bdb3079f80cf74a7eec3474a18daf797e5ad diff --git a/third_party/rust/http/v0_1/crate/.cargo_vcs_info.json b/third_party/rust/http/v0_1/crate/.cargo_vcs_info.json deleted file mode 100644 index d34600c7d79b..000000000000 --- a/third_party/rust/http/v0_1/crate/.cargo_vcs_info.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "git": { - "sha1": "6443bdb3079f80cf74a7eec3474a18daf797e5ad" - } -} diff --git a/third_party/rust/http/v0_1/crate/.gitignore b/third_party/rust/http/v0_1/crate/.gitignore deleted file mode 100644 index a9d37c560c6a..000000000000 --- a/third_party/rust/http/v0_1/crate/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target -Cargo.lock diff --git a/third_party/rust/http/v0_1/crate/.travis.yml b/third_party/rust/http/v0_1/crate/.travis.yml deleted file mode 100644 index 053ba1f5968e..000000000000 --- a/third_party/rust/http/v0_1/crate/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -language: rust -sudo: false - -cache: cargo - -matrix: - include: - - rust: stable - - rust: beta - - rust: nightly - # ensure wasm always builds - - rust: stable - script: - - rustup target add wasm32-unknown-unknown - - cargo build --target=wasm32-unknown-unknown - # minimum rustc version - - rust: 1.20.0 - script: cargo build - -script: - - cargo test - - 'if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then cargo test --benches; fi' - -notifications: - email: - on_success: never diff --git a/third_party/rust/http/v0_1/crate/CHANGELOG.md b/third_party/rust/http/v0_1/crate/CHANGELOG.md deleted file mode 100644 index 4166f0df888c..000000000000 --- a/third_party/rust/http/v0_1/crate/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# 0.1.21 (December 2, 2019) - -* Fix `Method::is_idempotent` returning `false` for `PUT` and `DELETE. - -# 0.1.20 (November 26, 2019) - -* Fix possible double-free if `header::Drain` iterator is `std::mem::forgot`en (#357). -* Fix possible data race if multiple `header::ValueDrain`s are iterated on different threads (#362). -* Fix `HeaderMap::reserve` capacity overflows (#360). -* Fix parsing long authority-form `Uri`s (#351). - -# 0.1.19 (October 15, 2019) - -* Allow `%` in IPv6 addresses in `Uri` (#343). - -# 0.1.18 (July 26, 2019) - -* Fix compilation of `HeaderName` parsing on WASM targets (#324). -* Implement `HttpTryFrom` for `HeaderMap` (#326). -* Export `http::header::HeaderValue` as `http::HeaderValue`. - -# 0.1.17 (April 5, 2019) - -* Add `Error::inner_ref()` to view the kind of error (#303) -* Add `headers_ref()` and `headers_mut()` methods to `request::Builder` and `response::Builder` (#293) - -# 0.1.16 (February 19, 2019) - -* Fix `Uri` to permit more characters in the `path` (#296) - -# 0.1.15 (January 22, 2019) - -* Fix `Uri::host()` to include brackets of IPv6 literals (#292) -* Add `scheme_str` and `port_u16` methods to `Uri` (#287) -* Add `method_ref`, `uri_ref`, and `headers_ref` to `request::Builder` (#284) - -# 0.1.14 (November 21, 2018) - -* Add `Port` struct (#252, #255, #265) -* Introduce `Uri` builder (#219) -* Empty `Method` no longer considered valid (#262) -* Fix `Uri` equality when terminating question mark is present (#270) -* Allow % character in userinfo (#269) -* Support additional tokens for header names (#271) -* Export `http::headers::{IterMut, ValuesMut}` (#278) - -# 0.1.13 (September 14, 2018) - -* impl `fmt::Display` for `HeaderName` (#249) -* Fix `uri::Authority` parsing when there is no host after an `@` (#248) -* Fix `Uri` parsing to allow more characters in query strings (#247) - -# 0.1.12 (September 7, 2018) - -* Fix `HeaderValue` parsing to allow HTABs (#244) - -# 0.1.11 (September 5, 2018) - -* Add `From<&Self>` for `HeaderValue`, `Method`, and `StatusCode` (#238) -* Add `Uri::from_static` (#240) - -# 0.1.10 (August 8, 2018) - -* impl HttpTryFrom for HeaderValue (#236) - -# 0.1.9 (August 7, 2018) - -* Fix double percent encoding (#233) -* Add additional HttpTryFrom impls (#234) - -# 0.1.8 (July 23, 2018) - -* Add fuller set of `PartialEq` for `Method` (#221) -* Reduce size of `HeaderMap` by using `Box<[Entry]>` instea of `Vec` (#224) -* Reduce size of `Extensions` by storing as `Option>` (#227) -* Implement `Iterator::size_hint` for most iterators in `header` (#226) - -# 0.1.7 (June 22, 2018) - -* Add `From for HeaderValue` for most integer types (#218). -* Add `Uri::into_parts()` inherent method (same as `Parts::from(uri)`) (#214). -* Fix converting `Uri`s in authority-form to `Parts` and then back into `Uri` (#216). -* Fix `Authority` parsing to reject multiple port sections (#215). -* Fix parsing 1 character authority-form `Uri`s into illegal forms (#220). - -# 0.1.6 (June 13, 2018) - -* Add `HeaderName::from_static()` constructor (#195). -* Add `Authority::from_static()` constructor (#186). -* Implement `From` for `HeaderValue` (#184). -* Fix duplicate keys when iterating over `header::Keys` (#201). - -# 0.1.5 (February 28, 2018) - -* Add websocket handshake related header constants (#162). -* Parsing `Authority` with an empty string now returns an error (#164). -* Implement `PartialEq` for `StatusCode` (#153). -* Implement `HttpTryFrom<&Uri>` for `Uri` (#165). -* Implement `FromStr` for `Method` (#167). -* Implement `HttpTryFrom` for `Uri` (#171). -* Add `into_body` fns to `Request` and `Response` (#172). -* Fix `Request::options` (#177). - -# 0.1.4 (January 4, 2018) - -* Add PathAndQuery::from_static (#148). -* Impl PartialOrd / PartialEq for Authority and PathAndQuery (#150). -* Add `map` fn to `Request` and `Response` (#151). - -# 0.1.3 (December 11, 2017) - -* Add `Scheme` associated consts for common protos. - -# 0.1.2 (November 29, 2017) - -* Add Uri accessor for scheme part. -* Fix Uri parsing bug (#134) - -# 0.1.1 (October 9, 2017) - -* Provide Uri accessors for parts (#129) -* Add Request builder helpers. (#123) -* Misc performance improvements (#126) - -# 0.1.0 (September 8, 2017) - -* Initial release. diff --git a/third_party/rust/http/v0_1/crate/Cargo.toml b/third_party/rust/http/v0_1/crate/Cargo.toml deleted file mode 100644 index 813e380c53e2..000000000000 --- a/third_party/rust/http/v0_1/crate/Cargo.toml +++ /dev/null @@ -1,67 +0,0 @@ -# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO -# -# When uploading crates to the registry Cargo will automatically -# "normalize" Cargo.toml files for maximal compatibility -# with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g., crates.io) dependencies -# -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) - -[package] -name = "http" -version = "0.1.21" -authors = ["Alex Crichton ", "Carl Lerche ", "Sean McArthur "] -description = "A set of types for representing HTTP requests and responses.\n" -documentation = "https://docs.rs/http" -readme = "README.md" -keywords = ["http"] -categories = ["web-programming"] -license = "MIT/Apache-2.0" -repository = "https://github.com/hyperium/http" - -[[bench]] -name = "header_map" -path = "benches/header_map/mod.rs" - -[[bench]] -name = "header_name" -path = "benches/header_name.rs" - -[[bench]] -name = "header_value" -path = "benches/header_value.rs" - -[[bench]] -name = "uri" -path = "benches/uri.rs" -[dependencies.bytes] -version = "0.4" - -[dependencies.fnv] -version = "1.0.5" - -[dependencies.itoa] -version = "0.4.1" -[dev-dependencies.doc-comment] -version = "0.3" - -[dev-dependencies.indexmap] -version = "1.0" - -[dev-dependencies.quickcheck] -version = "0.6" - -[dev-dependencies.rand] -version = "0.4" - -[dev-dependencies.seahash] -version = "3.0.5" - -[dev-dependencies.serde] -version = "1.0" - -[dev-dependencies.serde_json] -version = "1.0" diff --git a/third_party/rust/http/v0_1/crate/Cargo.toml.orig b/third_party/rust/http/v0_1/crate/Cargo.toml.orig deleted file mode 100644 index 81416c664214..000000000000 --- a/third_party/rust/http/v0_1/crate/Cargo.toml.orig +++ /dev/null @@ -1,51 +0,0 @@ -[package] -name = "http" -# When releasing to crates.io: -# - Update html_root_url in lib.rs. -# - Update CHANGELOG.md. -# - Create git tag -version = "0.1.21" -readme = "README.md" -documentation = "https://docs.rs/http" -repository = "https://github.com/hyperium/http" -license = "MIT/Apache-2.0" -authors = [ - "Alex Crichton ", - "Carl Lerche ", - "Sean McArthur ", -] -description = """ -A set of types for representing HTTP requests and responses. -""" -keywords = ["http"] -categories = ["web-programming"] - -[dependencies] -bytes = "0.4" -fnv = "1.0.5" -itoa = "0.4.1" - -[dev-dependencies] -indexmap = "1.0" -quickcheck = "0.6" -rand = "0.4" -seahash = "3.0.5" -serde = "1.0" -serde_json = "1.0" -doc-comment = "0.3" - -[[bench]] -name = "header_map" -path = "benches/header_map/mod.rs" - -[[bench]] -name = "header_name" -path = "benches/header_name.rs" - -[[bench]] -name = "header_value" -path = "benches/header_value.rs" - -[[bench]] -name = "uri" -path = "benches/uri.rs" diff --git a/third_party/rust/http/v0_1/crate/LICENSE-APACHE b/third_party/rust/http/v0_1/crate/LICENSE-APACHE deleted file mode 100644 index 80176c2b2399..000000000000 --- a/third_party/rust/http/v0_1/crate/LICENSE-APACHE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - -Copyright 2017 http-rs authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/third_party/rust/http/v0_1/crate/LICENSE-MIT b/third_party/rust/http/v0_1/crate/LICENSE-MIT deleted file mode 100644 index 0cbc55049230..000000000000 --- a/third_party/rust/http/v0_1/crate/LICENSE-MIT +++ /dev/null @@ -1,25 +0,0 @@ -Copyright (c) 2017 http-rs authors - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/http/v0_1/crate/README.md b/third_party/rust/http/v0_1/crate/README.md deleted file mode 100644 index 1669761c32c5..000000000000 --- a/third_party/rust/http/v0_1/crate/README.md +++ /dev/null @@ -1,80 +0,0 @@ -# HTTP - -A general purpose library of common HTTP types - -[![Build Status](https://travis-ci.org/hyperium/http.svg?branch=master)](https://travis-ci.org/hyperium/http) -[![Crates.io](https://img.shields.io/crates/v/http.svg)](https://crates.io/crates/http) -[![Documentation](https://docs.rs/http/badge.svg)][dox] - -More information about this crate can be found in the [crate -documentation][dox]. - -[dox]: https://docs.rs/http - -## Usage - -To use `http`, first add this to your `Cargo.toml`: - -```toml -[dependencies] -http = "0.1" -``` - -Next, add this to your crate: - -```rust -extern crate http; - -use http::{Request, Response}; - -fn main() { - // ... -} -``` - -## Examples - -Create an HTTP request: - -```rust -extern crate http; - -use http::Request; - -fn main() { - let request = Request::builder() - .uri("https://www.rust-lang.org/") - .header("User-Agent", "awesome/1.0") - .body(()) - .unwrap(); -} -``` - -Create an HTTP response: - -```rust -extern crate http; - -use http::{Response, StatusCode}; - -fn main() { - let response = Response::builder() - .status(StatusCode::MOVED_PERMANENTLY) - .header("Location", "https://www.rust-lang.org/install.html") - .body(()) - .unwrap(); -} -``` - -# License - -Licensed under either of - -- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://apache.org/licenses/LICENSE-2.0) -- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) - -# Contribution - -Unless you explicitly state otherwise, any contribution intentionally submitted -for inclusion in the work by you, as defined in the Apache-2.0 license, shall be -dual licensed as above, without any additional terms or conditions. diff --git a/third_party/rust/http/v0_1/crate/benches/header_map/basic.rs b/third_party/rust/http/v0_1/crate/benches/header_map/basic.rs deleted file mode 100644 index 4ce7ed628737..000000000000 --- a/third_party/rust/http/v0_1/crate/benches/header_map/basic.rs +++ /dev/null @@ -1,586 +0,0 @@ -macro_rules! bench { - ($name:ident($map:ident, $b:ident) $body:expr) => { - mod $name { - #[allow(unused_imports)] - use test::{self, Bencher}; - use seahash::SeaHasher; - use fnv::FnvHasher; - use std::hash::BuildHasherDefault; - use http::header::*; - #[allow(unused_imports)] - use super::custom_hdr; - - #[bench] - fn header_map($b: &mut Bencher) { - let $map = || HeaderMap::default(); - $body - } - - #[bench] - fn order_map_fnv($b: &mut Bencher) { - use indexmap::IndexMap; - let $map = || IndexMap::<_, _, BuildHasherDefault>::default(); - $body - } - - #[bench] - fn vec_map($b: &mut Bencher) { - use vec_map::VecMap; - - let $map = || VecMap::with_capacity(0); - $body - } - - #[bench] - fn order_map_seahash($b: &mut Bencher) { - use indexmap::IndexMap; - let $map = || IndexMap::<_, _, BuildHasherDefault>::default(); - $body - } - - /* - #[bench] - fn order_map_siphash($b: &mut Bencher) { - use indexmap::IndexMap; - let $map = || IndexMap::new(); - $body - } - - #[bench] - fn std_map_siphash($b: &mut Bencher) { - use std::collections::HashMap; - let $map = || HashMap::new(); - $body - } - */ - } - }; -} - -bench!(new_insert_get_host(new_map, b) { - b.iter(|| { - let mut h = new_map(); - h.insert(HOST, "hyper.rs"); - test::black_box(h.get(&HOST)); - }) -}); - -bench!(insert_4_std_get_30(new_map, b) { - - b.iter(|| { - let mut h = new_map(); - - for i in 0..4 { - h.insert(super::STD[i].clone(), "foo"); - } - - for i in 0..30 { - test::black_box(h.get(&super::STD[i % 4])); - } - }) -}); - -bench!(insert_6_std_get_6(new_map, b) { - - b.iter(|| { - let mut h = new_map(); - - for i in 0..6 { - h.insert(super::STD[i].clone(), "foo"); - } - - for i in 0..6 { - test::black_box(h.get(&super::STD[i % 4])); - } - }) -}); - -/* -bench!(insert_remove_host(new_map, b) { - let mut h = new_map(); - - b.iter(|| { - test::black_box(h.insert(HOST, "hyper.rs")); - test::black_box(h.remove(&HOST)); - }) -}); - -bench!(insert_insert_host(new_map, b) { - let mut h = new_map(); - - b.iter(|| { - test::black_box(h.insert(HOST, "hyper.rs")); - test::black_box(h.insert(HOST, "hyper.rs")); - }) -}); -*/ - -bench!(get_10_of_20_std(new_map, b) { - let mut h = new_map(); - - for hdr in super::STD[10..30].iter() { - h.insert(hdr.clone(), hdr.as_str().to_string()); - } - - b.iter(|| { - for hdr in &super::STD[10..20] { - test::black_box(h.get(hdr)); - } - }) -}); - -bench!(get_100_std(new_map, b) { - let mut h = new_map(); - - for hdr in super::STD.iter() { - h.insert(hdr.clone(), hdr.as_str().to_string()); - } - - b.iter(|| { - for i in 0..100 { - test::black_box(h.get(&super::STD[i % super::STD.len()])); - } - }) -}); - -bench!(set_8_get_1_std(new_map, b) { - b.iter(|| { - let mut h = new_map(); - - for hdr in &super::STD[0..8] { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&super::STD[0])); - }) -}); - -bench!(set_10_get_1_std(new_map, b) { - b.iter(|| { - let mut h = new_map(); - - for hdr in &super::STD[0..10] { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&super::STD[0])); - }) -}); - -bench!(set_20_get_1_std(new_map, b) { - b.iter(|| { - let mut h = new_map(); - - for hdr in &super::STD[0..20] { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&super::STD[0])); - }) -}); - -bench!(get_10_custom_short(new_map, b) { - let hdrs = custom_hdr(20); - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), hdr.as_str().to_string()); - } - - b.iter(|| { - for hdr in &hdrs[..10] { - test::black_box(h.get(hdr)); - } - }) -}); - -bench!(set_10_get_1_custom_short(new_map, b) { - let hdrs = custom_hdr(10); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - - -bench!(set_10_get_1_custom_med(new_map, b) { - let hdrs = super::med_custom_hdr(10); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(set_10_get_1_custom_long(new_map, b) { - let hdrs = super::long_custom_hdr(10); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(set_10_get_1_custom_very_long(new_map, b) { - let hdrs = super::very_long_custom_hdr(10); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(set_20_get_1_custom_short(new_map, b) { - let hdrs = custom_hdr(20); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(set_20_get_1_custom_med(new_map, b) { - let hdrs = super::med_custom_hdr(20); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(set_20_get_1_custom_long(new_map, b) { - let hdrs = super::long_custom_hdr(20); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(set_20_get_1_custom_very_long(new_map, b) { - let hdrs = super::very_long_custom_hdr(20); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - - test::black_box(h.get(&hdrs[0])); - }) -}); - -bench!(insert_all_std_headers(new_map, b) { - b.iter(|| { - let mut h = new_map(); - - for hdr in super::STD { - test::black_box(h.insert(hdr.clone(), "foo")); - } - }) -}); - -bench!(insert_79_custom_std_headers(new_map, b) { - let hdrs = super::custom_std(79); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - h.insert(hdr.clone(), "foo"); - } - }) -}); - -bench!(insert_100_custom_headers(new_map, b) { - let hdrs = custom_hdr(100); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - test::black_box(h.insert(hdr.clone(), "foo")); - } - }) -}); - -bench!(insert_500_custom_headers(new_map, b) { - let hdrs = custom_hdr(500); - - b.iter(|| { - let mut h = new_map(); - - for hdr in &hdrs { - test::black_box(h.insert(hdr.clone(), "foo")); - } - }) -}); - -bench!(insert_one_15_char_header(new_map, b) { - let hdr: HeaderName = "abcd-abcd-abcde" - .parse().unwrap(); - - b.iter(|| { - let mut h = new_map(); - h.insert(hdr.clone(), "hello"); - test::black_box(h); - }) -}); - -bench!(insert_one_25_char_header(new_map, b) { - let hdr: HeaderName = "abcd-abcd-abcd-abcd-abcde" - .parse().unwrap(); - - b.iter(|| { - let mut h = new_map(); - h.insert(hdr.clone(), "hello"); - test::black_box(h); - }) -}); - -bench!(insert_one_50_char_header(new_map, b) { - let hdr: HeaderName = "abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcde" - .parse().unwrap(); - - b.iter(|| { - let mut h = new_map(); - h.insert(hdr.clone(), "hello"); - test::black_box(h); - }) -}); - -bench!(insert_one_100_char_header(new_map, b) { - let hdr: HeaderName = "abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcdeabcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcd-abcde" - .parse().unwrap(); - - b.iter(|| { - let mut h = new_map(); - h.insert(hdr.clone(), "hello"); - test::black_box(h); - }) -}); - -const HN_HDRS: [(&'static str, &'static str); 11] = [ - ("Date", "Fri, 27 Jan 2017 23:00:00 GMT"), - ("Content-Type", "text/html; charset=utf-8"), - ("Transfer-Encoding", "chunked"), - ("Connection", "keep-alive"), - ("Set-Cookie", "__cfduid=dbdfbbe3822b61cb8750ba37d894022151485558000; expires=Sat, 27-Jan-18 23:00:00 GMT; path=/; domain=.ycombinator.com; HttpOnly"), - ("Vary", "Accept-Encoding"), - ("Cache-Control", "private"), - ("X-Frame-Options", "DENY"), - ("Strict-Transport-Security", "max-age=31556900; includeSubDomains"), - ("Server", "cloudflare-nginx"), - ("CF-RAY", "327fd1809f3c1baf-SEA"), -]; - -bench!(hn_hdrs_set_8_get_many(new_map, b) { - let hdrs: Vec<(HeaderName, &'static str)> = super::HN_HDRS[..8].iter() - .map(|&(name, val)| (name.parse().unwrap(), val)) - .collect(); - - b.iter(|| { - let mut h = new_map(); - - for &(ref name, val) in hdrs.iter() { - h.insert(name.clone(), val); - } - - for _ in 0..15 { - test::black_box(h.get(&CONTENT_LENGTH)); - test::black_box(h.get(&VARY)); - } - }); -}); - -bench!(hn_hdrs_set_8_get_miss(new_map, b) { - let hdrs: Vec<(HeaderName, &'static str)> = super::HN_HDRS[..8].iter() - .map(|&(name, val)| (name.parse().unwrap(), val)) - .collect(); - - let miss: HeaderName = "x-wat".parse().unwrap(); - - b.iter(|| { - let mut h = new_map(); - - for &(ref name, val) in hdrs.iter() { - h.insert(name.clone(), val); - } - - test::black_box(h.get(&CONTENT_LENGTH)); - test::black_box(h.get(&miss)); - }); -}); - -bench!(hn_hdrs_set_11_get_with_miss(new_map, b) { - let hdrs: Vec<(HeaderName, &'static str)> = super::HN_HDRS.iter() - .map(|&(name, val)| (name.parse().unwrap(), val)) - .collect(); - - let miss: HeaderName = "x-wat".parse().unwrap(); - - b.iter(|| { - let mut h = new_map(); - - for &(ref name, val) in hdrs.iter() { - h.insert(name.clone(), val); - } - - for _ in 0..10 { - test::black_box(h.get(&CONTENT_LENGTH)); - test::black_box(h.get(&VARY)); - test::black_box(h.get(&miss)); - } - }); -}); - -use http::header::*; - -fn custom_hdr(n: usize) -> Vec { - (0..n).map(|i| { - let s = format!("x-custom-{}", i); - s.parse().unwrap() - }).collect() -} - -fn med_custom_hdr(n: usize) -> Vec { - (0..n).map(|i| { - let s = format!("content-length-{}", i); - s.parse().unwrap() - }).collect() -} - -fn long_custom_hdr(n: usize) -> Vec { - (0..n).map(|i| { - let s = format!("access-control-allow-headers-{}", i); - s.parse().unwrap() - }).collect() -} - -fn very_long_custom_hdr(n: usize) -> Vec { - (0..n).map(|i| { - let s = format!("access-control-allow-access-control-allow-headers-{}", i); - s.parse().unwrap() - }).collect() -} - -fn custom_std(n: usize) -> Vec { - (0..n).map(|i| { - let s = format!("{}-{}", STD[i % STD.len()].as_str(), i); - s.parse().unwrap() - }).collect() -} - -const STD: &'static [HeaderName] = &[ - ACCEPT, - ACCEPT_CHARSET, - ACCEPT_ENCODING, - ACCEPT_LANGUAGE, - ACCEPT_RANGES, - ACCESS_CONTROL_ALLOW_CREDENTIALS, - ACCESS_CONTROL_ALLOW_HEADERS, - ACCESS_CONTROL_ALLOW_METHODS, - ACCESS_CONTROL_ALLOW_ORIGIN, - ACCESS_CONTROL_EXPOSE_HEADERS, - ACCESS_CONTROL_MAX_AGE, - ACCESS_CONTROL_REQUEST_HEADERS, - ACCESS_CONTROL_REQUEST_METHOD, - AGE, - ALLOW, - ALT_SVC, - AUTHORIZATION, - CACHE_CONTROL, - CONNECTION, - CONTENT_DISPOSITION, - CONTENT_ENCODING, - CONTENT_LANGUAGE, - CONTENT_LENGTH, - CONTENT_LOCATION, - CONTENT_RANGE, - CONTENT_SECURITY_POLICY, - CONTENT_SECURITY_POLICY_REPORT_ONLY, - CONTENT_TYPE, - COOKIE, - DNT, - DATE, - ETAG, - EXPECT, - EXPIRES, - FORWARDED, - FROM, - HOST, - IF_MATCH, - IF_MODIFIED_SINCE, - IF_NONE_MATCH, - IF_RANGE, - IF_UNMODIFIED_SINCE, - LAST_MODIFIED, - LINK, - LOCATION, - MAX_FORWARDS, - ORIGIN, - PRAGMA, - PROXY_AUTHENTICATE, - PROXY_AUTHORIZATION, - PUBLIC_KEY_PINS, - PUBLIC_KEY_PINS_REPORT_ONLY, - RANGE, - REFERER, - REFERRER_POLICY, - REFRESH, - RETRY_AFTER, - SERVER, - SET_COOKIE, - STRICT_TRANSPORT_SECURITY, - TE, - TRAILER, - TRANSFER_ENCODING, - USER_AGENT, - UPGRADE, - UPGRADE_INSECURE_REQUESTS, - VARY, - VIA, - WARNING, - WWW_AUTHENTICATE, - X_CONTENT_TYPE_OPTIONS, - X_DNS_PREFETCH_CONTROL, - X_FRAME_OPTIONS, - X_XSS_PROTECTION, -]; diff --git a/third_party/rust/http/v0_1/crate/benches/header_map/mod.rs b/third_party/rust/http/v0_1/crate/benches/header_map/mod.rs deleted file mode 100644 index 50a97f7a420a..000000000000 --- a/third_party/rust/http/v0_1/crate/benches/header_map/mod.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![feature(test)] - -extern crate http; -extern crate test; -extern crate indexmap; -extern crate seahash; -extern crate fnv; - -mod basic; -mod vec_map; diff --git a/third_party/rust/http/v0_1/crate/benches/header_map/vec_map.rs b/third_party/rust/http/v0_1/crate/benches/header_map/vec_map.rs deleted file mode 100644 index 995e8062334a..000000000000 --- a/third_party/rust/http/v0_1/crate/benches/header_map/vec_map.rs +++ /dev/null @@ -1,103 +0,0 @@ -#![allow(dead_code)] - -#[derive(Clone)] -pub struct VecMap { - vec: Vec<(K, V)>, -} - -impl VecMap { - #[inline] - pub fn with_capacity(cap: usize) -> VecMap { - VecMap { - vec: Vec::with_capacity(cap) - } - } - - #[inline] - pub fn insert(&mut self, key: K, value: V) { - match self.find(&key) { - Some(pos) => self.vec[pos] = (key, value), - None => self.vec.push((key, value)) - } - } - - #[inline] - pub fn entry(&mut self, key: K) -> Entry { - match self.find(&key) { - Some(pos) => Entry::Occupied(OccupiedEntry { - vec: self, - pos: pos, - }), - None => Entry::Vacant(VacantEntry { - vec: self, - key: key, - }) - } - } - - #[inline] - pub fn get + ?Sized>(&self, key: &K2) -> Option<&V> { - self.find(key).map(move |pos| &self.vec[pos].1) - } - - #[inline] - pub fn get_mut + ?Sized>(&mut self, key: &K2) -> Option<&mut V> { - self.find(key).map(move |pos| &mut self.vec[pos].1) - } - - #[inline] - pub fn contains_key + ?Sized>(&self, key: &K2) -> bool { - self.find(key).is_some() - } - - #[inline] - pub fn len(&self) -> usize { self.vec.len() } - - #[inline] - pub fn iter(&self) -> ::std::slice::Iter<(K, V)> { - self.vec.iter() - } - #[inline] - pub fn remove + ?Sized>(&mut self, key: &K2) -> Option { - self.find(key).map(|pos| self.vec.remove(pos)).map(|(_, v)| v) - } - #[inline] - pub fn clear(&mut self) { - self.vec.clear(); - } - - #[inline] - fn find + ?Sized>(&self, key: &K2) -> Option { - self.vec.iter().position(|entry| key == &entry.0) - } -} - -pub enum Entry<'a, K: 'a, V: 'a> { - Vacant(VacantEntry<'a, K, V>), - Occupied(OccupiedEntry<'a, K, V>) -} - -pub struct VacantEntry<'a, K: 'a, V: 'a> { - vec: &'a mut VecMap, - key: K, -} - -impl<'a, K, V> VacantEntry<'a, K, V> { - pub fn insert(self, val: V) -> &'a mut V { - let vec = self.vec; - vec.vec.push((self.key, val)); - let pos = vec.vec.len() - 1; - &mut vec.vec[pos].1 - } -} - -pub struct OccupiedEntry<'a, K: 'a, V: 'a> { - vec: &'a mut VecMap, - pos: usize, -} - -impl<'a, K, V> OccupiedEntry<'a, K, V> { - pub fn into_mut(self) -> &'a mut V { - &mut self.vec.vec[self.pos].1 - } -} diff --git a/third_party/rust/http/v0_1/crate/benches/header_name.rs b/third_party/rust/http/v0_1/crate/benches/header_name.rs deleted file mode 100644 index d65f7d940d2e..000000000000 --- a/third_party/rust/http/v0_1/crate/benches/header_name.rs +++ /dev/null @@ -1,157 +0,0 @@ -#![feature(test)] - -extern crate bytes; -extern crate http; -extern crate test; - -use http::header::HeaderName; -use test::Bencher; - -fn make_all_known_headers() -> Vec> { - // Standard request headers - vec![b"A-IM".to_vec(), - b"Accept".to_vec(), - b"Accept-Charset".to_vec(), - b"Accept-Datetime".to_vec(), - b"Accept-Encoding".to_vec(), - b"Accept-Language".to_vec(), - b"Access-Control-Request-Method".to_vec(), - b"Authorization".to_vec(), - b"Cache-Control".to_vec(), - b"Connection".to_vec(), - b"Permanent".to_vec(), - b"Content-Length".to_vec(), - b"Content-MD5".to_vec(), - b"Content-Type".to_vec(), - b"Cookie".to_vec(), - b"Date".to_vec(), - b"Expect".to_vec(), - b"Forwarded".to_vec(), - b"From".to_vec(), - b"Host".to_vec(), - b"Permanent".to_vec(), - b"HTTP2-Settings".to_vec(), - b"If-Match".to_vec(), - b"If-Modified-Since".to_vec(), - b"If-None-Match".to_vec(), - b"If-Range".to_vec(), - b"If-Unmodified-Since".to_vec(), - b"Max-Forwards".to_vec(), - b"Origin".to_vec(), - b"Pragma".to_vec(), - b"Proxy-Authorization".to_vec(), - b"Range".to_vec(), - b"Referer".to_vec(), - b"TE".to_vec(), - b"User-Agent".to_vec(), - b"Upgrade".to_vec(), - b"Via".to_vec(), - b"Warning".to_vec(), - // common_non_standard - b"Upgrade-Insecure-Requests".to_vec(), - b"Upgrade-Insecure-Requests".to_vec(), - b"X-Requested-With".to_vec(), - b"DNT".to_vec(), - b"X-Forwarded-For".to_vec(), - b"X-Forwarded-Host".to_vec(), - b"X-Forwarded-Proto".to_vec(), - b"Front-End-Https".to_vec(), - b"X-Http-Method-Override".to_vec(), - b"X-ATT-DeviceId".to_vec(), - b"X-Wap-Profile".to_vec(), - b"Proxy-Connection".to_vec(), - b"X-UIDH".to_vec(), - b"X-Csrf-Token".to_vec(), - b"X-Request-ID".to_vec(), - b"X-Correlation-ID".to_vec(), - b"Save-Data".to_vec(), - // standard_response_headers - b"Accept-Patch".to_vec(), - b"Accept-Ranges".to_vec(), - b"Access-Control-Allow-Credentials".to_vec(), - b"Access-Control-Allow-Headers".to_vec(), - b"Access-Control-Allow-Methods".to_vec(), - b"Access-Control-Allow-Origin".to_vec(), - b"Access-Control-Expose-Headers".to_vec(), - b"Access-Control-Max-Age".to_vec(), - b"Age".to_vec(), - b"Allow".to_vec(), - b"Alt-Svc".to_vec(), - b"Cache-Control".to_vec(), - b"Connection".to_vec(), - b"Content-Disposition".to_vec(), - b"Content-Encoding".to_vec(), - b"Content-Language".to_vec(), - b"Content-Length".to_vec(), - b"Content-Location".to_vec(), - b"Content-MD5".to_vec(), - b"Content-Range".to_vec(), - b"Content-Type".to_vec(), - b"Date".to_vec(), - b"Delta-Base".to_vec(), - b"ETag".to_vec(), - b"Expires".to_vec(), - b"IM".to_vec(), - b"Last-Modified".to_vec(), - b"Link".to_vec(), - b"Location".to_vec(), - b"P3P".to_vec(), - b"Permanent".to_vec(), - b"Pragma".to_vec(), - b"Proxy-Authenticate".to_vec(), - b"Public-Key-Pins".to_vec(), - b"Retry-After".to_vec(), - b"Server".to_vec(), - b"Set-Cookie".to_vec(), - b"Strict-Transport-Security".to_vec(), - b"Tk".to_vec(), - b"Trailer".to_vec(), - b"Transfer-Encoding".to_vec(), - b"Upgrade".to_vec(), - b"Vary".to_vec(), - b"Via".to_vec(), - b"Warning".to_vec(), - b"WWW-Authenticate".to_vec(), - b"X-Frame-Options".to_vec(), - // common_non_standard_response - b"Content-Security-Policy".to_vec(), - b"Refresh".to_vec(), - b"Status".to_vec(), - b"Timing-Allow-Origin".to_vec(), - b"X-Content-Duration".to_vec(), - b"X-Content-Security-Policy".to_vec(), - b"X-Content-Type-Options".to_vec(), - b"X-Correlation-ID".to_vec(), - b"X-Powered-By".to_vec(), - b"X-Request-ID".to_vec(), - b"X-UA-Compatible".to_vec(), - b"X-WebKit-CSP".to_vec(), - b"X-XSS-Protection".to_vec(), - ] -} - -#[bench] -fn header_name_easy(b: &mut Bencher) { - let name = b"Content-type"; - b.iter(|| { - HeaderName::from_bytes(&name[..]).unwrap(); - }); -} - -#[bench] -fn header_name_bad(b: &mut Bencher) { - let name = b"bad header name"; - b.iter(|| { - HeaderName::from_bytes(&name[..]).expect_err("Bad header name"); - }); -} - -#[bench] -fn header_name_various(b: &mut Bencher) { - let all_known_headers = make_all_known_headers(); - b.iter(|| { - for name in &all_known_headers{ - HeaderName::from_bytes(name.as_slice()).unwrap(); - } - }); -} diff --git a/third_party/rust/http/v0_1/crate/benches/header_value.rs b/third_party/rust/http/v0_1/crate/benches/header_value.rs deleted file mode 100644 index aea0b20d4b5e..000000000000 --- a/third_party/rust/http/v0_1/crate/benches/header_value.rs +++ /dev/null @@ -1,54 +0,0 @@ -#![feature(test)] - -extern crate bytes; -extern crate http; -extern crate test; - -use bytes::Bytes; -use http::HeaderValue; -use test::Bencher; - -static SHORT: &'static [u8] = b"localhost"; -static LONG: &'static [u8] = b"Mozilla/5.0 (X11; CrOS x86_64 9592.71.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.80 Safari/537.36"; - - -#[bench] -fn from_shared_short(b: &mut Bencher) { - b.bytes = SHORT.len() as u64; - let bytes = Bytes::from_static(SHORT); - b.iter(|| { - HeaderValue::from_shared(bytes.clone()).unwrap(); - }); -} - -#[bench] -fn from_shared_long(b: &mut Bencher) { - b.bytes = LONG.len() as u64; - let bytes = Bytes::from_static(LONG); - b.iter(|| { - HeaderValue::from_shared(bytes.clone()).unwrap(); - }); -} - - -#[bench] -fn from_shared_unchecked_short(b: &mut Bencher) { - b.bytes = SHORT.len() as u64; - let bytes = Bytes::from_static(SHORT); - b.iter(|| { - unsafe { - HeaderValue::from_shared_unchecked(bytes.clone()); - } - }); -} - -#[bench] -fn from_shared_unchecked_long(b: &mut Bencher) { - b.bytes = LONG.len() as u64; - let bytes = Bytes::from_static(LONG); - b.iter(|| { - unsafe { - HeaderValue::from_shared_unchecked(bytes.clone()); - } - }); -} diff --git a/third_party/rust/http/v0_1/crate/benches/uri.rs b/third_party/rust/http/v0_1/crate/benches/uri.rs deleted file mode 100644 index bf2e3f488792..000000000000 --- a/third_party/rust/http/v0_1/crate/benches/uri.rs +++ /dev/null @@ -1,33 +0,0 @@ -#![feature(test)] - -extern crate http; -extern crate test; - -use http::Uri; -use test::Bencher; - -#[bench] -fn uri_parse_slash(b: &mut Bencher) { - b.bytes = 1; - b.iter(|| { - "/".parse::().unwrap(); - }); -} - -#[bench] -fn uri_parse_relative_medium(b: &mut Bencher) { - let s = "/wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg"; - b.bytes = s.len() as u64; - b.iter(|| { - s.parse::().unwrap(); - }); -} - -#[bench] -fn uri_parse_relative_query(b: &mut Bencher) { - let s = "/wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg?foo={bar}|baz%13%11quux"; - b.bytes = s.len() as u64; - b.iter(|| { - s.parse::().unwrap(); - }); -} diff --git a/third_party/rust/http/v0_1/crate/src/byte_str.rs b/third_party/rust/http/v0_1/crate/src/byte_str.rs deleted file mode 100644 index 00d9a2314b76..000000000000 --- a/third_party/rust/http/v0_1/crate/src/byte_str.rs +++ /dev/null @@ -1,61 +0,0 @@ -use bytes::Bytes; - -use std::{ops, str}; - -#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub(crate) struct ByteStr { - bytes: Bytes, -} - -impl ByteStr { - #[inline] - pub fn new() -> ByteStr { - ByteStr { bytes: Bytes::new() } - } - - #[inline] - pub fn from_static(val: &'static str) -> ByteStr { - ByteStr { bytes: Bytes::from_static(val.as_bytes()) } - } - - #[inline] - pub unsafe fn from_utf8_unchecked(bytes: Bytes) -> ByteStr { - if cfg!(debug_assertions) { - match str::from_utf8(&bytes) { - Ok(_) => (), - Err(err) => panic!("ByteStr::from_utf8_unchecked() with invalid bytes; error = {}, bytes = {:?}", err, bytes), - } - } - ByteStr { bytes: bytes } - } -} - -impl ops::Deref for ByteStr { - type Target = str; - - #[inline] - fn deref(&self) -> &str { - let b: &[u8] = self.bytes.as_ref(); - unsafe { str::from_utf8_unchecked(b) } - } -} - -impl From for ByteStr { - #[inline] - fn from(src: String) -> ByteStr { - ByteStr { bytes: Bytes::from(src) } - } -} - -impl<'a> From<&'a str> for ByteStr { - #[inline] - fn from(src: &'a str) -> ByteStr { - ByteStr { bytes: Bytes::from(src) } - } -} - -impl From for Bytes { - fn from(src: ByteStr) -> Self { - src.bytes - } -} diff --git a/third_party/rust/http/v0_1/crate/src/convert.rs b/third_party/rust/http/v0_1/crate/src/convert.rs deleted file mode 100644 index 4d937a6c5e0b..000000000000 --- a/third_party/rust/http/v0_1/crate/src/convert.rs +++ /dev/null @@ -1,76 +0,0 @@ -use Error; -use header::{HeaderName, HeaderValue, HeaderMap}; -use method::Method; -use sealed::Sealed; -use status::StatusCode; -use uri::{Scheme, Authority, PathAndQuery, Uri}; - -/// Private trait for the `http` crate to have generic methods with fallible -/// conversions. -/// -/// This trait is similar to the `TryFrom` trait proposed in the standard -/// library, except this is specialized for the `http` crate and isn't intended -/// for general consumption. -/// -/// This trait cannot be implemented types outside of the `http` crate, and is -/// only intended for use as a generic bound on methods in the `http` crate. -pub trait HttpTryFrom: Sized + Sealed { - /// Associated error with the conversion this implementation represents. - type Error: Into; - - #[doc(hidden)] - fn try_from(t: T) -> Result; -} - -pub(crate) trait HttpTryInto: Sized { - fn http_try_into(self) -> Result; -} - -#[doc(hidden)] -impl HttpTryInto for T -where - U: HttpTryFrom, - T: Sized, -{ - fn http_try_into(self) -> Result { - HttpTryFrom::try_from(self) - .map_err(|e: U::Error| e.into()) - } -} - -macro_rules! reflexive { - ($($t:ty,)*) => ($( - impl HttpTryFrom<$t> for $t { - type Error = Error; - - fn try_from(t: Self) -> Result { - Ok(t) - } - } - - impl Sealed for $t {} - )*) -} - -reflexive! { - Uri, - Method, - StatusCode, - HeaderName, - HeaderValue, - Scheme, - Authority, - PathAndQuery, -} - -// HeaderMap can't use reflexive easily due to the generic T - -impl HttpTryFrom> for HeaderMap { - type Error = Error; - - fn try_from(t: Self) -> Result { - Ok(t) - } -} - -impl Sealed for HeaderMap {} diff --git a/third_party/rust/http/v0_1/crate/src/error.rs b/third_party/rust/http/v0_1/crate/src/error.rs deleted file mode 100644 index 641c39842ced..000000000000 --- a/third_party/rust/http/v0_1/crate/src/error.rs +++ /dev/null @@ -1,203 +0,0 @@ -use std::error; -use std::fmt; -use std::result; - -use header; -use method; -use status; -use uri; - -/// A generic "error" for HTTP connections -/// -/// This error type is less specific than the error returned from other -/// functions in this crate, but all other errors can be converted to this -/// error. Consumers of this crate can typically consume and work with this form -/// of error for conversions with the `?` operator. -pub struct Error { - inner: ErrorKind, -} - -/// A `Result` typedef to use with the `http::Error` type -pub type Result = result::Result; - -enum ErrorKind { - StatusCode(status::InvalidStatusCode), - Method(method::InvalidMethod), - Uri(uri::InvalidUri), - UriShared(uri::InvalidUriBytes), - UriParts(uri::InvalidUriParts), - HeaderName(header::InvalidHeaderName), - HeaderNameShared(header::InvalidHeaderNameBytes), - HeaderValue(header::InvalidHeaderValue), - HeaderValueShared(header::InvalidHeaderValueBytes), -} - -impl fmt::Debug for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("http::Error") - // Skip the noise of the ErrorKind enum - .field(&self.get_ref()) - .finish() - } -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(self.get_ref(), f) - } -} - -impl Error { - /// Return true if the underlying error has the same type as T. - pub fn is(&self) -> bool { - self.get_ref().is::() - } - - /// Return a reference to the lower level, inner error. - #[allow(warnings)] - pub fn get_ref(&self) -> &(error::Error + 'static) { - use self::ErrorKind::*; - - match self.inner { - StatusCode(ref e) => e, - Method(ref e) => e, - Uri(ref e) => e, - UriShared(ref e) => e, - UriParts(ref e) => e, - HeaderName(ref e) => e, - HeaderNameShared(ref e) => e, - HeaderValue(ref e) => e, - HeaderValueShared(ref e) => e, - } - } -} - -impl error::Error for Error { - fn description(&self) -> &str { - use self::ErrorKind::*; - - match self.inner { - StatusCode(ref e) => e.description(), - Method(ref e) => e.description(), - Uri(ref e) => e.description(), - UriShared(ref e) => e.description(), - UriParts(ref e) => e.description(), - HeaderName(ref e) => e.description(), - HeaderNameShared(ref e) => e.description(), - HeaderValue(ref e) => e.description(), - HeaderValueShared(ref e) => e.description(), - } - } - - // Return any available cause from the inner error. Note the inner error is - // not itself the cause. - #[allow(warnings)] - fn cause(&self) -> Option<&error::Error> { - self.get_ref().cause() - } -} - -impl From for Error { - fn from(err: status::InvalidStatusCode) -> Error { - Error { inner: ErrorKind::StatusCode(err) } - } -} - -impl From for Error { - fn from(err: method::InvalidMethod) -> Error { - Error { inner: ErrorKind::Method(err) } - } -} - -impl From for Error { - fn from(err: uri::InvalidUri) -> Error { - Error { inner: ErrorKind::Uri(err) } - } -} - -impl From for Error { - fn from(err: uri::InvalidUriBytes) -> Error { - Error { inner: ErrorKind::UriShared(err) } - } -} - -impl From for Error { - fn from(err: uri::InvalidUriParts) -> Error { - Error { inner: ErrorKind::UriParts(err) } - } -} - -impl From for Error { - fn from(err: header::InvalidHeaderName) -> Error { - Error { inner: ErrorKind::HeaderName(err) } - } -} - -impl From for Error { - fn from(err: header::InvalidHeaderNameBytes) -> Error { - Error { inner: ErrorKind::HeaderNameShared(err) } - } -} - -impl From for Error { - fn from(err: header::InvalidHeaderValue) -> Error { - Error { inner: ErrorKind::HeaderValue(err) } - } -} - -impl From for Error { - fn from(err: header::InvalidHeaderValueBytes) -> Error { - Error { inner: ErrorKind::HeaderValueShared(err) } - } -} - -// A crate-private type until we can use !. -// -// Being crate-private, we should be able to swap the type out in a -// backwards compatible way. -pub enum Never {} - -impl From for Error { - fn from(never: Never) -> Error { - match never {} - } -} - -impl fmt::Debug for Never { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - match *self {} - } -} - -impl fmt::Display for Never { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - match *self {} - } -} - -impl error::Error for Never { - fn description(&self) -> &str { - match *self {} - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn inner_error_is_invalid_status_code() { - if let Err(e) = status::StatusCode::from_u16(6666) { - let err: Error = e.into(); - let ie = err.get_ref(); - assert!(!ie.is::()); - assert!( ie.is::()); - ie.downcast_ref::().unwrap(); - - assert!(!err.is::()); - assert!( err.is::()); - } else { - panic!("Bad status allowed!"); - } - } -} diff --git a/third_party/rust/http/v0_1/crate/src/extensions.rs b/third_party/rust/http/v0_1/crate/src/extensions.rs deleted file mode 100644 index e8f928fe1db8..000000000000 --- a/third_party/rust/http/v0_1/crate/src/extensions.rs +++ /dev/null @@ -1,209 +0,0 @@ -use std::any::{Any, TypeId}; -use std::collections::HashMap; -use std::hash::{BuildHasherDefault, Hasher}; -use std::fmt; - -// `Box` is now `Box`, but we can't change yet (minimum Rust) -#[allow(warnings)] -type AnyMap = HashMap, BuildHasherDefault>; - -// With TypeIds as keys, there's no need to hash them. They are already hashes -// themselves, coming from the compiler. The IdHasher just holds the u64 of -// the TypeId, and then returns it, instead of doing any bit fiddling. -#[derive(Default)] -struct IdHasher(u64); - -impl Hasher for IdHasher { - fn write(&mut self, _: &[u8]) { - unreachable!("TypeId calls write_u64"); - } - - #[inline] - fn write_u64(&mut self, id: u64) { - self.0 = id; - } - - #[inline] - fn finish(&self) -> u64 { - self.0 - } -} - - - -/// A type map of protocol extensions. -/// -/// `Extensions` can be used by `Request` and `Response` to store -/// extra data derived from the underlying protocol. -#[derive(Default)] -pub struct Extensions { - // If extensions are never used, no need to carry around an empty HashMap. - // That's 3 words. Instead, this is only 1 word. - map: Option>, -} - -impl Extensions { - /// Create an empty `Extensions`. - #[inline] - pub fn new() -> Extensions { - Extensions { - map: None, - } - } - - /// Insert a type into this `Extensions`. - /// - /// If a extension of this type already existed, it will - /// be returned. - /// - /// # Example - /// - /// ``` - /// # use http::Extensions; - /// let mut ext = Extensions::new(); - /// assert!(ext.insert(5i32).is_none()); - /// assert!(ext.insert(4u8).is_none()); - /// assert_eq!(ext.insert(9i32), Some(5i32)); - /// ``` - pub fn insert(&mut self, val: T) -> Option { - self - .map - .get_or_insert_with(|| Box::new(HashMap::default())) - .insert(TypeId::of::(), Box::new(val)) - .and_then(|boxed| { - #[allow(warnings)] - { - (boxed as Box) - .downcast() - .ok() - .map(|boxed| *boxed) - } - }) - } - - /// Get a reference to a type previously inserted on this `Extensions`. - /// - /// # Example - /// - /// ``` - /// # use http::Extensions; - /// let mut ext = Extensions::new(); - /// assert!(ext.get::().is_none()); - /// ext.insert(5i32); - /// - /// assert_eq!(ext.get::(), Some(&5i32)); - /// ``` - pub fn get(&self) -> Option<&T> { - self - .map - .as_ref() - .and_then(|map| map.get(&TypeId::of::())) - .and_then(|boxed| { - #[allow(warnings)] - { - (&**boxed as &(Any + 'static)).downcast_ref() - } - }) - } - - /// Get a mutable reference to a type previously inserted on this `Extensions`. - /// - /// # Example - /// - /// ``` - /// # use http::Extensions; - /// let mut ext = Extensions::new(); - /// ext.insert(String::from("Hello")); - /// ext.get_mut::().unwrap().push_str(" World"); - /// - /// assert_eq!(ext.get::().unwrap(), "Hello World"); - /// ``` - pub fn get_mut(&mut self) -> Option<&mut T> { - self - .map - .as_mut() - .and_then(|map| map.get_mut(&TypeId::of::())) - .and_then(|boxed| { - #[allow(warnings)] - { - (&mut **boxed as &mut (Any + 'static)).downcast_mut() - } - }) - } - - - /// Remove a type from this `Extensions`. - /// - /// If a extension of this type existed, it will be returned. - /// - /// # Example - /// - /// ``` - /// # use http::Extensions; - /// let mut ext = Extensions::new(); - /// ext.insert(5i32); - /// assert_eq!(ext.remove::(), Some(5i32)); - /// assert!(ext.get::().is_none()); - /// ``` - pub fn remove(&mut self) -> Option { - self - .map - .as_mut() - .and_then(|map| map.remove(&TypeId::of::())) - .and_then(|boxed| { - #[allow(warnings)] - { - (boxed as Box) - .downcast() - .ok() - .map(|boxed| *boxed) - } - }) - } - - /// Clear the `Extensions` of all inserted extensions. - /// - /// # Example - /// - /// ``` - /// # use http::Extensions; - /// let mut ext = Extensions::new(); - /// ext.insert(5i32); - /// ext.clear(); - /// - /// assert!(ext.get::().is_none()); - /// ``` - #[inline] - pub fn clear(&mut self) { - if let Some(ref mut map) = self.map { - map.clear(); - } - } -} - -impl fmt::Debug for Extensions { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Extensions") - .finish() - } -} - -#[test] -fn test_extensions() { - #[derive(Debug, PartialEq)] - struct MyType(i32); - - let mut extensions = Extensions::new(); - - extensions.insert(5i32); - extensions.insert(MyType(10)); - - assert_eq!(extensions.get(), Some(&5i32)); - assert_eq!(extensions.get_mut(), Some(&mut 5i32)); - - assert_eq!(extensions.remove::(), Some(5i32)); - assert!(extensions.get::().is_none()); - - assert_eq!(extensions.get::(), None); - assert_eq!(extensions.get(), Some(&MyType(10))); -} diff --git a/third_party/rust/http/v0_1/crate/src/header/map.rs b/third_party/rust/http/v0_1/crate/src/header/map.rs deleted file mode 100644 index 2bb62b556cbe..000000000000 --- a/third_party/rust/http/v0_1/crate/src/header/map.rs +++ /dev/null @@ -1,3443 +0,0 @@ -use std::{fmt, mem, ops, ptr, vec}; -use std::collections::hash_map::RandomState; -use std::collections::HashMap; -use std::hash::{BuildHasher, Hash, Hasher}; -use std::iter::FromIterator; -use std::marker::PhantomData; - -use convert::{HttpTryFrom, HttpTryInto}; -use Error; - -use super::HeaderValue; -use super::name::{HdrName, HeaderName, InvalidHeaderName}; - -pub use self::as_header_name::AsHeaderName; -pub use self::into_header_name::IntoHeaderName; - -/// A set of HTTP headers -/// -/// `HeaderMap` is an multimap of [`HeaderName`] to values. -/// -/// [`HeaderName`]: struct.HeaderName.html -/// -/// # Examples -/// -/// Basic usage -/// -/// ``` -/// # use http::HeaderMap; -/// # use http::header::{CONTENT_LENGTH, HOST, LOCATION}; -/// let mut headers = HeaderMap::new(); -/// -/// headers.insert(HOST, "example.com".parse().unwrap()); -/// headers.insert(CONTENT_LENGTH, "123".parse().unwrap()); -/// -/// assert!(headers.contains_key(HOST)); -/// assert!(!headers.contains_key(LOCATION)); -/// -/// assert_eq!(headers[HOST], "example.com"); -/// -/// headers.remove(HOST); -/// -/// assert!(!headers.contains_key(HOST)); -/// ``` -#[derive(Clone)] -pub struct HeaderMap { - // Used to mask values to get an index - mask: Size, - indices: Box<[Pos]>, - entries: Vec>, - extra_values: Vec>, - danger: Danger, -} - -// # Implementation notes -// -// Below, you will find a fairly large amount of code. Most of this is to -// provide the necessary functions to efficiently manipulate the header -// multimap. The core hashing table is based on robin hood hashing [1]. While -// this is the same hashing algorithm used as part of Rust's `HashMap` in -// stdlib, many implementation details are different. The two primary reasons -// for this divergence are that `HeaderMap` is a multimap and the structure has -// been optimized to take advantage of the characteristics of HTTP headers. -// -// ## Structure Layout -// -// Most of the data contained by `HeaderMap` is *not* stored in the hash table. -// Instead, pairs of header name and *first* associated header value are stored -// in the `entries` vector. If the header name has more than one associated -// header value, then additional values are stored in `extra_values`. The actual -// hash table (`indices`) only maps hash codes to indices in `entries`. This -// means that, when an eviction happens, the actual header name and value stay -// put and only a tiny amount of memory has to be copied. -// -// Extra values associated with a header name are tracked using a linked list. -// Links are formed with offsets into `extra_values` and not pointers. -// -// [1]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing - -/// `HeaderMap` entry iterator. -/// -/// Yields `(&HeaderName, &value)` tuples. The same header name may be yielded -/// more than once if it has more than one associated value. -#[derive(Debug)] -pub struct Iter<'a, T: 'a> { - inner: IterMut<'a, T>, -} - -/// `HeaderMap` mutable entry iterator -/// -/// Yields `(&HeaderName, &mut value)` tuples. The same header name may be -/// yielded more than once if it has more than one associated value. -#[derive(Debug)] -pub struct IterMut<'a, T: 'a> { - map: *mut HeaderMap, - entry: usize, - cursor: Option, - lt: PhantomData<&'a mut HeaderMap>, -} - -/// An owning iterator over the entries of a `HeaderMap`. -/// -/// This struct is created by the `into_iter` method on `HeaderMap`. -#[derive(Debug)] -pub struct IntoIter { - // If None, pull from `entries` - next: Option, - entries: vec::IntoIter>, - extra_values: Vec>, -} - -/// An iterator over `HeaderMap` keys. -/// -/// Each header name is yielded only once, even if it has more than one -/// associated value. -#[derive(Debug)] -pub struct Keys<'a, T: 'a> { - inner: ::std::slice::Iter<'a, Bucket>, -} - -/// `HeaderMap` value iterator. -/// -/// Each value contained in the `HeaderMap` will be yielded. -#[derive(Debug)] -pub struct Values<'a, T: 'a> { - inner: Iter<'a, T>, -} - -/// `HeaderMap` mutable value iterator -#[derive(Debug)] -pub struct ValuesMut<'a, T: 'a> { - inner: IterMut<'a, T>, -} - -/// A drain iterator for `HeaderMap`. -#[derive(Debug)] -pub struct Drain<'a, T: 'a> { - idx: usize, - len: usize, - entries: *mut [Bucket], - extra_values: *mut Vec>, - lt: PhantomData<&'a mut HeaderMap>, -} - -/// A view to all values stored in a single entry. -/// -/// This struct is returned by `HeaderMap::get_all`. -#[derive(Debug)] -pub struct GetAll<'a, T: 'a> { - map: &'a HeaderMap, - index: Option, -} - -/// A view into a single location in a `HeaderMap`, which may be vacant or occupied. -#[derive(Debug)] -pub enum Entry<'a, T: 'a> { - /// An occupied entry - Occupied(OccupiedEntry<'a, T>), - - /// A vacant entry - Vacant(VacantEntry<'a, T>), -} - -/// A view into a single empty location in a `HeaderMap`. -/// -/// This struct is returned as part of the `Entry` enum. -#[derive(Debug)] -pub struct VacantEntry<'a, T: 'a> { - map: &'a mut HeaderMap, - key: HeaderName, - hash: HashValue, - probe: usize, - danger: bool, -} - -/// A view into a single occupied location in a `HeaderMap`. -/// -/// This struct is returned as part of the `Entry` enum. -#[derive(Debug)] -pub struct OccupiedEntry<'a, T: 'a> { - map: &'a mut HeaderMap, - probe: usize, - index: usize, -} - -/// An iterator of all values associated with a single header name. -#[derive(Debug)] -pub struct ValueIter<'a, T: 'a> { - map: &'a HeaderMap, - index: usize, - front: Option, - back: Option, -} - -/// A mutable iterator of all values associated with a single header name. -#[derive(Debug)] -pub struct ValueIterMut<'a, T: 'a> { - map: *mut HeaderMap, - index: usize, - front: Option, - back: Option, - lt: PhantomData<&'a mut HeaderMap>, -} - -/// An drain iterator of all values associated with a single header name. -#[derive(Debug)] -pub struct ValueDrain<'a, T: 'a> { - first: Option, - next: Option<::std::vec::IntoIter>, - lt: PhantomData<&'a mut HeaderMap>, -} - -/// Tracks the value iterator state -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -enum Cursor { - Head, - Values(usize), -} - -/// Type used for representing the size of a HeaderMap value. -/// -/// 32,768 is more than enough entries for a single header map. Setting this -/// limit enables using `u16` to represent all offsets, which takes 2 bytes -/// instead of 8 on 64 bit processors. -/// -/// Setting this limit is especially benificial for `indices`, making it more -/// cache friendly. More hash codes can fit in a cache line. -/// -/// You may notice that `u16` may represent more than 32,768 values. This is -/// true, but 32,768 should be plenty and it allows us to reserve the top bit -/// for future usage. -type Size = usize; - -/// This limit falls out from above. -const MAX_SIZE: usize = (1 << 15); - -/// An entry in the hash table. This represents the full hash code for an entry -/// as well as the position of the entry in the `entries` vector. -#[derive(Copy, Clone)] -struct Pos { - // Index in the `entries` vec - index: Size, - // Full hash value for the entry. - hash: HashValue, -} - -/// Hash values are limited to u16 as well. While `fast_hash` and `Hasher` -/// return `usize` hash codes, limiting the effective hash code to the lower 16 -/// bits is fine since we know that the `indices` vector will never grow beyond -/// that size. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -struct HashValue(usize); - -/// Stores the data associated with a `HeaderMap` entry. Only the first value is -/// included in this struct. If a header name has more than one associated -/// value, all extra values are stored in the `extra_values` vector. A doubly -/// linked list of entries is maintained. The doubly linked list is used so that -/// removing a value is constant time. This also has the nice property of -/// enabling double ended iteration. -#[derive(Debug, Clone)] -struct Bucket { - hash: HashValue, - key: HeaderName, - value: T, - links: Option, -} - -/// The head and tail of the value linked list. -#[derive(Debug, Copy, Clone)] -struct Links { - next: usize, - tail: usize, -} - -/// Access to the `links` value in a slice of buckets. -/// -/// It's important that no other field is accessed, since it may have been -/// freed in a `Drain` iterator. -#[derive(Debug)] -struct RawLinks(*mut [Bucket]); - -/// Node in doubly-linked list of header value entries -#[derive(Debug, Clone)] -struct ExtraValue { - value: T, - prev: Link, - next: Link, -} - -/// A header value node is either linked to another node in the `extra_values` -/// list or it points to an entry in `entries`. The entry in `entries` is the -/// start of the list and holds the associated header name. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -enum Link { - Entry(usize), - Extra(usize), -} - -/// Tracks the header map danger level! This relates to the adaptive hashing -/// algorithm. A HeaderMap starts in the "green" state, when a large number of -/// collisions are detected, it transitions to the yellow state. At this point, -/// the header map will either grow and switch back to the green state OR it -/// will transition to the red state. -/// -/// When in the red state, a safe hashing algorithm is used and all values in -/// the header map have to be rehashed. -#[derive(Clone)] -enum Danger { - Green, - Yellow, - Red(RandomState), -} - -// Constants related to detecting DOS attacks. -// -// Displacement is the number of entries that get shifted when inserting a new -// value. Forward shift is how far the entry gets stored from the ideal -// position. -// -// The current constant values were picked from another implementation. It could -// be that there are different values better suited to the header map case. -const DISPLACEMENT_THRESHOLD: usize = 128; -const FORWARD_SHIFT_THRESHOLD: usize = 512; - -// The default strategy for handling the yellow danger state is to increase the -// header map capacity in order to (hopefully) reduce the number of collisions. -// If growing the hash map would cause the load factor to drop bellow this -// threshold, then instead of growing, the headermap is switched to the red -// danger state and safe hashing is used instead. -const LOAD_FACTOR_THRESHOLD: f32 = 0.2; - -// Macro used to iterate the hash table starting at a given point, looping when -// the end is hit. -macro_rules! probe_loop { - ($label:tt: $probe_var: ident < $len: expr, $body: expr) => { - debug_assert!($len > 0); - $label: - loop { - if $probe_var < $len { - $body - $probe_var += 1; - } else { - $probe_var = 0; - } - } - }; - ($probe_var: ident < $len: expr, $body: expr) => { - debug_assert!($len > 0); - loop { - if $probe_var < $len { - $body - $probe_var += 1; - } else { - $probe_var = 0; - } - } - }; -} - -// First part of the robinhood algorithm. Given a key, find the slot in which it -// will be inserted. This is done by starting at the "ideal" spot. Then scanning -// until the destination slot is found. A destination slot is either the next -// empty slot or the next slot that is occupied by an entry that has a lower -// displacement (displacement is the distance from the ideal spot). -// -// This is implemented as a macro instead of a function that takes a closure in -// order to guarantee that it is "inlined". There is no way to annotate closures -// to guarantee inlining. -macro_rules! insert_phase_one { - ($map:ident, - $key:expr, - $probe:ident, - $pos:ident, - $hash:ident, - $danger:ident, - $vacant:expr, - $occupied:expr, - $robinhood:expr) => - {{ - let $hash = hash_elem_using(&$map.danger, &$key); - let mut $probe = desired_pos($map.mask, $hash); - let mut dist = 0; - let ret; - - // Start at the ideal position, checking all slots - probe_loop!('probe: $probe < $map.indices.len(), { - if let Some(($pos, entry_hash)) = $map.indices[$probe].resolve() { - // The slot is already occupied, but check if it has a lower - // displacement. - let their_dist = probe_distance($map.mask, entry_hash, $probe); - - if their_dist < dist { - // The new key's distance is larger, so claim this spot and - // displace the current entry. - // - // Check if this insertion is above the danger threshold. - let $danger = - dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red(); - - ret = $robinhood; - break 'probe; - } else if entry_hash == $hash && $map.entries[$pos].key == $key { - // There already is an entry with the same key. - ret = $occupied; - break 'probe; - } - } else { - // The entry is vacant, use it for this key. - let $danger = - dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red(); - - ret = $vacant; - break 'probe; - } - - dist += 1; - }); - - ret - }} -} - -// ===== impl HeaderMap ===== - -impl HeaderMap { - /// Create an empty `HeaderMap`. - /// - /// The map will be created without any capacity. This function will not - /// allocate. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// let map = HeaderMap::new(); - /// - /// assert!(map.is_empty()); - /// assert_eq!(0, map.capacity()); - /// ``` - pub fn new() -> Self { - HeaderMap::with_capacity(0) - } -} - -impl HeaderMap { - /// Create an empty `HeaderMap` with the specified capacity. - /// - /// The returned map will allocate internal storage in order to hold about - /// `capacity` elements without reallocating. However, this is a "best - /// effort" as there are usage patterns that could cause additional - /// allocations before `capacity` headers are stored in the map. - /// - /// More capacity than requested may be allocated. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// let map: HeaderMap = HeaderMap::with_capacity(10); - /// - /// assert!(map.is_empty()); - /// assert_eq!(12, map.capacity()); - /// ``` - pub fn with_capacity(capacity: usize) -> HeaderMap { - assert!(capacity <= MAX_SIZE, "requested capacity too large"); - - if capacity == 0 { - HeaderMap { - mask: 0, - indices: Box::new([]), // as a ZST, this doesn't actually allocate anything - entries: Vec::new(), - extra_values: Vec::new(), - danger: Danger::Green, - } - } else { - let raw_cap = to_raw_capacity(capacity).next_power_of_two(); - debug_assert!(raw_cap > 0); - - HeaderMap { - mask: (raw_cap - 1) as Size, - indices: vec![Pos::none(); raw_cap].into_boxed_slice(), - entries: Vec::with_capacity(raw_cap), - extra_values: Vec::new(), - danger: Danger::Green, - } - } - } - - /// Returns the number of headers stored in the map. - /// - /// This number represents the total number of **values** stored in the map. - /// This number can be greater than or equal to the number of **keys** - /// stored given that a single key may have more than one associated value. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{ACCEPT, HOST}; - /// let mut map = HeaderMap::new(); - /// - /// assert_eq!(0, map.len()); - /// - /// map.insert(ACCEPT, "text/plain".parse().unwrap()); - /// map.insert(HOST, "localhost".parse().unwrap()); - /// - /// assert_eq!(2, map.len()); - /// - /// map.append(ACCEPT, "text/html".parse().unwrap()); - /// - /// assert_eq!(3, map.len()); - /// ``` - pub fn len(&self) -> usize { - self.entries.len() + self.extra_values.len() - } - - /// Returns the number of keys stored in the map. - /// - /// This number will be less than or equal to `len()` as each key may have - /// more than one associated value. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{ACCEPT, HOST}; - /// let mut map = HeaderMap::new(); - /// - /// assert_eq!(0, map.keys_len()); - /// - /// map.insert(ACCEPT, "text/plain".parse().unwrap()); - /// map.insert(HOST, "localhost".parse().unwrap()); - /// - /// assert_eq!(2, map.keys_len()); - /// - /// map.insert(ACCEPT, "text/html".parse().unwrap()); - /// - /// assert_eq!(2, map.keys_len()); - /// ``` - pub fn keys_len(&self) -> usize { - self.entries.len() - } - - /// Returns true if the map contains no elements. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// - /// assert!(map.is_empty()); - /// - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// - /// assert!(!map.is_empty()); - /// ``` - pub fn is_empty(&self) -> bool { - self.entries.len() == 0 - } - - /// Clears the map, removing all key-value pairs. Keeps the allocated memory - /// for reuse. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// - /// map.clear(); - /// assert!(map.is_empty()); - /// assert!(map.capacity() > 0); - /// ``` - pub fn clear(&mut self) { - self.entries.clear(); - self.extra_values.clear(); - self.danger = Danger::Green; - - for e in self.indices.iter_mut() { - *e = Pos::none(); - } - } - - /// Returns the number of headers the map can hold without reallocating. - /// - /// This number is an approximation as certain usage patterns could cause - /// additional allocations before the returned capacity is filled. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// - /// assert_eq!(0, map.capacity()); - /// - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// assert_eq!(6, map.capacity()); - /// ``` - pub fn capacity(&self) -> usize { - usable_capacity(self.indices.len()) - } - - /// Reserves capacity for at least `additional` more headers to be inserted - /// into the `HeaderMap`. - /// - /// The header map may reserve more space to avoid frequent reallocations. - /// Like with `with_capacity`, this will be a "best effort" to avoid - /// allocations until `additional` more headers are inserted. Certain usage - /// patterns could cause additional allocations before the number is - /// reached. - /// - /// # Panics - /// - /// Panics if the new allocation size overflows `usize`. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// map.reserve(10); - /// # map.insert(HOST, "bar".parse().unwrap()); - /// ``` - pub fn reserve(&mut self, additional: usize) { - // TODO: This can't overflow if done properly... since the max # of - // elements is u16::MAX. - let cap = self.entries.len() - .checked_add(additional) - .expect("reserve overflow"); - - if cap > self.indices.len() { - let cap = cap.next_power_of_two(); - assert!(cap < MAX_SIZE, "header map reserve over max capacity"); - assert!(cap != 0, "header map reserve overflowed"); - - if self.entries.len() == 0 { - self.mask = cap - 1; - self.indices = vec![Pos::none(); cap].into_boxed_slice(); - self.entries = Vec::with_capacity(usable_capacity(cap)); - } else { - self.grow(cap); - } - } - } - - /// Returns a reference to the value associated with the key. - /// - /// If there are multiple values associated with the key, then the first one - /// is returned. Use `get_all` to get all values associated with a given - /// key. Returns `None` if there are no values associated with the key. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// assert!(map.get("host").is_none()); - /// - /// map.insert(HOST, "hello".parse().unwrap()); - /// assert_eq!(map.get(HOST).unwrap(), &"hello"); - /// assert_eq!(map.get("host").unwrap(), &"hello"); - /// - /// map.append(HOST, "world".parse().unwrap()); - /// assert_eq!(map.get("host").unwrap(), &"hello"); - /// ``` - pub fn get(&self, key: K) -> Option<&T> - where K: AsHeaderName - { - self.get2(&key) - } - - fn get2(&self, key: &K) -> Option<&T> - where K: AsHeaderName - { - match key.find(self) { - Some((_, found)) => { - let entry = &self.entries[found]; - Some(&entry.value) - } - None => None, - } - } - - /// Returns a mutable reference to the value associated with the key. - /// - /// If there are multiple values associated with the key, then the first one - /// is returned. Use `entry` to get all values associated with a given - /// key. Returns `None` if there are no values associated with the key. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::default(); - /// map.insert(HOST, "hello".to_string()); - /// map.get_mut("host").unwrap().push_str("-world"); - /// - /// assert_eq!(map.get(HOST).unwrap(), &"hello-world"); - /// ``` - pub fn get_mut(&mut self, key: K) -> Option<&mut T> - where K: AsHeaderName - { - match key.find(self) { - Some((_, found)) => { - let entry = &mut self.entries[found]; - Some(&mut entry.value) - } - None => None, - } - } - - /// Returns a view of all values associated with a key. - /// - /// The returned view does not incur any allocations and allows iterating - /// the values associated with the key. See [`GetAll`] for more details. - /// Returns `None` if there are no values associated with the key. - /// - /// [`GetAll`]: struct.GetAll.html - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// - /// map.insert(HOST, "hello".parse().unwrap()); - /// map.append(HOST, "goodbye".parse().unwrap()); - /// - /// let view = map.get_all("host"); - /// - /// let mut iter = view.iter(); - /// assert_eq!(&"hello", iter.next().unwrap()); - /// assert_eq!(&"goodbye", iter.next().unwrap()); - /// assert!(iter.next().is_none()); - /// ``` - pub fn get_all(&self, key: K) -> GetAll - where K: AsHeaderName - { - GetAll { - map: self, - index: key.find(self).map(|(_, i)| i), - } - } - - /// Returns true if the map contains a value for the specified key. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// assert!(!map.contains_key(HOST)); - /// - /// map.insert(HOST, "world".parse().unwrap()); - /// assert!(map.contains_key("host")); - /// ``` - pub fn contains_key(&self, key: K) -> bool - where K: AsHeaderName - { - key.find(self).is_some() - } - - /// An iterator visiting all key-value pairs. - /// - /// The iteration order is arbitrary, but consistent across platforms for - /// the same crate version. Each key will be yielded once per associated - /// value. So, if a key has 3 associated values, it will be yielded 3 times. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{CONTENT_LENGTH, HOST}; - /// let mut map = HeaderMap::new(); - /// - /// map.insert(HOST, "hello".parse().unwrap()); - /// map.append(HOST, "goodbye".parse().unwrap()); - /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); - /// - /// for (key, value) in map.iter() { - /// println!("{:?}: {:?}", key, value); - /// } - /// ``` - pub fn iter(&self) -> Iter { - Iter { - inner: IterMut { - map: self as *const _ as *mut _, - entry: 0, - cursor: self.entries.first().map(|_| Cursor::Head), - lt: PhantomData, - } - } - } - - /// An iterator visiting all key-value pairs, with mutable value references. - /// - /// The iterator order is arbitrary, but consistent across platforms for the - /// same crate version. Each key will be yielded once per associated value, - /// so if a key has 3 associated values, it will be yielded 3 times. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{CONTENT_LENGTH, HOST}; - /// let mut map = HeaderMap::default(); - /// - /// map.insert(HOST, "hello".to_string()); - /// map.append(HOST, "goodbye".to_string()); - /// map.insert(CONTENT_LENGTH, "123".to_string()); - /// - /// for (key, value) in map.iter_mut() { - /// value.push_str("-boop"); - /// } - /// ``` - pub fn iter_mut(&mut self) -> IterMut { - IterMut { - map: self as *mut _, - entry: 0, - cursor: self.entries.first().map(|_| Cursor::Head), - lt: PhantomData, - } - } - - /// An iterator visiting all keys. - /// - /// The iteration order is arbitrary, but consistent across platforms for - /// the same crate version. Each key will be yielded only once even if it - /// has multiple associated values. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{CONTENT_LENGTH, HOST}; - /// let mut map = HeaderMap::new(); - /// - /// map.insert(HOST, "hello".parse().unwrap()); - /// map.append(HOST, "goodbye".parse().unwrap()); - /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); - /// - /// for key in map.keys() { - /// println!("{:?}", key); - /// } - /// ``` - pub fn keys(&self) -> Keys { - Keys { inner: self.entries.iter() } - } - - /// An iterator visiting all values. - /// - /// The iteration order is arbitrary, but consistent across platforms for - /// the same crate version. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{CONTENT_LENGTH, HOST}; - /// let mut map = HeaderMap::new(); - /// - /// map.insert(HOST, "hello".parse().unwrap()); - /// map.append(HOST, "goodbye".parse().unwrap()); - /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); - /// - /// for value in map.values() { - /// println!("{:?}", value); - /// } - /// ``` - pub fn values(&self) -> Values { - Values { inner: self.iter() } - } - - /// An iterator visiting all values mutably. - /// - /// The iteration order is arbitrary, but consistent across platforms for - /// the same crate version. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{CONTENT_LENGTH, HOST}; - /// let mut map = HeaderMap::default(); - /// - /// map.insert(HOST, "hello".to_string()); - /// map.append(HOST, "goodbye".to_string()); - /// map.insert(CONTENT_LENGTH, "123".to_string()); - /// - /// for value in map.values_mut() { - /// value.push_str("-boop"); - /// } - /// ``` - pub fn values_mut(&mut self) -> ValuesMut { - ValuesMut { inner: self.iter_mut() } - } - - /// Clears the map, returning all entries as an iterator. - /// - /// The internal memory is kept for reuse. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::{CONTENT_LENGTH, HOST}; - /// let mut map = HeaderMap::new(); - /// - /// map.insert(HOST, "hello".parse().unwrap()); - /// map.append(HOST, "goodbye".parse().unwrap()); - /// map.insert(CONTENT_LENGTH, "123".parse().unwrap()); - /// - /// let mut drain = map.drain(); - /// - /// let (key, mut vals) = drain.next().unwrap(); - /// - /// assert_eq!("host", key); - /// assert_eq!("hello", vals.next().unwrap()); - /// assert_eq!("goodbye", vals.next().unwrap()); - /// assert!(vals.next().is_none()); - /// - /// let (key, mut vals) = drain.next().unwrap(); - /// - /// assert_eq!("content-length", key); - /// assert_eq!("123", vals.next().unwrap()); - /// assert!(vals.next().is_none()); - /// ``` - pub fn drain(&mut self) -> Drain { - for i in self.indices.iter_mut() { - *i = Pos::none(); - } - - // Memory safety - // - // When the Drain is first created, it shortens the length of - // the source vector to make sure no uninitialized or moved-from - // elements are accessible at all if the Drain's destructor never - // gets to run. - - let entries = &mut self.entries[..] as *mut _; - let extra_values = &mut self.extra_values as *mut _; - let len = self.entries.len(); - unsafe { self.entries.set_len(0); } - - Drain { - idx: 0, - len, - entries, - extra_values, - lt: PhantomData, - } - } - - fn value_iter(&self, idx: Option) -> ValueIter { - use self::Cursor::*; - - if let Some(idx) = idx { - let back = { - let entry = &self.entries[idx]; - - entry.links - .map(|l| Values(l.tail)) - .unwrap_or(Head) - }; - - ValueIter { - map: self, - index: idx, - front: Some(Head), - back: Some(back), - } - } else { - ValueIter { - map: self, - index: ::std::usize::MAX, - front: None, - back: None, - } - } - } - - fn value_iter_mut(&mut self, idx: usize) -> ValueIterMut { - use self::Cursor::*; - - let back = { - let entry = &self.entries[idx]; - - entry.links - .map(|l| Values(l.tail)) - .unwrap_or(Head) - }; - - ValueIterMut { - map: self as *mut _, - index: idx, - front: Some(Head), - back: Some(back), - lt: PhantomData, - } - } - - /// Gets the given key's corresponding entry in the map for in-place - /// manipulation. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// let mut map: HeaderMap = HeaderMap::default(); - /// - /// let headers = &[ - /// "content-length", - /// "x-hello", - /// "Content-Length", - /// "x-world", - /// ]; - /// - /// for &header in headers { - /// let counter = map.entry(header).unwrap().or_insert(0); - /// *counter += 1; - /// } - /// - /// assert_eq!(map["content-length"], 2); - /// assert_eq!(map["x-hello"], 1); - /// ``` - pub fn entry(&mut self, key: K) -> Result, InvalidHeaderName> - where K: AsHeaderName, - { - key.entry(self) - } - - fn entry2(&mut self, key: K) -> Entry - where K: Hash + Into, - HeaderName: PartialEq, - { - // Ensure that there is space in the map - self.reserve_one(); - - insert_phase_one!( - self, - key, - probe, - pos, - hash, - danger, - Entry::Vacant(VacantEntry { - map: self, - hash: hash, - key: key.into(), - probe: probe, - danger: danger, - }), - Entry::Occupied(OccupiedEntry { - map: self, - index: pos, - probe: probe, - }), - Entry::Vacant(VacantEntry { - map: self, - hash: hash, - key: key.into(), - probe: probe, - danger: danger, - })) - } - - /// Inserts a key-value pair into the map. - /// - /// If the map did not previously have this key present, then `None` is - /// returned. - /// - /// If the map did have this key present, the new value is associated with - /// the key and all previous values are removed. **Note** that only a single - /// one of the previous values is returned. If there are multiple values - /// that have been previously associated with the key, then the first one is - /// returned. See `insert_mult` on `OccupiedEntry` for an API that returns - /// all values. - /// - /// The key is not updated, though; this matters for types that can be `==` - /// without being identical. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none()); - /// assert!(!map.is_empty()); - /// - /// let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap(); - /// assert_eq!("world", prev); - /// ``` - pub fn insert(&mut self, key: K, val: T) -> Option - where K: IntoHeaderName, - { - key.insert(self, val) - } - - #[inline] - fn insert2(&mut self, key: K, value: T) -> Option - where K: Hash + Into, - HeaderName: PartialEq, - { - self.reserve_one(); - - insert_phase_one!( - self, key, probe, pos, hash, danger, - // Vacant - { - drop(danger); // Make lint happy - let index = self.entries.len(); - self.insert_entry(hash, key.into(), value); - self.indices[probe] = Pos::new(index, hash); - None - }, - // Occupied - Some(self.insert_occupied(pos, value)), - // Robinhood - { - self.insert_phase_two( - key.into(), - value, - hash, - probe, - danger); - None - }) - } - - /// Set an occupied bucket to the given value - #[inline] - fn insert_occupied(&mut self, index: usize, value: T) -> T { - if let Some(links) = self.entries[index].links { - self.remove_all_extra_values(links.next); - } - - let entry = &mut self.entries[index]; - mem::replace(&mut entry.value, value) - } - - fn insert_occupied_mult(&mut self, index: usize, value: T) -> ValueDrain { - let old; - let links; - - { - let entry = &mut self.entries[index]; - - old = mem::replace(&mut entry.value, value); - links = entry.links.take(); - } - - let raw_links = self.raw_links(); - let extra_values = &mut self.extra_values; - - let next = links.map(|l| { - drain_all_extra_values(raw_links, extra_values, l.next) - .into_iter() - }); - - ValueDrain { - first: Some(old), - next: next, - lt: PhantomData, - } - } - - /// Inserts a key-value pair into the map. - /// - /// If the map did not previously have this key present, then `false` is - /// returned. - /// - /// If the map did have this key present, the new value is pushed to the end - /// of the list of values currently associated with the key. The key is not - /// updated, though; this matters for types that can be `==` without being - /// identical. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none()); - /// assert!(!map.is_empty()); - /// - /// map.append(HOST, "earth".parse().unwrap()); - /// - /// let values = map.get_all("host"); - /// let mut i = values.iter(); - /// assert_eq!("world", *i.next().unwrap()); - /// assert_eq!("earth", *i.next().unwrap()); - /// ``` - pub fn append(&mut self, key: K, value: T) -> bool - where K: IntoHeaderName, - { - key.append(self, value) - } - - #[inline] - fn append2(&mut self, key: K, value: T) -> bool - where K: Hash + Into, - HeaderName: PartialEq, - { - self.reserve_one(); - - insert_phase_one!( - self, key, probe, pos, hash, danger, - // Vacant - { - drop(danger); - let index = self.entries.len(); - self.insert_entry(hash, key.into(), value); - self.indices[probe] = Pos::new(index, hash); - false - }, - // Occupied - { - append_value(pos, &mut self.entries[pos], &mut self.extra_values, value); - true - }, - // Robinhood - { - self.insert_phase_two( - key.into(), - value, - hash, - probe, - danger); - - false - }) - } - - #[inline] - fn find(&self, key: &K) -> Option<(usize, usize)> - where K: Hash + Into, - HeaderName: PartialEq, - { - if self.entries.is_empty() { - return None; - } - - let hash = hash_elem_using(&self.danger, key); - let mask = self.mask; - let mut probe = desired_pos(mask, hash); - let mut dist = 0; - - probe_loop!(probe < self.indices.len(), { - if let Some((i, entry_hash)) = self.indices[probe].resolve() { - if dist > probe_distance(mask, entry_hash, probe) { - // give up when probe distance is too long - return None; - } else if entry_hash == hash && self.entries[i].key == *key { - return Some((probe, i)); - } - } else { - return None; - } - - dist += 1; - }); - } - - /// phase 2 is post-insert where we forward-shift `Pos` in the indices. - #[inline] - fn insert_phase_two(&mut self, - key: HeaderName, - value: T, - hash: HashValue, - probe: usize, - danger: bool) -> usize - { - // Push the value and get the index - let index = self.entries.len(); - self.insert_entry(hash, key, value); - - let num_displaced = do_insert_phase_two( - &mut self.indices, - probe, - Pos::new(index, hash)); - - if danger || num_displaced >= DISPLACEMENT_THRESHOLD { - // Increase danger level - self.danger.to_yellow(); - } - - index - } - - /// Removes a key from the map, returning the value associated with the key. - /// - /// Returns `None` if the map does not contain the key. If there are - /// multiple values associated with the key, then the first one is returned. - /// See `remove_entry_mult` on `OccupiedEntry` for an API that yields all - /// values. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// - /// let prev = map.remove(HOST).unwrap(); - /// assert_eq!("hello.world", prev); - /// - /// assert!(map.remove(HOST).is_none()); - /// ``` - pub fn remove(&mut self, key: K) -> Option - where K: AsHeaderName - { - match key.find(self) { - Some((probe, idx)) => { - if let Some(links) = self.entries[idx].links { - self.remove_all_extra_values(links.next); - } - - let entry = self.remove_found(probe, idx); - - Some(entry.value) - } - None => None, - } - } - - /// Remove an entry from the map. - #[inline] - fn remove_found(&mut self, probe: usize, found: usize) -> Bucket { - // index `probe` and entry `found` is to be removed - // use swap_remove, but then we need to update the index that points - // to the other entry that has to move - self.indices[probe] = Pos::none(); - let entry = self.entries.swap_remove(found); - - // correct index that points to the entry that had to swap places - if let Some(entry) = self.entries.get(found) { - // was not last element - // examine new element in `found` and find it in indices - let mut probe = desired_pos(self.mask, entry.hash); - - probe_loop!(probe < self.indices.len(), { - if let Some((i, _)) = self.indices[probe].resolve() { - if i >= self.entries.len() { - // found it - self.indices[probe] = Pos::new(found, entry.hash); - break; - } - } - }); - - // Update links - if let Some(links) = entry.links { - self.extra_values[links.next].prev = Link::Entry(found); - self.extra_values[links.tail].next = Link::Entry(found); - } - } - - // backward shift deletion in self.indices - // after probe, shift all non-ideally placed indices backward - if self.entries.len() > 0 { - let mut last_probe = probe; - let mut probe = probe + 1; - - probe_loop!(probe < self.indices.len(), { - if let Some((_, entry_hash)) = self.indices[probe].resolve() { - if probe_distance(self.mask, entry_hash, probe) > 0 { - self.indices[last_probe] = self.indices[probe]; - self.indices[probe] = Pos::none(); - } else { - break; - } - } else { - break; - } - - last_probe = probe; - }); - } - - entry - } - - /// Removes the `ExtraValue` at the given index. - #[inline] - fn remove_extra_value(&mut self, idx: usize) -> ExtraValue { - let raw_links = self.raw_links(); - remove_extra_value(raw_links, &mut self.extra_values, idx) - } - - fn remove_all_extra_values(&mut self, mut head: usize) { - loop { - let extra = self.remove_extra_value(head); - - if let Link::Extra(idx) = extra.next { - head = idx; - } else { - break; - } - } - } - - #[inline] - fn insert_entry(&mut self, hash: HashValue, key: HeaderName, value: T) { - assert!(self.entries.len() < MAX_SIZE, "header map at capacity"); - - self.entries.push(Bucket { - hash: hash, - key: key, - value: value, - links: None, - }); - } - - fn rebuild(&mut self) { - // Loop over all entries and re-insert them into the map - 'outer: - for (index, entry) in self.entries.iter_mut().enumerate() { - let hash = hash_elem_using(&self.danger, &entry.key); - let mut probe = desired_pos(self.mask, hash); - let mut dist = 0; - - // Update the entry's hash code - entry.hash = hash; - - probe_loop!(probe < self.indices.len(), { - if let Some((_, entry_hash)) = self.indices[probe].resolve() { - // if existing element probed less than us, swap - let their_dist = probe_distance(self.mask, entry_hash, probe); - - if their_dist < dist { - // Robinhood - break; - } - } else { - // Vacant slot - self.indices[probe] = Pos::new(index, hash); - continue 'outer; - } - - dist += 1; - }); - - do_insert_phase_two( - &mut self.indices, - probe, - Pos::new(index, hash)); - } - } - - fn reinsert_entry_in_order(&mut self, pos: Pos) { - if let Some((_, entry_hash)) = pos.resolve() { - // Find first empty bucket and insert there - let mut probe = desired_pos(self.mask, entry_hash); - - probe_loop!(probe < self.indices.len(), { - if self.indices[probe].resolve().is_none() { - // empty bucket, insert here - self.indices[probe] = pos; - return; - } - }); - } - } - - fn reserve_one(&mut self) { - let len = self.entries.len(); - - if self.danger.is_yellow() { - let load_factor = self.entries.len() as f32 / self.indices.len() as f32; - - if load_factor >= LOAD_FACTOR_THRESHOLD { - // Transition back to green danger level - self.danger.to_green(); - - // Double the capacity - let new_cap = self.indices.len() * 2; - - // Grow the capacity - self.grow(new_cap); - } else { - self.danger.to_red(); - - // Rebuild hash table - for index in self.indices.iter_mut() { - *index = Pos::none(); - } - - self.rebuild(); - } - } else if len == self.capacity() { - if len == 0 { - let new_raw_cap = 8; - self.mask = 8 - 1; - self.indices = vec![Pos::none(); new_raw_cap].into_boxed_slice(); - self.entries = Vec::with_capacity(usable_capacity(new_raw_cap)); - } else { - let raw_cap = self.indices.len(); - self.grow(raw_cap << 1); - } - } - } - - #[inline] - fn grow(&mut self, new_raw_cap: usize) { - // This path can never be reached when handling the first allocation in - // the map. - - // find first ideally placed element -- start of cluster - let mut first_ideal = 0; - - for (i, pos) in self.indices.iter().enumerate() { - if let Some((_, entry_hash)) = pos.resolve() { - if 0 == probe_distance(self.mask, entry_hash, i) { - first_ideal = i; - break; - } - } - } - - // visit the entries in an order where we can simply reinsert them - // into self.indices without any bucket stealing. - let old_indices = mem::replace(&mut self.indices, vec![Pos::none(); new_raw_cap].into_boxed_slice()); - self.mask = new_raw_cap.wrapping_sub(1) as Size; - - for &pos in &old_indices[first_ideal..] { - self.reinsert_entry_in_order(pos); - } - - for &pos in &old_indices[..first_ideal] { - self.reinsert_entry_in_order(pos); - } - - // Reserve additional entry slots - let more = self.capacity() - self.entries.len(); - self.entries.reserve_exact(more); - } - - #[inline] - fn raw_links(&mut self) -> RawLinks { - RawLinks(&mut self.entries[..] as *mut _) - } -} - -/// Removes the `ExtraValue` at the given index. -#[inline] -fn remove_extra_value(mut raw_links: RawLinks, extra_values: &mut Vec>, idx: usize) -> ExtraValue { - let prev; - let next; - - { - debug_assert!(extra_values.len() > idx); - let extra = &extra_values[idx]; - prev = extra.prev; - next = extra.next; - } - - // First unlink the extra value - match (prev, next) { - (Link::Entry(prev), Link::Entry(next)) => { - debug_assert_eq!(prev, next); - - raw_links[prev] = None; - } - (Link::Entry(prev), Link::Extra(next)) => { - debug_assert!(raw_links[prev].is_some()); - - raw_links[prev].as_mut().unwrap() - .next = next; - - debug_assert!(extra_values.len() > next); - extra_values[next].prev = Link::Entry(prev); - } - (Link::Extra(prev), Link::Entry(next)) => { - debug_assert!(raw_links[next].is_some()); - - raw_links[next].as_mut().unwrap() - .tail = prev; - - debug_assert!(extra_values.len() > prev); - extra_values[prev].next = Link::Entry(next); - } - (Link::Extra(prev), Link::Extra(next)) => { - debug_assert!(extra_values.len() > next); - debug_assert!(extra_values.len() > prev); - - extra_values[prev].next = Link::Extra(next); - extra_values[next].prev = Link::Extra(prev); - } - } - - // Remove the extra value - let mut extra = extra_values.swap_remove(idx); - - // This is the index of the value that was moved (possibly `extra`) - let old_idx = extra_values.len(); - - // Update the links - if extra.prev == Link::Extra(old_idx) { - extra.prev = Link::Extra(idx); - } - - if extra.next == Link::Extra(old_idx) { - extra.next = Link::Extra(idx); - } - - // Check if another entry was displaced. If it was, then the links - // need to be fixed. - if idx != old_idx { - let next; - let prev; - - { - debug_assert!(extra_values.len() > idx); - let moved = &extra_values[idx]; - next = moved.next; - prev = moved.prev; - } - - // An entry was moved, we have to the links - match prev { - Link::Entry(entry_idx) => { - // It is critical that we do not attempt to read the - // header name or value as that memory may have been - // "released" already. - debug_assert!(raw_links[entry_idx].is_some()); - - let links = raw_links[entry_idx].as_mut().unwrap(); - links.next = idx; - } - Link::Extra(extra_idx) => { - debug_assert!(extra_values.len() > extra_idx); - extra_values[extra_idx].next = Link::Extra(idx); - } - } - - match next { - Link::Entry(entry_idx) => { - debug_assert!(raw_links[entry_idx].is_some()); - - let links = raw_links[entry_idx].as_mut().unwrap(); - links.tail = idx; - } - Link::Extra(extra_idx) => { - debug_assert!(extra_values.len() > extra_idx); - extra_values[extra_idx].prev = Link::Extra(idx); - } - } - } - - debug_assert!({ - for v in &*extra_values { - assert!(v.next != Link::Extra(old_idx)); - assert!(v.prev != Link::Extra(old_idx)); - } - - true - }); - - extra -} - - -fn drain_all_extra_values(raw_links: RawLinks, extra_values: &mut Vec>, mut head: usize) -> Vec { - let mut vec = Vec::new(); - loop { - let extra = remove_extra_value(raw_links, extra_values, head); - vec.push(extra.value); - - if let Link::Extra(idx) = extra.next { - head = idx; - } else { - break; - } - } - vec -} - -impl<'a, T> IntoIterator for &'a HeaderMap { - type Item = (&'a HeaderName, &'a T); - type IntoIter = Iter<'a, T>; - - fn into_iter(self) -> Iter<'a, T> { - self.iter() - } -} - -impl<'a, T> IntoIterator for &'a mut HeaderMap { - type Item = (&'a HeaderName, &'a mut T); - type IntoIter = IterMut<'a, T>; - - fn into_iter(self) -> IterMut<'a, T> { - self.iter_mut() - } -} - -impl IntoIterator for HeaderMap { - type Item = (Option, T); - type IntoIter = IntoIter; - - /// Creates a consuming iterator, that is, one that moves keys and values - /// out of the map in arbitrary order. The map cannot be used after calling - /// this. - /// - /// For each yielded item that has `None` provided for the `HeaderName`, - /// then the associated header name is the same as that of the previously - /// yielded item. The first yielded item will have `HeaderName` set. - /// - /// # Examples - /// - /// Basic usage. - /// - /// ``` - /// # use http::header; - /// # use http::header::*; - /// let mut map = HeaderMap::new(); - /// map.insert(header::CONTENT_LENGTH, "123".parse().unwrap()); - /// map.insert(header::CONTENT_TYPE, "json".parse().unwrap()); - /// - /// let mut iter = map.into_iter(); - /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap()))); - /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap()))); - /// assert!(iter.next().is_none()); - /// ``` - /// - /// Multiple values per key. - /// - /// ``` - /// # use http::header; - /// # use http::header::*; - /// let mut map = HeaderMap::new(); - /// - /// map.append(header::CONTENT_LENGTH, "123".parse().unwrap()); - /// map.append(header::CONTENT_LENGTH, "456".parse().unwrap()); - /// - /// map.append(header::CONTENT_TYPE, "json".parse().unwrap()); - /// map.append(header::CONTENT_TYPE, "html".parse().unwrap()); - /// map.append(header::CONTENT_TYPE, "xml".parse().unwrap()); - /// - /// let mut iter = map.into_iter(); - /// - /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap()))); - /// assert_eq!(iter.next(), Some((None, "456".parse().unwrap()))); - /// - /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap()))); - /// assert_eq!(iter.next(), Some((None, "html".parse().unwrap()))); - /// assert_eq!(iter.next(), Some((None, "xml".parse().unwrap()))); - /// assert!(iter.next().is_none()); - /// ``` - fn into_iter(self) -> IntoIter { - IntoIter { - next: None, - entries: self.entries.into_iter(), - extra_values: self.extra_values, - } - } -} - -impl FromIterator<(HeaderName, T)> for HeaderMap -{ - fn from_iter(iter: I) -> Self - where I: IntoIterator - { - let mut map = HeaderMap::default(); - map.extend(iter); - map - } -} - -/// Try to convert a `HashMap` into a `HeaderMap`. -/// -/// # Examples -/// -/// ``` -/// use std::collections::HashMap; -/// use http::{HttpTryFrom, header::HeaderMap}; -/// -/// let mut map = HashMap::new(); -/// map.insert("X-Custom-Header".to_string(), "my value".to_string()); -/// -/// let headers: HeaderMap = HttpTryFrom::try_from(&map).expect("valid headers"); -/// assert_eq!(headers["X-Custom-Header"], "my value"); -/// ``` -impl<'a, K, V, T> HttpTryFrom<&'a HashMap> for HeaderMap - where - K: Eq + Hash, - HeaderName: HttpTryFrom<&'a K>, - T: HttpTryFrom<&'a V> -{ - type Error = Error; - - fn try_from(c: &'a HashMap) -> Result { - c.into_iter() - .map(|(k, v)| { - let name = k.http_try_into()?; - let value = v.http_try_into()?; - Ok((name, value)) - }) - .collect() - } -} - -impl Extend<(Option, T)> for HeaderMap { - /// Extend a `HeaderMap` with the contents of another `HeaderMap`. - /// - /// This function expects the yielded items to follow the same structure as - /// `IntoIter`. - /// - /// # Panics - /// - /// This panics if the first yielded item does not have a `HeaderName`. - /// - /// # Examples - /// - /// ``` - /// # use http::header::*; - /// let mut map = HeaderMap::new(); - /// - /// map.insert(ACCEPT, "text/plain".parse().unwrap()); - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// - /// let mut extra = HeaderMap::new(); - /// - /// extra.insert(HOST, "foo.bar".parse().unwrap()); - /// extra.insert(COOKIE, "hello".parse().unwrap()); - /// extra.append(COOKIE, "world".parse().unwrap()); - /// - /// map.extend(extra); - /// - /// assert_eq!(map["host"], "foo.bar"); - /// assert_eq!(map["accept"], "text/plain"); - /// assert_eq!(map["cookie"], "hello"); - /// - /// let v = map.get_all("host"); - /// assert_eq!(1, v.iter().count()); - /// - /// let v = map.get_all("cookie"); - /// assert_eq!(2, v.iter().count()); - /// ``` - fn extend, T)>>(&mut self, iter: I) { - let mut iter = iter.into_iter(); - - // The structure of this is a bit weird, but it is mostly to make the - // borrow checker happy. - let (mut key, mut val) = match iter.next() { - Some((Some(key), val)) => (key, val), - Some((None, _)) => panic!("expected a header name, but got None"), - None => return, - }; - - 'outer: - loop { - let mut entry = match self.entry2(key) { - Entry::Occupied(mut e) => { - // Replace all previous values while maintaining a handle to - // the entry. - e.insert(val); - e - } - Entry::Vacant(e) => { - e.insert_entry(val) - } - }; - - // As long as `HeaderName` is none, keep inserting the value into - // the current entry - loop { - match iter.next() { - Some((Some(k), v)) => { - key = k; - val = v; - continue 'outer; - } - Some((None, v)) => { - entry.append(v); - } - None => { - return; - } - } - } - } - } -} - -impl Extend<(HeaderName, T)> for HeaderMap -{ - fn extend>(&mut self, iter: I) { - // Keys may be already present or show multiple times in the iterator. - // Reserve the entire hint lower bound if the map is empty. - // Otherwise reserve half the hint (rounded up), so the map - // will only resize twice in the worst case. - let iter = iter.into_iter(); - - let reserve = if self.is_empty() { - iter.size_hint().0 - } else { - (iter.size_hint().0 + 1) / 2 - }; - - self.reserve(reserve); - - for (k, v) in iter { - self.append(k, v); - } - } -} - -impl PartialEq for HeaderMap { - fn eq(&self, other: &HeaderMap) -> bool { - if self.len() != other.len() { - return false; - } - - self.keys().all(|key| { - self.get_all(key) == other.get_all(key) - }) - } -} - -impl Eq for HeaderMap {} - -impl fmt::Debug for HeaderMap { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_map().entries(self.iter()).finish() - } -} - -impl Default for HeaderMap { - fn default() -> Self { - HeaderMap::with_capacity(0) - } -} - -impl<'a, K, T> ops::Index for HeaderMap - where K: AsHeaderName, -{ - type Output = T; - - /// # Panics - /// Using the index operator will cause a panic if the header you're querying isn't set. - #[inline] - fn index(&self, index: K) -> &T { - match self.get2(&index) { - Some(val) => val, - None => panic!("no entry found for key {:?}", index.as_str()), - } - } -} - -/// phase 2 is post-insert where we forward-shift `Pos` in the indices. -/// -/// returns the number of displaced elements -#[inline] -fn do_insert_phase_two(indices: &mut [Pos], - mut probe: usize, - mut old_pos: Pos) - -> usize -{ - let mut num_displaced = 0; - - probe_loop!(probe < indices.len(), { - let pos = &mut indices[probe]; - - if pos.is_none() { - *pos = old_pos; - break; - } else { - num_displaced += 1; - old_pos = mem::replace(pos, old_pos); - } - }); - - num_displaced -} - -#[inline] -fn append_value(entry_idx: usize, - entry: &mut Bucket, - extra: &mut Vec>, - value: T) -{ - match entry.links { - Some(links) => { - let idx = extra.len(); - extra.push(ExtraValue { - value: value, - prev: Link::Extra(links.tail), - next: Link::Entry(entry_idx), - }); - - extra[links.tail].next = Link::Extra(idx); - - entry.links = Some(Links { - tail: idx, - .. links - }); - } - None => { - let idx = extra.len(); - extra.push(ExtraValue { - value: value, - prev: Link::Entry(entry_idx), - next: Link::Entry(entry_idx), - }); - - entry.links = Some(Links { - next: idx, - tail: idx, - }); - } - } -} - -// ===== impl Iter ===== - -impl<'a, T> Iterator for Iter<'a, T> { - type Item = (&'a HeaderName, &'a T); - - fn next(&mut self) -> Option { - self.inner.next_unsafe().map(|(key, ptr)| { - (key, unsafe { &*ptr }) - }) - } - - fn size_hint(&self) -> (usize, Option) { - self.inner.size_hint() - } -} - -unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {} -unsafe impl<'a, T: Sync> Send for Iter<'a, T> {} - -// ===== impl IterMut ===== - -impl<'a, T> IterMut<'a, T> { - fn next_unsafe(&mut self) -> Option<(&'a HeaderName, *mut T)> { - use self::Cursor::*; - - if self.cursor.is_none() { - if (self.entry + 1) >= unsafe { &*self.map }.entries.len() { - return None; - } - - self.entry += 1; - self.cursor = Some(Cursor::Head); - } - - let entry = unsafe { &(*self.map).entries[self.entry] }; - - match self.cursor.unwrap() { - Head => { - self.cursor = entry.links.map(|l| Values(l.next)); - Some((&entry.key, &entry.value as *const _ as *mut _)) - } - Values(idx) => { - let extra = unsafe { &(*self.map).extra_values[idx] }; - - match extra.next { - Link::Entry(_) => self.cursor = None, - Link::Extra(i) => self.cursor = Some(Values(i)), - } - - Some((&entry.key, &extra.value as *const _ as *mut _)) - } - } - } -} - -impl<'a, T> Iterator for IterMut<'a, T> { - type Item = (&'a HeaderName, &'a mut T); - - fn next(&mut self) -> Option { - self.next_unsafe().map(|(key, ptr)| { - (key, unsafe { &mut *ptr }) - }) - } - - fn size_hint(&self) -> (usize, Option) { - let map = unsafe { &*self.map }; - debug_assert!(map.entries.len() >= self.entry); - - let lower = map.entries.len() - self.entry; - // We could pessimistically guess at the upper bound, saying - // that its lower + map.extra_values.len(). That could be - // way over though, such as if we're near the end, and have - // already gone through several extra values... - (lower, None) - } -} - -unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {} -unsafe impl<'a, T: Send> Send for IterMut<'a, T> {} - -// ===== impl Keys ===== - -impl<'a, T> Iterator for Keys<'a, T> { - type Item = &'a HeaderName; - - fn next(&mut self) -> Option { - self.inner.next().map(|b| &b.key) - } - - fn size_hint(&self) -> (usize, Option) { - self.inner.size_hint() - } -} - -impl<'a, T> ExactSizeIterator for Keys<'a, T> {} - -// ===== impl Values ==== - -impl<'a, T> Iterator for Values<'a, T> { - type Item = &'a T; - - fn next(&mut self) -> Option { - self.inner.next().map(|(_, v)| v) - } - - fn size_hint(&self) -> (usize, Option) { - self.inner.size_hint() - } -} - -// ===== impl ValuesMut ==== - -impl<'a, T> Iterator for ValuesMut<'a, T> { - type Item = &'a mut T; - - fn next(&mut self) -> Option { - self.inner.next().map(|(_, v)| v) - } - - fn size_hint(&self) -> (usize, Option) { - self.inner.size_hint() - } -} - -// ===== impl Drain ===== - -impl<'a, T> Iterator for Drain<'a, T> { - type Item = (HeaderName, ValueDrain<'a, T>); - - fn next(&mut self) -> Option { - let idx = self.idx; - - if idx == self.len { - return None; - } - - self.idx += 1; - - let key; - let value; - let next; - - let values = unsafe { - let entry = &(*self.entries)[idx]; - - // Read the header name - key = ptr::read(&entry.key as *const _); - value = ptr::read(&entry.value as *const _); - - let raw_links = RawLinks(self.entries); - let extra_values = &mut *self.extra_values; - next = entry.links.map(|l| { - drain_all_extra_values(raw_links, extra_values, l.next) - .into_iter() - }); - - ValueDrain { - first: Some(value), - next, - lt: PhantomData, - } - }; - - Some((key, values)) - } - - fn size_hint(&self) -> (usize, Option) { - let lower = self.len - self.idx; - (lower, Some(lower)) - } -} - -impl<'a, T> Drop for Drain<'a, T> { - fn drop(&mut self) { - for _ in self {} - } -} - -unsafe impl<'a, T: Sync> Sync for Drain<'a, T> {} -unsafe impl<'a, T: Send> Send for Drain<'a, T> {} - -// ===== impl Entry ===== - -impl<'a, T> Entry<'a, T> { - /// Ensures a value is in the entry by inserting the default if empty. - /// - /// Returns a mutable reference to the **first** value in the entry. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// let mut map: HeaderMap = HeaderMap::default(); - /// - /// let headers = &[ - /// "content-length", - /// "x-hello", - /// "Content-Length", - /// "x-world", - /// ]; - /// - /// for &header in headers { - /// let counter = map.entry(header) - /// .expect("valid header names") - /// .or_insert(0); - /// *counter += 1; - /// } - /// - /// assert_eq!(map["content-length"], 2); - /// assert_eq!(map["x-hello"], 1); - /// ``` - pub fn or_insert(self, default: T) -> &'a mut T { - use self::Entry::*; - - match self { - Occupied(e) => e.into_mut(), - Vacant(e) => e.insert(default), - } - } - - /// Ensures a value is in the entry by inserting the result of the default - /// function if empty. - /// - /// The default function is not called if the entry exists in the map. - /// Returns a mutable reference to the **first** value in the entry. - /// - /// # Examples - /// - /// Basic usage. - /// - /// ``` - /// # use http::HeaderMap; - /// let mut map = HeaderMap::new(); - /// - /// let res = map.entry("x-hello").unwrap() - /// .or_insert_with(|| "world".parse().unwrap()); - /// - /// assert_eq!(res, "world"); - /// ``` - /// - /// The default function is not called if the entry exists in the map. - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// - /// let res = map.entry("host") - /// .expect("host is a valid string") - /// .or_insert_with(|| unreachable!()); - /// - /// - /// assert_eq!(res, "world"); - /// ``` - pub fn or_insert_with T>(self, default: F) -> &'a mut T { - use self::Entry::*; - - match self { - Occupied(e) => e.into_mut(), - Vacant(e) => e.insert(default()), - } - } - - /// Returns a reference to the entry's key - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// let mut map = HeaderMap::new(); - /// - /// assert_eq!(map.entry("x-hello").unwrap().key(), "x-hello"); - /// ``` - pub fn key(&self) -> &HeaderName { - use self::Entry::*; - - match *self { - Vacant(ref e) => e.key(), - Occupied(ref e) => e.key(), - } - } -} - -// ===== impl VacantEntry ===== - -impl<'a, T> VacantEntry<'a, T> { - /// Returns a reference to the entry's key - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// let mut map = HeaderMap::new(); - /// - /// assert_eq!(map.entry("x-hello").unwrap().key().as_str(), "x-hello"); - /// ``` - pub fn key(&self) -> &HeaderName { - &self.key - } - - /// Take ownership of the key - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry}; - /// let mut map = HeaderMap::new(); - /// - /// if let Entry::Vacant(v) = map.entry("x-hello").unwrap() { - /// assert_eq!(v.into_key().as_str(), "x-hello"); - /// } - /// ``` - pub fn into_key(self) -> HeaderName { - self.key - } - - /// Insert the value into the entry. - /// - /// The value will be associated with this entry's key. A mutable reference - /// to the inserted value will be returned. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry}; - /// let mut map = HeaderMap::new(); - /// - /// if let Entry::Vacant(v) = map.entry("x-hello").unwrap() { - /// v.insert("world".parse().unwrap()); - /// } - /// - /// assert_eq!(map["x-hello"], "world"); - /// ``` - pub fn insert(self, value: T) -> &'a mut T { - // Ensure that there is space in the map - let index = self.map.insert_phase_two( - self.key, - value.into(), - self.hash, - self.probe, - self.danger); - - &mut self.map.entries[index].value - } - - /// Insert the value into the entry. - /// - /// The value will be associated with this entry's key. The new - /// `OccupiedEntry` is returned, allowing for further manipulation. - /// - /// # Examples - /// - /// ``` - /// # use http::header::*; - /// let mut map = HeaderMap::new(); - /// - /// if let Entry::Vacant(v) = map.entry("x-hello").unwrap() { - /// let mut e = v.insert_entry("world".parse().unwrap()); - /// e.insert("world2".parse().unwrap()); - /// } - /// - /// assert_eq!(map["x-hello"], "world2"); - /// ``` - pub fn insert_entry(self, value: T) -> OccupiedEntry<'a, T> { - // Ensure that there is space in the map - let index = self.map.insert_phase_two( - self.key, - value.into(), - self.hash, - self.probe, - self.danger); - - OccupiedEntry { - map: self.map, - index: index, - probe: self.probe, - } - } -} - - -// ===== impl GetAll ===== - -impl<'a, T: 'a> GetAll<'a, T> { - /// Returns an iterator visiting all values associated with the entry. - /// - /// Values are iterated in insertion order. - /// - /// # Examples - /// - /// ``` - /// # use http::HeaderMap; - /// # use http::header::HOST; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// map.append(HOST, "hello.earth".parse().unwrap()); - /// - /// let values = map.get_all("host"); - /// let mut iter = values.iter(); - /// assert_eq!(&"hello.world", iter.next().unwrap()); - /// assert_eq!(&"hello.earth", iter.next().unwrap()); - /// assert!(iter.next().is_none()); - /// ``` - pub fn iter(&self) -> ValueIter<'a, T> { - // This creates a new GetAll struct so that the lifetime - // isn't bound to &self. - GetAll { - map: self.map, - index: self.index, - }.into_iter() - } -} - -impl<'a, T: PartialEq> PartialEq for GetAll<'a, T> { - fn eq(&self, other: &Self) -> bool { - self.iter().eq(other.iter()) - } -} - -impl<'a, T> IntoIterator for GetAll<'a, T> { - type Item = &'a T; - type IntoIter = ValueIter<'a, T>; - - fn into_iter(self) -> ValueIter<'a, T> { - self.map.value_iter(self.index) - } -} - -impl<'a, 'b: 'a, T> IntoIterator for &'b GetAll<'a, T> { - type Item = &'a T; - type IntoIter = ValueIter<'a, T>; - - fn into_iter(self) -> ValueIter<'a, T> { - self.map.value_iter(self.index) - } -} - -// ===== impl ValueIter ===== - -impl<'a, T: 'a> Iterator for ValueIter<'a, T> { - type Item = &'a T; - - fn next(&mut self) -> Option { - use self::Cursor::*; - - - match self.front { - Some(Head) => { - let entry = &self.map.entries[self.index]; - - if self.back == Some(Head) { - self.front = None; - self.back = None; - } else { - // Update the iterator state - match entry.links { - Some(links) => { - self.front = Some(Values(links.next)); - } - None => unreachable!(), - } - } - - Some(&entry.value) - } - Some(Values(idx)) => { - let extra = &self.map.extra_values[idx]; - - if self.front == self.back { - self.front = None; - self.back = None; - } else { - match extra.next { - Link::Entry(_) => self.front = None, - Link::Extra(i) => self.front = Some(Values(i)), - } - } - - Some(&extra.value) - } - None => None, - } - } - - fn size_hint(&self) -> (usize, Option) { - match (self.front, self.back) { - // Exactly 1 value... - (Some(Cursor::Head), Some(Cursor::Head)) => (1, Some(1)), - // At least 1... - (Some(_), _) => (1, None), - // No more values... - (None, _) => (0, Some(0)), - } - } -} - -impl<'a, T: 'a> DoubleEndedIterator for ValueIter<'a, T> { - fn next_back(&mut self) -> Option { - use self::Cursor::*; - - - match self.back { - Some(Head) => { - self.front = None; - self.back = None; - Some(&self.map.entries[self.index].value) - } - Some(Values(idx)) => { - let extra = &self.map.extra_values[idx]; - - if self.front == self.back { - self.front = None; - self.back = None; - } else { - match extra.prev { - Link::Entry(_) => self.back = Some(Head), - Link::Extra(idx) => self.back = Some(Values(idx)), - } - } - - Some(&extra.value) - } - None => None, - } - } -} - -// ===== impl ValueIterMut ===== - -impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> { - type Item = &'a mut T; - - fn next(&mut self) -> Option { - use self::Cursor::*; - - let entry = unsafe { &mut (*self.map).entries[self.index] }; - - match self.front { - Some(Head) => { - if self.back == Some(Head) { - self.front = None; - self.back = None; - } else { - // Update the iterator state - match entry.links { - Some(links) => { - self.front = Some(Values(links.next)); - } - None => unreachable!(), - } - } - - Some(&mut entry.value) - } - Some(Values(idx)) => { - let extra = unsafe { &mut (*self.map).extra_values[idx] }; - - if self.front == self.back { - self.front = None; - self.back = None; - } else { - match extra.next { - Link::Entry(_) => self.front = None, - Link::Extra(i) => self.front = Some(Values(i)), - } - } - - Some(&mut extra.value) - } - None => None, - } - } -} - -impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> { - fn next_back(&mut self) -> Option { - use self::Cursor::*; - - let entry = unsafe { &mut (*self.map).entries[self.index] }; - - match self.back { - Some(Head) => { - self.front = None; - self.back = None; - Some(&mut entry.value) - } - Some(Values(idx)) => { - let extra = unsafe { &mut (*self.map).extra_values[idx] }; - - if self.front == self.back { - self.front = None; - self.back = None; - } else { - match extra.prev { - Link::Entry(_) => self.back = Some(Head), - Link::Extra(idx) => self.back = Some(Values(idx)), - } - } - - Some(&mut extra.value) - } - None => None, - } - } -} - -unsafe impl<'a, T: Sync> Sync for ValueIterMut<'a, T> {} -unsafe impl<'a, T: Send> Send for ValueIterMut<'a, T> {} - -// ===== impl IntoIter ===== - -impl Iterator for IntoIter { - type Item = (Option, T); - - fn next(&mut self) -> Option { - if let Some(next) = self.next { - self.next = match self.extra_values[next].next { - Link::Entry(_) => None, - Link::Extra(v) => Some(v), - }; - - let value = unsafe { ptr::read(&self.extra_values[next].value) }; - - return Some((None, value)); - } - - if let Some(bucket) = self.entries.next() { - self.next = bucket.links.map(|l| l.next); - let name = Some(bucket.key); - let value = bucket.value; - - return Some((name, value)); - } - - None - } - - fn size_hint(&self) -> (usize, Option) { - let (lower, _) = self.entries.size_hint(); - // There could be more than just the entries upper, as there - // could be items in the `extra_values`. We could guess, saying - // `upper + extra_values.len()`, but that could overestimate by a lot. - (lower, None) - } -} - -impl Drop for IntoIter { - fn drop(&mut self) { - // Ensure the iterator is consumed - for _ in self.by_ref() { } - - // All the values have already been yielded out. - unsafe { self.extra_values.set_len(0); } - } -} - -// ===== impl OccupiedEntry ===== - -impl<'a, T> OccupiedEntry<'a, T> { - /// Returns a reference to the entry's key. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// - /// if let Entry::Occupied(e) = map.entry("host").unwrap() { - /// assert_eq!("host", e.key()); - /// } - /// ``` - pub fn key(&self) -> &HeaderName { - &self.map.entries[self.index].key - } - - /// Get a reference to the first value in the entry. - /// - /// Values are stored in insertion order. - /// - /// # Panics - /// - /// `get` panics if there are no values associated with the entry. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// - /// if let Entry::Occupied(mut e) = map.entry("host").unwrap() { - /// assert_eq!(e.get(), &"hello.world"); - /// - /// e.append("hello.earth".parse().unwrap()); - /// - /// assert_eq!(e.get(), &"hello.world"); - /// } - /// ``` - pub fn get(&self) -> &T { - &self.map.entries[self.index].value - } - - /// Get a mutable reference to the first value in the entry. - /// - /// Values are stored in insertion order. - /// - /// # Panics - /// - /// `get_mut` panics if there are no values associated with the entry. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::default(); - /// map.insert(HOST, "hello.world".to_string()); - /// - /// if let Entry::Occupied(mut e) = map.entry("host").unwrap() { - /// e.get_mut().push_str("-2"); - /// assert_eq!(e.get(), &"hello.world-2"); - /// } - /// ``` - pub fn get_mut(&mut self) -> &mut T { - &mut self.map.entries[self.index].value - } - - /// Converts the `OccupiedEntry` into a mutable reference to the **first** - /// value. - /// - /// The lifetime of the returned reference is bound to the original map. - /// - /// # Panics - /// - /// `into_mut` panics if there are no values associated with the entry. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::default(); - /// map.insert(HOST, "hello.world".to_string()); - /// map.append(HOST, "hello.earth".to_string()); - /// - /// if let Entry::Occupied(e) = map.entry("host").unwrap() { - /// e.into_mut().push_str("-2"); - /// } - /// - /// assert_eq!("hello.world-2", map["host"]); - /// ``` - pub fn into_mut(self) -> &'a mut T { - &mut self.map.entries[self.index].value - } - - /// Sets the value of the entry. - /// - /// All previous values associated with the entry are removed and the first - /// one is returned. See `insert_mult` for an API that returns all values. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "hello.world".parse().unwrap()); - /// - /// if let Entry::Occupied(mut e) = map.entry("host").unwrap() { - /// let mut prev = e.insert("earth".parse().unwrap()); - /// assert_eq!("hello.world", prev); - /// } - /// - /// assert_eq!("earth", map["host"]); - /// ``` - pub fn insert(&mut self, value: T) -> T { - self.map.insert_occupied(self.index, value.into()) - } - - /// Sets the value of the entry. - /// - /// This function does the same as `insert` except it returns an iterator - /// that yields all values previously associated with the key. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// map.append(HOST, "world2".parse().unwrap()); - /// - /// if let Entry::Occupied(mut e) = map.entry("host").unwrap() { - /// let mut prev = e.insert_mult("earth".parse().unwrap()); - /// assert_eq!("world", prev.next().unwrap()); - /// assert_eq!("world2", prev.next().unwrap()); - /// assert!(prev.next().is_none()); - /// } - /// - /// assert_eq!("earth", map["host"]); - /// ``` - pub fn insert_mult(&mut self, value: T) -> ValueDrain { - self.map.insert_occupied_mult(self.index, value.into()) - } - - /// Insert the value into the entry. - /// - /// The new value is appended to the end of the entry's value list. All - /// previous values associated with the entry are retained. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// - /// if let Entry::Occupied(mut e) = map.entry("host").unwrap() { - /// e.append("earth".parse().unwrap()); - /// } - /// - /// let values = map.get_all("host"); - /// let mut i = values.iter(); - /// assert_eq!("world", *i.next().unwrap()); - /// assert_eq!("earth", *i.next().unwrap()); - /// ``` - pub fn append(&mut self, value: T) { - let idx = self.index; - let entry = &mut self.map.entries[idx]; - append_value(idx, entry, &mut self.map.extra_values, value.into()); - } - - /// Remove the entry from the map. - /// - /// All values associated with the entry are removed and the first one is - /// returned. See `remove_entry_mult` for an API that returns all values. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// - /// if let Entry::Occupied(e) = map.entry("host").unwrap() { - /// let mut prev = e.remove(); - /// assert_eq!("world", prev); - /// } - /// - /// assert!(!map.contains_key("host")); - /// ``` - pub fn remove(self) -> T { - self.remove_entry().1 - } - - /// Remove the entry from the map. - /// - /// The key and all values associated with the entry are removed and the - /// first one is returned. See `remove_entry_mult` for an API that returns - /// all values. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// - /// if let Entry::Occupied(e) = map.entry("host").unwrap() { - /// let (key, mut prev) = e.remove_entry(); - /// assert_eq!("host", key.as_str()); - /// assert_eq!("world", prev); - /// } - /// - /// assert!(!map.contains_key("host")); - /// ``` - pub fn remove_entry(self) -> (HeaderName, T) { - let entry = self.map.remove_found(self.probe, self.index); - - if let Some(links) = entry.links { - self.map.remove_all_extra_values(links.next); - } - - (entry.key, entry.value) - } - - /// Remove the entry from the map. - /// - /// The key and all values associated with the entry are removed and - /// returned. - pub fn remove_entry_mult(self) -> (HeaderName, ValueDrain<'a, T>) { - let entry = self.map.remove_found(self.probe, self.index); - let raw_links = self.map.raw_links(); - let extra_values = &mut self.map.extra_values; - - let next = entry.links.map(|l| { - drain_all_extra_values(raw_links, extra_values, l.next) - .into_iter() - }); - let drain = ValueDrain { - first: Some(entry.value), - next, - lt: PhantomData, - }; - (entry.key, drain) - } - - /// Returns an iterator visiting all values associated with the entry. - /// - /// Values are iterated in insertion order. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::new(); - /// map.insert(HOST, "world".parse().unwrap()); - /// map.append(HOST, "earth".parse().unwrap()); - /// - /// if let Entry::Occupied(e) = map.entry("host").unwrap() { - /// let mut iter = e.iter(); - /// assert_eq!(&"world", iter.next().unwrap()); - /// assert_eq!(&"earth", iter.next().unwrap()); - /// assert!(iter.next().is_none()); - /// } - /// ``` - pub fn iter(&self) -> ValueIter { - self.map.value_iter(Some(self.index)) - } - - /// Returns an iterator mutably visiting all values associated with the - /// entry. - /// - /// Values are iterated in insertion order. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderMap, Entry, HOST}; - /// let mut map = HeaderMap::default(); - /// map.insert(HOST, "world".to_string()); - /// map.append(HOST, "earth".to_string()); - /// - /// if let Entry::Occupied(mut e) = map.entry("host").unwrap() { - /// for e in e.iter_mut() { - /// e.push_str("-boop"); - /// } - /// } - /// - /// let mut values = map.get_all("host"); - /// let mut i = values.iter(); - /// assert_eq!(&"world-boop", i.next().unwrap()); - /// assert_eq!(&"earth-boop", i.next().unwrap()); - /// ``` - pub fn iter_mut(&mut self) -> ValueIterMut { - self.map.value_iter_mut(self.index) - } -} - -impl<'a, T> IntoIterator for OccupiedEntry<'a, T> { - type Item = &'a mut T; - type IntoIter = ValueIterMut<'a, T>; - - fn into_iter(self) -> ValueIterMut<'a, T> { - self.map.value_iter_mut(self.index) - } -} - -impl<'a, 'b: 'a, T> IntoIterator for &'b OccupiedEntry<'a, T> { - type Item = &'a T; - type IntoIter = ValueIter<'a, T>; - - fn into_iter(self) -> ValueIter<'a, T> { - self.iter() - } -} - -impl<'a, 'b: 'a, T> IntoIterator for &'b mut OccupiedEntry<'a, T> { - type Item = &'a mut T; - type IntoIter = ValueIterMut<'a, T>; - - fn into_iter(self) -> ValueIterMut<'a, T> { - self.iter_mut() - } -} - -// ===== impl ValueDrain ===== - -impl<'a, T> Iterator for ValueDrain<'a, T> { - type Item = T; - - fn next(&mut self) -> Option { - if self.first.is_some() { - self.first.take() - } else if let Some(ref mut extras) = self.next { - extras.next() - } else { - None - } - } - - fn size_hint(&self) -> (usize, Option) { - match (&self.first, &self.next) { - // Exactly 1 - (&Some(_), &None) => (1, Some(1)), - // 1 + extras - (&Some(_), &Some(ref extras)) => { - let (l, u) = extras.size_hint(); - (l + 1, u.map(|u| u + 1)) - }, - // Extras only - (&None, &Some(ref extras)) => extras.size_hint(), - // No more - (&None, &None) => (0, Some(0)), - } - } -} - -impl<'a, T> Drop for ValueDrain<'a, T> { - fn drop(&mut self) { - while let Some(_) = self.next() { - } - } -} - -unsafe impl<'a, T: Sync> Sync for ValueDrain<'a, T> {} -unsafe impl<'a, T: Send> Send for ValueDrain<'a, T> {} - -// ===== impl RawLinks ===== - -impl Clone for RawLinks { - fn clone(&self) -> RawLinks { - *self - } -} - -impl Copy for RawLinks {} - -impl ops::Index for RawLinks { - type Output = Option; - - fn index(&self, idx: usize) -> &Self::Output { - unsafe { - &(*self.0)[idx].links - } - } -} - -impl ops::IndexMut for RawLinks { - fn index_mut(&mut self, idx: usize) -> &mut Self::Output { - unsafe { - &mut (*self.0)[idx].links - } - } -} - -// ===== impl Pos ===== - -impl Pos { - #[inline] - fn new(index: usize, hash: HashValue) -> Self { - Pos { - index: index as Size, - hash: hash, - } - } - - #[inline] - fn none() -> Self { - Pos { - index: !0, - hash: HashValue(0), - } - } - - #[inline] - fn is_some(&self) -> bool { - !self.is_none() - } - - #[inline] - fn is_none(&self) -> bool { - self.index == !0 - } - - #[inline] - fn resolve(&self) -> Option<(usize, HashValue)> { - if self.is_some() { - Some((self.index, self.hash)) - } else { - None - } - } -} - -impl Danger { - fn is_red(&self) -> bool { - match *self { - Danger::Red(_) => true, - _ => false, - } - } - - fn to_red(&mut self) { - debug_assert!(self.is_yellow()); - *self = Danger::Red(RandomState::new()); - } - - fn is_yellow(&self) -> bool { - match *self { - Danger::Yellow => true, - _ => false, - } - } - - fn to_yellow(&mut self) { - match *self { - Danger::Green => { - *self = Danger::Yellow; - } - _ => {} - } - } - - fn to_green(&mut self) { - debug_assert!(self.is_yellow()); - *self = Danger::Green; - } -} - -// ===== impl Utils ===== - -#[inline] -fn usable_capacity(cap: usize) -> usize { - cap - cap / 4 -} - -#[inline] -fn to_raw_capacity(n: usize) -> usize { - n + n / 3 -} - -#[inline] -fn desired_pos(mask: Size, hash: HashValue) -> usize { - (hash.0 & mask) -} - -/// The number of steps that `current` is forward of the desired position for hash -#[inline] -fn probe_distance(mask: Size, hash: HashValue, current: usize) -> usize { - current.wrapping_sub(desired_pos(mask, hash)) & mask -} - -fn hash_elem_using(danger: &Danger, k: &K) -> HashValue - where K: Hash -{ - use fnv::FnvHasher; - - const MASK: u64 = (MAX_SIZE as u64) - 1; - - let hash = match *danger { - // Safe hash - Danger::Red(ref hasher) => { - let mut h = hasher.build_hasher(); - k.hash(&mut h); - h.finish() - } - // Fast hash - _ => { - let mut h = FnvHasher::default(); - k.hash(&mut h); - h.finish() - } - }; - - HashValue((hash & MASK) as usize) -} - -/* - * - * ===== impl IntoHeaderName / AsHeaderName ===== - * - */ - - -mod into_header_name { - use super::{HdrName, HeaderMap, HeaderName}; - - /// A marker trait used to identify values that can be used as insert keys - /// to a `HeaderMap`. - pub trait IntoHeaderName: Sealed {} - - // All methods are on this pub(super) trait, instead of `IntoHeaderName`, - // so that they aren't publicly exposed to the world. - // - // Being on the `IntoHeaderName` trait would mean users could call - // `"host".insert(&mut map, "localhost")`. - // - // Ultimately, this allows us to adjust the signatures of these methods - // without breaking any external crate. - pub trait Sealed { - #[doc(hidden)] - fn insert(self, map: &mut HeaderMap, val: T) -> Option; - - #[doc(hidden)] - fn append(self, map: &mut HeaderMap, val: T) -> bool; - } - - // ==== impls ==== - - impl Sealed for HeaderName { - #[doc(hidden)] - #[inline] - fn insert(self, map: &mut HeaderMap, val: T) -> Option { - map.insert2(self, val) - } - - #[doc(hidden)] - #[inline] - fn append(self, map: &mut HeaderMap, val: T) -> bool { - map.append2(self, val) - } - } - - impl IntoHeaderName for HeaderName {} - - impl<'a> Sealed for &'a HeaderName { - #[doc(hidden)] - #[inline] - fn insert(self, map: &mut HeaderMap, val: T) -> Option { - map.insert2(self, val) - } - #[doc(hidden)] - #[inline] - fn append(self, map: &mut HeaderMap, val: T) -> bool { - map.append2(self, val) - } - } - - impl<'a> IntoHeaderName for &'a HeaderName {} - - impl Sealed for &'static str { - #[doc(hidden)] - #[inline] - fn insert(self, map: &mut HeaderMap, val: T) -> Option { - HdrName::from_static(self, move |hdr| map.insert2(hdr, val)) - } - #[doc(hidden)] - #[inline] - fn append(self, map: &mut HeaderMap, val: T) -> bool { - HdrName::from_static(self, move |hdr| map.append2(hdr, val)) - } - } - - impl IntoHeaderName for &'static str {} -} - -mod as_header_name { - use super::{Entry, HdrName, HeaderMap, HeaderName, InvalidHeaderName}; - - /// A marker trait used to identify values that can be used as search keys - /// to a `HeaderMap`. - pub trait AsHeaderName: Sealed {} - - // All methods are on this pub(super) trait, instead of `AsHeaderName`, - // so that they aren't publicly exposed to the world. - // - // Being on the `AsHeaderName` trait would mean users could call - // `"host".find(&map)`. - // - // Ultimately, this allows us to adjust the signatures of these methods - // without breaking any external crate. - pub trait Sealed { - #[doc(hidden)] - fn entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName>; - - #[doc(hidden)] - fn find(&self, map: &HeaderMap) -> Option<(usize, usize)>; - - #[doc(hidden)] - fn as_str(&self) -> &str; - } - - // ==== impls ==== - - impl Sealed for HeaderName { - #[doc(hidden)] - #[inline] - fn entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { - Ok(map.entry2(self)) - } - - #[doc(hidden)] - #[inline] - fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { - map.find(self) - } - - #[doc(hidden)] - fn as_str(&self) -> &str { - ::as_str(self) - } - } - - impl AsHeaderName for HeaderName {} - - impl<'a> Sealed for &'a HeaderName { - #[doc(hidden)] - #[inline] - fn entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { - Ok(map.entry2(self)) - } - - #[doc(hidden)] - #[inline] - fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { - map.find(*self) - } - - #[doc(hidden)] - fn as_str(&self) -> &str { - ::as_str(*self) - } - } - - impl<'a> AsHeaderName for &'a HeaderName {} - - impl<'a> Sealed for &'a str { - #[doc(hidden)] - #[inline] - fn entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { - HdrName::from_bytes(self.as_bytes(), move |hdr| map.entry2(hdr)) - } - - #[doc(hidden)] - #[inline] - fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { - HdrName::from_bytes(self.as_bytes(), move |hdr| map.find(&hdr)).unwrap_or(None) - } - - #[doc(hidden)] - fn as_str(&self) -> &str { - self - } - } - - impl<'a> AsHeaderName for &'a str {} - - impl Sealed for String { - #[doc(hidden)] - #[inline] - fn entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { - self.as_str().entry(map) - } - - #[doc(hidden)] - #[inline] - fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { - Sealed::find(&self.as_str(), map) - } - - #[doc(hidden)] - fn as_str(&self) -> &str { - self - } - } - - impl AsHeaderName for String {} - - impl<'a> Sealed for &'a String { - #[doc(hidden)] - #[inline] - fn entry(self, map: &mut HeaderMap) -> Result, InvalidHeaderName> { - self.as_str().entry(map) - } - - #[doc(hidden)] - #[inline] - fn find(&self, map: &HeaderMap) -> Option<(usize, usize)> { - Sealed::find(*self, map) - } - - #[doc(hidden)] - fn as_str(&self) -> &str { - *self - } - } - - impl<'a> AsHeaderName for &'a String {} -} - - -#[test] -fn test_bounds() { - fn check_bounds() {} - - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); - check_bounds::>(); -} - -#[test] -fn skip_duplicates_during_key_iteration() { - let mut map = HeaderMap::new(); - map.append("a", HeaderValue::from_static("a")); - map.append("a", HeaderValue::from_static("b")); - assert_eq!(map.keys().count(), map.keys_len()); -} diff --git a/third_party/rust/http/v0_1/crate/src/header/mod.rs b/third_party/rust/http/v0_1/crate/src/header/mod.rs deleted file mode 100644 index 2b8582146339..000000000000 --- a/third_party/rust/http/v0_1/crate/src/header/mod.rs +++ /dev/null @@ -1,196 +0,0 @@ -//! HTTP header types -//! -//! The module provides [`HeaderName`], [`HeaderMap`], and a number of types -//! used for interacting with `HeaderMap`. These types allow representing both -//! HTTP/1 and HTTP/2 headers. -//! -//! # `HeaderName` -//! -//! The `HeaderName` type represents both standard header names as well as -//! custom header names. The type handles the case insensitive nature of header -//! names and is used as the key portion of `HeaderMap`. Header names are -//! normalized to lower case. In other words, when creating a `HeaderName` with -//! a string, even if upper case characters are included, when getting a string -//! representation of the `HeaderName`, it will be all lower case. This allows -//! for faster `HeaderMap` comparison operations. -//! -//! The internal representation is optimized to efficiently handle the cases -//! most commonly encountered when working with HTTP. Standard header names are -//! special cased and are represented internally as an enum. Short custom -//! headers will be stored directly in the `HeaderName` struct and will not -//! incur any allocation overhead, however longer strings will require an -//! allocation for storage. -//! -//! ## Limitations -//! -//! `HeaderName` has a max length of 32,768 for header names. Attempting to -//! parse longer names will result in a panic. -//! -//! # `HeaderMap` -//! -//! `HeaderMap` is a map structure of header names highly optimized for use -//! cases common with HTTP. It is a [multimap] structure, where each header name -//! may have multiple associated header values. Given this, some of the APIs -//! diverge from [`HashMap`]. -//! -//! ## Overview -//! -//! Just like `HashMap` in Rust's stdlib, `HeaderMap` is based on [Robin Hood -//! hashing]. This algorithm tends to reduce the worst case search times in the -//! table and enables high load factors without seriously affecting performance. -//! Internally, keys and values are stored in vectors. As such, each insertion -//! will not incur allocation overhead. However, once the underlying vector -//! storage is full, a larger vector must be allocated and all values copied. -//! -//! ## Deterministic ordering -//! -//! Unlike Rust's `HashMap`, values in `HeaderMap` are deterministically -//! ordered. Roughly, values are ordered by insertion. This means that a -//! function that deterministically operates on a header map can rely on the -//! iteration order to remain consistent across processes and platforms. -//! -//! ## Adaptive hashing -//! -//! `HeaderMap` uses an adaptive hashing strategy in order to efficiently handle -//! most common cases. All standard headers have statically computed hash values -//! which removes the need to perform any hashing of these headers at runtime. -//! The default hash function emphasizes performance over robustness. However, -//! `HeaderMap` detects high collision rates and switches to a secure hash -//! function in those events. The threshold is set such that only denial of -//! service attacks should trigger it. -//! -//! ## Limitations -//! -//! `HeaderMap` can store a maximum of 32,768 headers (header name / value -//! pairs). Attempting to insert more will result in a panic. -//! -//! [`HeaderName`]: struct.HeaderName.html -//! [`HeaderMap`]: struct.HeaderMap.html -//! [multimap]: https://en.wikipedia.org/wiki/Multimap -//! [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html -//! [Robin Hood hashing]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing - -mod map; -mod name; -mod value; - -pub use self::map::{ - HeaderMap, - AsHeaderName, - IntoHeaderName, - Iter, - IterMut, - Keys, - Values, - ValuesMut, - Drain, - GetAll, - Entry, - VacantEntry, - OccupiedEntry, - ValueIter, - ValueIterMut, - ValueDrain, - IntoIter, -}; -pub use self::name::{ - HeaderName, - InvalidHeaderName, - InvalidHeaderNameBytes, -}; -pub use self::value::{ - HeaderValue, - InvalidHeaderValue, - InvalidHeaderValueBytes, - ToStrError, -}; - -// Use header name constants -pub use self::name::{ - ACCEPT, - ACCEPT_CHARSET, - ACCEPT_ENCODING, - ACCEPT_LANGUAGE, - ACCEPT_RANGES, - ACCESS_CONTROL_ALLOW_CREDENTIALS, - ACCESS_CONTROL_ALLOW_HEADERS, - ACCESS_CONTROL_ALLOW_METHODS, - ACCESS_CONTROL_ALLOW_ORIGIN, - ACCESS_CONTROL_EXPOSE_HEADERS, - ACCESS_CONTROL_MAX_AGE, - ACCESS_CONTROL_REQUEST_HEADERS, - ACCESS_CONTROL_REQUEST_METHOD, - AGE, - ALLOW, - ALT_SVC, - AUTHORIZATION, - CACHE_CONTROL, - CONNECTION, - CONTENT_DISPOSITION, - CONTENT_ENCODING, - CONTENT_LANGUAGE, - CONTENT_LENGTH, - CONTENT_LOCATION, - CONTENT_RANGE, - CONTENT_SECURITY_POLICY, - CONTENT_SECURITY_POLICY_REPORT_ONLY, - CONTENT_TYPE, - COOKIE, - DNT, - DATE, - ETAG, - EXPECT, - EXPIRES, - FORWARDED, - FROM, - HOST, - IF_MATCH, - IF_MODIFIED_SINCE, - IF_NONE_MATCH, - IF_RANGE, - IF_UNMODIFIED_SINCE, - LAST_MODIFIED, - LINK, - LOCATION, - MAX_FORWARDS, - ORIGIN, - PRAGMA, - PROXY_AUTHENTICATE, - PROXY_AUTHORIZATION, - PUBLIC_KEY_PINS, - PUBLIC_KEY_PINS_REPORT_ONLY, - RANGE, - REFERER, - REFERRER_POLICY, - REFRESH, - RETRY_AFTER, - SEC_WEBSOCKET_ACCEPT, - SEC_WEBSOCKET_EXTENSIONS, - SEC_WEBSOCKET_KEY, - SEC_WEBSOCKET_PROTOCOL, - SEC_WEBSOCKET_VERSION, - SERVER, - SET_COOKIE, - STRICT_TRANSPORT_SECURITY, - TE, - TRAILER, - TRANSFER_ENCODING, - UPGRADE, - UPGRADE_INSECURE_REQUESTS, - USER_AGENT, - VARY, - VIA, - WARNING, - WWW_AUTHENTICATE, - X_CONTENT_TYPE_OPTIONS, - X_DNS_PREFETCH_CONTROL, - X_FRAME_OPTIONS, - X_XSS_PROTECTION, -}; - -/// Maximum length of a header name -/// -/// Generally, 64kb for a header name is WAY too much than would ever be needed -/// in practice. Restricting it to this size enables using `u16` values to -/// represent offsets when dealing with header names. -const MAX_HEADER_NAME_LEN: usize = 1 << 16; diff --git a/third_party/rust/http/v0_1/crate/src/header/name.rs b/third_party/rust/http/v0_1/crate/src/header/name.rs deleted file mode 100644 index df4d2a88b38c..000000000000 --- a/third_party/rust/http/v0_1/crate/src/header/name.rs +++ /dev/null @@ -1,2353 +0,0 @@ -use HttpTryFrom; -use byte_str::ByteStr; -use bytes::{Bytes, BytesMut}; - -use std::{fmt, mem}; -use std::borrow::Borrow; -use std::hash::{Hash, Hasher}; -use std::str::FromStr; -use std::error::Error; - -/// Represents an HTTP header field name -/// -/// Header field names identify the header. Header sets may include multiple -/// headers with the same name. The HTTP specification defines a number of -/// standard headers, but HTTP messages may include non-standard header names as -/// well as long as they adhere to the specification. -/// -/// `HeaderName` is used as the [`HeaderMap`] key. Constants are available for -/// all standard header names in the [`header`] module. -/// -/// # Representation -/// -/// `HeaderName` represents standard header names using an `enum`, as such they -/// will not require an allocation for storage. All custom header names are -/// lower cased upon conversion to a `HeaderName` value. This avoids the -/// overhead of dynamically doing lower case conversion during the hash code -/// computation and the comparison operation. -/// -/// [`HeaderMap`]: struct.HeaderMap.html -/// [`header`]: index.html -#[derive(Clone, Eq, PartialEq, Hash)] -pub struct HeaderName { - inner: Repr, -} - -// Almost a full `HeaderName` -#[derive(Debug, Hash)] -pub struct HdrName<'a> { - inner: Repr>, -} - -#[derive(Debug, Clone, Eq, PartialEq, Hash)] -enum Repr { - Standard(StandardHeader), - Custom(T), -} - -// Used to hijack the Hash impl -#[derive(Debug, Clone, Eq, PartialEq)] -struct Custom(ByteStr); - -#[derive(Debug, Clone)] -struct MaybeLower<'a> { - buf: &'a [u8], - lower: bool, -} - -/// A possible error when converting a `HeaderName` from another type. -pub struct InvalidHeaderName { - _priv: (), -} - -/// A possible error when converting a `HeaderName` from another type. -#[derive(Debug)] -pub struct InvalidHeaderNameBytes(InvalidHeaderName) ; - -macro_rules! standard_headers { - ( - $( - $(#[$docs:meta])* - ($konst:ident, $upcase:ident, $name:expr); - )+ - ) => { - #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] - enum StandardHeader { - $( - $konst, - )+ - } - - $( - $(#[$docs])* - pub const $upcase: HeaderName = HeaderName { - inner: Repr::Standard(StandardHeader::$konst), - }; - )+ - - impl StandardHeader { - #[inline] - fn as_str(&self) -> &'static str { - match *self { - $( - StandardHeader::$konst => $name, - )+ - } - } - } - - #[cfg(test)] - const TEST_HEADERS: &'static [(StandardHeader, &'static str)] = &[ - $( - (StandardHeader::$konst, $name), - )+ - ]; - - #[test] - fn test_parse_standard_headers() { - for &(std, name) in TEST_HEADERS { - // Test lower case - assert_eq!(HeaderName::from_bytes(name.as_bytes()).unwrap(), HeaderName::from(std)); - - // Test upper case - let upper = name.to_uppercase().to_string(); - assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(), HeaderName::from(std)); - } - } - - #[test] - fn test_standard_headers_into_bytes() { - for &(std, name) in TEST_HEADERS { - let std = HeaderName::from(std); - // Test lower case - let name_bytes = name.as_bytes(); - let bytes: Bytes = - HeaderName::from_bytes(name_bytes).unwrap().into(); - assert_eq!(bytes, name_bytes); - assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std); - - // Test upper case - let upper = name.to_uppercase().to_string(); - let bytes: Bytes = - HeaderName::from_bytes(upper.as_bytes()).unwrap().into(); - assert_eq!(bytes, name.as_bytes()); - assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(), - std); - - - } - - } - } -} - -// Generate constants for all standard HTTP headers. This includes a static hash -// code for the "fast hash" path. The hash code for static headers *do not* have -// to match the text representation of those headers. This is because header -// strings are always converted to the static values (when they match) before -// being hashed. This means that it is impossible to compare the static hash -// code of CONTENT_LENGTH with "content-length". -standard_headers! { - /// Advertises which content types the client is able to understand. - /// - /// The Accept request HTTP header advertises which content types, expressed - /// as MIME types, the client is able to understand. Using content - /// negotiation, the server then selects one of the proposals, uses it and - /// informs the client of its choice with the Content-Type response header. - /// Browsers set adequate values for this header depending of the context - /// where the request is done: when fetching a CSS stylesheet a different - /// value is set for the request than when fetching an image, video or a - /// script. - (Accept, ACCEPT, "accept"); - - /// Advertises which character set the client is able to understand. - /// - /// The Accept-Charset request HTTP header advertises which character set - /// the client is able to understand. Using content negotiation, the server - /// then selects one of the proposals, uses it and informs the client of its - /// choice within the Content-Type response header. Browsers usually don't - /// set this header as the default value for each content type is usually - /// correct and transmitting it would allow easier fingerprinting. - /// - /// If the server cannot serve any matching character set, it can - /// theoretically send back a 406 (Not Acceptable) error code. But, for a - /// better user experience, this is rarely done and the more common way is - /// to ignore the Accept-Charset header in this case. - (AcceptCharset, ACCEPT_CHARSET, "accept-charset"); - - /// Advertises which content encoding the client is able to understand. - /// - /// The Accept-Encoding request HTTP header advertises which content - /// encoding, usually a compression algorithm, the client is able to - /// understand. Using content negotiation, the server selects one of the - /// proposals, uses it and informs the client of its choice with the - /// Content-Encoding response header. - /// - /// Even if both the client and the server supports the same compression - /// algorithms, the server may choose not to compress the body of a - /// response, if the identity value is also acceptable. Two common cases - /// lead to this: - /// - /// * The data to be sent is already compressed and a second compression - /// won't lead to smaller data to be transmitted. This may the case with - /// some image formats; - /// - /// * The server is overloaded and cannot afford the computational overhead - /// induced by the compression requirement. Typically, Microsoft recommends - /// not to compress if a server use more than 80 % of its computational - /// power. - /// - /// As long as the identity value, meaning no encryption, is not explicitly - /// forbidden, by an identity;q=0 or a *;q=0 without another explicitly set - /// value for identity, the server must never send back a 406 Not Acceptable - /// error. - (AcceptEncoding, ACCEPT_ENCODING, "accept-encoding"); - - /// Advertises which languages the client is able to understand. - /// - /// The Accept-Language request HTTP header advertises which languages the - /// client is able to understand, and which locale variant is preferred. - /// Using content negotiation, the server then selects one of the proposals, - /// uses it and informs the client of its choice with the Content-Language - /// response header. Browsers set adequate values for this header according - /// their user interface language and even if a user can change it, this - /// happens rarely (and is frown upon as it leads to fingerprinting). - /// - /// This header is a hint to be used when the server has no way of - /// determining the language via another way, like a specific URL, that is - /// controlled by an explicit user decision. It is recommended that the - /// server never overrides an explicit decision. The content of the - /// Accept-Language is often out of the control of the user (like when - /// traveling and using an Internet Cafe in a different country); the user - /// may also want to visit a page in another language than the locale of - /// their user interface. - /// - /// If the server cannot serve any matching language, it can theoretically - /// send back a 406 (Not Acceptable) error code. But, for a better user - /// experience, this is rarely done and more common way is to ignore the - /// Accept-Language header in this case. - (AcceptLanguage, ACCEPT_LANGUAGE, "accept-language"); - - /// Marker used by the server to advertise partial request support. - /// - /// The Accept-Ranges response HTTP header is a marker used by the server to - /// advertise its support of partial requests. The value of this field - /// indicates the unit that can be used to define a range. - /// - /// In presence of an Accept-Ranges header, the browser may try to resume an - /// interrupted download, rather than to start it from the start again. - (AcceptRanges, ACCEPT_RANGES, "accept-ranges"); - - /// Preflight response indicating if the response to the request can be - /// exposed to the page. - /// - /// The Access-Control-Allow-Credentials response header indicates whether - /// or not the response to the request can be exposed to the page. It can be - /// exposed when the true value is returned; it can't in other cases. - /// - /// Credentials are cookies, authorization headers or TLS client - /// certificates. - /// - /// When used as part of a response to a preflight request, this indicates - /// whether or not the actual request can be made using credentials. Note - /// that simple GET requests are not preflighted, and so if a request is - /// made for a resource with credentials, if this header is not returned - /// with the resource, the response is ignored by the browser and not - /// returned to web content. - /// - /// The Access-Control-Allow-Credentials header works in conjunction with - /// the XMLHttpRequest.withCredentials property or with the credentials - /// option in the Request() constructor of the Fetch API. Credentials must - /// be set on both sides (the Access-Control-Allow-Credentials header and in - /// the XHR or Fetch request) in order for the CORS request with credentials - /// to succeed. - (AccessControlAllowCredentials, ACCESS_CONTROL_ALLOW_CREDENTIALS, "access-control-allow-credentials"); - - /// Preflight response indicating permitted HTTP headers. - /// - /// The Access-Control-Allow-Headers response header is used in response to - /// a preflight request to indicate which HTTP headers will be available via - /// Access-Control-Expose-Headers when making the actual request. - /// - /// The simple headers, Accept, Accept-Language, Content-Language, - /// Content-Type (but only with a MIME type of its parsed value (ignoring - /// parameters) of either application/x-www-form-urlencoded, - /// multipart/form-data, or text/plain), are always available and don't need - /// to be listed by this header. - /// - /// This header is required if the request has an - /// Access-Control-Request-Headers header. - (AccessControlAllowHeaders, ACCESS_CONTROL_ALLOW_HEADERS, "access-control-allow-headers"); - - /// Preflight header response indicating permitted access methods. - /// - /// The Access-Control-Allow-Methods response header specifies the method or - /// methods allowed when accessing the resource in response to a preflight - /// request. - (AccessControlAllowMethods, ACCESS_CONTROL_ALLOW_METHODS, "access-control-allow-methods"); - - /// Indicates whether the response can be shared with resources with the - /// given origin. - (AccessControlAllowOrigin, ACCESS_CONTROL_ALLOW_ORIGIN, "access-control-allow-origin"); - - /// Indicates which headers can be exposed as part of the response by - /// listing their names. - (AccessControlExposeHeaders, ACCESS_CONTROL_EXPOSE_HEADERS, "access-control-expose-headers"); - - /// Indicates how long the results of a preflight request can be cached. - (AccessControlMaxAge, ACCESS_CONTROL_MAX_AGE, "access-control-max-age"); - - /// Informs the server which HTTP headers will be used when an actual - /// request is made. - (AccessControlRequestHeaders, ACCESS_CONTROL_REQUEST_HEADERS, "access-control-request-headers"); - - /// Informs the server know which HTTP method will be used when the actual - /// request is made. - (AccessControlRequestMethod, ACCESS_CONTROL_REQUEST_METHOD, "access-control-request-method"); - - /// Indicates the time in seconds the object has been in a proxy cache. - /// - /// The Age header is usually close to zero. If it is Age: 0, it was - /// probably just fetched from the origin server; otherwise It is usually - /// calculated as a difference between the proxy's current date and the Date - /// general header included in the HTTP response. - (Age, AGE, "age"); - - /// Lists the set of methods support by a resource. - /// - /// This header must be sent if the server responds with a 405 Method Not - /// Allowed status code to indicate which request methods can be used. An - /// empty Allow header indicates that the resource allows no request - /// methods, which might occur temporarily for a given resource, for - /// example. - (Allow, ALLOW, "allow"); - - /// Advertises the availability of alternate services to clients. - (AltSvc, ALT_SVC, "alt-svc"); - - /// Contains the credentials to authenticate a user agent with a server. - /// - /// Usually this header is included after the server has responded with a - /// 401 Unauthorized status and the WWW-Authenticate header. - (Authorization, AUTHORIZATION, "authorization"); - - /// Specifies directives for caching mechanisms in both requests and - /// responses. - /// - /// Caching directives are unidirectional, meaning that a given directive in - /// a request is not implying that the same directive is to be given in the - /// response. - (CacheControl, CACHE_CONTROL, "cache-control"); - - /// Controls whether or not the network connection stays open after the - /// current transaction finishes. - /// - /// If the value sent is keep-alive, the connection is persistent and not - /// closed, allowing for subsequent requests to the same server to be done. - /// - /// Except for the standard hop-by-hop headers (Keep-Alive, - /// Transfer-Encoding, TE, Connection, Trailer, Upgrade, Proxy-Authorization - /// and Proxy-Authenticate), any hop-by-hop headers used by the message must - /// be listed in the Connection header, so that the first proxy knows he has - /// to consume them and not to forward them further. Standard hop-by-hop - /// headers can be listed too (it is often the case of Keep-Alive, but this - /// is not mandatory. - (Connection, CONNECTION, "connection"); - - /// Indicates if the content is expected to be displayed inline. - /// - /// In a regular HTTP response, the Content-Disposition response header is a - /// header indicating if the content is expected to be displayed inline in - /// the browser, that is, as a Web page or as part of a Web page, or as an - /// attachment, that is downloaded and saved locally. - /// - /// In a multipart/form-data body, the HTTP Content-Disposition general - /// header is a header that can be used on the subpart of a multipart body - /// to give information about the field it applies to. The subpart is - /// delimited by the boundary defined in the Content-Type header. Used on - /// the body itself, Content-Disposition has no effect. - /// - /// The Content-Disposition header is defined in the larger context of MIME - /// messages for e-mail, but only a subset of the possible parameters apply - /// to HTTP forms and POST requests. Only the value form-data, as well as - /// the optional directive name and filename, can be used in the HTTP - /// context. - (ContentDisposition, CONTENT_DISPOSITION, "content-disposition"); - - /// Used to compress the media-type. - /// - /// When present, its value indicates what additional content encoding has - /// been applied to the entity-body. It lets the client know, how to decode - /// in order to obtain the media-type referenced by the Content-Type header. - /// - /// It is recommended to compress data as much as possible and therefore to - /// use this field, but some types of resources, like jpeg images, are - /// already compressed. Sometimes using additional compression doesn't - /// reduce payload size and can even make the payload longer. - (ContentEncoding, CONTENT_ENCODING, "content-encoding"); - - /// Used to describe the languages intended for the audience. - /// - /// This header allows a user to differentiate according to the users' own - /// preferred language. For example, if "Content-Language: de-DE" is set, it - /// says that the document is intended for German language speakers - /// (however, it doesn't indicate the document is written in German. For - /// example, it might be written in English as part of a language course for - /// German speakers). - /// - /// If no Content-Language is specified, the default is that the content is - /// intended for all language audiences. Multiple language tags are also - /// possible, as well as applying the Content-Language header to various - /// media types and not only to textual documents. - (ContentLanguage, CONTENT_LANGUAGE, "content-language"); - - /// Indicates the size fo the entity-body. - /// - /// The header value must be a decimal indicating the number of octets sent - /// to the recipient. - (ContentLength, CONTENT_LENGTH, "content-length"); - - /// Indicates an alternate location for the returned data. - /// - /// The principal use case is to indicate the URL of the resource - /// transmitted as the result of content negotiation. - /// - /// Location and Content-Location are different: Location indicates the - /// target of a redirection (or the URL of a newly created document), while - /// Content-Location indicates the direct URL to use to access the resource, - /// without the need of further content negotiation. Location is a header - /// associated with the response, while Content-Location is associated with - /// the entity returned. - (ContentLocation, CONTENT_LOCATION, "content-location"); - - /// Indicates where in a full body message a partial message belongs. - (ContentRange, CONTENT_RANGE, "content-range"); - - /// Allows controlling resources the user agent is allowed to load for a - /// given page. - /// - /// With a few exceptions, policies mostly involve specifying server origins - /// and script endpoints. This helps guard against cross-site scripting - /// attacks (XSS). - (ContentSecurityPolicy, CONTENT_SECURITY_POLICY, "content-security-policy"); - - /// Allows experimenting with policies by monitoring their effects. - /// - /// The HTTP Content-Security-Policy-Report-Only response header allows web - /// developers to experiment with policies by monitoring (but not enforcing) - /// their effects. These violation reports consist of JSON documents sent - /// via an HTTP POST request to the specified URI. - (ContentSecurityPolicyReportOnly, CONTENT_SECURITY_POLICY_REPORT_ONLY, "content-security-policy-report-only"); - - /// Used to indicate the media type of the resource. - /// - /// In responses, a Content-Type header tells the client what the content - /// type of the returned content actually is. Browsers will do MIME sniffing - /// in some cases and will not necessarily follow the value of this header; - /// to prevent this behavior, the header X-Content-Type-Options can be set - /// to nosniff. - /// - /// In requests, (such as POST or PUT), the client tells the server what - /// type of data is actually sent. - (ContentType, CONTENT_TYPE, "content-type"); - - /// Contains stored HTTP cookies previously sent by the server with the - /// Set-Cookie header. - /// - /// The Cookie header might be omitted entirely, if the privacy setting of - /// the browser are set to block them, for example. - (Cookie, COOKIE, "cookie"); - - /// Indicates the client's tracking preference. - /// - /// This header lets users indicate whether they would prefer privacy rather - /// than personalized content. - (Dnt, DNT, "dnt"); - - /// Contains the date and time at which the message was originated. - (Date, DATE, "date"); - - /// Identifier for a specific version of a resource. - /// - /// This header allows caches to be more efficient, and saves bandwidth, as - /// a web server does not need to send a full response if the content has - /// not changed. On the other side, if the content has changed, etags are - /// useful to help prevent simultaneous updates of a resource from - /// overwriting each other ("mid-air collisions"). - /// - /// If the resource at a given URL changes, a new Etag value must be - /// generated. Etags are therefore similar to fingerprints and might also be - /// used for tracking purposes by some servers. A comparison of them allows - /// to quickly determine whether two representations of a resource are the - /// same, but they might also be set to persist indefinitely by a tracking - /// server. - (Etag, ETAG, "etag"); - - /// Indicates expectations that need to be fulfilled by the server in order - /// to properly handle the request. - /// - /// The only expectation defined in the specification is Expect: - /// 100-continue, to which the server shall respond with: - /// - /// * 100 if the information contained in the header is sufficient to cause - /// an immediate success, - /// - /// * 417 (Expectation Failed) if it cannot meet the expectation; or any - /// other 4xx status otherwise. - /// - /// For example, the server may reject a request if its Content-Length is - /// too large. - /// - /// No common browsers send the Expect header, but some other clients such - /// as cURL do so by default. - (Expect, EXPECT, "expect"); - - /// Contains the date/time after which the response is considered stale. - /// - /// Invalid dates, like the value 0, represent a date in the past and mean - /// that the resource is already expired. - /// - /// If there is a Cache-Control header with the "max-age" or "s-max-age" - /// directive in the response, the Expires header is ignored. - (Expires, EXPIRES, "expires"); - - /// Contains information from the client-facing side of proxy servers that - /// is altered or lost when a proxy is involved in the path of the request. - /// - /// The alternative and de-facto standard versions of this header are the - /// X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto headers. - /// - /// This header is used for debugging, statistics, and generating - /// location-dependent content and by design it exposes privacy sensitive - /// information, such as the IP address of the client. Therefore the user's - /// privacy must be kept in mind when deploying this header. - (Forwarded, FORWARDED, "forwarded"); - - /// Contains an Internet email address for a human user who controls the - /// requesting user agent. - /// - /// If you are running a robotic user agent (e.g. a crawler), the From - /// header should be sent, so you can be contacted if problems occur on - /// servers, such as if the robot is sending excessive, unwanted, or invalid - /// requests. - (From, FROM, "from"); - - /// Specifies the domain name of the server and (optionally) the TCP port - /// number on which the server is listening. - /// - /// If no port is given, the default port for the service requested (e.g., - /// "80" for an HTTP URL) is implied. - /// - /// A Host header field must be sent in all HTTP/1.1 request messages. A 400 - /// (Bad Request) status code will be sent to any HTTP/1.1 request message - /// that lacks a Host header field or contains more than one. - (Host, HOST, "host"); - - /// Makes a request conditional based on the E-Tag. - /// - /// For GET and HEAD methods, the server will send back the requested - /// resource only if it matches one of the listed ETags. For PUT and other - /// non-safe methods, it will only upload the resource in this case. - /// - /// The comparison with the stored ETag uses the strong comparison - /// algorithm, meaning two files are considered identical byte to byte only. - /// This is weakened when the W/ prefix is used in front of the ETag. - /// - /// There are two common use cases: - /// - /// * For GET and HEAD methods, used in combination with an Range header, it - /// can guarantee that the new ranges requested comes from the same resource - /// than the previous one. If it doesn't match, then a 416 (Range Not - /// Satisfiable) response is returned. - /// - /// * For other methods, and in particular for PUT, If-Match can be used to - /// prevent the lost update problem. It can check if the modification of a - /// resource that the user wants to upload will not override another change - /// that has been done since the original resource was fetched. If the - /// request cannot be fulfilled, the 412 (Precondition Failed) response is - /// returned. - (IfMatch, IF_MATCH, "if-match"); - - /// Makes a request conditional based on the modification date. - /// - /// The If-Modified-Since request HTTP header makes the request conditional: - /// the server will send back the requested resource, with a 200 status, - /// only if it has been last modified after the given date. If the request - /// has not been modified since, the response will be a 304 without any - /// body; the Last-Modified header will contain the date of last - /// modification. Unlike If-Unmodified-Since, If-Modified-Since can only be - /// used with a GET or HEAD. - /// - /// When used in combination with If-None-Match, it is ignored, unless the - /// server doesn't support If-None-Match. - /// - /// The most common use case is to update a cached entity that has no - /// associated ETag. - (IfModifiedSince, IF_MODIFIED_SINCE, "if-modified-since"); - - /// Makes a request conditional based on the E-Tag. - /// - /// The If-None-Match HTTP request header makes the request conditional. For - /// GET and HEAD methods, the server will send back the requested resource, - /// with a 200 status, only if it doesn't have an ETag matching the given - /// ones. For other methods, the request will be processed only if the - /// eventually existing resource's ETag doesn't match any of the values - /// listed. - /// - /// When the condition fails for GET and HEAD methods, then the server must - /// return HTTP status code 304 (Not Modified). For methods that apply - /// server-side changes, the status code 412 (Precondition Failed) is used. - /// Note that the server generating a 304 response MUST generate any of the - /// following header fields that would have been sent in a 200 (OK) response - /// to the same request: Cache-Control, Content-Location, Date, ETag, - /// Expires, and Vary. - /// - /// The comparison with the stored ETag uses the weak comparison algorithm, - /// meaning two files are considered identical not only if they are - /// identical byte to byte, but if the content is equivalent. For example, - /// two pages that would differ only by the date of generation in the footer - /// would be considered as identical. - /// - /// When used in combination with If-Modified-Since, it has precedence (if - /// the server supports it). - /// - /// There are two common use cases: - /// - /// * For `GET` and `HEAD` methods, to update a cached entity that has an associated ETag. - /// * For other methods, and in particular for `PUT`, `If-None-Match` used with - /// the `*` value can be used to save a file not known to exist, - /// guaranteeing that another upload didn't happen before, losing the data - /// of the previous put; this problems is the variation of the lost update - /// problem. - (IfNoneMatch, IF_NONE_MATCH, "if-none-match"); - - /// Makes a request conditional based on range. - /// - /// The If-Range HTTP request header makes a range request conditional: if - /// the condition is fulfilled, the range request will be issued and the - /// server sends back a 206 Partial Content answer with the appropriate - /// body. If the condition is not fulfilled, the full resource is sent back, - /// with a 200 OK status. - /// - /// This header can be used either with a Last-Modified validator, or with - /// an ETag, but not with both. - /// - /// The most common use case is to resume a download, to guarantee that the - /// stored resource has not been modified since the last fragment has been - /// received. - (IfRange, IF_RANGE, "if-range"); - - /// Makes the request conditional based on the last modification date. - /// - /// The If-Unmodified-Since request HTTP header makes the request - /// conditional: the server will send back the requested resource, or accept - /// it in the case of a POST or another non-safe method, only if it has not - /// been last modified after the given date. If the request has been - /// modified after the given date, the response will be a 412 (Precondition - /// Failed) error. - /// - /// There are two common use cases: - /// - /// * In conjunction non-safe methods, like POST, it can be used to - /// implement an optimistic concurrency control, like done by some wikis: - /// editions are rejected if the stored document has been modified since the - /// original has been retrieved. - /// - /// * In conjunction with a range request with a If-Range header, it can be - /// used to ensure that the new fragment requested comes from an unmodified - /// document. - (IfUnmodifiedSince, IF_UNMODIFIED_SINCE, "if-unmodified-since"); - - /// Content-Types that are acceptable for the response. - (LastModified, LAST_MODIFIED, "last-modified"); - - /// Allows the server to point an interested client to another resource - /// containing metadata about the requested resource. - (Link, LINK, "link"); - - /// Indicates the URL to redirect a page to. - /// - /// The Location response header indicates the URL to redirect a page to. It - /// only provides a meaning when served with a 3xx status response. - /// - /// The HTTP method used to make the new request to fetch the page pointed - /// to by Location depends of the original method and of the kind of - /// redirection: - /// - /// * If 303 (See Also) responses always lead to the use of a GET method, - /// 307 (Temporary Redirect) and 308 (Permanent Redirect) don't change the - /// method used in the original request; - /// - /// * 301 (Permanent Redirect) and 302 (Found) doesn't change the method - /// most of the time, though older user-agents may (so you basically don't - /// know). - /// - /// All responses with one of these status codes send a Location header. - /// - /// Beside redirect response, messages with 201 (Created) status also - /// include the Location header. It indicates the URL to the newly created - /// resource. - /// - /// Location and Content-Location are different: Location indicates the - /// target of a redirection (or the URL of a newly created resource), while - /// Content-Location indicates the direct URL to use to access the resource - /// when content negotiation happened, without the need of further content - /// negotiation. Location is a header associated with the response, while - /// Content-Location is associated with the entity returned. - (Location, LOCATION, "location"); - - /// Indicates the max number of intermediaries the request should be sent - /// through. - (MaxForwards, MAX_FORWARDS, "max-forwards"); - - /// Indicates where a fetch originates from. - /// - /// It doesn't include any path information, but only the server name. It is - /// sent with CORS requests, as well as with POST requests. It is similar to - /// the Referer header, but, unlike this header, it doesn't disclose the - /// whole path. - (Origin, ORIGIN, "origin"); - - /// HTTP/1.0 header usually used for backwards compatibility. - /// - /// The Pragma HTTP/1.0 general header is an implementation-specific header - /// that may have various effects along the request-response chain. It is - /// used for backwards compatibility with HTTP/1.0 caches where the - /// Cache-Control HTTP/1.1 header is not yet present. - (Pragma, PRAGMA, "pragma"); - - /// Defines the authentication method that should be used to gain access to - /// a proxy. - /// - /// Unlike `www-authenticate`, the `proxy-authenticate` header field applies - /// only to the next outbound client on the response chain. This is because - /// only the client that chose a given proxy is likely to have the - /// credentials necessary for authentication. However, when multiple proxies - /// are used within the same administrative domain, such as office and - /// regional caching proxies within a large corporate network, it is common - /// for credentials to be generated by the user agent and passed through the - /// hierarchy until consumed. Hence, in such a configuration, it will appear - /// as if Proxy-Authenticate is being forwarded because each proxy will send - /// the same challenge set. - /// - /// The `proxy-authenticate` header is sent along with a `407 Proxy - /// Authentication Required`. - (ProxyAuthenticate, PROXY_AUTHENTICATE, "proxy-authenticate"); - - /// Contains the credentials to authenticate a user agent to a proxy server. - /// - /// This header is usually included after the server has responded with a - /// 407 Proxy Authentication Required status and the Proxy-Authenticate - /// header. - (ProxyAuthorization, PROXY_AUTHORIZATION, "proxy-authorization"); - - /// Associates a specific cryptographic public key with a certain server. - /// - /// This decreases the risk of MITM attacks with forged certificates. If one - /// or several keys are pinned and none of them are used by the server, the - /// browser will not accept the response as legitimate, and will not display - /// it. - (PublicKeyPins, PUBLIC_KEY_PINS, "public-key-pins"); - - /// Sends reports of pinning violation to the report-uri specified in the - /// header. - /// - /// Unlike `Public-Key-Pins`, this header still allows browsers to connect - /// to the server if the pinning is violated. - (PublicKeyPinsReportOnly, PUBLIC_KEY_PINS_REPORT_ONLY, "public-key-pins-report-only"); - - /// Indicates the part of a document that the server should return. - /// - /// Several parts can be requested with one Range header at once, and the - /// server may send back these ranges in a multipart document. If the server - /// sends back ranges, it uses the 206 Partial Content for the response. If - /// the ranges are invalid, the server returns the 416 Range Not Satisfiable - /// error. The server can also ignore the Range header and return the whole - /// document with a 200 status code. - (Range, RANGE, "range"); - - /// Contains the address of the previous web page from which a link to the - /// currently requested page was followed. - /// - /// The Referer header allows servers to identify where people are visiting - /// them from and may use that data for analytics, logging, or optimized - /// caching, for example. - (Referer, REFERER, "referer"); - - /// Governs which referrer information should be included with requests - /// made. - (ReferrerPolicy, REFERRER_POLICY, "referrer-policy"); - - /// Informs the web browser that the current page or frame should be - /// refreshed. - (Refresh, REFRESH, "refresh"); - - /// The Retry-After response HTTP header indicates how long the user agent - /// should wait before making a follow-up request. There are two main cases - /// this header is used: - /// - /// * When sent with a 503 (Service Unavailable) response, it indicates how - /// long the service is expected to be unavailable. - /// - /// * When sent with a redirect response, such as 301 (Moved Permanently), - /// it indicates the minimum time that the user agent is asked to wait - /// before issuing the redirected request. - (RetryAfter, RETRY_AFTER, "retry-after"); - - /// The |Sec-WebSocket-Accept| header field is used in the WebSocket - /// opening handshake. It is sent from the server to the client to - /// confirm that the server is willing to initiate the WebSocket - /// connection. - (SecWebSocketAccept, SEC_WEBSOCKET_ACCEPT, "sec-websocket-accept"); - - /// The |Sec-WebSocket-Extensions| header field is used in the WebSocket - /// opening handshake. It is initially sent from the client to the - /// server, and then subsequently sent from the server to the client, to - /// agree on a set of protocol-level extensions to use for the duration - /// of the connection. - (SecWebSocketExtensions, SEC_WEBSOCKET_EXTENSIONS, "sec-websocket-extensions"); - - /// The |Sec-WebSocket-Key| header field is used in the WebSocket opening - /// handshake. It is sent from the client to the server to provide part - /// of the information used by the server to prove that it received a - /// valid WebSocket opening handshake. This helps ensure that the server - /// does not accept connections from non-WebSocket clients (e.g., HTTP - /// clients) that are being abused to send data to unsuspecting WebSocket - /// servers. - (SecWebSocketKey, SEC_WEBSOCKET_KEY, "sec-websocket-key"); - - /// The |Sec-WebSocket-Protocol| header field is used in the WebSocket - /// opening handshake. It is sent from the client to the server and back - /// from the server to the client to confirm the subprotocol of the - /// connection. This enables scripts to both select a subprotocol and be - /// sure that the server agreed to serve that subprotocol. - (SecWebSocketProtocol, SEC_WEBSOCKET_PROTOCOL, "sec-websocket-protocol"); - - /// The |Sec-WebSocket-Version| header field is used in the WebSocket - /// opening handshake. It is sent from the client to the server to - /// indicate the protocol version of the connection. This enables - /// servers to correctly interpret the opening handshake and subsequent - /// data being sent from the data, and close the connection if the server - /// cannot interpret that data in a safe manner. - (SecWebSocketVersion, SEC_WEBSOCKET_VERSION, "sec-websocket-version"); - - /// Contains information about the software used by the origin server to - /// handle the request. - /// - /// Overly long and detailed Server values should be avoided as they - /// potentially reveal internal implementation details that might make it - /// (slightly) easier for attackers to find and exploit known security - /// holes. - (Server, SERVER, "server"); - - /// Used to send cookies from the server to the user agent. - (SetCookie, SET_COOKIE, "set-cookie"); - - /// Tells the client to communicate with HTTPS instead of using HTTP. - (StrictTransportSecurity, STRICT_TRANSPORT_SECURITY, "strict-transport-security"); - - /// Informs the server of transfer encodings willing to be accepted as part - /// of the response. - /// - /// See also the Transfer-Encoding response header for more details on - /// transfer encodings. Note that chunked is always acceptable for HTTP/1.1 - /// recipients and you that don't have to specify "chunked" using the TE - /// header. However, it is useful for setting if the client is accepting - /// trailer fields in a chunked transfer coding using the "trailers" value. - (Te, TE, "te"); - - /// Allows the sender to include additional fields at the end of chunked - /// messages. - (Trailer, TRAILER, "trailer"); - - /// Specifies the form of encoding used to safely transfer the entity to the - /// client. - /// - /// `transfer-encoding` is a hop-by-hop header, that is applying to a - /// message between two nodes, not to a resource itself. Each segment of a - /// multi-node connection can use different `transfer-encoding` values. If - /// you want to compress data over the whole connection, use the end-to-end - /// header `content-encoding` header instead. - /// - /// When present on a response to a `HEAD` request that has no body, it - /// indicates the value that would have applied to the corresponding `GET` - /// message. - (TransferEncoding, TRANSFER_ENCODING, "transfer-encoding"); - - /// Contains a string that allows identifying the requesting client's - /// software. - (UserAgent, USER_AGENT, "user-agent"); - - /// Used as part of the exchange to upgrade the protocol. - (Upgrade, UPGRADE, "upgrade"); - - /// Sends a signal to the server expressing the client’s preference for an - /// encrypted and authenticated response. - (UpgradeInsecureRequests, UPGRADE_INSECURE_REQUESTS, "upgrade-insecure-requests"); - - /// Determines how to match future requests with cached responses. - /// - /// The `vary` HTTP response header determines how to match future request - /// headers to decide whether a cached response can be used rather than - /// requesting a fresh one from the origin server. It is used by the server - /// to indicate which headers it used when selecting a representation of a - /// resource in a content negotiation algorithm. - /// - /// The `vary` header should be set on a 304 Not Modified response exactly - /// like it would have been set on an equivalent 200 OK response. - (Vary, VARY, "vary"); - - /// Added by proxies to track routing. - /// - /// The `via` general header is added by proxies, both forward and reverse - /// proxies, and can appear in the request headers and the response headers. - /// It is used for tracking message forwards, avoiding request loops, and - /// identifying the protocol capabilities of senders along the - /// request/response chain. - (Via, VIA, "via"); - - /// General HTTP header contains information about possible problems with - /// the status of the message. - /// - /// More than one `warning` header may appear in a response. Warning header - /// fields can in general be applied to any message, however some warn-codes - /// are specific to caches and can only be applied to response messages. - (Warning, WARNING, "warning"); - - /// Defines the authentication method that should be used to gain access to - /// a resource. - (WwwAuthenticate, WWW_AUTHENTICATE, "www-authenticate"); - - /// Marker used by the server to indicate that the MIME types advertised in - /// the `content-type` headers should not be changed and be followed. - /// - /// This allows to opt-out of MIME type sniffing, or, in other words, it is - /// a way to say that the webmasters knew what they were doing. - /// - /// This header was introduced by Microsoft in IE 8 as a way for webmasters - /// to block content sniffing that was happening and could transform - /// non-executable MIME types into executable MIME types. Since then, other - /// browsers have introduced it, even if their MIME sniffing algorithms were - /// less aggressive. - /// - /// Site security testers usually expect this header to be set. - (XContentTypeOptions, X_CONTENT_TYPE_OPTIONS, "x-content-type-options"); - - /// Controls DNS prefetching. - /// - /// The `x-dns-prefetch-control` HTTP response header controls DNS - /// prefetching, a feature by which browsers proactively perform domain name - /// resolution on both links that the user may choose to follow as well as - /// URLs for items referenced by the document, including images, CSS, - /// JavaScript, and so forth. - /// - /// This prefetching is performed in the background, so that the DNS is - /// likely to have been resolved by the time the referenced items are - /// needed. This reduces latency when the user clicks a link. - (XDnsPrefetchControl, X_DNS_PREFETCH_CONTROL, "x-dns-prefetch-control"); - - /// Indicates whether or not a browser should be allowed to render a page in - /// a frame. - /// - /// Sites can use this to avoid clickjacking attacks, by ensuring that their - /// content is not embedded into other sites. - /// - /// The added security is only provided if the user accessing the document - /// is using a browser supporting `x-frame-options`. - (XFrameOptions, X_FRAME_OPTIONS, "x-frame-options"); - - /// Stop pages from loading when an XSS attack is detected. - /// - /// The HTTP X-XSS-Protection response header is a feature of Internet - /// Explorer, Chrome and Safari that stops pages from loading when they - /// detect reflected cross-site scripting (XSS) attacks. Although these - /// protections are largely unnecessary in modern browsers when sites - /// implement a strong Content-Security-Policy that disables the use of - /// inline JavaScript ('unsafe-inline'), they can still provide protections - /// for users of older web browsers that don't yet support CSP. - (XXssProtection, X_XSS_PROTECTION, "x-xss-protection"); -} - -/// Valid header name characters -/// -/// ```not_rust -/// field-name = token -/// separators = "(" | ")" | "<" | ">" | "@" -/// | "," | ";" | ":" | "\" | <"> -/// | "/" | "[" | "]" | "?" | "=" -/// | "{" | "}" | SP | HT -/// token = 1*tchar -/// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" -/// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" -/// / DIGIT / ALPHA -/// ; any VCHAR, except delimiters -/// ``` -const HEADER_CHARS: [u8; 256] = [ - // 0 1 2 3 4 5 6 7 8 9 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x - 0, 0, 0, b'!', b'"', b'#', b'$', b'%', b'&', b'\'', // 3x - 0, 0, b'*', b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x - b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0, // 5x - 0, 0, 0, 0, 0, b'a', b'b', b'c', b'd', b'e', // 6x - b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', // 7x - b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', // 8x - b'z', 0, 0, 0, b'^', b'_', b'`', b'a', b'b', b'c', // 9x - b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x - b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x - b'x', b'y', b'z', 0, b'|', 0, b'~', 0, 0, 0, // 12x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x - 0, 0, 0, 0, 0, 0 // 25x -]; - -const HEADER_CHARS_H2: [u8; 256] = [ - // 0 1 2 3 4 5 6 7 8 9 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x - 0, 0, 0, b'!', b'"', b'#', b'$', b'%', b'&', b'\'', // 3x - 0, 0, b'*', b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x - b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0, // 5x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x - 0, 0, 0, 0, b'^', b'_', b'`', b'a', b'b', b'c', // 9x - b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x - b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x - b'x', b'y', b'z', 0, b'|', 0, b'~', 0, 0, 0, // 12x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x - 0, 0, 0, 0, 0, 0 // 25x -]; - -#[cfg(any(not(debug_assertions), not(target_arch = "wasm32")))] -macro_rules! eq { - (($($cmp:expr,)*) $v:ident[$n:expr] ==) => { - $($cmp) && * - }; - (($($cmp:expr,)*) $v:ident[$n:expr] == $a:tt $($rest:tt)*) => { - eq!(($($cmp,)* $v[$n] == $a,) $v[$n+1] == $($rest)*) - }; - ($v:ident == $($rest:tt)+) => { - eq!(() $v[0] == $($rest)+) - }; - ($v:ident[$n:expr] == $($rest:tt)+) => { - eq!(() $v[$n] == $($rest)+) - }; -} - -#[cfg(any(not(debug_assertions), not(target_arch = "wasm32")))] -/// This version is best under optimized mode, however in a wasm debug compile, -/// the `eq` macro expands to 1 + 1 + 1 + 1... and wasm explodes when this chain gets too long -/// See https://github.com/DenisKolodin/yew/issues/478 -fn parse_hdr<'a>(data: &'a [u8], b: &'a mut [u8; 64], table: &[u8; 256]) - -> Result, InvalidHeaderName> -{ - use self::StandardHeader::*; - - let len = data.len(); - - let validate = |buf: &'a [u8], len: usize| { - let buf = &buf[..len]; - if buf.iter().any(|&b| b == 0) { - Err(InvalidHeaderName::new()) - } else { - Ok(HdrName::custom(buf, true)) - } - }; - - - macro_rules! to_lower { - ($d:ident, $src:ident, 1) => { $d[0] = table[$src[0] as usize]; }; - ($d:ident, $src:ident, 2) => { to_lower!($d, $src, 1); $d[1] = table[$src[1] as usize]; }; - ($d:ident, $src:ident, 3) => { to_lower!($d, $src, 2); $d[2] = table[$src[2] as usize]; }; - ($d:ident, $src:ident, 4) => { to_lower!($d, $src, 3); $d[3] = table[$src[3] as usize]; }; - ($d:ident, $src:ident, 5) => { to_lower!($d, $src, 4); $d[4] = table[$src[4] as usize]; }; - ($d:ident, $src:ident, 6) => { to_lower!($d, $src, 5); $d[5] = table[$src[5] as usize]; }; - ($d:ident, $src:ident, 7) => { to_lower!($d, $src, 6); $d[6] = table[$src[6] as usize]; }; - ($d:ident, $src:ident, 8) => { to_lower!($d, $src, 7); $d[7] = table[$src[7] as usize]; }; - ($d:ident, $src:ident, 9) => { to_lower!($d, $src, 8); $d[8] = table[$src[8] as usize]; }; - ($d:ident, $src:ident, 10) => { to_lower!($d, $src, 9); $d[9] = table[$src[9] as usize]; }; - ($d:ident, $src:ident, 11) => { to_lower!($d, $src, 10); $d[10] = table[$src[10] as usize]; }; - ($d:ident, $src:ident, 12) => { to_lower!($d, $src, 11); $d[11] = table[$src[11] as usize]; }; - ($d:ident, $src:ident, 13) => { to_lower!($d, $src, 12); $d[12] = table[$src[12] as usize]; }; - ($d:ident, $src:ident, 14) => { to_lower!($d, $src, 13); $d[13] = table[$src[13] as usize]; }; - ($d:ident, $src:ident, 15) => { to_lower!($d, $src, 14); $d[14] = table[$src[14] as usize]; }; - ($d:ident, $src:ident, 16) => { to_lower!($d, $src, 15); $d[15] = table[$src[15] as usize]; }; - ($d:ident, $src:ident, 17) => { to_lower!($d, $src, 16); $d[16] = table[$src[16] as usize]; }; - ($d:ident, $src:ident, 18) => { to_lower!($d, $src, 17); $d[17] = table[$src[17] as usize]; }; - ($d:ident, $src:ident, 19) => { to_lower!($d, $src, 18); $d[18] = table[$src[18] as usize]; }; - ($d:ident, $src:ident, 20) => { to_lower!($d, $src, 19); $d[19] = table[$src[19] as usize]; }; - ($d:ident, $src:ident, 21) => { to_lower!($d, $src, 20); $d[20] = table[$src[20] as usize]; }; - ($d:ident, $src:ident, 22) => { to_lower!($d, $src, 21); $d[21] = table[$src[21] as usize]; }; - ($d:ident, $src:ident, 23) => { to_lower!($d, $src, 22); $d[22] = table[$src[22] as usize]; }; - ($d:ident, $src:ident, 24) => { to_lower!($d, $src, 23); $d[23] = table[$src[23] as usize]; }; - ($d:ident, $src:ident, 25) => { to_lower!($d, $src, 24); $d[24] = table[$src[24] as usize]; }; - ($d:ident, $src:ident, 26) => { to_lower!($d, $src, 25); $d[25] = table[$src[25] as usize]; }; - ($d:ident, $src:ident, 27) => { to_lower!($d, $src, 26); $d[26] = table[$src[26] as usize]; }; - ($d:ident, $src:ident, 28) => { to_lower!($d, $src, 27); $d[27] = table[$src[27] as usize]; }; - ($d:ident, $src:ident, 29) => { to_lower!($d, $src, 28); $d[28] = table[$src[28] as usize]; }; - ($d:ident, $src:ident, 30) => { to_lower!($d, $src, 29); $d[29] = table[$src[29] as usize]; }; - ($d:ident, $src:ident, 31) => { to_lower!($d, $src, 30); $d[30] = table[$src[30] as usize]; }; - ($d:ident, $src:ident, 32) => { to_lower!($d, $src, 31); $d[31] = table[$src[31] as usize]; }; - ($d:ident, $src:ident, 33) => { to_lower!($d, $src, 32); $d[32] = table[$src[32] as usize]; }; - ($d:ident, $src:ident, 34) => { to_lower!($d, $src, 33); $d[33] = table[$src[33] as usize]; }; - ($d:ident, $src:ident, 35) => { to_lower!($d, $src, 34); $d[34] = table[$src[34] as usize]; }; - } - - assert!(len < super::MAX_HEADER_NAME_LEN, - "header name too long -- max length is {}", - super::MAX_HEADER_NAME_LEN); - - match len { - 0 => { - Err(InvalidHeaderName::new()) - } - 2 => { - to_lower!(b, data, 2); - - if eq!(b == b't' b'e') { - Ok(Te.into()) - } else { - validate(b, len) - } - } - 3 => { - to_lower!(b, data, 3); - - if eq!(b == b'a' b'g' b'e') { - Ok(Age.into()) - } else if eq!(b == b'v' b'i' b'a') { - Ok(Via.into()) - } else if eq!(b == b'd' b'n' b't') { - Ok(Dnt.into()) - } else { - validate(b, len) - } - } - 4 => { - to_lower!(b, data, 4); - - if eq!(b == b'd' b'a' b't' b'e') { - Ok(Date.into()) - } else if eq!(b == b'e' b't' b'a' b'g') { - Ok(Etag.into()) - } else if eq!(b == b'f' b'r' b'o' b'm') { - Ok(From.into()) - } else if eq!(b == b'h' b'o' b's' b't') { - Ok(Host.into()) - } else if eq!(b == b'l' b'i' b'n' b'k') { - Ok(Link.into()) - } else if eq!(b == b'v' b'a' b'r' b'y') { - Ok(Vary.into()) - } else { - validate(b, len) - } - } - 5 => { - to_lower!(b, data, 5); - - if eq!(b == b'a' b'l' b'l' b'o' b'w') { - Ok(Allow.into()) - } else if eq!(b == b'r' b'a' b'n' b'g' b'e') { - Ok(Range.into()) - } else { - validate(b, len) - } - } - 6 => { - to_lower!(b, data, 6); - - if eq!(b == b'a' b'c' b'c' b'e' b'p' b't') { - return Ok(Accept.into()) - } else if eq!(b == b'c' b'o' b'o' b'k' b'i' b'e') { - return Ok(Cookie.into()) - } else if eq!(b == b'e' b'x' b'p' b'e' b'c' b't') { - return Ok(Expect.into()) - } else if eq!(b == b'o' b'r' b'i' b'g' b'i' b'n') { - return Ok(Origin.into()) - } else if eq!(b == b'p' b'r' b'a' b'g' b'm' b'a') { - return Ok(Pragma.into()) - } else if b[0] == b's' { - if eq!(b[1] == b'e' b'r' b'v' b'e' b'r') { - return Ok(Server.into()) - } - } - - validate(b, len) - } - 7 => { - to_lower!(b, data, 7); - - if eq!(b == b'a' b'l' b't' b'-' b's' b'v' b'c') { - Ok(AltSvc.into()) - } else if eq!(b == b'e' b'x' b'p' b'i' b'r' b'e' b's') { - Ok(Expires.into()) - } else if eq!(b == b'r' b'e' b'f' b'e' b'r' b'e' b'r') { - Ok(Referer.into()) - } else if eq!(b == b'r' b'e' b'f' b'r' b'e' b's' b'h') { - Ok(Refresh.into()) - } else if eq!(b == b't' b'r' b'a' b'i' b'l' b'e' b'r') { - Ok(Trailer.into()) - } else if eq!(b == b'u' b'p' b'g' b'r' b'a' b'd' b'e') { - Ok(Upgrade.into()) - } else if eq!(b == b'w' b'a' b'r' b'n' b'i' b'n' b'g') { - Ok(Warning.into()) - } else { - validate(b, len) - } - } - 8 => { - to_lower!(b, data, 8); - - if eq!(b == b'i' b'f' b'-') { - if eq!(b[3] == b'm' b'a' b't' b'c' b'h') { - return Ok(IfMatch.into()) - } else if eq!(b[3] == b'r' b'a' b'n' b'g' b'e') { - return Ok(IfRange.into()) - } - } else if eq!(b == b'l' b'o' b'c' b'a' b't' b'i' b'o' b'n') { - return Ok(Location.into()) - } - - validate(b, len) - } - 9 => { - to_lower!(b, data, 9); - - if eq!(b == b'f' b'o' b'r' b'w' b'a' b'r' b'd' b'e' b'd') { - Ok(Forwarded.into()) - } else { - validate(b, len) - } - } - 10 => { - to_lower!(b, data, 10); - - if eq!(b == b'c' b'o' b'n' b'n' b'e' b'c' b't' b'i' b'o' b'n') { - Ok(Connection.into()) - } else if eq!(b == b's' b'e' b't' b'-' b'c' b'o' b'o' b'k' b'i' b'e') { - Ok(SetCookie.into()) - } else if eq!(b == b'u' b's' b'e' b'r' b'-' b'a' b'g' b'e' b'n' b't') { - Ok(UserAgent.into()) - } else { - validate(b, len) - } - } - 11 => { - to_lower!(b, data, 11); - - if eq!(b == b'r' b'e' b't' b'r' b'y' b'-' b'a' b'f' b't' b'e' b'r') { - Ok(RetryAfter.into()) - } else { - validate(b, len) - } - } - 12 => { - to_lower!(b, data, 12); - - if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b't' b'y' b'p' b'e') { - Ok(ContentType.into()) - } else if eq!(b == b'm' b'a' b'x' b'-' b'f' b'o' b'r' b'w' b'a' b'r' b'd' b's') { - Ok(MaxForwards.into()) - } else { - validate(b, len) - } - } - 13 => { - to_lower!(b, data, 13); - - if b[0] == b'a' { - if eq!(b[1] == b'c' b'c' b'e' b'p' b't' b'-' b'r' b'a' b'n' b'g' b'e' b's') { - return Ok(AcceptRanges.into()) - } else if eq!(b[1] == b'u' b't' b'h' b'o' b'r' b'i' b'z' b'a' b't' b'i' b'o' b'n') { - return Ok(Authorization.into()) - } - } else if b[0] == b'c' { - if eq!(b[1] == b'a' b'c' b'h' b'e' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l') { - return Ok(CacheControl.into()) - } else if eq!(b[1] == b'o' b'n' b't' b'e' b'n' b't' b'-' b'r' b'a' b'n' b'g' b'e' ) { - return Ok(ContentRange.into()) - } - } else if eq!(b == b'i' b'f' b'-' b'n' b'o' b'n' b'e' b'-' b'm' b'a' b't' b'c' b'h') { - return Ok(IfNoneMatch.into()) - } else if eq!(b == b'l' b'a' b's' b't' b'-' b'm' b'o' b'd' b'i' b'f' b'i' b'e' b'd') { - return Ok(LastModified.into()) - } - - validate(b, len) - } - 14 => { - to_lower!(b, data, 14); - - if eq!(b == b'a' b'c' b'c' b'e' b'p' b't' b'-' b'c' b'h' b'a' b'r' b's' b'e' b't') { - Ok(AcceptCharset.into()) - } else if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b'l' b'e' b'n' b'g' b't' b'h') { - Ok(ContentLength.into()) - } else { - validate(b, len) - } - } - 15 => { - to_lower!(b, data, 15); - - if eq!(b == b'a' b'c' b'c' b'e' b'p' b't' b'-') { // accept- - if eq!(b[7] == b'e' b'n' b'c' b'o' b'd' b'i' b'n' b'g') { - return Ok(AcceptEncoding.into()) - } else if eq!(b[7] == b'l' b'a' b'n' b'g' b'u' b'a' b'g' b'e') { - return Ok(AcceptLanguage.into()) - } - } else if eq!(b == b'p' b'u' b'b' b'l' b'i' b'c' b'-' b'k' b'e' b'y' b'-' b'p' b'i' b'n' b's') { - return Ok(PublicKeyPins.into()) - } else if eq!(b == b'x' b'-' b'f' b'r' b'a' b'm' b'e' b'-' b'o' b'p' b't' b'i' b'o' b'n' b's') { - return Ok(XFrameOptions.into()) - } - else if eq!(b == b'r' b'e' b'f' b'e' b'r' b'r' b'e' b'r' b'-' b'p' b'o' b'l' b'i' b'c' b'y') { - return Ok(ReferrerPolicy.into()) - } - - validate(b, len) - } - 16 => { - to_lower!(b, data, 16); - - if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-') { - if eq!(b[8] == b'l' b'a' b'n' b'g' b'u' b'a' b'g' b'e') { - return Ok(ContentLanguage.into()) - } else if eq!(b[8] == b'l' b'o' b'c' b'a' b't' b'i' b'o' b'n') { - return Ok(ContentLocation.into()) - } else if eq!(b[8] == b'e' b'n' b'c' b'o' b'd' b'i' b'n' b'g') { - return Ok(ContentEncoding.into()) - } - } else if eq!(b == b'w' b'w' b'w' b'-' b'a' b'u' b't' b'h' b'e' b'n' b't' b'i' b'c' b'a' b't' b'e') { - return Ok(WwwAuthenticate.into()) - } else if eq!(b == b'x' b'-' b'x' b's' b's' b'-' b'p' b'r' b'o' b't' b'e' b'c' b't' b'i' b'o' b'n') { - return Ok(XXssProtection.into()) - } - - validate(b, len) - } - 17 => { - to_lower!(b, data, 17); - - if eq!(b == b't' b'r' b'a' b'n' b's' b'f' b'e' b'r' b'-' b'e' b'n' b'c' b'o' b'd' b'i' b'n' b'g') { - Ok(TransferEncoding.into()) - } else if eq!(b == b'i' b'f' b'-' b'm' b'o' b'd' b'i' b'f' b'i' b'e' b'd' b'-' b's' b'i' b'n' b'c' b'e') { - Ok(IfModifiedSince.into()) - } else if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'k' b'e' b'y') { - Ok(SecWebSocketKey.into()) - } else { - validate(b, len) - } - } - 18 => { - to_lower!(b, data, 18); - - if eq!(b == b'p' b'r' b'o' b'x' b'y' b'-' b'a' b'u' b't' b'h' b'e' b'n' b't' b'i' b'c' b'a' b't' b'e') { - Ok(ProxyAuthenticate.into()) - } else { - validate(b, len) - } - } - 19 => { - to_lower!(b, data, 19); - - if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b'd' b'i' b's' b'p' b'o' b's' b'i' b't' b'i' b'o' b'n') { - Ok(ContentDisposition.into()) - } else if eq!(b == b'i' b'f' b'-' b'u' b'n' b'm' b'o' b'd' b'i' b'f' b'i' b'e' b'd' b'-' b's' b'i' b'n' b'c' b'e') { - Ok(IfUnmodifiedSince.into()) - } else if eq!(b == b'p' b'r' b'o' b'x' b'y' b'-' b'a' b'u' b't' b'h' b'o' b'r' b'i' b'z' b'a' b't' b'i' b'o' b'n') { - Ok(ProxyAuthorization.into()) - } else { - validate(b, len) - } - } - 20 => { - to_lower!(b, data, 20); - - if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'a' b'c' b'c' b'e' b'p' b't') { - Ok(SecWebSocketAccept.into()) - } else { - validate(b, len) - } - } - 21 => { - to_lower!(b, data, 21); - - if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'v' b'e' b'r' b's' b'i' b'o' b'n') { - Ok(SecWebSocketVersion.into()) - } else { - validate(b, len) - } - } - 22 => { - to_lower!(b, data, 22); - - if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'm' b'a' b'x' b'-' b'a' b'g' b'e') { - Ok(AccessControlMaxAge.into()) - } else if eq!(b == b'x' b'-' b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b't' b'y' b'p' b'e' b'-' b'o' b'p' b't' b'i' b'o' b'n' b's') { - Ok(XContentTypeOptions.into()) - } else if eq!(b == b'x' b'-' b'd' b'n' b's' b'-' b'p' b'r' b'e' b'f' b'e' b't' b'c' b'h' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l') { - Ok(XDnsPrefetchControl.into()) - } else if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'p' b'r' b'o' b't' b'o' b'c' b'o' b'l') { - Ok(SecWebSocketProtocol.into()) - } else { - validate(b, len) - } - } - 23 => { - to_lower!(b, data, 23); - - if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b's' b'e' b'c' b'u' b'r' b'i' b't' b'y' b'-' b'p' b'o' b'l' b'i' b'c' b'y') { - Ok(ContentSecurityPolicy.into()) - } else { - validate(b, len) - } - } - 24 => { - to_lower!(b, data, 24); - - if eq!(b == b's' b'e' b'c' b'-' b'w' b'e' b'b' b's' b'o' b'c' b'k' b'e' b't' b'-' b'e' b'x' b't' b'e' b'n' b's' b'i' b'o' b'n' b's') { - Ok(SecWebSocketExtensions.into()) - } else { - validate(b, len) - } - } - 25 => { - to_lower!(b, data, 25); - - if eq!(b == b's' b't' b'r' b'i' b'c' b't' b'-' b't' b'r' b'a' b'n' b's' b'p' b'o' b'r' b't' b'-' b's' b'e' b'c' b'u' b'r' b'i' b't' b'y') { - Ok(StrictTransportSecurity.into()) - } else if eq!(b == b'u' b'p' b'g' b'r' b'a' b'd' b'e' b'-' b'i' b'n' b's' b'e' b'c' b'u' b'r' b'e' b'-' b'r' b'e' b'q' b'u' b'e' b's' b't' b's') { - Ok(UpgradeInsecureRequests.into()) - } else { - validate(b, len) - } - } - 27 => { - to_lower!(b, data, 27); - - if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'a' b'l' b'l' b'o' b'w' b'-' b'o' b'r' b'i' b'g' b'i' b'n') { - Ok(AccessControlAllowOrigin.into()) - } else if eq!(b == b'p' b'u' b'b' b'l' b'i' b'c' b'-' b'k' b'e' b'y' b'-' b'p' b'i' b'n' b's' b'-' b'r' b'e' b'p' b'o' b'r' b't' b'-' b'o' b'n' b'l' b'y') { - Ok(PublicKeyPinsReportOnly.into()) - } else { - validate(b, len) - } - } - 28 => { - to_lower!(b, data, 28); - - if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'a' b'l' b'l' b'o' b'w' b'-') { - if eq!(b[21] == b'h' b'e' b'a' b'd' b'e' b'r' b's') { - return Ok(AccessControlAllowHeaders.into()) - } else if eq!(b[21] == b'm' b'e' b't' b'h' b'o' b'd' b's') { - return Ok(AccessControlAllowMethods.into()) - } - } - - validate(b, len) - } - 29 => { - to_lower!(b, data, 29); - - if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-') { - if eq!(b[15] == b'e' b'x' b'p' b'o' b's' b'e' b'-' b'h' b'e' b'a' b'd' b'e' b'r' b's') { - return Ok(AccessControlExposeHeaders.into()) - } else if eq!(b[15] == b'r' b'e' b'q' b'u' b'e' b's' b't' b'-' b'm' b'e' b't' b'h' b'o' b'd') { - return Ok(AccessControlRequestMethod.into()) - } - } - - validate(b, len) - } - 30 => { - to_lower!(b, data, 30); - - if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'r' b'e' b'q' b'u' b'e' b's' b't' b'-' b'h' b'e' b'a' b'd' b'e' b'r' b's') { - Ok(AccessControlRequestHeaders.into()) - } else { - validate(b, len) - } - } - 32 => { - to_lower!(b, data, 32); - - if eq!(b == b'a' b'c' b'c' b'e' b's' b's' b'-' b'c' b'o' b'n' b't' b'r' b'o' b'l' b'-' b'a' b'l' b'l' b'o' b'w' b'-' b'c' b'r' b'e' b'd' b'e' b'n' b't' b'i' b'a' b'l' b's') { - Ok(AccessControlAllowCredentials.into()) - } else { - validate(b, len) - } - } - 35 => { - to_lower!(b, data, 35); - - if eq!(b == b'c' b'o' b'n' b't' b'e' b'n' b't' b'-' b's' b'e' b'c' b'u' b'r' b'i' b't' b'y' b'-' b'p' b'o' b'l' b'i' b'c' b'y' b'-' b'r' b'e' b'p' b'o' b'r' b't' b'-' b'o' b'n' b'l' b'y') { - Ok(ContentSecurityPolicyReportOnly.into()) - } else { - validate(b, len) - } - } - _ => { - if len < 64 { - for i in 0..len { - b[i] = table[data[i] as usize]; - } - - validate(b, len) - } else { - Ok(HdrName::custom(data, false)) - } - } - } -} - -#[cfg(all(debug_assertions, target_arch = "wasm32"))] -/// This version works best in debug mode in wasm -fn parse_hdr<'a>( - data: &'a [u8], - b: &'a mut [u8; 64], - table: &[u8; 256], -) -> Result, InvalidHeaderName> { - use self::StandardHeader::*; - - let len = data.len(); - - let validate = |buf: &'a [u8], len: usize| { - let buf = &buf[..len]; - if buf.iter().any(|&b| b == 0) { - Err(InvalidHeaderName::new()) - } else { - Ok(HdrName::custom(buf, true)) - } - }; - - assert!( - len < super::MAX_HEADER_NAME_LEN, - "header name too long -- max length is {}", - super::MAX_HEADER_NAME_LEN - ); - - match len { - 0 => Err(InvalidHeaderName::new()), - len if len > 64 => Ok(HdrName::custom(data, false)), - len => { - // Read from data into the buffer - transforming using `table` as we go - data.iter().zip(b.iter_mut()).for_each(|(index, out)| *out = table[*index as usize]); - match &b[0..len] { - b"te" => Ok(Te.into()), - b"age" => Ok(Age.into()), - b"via" => Ok(Via.into()), - b"dnt" => Ok(Dnt.into()), - b"date" => Ok(Date.into()), - b"etag" => Ok(Etag.into()), - b"from" => Ok(From.into()), - b"host" => Ok(Host.into()), - b"link" => Ok(Link.into()), - b"vary" => Ok(Vary.into()), - b"allow" => Ok(Allow.into()), - b"range" => Ok(Range.into()), - b"accept" => Ok(Accept.into()), - b"cookie" => Ok(Cookie.into()), - b"expect" => Ok(Expect.into()), - b"origin" => Ok(Origin.into()), - b"pragma" => Ok(Pragma.into()), - b"server" => Ok(Server.into()), - b"alt-svc" => Ok(AltSvc.into()), - b"expires" => Ok(Expires.into()), - b"referer" => Ok(Referer.into()), - b"refresh" => Ok(Refresh.into()), - b"trailer" => Ok(Trailer.into()), - b"upgrade" => Ok(Upgrade.into()), - b"warning" => Ok(Warning.into()), - b"if-match" => Ok(IfMatch.into()), - b"if-range" => Ok(IfRange.into()), - b"location" => Ok(Location.into()), - b"forwarded" => Ok(Forwarded.into()), - b"connection" => Ok(Connection.into()), - b"set-cookie" => Ok(SetCookie.into()), - b"user-agent" => Ok(UserAgent.into()), - b"retry-after" => Ok(RetryAfter.into()), - b"content-type" => Ok(ContentType.into()), - b"max-forwards" => Ok(MaxForwards.into()), - b"accept-ranges" => Ok(AcceptRanges.into()), - b"authorization" => Ok(Authorization.into()), - b"cache-control" => Ok(CacheControl.into()), - b"content-range" => Ok(ContentRange.into()), - b"if-none-match" => Ok(IfNoneMatch.into()), - b"last-modified" => Ok(LastModified.into()), - b"accept-charset" => Ok(AcceptCharset.into()), - b"content-length" => Ok(ContentLength.into()), - b"accept-encoding" => Ok(AcceptEncoding.into()), - b"accept-language" => Ok(AcceptLanguage.into()), - b"public-key-pins" => Ok(PublicKeyPins.into()), - b"x-frame-options" => Ok(XFrameOptions.into()), - b"referrer-policy" => Ok(ReferrerPolicy.into()), - b"content-language" => Ok(ContentLanguage.into()), - b"content-location" => Ok(ContentLocation.into()), - b"content-encoding" => Ok(ContentEncoding.into()), - b"www-authenticate" => Ok(WwwAuthenticate.into()), - b"x-xss-protection" => Ok(XXssProtection.into()), - b"transfer-encoding" => Ok(TransferEncoding.into()), - b"if-modified-since" => Ok(IfModifiedSince.into()), - b"sec-websocket-key" => Ok(SecWebSocketKey.into()), - b"proxy-authenticate" => Ok(ProxyAuthenticate.into()), - b"content-disposition" => Ok(ContentDisposition.into()), - b"if-unmodified-since" => Ok(IfUnmodifiedSince.into()), - b"proxy-authorization" => Ok(ProxyAuthorization.into()), - b"sec-websocket-accept" => Ok(SecWebSocketAccept.into()), - b"sec-websocket-version" => Ok(SecWebSocketVersion.into()), - b"access-control-max-age" => Ok(AccessControlMaxAge.into()), - b"x-content-type-options" => Ok(XContentTypeOptions.into()), - b"x-dns-prefetch-control" => Ok(XDnsPrefetchControl.into()), - b"sec-websocket-protocol" => Ok(SecWebSocketProtocol.into()), - b"content-security-policy" => Ok(ContentSecurityPolicy.into()), - b"sec-websocket-extensions" => Ok(SecWebSocketExtensions.into()), - b"strict-transport-security" => Ok(StrictTransportSecurity.into()), - b"upgrade-insecure-requests" => Ok(UpgradeInsecureRequests.into()), - b"access-control-allow-origin" => Ok(AccessControlAllowOrigin.into()), - b"public-key-pins-report-only" => Ok(PublicKeyPinsReportOnly.into()), - b"access-control-allow-headers" => Ok(AccessControlAllowHeaders.into()), - b"access-control-allow-methods" => Ok(AccessControlAllowMethods.into()), - b"access-control-expose-headers" => Ok(AccessControlExposeHeaders.into()), - b"access-control-request-method" => Ok(AccessControlRequestMethod.into()), - b"access-control-request-headers" => Ok(AccessControlRequestHeaders.into()), - b"access-control-allow-credentials" => Ok(AccessControlAllowCredentials.into()), - b"content-security-policy-report-only" => { - Ok(ContentSecurityPolicyReportOnly.into()) - } - other => validate(other, len), - } - } - } -} - - - -impl<'a> From for HdrName<'a> { - fn from(hdr: StandardHeader) -> HdrName<'a> { - HdrName { inner: Repr::Standard(hdr) } - } -} - -impl HeaderName { - /// Converts a slice of bytes to an HTTP header name. - /// - /// This function normalizes the input. - #[allow(deprecated)] - pub fn from_bytes(src: &[u8]) -> Result { - let mut buf = unsafe { mem::uninitialized() }; - match parse_hdr(src, &mut buf, &HEADER_CHARS)?.inner { - Repr::Standard(std) => Ok(std.into()), - Repr::Custom(MaybeLower { buf, lower: true }) => { - let buf = Bytes::from(buf); - let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; - Ok(Custom(val).into()) - } - Repr::Custom(MaybeLower { buf, lower: false }) => { - use bytes::{BufMut}; - let mut dst = BytesMut::with_capacity(buf.len()); - - for b in buf.iter() { - let b = HEADER_CHARS[*b as usize]; - - if b == 0 { - return Err(InvalidHeaderName::new()); - } - - dst.put(b); - } - - let val = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) }; - - Ok(Custom(val).into()) - } - } - } - - /// Converts a slice of bytes to an HTTP header name. - /// - /// This function expects the input to only contain lowercase characters. - /// This is useful when decoding HTTP/2.0 headers. The HTTP/2.0 - /// specification requires that all headers be represented in lower case. - /// - /// # Examples - /// - /// ``` - /// # use http::header::*; - /// - /// // Parsing a lower case header - /// let hdr = HeaderName::from_lowercase(b"content-length").unwrap(); - /// assert_eq!(CONTENT_LENGTH, hdr); - /// - /// // Parsing a header that contains uppercase characters - /// assert!(HeaderName::from_lowercase(b"Content-Length").is_err()); - /// ``` - #[allow(deprecated)] - pub fn from_lowercase(src: &[u8]) -> Result { - let mut buf = unsafe { mem::uninitialized() }; - match parse_hdr(src, &mut buf, &HEADER_CHARS_H2)?.inner { - Repr::Standard(std) => Ok(std.into()), - Repr::Custom(MaybeLower { buf, lower: true }) => { - let buf = Bytes::from(buf); - let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; - Ok(Custom(val).into()) - } - Repr::Custom(MaybeLower { buf, lower: false }) => { - for &b in buf.iter() { - if b != HEADER_CHARS[b as usize] { - return Err(InvalidHeaderName::new()); - } - } - - let buf = Bytes::from(buf); - let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; - Ok(Custom(val).into()) - } - } - } - - /// Converts a static string to a HTTP header name. - /// - /// This function panics when the static string is a invalid header. - /// - /// This function requires the static string to only contain lowercase - /// characters, numerals and symbols, as per the HTTP/2.0 specification - /// and header names internal representation within this library. - /// - /// - /// # Examples - /// - /// ``` - /// # use http::header::*; - /// // Parsing a standard header - /// let hdr = HeaderName::from_static("content-length"); - /// assert_eq!(CONTENT_LENGTH, hdr); - /// - /// // Parsing a custom header - /// let CUSTOM_HEADER: &'static str = "custom-header"; - /// - /// let a = HeaderName::from_lowercase(b"custom-header").unwrap(); - /// let b = HeaderName::from_static(CUSTOM_HEADER); - /// assert_eq!(a, b); - /// ``` - /// - /// ```should_panic - /// # use http::header::*; - /// # - /// // Parsing a header that contains invalid symbols(s): - /// HeaderName::from_static("content{}{}length"); // This line panics! - /// - /// // Parsing a header that contains invalid uppercase characters. - /// let a = HeaderName::from_static("foobar"); - /// let b = HeaderName::from_static("FOOBAR"); // This line panics! - /// ``` - #[allow(deprecated)] - pub fn from_static(src: &'static str) -> HeaderName { - let bytes = src.as_bytes(); - let mut buf = unsafe { mem::uninitialized() }; - match parse_hdr(bytes, &mut buf, &HEADER_CHARS_H2) { - Ok(hdr_name) => match hdr_name.inner { - Repr::Standard(std) => std.into(), - Repr::Custom(MaybeLower { buf: _, lower: true }) => { - let val = ByteStr::from_static(src); - Custom(val).into() - }, - Repr::Custom(MaybeLower { buf: _, lower: false }) => { - // With lower false, the string is left unchecked by - // parse_hdr and must be validated manually. - for &b in bytes.iter() { - if HEADER_CHARS_H2[b as usize] == 0 { - panic!("invalid header name") - } - } - - let val = ByteStr::from_static(src); - Custom(val).into() - } - }, - - Err(_) => panic!("invalid header name") - } - } - - /// Returns a `str` representation of the header. - /// - /// The returned string will always be lower case. - #[inline] - pub fn as_str(&self) -> &str { - match self.inner { - Repr::Standard(v) => v.as_str(), - Repr::Custom(ref v) => &*v.0, - } - } -} - -impl FromStr for HeaderName { - type Err = InvalidHeaderName; - - fn from_str(s: &str) -> Result { - HeaderName::from_bytes(s.as_bytes()) - .map_err(|_| InvalidHeaderName { - _priv: (), - }) - } -} - -impl AsRef for HeaderName { - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl AsRef<[u8]> for HeaderName { - fn as_ref(&self) -> &[u8] { - self.as_str().as_bytes() - } -} - -impl Borrow for HeaderName { - fn borrow(&self) -> &str { - self.as_str() - } -} - -impl fmt::Debug for HeaderName { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(self.as_str(), fmt) - } -} - -impl fmt::Display for HeaderName { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(self.as_str(), fmt) - } -} - -impl InvalidHeaderName { - fn new() -> InvalidHeaderName { - InvalidHeaderName { _priv: () } - } -} - -impl<'a> From<&'a HeaderName> for HeaderName { - fn from(src: &'a HeaderName) -> HeaderName { - src.clone() - } -} - -#[doc(hidden)] -impl From> for Bytes -where T: Into { - fn from(repr: Repr) -> Bytes { - match repr { - Repr::Standard(header) => - Bytes::from_static(header.as_str().as_bytes()), - Repr::Custom(header) => header.into() - } - } -} - -impl From for Bytes { - #[inline] - fn from(Custom(inner): Custom) -> Bytes { - Bytes::from(inner) - } -} - -impl From for Bytes { - #[inline] - fn from(name: HeaderName) -> Bytes { - name.inner.into() - } -} - -impl<'a> HttpTryFrom<&'a HeaderName> for HeaderName { - type Error = ::error::Never; - - #[inline] - fn try_from(t: &'a HeaderName) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a str> for HeaderName { - type Error = InvalidHeaderName; - #[inline] - fn try_from(s: &'a str) -> Result { - Self::from_bytes(s.as_bytes()) - } -} - -impl<'a> HttpTryFrom<&'a String> for HeaderName { - type Error = InvalidHeaderName; - #[inline] - fn try_from(s: &'a String) -> Result { - Self::from_bytes(s.as_bytes()) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for HeaderName { - type Error = InvalidHeaderName; - #[inline] - fn try_from(s: &'a [u8]) -> Result { - Self::from_bytes(s) - } -} - -impl HttpTryFrom for HeaderName { - type Error = InvalidHeaderNameBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - Self::from_bytes(bytes.as_ref()).map_err(InvalidHeaderNameBytes) - } -} - -#[doc(hidden)] -impl From for HeaderName { - fn from(src: StandardHeader) -> HeaderName { - HeaderName { - inner: Repr::Standard(src), - } - } -} - -#[doc(hidden)] -impl From for HeaderName { - fn from(src: Custom) -> HeaderName { - HeaderName { inner: Repr::Custom(src) } - } -} - -impl<'a> PartialEq<&'a HeaderName> for HeaderName { - #[inline] - fn eq(&self, other: &&'a HeaderName) -> bool { - *self == **other - } -} - - -impl<'a> PartialEq for &'a HeaderName { - #[inline] - fn eq(&self, other: &HeaderName) -> bool { - *other == *self - } -} - -impl PartialEq for HeaderName { - /// Performs a case-insensitive comparison of the string against the header - /// name - /// - /// # Examples - /// - /// ``` - /// use http::header::CONTENT_LENGTH; - /// - /// assert_eq!(CONTENT_LENGTH, "content-length"); - /// assert_eq!(CONTENT_LENGTH, "Content-Length"); - /// assert_ne!(CONTENT_LENGTH, "content length"); - /// ``` - #[inline] - fn eq(&self, other: &str) -> bool { - eq_ignore_ascii_case(self.as_ref(), other.as_bytes()) - } -} - - -impl PartialEq for str { - /// Performs a case-insensitive comparison of the string against the header - /// name - /// - /// # Examples - /// - /// ``` - /// use http::header::CONTENT_LENGTH; - /// - /// assert_eq!(CONTENT_LENGTH, "content-length"); - /// assert_eq!(CONTENT_LENGTH, "Content-Length"); - /// assert_ne!(CONTENT_LENGTH, "content length"); - /// ``` - #[inline] - fn eq(&self, other: &HeaderName) -> bool { - *other == *self - } -} - -impl<'a> PartialEq<&'a str> for HeaderName { - /// Performs a case-insensitive comparison of the string against the header - /// name - #[inline] - fn eq(&self, other: &&'a str) -> bool { - *self == **other - } -} - - -impl<'a> PartialEq for &'a str { - /// Performs a case-insensitive comparison of the string against the header - /// name - #[inline] - fn eq(&self, other: &HeaderName) -> bool { - *other == *self - } -} - -impl fmt::Debug for InvalidHeaderName { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("InvalidHeaderName") - // skip _priv noise - .finish() - } -} - -impl fmt::Display for InvalidHeaderName { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.description().fmt(f) - } -} - -impl Error for InvalidHeaderName { - fn description(&self) -> &str { - "invalid HTTP header name" - } -} - -impl fmt::Display for InvalidHeaderNameBytes { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Error for InvalidHeaderNameBytes { - fn description(&self) -> &str { - self.0.description() - } -} - -// ===== HdrName ===== - -impl<'a> HdrName<'a> { - fn custom(buf: &'a [u8], lower: bool) -> HdrName<'a> { - HdrName { - inner: Repr::Custom(MaybeLower { - buf: buf, - lower: lower, - }), - } - } - - #[allow(deprecated)] - pub fn from_bytes(hdr: &[u8], f: F) -> Result - where F: FnOnce(HdrName) -> U, - { - let mut buf = unsafe { mem::uninitialized() }; - let hdr = parse_hdr(hdr, &mut buf, &HEADER_CHARS)?; - Ok(f(hdr)) - } - - #[allow(deprecated)] - pub fn from_static(hdr: &'static str, f: F) -> U - where F: FnOnce(HdrName) -> U, - { - let mut buf = unsafe { mem::uninitialized() }; - let hdr = parse_hdr(hdr.as_bytes(), &mut buf, &HEADER_CHARS) - .expect("static str is invalid name"); - f(hdr) - } -} - -#[doc(hidden)] -impl<'a> From> for HeaderName { - fn from(src: HdrName<'a>) -> HeaderName { - match src.inner { - Repr::Standard(s) => { - HeaderName { - inner: Repr::Standard(s), - } - } - Repr::Custom(maybe_lower) => { - if maybe_lower.lower { - let buf = Bytes::from(&maybe_lower.buf[..]); - let byte_str = unsafe { ByteStr::from_utf8_unchecked(buf) }; - - HeaderName { - inner: Repr::Custom(Custom(byte_str)), - } - } else { - use bytes::{BufMut}; - let mut dst = BytesMut::with_capacity(maybe_lower.buf.len()); - - for b in maybe_lower.buf.iter() { - dst.put(HEADER_CHARS[*b as usize]); - } - - let buf = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) }; - - HeaderName { - inner: Repr::Custom(Custom(buf)), - } - } - } - } - } -} - -#[doc(hidden)] -impl<'a> PartialEq> for HeaderName { - #[inline] - fn eq(&self, other: &HdrName<'a>) -> bool { - match self.inner { - Repr::Standard(a) => { - match other.inner { - Repr::Standard(b) => a == b, - _ => false, - } - } - Repr::Custom(Custom(ref a)) => { - match other.inner { - Repr::Custom(ref b) => { - if b.lower { - a.as_bytes() == b.buf - } else { - eq_ignore_ascii_case(a.as_bytes(), b.buf) - } - } - _ => false, - } - } - } - } -} - -// ===== Custom ===== - -impl Hash for Custom { - #[inline] - fn hash(&self, hasher: &mut H) { - hasher.write(self.0.as_bytes()) - } -} - -// ===== MaybeLower ===== - -impl<'a> Hash for MaybeLower<'a> { - #[inline] - fn hash(&self, hasher: &mut H) { - if self.lower { - hasher.write(self.buf); - } else { - for &b in self.buf { - hasher.write(&[HEADER_CHARS[b as usize]]); - } - } - } -} - -// Assumes that the left hand side is already lower case -#[inline] -fn eq_ignore_ascii_case(lower: &[u8], s: &[u8]) -> bool { - if lower.len() != s.len() { - return false; - } - - lower.iter().zip(s).all(|(a, b)| { - *a == HEADER_CHARS[*b as usize] - }) -} - -#[cfg(test)] -mod tests { - use super::*; - use self::StandardHeader::Vary; - - #[test] - fn test_bounds() { - fn check_bounds() {} - check_bounds::(); - } - - #[test] - fn test_parse_invalid_headers() { - for i in 0..128 { - let hdr = vec![1u8; i]; - assert!(HeaderName::from_bytes(&hdr).is_err(), "{} invalid header chars did not fail", i); - } - } - - #[test] - fn test_from_hdr_name() { - use self::StandardHeader::Vary; - - let name = HeaderName::from(HdrName { - inner: Repr::Standard(Vary), - }); - - assert_eq!(name.inner, Repr::Standard(Vary)); - - let name = HeaderName::from(HdrName { - inner: Repr::Custom(MaybeLower { - buf: b"hello-world", - lower: true, - }), - }); - - assert_eq!(name.inner, Repr::Custom(Custom(ByteStr::from_static("hello-world")))); - - let name = HeaderName::from(HdrName { - inner: Repr::Custom(MaybeLower { - buf: b"Hello-World", - lower: false, - }), - }); - - assert_eq!(name.inner, Repr::Custom(Custom(ByteStr::from_static("hello-world")))); - } - - #[test] - fn test_eq_hdr_name() { - use self::StandardHeader::Vary; - - let a = HeaderName { inner: Repr::Standard(Vary) }; - let b = HdrName { inner: Repr::Standard(Vary) }; - - assert_eq!(a, b); - - let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("vaary"))) }; - assert_ne!(a, b); - - let b = HdrName { inner: Repr::Custom(MaybeLower { - buf: b"vaary", - lower: true, - })}; - - assert_eq!(a, b); - - let b = HdrName { inner: Repr::Custom(MaybeLower { - buf: b"vaary", - lower: false, - })}; - - assert_eq!(a, b); - - let b = HdrName { inner: Repr::Custom(MaybeLower { - buf: b"VAARY", - lower: false, - })}; - - assert_eq!(a, b); - - let a = HeaderName { inner: Repr::Standard(Vary) }; - assert_ne!(a, b); - } - - #[test] - fn test_from_static_std() { - let a = HeaderName { inner: Repr::Standard(Vary) }; - - let b = HeaderName::from_static("vary"); - assert_eq!(a, b); - - let b = HeaderName::from_static("vaary"); - assert_ne!(a, b); - } - - #[test] - #[should_panic] - fn test_from_static_std_uppercase() { - HeaderName::from_static("Vary"); - } - - #[test] - #[should_panic] - fn test_from_static_std_symbol() { - HeaderName::from_static("vary{}"); - } - - // MaybeLower { lower: true } - #[test] - fn test_from_static_custom_short() { - let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("customheader"))) }; - let b = HeaderName::from_static("customheader"); - assert_eq!(a, b); - } - - #[test] - #[should_panic] - fn test_from_static_custom_short_uppercase() { - HeaderName::from_static("custom header"); - } - - #[test] - #[should_panic] - fn test_from_static_custom_short_symbol() { - HeaderName::from_static("CustomHeader"); - } - - // MaybeLower { lower: false } - #[test] - fn test_from_static_custom_long() { - let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static( - "longer-than-63--thisheaderislongerthansixtythreecharactersandthushandleddifferent" - ))) }; - let b = HeaderName::from_static( - "longer-than-63--thisheaderislongerthansixtythreecharactersandthushandleddifferent" - ); - assert_eq!(a, b); - } - - #[test] - #[should_panic] - fn test_from_static_custom_long_uppercase() { - HeaderName::from_static( - "Longer-Than-63--ThisHeaderIsLongerThanSixtyThreeCharactersAndThusHandledDifferent" - ); - } - - #[test] - #[should_panic] - fn test_from_static_custom_long_symbol() { - HeaderName::from_static( - "longer-than-63--thisheader{}{}{}{}islongerthansixtythreecharactersandthushandleddifferent" - ); - } - - #[test] - fn test_from_static_custom_single_char() { - let a = HeaderName { inner: Repr::Custom(Custom(ByteStr::from_static("a"))) }; - let b = HeaderName::from_static("a"); - assert_eq!(a, b); - } - - #[test] - #[should_panic] - fn test_from_static_empty() { - HeaderName::from_static(""); - } - - #[test] - fn test_all_tokens() { - HeaderName::from_static("!#$%&'*+-.^_`|~0123456789abcdefghijklmnopqrstuvwxyz"); - } -} diff --git a/third_party/rust/http/v0_1/crate/src/header/value.rs b/third_party/rust/http/v0_1/crate/src/header/value.rs deleted file mode 100644 index 25b0078a1ee5..000000000000 --- a/third_party/rust/http/v0_1/crate/src/header/value.rs +++ /dev/null @@ -1,812 +0,0 @@ -use bytes::{Bytes, BytesMut}; - -use std::{cmp, fmt, mem, str}; -use std::error::Error; -use std::str::FromStr; - -use ::convert::HttpTryFrom; -use ::error::Never; -use header::name::HeaderName; - -/// Represents an HTTP header field value. -/// -/// In practice, HTTP header field values are usually valid ASCII. However, the -/// HTTP spec allows for a header value to contain opaque bytes as well. In this -/// case, the header field value is not able to be represented as a string. -/// -/// To handle this, the `HeaderValue` is useable as a type and can be compared -/// with strings and implements `Debug`. A `to_str` fn is provided that returns -/// an `Err` if the header value contains non visible ascii characters. -#[derive(Clone, Hash)] -pub struct HeaderValue { - inner: Bytes, - is_sensitive: bool, -} - -/// A possible error when converting a `HeaderValue` from a string or byte -/// slice. -pub struct InvalidHeaderValue { - _priv: (), -} - -/// A possible error when converting a `HeaderValue` from a string or byte -/// slice. -#[derive(Debug)] -pub struct InvalidHeaderValueBytes(InvalidHeaderValue); - -/// A possible error when converting a `HeaderValue` to a string representation. -/// -/// Header field values may contain opaque bytes, in which case it is not -/// possible to represent the value as a string. -#[derive(Debug)] -pub struct ToStrError { - _priv: (), -} - -impl HeaderValue { - /// Convert a static string to a `HeaderValue`. - /// - /// This function will not perform any copying, however the string is - /// checked to ensure that no invalid characters are present. Only visible - /// ASCII characters (32-127) are permitted. - /// - /// # Panics - /// - /// This function panics if the argument contains invalid header value - /// characters. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_static("hello"); - /// assert_eq!(val, "hello"); - /// ``` - #[inline] - pub fn from_static(src: &'static str) -> HeaderValue { - let bytes = src.as_bytes(); - for &b in bytes { - if !is_visible_ascii(b) { - panic!("invalid header value"); - } - } - - HeaderValue { - inner: Bytes::from_static(bytes), - is_sensitive: false, - } - } - - /// Attempt to convert a string to a `HeaderValue`. - /// - /// If the argument contains invalid header value characters, an error is - /// returned. Only visible ASCII characters (32-127) are permitted. Use - /// `from_bytes` to create a `HeaderValue` that includes opaque octets - /// (128-255). - /// - /// This function is intended to be replaced in the future by a `TryFrom` - /// implementation once the trait is stabilized in std. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_str("hello").unwrap(); - /// assert_eq!(val, "hello"); - /// ``` - /// - /// An invalid value - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_str("\n"); - /// assert!(val.is_err()); - /// ``` - #[inline] - pub fn from_str(src: &str) -> Result { - HeaderValue::try_from(src) - } - - /// Converts a HeaderName into a HeaderValue - /// - /// Since every valid HeaderName is a valid HeaderValue this is done infallibly. - /// - /// # Examples - /// - /// ``` - /// # use http::header::{HeaderValue, HeaderName}; - /// # use http::header::ACCEPT; - /// let val = HeaderValue::from_name(ACCEPT); - /// assert_eq!(val, HeaderValue::from_bytes(b"accept").unwrap()); - /// ``` - #[inline] - pub fn from_name(name: HeaderName) -> HeaderValue { - name.into() - } - - /// Attempt to convert a byte slice to a `HeaderValue`. - /// - /// If the argument contains invalid header value bytes, an error is - /// returned. Only byte values between 32 and 255 (inclusive) are permitted, - /// excluding byte 127 (DEL). - /// - /// This function is intended to be replaced in the future by a `TryFrom` - /// implementation once the trait is stabilized in std. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_bytes(b"hello\xfa").unwrap(); - /// assert_eq!(val, &b"hello\xfa"[..]); - /// ``` - /// - /// An invalid value - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_bytes(b"\n"); - /// assert!(val.is_err()); - /// ``` - #[inline] - pub fn from_bytes(src: &[u8]) -> Result { - HeaderValue::try_from(src) - } - - /// Attempt to convert a `Bytes` buffer to a `HeaderValue`. - /// - /// If the argument contains invalid header value bytes, an error is - /// returned. Only byte values between 32 and 255 (inclusive) are permitted, - /// excluding byte 127 (DEL). - /// - /// This function is intended to be replaced in the future by a `TryFrom` - /// implementation once the trait is stabilized in std. - #[inline] - pub fn from_shared(src: Bytes) -> Result { - HeaderValue::try_from(src).map_err(InvalidHeaderValueBytes) - } - - /// Convert a `Bytes` directly into a `HeaderValue` without validating. - /// - /// This function does NOT validate that illegal bytes are not contained - /// within the buffer. - #[inline] - pub unsafe fn from_shared_unchecked(src: Bytes) -> HeaderValue { - if cfg!(debug_assertions) { - match HeaderValue::from_shared(src) { - Ok(val) => val, - Err(_err) => { - //TODO: if the Bytes were part of the InvalidHeaderValueBytes, - //this message could include the invalid bytes. - panic!("HeaderValue::from_shared_unchecked() with invalid bytes"); - }, - } - } else { - HeaderValue { - inner: src, - is_sensitive: false, - } - } - } - - fn try_from + Into>(src: T) -> Result { - for &b in src.as_ref() { - if !is_valid(b) { - return Err(InvalidHeaderValue { - _priv: (), - }); - } - } - Ok(HeaderValue { - inner: src.into(), - is_sensitive: false, - }) - } - - /// Yields a `&str` slice if the `HeaderValue` only contains visible ASCII - /// chars. - /// - /// This function will perform a scan of the header value, checking all the - /// characters. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_static("hello"); - /// assert_eq!(val.to_str().unwrap(), "hello"); - /// ``` - pub fn to_str(&self) -> Result<&str, ToStrError> { - let bytes = self.as_ref(); - - for &b in bytes { - if !is_visible_ascii(b) { - return Err(ToStrError { _priv: () }); - } - } - - unsafe { Ok(str::from_utf8_unchecked(bytes)) } - } - - /// Returns the length of `self`. - /// - /// This length is in bytes. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_static("hello"); - /// assert_eq!(val.len(), 5); - /// ``` - #[inline] - pub fn len(&self) -> usize { - self.as_ref().len() - } - - /// Returns true if the `HeaderValue` has a length of zero bytes. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_static(""); - /// assert!(val.is_empty()); - /// - /// let val = HeaderValue::from_static("hello"); - /// assert!(!val.is_empty()); - /// ``` - #[inline] - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Converts a `HeaderValue` to a byte slice. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let val = HeaderValue::from_static("hello"); - /// assert_eq!(val.as_bytes(), b"hello"); - /// ``` - #[inline] - pub fn as_bytes(&self) -> &[u8] { - self.as_ref() - } - - /// Mark that the header value represents sensitive information. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let mut val = HeaderValue::from_static("my secret"); - /// - /// val.set_sensitive(true); - /// assert!(val.is_sensitive()); - /// - /// val.set_sensitive(false); - /// assert!(!val.is_sensitive()); - /// ``` - #[inline] - pub fn set_sensitive(&mut self, val: bool) { - self.is_sensitive = val; - } - - /// Returns `true` if the value represents sensitive data. - /// - /// Sensitive data could represent passwords or other data that should not - /// be stored on disk or in memory. This setting can be used by components - /// like caches to avoid storing the value. HPACK encoders must set the - /// header field to never index when `is_sensitive` returns true. - /// - /// Note that sensitivity is not factored into equality or ordering. - /// - /// # Examples - /// - /// ``` - /// # use http::header::HeaderValue; - /// let mut val = HeaderValue::from_static("my secret"); - /// - /// val.set_sensitive(true); - /// assert!(val.is_sensitive()); - /// - /// val.set_sensitive(false); - /// assert!(!val.is_sensitive()); - /// ``` - #[inline] - pub fn is_sensitive(&self) -> bool { - self.is_sensitive - } -} - -impl AsRef<[u8]> for HeaderValue { - #[inline] - fn as_ref(&self) -> &[u8] { - self.inner.as_ref() - } -} - -impl fmt::Debug for HeaderValue { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.is_sensitive { - f.write_str("Sensitive") - } else { - f.write_str("\"")?; - let mut from = 0; - let bytes = self.as_bytes(); - for (i, &b) in bytes.iter().enumerate() { - if !is_visible_ascii(b) || b == b'"' { - if from != i { - f.write_str(unsafe { - str::from_utf8_unchecked(&bytes[from..i]) - })?; - } - if b == b'"' { - f.write_str("\\\"")?; - } else { - write!(f, "\\x{:x}", b)?; - } - from = i + 1; - } - } - - f.write_str(unsafe { - str::from_utf8_unchecked(&bytes[from..]) - })?; - f.write_str("\"") - } - } -} - -impl From for HeaderValue { - #[inline] - fn from(h: HeaderName) -> HeaderValue { - HeaderValue { - inner: h.into(), - is_sensitive: false, - } - } -} - -macro_rules! from_integers { - ($($name:ident: $t:ident => $max_len:expr),*) => {$( - impl From<$t> for HeaderValue { - fn from(num: $t) -> HeaderValue { - let mut buf = if mem::size_of::() - 1 < $max_len { - // On 32bit platforms, BytesMut max inline size - // is 15 bytes, but the $max_len could be bigger. - // - // The likelihood of the number *actually* being - // that big is very small, so only allocate - // if the number needs that space. - // - // The largest decimal number in 15 digits: - // It wold be 10.pow(15) - 1, but this is a constant - // version. - if num as u64 > 999_999_999_999_999_999 { - BytesMut::with_capacity($max_len) - } else { - // fits inline... - BytesMut::new() - } - } else { - // full value fits inline, so don't allocate! - BytesMut::new() - }; - let _ = ::itoa::fmt(&mut buf, num); - HeaderValue { - inner: buf.freeze(), - is_sensitive: false, - } - } - } - - impl HttpTryFrom<$t> for HeaderValue { - type Error = Never; - - #[inline] - fn try_from(num: $t) -> Result { - Ok(num.into()) - } - } - - #[test] - fn $name() { - let n: $t = 55; - let val = HeaderValue::from(n); - assert_eq!(val, &n.to_string()); - - let n = ::std::$t::MAX; - let val = HeaderValue::from(n); - assert_eq!(val, &n.to_string()); - } - )*}; -} - -from_integers! { - // integer type => maximum decimal length - - // u8 purposely left off... HeaderValue::from(b'3') could be confusing - from_u16: u16 => 5, - from_i16: i16 => 6, - from_u32: u32 => 10, - from_i32: i32 => 11, - from_u64: u64 => 20, - from_i64: i64 => 20 -} - -#[cfg(target_pointer_width = "16")] -from_integers! { - from_usize: usize => 5, - from_isize: isize => 6 -} - -#[cfg(target_pointer_width = "32")] -from_integers! { - from_usize: usize => 10, - from_isize: isize => 11 -} - -#[cfg(target_pointer_width = "64")] -from_integers! { - from_usize: usize => 20, - from_isize: isize => 20 -} - -#[cfg(test)] -mod from_header_name_tests { - use super::*; - use header::map::HeaderMap; - use header::name; - - #[test] - fn it_can_insert_header_name_as_header_value() { - let mut map = HeaderMap::new(); - map.insert(name::UPGRADE, name::SEC_WEBSOCKET_PROTOCOL.into()); - map.insert(name::ACCEPT, name::HeaderName::from_bytes(b"hello-world").unwrap().into()); - - assert_eq!( - map.get(name::UPGRADE).unwrap(), - HeaderValue::from_bytes(b"sec-websocket-protocol").unwrap() - ); - - assert_eq!( - map.get(name::ACCEPT).unwrap(), - HeaderValue::from_bytes(b"hello-world").unwrap() - ); - } -} - -impl FromStr for HeaderValue { - type Err = InvalidHeaderValue; - - #[inline] - fn from_str(s: &str) -> Result { - HeaderValue::from_str(s) - } -} - -impl From for Bytes { - #[inline] - fn from(value: HeaderValue) -> Bytes { - value.inner - } -} - -impl<'a> From<&'a HeaderValue> for HeaderValue { - #[inline] - fn from(t: &'a HeaderValue) -> Self { - t.clone() - } -} - -impl<'a> HttpTryFrom<&'a HeaderValue> for HeaderValue { - type Error = ::error::Never; - - #[inline] - fn try_from(t: &'a HeaderValue) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a str> for HeaderValue { - type Error = InvalidHeaderValue; - - #[inline] - fn try_from(t: &'a str) -> Result { - t.parse() - } -} - -impl<'a> HttpTryFrom<&'a String> for HeaderValue { - type Error = InvalidHeaderValue; - #[inline] - fn try_from(s: &'a String) -> Result { - Self::from_bytes(s.as_bytes()) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue { - type Error = InvalidHeaderValue; - - #[inline] - fn try_from(t: &'a [u8]) -> Result { - HeaderValue::from_bytes(t) - } -} - -impl HttpTryFrom for HeaderValue { - type Error = InvalidHeaderValueBytes; - - #[inline] - fn try_from(t: String) -> Result { - HeaderValue::from_shared(t.into()) - } -} - -impl HttpTryFrom for HeaderValue { - type Error = InvalidHeaderValueBytes; - - #[inline] - fn try_from(bytes: Bytes) -> Result { - HeaderValue::from_shared(bytes) - } -} - -impl HttpTryFrom for HeaderValue { - type Error = InvalidHeaderValue; - - #[inline] - fn try_from(name: HeaderName) -> Result { - // Infallable as header names have the same validations - Ok(name.into()) - } -} - -#[cfg(test)] -mod try_from_header_name_tests { - use super::*; - use header::name; - - #[test] - fn it_converts_using_try_from() { - assert_eq!( - HeaderValue::try_from(name::UPGRADE).unwrap(), - HeaderValue::from_bytes(b"upgrade").unwrap() - ); - } -} - -fn is_visible_ascii(b: u8) -> bool { - b >= 32 && b < 127 || b == b'\t' -} - -#[inline] -fn is_valid(b: u8) -> bool { - b >= 32 && b != 127 || b == b'\t' -} - -impl fmt::Debug for InvalidHeaderValue { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("InvalidHeaderValue") - // skip _priv noise - .finish() - } -} - -impl fmt::Display for InvalidHeaderValue { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.description().fmt(f) - } -} - -impl Error for InvalidHeaderValue { - fn description(&self) -> &str { - "failed to parse header value" - } -} - -impl fmt::Display for InvalidHeaderValueBytes { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Error for InvalidHeaderValueBytes { - fn description(&self) -> &str { - self.0.description() - } -} - -impl fmt::Display for ToStrError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.description().fmt(f) - } -} - -impl Error for ToStrError { - fn description(&self) -> &str { - "failed to convert header to a str" - } -} - -// ===== PartialEq / PartialOrd ===== - -impl PartialEq for HeaderValue { - #[inline] - fn eq(&self, other: &HeaderValue) -> bool { - self.inner == other.inner - } -} - -impl Eq for HeaderValue {} - -impl PartialOrd for HeaderValue { - #[inline] - fn partial_cmp(&self, other: &HeaderValue) -> Option { - self.inner.partial_cmp(&other.inner) - } -} - -impl Ord for HeaderValue { - #[inline] - fn cmp(&self, other: &Self) -> cmp::Ordering { - self.inner.cmp(&other.inner) - } -} - -impl PartialEq for HeaderValue { - #[inline] - fn eq(&self, other: &str) -> bool { - self.inner == other.as_bytes() - } -} - -impl PartialEq<[u8]> for HeaderValue { - #[inline] - fn eq(&self, other: &[u8]) -> bool { - self.inner == other - } -} - -impl PartialOrd for HeaderValue { - #[inline] - fn partial_cmp(&self, other: &str) -> Option { - (*self.inner).partial_cmp(other.as_bytes()) - } -} - -impl PartialOrd<[u8]> for HeaderValue { - #[inline] - fn partial_cmp(&self, other: &[u8]) -> Option { - (*self.inner).partial_cmp(other) - } -} - -impl PartialEq for str { - #[inline] - fn eq(&self, other: &HeaderValue) -> bool { - *other == *self - } -} - -impl PartialEq for [u8] { - #[inline] - fn eq(&self, other: &HeaderValue) -> bool { - *other == *self - } -} - -impl PartialOrd for str { - #[inline] - fn partial_cmp(&self, other: &HeaderValue) -> Option { - self.as_bytes().partial_cmp(other.as_bytes()) - } -} - -impl PartialOrd for [u8] { - #[inline] - fn partial_cmp(&self, other: &HeaderValue) -> Option { - self.partial_cmp(other.as_bytes()) - } -} - -impl PartialEq for HeaderValue { - #[inline] - fn eq(&self, other: &String) -> bool { - *self == &other[..] - } -} - -impl PartialOrd for HeaderValue { - #[inline] - fn partial_cmp(&self, other: &String) -> Option { - self.inner.partial_cmp(other.as_bytes()) - } -} - -impl PartialEq for String { - #[inline] - fn eq(&self, other: &HeaderValue) -> bool { - *other == *self - } -} - -impl PartialOrd for String { - #[inline] - fn partial_cmp(&self, other: &HeaderValue) -> Option { - self.as_bytes().partial_cmp(other.as_bytes()) - } -} - -impl<'a> PartialEq for &'a HeaderValue { - #[inline] - fn eq(&self, other: &HeaderValue) -> bool { - **self == *other - } -} - -impl<'a> PartialOrd for &'a HeaderValue { - #[inline] - fn partial_cmp(&self, other: &HeaderValue) -> Option { - (**self).partial_cmp(other) - } -} - -impl<'a, T: ?Sized> PartialEq<&'a T> for HeaderValue - where HeaderValue: PartialEq -{ - #[inline] - fn eq(&self, other: &&'a T) -> bool { - *self == **other - } -} - -impl<'a, T: ?Sized> PartialOrd<&'a T> for HeaderValue - where HeaderValue: PartialOrd -{ - #[inline] - fn partial_cmp(&self, other: &&'a T) -> Option { - self.partial_cmp(*other) - } -} - -impl<'a> PartialEq for &'a str { - #[inline] - fn eq(&self, other: &HeaderValue) -> bool { - *other == *self - } -} - -impl<'a> PartialOrd for &'a str { - #[inline] - fn partial_cmp(&self, other: &HeaderValue) -> Option { - self.as_bytes().partial_cmp(other.as_bytes()) - } -} - -#[test] -fn test_try_from() { - HeaderValue::try_from(vec![127]).unwrap_err(); -} - -#[test] -fn test_debug() { - let cases = &[ - ("hello", "\"hello\""), - ("hello \"world\"", "\"hello \\\"world\\\"\""), - ("\u{7FFF}hello", "\"\\xe7\\xbf\\xbfhello\""), - ]; - - for &(value, expected) in cases { - let val = HeaderValue::from_bytes(value.as_bytes()).unwrap(); - let actual = format!("{:?}", val); - assert_eq!(expected, actual); - } - - let mut sensitive = HeaderValue::from_static("password"); - sensitive.set_sensitive(true); - assert_eq!("Sensitive", format!("{:?}", sensitive)); -} diff --git a/third_party/rust/http/v0_1/crate/src/lib.rs b/third_party/rust/http/v0_1/crate/src/lib.rs deleted file mode 100644 index 2f6988507c92..000000000000 --- a/third_party/rust/http/v0_1/crate/src/lib.rs +++ /dev/null @@ -1,213 +0,0 @@ -#![doc(html_root_url = "https://docs.rs/http/0.1.21")] - -//! A general purpose library of common HTTP types -//! -//! This crate is a general purpose library for common types found when working -//! with the HTTP protocol. You'll find `Request` and `Response` types for -//! working as either a client or a server as well as all of their components. -//! Notably you'll find `Uri` for what a `Request` is requesting, a `Method` -//! for how it's being requested, a `StatusCode` for what sort of response came -//! back, a `Version` for how this was communicated, and -//! `HeaderName`/`HeaderValue` definitions to get grouped in a `HeaderMap` to -//! work with request/response headers. -//! -//! You will notably *not* find an implementation of sending requests or -//! spinning up a server in this crate. It's intended that this crate is the -//! "standard library" for HTTP clients and servers without dictating any -//! particular implementation. Note that this crate is still early on in its -//! lifecycle so the support libraries that integrate with the `http` crate are -//! a work in progress! Stay tuned and we'll be sure to highlight crates here -//! in the future. -//! -//! ## Requests and Responses -//! -//! Perhaps the main two types in this crate are the `Request` and `Response` -//! types. A `Request` could either be constructed to get sent off as a client -//! or it can also be received to generate a `Response` for a server. Similarly -//! as a client a `Response` is what you get after sending a `Request`, whereas -//! on a server you'll be manufacturing a `Response` to send back to the client. -//! -//! Each type has a number of accessors for the component fields. For as a -//! server you might want to inspect a requests URI to dispatch it: -//! -//! ``` -//! use http::{Request, Response}; -//! -//! fn response(req: Request<()>) -> http::Result> { -//! match req.uri().path() { -//! "/" => index(req), -//! "/foo" => foo(req), -//! "/bar" => bar(req), -//! _ => not_found(req), -//! } -//! } -//! # fn index(_req: Request<()>) -> http::Result> { panic!() } -//! # fn foo(_req: Request<()>) -> http::Result> { panic!() } -//! # fn bar(_req: Request<()>) -> http::Result> { panic!() } -//! # fn not_found(_req: Request<()>) -> http::Result> { panic!() } -//! ``` -//! -//! On a `Request` you'll also find accessors like `method` to return a -//! `Method` and `headers` to inspect the various headers. A `Response` -//! has similar methods for headers, the status code, etc. -//! -//! In addition to getters, request/response types also have mutable accessors -//! to edit the request/response: -//! -//! ``` -//! use http::{HeaderValue, Response, StatusCode}; -//! use http::header::CONTENT_TYPE; -//! -//! fn add_server_headers(response: &mut Response) { -//! response.headers_mut() -//! .insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); -//! *response.status_mut() = StatusCode::OK; -//! } -//! ``` -//! -//! And finally, one of the most important aspects of requests/responses, the -//! body! The `Request` and `Response` types in this crate are *generic* in -//! what their body is. This allows downstream libraries to use different -//! representations such as `Request>`, `Response`, -//! `Request, Error = _>>`, or even -//! `Response` where the custom type was deserialized from JSON. -//! -//! The body representation is intentionally flexible to give downstream -//! libraries maximal flexibility in implementing the body as appropriate. -//! -//! ## HTTP Headers -//! -//! Another major piece of functionality in this library is HTTP header -//! interpretation and generation. The `HeaderName` type serves as a way to -//! define header *names*, or what's to the left of the colon. A `HeaderValue` -//! conversely is the header *value*, or what's to the right of a colon. -//! -//! For example, if you have an HTTP request that looks like: -//! -//! ```http -//! GET /foo HTTP/1.1 -//! Accept: text/html -//! ``` -//! -//! Then `"Accept"` is a `HeaderName` while `"text/html"` is a `HeaderValue`. -//! Each of these is a dedicated type to allow for a number of interesting -//! optimizations and to also encode the static guarantees of each type. For -//! example a `HeaderName` is always a valid `&str`, but a `HeaderValue` may -//! not be valid UTF-8. -//! -//! The most common header names are already defined for you as constant values -//! in the `header` module of this crate. For example: -//! -//! ``` -//! use http::header::{self, HeaderName}; -//! -//! let name: HeaderName = header::ACCEPT; -//! assert_eq!(name.as_str(), "accept"); -//! ``` -//! -//! You can, however, also parse header names from strings: -//! -//! ``` -//! use http::header::{self, HeaderName}; -//! -//! let name = "Accept".parse::().unwrap(); -//! assert_eq!(name, header::ACCEPT); -//! ``` -//! -//! Header values can be created from string literals through the `from_static` -//! function: -//! -//! ``` -//! use http::HeaderValue; -//! -//! let value = HeaderValue::from_static("text/html"); -//! assert_eq!(value.as_bytes(), b"text/html"); -//! ``` -//! -//! And header values can also be parsed like names: -//! -//! ``` -//! use http::HeaderValue; -//! -//! let value = "text/html"; -//! let value = value.parse::().unwrap(); -//! ``` -//! -//! Most HTTP requests and responses tend to come with more than one header, so -//! it's not too useful to just work with names and values only! This crate also -//! provides a `HeaderMap` type which is a specialized hash map for keys as -//! `HeaderName` and generic values. This type, like header names, is optimized -//! for common usage but should continue to scale with your needs over time. -//! -//! # URIs -//! -//! Each HTTP `Request` has an associated URI with it. This may just be a path -//! like `/index.html` but it could also be an absolute URL such as -//! `https://www.rust-lang.org/index.html`. A `URI` has a number of accessors to -//! interpret it: -//! -//! ``` -//! use http::Uri; -//! -//! let uri = "https://www.rust-lang.org/index.html".parse::().unwrap(); -//! -//! assert_eq!(uri.scheme_str(), Some("https")); -//! assert_eq!(uri.host(), Some("www.rust-lang.org")); -//! assert_eq!(uri.path(), "/index.html"); -//! assert_eq!(uri.query(), None); -//! ``` - -#![deny(warnings, missing_docs, missing_debug_implementations)] - -extern crate bytes; -extern crate fnv; -extern crate itoa; - -#[cfg(test)] -#[macro_use] -extern crate doc_comment; - -#[cfg(test)] -doctest!("../README.md"); - -pub mod header; -pub mod method; -pub mod request; -pub mod response; -pub mod status; -pub mod version; -pub mod uri; - -mod byte_str; -mod convert; -mod error; -mod extensions; - -pub use convert::HttpTryFrom; -pub use error::{Error, Result}; -pub use extensions::Extensions; -#[doc(no_inline)] -pub use header::{HeaderMap, HeaderValue}; -pub use method::Method; -pub use request::Request; -pub use response::Response; -pub use status::StatusCode; -pub use uri::Uri; -pub use version::Version; - -fn _assert_types() { - fn assert_send() {} - fn assert_sync() {} - - assert_send::>(); - assert_send::>(); - - assert_sync::>(); - assert_sync::>(); -} - -mod sealed { - /// Private trait to this crate to prevent traits from being implemented in - /// downstream crates. - pub trait Sealed {} -} diff --git a/third_party/rust/http/v0_1/crate/src/method.rs b/third_party/rust/http/v0_1/crate/src/method.rs deleted file mode 100644 index e725a68b5142..000000000000 --- a/third_party/rust/http/v0_1/crate/src/method.rs +++ /dev/null @@ -1,439 +0,0 @@ -//! The HTTP request method -//! -//! This module contains HTTP-method related structs and errors and such. The -//! main type of this module, `Method`, is also reexported at the root of the -//! crate as `http::Method` and is intended for import through that location -//! primarily. -//! -//! # Examples -//! -//! ``` -//! use http::Method; -//! -//! assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); -//! assert!(Method::GET.is_idempotent()); -//! assert_eq!(Method::POST.as_str(), "POST"); -//! ``` - -use HttpTryFrom; -use self::Inner::*; - -use std::{fmt, str}; -use std::convert::AsRef; -use std::error::Error; -use std::str::FromStr; - -/// The Request Method (VERB) -/// -/// This type also contains constants for a number of common HTTP methods such -/// as GET, POST, etc. -/// -/// Currently includes 8 variants representing the 8 methods defined in -/// [RFC 7230](https://tools.ietf.org/html/rfc7231#section-4.1), plus PATCH, -/// and an Extension variant for all extensions. -/// -/// # Examples -/// -/// ``` -/// use http::Method; -/// -/// assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); -/// assert!(Method::GET.is_idempotent()); -/// assert_eq!(Method::POST.as_str(), "POST"); -/// ``` -#[derive(Clone, PartialEq, Eq, Hash)] -pub struct Method(Inner); - -/// A possible error value when converting `Method` from bytes. -pub struct InvalidMethod { - _priv: (), -} - -#[derive(Clone, PartialEq, Eq, Hash)] -enum Inner { - Options, - Get, - Post, - Put, - Delete, - Head, - Trace, - Connect, - Patch, - // If the extension is short enough, store it inline - ExtensionInline([u8; MAX_INLINE], u8), - // Otherwise, allocate it - ExtensionAllocated(Box<[u8]>), -} - -const MAX_INLINE: usize = 15; - -// From the HTTP spec section 5.1.1, the HTTP method is case-sensitive and can -// contain the following characters: -// -// ``` -// method = token -// token = 1*tchar -// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / -// "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA -// ``` -// -// https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01#Method -// -const METHOD_CHARS: [u8; 256] = [ - // 0 1 2 3 4 5 6 7 8 9 - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 1x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 2x - b'\0', b'\0', b'\0', b'!', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 3x - b'\0', b'\0', b'*', b'+', b'\0', b'-', b'.', b'\0', b'0', b'1', // 4x - b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'\0', b'\0', // 5x - b'\0', b'\0', b'\0', b'\0', b'\0', b'A', b'B', b'C', b'D', b'E', // 6x - b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x - b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x - b'Z', b'\0', b'\0', b'\0', b'^', b'_', b'`', b'a', b'b', b'c', // 9x - b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x - b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x - b'x', b'y', b'z', b'\0', b'|', b'\0', b'~', b'\0', b'\0', b'\0', // 12x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 13x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 14x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 15x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 16x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 17x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 18x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 19x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 20x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 21x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 22x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 23x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', b'\0', // 24x - b'\0', b'\0', b'\0', b'\0', b'\0', b'\0' // 25x -]; - - -impl Method { - /// GET - pub const GET: Method = Method(Get); - - /// POST - pub const POST: Method = Method(Post); - - /// PUT - pub const PUT: Method = Method(Put); - - /// DELETE - pub const DELETE: Method = Method(Delete); - - /// HEAD - pub const HEAD: Method = Method(Head); - - /// OPTIONS - pub const OPTIONS: Method = Method(Options); - - /// CONNECT - pub const CONNECT: Method = Method(Connect); - - /// PATCH - pub const PATCH: Method = Method(Patch); - - /// TRACE - pub const TRACE: Method = Method(Trace); - - /// Converts a slice of bytes to an HTTP method. - pub fn from_bytes(src: &[u8]) -> Result { - match src.len() { - 0 => { - Err(InvalidMethod::new()) - } - 3 => { - match src { - b"GET" => Ok(Method(Get)), - b"PUT" => Ok(Method(Put)), - _ => Method::extension_inline(src), - } - } - 4 => { - match src { - b"POST" => Ok(Method(Post)), - b"HEAD" => Ok(Method(Head)), - _ => Method::extension_inline(src), - } - } - 5 => { - match src { - b"PATCH" => Ok(Method(Patch)), - b"TRACE" => Ok(Method(Trace)), - _ => Method::extension_inline(src), - } - } - 6 => { - match src { - b"DELETE" => Ok(Method(Delete)), - _ => Method::extension_inline(src), - } - } - 7 => { - match src { - b"OPTIONS" => Ok(Method(Options)), - b"CONNECT" => Ok(Method(Connect)), - _ => Method::extension_inline(src), - } - } - _ => { - if src.len() < MAX_INLINE { - Method::extension_inline(src) - } else { - let mut data: Vec = vec![0; src.len()]; - - write_checked(src, &mut data)?; - - Ok(Method(ExtensionAllocated(data.into_boxed_slice()))) - } - } - } - } - - fn extension_inline(src: &[u8]) -> Result { - let mut data: [u8; MAX_INLINE] = Default::default(); - - write_checked(src, &mut data)?; - - Ok(Method(ExtensionInline(data, src.len() as u8))) - } - - /// Whether a method is considered "safe", meaning the request is - /// essentially read-only. - /// - /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.1) - /// for more words. - pub fn is_safe(&self) -> bool { - match self.0 { - Get | Head | Options | Trace => true, - _ => false - } - } - - /// Whether a method is considered "idempotent", meaning the request has - /// the same result if executed multiple times. - /// - /// See [the spec](https://tools.ietf.org/html/rfc7231#section-4.2.2) for - /// more words. - pub fn is_idempotent(&self) -> bool { - match self.0 { - Put | Delete => true, - _ => self.is_safe(), - } - } - - /// Return a &str representation of the HTTP method - #[inline] - pub fn as_str(&self) -> &str { - match self.0 { - Options => "OPTIONS", - Get => "GET", - Post => "POST", - Put => "PUT", - Delete => "DELETE", - Head => "HEAD", - Trace => "TRACE", - Connect => "CONNECT", - Patch => "PATCH", - ExtensionInline(ref data, len) => { - unsafe { - str::from_utf8_unchecked(&data[..len as usize]) - } - } - ExtensionAllocated(ref data) => { - unsafe { - str::from_utf8_unchecked(data) - } - } - } - } -} - -fn write_checked(src: &[u8], dst: &mut [u8]) -> Result<(), InvalidMethod> { - for (i, &b) in src.iter().enumerate() { - let b = METHOD_CHARS[b as usize]; - - if b == 0 { - return Err(InvalidMethod::new()); - } - - dst[i] = b; - } - - Ok(()) -} - -impl AsRef for Method { - #[inline] - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl<'a> PartialEq<&'a Method> for Method { - #[inline] - fn eq(&self, other: & &'a Method) -> bool { - self == *other - } -} - -impl<'a> PartialEq for &'a Method { - #[inline] - fn eq(&self, other: &Method) -> bool { - *self == other - } -} - -impl PartialEq for Method { - #[inline] - fn eq(&self, other: &str) -> bool { - self.as_ref() == other - } -} - -impl PartialEq for str { - #[inline] - fn eq(&self, other: &Method) -> bool { - self == other.as_ref() - } -} - -impl<'a> PartialEq<&'a str> for Method { - #[inline] - fn eq(&self, other: &&'a str) -> bool { - self.as_ref() == *other - } -} - -impl<'a> PartialEq for &'a str { - #[inline] - fn eq(&self, other: &Method) -> bool { - *self == other.as_ref() - } -} - -impl fmt::Debug for Method { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.as_ref()) - } -} - -impl fmt::Display for Method { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt.write_str(self.as_ref()) - } -} - -impl Default for Method { - #[inline] - fn default() -> Method { - Method::GET - } -} - -impl<'a> From<&'a Method> for Method { - #[inline] - fn from(t: &'a Method) -> Self { - t.clone() - } -} - -impl<'a> HttpTryFrom<&'a Method> for Method { - type Error = ::error::Never; - - #[inline] - fn try_from(t: &'a Method) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for Method { - type Error = InvalidMethod; - - #[inline] - fn try_from(t: &'a [u8]) -> Result { - Method::from_bytes(t) - } -} - -impl<'a> HttpTryFrom<&'a str> for Method { - type Error = InvalidMethod; - - #[inline] - fn try_from(t: &'a str) -> Result { - HttpTryFrom::try_from(t.as_bytes()) - } -} - -impl FromStr for Method { - type Err = InvalidMethod; - - #[inline] - fn from_str(t: &str) -> Result { - HttpTryFrom::try_from(t) - } -} - -impl InvalidMethod { - fn new() -> InvalidMethod { - InvalidMethod { - _priv: (), - } - } -} - -impl fmt::Debug for InvalidMethod { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("InvalidMethod") - // skip _priv noise - .finish() - } -} - -impl fmt::Display for InvalidMethod { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.description()) - } -} - -impl Error for InvalidMethod { - fn description(&self) -> &str { - "invalid HTTP method" - } -} - -#[test] -fn test_method_eq() { - assert_eq!(Method::GET, Method::GET); - assert_eq!(Method::GET, "GET"); - assert_eq!(&Method::GET, "GET"); - - assert_eq!("GET", Method::GET); - assert_eq!("GET", &Method::GET); - - assert_eq!(&Method::GET, Method::GET); - assert_eq!(Method::GET, &Method::GET); -} - -#[test] -fn test_invalid_method() { - assert!(Method::from_str("").is_err()); - assert!(Method::from_bytes(b"").is_err()); -} - -#[test] -fn test_is_idempotent() { - assert!(Method::OPTIONS.is_idempotent()); - assert!(Method::GET.is_idempotent()); - assert!(Method::PUT.is_idempotent()); - assert!(Method::DELETE.is_idempotent()); - assert!(Method::HEAD.is_idempotent()); - assert!(Method::TRACE.is_idempotent()); - - assert!(!Method::POST.is_idempotent()); - assert!(!Method::CONNECT.is_idempotent()); - assert!(!Method::PATCH.is_idempotent()); -} diff --git a/third_party/rust/http/v0_1/crate/src/request.rs b/third_party/rust/http/v0_1/crate/src/request.rs deleted file mode 100644 index ac76470e178c..000000000000 --- a/third_party/rust/http/v0_1/crate/src/request.rs +++ /dev/null @@ -1,1069 +0,0 @@ -//! HTTP request types. -//! -//! This module contains structs related to HTTP requests, notably the -//! `Request` type itself as well as a builder to create requests. Typically -//! you'll import the `http::Request` type rather than reaching into this -//! module itself. -//! -//! # Examples -//! -//! Creating a `Request` to send -//! -//! ```no_run -//! use http::{Request, Response}; -//! -//! let mut request = Request::builder(); -//! request.uri("https://www.rust-lang.org/") -//! .header("User-Agent", "my-awesome-agent/1.0"); -//! -//! if needs_awesome_header() { -//! request.header("Awesome", "yes"); -//! } -//! -//! let response = send(request.body(()).unwrap()); -//! -//! # fn needs_awesome_header() -> bool { -//! # true -//! # } -//! # -//! fn send(req: Request<()>) -> Response<()> { -//! // ... -//! # panic!() -//! } -//! ``` -//! -//! Inspecting a request to see what was sent. -//! -//! ``` -//! use http::{Request, Response, StatusCode}; -//! -//! fn respond_to(req: Request<()>) -> http::Result> { -//! if req.uri() != "/awesome-url" { -//! return Response::builder() -//! .status(StatusCode::NOT_FOUND) -//! .body(()) -//! } -//! -//! let has_awesome_header = req.headers().contains_key("Awesome"); -//! let body = req.body(); -//! -//! // ... -//! # panic!() -//! } -//! ``` - -use std::any::Any; -use std::fmt; - -use {Uri, Error, Result, HttpTryFrom, Extensions}; -use header::{HeaderMap, HeaderName, HeaderValue}; -use method::Method; -use version::Version; - -/// Represents an HTTP request. -/// -/// An HTTP request consists of a head and a potentially optional body. The body -/// component is generic, enabling arbitrary types to represent the HTTP body. -/// For example, the body could be `Vec`, a `Stream` of byte chunks, or a -/// value that has been deserialized. -/// -/// # Examples -/// -/// Creating a `Request` to send -/// -/// ```no_run -/// use http::{Request, Response}; -/// -/// let mut request = Request::builder(); -/// request.uri("https://www.rust-lang.org/") -/// .header("User-Agent", "my-awesome-agent/1.0"); -/// -/// if needs_awesome_header() { -/// request.header("Awesome", "yes"); -/// } -/// -/// let response = send(request.body(()).unwrap()); -/// -/// # fn needs_awesome_header() -> bool { -/// # true -/// # } -/// # -/// fn send(req: Request<()>) -> Response<()> { -/// // ... -/// # panic!() -/// } -/// ``` -/// -/// Inspecting a request to see what was sent. -/// -/// ``` -/// use http::{Request, Response, StatusCode}; -/// -/// fn respond_to(req: Request<()>) -> http::Result> { -/// if req.uri() != "/awesome-url" { -/// return Response::builder() -/// .status(StatusCode::NOT_FOUND) -/// .body(()) -/// } -/// -/// let has_awesome_header = req.headers().contains_key("Awesome"); -/// let body = req.body(); -/// -/// // ... -/// # panic!() -/// } -/// ``` -/// -/// Deserialize a request of bytes via json: -/// -/// ``` -/// # extern crate serde; -/// # extern crate serde_json; -/// # extern crate http; -/// use http::Request; -/// use serde::de; -/// -/// fn deserialize(req: Request>) -> serde_json::Result> -/// where for<'de> T: de::Deserialize<'de>, -/// { -/// let (parts, body) = req.into_parts(); -/// let body = serde_json::from_slice(&body)?; -/// Ok(Request::from_parts(parts, body)) -/// } -/// # -/// # fn main() {} -/// ``` -/// -/// Or alternatively, serialize the body of a request to json -/// -/// ``` -/// # extern crate serde; -/// # extern crate serde_json; -/// # extern crate http; -/// use http::Request; -/// use serde::ser; -/// -/// fn serialize(req: Request) -> serde_json::Result>> -/// where T: ser::Serialize, -/// { -/// let (parts, body) = req.into_parts(); -/// let body = serde_json::to_vec(&body)?; -/// Ok(Request::from_parts(parts, body)) -/// } -/// # -/// # fn main() {} -/// ``` -pub struct Request { - head: Parts, - body: T, -} - -/// Component parts of an HTTP `Request` -/// -/// The HTTP request head consists of a method, uri, version, and a set of -/// header fields. -pub struct Parts { - /// The request's method - pub method: Method, - - /// The request's URI - pub uri: Uri, - - /// The request's version - pub version: Version, - - /// The request's headers - pub headers: HeaderMap, - - /// The request's extensions - pub extensions: Extensions, - - _priv: (), -} - -/// An HTTP request builder -/// -/// This type can be used to construct an instance or `Request` -/// through a builder-like pattern. -#[derive(Debug)] -pub struct Builder { - head: Option, - err: Option, -} - -impl Request<()> { - /// Creates a new builder-style object to manufacture a `Request` - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request = Request::builder() - /// .method("GET") - /// .uri("https://www.rust-lang.org/") - /// .header("X-Custom-Foo", "Bar") - /// .body(()) - /// .unwrap(); - /// ``` - #[inline] - pub fn builder() -> Builder { - Builder::new() - } - - - /// Creates a new `Builder` initialized with a GET method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::get("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn get(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::GET).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a PUT method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::put("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn put(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::PUT).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a POST method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::post("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn post(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::POST).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a DELETE method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::delete("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn delete(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::DELETE).uri(uri); - b - } - - /// Creates a new `Builder` initialized with an OPTIONS method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::options("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// # assert_eq!(*request.method(), Method::OPTIONS); - /// ``` - pub fn options(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::OPTIONS).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a HEAD method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::head("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn head(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::HEAD).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a CONNECT method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::connect("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn connect(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::CONNECT).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a PATCH method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::patch("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn patch(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::PATCH).uri(uri); - b - } - - /// Creates a new `Builder` initialized with a TRACE method and the given URI. - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Request`. - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::trace("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn trace(uri: T) -> Builder - where Uri: HttpTryFrom { - let mut b = Builder::new(); - b.method(Method::TRACE).uri(uri); - b - } -} - -impl Request { - /// Creates a new blank `Request` with the body - /// - /// The component parts of this request will be set to their default, e.g. - /// the GET method, no headers, etc. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request = Request::new("hello world"); - /// - /// assert_eq!(*request.method(), Method::GET); - /// assert_eq!(*request.body(), "hello world"); - /// ``` - #[inline] - pub fn new(body: T) -> Request { - Request { - head: Parts::new(), - body: body, - } - } - - /// Creates a new `Request` with the given components parts and body. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request = Request::new("hello world"); - /// let (mut parts, body) = request.into_parts(); - /// parts.method = Method::POST; - /// - /// let request = Request::from_parts(parts, body); - /// ``` - #[inline] - pub fn from_parts(parts: Parts, body: T) -> Request { - Request { - head: parts, - body: body, - } - } - - /// Returns a reference to the associated HTTP method. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request: Request<()> = Request::default(); - /// assert_eq!(*request.method(), Method::GET); - /// ``` - #[inline] - pub fn method(&self) -> &Method { - &self.head.method - } - - /// Returns a mutable reference to the associated HTTP method. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut request: Request<()> = Request::default(); - /// *request.method_mut() = Method::PUT; - /// assert_eq!(*request.method(), Method::PUT); - /// ``` - #[inline] - pub fn method_mut(&mut self) -> &mut Method { - &mut self.head.method - } - - /// Returns a reference to the associated URI. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request: Request<()> = Request::default(); - /// assert_eq!(*request.uri(), *"/"); - /// ``` - #[inline] - pub fn uri(&self) -> &Uri { - &self.head.uri - } - - /// Returns a mutable reference to the associated URI. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut request: Request<()> = Request::default(); - /// *request.uri_mut() = "/hello".parse().unwrap(); - /// assert_eq!(*request.uri(), *"/hello"); - /// ``` - #[inline] - pub fn uri_mut(&mut self) -> &mut Uri { - &mut self.head.uri - } - - /// Returns the associated version. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request: Request<()> = Request::default(); - /// assert_eq!(request.version(), Version::HTTP_11); - /// ``` - #[inline] - pub fn version(&self) -> Version { - self.head.version - } - - /// Returns a mutable reference to the associated version. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut request: Request<()> = Request::default(); - /// *request.version_mut() = Version::HTTP_2; - /// assert_eq!(request.version(), Version::HTTP_2); - /// ``` - #[inline] - pub fn version_mut(&mut self) -> &mut Version { - &mut self.head.version - } - - /// Returns a reference to the associated header field map. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request: Request<()> = Request::default(); - /// assert!(request.headers().is_empty()); - /// ``` - #[inline] - pub fn headers(&self) -> &HeaderMap { - &self.head.headers - } - - /// Returns a mutable reference to the associated header field map. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// # use http::header::*; - /// let mut request: Request<()> = Request::default(); - /// request.headers_mut().insert(HOST, HeaderValue::from_static("world")); - /// assert!(!request.headers().is_empty()); - /// ``` - #[inline] - pub fn headers_mut(&mut self) -> &mut HeaderMap { - &mut self.head.headers - } - - /// Returns a reference to the associated extensions. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request: Request<()> = Request::default(); - /// assert!(request.extensions().get::().is_none()); - /// ``` - #[inline] - pub fn extensions(&self) -> &Extensions { - &self.head.extensions - } - - /// Returns a mutable reference to the associated extensions. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// # use http::header::*; - /// let mut request: Request<()> = Request::default(); - /// request.extensions_mut().insert("hello"); - /// assert_eq!(request.extensions().get(), Some(&"hello")); - /// ``` - #[inline] - pub fn extensions_mut(&mut self) -> &mut Extensions { - &mut self.head.extensions - } - - /// Returns a reference to the associated HTTP body. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request: Request = Request::default(); - /// assert!(request.body().is_empty()); - /// ``` - #[inline] - pub fn body(&self) -> &T { - &self.body - } - - /// Returns a mutable reference to the associated HTTP body. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut request: Request = Request::default(); - /// request.body_mut().push_str("hello world"); - /// assert!(!request.body().is_empty()); - /// ``` - #[inline] - pub fn body_mut(&mut self) -> &mut T { - &mut self.body - } - - - /// Consumes the request, returning just the body. - /// - /// # Examples - /// - /// ``` - /// # use http::Request; - /// let request = Request::new(10); - /// let body = request.into_body(); - /// assert_eq!(body, 10); - /// ``` - #[inline] - pub fn into_body(self) -> T { - self.body - } - - /// Consumes the request returning the head and body parts. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request = Request::new(()); - /// let (parts, body) = request.into_parts(); - /// assert_eq!(parts.method, Method::GET); - /// ``` - #[inline] - pub fn into_parts(self) -> (Parts, T) { - (self.head, self.body) - } - - /// Consumes the request returning a new request with body mapped to the - /// return type of the passed in function. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let request = Request::builder().body("some string").unwrap(); - /// let mapped_request: Request<&[u8]> = request.map(|b| { - /// assert_eq!(b, "some string"); - /// b.as_bytes() - /// }); - /// assert_eq!(mapped_request.body(), &"some string".as_bytes()); - /// ``` - #[inline] - pub fn map(self, f: F) -> Request - where F: FnOnce(T) -> U - { - Request { body: f(self.body), head: self.head } - } -} - -impl Default for Request { - fn default() -> Request { - Request::new(T::default()) - } -} - -impl fmt::Debug for Request { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Request") - .field("method", self.method()) - .field("uri", self.uri()) - .field("version", &self.version()) - .field("headers", self.headers()) - // omits Extensions because not useful - .field("body", self.body()) - .finish() - } -} - -impl Parts { - /// Creates a new default instance of `Parts` - fn new() -> Parts { - Parts{ - method: Method::default(), - uri: Uri::default(), - version: Version::default(), - headers: HeaderMap::default(), - extensions: Extensions::default(), - _priv: (), - } - } -} - -impl fmt::Debug for Parts { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Parts") - .field("method", &self.method) - .field("uri", &self.uri) - .field("version", &self.version) - .field("headers", &self.headers) - // omits Extensions because not useful - // omits _priv because not useful - .finish() - } -} - -impl Builder { - /// Creates a new default instance of `Builder` to construct a `Request`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let req = request::Builder::new() - /// .method("POST") - /// .body(()) - /// .unwrap(); - /// ``` - #[inline] - pub fn new() -> Builder { - Builder::default() - } - - /// Set the HTTP method for this request. - /// - /// This function will configure the HTTP method of the `Request` that will - /// be returned from `Builder::build`. - /// - /// By default this is `GET`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let req = Request::builder() - /// .method("POST") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn method(&mut self, method: T) -> &mut Builder - where Method: HttpTryFrom, - { - if let Some(head) = head(&mut self.head, &self.err) { - match HttpTryFrom::try_from(method) { - Ok(s) => head.method = s, - Err(e) => self.err = Some(e.into()), - } - } - self - } - - /// Get the HTTP Method for this request. - /// - /// By default this is `GET`. - /// if builder has error, returns None. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let mut req = Request::builder(); - /// assert_eq!(req.method_ref(),Some(&Method::GET)); - /// req.method("POST"); - /// assert_eq!(req.method_ref(),Some(&Method::POST)); - /// req.method("DELETE"); - /// assert_eq!(req.method_ref(),Some(&Method::DELETE)); - /// ``` - pub fn method_ref(&self) -> Option<&Method> - { - if self.err.is_some() { - return None - } - match self.head { - Some(ref head) => Some(&head.method), - None => None - } - } - - /// Set the URI for this request. - /// - /// This function will configure the URI of the `Request` that will - /// be returned from `Builder::build`. - /// - /// By default this is `/`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let req = Request::builder() - /// .uri("https://www.rust-lang.org/") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn uri(&mut self, uri: T) -> &mut Builder - where Uri: HttpTryFrom, - { - if let Some(head) = head(&mut self.head, &self.err) { - match HttpTryFrom::try_from(uri) { - Ok(s) => head.uri = s, - Err(e) => self.err = Some(e.into()), - } - } - self - } - - /// Get the URI for this request - /// - /// By default this is `/` - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let mut req = Request::builder(); - /// assert_eq!(req.uri_ref().unwrap().to_string(), "/" ); - /// req.uri("https://www.rust-lang.org/"); - /// assert_eq!(req.uri_ref().unwrap().to_string(), "https://www.rust-lang.org/" ); - /// ``` - pub fn uri_ref(&self) -> Option<&Uri> - { - if self.err.is_some() { - return None; - } - match self.head - { - Some(ref head) => Some(&head.uri), - None => None - } - } - - /// Set the HTTP version for this request. - /// - /// This function will configure the HTTP version of the `Request` that - /// will be returned from `Builder::build`. - /// - /// By default this is HTTP/1.1 - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let req = Request::builder() - /// .version(Version::HTTP_2) - /// .body(()) - /// .unwrap(); - /// ``` - pub fn version(&mut self, version: Version) -> &mut Builder { - if let Some(head) = head(&mut self.head, &self.err) { - head.version = version; - } - self - } - - /// Appends a header to this request builder. - /// - /// This function will append the provided key/value as a header to the - /// internal `HeaderMap` being constructed. Essentially this is equivalent - /// to calling `HeaderMap::append`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// # use http::header::HeaderValue; - /// - /// let req = Request::builder() - /// .header("Accept", "text/html") - /// .header("X-Custom-Foo", "bar") - /// .body(()) - /// .unwrap(); - /// ``` - pub fn header(&mut self, key: K, value: V) -> &mut Builder - where HeaderName: HttpTryFrom, - HeaderValue: HttpTryFrom - { - if let Some(head) = head(&mut self.head, &self.err) { - match >::try_from(key) { - Ok(key) => { - match >::try_from(value) { - Ok(value) => { head.headers.append(key, value); } - Err(e) => self.err = Some(e.into()), - } - }, - Err(e) => self.err = Some(e.into()), - }; - } - self - } - - /// Get header on this request builder. - /// when builder has error returns None - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// # use http::header::HeaderValue; - /// # use http::request::Builder; - /// let mut req = Request::builder(); - /// req.header("Accept", "text/html") - /// .header("X-Custom-Foo", "bar"); - /// let headers = req.headers_ref().unwrap(); - /// assert_eq!( headers["Accept"], "text/html" ); - /// assert_eq!( headers["X-Custom-Foo"], "bar" ); - /// ``` - pub fn headers_ref(&self) -> Option<&HeaderMap> { - if self.err.is_some() { - return None; - } - match self.head - { - Some(ref head) => Some(&head.headers), - None => None - } - } - - /// Get header on this request builder. - /// when builder has error returns None - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// # use http::header::HeaderValue; - /// # use http::request::Builder; - /// let mut req = Request::builder(); - /// { - /// let headers = req.headers_mut().unwrap(); - /// headers.insert("Accept", HeaderValue::from_static("text/html")); - /// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar")); - /// } - /// let headers = req.headers_ref().unwrap(); - /// assert_eq!( headers["Accept"], "text/html" ); - /// assert_eq!( headers["X-Custom-Foo"], "bar" ); - /// ``` - pub fn headers_mut(&mut self) -> Option<&mut HeaderMap> { - if self.err.is_some() { - return None; - } - match self.head - { - Some(ref mut head) => Some(&mut head.headers), - None => None - } - } - - /// Adds an extension to this builder - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let req = Request::builder() - /// .extension("My Extension") - /// .body(()) - /// .unwrap(); - /// - /// assert_eq!(req.extensions().get::<&'static str>(), - /// Some(&"My Extension")); - /// ``` - pub fn extension(&mut self, extension: T) -> &mut Builder - where T: Any + Send + Sync + 'static, - { - if let Some(head) = head(&mut self.head, &self.err) { - head.extensions.insert(extension); - } - self - } - - fn take_parts(&mut self) -> Result { - let ret = self.head.take().expect("cannot reuse request builder"); - if let Some(e) = self.err.take() { - return Err(e) - } - Ok(ret) - } - - /// "Consumes" this builder, using the provided `body` to return a - /// constructed `Request`. - /// - /// # Errors - /// - /// This function may return an error if any previously configured argument - /// failed to parse or get converted to the internal representation. For - /// example if an invalid `head` was specified via `header("Foo", - /// "Bar\r\n")` the error will be returned when this function is called - /// rather than when `header` was called. - /// - /// # Panics - /// - /// This method will panic if the builder is reused. The `body` function can - /// only be called once. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let request = Request::builder() - /// .body(()) - /// .unwrap(); - /// ``` - pub fn body(&mut self, body: T) -> Result> { - Ok(Request { - head: self.take_parts()?, - body: body, - }) - } -} - -fn head<'a>(head: &'a mut Option, err: &Option) - -> Option<&'a mut Parts> -{ - if err.is_some() { - return None - } - head.as_mut() -} - -impl Default for Builder { - #[inline] - fn default() -> Builder { - Builder { - head: Some(Parts::new()), - err: None, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_can_map_a_body_from_one_type_to_another() { - let request= Request::builder().body("some string").unwrap(); - let mapped_request = request.map(|s| { - assert_eq!(s, "some string"); - 123u32 - }); - assert_eq!(mapped_request.body(), &123u32); - } -} diff --git a/third_party/rust/http/v0_1/crate/src/response.rs b/third_party/rust/http/v0_1/crate/src/response.rs deleted file mode 100644 index fc877509f3c7..000000000000 --- a/third_party/rust/http/v0_1/crate/src/response.rs +++ /dev/null @@ -1,787 +0,0 @@ -//! HTTP response types. -//! -//! This module contains structs related to HTTP responses, notably the -//! `Response` type itself as well as a builder to create responses. Typically -//! you'll import the `http::Response` type rather than reaching into this -//! module itself. -//! -//! # Examples -//! -//! Creating a `Response` to return -//! -//! ``` -//! use http::{Request, Response, StatusCode}; -//! -//! fn respond_to(req: Request<()>) -> http::Result> { -//! let mut response = Response::builder(); -//! response.header("Foo", "Bar") -//! .status(StatusCode::OK); -//! -//! if req.headers().contains_key("Another-Header") { -//! response.header("Another-Header", "Ack"); -//! } -//! -//! response.body(()) -//! } -//! ``` -//! -//! A simple 404 handler -//! -//! ``` -//! use http::{Request, Response, StatusCode}; -//! -//! fn not_found(_req: Request<()>) -> http::Result> { -//! Response::builder() -//! .status(StatusCode::NOT_FOUND) -//! .body(()) -//! } -//! ``` -//! -//! Or otherwise inspecting the result of a request: -//! -//! ```no_run -//! use http::{Request, Response}; -//! -//! fn get(url: &str) -> http::Result> { -//! // ... -//! # panic!() -//! } -//! -//! let response = get("https://www.rust-lang.org/").unwrap(); -//! -//! if !response.status().is_success() { -//! panic!("failed to get a successful response status!"); -//! } -//! -//! if let Some(date) = response.headers().get("Date") { -//! // we've got a `Date` header! -//! } -//! -//! let body = response.body(); -//! // ... -//! ``` - -use std::any::Any; -use std::fmt; - -use {Error, Result, HttpTryFrom, Extensions}; -use header::{HeaderMap, HeaderName, HeaderValue}; -use status::StatusCode; -use version::Version; - -/// Represents an HTTP response -/// -/// An HTTP response consists of a head and a potentially optional body. The body -/// component is generic, enabling arbitrary types to represent the HTTP body. -/// For example, the body could be `Vec`, a `Stream` of byte chunks, or a -/// value that has been deserialized. -/// -/// Typically you'll work with responses on the client side as the result of -/// sending a `Request` and on the server you'll be generating a `Request` to -/// send back to the client. -/// -/// # Examples -/// -/// Creating a `Response` to return -/// -/// ``` -/// use http::{Request, Response, StatusCode}; -/// -/// fn respond_to(req: Request<()>) -> http::Result> { -/// let mut response = Response::builder(); -/// response.header("Foo", "Bar") -/// .status(StatusCode::OK); -/// -/// if req.headers().contains_key("Another-Header") { -/// response.header("Another-Header", "Ack"); -/// } -/// -/// response.body(()) -/// } -/// ``` -/// -/// A simple 404 handler -/// -/// ``` -/// use http::{Request, Response, StatusCode}; -/// -/// fn not_found(_req: Request<()>) -> http::Result> { -/// Response::builder() -/// .status(StatusCode::NOT_FOUND) -/// .body(()) -/// } -/// ``` -/// -/// Or otherwise inspecting the result of a request: -/// -/// ```no_run -/// use http::{Request, Response}; -/// -/// fn get(url: &str) -> http::Result> { -/// // ... -/// # panic!() -/// } -/// -/// let response = get("https://www.rust-lang.org/").unwrap(); -/// -/// if !response.status().is_success() { -/// panic!("failed to get a successful response status!"); -/// } -/// -/// if let Some(date) = response.headers().get("Date") { -/// // we've got a `Date` header! -/// } -/// -/// let body = response.body(); -/// // ... -/// ``` -/// -/// Deserialize a response of bytes via json: -/// -/// ``` -/// # extern crate serde; -/// # extern crate serde_json; -/// # extern crate http; -/// use http::Response; -/// use serde::de; -/// -/// fn deserialize(req: Response>) -> serde_json::Result> -/// where for<'de> T: de::Deserialize<'de>, -/// { -/// let (parts, body) = req.into_parts(); -/// let body = serde_json::from_slice(&body)?; -/// Ok(Response::from_parts(parts, body)) -/// } -/// # -/// # fn main() {} -/// ``` -/// -/// Or alternatively, serialize the body of a response to json -/// -/// ``` -/// # extern crate serde; -/// # extern crate serde_json; -/// # extern crate http; -/// use http::Response; -/// use serde::ser; -/// -/// fn serialize(req: Response) -> serde_json::Result>> -/// where T: ser::Serialize, -/// { -/// let (parts, body) = req.into_parts(); -/// let body = serde_json::to_vec(&body)?; -/// Ok(Response::from_parts(parts, body)) -/// } -/// # -/// # fn main() {} -/// ``` -pub struct Response { - head: Parts, - body: T, -} - -/// Component parts of an HTTP `Response` -/// -/// The HTTP response head consists of a status, version, and a set of -/// header fields. -pub struct Parts { - /// The response's status - pub status: StatusCode, - - /// The response's version - pub version: Version, - - /// The response's headers - pub headers: HeaderMap, - - /// The response's extensions - pub extensions: Extensions, - - _priv: (), -} - -/// An HTTP response builder -/// -/// This type can be used to construct an instance of `Response` through a -/// builder-like pattern. -#[derive(Debug)] -pub struct Builder { - head: Option, - err: Option, -} - -impl Response<()> { - /// Creates a new builder-style object to manufacture a `Response` - /// - /// This method returns an instance of `Builder` which can be used to - /// create a `Response`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response = Response::builder() - /// .status(200) - /// .header("X-Custom-Foo", "Bar") - /// .body(()) - /// .unwrap(); - /// ``` - #[inline] - pub fn builder() -> Builder { - Builder::new() - } -} - -impl Response { - /// Creates a new blank `Response` with the body - /// - /// The component ports of this response will be set to their default, e.g. - /// the ok status, no headers, etc. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response = Response::new("hello world"); - /// - /// assert_eq!(response.status(), StatusCode::OK); - /// assert_eq!(*response.body(), "hello world"); - /// ``` - #[inline] - pub fn new(body: T) -> Response { - Response { - head: Parts::new(), - body: body, - } - } - - /// Creates a new `Response` with the given head and body - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response = Response::new("hello world"); - /// let (mut parts, body) = response.into_parts(); - /// - /// parts.status = StatusCode::BAD_REQUEST; - /// let response = Response::from_parts(parts, body); - /// - /// assert_eq!(response.status(), StatusCode::BAD_REQUEST); - /// assert_eq!(*response.body(), "hello world"); - /// ``` - #[inline] - pub fn from_parts(parts: Parts, body: T) -> Response { - Response { - head: parts, - body: body, - } - } - - /// Returns the `StatusCode`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response: Response<()> = Response::default(); - /// assert_eq!(response.status(), StatusCode::OK); - /// ``` - #[inline] - pub fn status(&self) -> StatusCode { - self.head.status - } - - /// Returns a mutable reference to the associated `StatusCode`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut response: Response<()> = Response::default(); - /// *response.status_mut() = StatusCode::CREATED; - /// assert_eq!(response.status(), StatusCode::CREATED); - /// ``` - #[inline] - pub fn status_mut(&mut self) -> &mut StatusCode { - &mut self.head.status - } - - /// Returns a reference to the associated version. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response: Response<()> = Response::default(); - /// assert_eq!(response.version(), Version::HTTP_11); - /// ``` - #[inline] - pub fn version(&self) -> Version { - self.head.version - } - - /// Returns a mutable reference to the associated version. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut response: Response<()> = Response::default(); - /// *response.version_mut() = Version::HTTP_2; - /// assert_eq!(response.version(), Version::HTTP_2); - /// ``` - #[inline] - pub fn version_mut(&mut self) -> &mut Version { - &mut self.head.version - } - - /// Returns a reference to the associated header field map. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response: Response<()> = Response::default(); - /// assert!(response.headers().is_empty()); - /// ``` - #[inline] - pub fn headers(&self) -> &HeaderMap { - &self.head.headers - } - - /// Returns a mutable reference to the associated header field map. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// # use http::header::*; - /// let mut response: Response<()> = Response::default(); - /// response.headers_mut().insert(HOST, HeaderValue::from_static("world")); - /// assert!(!response.headers().is_empty()); - /// ``` - #[inline] - pub fn headers_mut(&mut self) -> &mut HeaderMap { - &mut self.head.headers - } - - /// Returns a reference to the associated extensions. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response: Response<()> = Response::default(); - /// assert!(response.extensions().get::().is_none()); - /// ``` - #[inline] - pub fn extensions(&self) -> &Extensions { - &self.head.extensions - } - - /// Returns a mutable reference to the associated extensions. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// # use http::header::*; - /// let mut response: Response<()> = Response::default(); - /// response.extensions_mut().insert("hello"); - /// assert_eq!(response.extensions().get(), Some(&"hello")); - /// ``` - #[inline] - pub fn extensions_mut(&mut self) -> &mut Extensions { - &mut self.head.extensions - } - - /// Returns a reference to the associated HTTP body. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response: Response = Response::default(); - /// assert!(response.body().is_empty()); - /// ``` - #[inline] - pub fn body(&self) -> &T { - &self.body - } - - /// Returns a mutable reference to the associated HTTP body. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let mut response: Response = Response::default(); - /// response.body_mut().push_str("hello world"); - /// assert!(!response.body().is_empty()); - /// ``` - #[inline] - pub fn body_mut(&mut self) -> &mut T { - &mut self.body - } - - /// Consumes the response, returning just the body. - /// - /// # Examples - /// - /// ``` - /// # use http::Response; - /// let response = Response::new(10); - /// let body = response.into_body(); - /// assert_eq!(body, 10); - /// ``` - #[inline] - pub fn into_body(self) -> T { - self.body - } - - /// Consumes the response returning the head and body parts. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response: Response<()> = Response::default(); - /// let (parts, body) = response.into_parts(); - /// assert_eq!(parts.status, StatusCode::OK); - /// ``` - #[inline] - pub fn into_parts(self) -> (Parts, T) { - (self.head, self.body) - } - - /// Consumes the response returning a new response with body mapped to the - /// return type of the passed in function. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// let response = Response::builder().body("some string").unwrap(); - /// let mapped_response: Response<&[u8]> = response.map(|b| { - /// assert_eq!(b, "some string"); - /// b.as_bytes() - /// }); - /// assert_eq!(mapped_response.body(), &"some string".as_bytes()); - /// ``` - #[inline] - pub fn map(self, f: F) -> Response - where F: FnOnce(T) -> U - { - Response { body: f(self.body), head: self.head } - } -} - -impl Default for Response { - #[inline] - fn default() -> Response { - Response::new(T::default()) - } -} - -impl fmt::Debug for Response { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Response") - .field("status", &self.status()) - .field("version", &self.version()) - .field("headers", self.headers()) - // omits Extensions because not useful - .field("body", self.body()) - .finish() - } -} - -impl Parts { - /// Creates a new default instance of `Parts` - fn new() -> Parts { - Parts{ - status: StatusCode::default(), - version: Version::default(), - headers: HeaderMap::default(), - extensions: Extensions::default(), - _priv: (), - } - } -} - -impl fmt::Debug for Parts { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Parts") - .field("status", &self.status) - .field("version", &self.version) - .field("headers", &self.headers) - // omits Extensions because not useful - // omits _priv because not useful - .finish() - } -} - -impl Builder { - /// Creates a new default instance of `Builder` to construct either a - /// `Head` or a `Response`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let response = response::Builder::new() - /// .status(200) - /// .body(()) - /// .unwrap(); - /// ``` - #[inline] - pub fn new() -> Builder { - Builder::default() - } - - /// Set the HTTP status for this response. - /// - /// This function will configure the HTTP status code of the `Response` that - /// will be returned from `Builder::build`. - /// - /// By default this is `200`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let response = Response::builder() - /// .status(200) - /// .body(()) - /// .unwrap(); - /// ``` - pub fn status(&mut self, status: T) -> &mut Builder - where StatusCode: HttpTryFrom, - { - if let Some(head) = head(&mut self.head, &self.err) { - match HttpTryFrom::try_from(status) { - Ok(s) => head.status = s, - Err(e) => self.err = Some(e.into()), - } - } - self - } - - /// Set the HTTP version for this response. - /// - /// This function will configure the HTTP version of the `Response` that - /// will be returned from `Builder::build`. - /// - /// By default this is HTTP/1.1 - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let response = Response::builder() - /// .version(Version::HTTP_2) - /// .body(()) - /// .unwrap(); - /// ``` - pub fn version(&mut self, version: Version) -> &mut Builder { - if let Some(head) = head(&mut self.head, &self.err) { - head.version = version; - } - self - } - - /// Appends a header to this response builder. - /// - /// This function will append the provided key/value as a header to the - /// internal `HeaderMap` being constructed. Essentially this is equivalent - /// to calling `HeaderMap::append`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// # use http::header::HeaderValue; - /// - /// let response = Response::builder() - /// .header("Content-Type", "text/html") - /// .header("X-Custom-Foo", "bar") - /// .header("content-length", 0) - /// .body(()) - /// .unwrap(); - /// ``` - pub fn header(&mut self, key: K, value: V) -> &mut Builder - where HeaderName: HttpTryFrom, - HeaderValue: HttpTryFrom - { - if let Some(head) = head(&mut self.head, &self.err) { - match >::try_from(key) { - Ok(key) => { - match >::try_from(value) { - Ok(value) => { head.headers.append(key, value); } - Err(e) => self.err = Some(e.into()), - } - }, - Err(e) => self.err = Some(e.into()), - }; - } - self - } - - /// Get header on this response builder. - /// when builder has error returns None - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// # use http::header::HeaderValue; - /// # use http::response::Builder; - /// let mut res = Response::builder(); - /// res.header("Accept", "text/html") - /// .header("X-Custom-Foo", "bar"); - /// let headers = res.headers_ref().unwrap(); - /// assert_eq!( headers["Accept"], "text/html" ); - /// assert_eq!( headers["X-Custom-Foo"], "bar" ); - /// ``` - pub fn headers_ref(&self) -> Option<&HeaderMap> { - if self.err.is_some() { - return None; - } - match self.head - { - Some(ref head) => Some(&head.headers), - None => None - } - } - - /// Get header on this response builder. - /// when builder has error returns None - /// - /// # Example - /// - /// ``` - /// # use http::*; - /// # use http::header::HeaderValue; - /// # use http::response::Builder; - /// let mut res = Response::builder(); - /// { - /// let headers = res.headers_mut().unwrap(); - /// headers.insert("Accept", HeaderValue::from_static("text/html")); - /// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar")); - /// } - /// let headers = res.headers_ref().unwrap(); - /// assert_eq!( headers["Accept"], "text/html" ); - /// assert_eq!( headers["X-Custom-Foo"], "bar" ); - /// ``` - pub fn headers_mut(&mut self) -> Option<&mut HeaderMap> { - if self.err.is_some() { - return None; - } - match self.head - { - Some(ref mut head) => Some(&mut head.headers), - None => None - } - } - - /// Adds an extension to this builder - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let response = Response::builder() - /// .extension("My Extension") - /// .body(()) - /// .unwrap(); - /// - /// assert_eq!(response.extensions().get::<&'static str>(), - /// Some(&"My Extension")); - /// ``` - pub fn extension(&mut self, extension: T) -> &mut Builder - where T: Any + Send + Sync + 'static, - { - if let Some(head) = head(&mut self.head, &self.err) { - head.extensions.insert(extension); - } - self - } - - fn take_parts(&mut self) -> Result { - let ret = self.head.take().expect("cannot reuse response builder"); - if let Some(e) = self.err.take() { - return Err(e) - } - Ok(ret) - } - - /// "Consumes" this builder, using the provided `body` to return a - /// constructed `Response`. - /// - /// # Errors - /// - /// This function may return an error if any previously configured argument - /// failed to parse or get converted to the internal representation. For - /// example if an invalid `head` was specified via `header("Foo", - /// "Bar\r\n")` the error will be returned when this function is called - /// rather than when `header` was called. - /// - /// # Panics - /// - /// This method will panic if the builder is reused. The `body` function can - /// only be called once. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let response = Response::builder() - /// .body(()) - /// .unwrap(); - /// ``` - pub fn body(&mut self, body: T) -> Result> { - Ok(Response { - head: self.take_parts()?, - body: body, - }) - } -} - -fn head<'a>(head: &'a mut Option, err: &Option) - -> Option<&'a mut Parts> -{ - if err.is_some() { - return None - } - head.as_mut() -} - -impl Default for Builder { - #[inline] - fn default() -> Builder { - Builder { - head: Some(Parts::new()), - err: None, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_can_map_a_body_from_one_type_to_another() { - let response = Response::builder().body("some string").unwrap(); - let mapped_response = response.map(|s| { - assert_eq!(s, "some string"); - 123u32 - }); - assert_eq!(mapped_response.body(), &123u32); - } -} diff --git a/third_party/rust/http/v0_1/crate/src/status.rs b/third_party/rust/http/v0_1/crate/src/status.rs deleted file mode 100644 index ef95c9092159..000000000000 --- a/third_party/rust/http/v0_1/crate/src/status.rs +++ /dev/null @@ -1,570 +0,0 @@ -//! HTTP status codes -//! -//! This module contains HTTP-status code related structs an errors. The main -//! type in this module is `StatusCode` which is not intended to be used through -//! this module but rather the `http::StatusCode` type. -//! -//! # Examples -//! -//! ``` -//! use http::StatusCode; -//! -//! assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); -//! assert_eq!(StatusCode::NOT_FOUND, 404); -//! assert!(StatusCode::OK.is_success()); -//! ``` - -use std::fmt; -use std::error::Error; -use std::str::FromStr; - -use HttpTryFrom; - -/// An HTTP status code (`status-code` in RFC 7230 et al.). -/// -/// This type contains constants for all common status codes. -/// It allows status codes in the range [100, 599]. -/// -/// IANA maintain the [Hypertext Transfer Protocol (HTTP) Status Code -/// Registry](http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml) which is -/// the source for this enum (with one exception, 418 I'm a teapot, which is -/// inexplicably not in the register). -/// -/// # Examples -/// -/// ``` -/// use http::StatusCode; -/// -/// assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); -/// assert_eq!(StatusCode::NOT_FOUND.as_u16(), 404); -/// assert!(StatusCode::OK.is_success()); -/// ``` -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct StatusCode(u16); - -/// A possible error value when converting a `StatusCode` from a `u16` or `&str` -/// -/// This error indicates that the supplied input was not a valid number, was less -/// than 100, or was greater than 599. -pub struct InvalidStatusCode { - _priv: (), -} - -impl StatusCode { - /// Converts a u16 to a status code. - /// - /// The function validates the correctness of the supplied u16. It must be - /// greater or equal to 100 but less than 600. - /// - /// # Example - /// - /// ``` - /// use http::StatusCode; - /// - /// let ok = StatusCode::from_u16(200).unwrap(); - /// assert_eq!(ok, StatusCode::OK); - /// - /// let err = StatusCode::from_u16(99); - /// assert!(err.is_err()); - /// ``` - #[inline] - pub fn from_u16(src: u16) -> Result { - if src < 100 || src >= 600 { - return Err(InvalidStatusCode::new()); - } - - Ok(StatusCode(src)) - } - - /// Converts a &[u8] to a status code - pub fn from_bytes(src: &[u8]) -> Result { - if src.len() != 3 { - return Err(InvalidStatusCode::new()); - } - - let a = src[0].wrapping_sub(b'0') as u16; - let b = src[1].wrapping_sub(b'0') as u16; - let c = src[2].wrapping_sub(b'0') as u16; - - if a == 0 || a > 5 || b > 9 || c > 9 { - return Err(InvalidStatusCode::new()); - } - - let status = (a * 100) + (b * 10) + c; - Ok(StatusCode(status)) - } - - /// Returns the `u16` corresponding to this `StatusCode`. - /// - /// # Note - /// - /// This is the same as the `From` implementation, but - /// included as an inherent method because that implementation doesn't - /// appear in rustdocs, as well as a way to force the type instead of - /// relying on inference. - /// - /// # Example - /// - /// ``` - /// let status = http::StatusCode::OK; - /// assert_eq!(status.as_u16(), 200); - /// ``` - #[inline] - pub fn as_u16(&self) -> u16 { - (*self).into() - } - - /// Returns a &str representation of the `StatusCode` - /// - /// The return value only includes a numerical representation of the - /// status code. The canonical reason is not included. - /// - /// # Example - /// - /// ``` - /// let status = http::StatusCode::OK; - /// assert_eq!(status.as_str(), "200"); - /// ``` - #[inline] - pub fn as_str(&self) -> &str { - CODES_AS_STR[(self.0 - 100) as usize] - } - - /// Get the standardised `reason-phrase` for this status code. - /// - /// This is mostly here for servers writing responses, but could potentially have application - /// at other times. - /// - /// The reason phrase is defined as being exclusively for human readers. You should avoid - /// deriving any meaning from it at all costs. - /// - /// Bear in mind also that in HTTP/2.0 the reason phrase is abolished from transmission, and so - /// this canonical reason phrase really is the only reason phrase you’ll find. - /// - /// # Example - /// - /// ``` - /// let status = http::StatusCode::OK; - /// assert_eq!(status.canonical_reason(), Some("OK")); - /// ``` - pub fn canonical_reason(&self) -> Option<&'static str> { - canonical_reason(self.0) - } - - - /// Check if status is within 100-199. - #[inline] - pub fn is_informational(&self) -> bool { - 200 > self.0 && self.0 >= 100 - } - - /// Check if status is within 200-299. - #[inline] - pub fn is_success(&self) -> bool { - 300 > self.0 && self.0 >= 200 - } - - /// Check if status is within 300-399. - #[inline] - pub fn is_redirection(&self) -> bool { - 400 > self.0 && self.0 >= 300 - } - - /// Check if status is within 400-499. - #[inline] - pub fn is_client_error(&self) -> bool { - 500 > self.0 && self.0 >= 400 - } - - /// Check if status is within 500-599. - #[inline] - pub fn is_server_error(&self) -> bool { - 600 > self.0 && self.0 >= 500 - } -} - -impl fmt::Debug for StatusCode { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - -/// Formats the status code, *including* the canonical reason. -/// -/// # Example -/// -/// ``` -/// # use http::StatusCode; -/// assert_eq!(format!("{}", StatusCode::OK), "200 OK"); -/// ``` -impl fmt::Display for StatusCode { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} {}", u16::from(*self), - self.canonical_reason().unwrap_or("")) - } -} - -impl Default for StatusCode { - #[inline] - fn default() -> StatusCode { - StatusCode::OK - } -} - -impl PartialEq for StatusCode { - #[inline] - fn eq(&self, other: &u16) -> bool { - self.as_u16() == *other - } -} - -impl PartialEq for u16 { - #[inline] - fn eq(&self, other: &StatusCode) -> bool { - *self == other.as_u16() - } -} - -impl From for u16 { - #[inline] - fn from(status: StatusCode) -> u16 { - status.0 - } -} - -impl FromStr for StatusCode { - type Err = InvalidStatusCode; - - fn from_str(s: &str) -> Result { - StatusCode::from_bytes(s.as_ref()) - } -} - -impl<'a> From<&'a StatusCode> for StatusCode { - #[inline] - fn from(t: &'a StatusCode) -> Self { - t.clone() - } -} - -impl<'a> HttpTryFrom<&'a StatusCode> for StatusCode { - type Error = ::error::Never; - - #[inline] - fn try_from(t: &'a StatusCode) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for StatusCode { - type Error = InvalidStatusCode; - - #[inline] - fn try_from(t: &'a [u8]) -> Result { - StatusCode::from_bytes(t) - } -} - -impl<'a> HttpTryFrom<&'a str> for StatusCode { - type Error = InvalidStatusCode; - - #[inline] - fn try_from(t: &'a str) -> Result { - t.parse() - } -} - -impl HttpTryFrom for StatusCode { - type Error = InvalidStatusCode; - - #[inline] - fn try_from(t: u16) -> Result { - StatusCode::from_u16(t) - } -} - -macro_rules! status_codes { - ( - $( - $(#[$docs:meta])* - ($num:expr, $konst:ident, $phrase:expr); - )+ - ) => { - impl StatusCode { - $( - $(#[$docs])* - pub const $konst: StatusCode = StatusCode($num); - )+ - - } - - fn canonical_reason(num: u16) -> Option<&'static str> { - match num { - $( - $num => Some($phrase), - )+ - _ => None - } - } - } -} - -status_codes! { - /// 100 Continue - /// [[RFC7231, Section 6.2.1](https://tools.ietf.org/html/rfc7231#section-6.2.1)] - (100, CONTINUE, "Continue"); - /// 101 Switching Protocols - /// [[RFC7231, Section 6.2.2](https://tools.ietf.org/html/rfc7231#section-6.2.2)] - (101, SWITCHING_PROTOCOLS, "Switching Protocols"); - /// 102 Processing - /// [[RFC2518](https://tools.ietf.org/html/rfc2518)] - (102, PROCESSING, "Processing"); - - /// 200 OK - /// [[RFC7231, Section 6.3.1](https://tools.ietf.org/html/rfc7231#section-6.3.1)] - (200, OK, "OK"); - /// 201 Created - /// [[RFC7231, Section 6.3.2](https://tools.ietf.org/html/rfc7231#section-6.3.2)] - (201, CREATED, "Created"); - /// 202 Accepted - /// [[RFC7231, Section 6.3.3](https://tools.ietf.org/html/rfc7231#section-6.3.3)] - (202, ACCEPTED, "Accepted"); - /// 203 Non-Authoritative Information - /// [[RFC7231, Section 6.3.4](https://tools.ietf.org/html/rfc7231#section-6.3.4)] - (203, NON_AUTHORITATIVE_INFORMATION, "Non Authoritative Information"); - /// 204 No Content - /// [[RFC7231, Section 6.3.5](https://tools.ietf.org/html/rfc7231#section-6.3.5)] - (204, NO_CONTENT, "No Content"); - /// 205 Reset Content - /// [[RFC7231, Section 6.3.6](https://tools.ietf.org/html/rfc7231#section-6.3.6)] - (205, RESET_CONTENT, "Reset Content"); - /// 206 Partial Content - /// [[RFC7233, Section 4.1](https://tools.ietf.org/html/rfc7233#section-4.1)] - (206, PARTIAL_CONTENT, "Partial Content"); - /// 207 Multi-Status - /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] - (207, MULTI_STATUS, "Multi-Status"); - /// 208 Already Reported - /// [[RFC5842](https://tools.ietf.org/html/rfc5842)] - (208, ALREADY_REPORTED, "Already Reported"); - - /// 226 IM Used - /// [[RFC3229](https://tools.ietf.org/html/rfc3229)] - (226, IM_USED, "IM Used"); - - /// 300 Multiple Choices - /// [[RFC7231, Section 6.4.1](https://tools.ietf.org/html/rfc7231#section-6.4.1)] - (300, MULTIPLE_CHOICES, "Multiple Choices"); - /// 301 Moved Permanently - /// [[RFC7231, Section 6.4.2](https://tools.ietf.org/html/rfc7231#section-6.4.2)] - (301, MOVED_PERMANENTLY, "Moved Permanently"); - /// 302 Found - /// [[RFC7231, Section 6.4.3](https://tools.ietf.org/html/rfc7231#section-6.4.3)] - (302, FOUND, "Found"); - /// 303 See Other - /// [[RFC7231, Section 6.4.4](https://tools.ietf.org/html/rfc7231#section-6.4.4)] - (303, SEE_OTHER, "See Other"); - /// 304 Not Modified - /// [[RFC7232, Section 4.1](https://tools.ietf.org/html/rfc7232#section-4.1)] - (304, NOT_MODIFIED, "Not Modified"); - /// 305 Use Proxy - /// [[RFC7231, Section 6.4.5](https://tools.ietf.org/html/rfc7231#section-6.4.5)] - (305, USE_PROXY, "Use Proxy"); - /// 307 Temporary Redirect - /// [[RFC7231, Section 6.4.7](https://tools.ietf.org/html/rfc7231#section-6.4.7)] - (307, TEMPORARY_REDIRECT, "Temporary Redirect"); - /// 308 Permanent Redirect - /// [[RFC7238](https://tools.ietf.org/html/rfc7238)] - (308, PERMANENT_REDIRECT, "Permanent Redirect"); - - /// 400 Bad Request - /// [[RFC7231, Section 6.5.1](https://tools.ietf.org/html/rfc7231#section-6.5.1)] - (400, BAD_REQUEST, "Bad Request"); - /// 401 Unauthorized - /// [[RFC7235, Section 3.1](https://tools.ietf.org/html/rfc7235#section-3.1)] - (401, UNAUTHORIZED, "Unauthorized"); - /// 402 Payment Required - /// [[RFC7231, Section 6.5.2](https://tools.ietf.org/html/rfc7231#section-6.5.2)] - (402, PAYMENT_REQUIRED, "Payment Required"); - /// 403 Forbidden - /// [[RFC7231, Section 6.5.3](https://tools.ietf.org/html/rfc7231#section-6.5.3)] - (403, FORBIDDEN, "Forbidden"); - /// 404 Not Found - /// [[RFC7231, Section 6.5.4](https://tools.ietf.org/html/rfc7231#section-6.5.4)] - (404, NOT_FOUND, "Not Found"); - /// 405 Method Not Allowed - /// [[RFC7231, Section 6.5.5](https://tools.ietf.org/html/rfc7231#section-6.5.5)] - (405, METHOD_NOT_ALLOWED, "Method Not Allowed"); - /// 406 Not Acceptable - /// [[RFC7231, Section 6.5.6](https://tools.ietf.org/html/rfc7231#section-6.5.6)] - (406, NOT_ACCEPTABLE, "Not Acceptable"); - /// 407 Proxy Authentication Required - /// [[RFC7235, Section 3.2](https://tools.ietf.org/html/rfc7235#section-3.2)] - (407, PROXY_AUTHENTICATION_REQUIRED, "Proxy Authentication Required"); - /// 408 Request Timeout - /// [[RFC7231, Section 6.5.7](https://tools.ietf.org/html/rfc7231#section-6.5.7)] - (408, REQUEST_TIMEOUT, "Request Timeout"); - /// 409 Conflict - /// [[RFC7231, Section 6.5.8](https://tools.ietf.org/html/rfc7231#section-6.5.8)] - (409, CONFLICT, "Conflict"); - /// 410 Gone - /// [[RFC7231, Section 6.5.9](https://tools.ietf.org/html/rfc7231#section-6.5.9)] - (410, GONE, "Gone"); - /// 411 Length Required - /// [[RFC7231, Section 6.5.10](https://tools.ietf.org/html/rfc7231#section-6.5.10)] - (411, LENGTH_REQUIRED, "Length Required"); - /// 412 Precondition Failed - /// [[RFC7232, Section 4.2](https://tools.ietf.org/html/rfc7232#section-4.2)] - (412, PRECONDITION_FAILED, "Precondition Failed"); - /// 413 Payload Too Large - /// [[RFC7231, Section 6.5.11](https://tools.ietf.org/html/rfc7231#section-6.5.11)] - (413, PAYLOAD_TOO_LARGE, "Payload Too Large"); - /// 414 URI Too Long - /// [[RFC7231, Section 6.5.12](https://tools.ietf.org/html/rfc7231#section-6.5.12)] - (414, URI_TOO_LONG, "URI Too Long"); - /// 415 Unsupported Media Type - /// [[RFC7231, Section 6.5.13](https://tools.ietf.org/html/rfc7231#section-6.5.13)] - (415, UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type"); - /// 416 Range Not Satisfiable - /// [[RFC7233, Section 4.4](https://tools.ietf.org/html/rfc7233#section-4.4)] - (416, RANGE_NOT_SATISFIABLE, "Range Not Satisfiable"); - /// 417 Expectation Failed - /// [[RFC7231, Section 6.5.14](https://tools.ietf.org/html/rfc7231#section-6.5.14)] - (417, EXPECTATION_FAILED, "Expectation Failed"); - /// 418 I'm a teapot - /// [curiously not registered by IANA but [RFC2324](https://tools.ietf.org/html/rfc2324)] - (418, IM_A_TEAPOT, "I'm a teapot"); - - /// 421 Misdirected Request - /// [RFC7540, Section 9.1.2](http://tools.ietf.org/html/rfc7540#section-9.1.2) - (421, MISDIRECTED_REQUEST, "Misdirected Request"); - /// 422 Unprocessable Entity - /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] - (422, UNPROCESSABLE_ENTITY, "Unprocessable Entity"); - /// 423 Locked - /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] - (423, LOCKED, "Locked"); - /// 424 Failed Dependency - /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] - (424, FAILED_DEPENDENCY, "Failed Dependency"); - - /// 426 Upgrade Required - /// [[RFC7231, Section 6.5.15](https://tools.ietf.org/html/rfc7231#section-6.5.15)] - (426, UPGRADE_REQUIRED, "Upgrade Required"); - - /// 428 Precondition Required - /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] - (428, PRECONDITION_REQUIRED, "Precondition Required"); - /// 429 Too Many Requests - /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] - (429, TOO_MANY_REQUESTS, "Too Many Requests"); - - /// 431 Request Header Fields Too Large - /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] - (431, REQUEST_HEADER_FIELDS_TOO_LARGE, "Request Header Fields Too Large"); - - /// 451 Unavailable For Legal Reasons - /// [[RFC7725](http://tools.ietf.org/html/rfc7725)] - (451, UNAVAILABLE_FOR_LEGAL_REASONS, "Unavailable For Legal Reasons"); - - /// 500 Internal Server Error - /// [[RFC7231, Section 6.6.1](https://tools.ietf.org/html/rfc7231#section-6.6.1)] - (500, INTERNAL_SERVER_ERROR, "Internal Server Error"); - /// 501 Not Implemented - /// [[RFC7231, Section 6.6.2](https://tools.ietf.org/html/rfc7231#section-6.6.2)] - (501, NOT_IMPLEMENTED, "Not Implemented"); - /// 502 Bad Gateway - /// [[RFC7231, Section 6.6.3](https://tools.ietf.org/html/rfc7231#section-6.6.3)] - (502, BAD_GATEWAY, "Bad Gateway"); - /// 503 Service Unavailable - /// [[RFC7231, Section 6.6.4](https://tools.ietf.org/html/rfc7231#section-6.6.4)] - (503, SERVICE_UNAVAILABLE, "Service Unavailable"); - /// 504 Gateway Timeout - /// [[RFC7231, Section 6.6.5](https://tools.ietf.org/html/rfc7231#section-6.6.5)] - (504, GATEWAY_TIMEOUT, "Gateway Timeout"); - /// 505 HTTP Version Not Supported - /// [[RFC7231, Section 6.6.6](https://tools.ietf.org/html/rfc7231#section-6.6.6)] - (505, HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported"); - /// 506 Variant Also Negotiates - /// [[RFC2295](https://tools.ietf.org/html/rfc2295)] - (506, VARIANT_ALSO_NEGOTIATES, "Variant Also Negotiates"); - /// 507 Insufficient Storage - /// [[RFC4918](https://tools.ietf.org/html/rfc4918)] - (507, INSUFFICIENT_STORAGE, "Insufficient Storage"); - /// 508 Loop Detected - /// [[RFC5842](https://tools.ietf.org/html/rfc5842)] - (508, LOOP_DETECTED, "Loop Detected"); - - /// 510 Not Extended - /// [[RFC2774](https://tools.ietf.org/html/rfc2774)] - (510, NOT_EXTENDED, "Not Extended"); - /// 511 Network Authentication Required - /// [[RFC6585](https://tools.ietf.org/html/rfc6585)] - (511, NETWORK_AUTHENTICATION_REQUIRED, "Network Authentication Required"); -} - -impl InvalidStatusCode { - fn new() -> InvalidStatusCode { - InvalidStatusCode { - _priv: (), - } - } -} - -impl fmt::Debug for InvalidStatusCode { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("InvalidStatusCode") - // skip _priv noise - .finish() - } -} - -impl fmt::Display for InvalidStatusCode { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.description()) - } -} - -impl Error for InvalidStatusCode { - fn description(&self) -> &str { - "invalid status code" - } -} - -macro_rules! status_code_strs { - ($($num:expr,)+) => { - const CODES_AS_STR: [&'static str; 500] = [ $( stringify!($num), )+ ]; - } -} - -status_code_strs!( - 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, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, - 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, - 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, - 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, - ); diff --git a/third_party/rust/http/v0_1/crate/src/uri/authority.rs b/third_party/rust/http/v0_1/crate/src/uri/authority.rs deleted file mode 100644 index 0564f2b5b08b..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/authority.rs +++ /dev/null @@ -1,639 +0,0 @@ -// Deprecated in 1.26, needed until our minimum version is >=1.23. -#[allow(unused, deprecated)] -use std::ascii::AsciiExt; -use std::{cmp, fmt, str}; -use std::hash::{Hash, Hasher}; -use std::str::FromStr; - -use bytes::Bytes; - -use byte_str::ByteStr; -use convert::HttpTryFrom; -use super::{ErrorKind, InvalidUri, InvalidUriBytes, URI_CHARS, Port}; - -/// Represents the authority component of a URI. -#[derive(Clone)] -pub struct Authority { - pub(super) data: ByteStr, -} - -impl Authority { - pub(super) fn empty() -> Self { - Authority { data: ByteStr::new() } - } - - /// Attempt to convert an `Authority` from `Bytes`. - /// - /// This function will be replaced by a `TryFrom` implementation once the - /// trait lands in stable. - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("example.com"); - /// let authority = Authority::from_shared(bytes).unwrap(); - /// - /// assert_eq!(authority.host(), "example.com"); - /// # } - /// ``` - pub fn from_shared(s: Bytes) -> Result { - let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?; - - if authority_end != s.len() { - return Err(ErrorKind::InvalidUriChar.into()); - } - - Ok(Authority { - data: unsafe { ByteStr::from_utf8_unchecked(s) }, - }) - } - - /// Attempt to convert an `Authority` from a static string. - /// - /// This function will not perform any copying, and the string will be - /// checked if it is empty or contains an invalid character. - /// - /// # Panics - /// - /// This function panics if the argument contains invalid characters or - /// is empty. - /// - /// # Examples - /// - /// ``` - /// # use http::uri::Authority; - /// let authority = Authority::from_static("example.com"); - /// assert_eq!(authority.host(), "example.com"); - /// ``` - pub fn from_static(src: &'static str) -> Self { - let s = src.as_bytes(); - let b = Bytes::from_static(s); - let authority_end = Authority::parse_non_empty(&b[..]).expect("static str is not valid authority"); - - if authority_end != b.len() { - panic!("static str is not valid authority"); - } - - Authority { - data: unsafe { ByteStr::from_utf8_unchecked(b) }, - } - } - - // Note: this may return an *empty* Authority. You might want `parse_non_empty`. - pub(super) fn parse(s: &[u8]) -> Result { - let mut colon_cnt = 0; - let mut start_bracket = false; - let mut end_bracket = false; - let mut has_percent = false; - let mut end = s.len(); - let mut at_sign_pos = None; - - for (i, &b) in s.iter().enumerate() { - match URI_CHARS[b as usize] { - b'/' | b'?' | b'#' => { - end = i; - break; - } - b':' => { - colon_cnt += 1; - } - b'[' => { - start_bracket = true; - if has_percent { - // Something other than the userinfo has a `%`, so reject it. - return Err(ErrorKind::InvalidAuthority.into()); - } - } - b']' => { - end_bracket = true; - - // Those were part of an IPv6 hostname, so forget them... - colon_cnt = 0; - has_percent = false; - } - b'@' => { - at_sign_pos = Some(i); - - // Those weren't a port colon, but part of the - // userinfo, so it needs to be forgotten. - colon_cnt = 0; - has_percent = false; - } - 0 if b == b'%' => { - // Per https://tools.ietf.org/html/rfc3986#section-3.2.1 and - // https://url.spec.whatwg.org/#authority-state - // the userinfo can have a percent-encoded username and password, - // so record that a `%` was found. If this turns out to be - // part of the userinfo, this flag will be cleared. - // Also per https://tools.ietf.org/html/rfc6874, percent-encoding can - // be used to indicate a zone identifier. - // If the flag hasn't been cleared at the end, that means this - // was part of the hostname (and not part of an IPv6 address), and - // will fail with an error. - has_percent = true; - } - 0 => { - return Err(ErrorKind::InvalidUriChar.into()); - } - _ => {} - } - } - - if start_bracket ^ end_bracket { - return Err(ErrorKind::InvalidAuthority.into()); - } - - if colon_cnt > 1 { - // Things like 'localhost:8080:3030' are rejected. - return Err(ErrorKind::InvalidAuthority.into()); - } - - if end > 0 && at_sign_pos == Some(end - 1) { - // If there's nothing after an `@`, this is bonkers. - return Err(ErrorKind::InvalidAuthority.into()); - } - - if has_percent { - // Something after the userinfo has a `%`, so reject it. - return Err(ErrorKind::InvalidAuthority.into()); - } - - Ok(end) - } - - // Parse bytes as an Authority, not allowing an empty string. - // - // This should be used by functions that allow a user to parse - // an `Authority` by itself. - fn parse_non_empty(s: &[u8]) -> Result { - if s.is_empty() { - return Err(ErrorKind::Empty.into()); - } - Authority::parse(s) - } - - /// Get the host of this `Authority`. - /// - /// The host subcomponent of authority is identified by an IP literal - /// encapsulated within square brackets, an IPv4 address in dotted- decimal - /// form, or a registered name. The host subcomponent is **case-insensitive**. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |---------| - /// | - /// host - /// ``` - /// - /// # Examples - /// - /// ``` - /// # use http::uri::*; - /// let authority: Authority = "example.org:80".parse().unwrap(); - /// - /// assert_eq!(authority.host(), "example.org"); - /// ``` - #[inline] - pub fn host(&self) -> &str { - host(self.as_str()) - } - - #[deprecated(since="0.1.14", note="use `port_part` or `port_u16` instead")] - #[doc(hidden)] - pub fn port(&self) -> Option { - self.port_u16() - } - - /// Get the port part of this `Authority`. - /// - /// The port subcomponent of authority is designated by an optional port - /// number following the host and delimited from it by a single colon (":") - /// character. It can be turned into a decimal port number with the `as_u16` - /// method or as a `str` with the `as_str` method. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |-| - /// | - /// port - /// ``` - /// - /// # Examples - /// - /// Authority with port - /// - /// ``` - /// # use http::uri::Authority; - /// let authority: Authority = "example.org:80".parse().unwrap(); - /// - /// let port = authority.port_part().unwrap(); - /// assert_eq!(port.as_u16(), 80); - /// assert_eq!(port.as_str(), "80"); - /// ``` - /// - /// Authority without port - /// - /// ``` - /// # use http::uri::Authority; - /// let authority: Authority = "example.org".parse().unwrap(); - /// - /// assert!(authority.port_part().is_none()); - /// ``` - pub fn port_part(&self) -> Option> { - let bytes = self.as_str(); - bytes - .rfind(":") - .and_then(|i| Port::from_str(&bytes[i + 1..]).ok()) - } - - /// Get the port of this `Authority` as a `u16`. - /// - /// # Example - /// - /// ``` - /// # use http::uri::Authority; - /// let authority: Authority = "example.org:80".parse().unwrap(); - /// - /// assert_eq!(authority.port_u16(), Some(80)); - /// ``` - pub fn port_u16(&self) -> Option { - self.port_part().and_then(|p| Some(p.as_u16())) - } - - /// Return a str representation of the authority - #[inline] - pub fn as_str(&self) -> &str { - &self.data[..] - } - - /// Converts this `Authority` back to a sequence of bytes - #[inline] - pub fn into_bytes(self) -> Bytes { - self.into() - } -} - -impl AsRef for Authority { - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl PartialEq for Authority { - fn eq(&self, other: &Authority) -> bool { - self.data.eq_ignore_ascii_case(&other.data) - } -} - -impl Eq for Authority {} - -/// Case-insensitive equality -/// -/// # Examples -/// -/// ``` -/// # use http::uri::Authority; -/// let authority: Authority = "HELLO.com".parse().unwrap(); -/// assert_eq!(authority, "hello.coM"); -/// assert_eq!("hello.com", authority); -/// ``` -impl PartialEq for Authority { - fn eq(&self, other: &str) -> bool { - self.data.eq_ignore_ascii_case(other) - } -} - -impl PartialEq for str { - fn eq(&self, other: &Authority) -> bool { - self.eq_ignore_ascii_case(other.as_str()) - } -} - -impl<'a> PartialEq for &'a str { - fn eq(&self, other: &Authority) -> bool { - self.eq_ignore_ascii_case(other.as_str()) - } -} - -impl<'a> PartialEq<&'a str> for Authority { - fn eq(&self, other: &&'a str) -> bool { - self.data.eq_ignore_ascii_case(other) - } -} - -impl PartialEq for Authority { - fn eq(&self, other: &String) -> bool { - self.data.eq_ignore_ascii_case(other.as_str()) - } -} - -impl PartialEq for String { - fn eq(&self, other: &Authority) -> bool { - self.as_str().eq_ignore_ascii_case(other.as_str()) - } -} - -/// Case-insensitive ordering -/// -/// # Examples -/// -/// ``` -/// # use http::uri::Authority; -/// let authority: Authority = "DEF.com".parse().unwrap(); -/// assert!(authority < "ghi.com"); -/// assert!(authority > "abc.com"); -/// ``` -impl PartialOrd for Authority { - fn partial_cmp(&self, other: &Authority) -> Option { - let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -impl PartialOrd for Authority { - fn partial_cmp(&self, other: &str) -> Option { - let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -impl PartialOrd for str { - fn partial_cmp(&self, other: &Authority) -> Option { - let left = self.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -impl<'a> PartialOrd for &'a str { - fn partial_cmp(&self, other: &Authority) -> Option { - let left = self.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -impl<'a> PartialOrd<&'a str> for Authority { - fn partial_cmp(&self, other: &&'a str) -> Option { - let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -impl PartialOrd for Authority { - fn partial_cmp(&self, other: &String) -> Option { - let left = self.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -impl PartialOrd for String { - fn partial_cmp(&self, other: &Authority) -> Option { - let left = self.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - let right = other.data.as_bytes().iter().map(|b| b.to_ascii_lowercase()); - left.partial_cmp(right) - } -} - -/// Case-insensitive hashing -/// -/// # Examples -/// -/// ``` -/// # use http::uri::Authority; -/// # use std::hash::{Hash, Hasher}; -/// # use std::collections::hash_map::DefaultHasher; -/// -/// let a: Authority = "HELLO.com".parse().unwrap(); -/// let b: Authority = "hello.coM".parse().unwrap(); -/// -/// let mut s = DefaultHasher::new(); -/// a.hash(&mut s); -/// let a = s.finish(); -/// -/// let mut s = DefaultHasher::new(); -/// b.hash(&mut s); -/// let b = s.finish(); -/// -/// assert_eq!(a, b); -/// ``` -impl Hash for Authority { - fn hash(&self, state: &mut H) where H: Hasher { - self.data.len().hash(state); - for &b in self.data.as_bytes() { - state.write_u8(b.to_ascii_lowercase()); - } - } -} - -impl HttpTryFrom for Authority { - type Error = InvalidUriBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - Authority::from_shared(bytes) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for Authority { - type Error = InvalidUri; - #[inline] - fn try_from(s: &'a [u8]) -> Result { - // parse first, and only turn into Bytes if valid - let end = Authority::parse_non_empty(s)?; - - if end != s.len() { - return Err(ErrorKind::InvalidAuthority.into()); - } - - Ok(Authority { - data: unsafe { ByteStr::from_utf8_unchecked(s.into()) }, - }) - } -} - -impl<'a> HttpTryFrom<&'a str> for Authority { - type Error = InvalidUri; - #[inline] - fn try_from(s: &'a str) -> Result { - HttpTryFrom::try_from(s.as_bytes()) - } -} - -impl FromStr for Authority { - type Err = InvalidUri; - - fn from_str(s: &str) -> Result { - HttpTryFrom::try_from(s) - } -} - -impl From for Bytes { - #[inline] - fn from(src: Authority) -> Bytes { - src.data.into() - } -} - -impl fmt::Debug for Authority { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.as_str()) - } -} - -impl fmt::Display for Authority { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.as_str()) - } -} - -fn host(auth: &str) -> &str { - let host_port = auth.rsplitn(2, '@') - .next() - .expect("split always has at least 1 item"); - - if host_port.as_bytes()[0] == b'[' { - let i = host_port.find(']') - .expect("parsing should validate brackets"); - // ..= ranges aren't available in 1.20, our minimum Rust version... - &host_port[0 .. i + 1] - } else { - host_port.split(':') - .next() - .expect("split always has at least 1 item") - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn parse_empty_string_is_error() { - let err = Authority::parse_non_empty(b"").unwrap_err(); - assert_eq!(err.0, ErrorKind::Empty); - } - - #[test] - fn equal_to_self_of_same_authority() { - let authority1: Authority = "example.com".parse().unwrap(); - let authority2: Authority = "EXAMPLE.COM".parse().unwrap(); - assert_eq!(authority1, authority2); - assert_eq!(authority2, authority1); - } - - #[test] - fn not_equal_to_self_of_different_authority() { - let authority1: Authority = "example.com".parse().unwrap(); - let authority2: Authority = "test.com".parse().unwrap(); - assert_ne!(authority1, authority2); - assert_ne!(authority2, authority1); - } - - #[test] - fn equates_with_a_str() { - let authority: Authority = "example.com".parse().unwrap(); - assert_eq!(&authority, "EXAMPLE.com"); - assert_eq!("EXAMPLE.com", &authority); - assert_eq!(authority, "EXAMPLE.com"); - assert_eq!("EXAMPLE.com", authority); - } - - #[test] - fn not_equal_with_a_str_of_a_different_authority() { - let authority: Authority = "example.com".parse().unwrap(); - assert_ne!(&authority, "test.com"); - assert_ne!("test.com", &authority); - assert_ne!(authority, "test.com"); - assert_ne!("test.com", authority); - } - - #[test] - fn equates_with_a_string() { - let authority: Authority = "example.com".parse().unwrap(); - assert_eq!(authority, "EXAMPLE.com".to_string()); - assert_eq!("EXAMPLE.com".to_string(), authority); - } - - #[test] - fn equates_with_a_string_of_a_different_authority() { - let authority: Authority = "example.com".parse().unwrap(); - assert_ne!(authority, "test.com".to_string()); - assert_ne!("test.com".to_string(), authority); - } - - #[test] - fn compares_to_self() { - let authority1: Authority = "abc.com".parse().unwrap(); - let authority2: Authority = "def.com".parse().unwrap(); - assert!(authority1 < authority2); - assert!(authority2 > authority1); - } - - #[test] - fn compares_with_a_str() { - let authority: Authority = "def.com".parse().unwrap(); - // with ref - assert!(&authority < "ghi.com"); - assert!("ghi.com" > &authority); - assert!(&authority > "abc.com"); - assert!("abc.com" < &authority); - - // no ref - assert!(authority < "ghi.com"); - assert!("ghi.com" > authority); - assert!(authority > "abc.com"); - assert!("abc.com" < authority); - } - - #[test] - fn compares_with_a_string() { - let authority: Authority = "def.com".parse().unwrap(); - assert!(authority < "ghi.com".to_string()); - assert!("ghi.com".to_string() > authority); - assert!(authority > "abc.com".to_string()); - assert!("abc.com".to_string() < authority); - } - - #[test] - fn allows_percent_in_userinfo() { - let authority_str = "a%2f:b%2f@example.com"; - let authority: Authority = authority_str.parse().unwrap(); - assert_eq!(authority, authority_str); - } - - #[test] - fn rejects_percent_in_hostname() { - let err = Authority::parse_non_empty(b"example%2f.com").unwrap_err(); - assert_eq!(err.0, ErrorKind::InvalidAuthority); - - let err = Authority::parse_non_empty(b"a%2f:b%2f@example%2f.com").unwrap_err(); - assert_eq!(err.0, ErrorKind::InvalidAuthority); - } - - #[test] - fn allows_percent_in_ipv6_address() { - let authority_str = "[fe80::1:2:3:4%25eth0]"; - let result: Authority = authority_str.parse().unwrap(); - assert_eq!(result, authority_str); - } - - #[test] - fn rejects_percent_outside_ipv6_address() { - let err = Authority::parse_non_empty(b"1234%20[fe80::1:2:3:4]").unwrap_err(); - assert_eq!(err.0, ErrorKind::InvalidAuthority); - - let err = Authority::parse_non_empty(b"[fe80::1:2:3:4]%20").unwrap_err(); - assert_eq!(err.0, ErrorKind::InvalidAuthority); - } -} diff --git a/third_party/rust/http/v0_1/crate/src/uri/builder.rs b/third_party/rust/http/v0_1/crate/src/uri/builder.rs deleted file mode 100644 index c6d0c93be6ea..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/builder.rs +++ /dev/null @@ -1,156 +0,0 @@ -use {Uri, Result}; -use convert::{HttpTryFrom, HttpTryInto}; -use super::{Authority, Scheme, Parts, PathAndQuery}; - -/// A builder for `Uri`s. -/// -/// This type can be used to construct an instance of `Uri` -/// through a builder pattern. -#[derive(Debug)] -pub struct Builder { - parts: Option>, -} - -impl Builder { - /// Creates a new default instance of `Builder` to construct a `Uri`. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let uri = uri::Builder::new() - /// .scheme("https") - /// .authority("hyper.rs") - /// .path_and_query("/") - /// .build() - /// .unwrap(); - /// ``` - #[inline] - pub fn new() -> Builder { - Builder::default() - } - - /// Set the `Scheme` for this URI. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let mut builder = uri::Builder::new(); - /// builder.scheme("https"); - /// ``` - pub fn scheme(&mut self, scheme: T) -> &mut Self - where - Scheme: HttpTryFrom, - { - self.map(|parts| { - parts.scheme = Some(scheme.http_try_into()?); - Ok(()) - }) - } - - /// Set the `Authority` for this URI. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let uri = uri::Builder::new() - /// .authority("tokio.rs") - /// .build() - /// .unwrap(); - /// ``` - pub fn authority(&mut self, auth: T) -> &mut Self - where - Authority: HttpTryFrom, - { - self.map(|parts| { - parts.authority = Some(auth.http_try_into()?); - Ok(()) - }) - } - - /// Set the `PathAndQuery` for this URI. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let uri = uri::Builder::new() - /// .path_and_query("/hello?foo=bar") - /// .build() - /// .unwrap(); - /// ``` - pub fn path_and_query(&mut self, p_and_q: T) -> &mut Self - where - PathAndQuery: HttpTryFrom, - { - self.map(|parts| { - parts.path_and_query = Some(p_and_q.http_try_into()?); - Ok(()) - }) - } - - /// Consumes this builder, and tries to construct a valid `Uri` from - /// the configured pieces. - /// - /// # Errors - /// - /// This function may return an error if any previously configured argument - /// failed to parse or get converted to the internal representation. For - /// example if an invalid `scheme` was specified via `scheme("!@#%/^")` - /// the error will be returned when this function is called rather than - /// when `scheme` was called. - /// - /// Additionally, the various forms of URI require certain combinations of - /// parts to be set to be valid. If the parts don't fit into any of the - /// valid forms of URI, a new error is returned. - /// - /// # Examples - /// - /// ``` - /// # use http::*; - /// - /// let uri = Uri::builder() - /// .build() - /// .unwrap(); - /// ``` - pub fn build(&mut self) -> Result { - self - .parts - .take() - .expect("cannot reuse Uri builder") - .and_then(|parts| parts.http_try_into()) - } - - fn map(&mut self, f: F) -> &mut Self - where - F: FnOnce(&mut Parts) -> Result<()>, - { - let res = if let Some(Ok(ref mut parts)) = self.parts { - f(parts) - } else { - return self; - }; - - if let Err(err) = res { - self.parts = Some(Err(err)); - } - - self - } -} - -impl Default for Builder { - #[inline] - fn default() -> Builder { - Builder { - parts: Some(Ok(Parts::default())), - } - } -} - diff --git a/third_party/rust/http/v0_1/crate/src/uri/mod.rs b/third_party/rust/http/v0_1/crate/src/uri/mod.rs deleted file mode 100644 index 0d2f34b86b1f..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/mod.rs +++ /dev/null @@ -1,1138 +0,0 @@ -//! URI component of request and response lines -//! -//! This module primarily contains the `Uri` type which is a component of all -//! HTTP requests and also reexports this type at the root of the crate. A URI -//! is not always a "full URL" in the sense of something you'd type into a web -//! browser, but HTTP requests may only have paths on servers but may have full -//! schemes and hostnames on clients. -//! -//! # Examples -//! -//! ``` -//! use http::Uri; -//! -//! let uri = "/foo/bar?baz".parse::().unwrap(); -//! assert_eq!(uri.path(), "/foo/bar"); -//! assert_eq!(uri.query(), Some("baz")); -//! assert_eq!(uri.host(), None); -//! -//! let uri = "https://www.rust-lang.org/install.html".parse::().unwrap(); -//! assert_eq!(uri.scheme_part().map(|s| s.as_str()), Some("https")); -//! assert_eq!(uri.host(), Some("www.rust-lang.org")); -//! assert_eq!(uri.path(), "/install.html"); -//! ``` - -use HttpTryFrom; -use byte_str::ByteStr; - -use bytes::Bytes; - -use std::{fmt, u8, u16}; -// Deprecated in 1.26, needed until our minimum version is >=1.23. -#[allow(unused, deprecated)] -use std::ascii::AsciiExt; -use std::hash::{Hash, Hasher}; -use std::str::{self, FromStr}; -use std::error::Error; - -use self::scheme::Scheme2; - -pub use self::authority::Authority; -pub use self::builder::Builder; -pub use self::path::PathAndQuery; -pub use self::scheme::Scheme; -pub use self::port::Port; - -mod authority; -mod builder; -mod path; -mod port; -mod scheme; -#[cfg(test)] -mod tests; - -/// The URI component of a request. -/// -/// For HTTP 1, this is included as part of the request line. From Section 5.3, -/// Request Target: -/// -/// > Once an inbound connection is obtained, the client sends an HTTP -/// > request message (Section 3) with a request-target derived from the -/// > target URI. There are four distinct formats for the request-target, -/// > depending on both the method being requested and whether the request -/// > is to a proxy. -/// > -/// > ```notrust -/// > request-target = origin-form -/// > / absolute-form -/// > / authority-form -/// > / asterisk-form -/// > ``` -/// -/// The URI is structured as follows: -/// -/// ```notrust -/// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 -/// |-| |-------------------------------||--------| |-------------------| |-----| -/// | | | | | -/// scheme authority path query fragment -/// ``` -/// -/// For HTTP 2.0, the URI is encoded using pseudoheaders. -/// -/// # Examples -/// -/// ``` -/// use http::Uri; -/// -/// let uri = "/foo/bar?baz".parse::().unwrap(); -/// assert_eq!(uri.path(), "/foo/bar"); -/// assert_eq!(uri.query(), Some("baz")); -/// assert_eq!(uri.host(), None); -/// -/// let uri = "https://www.rust-lang.org/install.html".parse::().unwrap(); -/// assert_eq!(uri.scheme_part().map(|s| s.as_str()), Some("https")); -/// assert_eq!(uri.host(), Some("www.rust-lang.org")); -/// assert_eq!(uri.path(), "/install.html"); -/// ``` -#[derive(Clone)] -pub struct Uri { - scheme: Scheme, - authority: Authority, - path_and_query: PathAndQuery, -} - -/// The various parts of a URI. -/// -/// This struct is used to provide to and retrieve from a URI. -#[derive(Debug, Default)] -pub struct Parts { - /// The scheme component of a URI - pub scheme: Option, - - /// The authority component of a URI - pub authority: Option, - - /// The origin-form component of a URI - pub path_and_query: Option, - - /// Allow extending in the future - _priv: (), -} - -/// An error resulting from a failed attempt to construct a URI. -#[derive(Debug)] -pub struct InvalidUri(ErrorKind); - -/// An error resulting from a failed attempt to construct a URI. -#[derive(Debug)] -pub struct InvalidUriBytes(InvalidUri); - -/// An error resulting from a failed attempt to construct a URI. -#[derive(Debug)] -pub struct InvalidUriParts(InvalidUri); - -#[derive(Debug, Eq, PartialEq)] -enum ErrorKind { - InvalidUriChar, - InvalidScheme, - InvalidAuthority, - InvalidPort, - InvalidFormat, - SchemeMissing, - AuthorityMissing, - PathAndQueryMissing, - TooLong, - Empty, - SchemeTooLong, -} - -// u16::MAX is reserved for None -const MAX_LEN: usize = (u16::MAX - 1) as usize; - -const URI_CHARS: [u8; 256] = [ - // 0 1 2 3 4 5 6 7 8 9 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x - 0, 0, 0, b'!', 0, b'#', b'$', 0, b'&', b'\'', // 3x - b'(', b')', b'*', b'+', b',', b'-', b'.', b'/', b'0', b'1', // 4x - b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', b';', // 5x - 0, b'=', 0, b'?', b'@', b'A', b'B', b'C', b'D', b'E', // 6x - b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x - b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x - b'Z', b'[', 0, b']', 0, b'_', 0, b'a', b'b', b'c', // 9x - b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x - b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x - b'x', b'y', b'z', 0, 0, 0, b'~', 0, 0, 0, // 12x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x - 0, 0, 0, 0, 0, 0 // 25x -]; - -impl Uri { - /// Creates a new builder-style object to manufacture a `Uri`. - /// - /// This method returns an instance of `Builder` which can be usd to - /// create a `Uri`. - /// - /// # Examples - /// - /// ``` - /// use http::Uri; - /// - /// let uri = Uri::builder() - /// .scheme("https") - /// .authority("hyper.rs") - /// .path_and_query("/") - /// .build() - /// .unwrap(); - /// ``` - pub fn builder() -> Builder { - Builder::new() - } - - /// Attempt to convert a `Uri` from `Parts` - pub fn from_parts(src: Parts) -> Result { - if src.scheme.is_some() { - if src.authority.is_none() { - return Err(ErrorKind::AuthorityMissing.into()); - } - - if src.path_and_query.is_none() { - return Err(ErrorKind::PathAndQueryMissing.into()); - } - } else { - if src.authority.is_some() && src.path_and_query.is_some() { - return Err(ErrorKind::SchemeMissing.into()); - } - } - - let scheme = match src.scheme { - Some(scheme) => scheme, - None => Scheme { inner: Scheme2::None }, - }; - - let authority = match src.authority { - Some(authority) => authority, - None => Authority::empty(), - }; - - let path_and_query = match src.path_and_query { - Some(path_and_query) => path_and_query, - None => PathAndQuery::empty(), - }; - - Ok(Uri { - scheme: scheme, - authority: authority, - path_and_query: path_and_query, - }) - } - - /// Attempt to convert a `Uri` from `Bytes` - /// - /// This function will be replaced by a `TryFrom` implementation once the - /// trait lands in stable. - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("http://example.com/foo"); - /// let uri = Uri::from_shared(bytes).unwrap(); - /// - /// assert_eq!(uri.host().unwrap(), "example.com"); - /// assert_eq!(uri.path(), "/foo"); - /// # } - /// ``` - pub fn from_shared(s: Bytes) -> Result { - use self::ErrorKind::*; - - if s.len() > MAX_LEN { - return Err(TooLong.into()); - } - - match s.len() { - 0 => { - return Err(Empty.into()); - } - 1 => { - match s[0] { - b'/' => { - return Ok(Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::slash(), - }); - } - b'*' => { - return Ok(Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::star(), - }); - } - _ => { - let authority = Authority::from_shared(s)?; - - return Ok(Uri { - scheme: Scheme::empty(), - authority: authority, - path_and_query: PathAndQuery::empty(), - }); - } - } - } - _ => {} - } - - if s[0] == b'/' { - return Ok(Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::from_shared(s)?, - }); - } - - parse_full(s) - } - - /// Convert a `Uri` from a static string. - /// - /// This function will not perform any copying, however the string is - /// checked to ensure that it is valid. - /// - /// # Panics - /// - /// This function panics if the argument is an invalid URI. - /// - /// # Examples - /// - /// ``` - /// # use http::uri::Uri; - /// let uri = Uri::from_static("http://example.com/foo"); - /// - /// assert_eq!(uri.host().unwrap(), "example.com"); - /// assert_eq!(uri.path(), "/foo"); - /// ``` - pub fn from_static(src: &'static str) -> Self { - let s = Bytes::from_static(src.as_bytes()); - match Uri::from_shared(s) { - Ok(uri) => uri, - Err(e) => panic!("static str is not valid URI: {}", e), - } - } - - /// Convert a `Uri` into `Parts`. - /// - /// # Note - /// - /// This is just an inherent method providing the same functionality as - /// `let parts: Parts = uri.into()` - /// - /// # Examples - /// - /// ``` - /// # use http::uri::*; - /// let uri: Uri = "/foo".parse().unwrap(); - /// - /// let parts = uri.into_parts(); - /// - /// assert_eq!(parts.path_and_query.unwrap(), "/foo"); - /// - /// assert!(parts.scheme.is_none()); - /// assert!(parts.authority.is_none()); - /// ``` - #[inline] - pub fn into_parts(self) -> Parts { - self.into() - } - - /// Returns the path & query components of the Uri - #[inline] - pub fn path_and_query(&self) -> Option<&PathAndQuery> { - if !self.scheme.inner.is_none() || self.authority.data.is_empty() { - Some(&self.path_and_query) - } else { - None - } - } - - /// Get the path of this `Uri`. - /// - /// Both relative and absolute URIs contain a path component, though it - /// might be the empty string. The path component is **case sensitive**. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |--------| - /// | - /// path - /// ``` - /// - /// If the URI is `*` then the path component is equal to `*`. - /// - /// # Examples - /// - /// A relative URI - /// - /// ``` - /// # use http::Uri; - /// - /// let uri: Uri = "/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.path(), "/hello/world"); - /// ``` - /// - /// An absolute URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.path(), "/hello/world"); - /// ``` - #[inline] - pub fn path(&self) -> &str { - if self.has_path() { - self.path_and_query.path() - } else { - "" - } - } - - /// Get the scheme of this `Uri`. - /// - /// The URI scheme refers to a specification for assigning identifiers - /// within that scheme. Only absolute URIs contain a scheme component, but - /// not all absolute URIs will contain a scheme component. Although scheme - /// names are case-insensitive, the canonical form is lowercase. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |-| - /// | - /// scheme - /// ``` - /// - /// # Examples - /// - /// Absolute URI - /// - /// ``` - /// use http::uri::{Scheme, Uri}; - /// - /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.scheme_part(), Some(&Scheme::HTTP)); - /// ``` - /// - /// - /// Relative URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "/hello/world".parse().unwrap(); - /// - /// assert!(uri.scheme_part().is_none()); - /// ``` - #[inline] - pub fn scheme_part(&self) -> Option<&Scheme> { - if self.scheme.inner.is_none() { - None - } else { - Some(&self.scheme) - } - } - - #[deprecated(since = "0.1.2", note = "use scheme_part or scheme_str instead")] - #[doc(hidden)] - #[inline] - pub fn scheme(&self) -> Option<&str> { - self.scheme_str() - } - - /// Get the scheme of this `Uri` as a `&str`. - /// - /// # Example - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.scheme_str(), Some("http")); - /// ``` - #[inline] - pub fn scheme_str(&self) -> Option<&str> { - if self.scheme.inner.is_none() { - None - } else { - Some(self.scheme.as_str()) - } - } - - /// Get the authority of this `Uri`. - /// - /// The authority is a hierarchical element for naming authority such that - /// the remainder of the URI is delegated to that authority. For HTTP, the - /// authority consists of the host and port. The host portion of the - /// authority is **case-insensitive**. - /// - /// The authority also includes a `username:password` component, however - /// the use of this is deprecated and should be avoided. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |-------------------------------| - /// | - /// authority - /// ``` - /// - /// This function will be renamed to `authority` in the next semver release. - /// - /// # Examples - /// - /// Absolute URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.authority_part().map(|a| a.as_str()), Some("example.org:80")); - /// ``` - /// - /// - /// Relative URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "/hello/world".parse().unwrap(); - /// - /// assert!(uri.authority_part().is_none()); - /// ``` - #[inline] - pub fn authority_part(&self) -> Option<&Authority> { - if self.authority.data.is_empty() { - None - } else { - Some(&self.authority) - } - } - - #[deprecated(since = "0.1.1", note = "use authority_part instead")] - #[doc(hidden)] - #[inline] - pub fn authority(&self) -> Option<&str> { - if self.authority.data.is_empty() { - None - } else { - Some(self.authority.as_str()) - } - } - - /// Get the host of this `Uri`. - /// - /// The host subcomponent of authority is identified by an IP literal - /// encapsulated within square brackets, an IPv4 address in dotted- decimal - /// form, or a registered name. The host subcomponent is **case-insensitive**. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |---------| - /// | - /// host - /// ``` - /// - /// # Examples - /// - /// Absolute URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.host(), Some("example.org")); - /// ``` - /// - /// - /// Relative URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "/hello/world".parse().unwrap(); - /// - /// assert!(uri.host().is_none()); - /// ``` - #[inline] - pub fn host(&self) -> Option<&str> { - self.authority_part().map(|a| a.host()) - } - - #[deprecated(since="0.1.14", note="use `port_part` or `port_u16` instead")] - #[doc(hidden)] - pub fn port(&self) -> Option { - self.port_u16() - } - - /// Get the port part of this `Uri`. - /// - /// The port subcomponent of authority is designated by an optional port - /// number following the host and delimited from it by a single colon (":") - /// character. It can be turned into a decimal port number with the `as_u16` - /// method or as a `str` with the `as_str` method. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |-| - /// | - /// port - /// ``` - /// - /// # Examples - /// - /// Absolute URI with port - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); - /// - /// let port = uri.port_part().unwrap(); - /// assert_eq!(port.as_u16(), 80); - /// ``` - /// - /// Absolute URI without port - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org/hello/world".parse().unwrap(); - /// - /// assert!(uri.port_part().is_none()); - /// ``` - /// - /// Relative URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "/hello/world".parse().unwrap(); - /// - /// assert!(uri.port_part().is_none()); - /// ``` - pub fn port_part(&self) -> Option> { - self.authority_part() - .and_then(|a| a.port_part()) - } - - /// Get the port of this `Uri` as a `u16`. - /// - /// - /// # Example - /// - /// ``` - /// # use http::{Uri, uri::Port}; - /// let uri: Uri = "http://example.org:80/hello/world".parse().unwrap(); - /// - /// assert_eq!(uri.port_u16(), Some(80)); - /// ``` - pub fn port_u16(&self) -> Option { - self.port_part().and_then(|p| Some(p.as_u16())) - } - - /// Get the query string of this `Uri`, starting after the `?`. - /// - /// The query component contains non-hierarchical data that, along with data - /// in the path component, serves to identify a resource within the scope of - /// the URI's scheme and naming authority (if any). The query component is - /// indicated by the first question mark ("?") character and terminated by a - /// number sign ("#") character or by the end of the URI. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |-------------------| - /// | - /// query - /// ``` - /// - /// # Examples - /// - /// Absolute URI - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "http://example.org/hello/world?key=value".parse().unwrap(); - /// - /// assert_eq!(uri.query(), Some("key=value")); - /// ``` - /// - /// Relative URI with a query string component - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "/hello/world?key=value&foo=bar".parse().unwrap(); - /// - /// assert_eq!(uri.query(), Some("key=value&foo=bar")); - /// ``` - /// - /// Relative URI without a query string component - /// - /// ``` - /// # use http::Uri; - /// let uri: Uri = "/hello/world".parse().unwrap(); - /// - /// assert!(uri.query().is_none()); - /// ``` - #[inline] - pub fn query(&self) -> Option<&str> { - self.path_and_query.query() - } - - fn has_path(&self) -> bool { - !self.path_and_query.data.is_empty() || !self.scheme.inner.is_none() - } -} - -impl<'a> HttpTryFrom<&'a str> for Uri { - type Error = InvalidUri; - - #[inline] - fn try_from(t: &'a str) -> Result { - t.parse() - } -} - -impl<'a> HttpTryFrom<&'a String> for Uri { - type Error = InvalidUri; - - #[inline] - fn try_from(t: &'a String) -> Result { - t.parse() - } -} - -impl HttpTryFrom for Uri { - type Error = InvalidUriBytes; - - #[inline] - fn try_from(t: String) -> Result { - Uri::from_shared(Bytes::from(t)) - } -} - -impl HttpTryFrom for Uri { - type Error = InvalidUriBytes; - - #[inline] - fn try_from(t: Bytes) -> Result { - Uri::from_shared(t) - } -} - -impl HttpTryFrom for Uri { - type Error = InvalidUriParts; - - #[inline] - fn try_from(src: Parts) -> Result { - Uri::from_parts(src) - } -} - -impl<'a> HttpTryFrom<&'a Uri> for Uri { - type Error = ::Error; - - #[inline] - fn try_from(src: &'a Uri) -> Result { - Ok(src.clone()) - } -} - -/// Convert a `Uri` from parts -/// -/// # Examples -/// -/// Relative URI -/// -/// ``` -/// # use http::uri::*; -/// let mut parts = Parts::default(); -/// parts.path_and_query = Some("/foo".parse().unwrap()); -/// -/// let uri = Uri::from_parts(parts).unwrap(); -/// -/// assert_eq!(uri.path(), "/foo"); -/// -/// assert!(uri.scheme_part().is_none()); -/// assert!(uri.authority().is_none()); -/// ``` -/// -/// Absolute URI -/// -/// ``` -/// # use http::uri::*; -/// let mut parts = Parts::default(); -/// parts.scheme = Some("http".parse().unwrap()); -/// parts.authority = Some("foo.com".parse().unwrap()); -/// parts.path_and_query = Some("/foo".parse().unwrap()); -/// -/// let uri = Uri::from_parts(parts).unwrap(); -/// -/// assert_eq!(uri.scheme_part().unwrap().as_str(), "http"); -/// assert_eq!(uri.authority().unwrap(), "foo.com"); -/// assert_eq!(uri.path(), "/foo"); -/// ``` -impl From for Parts { - fn from(src: Uri) -> Self { - let path_and_query = if src.has_path() { - Some(src.path_and_query) - } else { - None - }; - - let scheme = match src.scheme.inner { - Scheme2::None => None, - _ => Some(src.scheme), - }; - - let authority = if src.authority.data.is_empty() { - None - } else { - Some(src.authority) - }; - - Parts { - scheme: scheme, - authority: authority, - path_and_query: path_and_query, - _priv: (), - } - } -} - -fn parse_full(mut s: Bytes) -> Result { - // Parse the scheme - let scheme = match Scheme2::parse(&s[..]).map_err(InvalidUriBytes)? { - Scheme2::None => Scheme2::None, - Scheme2::Standard(p) => { - // TODO: use truncate - let _ = s.split_to(p.len() + 3); - Scheme2::Standard(p) - } - Scheme2::Other(n) => { - // Grab the protocol - let mut scheme = s.split_to(n + 3); - - // Strip ://, TODO: truncate - let _ = scheme.split_off(n); - - // Allocate the ByteStr - let val = unsafe { ByteStr::from_utf8_unchecked(scheme) }; - - Scheme2::Other(Box::new(val)) - } - }; - - // Find the end of the authority. The scheme will already have been - // extracted. - let authority_end = Authority::parse(&s[..]).map_err(InvalidUriBytes)?; - - if scheme.is_none() { - if authority_end != s.len() { - return Err(ErrorKind::InvalidFormat.into()); - } - - let authority = Authority { - data: unsafe { ByteStr::from_utf8_unchecked(s) }, - }; - - return Ok(Uri { - scheme: scheme.into(), - authority: authority, - path_and_query: PathAndQuery::empty(), - }); - } - - // Authority is required when absolute - if authority_end == 0 { - return Err(ErrorKind::InvalidFormat.into()); - } - - let authority = s.split_to(authority_end); - let authority = Authority { - data: unsafe { ByteStr::from_utf8_unchecked(authority) }, - }; - - Ok(Uri { - scheme: scheme.into(), - authority: authority, - path_and_query: PathAndQuery::from_shared(s)?, - }) -} - -impl FromStr for Uri { - type Err = InvalidUri; - - #[inline] - fn from_str(s: &str) -> Result { - Uri::from_shared(s.into()).map_err(|e| e.0) - } -} - -impl PartialEq for Uri { - fn eq(&self, other: &Uri) -> bool { - if self.scheme_part() != other.scheme_part() { - return false; - } - - if self.authority_part() != other.authority_part() { - return false; - } - - if self.path() != other.path() { - return false; - } - - if self.query() != other.query() { - return false; - } - - true - } -} - -impl PartialEq for Uri { - fn eq(&self, other: &str) -> bool { - let mut other = other.as_bytes(); - let mut absolute = false; - - if let Some(scheme) = self.scheme_part() { - let scheme = scheme.as_str().as_bytes(); - absolute = true; - - if other.len() < scheme.len() + 3 { - return false; - } - - if !scheme.eq_ignore_ascii_case(&other[..scheme.len()]) { - return false; - } - - other = &other[scheme.len()..]; - - if &other[..3] != b"://" { - return false; - } - - other = &other[3..]; - } - - if let Some(auth) = self.authority_part() { - let len = auth.data.len(); - absolute = true; - - if other.len() < len { - return false; - } - - if !auth.data.as_bytes().eq_ignore_ascii_case(&other[..len]) { - return false; - } - - other = &other[len..]; - } - - let path = self.path(); - - if other.len() < path.len() || path.as_bytes() != &other[..path.len()] { - if absolute && path == "/" { - // PathAndQuery can be ommitted, fall through - } else { - return false; - } - } else { - other = &other[path.len()..]; - } - - if let Some(query) = self.query() { - if other.len() == 0 { - return query.len() == 0; - } - - if other[0] != b'?' { - return false; - } - - other = &other[1..]; - - if other.len() < query.len() { - return false; - } - - if query.as_bytes() != &other[..query.len()] { - return false; - } - - other = &other[query.len()..]; - } - - other.is_empty() || other[0] == b'#' - } -} - -impl PartialEq for str { - fn eq(&self, uri: &Uri) -> bool { - uri == self - } -} - -impl<'a> PartialEq<&'a str> for Uri { - fn eq(&self, other: & &'a str) -> bool { - self == *other - } -} - -impl<'a> PartialEq for &'a str { - fn eq(&self, uri: &Uri) -> bool { - uri == *self - } -} - -impl Eq for Uri {} - -/// Returns a `Uri` representing `/` -impl Default for Uri { - #[inline] - fn default() -> Uri { - Uri { - scheme: Scheme::empty(), - authority: Authority::empty(), - path_and_query: PathAndQuery::slash(), - } - } -} - -impl fmt::Display for Uri { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if let Some(scheme) = self.scheme_part() { - write!(f, "{}://", scheme)?; - } - - if let Some(authority) = self.authority_part() { - write!(f, "{}", authority)?; - } - - write!(f, "{}", self.path())?; - - if let Some(query) = self.query() { - write!(f, "?{}", query)?; - } - - Ok(()) - } -} - -impl fmt::Debug for Uri { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} - -impl From for InvalidUri { - fn from(src: ErrorKind) -> InvalidUri { - InvalidUri(src) - } -} - -impl From for InvalidUriBytes { - fn from(src: ErrorKind) -> InvalidUriBytes { - InvalidUriBytes(src.into()) - } -} - -impl From for InvalidUriParts { - fn from(src: ErrorKind) -> InvalidUriParts { - InvalidUriParts(src.into()) - } -} - -impl fmt::Display for InvalidUri { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.description().fmt(f) - } -} - -impl Error for InvalidUri { - fn description(&self) -> &str { - match self.0 { - ErrorKind::InvalidUriChar => "invalid uri character", - ErrorKind::InvalidScheme => "invalid scheme", - ErrorKind::InvalidAuthority => "invalid authority", - ErrorKind::InvalidPort => "invalid port", - ErrorKind::InvalidFormat => "invalid format", - ErrorKind::SchemeMissing => "scheme missing", - ErrorKind::AuthorityMissing => "authority missing", - ErrorKind::PathAndQueryMissing => "path missing", - ErrorKind::TooLong => "uri too long", - ErrorKind::Empty => "empty string", - ErrorKind::SchemeTooLong => "scheme too long", - } - } -} - -impl fmt::Display for InvalidUriBytes { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - -impl fmt::Display for InvalidUriParts { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Error for InvalidUriBytes { - fn description(&self) -> &str { - self.0.description() - } -} - -impl Error for InvalidUriParts { - fn description(&self) -> &str { - self.0.description() - } -} - -impl Hash for Uri { - fn hash(&self, state: &mut H) where H: Hasher { - if !self.scheme.inner.is_none() { - self.scheme.hash(state); - state.write_u8(0xff); - } - - if let Some(auth) = self.authority_part() { - auth.hash(state); - } - - Hash::hash_slice(self.path().as_bytes(), state); - - if let Some(query) = self.query() { - b'?'.hash(state); - Hash::hash_slice(query.as_bytes(), state); - } - } -} diff --git a/third_party/rust/http/v0_1/crate/src/uri/path.rs b/third_party/rust/http/v0_1/crate/src/uri/path.rs deleted file mode 100644 index 2cafbf89fd79..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/path.rs +++ /dev/null @@ -1,552 +0,0 @@ -use std::{cmp, fmt, str}; -use std::str::FromStr; - -use bytes::Bytes; - -use byte_str::ByteStr; -use convert::HttpTryFrom; -use super::{ErrorKind, InvalidUri, InvalidUriBytes}; - -/// Represents the path component of a URI -#[derive(Clone)] -pub struct PathAndQuery { - pub(super) data: ByteStr, - pub(super) query: u16, -} - -const NONE: u16 = ::std::u16::MAX; - -impl PathAndQuery { - /// Attempt to convert a `PathAndQuery` from `Bytes`. - /// - /// This function will be replaced by a `TryFrom` implementation once the - /// trait lands in stable. - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("/hello?world"); - /// let path_and_query = PathAndQuery::from_shared(bytes).unwrap(); - /// - /// assert_eq!(path_and_query.path(), "/hello"); - /// assert_eq!(path_and_query.query(), Some("world")); - /// # } - /// ``` - pub fn from_shared(mut src: Bytes) -> Result { - let mut query = NONE; - let mut fragment = None; - - // block for iterator borrow - // - // allow: `...` pattersn are now `..=`, but we cannot update yet - // because of minimum Rust version - #[allow(warnings)] - { - let mut iter = src.as_ref().iter().enumerate(); - - // path ... - for (i, &b) in &mut iter { - // See https://url.spec.whatwg.org/#path-state - match b { - b'?' => { - debug_assert_eq!(query, NONE); - query = i as u16; - break; - } - b'#' => { - fragment = Some(i); - break; - }, - - // This is the range of bytes that don't need to be - // percent-encoded in the path. If it should have been - // percent-encoded, then error. - 0x21 | - 0x24...0x3B | - 0x3D | - 0x40...0x5F | - 0x61...0x7A | - 0x7C | - 0x7E => {}, - - _ => return Err(ErrorKind::InvalidUriChar.into()), - } - } - - // query ... - if query != NONE { - - // allow: `...` pattersn are now `..=`, but we cannot update yet - // because of minimum Rust version - #[allow(warnings)] - for (i, &b) in iter { - match b { - // While queries *should* be percent-encoded, most - // bytes are actually allowed... - // See https://url.spec.whatwg.org/#query-state - // - // Allowed: 0x21 / 0x24 - 0x3B / 0x3D / 0x3F - 0x7E - 0x21 | - 0x24...0x3B | - 0x3D | - 0x3F...0x7E => {}, - - b'#' => { - fragment = Some(i); - break; - }, - - _ => return Err(ErrorKind::InvalidUriChar.into()), - } - } - } - } - - if let Some(i) = fragment { - src.truncate(i); - } - - Ok(PathAndQuery { - data: unsafe { ByteStr::from_utf8_unchecked(src) }, - query: query, - }) - } - - /// Convert a `PathAndQuery` from a static string. - /// - /// This function will not perform any copying, however the string is - /// checked to ensure that it is valid. - /// - /// # Panics - /// - /// This function panics if the argument is an invalid path and query. - /// - /// # Examples - /// - /// ``` - /// # use http::uri::*; - /// let v = PathAndQuery::from_static("/hello?world"); - /// - /// assert_eq!(v.path(), "/hello"); - /// assert_eq!(v.query(), Some("world")); - /// ``` - #[inline] - pub fn from_static(src: &'static str) -> Self { - let src = Bytes::from_static(src.as_bytes()); - - PathAndQuery::from_shared(src) - .unwrap() - } - - pub(super) fn empty() -> Self { - PathAndQuery { - data: ByteStr::new(), - query: NONE, - } - } - - pub(super) fn slash() -> Self { - PathAndQuery { - data: ByteStr::from_static("/"), - query: NONE, - } - } - - pub(super) fn star() -> Self { - PathAndQuery { - data: ByteStr::from_static("*"), - query: NONE, - } - } - - /// Returns the path component - /// - /// The path component is **case sensitive**. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |--------| - /// | - /// path - /// ``` - /// - /// If the URI is `*` then the path component is equal to `*`. - /// - /// # Examples - /// - /// ``` - /// # use http::uri::*; - /// - /// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap(); - /// - /// assert_eq!(path_and_query.path(), "/hello/world"); - /// ``` - #[inline] - pub fn path(&self) -> &str { - let ret = if self.query == NONE { - &self.data[..] - } else { - &self.data[..self.query as usize] - }; - - if ret.is_empty() { - return "/"; - } - - ret - } - - /// Returns the query string component - /// - /// The query component contains non-hierarchical data that, along with data - /// in the path component, serves to identify a resource within the scope of - /// the URI's scheme and naming authority (if any). The query component is - /// indicated by the first question mark ("?") character and terminated by a - /// number sign ("#") character or by the end of the URI. - /// - /// ```notrust - /// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1 - /// |-------------------| - /// | - /// query - /// ``` - /// - /// # Examples - /// - /// With a query string component - /// - /// ``` - /// # use http::uri::*; - /// let path_and_query: PathAndQuery = "/hello/world?key=value&foo=bar".parse().unwrap(); - /// - /// assert_eq!(path_and_query.query(), Some("key=value&foo=bar")); - /// ``` - /// - /// Without a query string component - /// - /// ``` - /// # use http::uri::*; - /// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap(); - /// - /// assert!(path_and_query.query().is_none()); - /// ``` - #[inline] - pub fn query(&self) -> Option<&str> { - if self.query == NONE { - None - } else { - let i = self.query + 1; - Some(&self.data[i as usize..]) - } - } - - /// Returns the path and query as a string component. - /// - /// # Examples - /// - /// With a query string component - /// - /// ``` - /// # use http::uri::*; - /// let path_and_query: PathAndQuery = "/hello/world?key=value&foo=bar".parse().unwrap(); - /// - /// assert_eq!(path_and_query.as_str(), "/hello/world?key=value&foo=bar"); - /// ``` - /// - /// Without a query string component - /// - /// ``` - /// # use http::uri::*; - /// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap(); - /// - /// assert_eq!(path_and_query.as_str(), "/hello/world"); - /// ``` - #[inline] - pub fn as_str(&self) -> &str { - let ret = &self.data[..]; - if ret.is_empty() { - return "/"; - } - ret - } - - /// Converts this `PathAndQuery` back to a sequence of bytes - #[inline] - pub fn into_bytes(self) -> Bytes { - self.into() - } -} - -impl HttpTryFrom for PathAndQuery { - type Error = InvalidUriBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - PathAndQuery::from_shared(bytes) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for PathAndQuery { - type Error = InvalidUri; - #[inline] - fn try_from(s: &'a [u8]) -> Result { - PathAndQuery::from_shared(s.into()).map_err(|e| e.0) - } -} - -impl<'a> HttpTryFrom<&'a str> for PathAndQuery { - type Error = InvalidUri; - #[inline] - fn try_from(s: &'a str) -> Result { - HttpTryFrom::try_from(s.as_bytes()) - } -} - -impl FromStr for PathAndQuery { - type Err = InvalidUri; - #[inline] - fn from_str(s: &str) -> Result { - HttpTryFrom::try_from(s) - } -} - -impl From for Bytes { - fn from(src: PathAndQuery) -> Bytes { - src.data.into() - } -} - -impl fmt::Debug for PathAndQuery { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} - -impl fmt::Display for PathAndQuery { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - if !self.data.is_empty() { - match self.data.as_bytes()[0] { - b'/' | b'*' => write!(fmt, "{}", &self.data[..]), - _ => write!(fmt, "/{}", &self.data[..]), - } - } else { - write!(fmt, "/") - } - } -} - -// ===== PartialEq / PartialOrd ===== - -impl PartialEq for PathAndQuery { - #[inline] - fn eq(&self, other: &PathAndQuery) -> bool { - self.data == other.data - } -} - -impl Eq for PathAndQuery {} - -impl PartialEq for PathAndQuery { - #[inline] - fn eq(&self, other: &str) -> bool { - self.as_str() == other - } -} - -impl<'a> PartialEq for &'a str { - #[inline] - fn eq(&self, other: &PathAndQuery) -> bool { - self == &other.as_str() - } -} - -impl<'a> PartialEq<&'a str> for PathAndQuery { - #[inline] - fn eq(&self, other: &&'a str) -> bool { - self.as_str() == *other - } -} - -impl PartialEq for str { - #[inline] - fn eq(&self, other: &PathAndQuery) -> bool { - self == other.as_str() - } -} - -impl PartialEq for PathAndQuery { - #[inline] - fn eq(&self, other: &String) -> bool { - self.as_str() == other.as_str() - } -} - -impl PartialEq for String { - #[inline] - fn eq(&self, other: &PathAndQuery) -> bool { - self.as_str() == other.as_str() - } -} - -impl PartialOrd for PathAndQuery { - #[inline] - fn partial_cmp(&self, other: &PathAndQuery) -> Option { - self.as_str().partial_cmp(other.as_str()) - } -} - -impl PartialOrd for PathAndQuery { - #[inline] - fn partial_cmp(&self, other: &str) -> Option { - self.as_str().partial_cmp(other) - } -} - -impl PartialOrd for str { - #[inline] - fn partial_cmp(&self, other: &PathAndQuery) -> Option { - self.partial_cmp(other.as_str()) - } -} - -impl<'a> PartialOrd<&'a str> for PathAndQuery { - #[inline] - fn partial_cmp(&self, other: &&'a str) -> Option { - self.as_str().partial_cmp(*other) - } -} - -impl<'a> PartialOrd for &'a str { - #[inline] - fn partial_cmp(&self, other: &PathAndQuery) -> Option { - self.partial_cmp(&other.as_str()) - } -} - -impl PartialOrd for PathAndQuery { - #[inline] - fn partial_cmp(&self, other: &String) -> Option { - self.as_str().partial_cmp(other.as_str()) - } -} - -impl PartialOrd for String { - #[inline] - fn partial_cmp(&self, other: &PathAndQuery) -> Option { - self.as_str().partial_cmp(other.as_str()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn equal_to_self_of_same_path() { - let p1: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - let p2: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - assert_eq!(p1, p2); - assert_eq!(p2, p1); - } - - #[test] - fn not_equal_to_self_of_different_path() { - let p1: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - let p2: PathAndQuery = "/world&foo=bar".parse().unwrap(); - assert_ne!(p1, p2); - assert_ne!(p2, p1); - } - - #[test] - fn equates_with_a_str() { - let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - assert_eq!(&path_and_query, "/hello/world&foo=bar"); - assert_eq!("/hello/world&foo=bar", &path_and_query); - assert_eq!(path_and_query, "/hello/world&foo=bar"); - assert_eq!("/hello/world&foo=bar", path_and_query); - } - - #[test] - fn not_equal_with_a_str_of_a_different_path() { - let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - // as a reference - assert_ne!(&path_and_query, "/hello&foo=bar"); - assert_ne!("/hello&foo=bar", &path_and_query); - // without reference - assert_ne!(path_and_query, "/hello&foo=bar"); - assert_ne!("/hello&foo=bar", path_and_query); - } - - #[test] - fn equates_with_a_string() { - let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - assert_eq!(path_and_query, "/hello/world&foo=bar".to_string()); - assert_eq!("/hello/world&foo=bar".to_string(), path_and_query); - } - - #[test] - fn not_equal_with_a_string_of_a_different_path() { - let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap(); - assert_ne!(path_and_query, "/hello&foo=bar".to_string()); - assert_ne!("/hello&foo=bar".to_string(), path_and_query); - } - - #[test] - fn compares_to_self() { - let p1: PathAndQuery = "/a/world&foo=bar".parse().unwrap(); - let p2: PathAndQuery = "/b/world&foo=bar".parse().unwrap(); - assert!(p1 < p2); - assert!(p2 > p1); - } - - #[test] - fn compares_with_a_str() { - let path_and_query: PathAndQuery = "/b/world&foo=bar".parse().unwrap(); - // by ref - assert!(&path_and_query < "/c/world&foo=bar"); - assert!("/c/world&foo=bar" > &path_and_query); - assert!(&path_and_query > "/a/world&foo=bar"); - assert!("/a/world&foo=bar" < &path_and_query); - - // by val - assert!(path_and_query < "/c/world&foo=bar"); - assert!("/c/world&foo=bar" > path_and_query); - assert!(path_and_query > "/a/world&foo=bar"); - assert!("/a/world&foo=bar" < path_and_query); - } - - #[test] - fn compares_with_a_string() { - let path_and_query: PathAndQuery = "/b/world&foo=bar".parse().unwrap(); - assert!(path_and_query < "/c/world&foo=bar".to_string()); - assert!("/c/world&foo=bar".to_string() > path_and_query); - assert!(path_and_query > "/a/world&foo=bar".to_string()); - assert!("/a/world&foo=bar".to_string() < path_and_query); - } - - #[test] - fn ignores_valid_percent_encodings() { - assert_eq!("/a%20b", pq("/a%20b?r=1").path()); - assert_eq!("qr=%31", pq("/a/b?qr=%31").query().unwrap()); - } - - #[test] - fn ignores_invalid_percent_encodings() { - assert_eq!("/a%%b", pq("/a%%b?r=1").path()); - assert_eq!("/aaa%", pq("/aaa%").path()); - assert_eq!("/aaa%", pq("/aaa%?r=1").path()); - assert_eq!("/aa%2", pq("/aa%2").path()); - assert_eq!("/aa%2", pq("/aa%2?r=1").path()); - assert_eq!("qr=%3", pq("/a/b?qr=%3").query().unwrap()); - } - - fn pq(s: &str) -> PathAndQuery { - s.parse().expect(&format!("parsing {}", s)) - } -} diff --git a/third_party/rust/http/v0_1/crate/src/uri/port.rs b/third_party/rust/http/v0_1/crate/src/uri/port.rs deleted file mode 100644 index d3177a7aa628..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/port.rs +++ /dev/null @@ -1,158 +0,0 @@ -use std::fmt; - -use super::{ErrorKind, InvalidUri}; - -/// The port component of a URI. -pub struct Port { - port: u16, - repr: T, -} - -impl Port { - /// Returns the port number as a `u16`. - /// - /// # Examples - /// - /// Port as `u16`. - /// - /// ``` - /// # use http::uri::Authority; - /// let authority: Authority = "example.org:80".parse().unwrap(); - /// - /// let port = authority.port_part().unwrap(); - /// assert_eq!(port.as_u16(), 80); - /// ``` - pub fn as_u16(&self) -> u16 { - self.port - } -} - -impl Port -where - T: AsRef, -{ - /// Converts a `str` to a port number. - /// - /// The supplied `str` must be a valid u16. - pub(crate) fn from_str(bytes: T) -> Result { - bytes - .as_ref() - .parse::() - .map(|port| Port { - port, - repr: bytes, - }) - .map_err(|_| { - ErrorKind::InvalidPort.into() - }) - } - - /// Returns the port number as a `str`. - /// - /// # Examples - /// - /// Port as `str`. - /// - /// ``` - /// # use http::uri::Authority; - /// let authority: Authority = "example.org:80".parse().unwrap(); - /// - /// let port = authority.port_part().unwrap(); - /// assert_eq!(port.as_str(), "80"); - /// ``` - pub fn as_str(&self) -> &str { - self.repr.as_ref() - } -} - -impl fmt::Debug for Port -where - T: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("Port") - .field(&self.port) - .finish() - } -} - -impl fmt::Display for Port { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - // Use `u16::fmt` so that it respects any formatting flags that - // may have been set (like padding, align, etc). - fmt::Display::fmt(&self.port, f) - } -} - -impl From> for u16 { - fn from(port: Port) -> Self { - port.as_u16() - } -} - -impl AsRef for Port -where - T: AsRef, -{ - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl PartialEq> for Port { - fn eq(&self, other: &Port) -> bool { - self.port == other.port - } -} - -impl PartialEq for Port { - fn eq(&self, other: &u16) -> bool { - self.port == *other - } -} - -impl PartialEq> for u16 { - fn eq(&self, other: &Port) -> bool { - other.port == *self - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn partialeq_port() { - let port_a = Port::from_str("8080").unwrap(); - let port_b = Port::from_str("8080").unwrap(); - assert_eq!(port_a, port_b); - } - - #[test] - fn partialeq_port_different_reprs() { - let port_a = Port { - repr: "8081", - port: 8081, - }; - let port_b = Port { - repr: String::from("8081"), - port: 8081, - }; - assert_eq!(port_a, port_b); - assert_eq!(port_b, port_a); - } - - #[test] - fn partialeq_u16() { - let port = Port::from_str("8080").unwrap(); - // test equals in both directions - assert_eq!(port, 8080); - assert_eq!(8080, port); - } - - #[test] - fn u16_from_port() { - let port = Port::from_str("8080").unwrap(); - assert_eq!(8080, u16::from(port)); - } -} diff --git a/third_party/rust/http/v0_1/crate/src/uri/scheme.rs b/third_party/rust/http/v0_1/crate/src/uri/scheme.rs deleted file mode 100644 index 017cc9a5f4a9..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/scheme.rs +++ /dev/null @@ -1,389 +0,0 @@ -// Deprecated in 1.26, needed until our minimum version is >=1.23. -#[allow(unused, deprecated)] -use std::ascii::AsciiExt; -use std::fmt; -use std::hash::{Hash, Hasher}; -use std::str::FromStr; - -use bytes::Bytes; - -use byte_str::ByteStr; -use convert::HttpTryFrom; -use super::{ErrorKind, InvalidUri, InvalidUriBytes}; - -/// Represents the scheme component of a URI -#[derive(Clone)] -pub struct Scheme { - pub(super) inner: Scheme2, -} - -#[derive(Clone, Debug)] -pub(super) enum Scheme2> { - None, - Standard(Protocol), - Other(T), -} - -#[derive(Copy, Clone, Debug)] -pub(super) enum Protocol { - Http, - Https, -} - -impl Scheme { - /// HTTP protocol scheme - pub const HTTP: Scheme = Scheme { - inner: Scheme2::Standard(Protocol::Http), - }; - - /// HTTP protocol over TLS. - pub const HTTPS: Scheme = Scheme { - inner: Scheme2::Standard(Protocol::Https), - }; - - /// Attempt to convert a `Scheme` from `Bytes` - /// - /// This function will be replaced by a `TryFrom` implementation once the - /// trait lands in stable. - /// - /// # Examples - /// - /// ``` - /// # extern crate http; - /// # use http::uri::*; - /// extern crate bytes; - /// - /// use bytes::Bytes; - /// - /// # pub fn main() { - /// let bytes = Bytes::from("http"); - /// let scheme = Scheme::from_shared(bytes).unwrap(); - /// - /// assert_eq!(scheme.as_str(), "http"); - /// # } - /// ``` - pub fn from_shared(s: Bytes) -> Result { - use self::Scheme2::*; - - match Scheme2::parse_exact(&s[..]).map_err(InvalidUriBytes)? { - None => Err(ErrorKind::InvalidScheme.into()), - Standard(p) => Ok(Standard(p).into()), - Other(_) => { - let b = unsafe { ByteStr::from_utf8_unchecked(s) }; - Ok(Other(Box::new(b)).into()) - } - } - } - - pub(super) fn empty() -> Self { - Scheme { - inner: Scheme2::None, - } - } - - /// Return a str representation of the scheme - /// - /// # Examples - /// - /// ``` - /// # use http::uri::*; - /// let scheme: Scheme = "http".parse().unwrap(); - /// assert_eq!(scheme.as_str(), "http"); - /// ``` - #[inline] - pub fn as_str(&self) -> &str { - use self::Scheme2::*; - use self::Protocol::*; - - match self.inner { - Standard(Http) => "http", - Standard(Https) => "https", - Other(ref v) => &v[..], - None => unreachable!(), - } - } - - /// Converts this `Scheme` back to a sequence of bytes - #[inline] - pub fn into_bytes(self) -> Bytes { - self.into() - } -} - -impl HttpTryFrom for Scheme { - type Error = InvalidUriBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - Scheme::from_shared(bytes) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for Scheme { - type Error = InvalidUri; - #[inline] - fn try_from(s: &'a [u8]) -> Result { - use self::Scheme2::*; - - match Scheme2::parse_exact(s)? { - None => Err(ErrorKind::InvalidScheme.into()), - Standard(p) => Ok(Standard(p).into()), - Other(_) => { - // Unsafe: parse_exact already checks for a strict subset of UTF-8 - Ok(Other(Box::new(unsafe { - ByteStr::from_utf8_unchecked(s.into()) - })).into()) - } - } - } -} - -impl<'a> HttpTryFrom<&'a str> for Scheme { - type Error = InvalidUri; - #[inline] - fn try_from(s: &'a str) -> Result { - HttpTryFrom::try_from(s.as_bytes()) - } -} - -impl FromStr for Scheme { - type Err = InvalidUri; - - fn from_str(s: &str) -> Result { - HttpTryFrom::try_from(s) - } -} - -impl From for Bytes { - #[inline] - fn from(src: Scheme) -> Self { - use self::Scheme2::*; - use self::Protocol::*; - - match src.inner { - None => Bytes::new(), - Standard(Http) => Bytes::from_static(b"http"), - Standard(Https) => Bytes::from_static(b"https"), - Other(v) => (*v).into(), - } - } -} - -impl fmt::Debug for Scheme { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(self.as_str(), f) - } -} - -impl fmt::Display for Scheme { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(self.as_str()) - } -} - -impl AsRef for Scheme { - #[inline] - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl PartialEq for Scheme { - fn eq(&self, other: &Scheme) -> bool { - use self::Protocol::*; - use self::Scheme2::*; - - match (&self.inner, &other.inner) { - (&Standard(Http), &Standard(Http)) => true, - (&Standard(Https), &Standard(Https)) => true, - (&Other(ref a), &Other(ref b)) => a.eq_ignore_ascii_case(b), - (&None, _) | (_, &None) => unreachable!(), - _ => false, - } - } -} - -impl Eq for Scheme {} - -/// Case-insensitive equality -/// -/// # Examples -/// -/// ``` -/// # use http::uri::Scheme; -/// let scheme: Scheme = "HTTP".parse().unwrap(); -/// assert_eq!(scheme, *"http"); -/// ``` -impl PartialEq for Scheme { - fn eq(&self, other: &str) -> bool { - self.as_str().eq_ignore_ascii_case(other) - } -} - -/// Case-insensitive equality -impl PartialEq for str { - fn eq(&self, other: &Scheme) -> bool { - other == self - } -} - -/// Case-insensitive hashing -impl Hash for Scheme { - fn hash(&self, state: &mut H) where H: Hasher { - match self.inner { - Scheme2::None => (), - Scheme2::Standard(Protocol::Http) => state.write_u8(1), - Scheme2::Standard(Protocol::Https) => state.write_u8(2), - Scheme2::Other(ref other) => { - other.len().hash(state); - for &b in other.as_bytes() { - state.write_u8(b.to_ascii_lowercase()); - } - } - } - } -} - -impl Scheme2 { - pub(super) fn is_none(&self) -> bool { - match *self { - Scheme2::None => true, - _ => false, - } - } -} - -// Require the scheme to not be too long in order to enable further -// optimizations later. -const MAX_SCHEME_LEN: usize = 64; - -// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) -// -const SCHEME_CHARS: [u8; 256] = [ - // 0 1 2 3 4 5 6 7 8 9 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3x - 0, 0, 0, b'+', 0, b'-', b'.', 0, b'0', b'1', // 4x - b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', 0, // 5x - 0, 0, 0, 0, 0, b'A', b'B', b'C', b'D', b'E', // 6x - b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', // 7x - b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', // 8x - b'Z', 0, 0, 0, 0, 0, 0, b'a', b'b', b'c', // 9x - b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', // 10x - b'n', b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', // 11x - b'x', b'y', b'z', 0, 0, 0, b'~', 0, 0, 0, // 12x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 13x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 14x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 15x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 17x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 18x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 19x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 21x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 22x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 23x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 24x - 0, 0, 0, 0, 0, 0 // 25x -]; - -impl Scheme2 { - fn parse_exact(s: &[u8]) -> Result, InvalidUri> { - match s { - b"http" => Ok(Protocol::Http.into()), - b"https" => Ok(Protocol::Https.into()), - _ => { - if s.len() > MAX_SCHEME_LEN { - return Err(ErrorKind::SchemeTooLong.into()); - } - - for &b in s { - match SCHEME_CHARS[b as usize] { - b':' => { - // Don't want :// here - return Err(ErrorKind::InvalidScheme.into()); - } - 0 => { - return Err(ErrorKind::InvalidScheme.into()); - } - _ => {} - } - } - - Ok(Scheme2::Other(())) - } - } - } - - pub(super) fn parse(s: &[u8]) -> Result, InvalidUri> { - if s.len() >= 7 { - // Check for HTTP - if s[..7].eq_ignore_ascii_case(b"http://") { - // Prefix will be striped - return Ok(Protocol::Http.into()); - } - } - - if s.len() >= 8 { - // Check for HTTPs - if s[..8].eq_ignore_ascii_case(b"https://") { - return Ok(Protocol::Https.into()); - } - } - - if s.len() > 3 { - for i in 0..s.len() { - let b = s[i]; - - match SCHEME_CHARS[b as usize] { - b':' => { - // Not enough data remaining - if s.len() < i + 3 { - break; - } - - // Not a scheme - if &s[i+1..i+3] != b"//" { - break; - } - - if i > MAX_SCHEME_LEN { - return Err(ErrorKind::SchemeTooLong.into()); - } - - // Return scheme - return Ok(Scheme2::Other(i)); - } - // Invald scheme character, abort - 0 => break, - _ => {} - } - } - } - - Ok(Scheme2::None) - } -} - -impl Protocol { - pub(super) fn len(&self) -> usize { - match *self { - Protocol::Http => 4, - Protocol::Https => 5, - } - } -} - -impl From for Scheme2 { - fn from(src: Protocol) -> Self { - Scheme2::Standard(src) - } -} - -#[doc(hidden)] -impl From for Scheme { - fn from(src: Scheme2) -> Self { - Scheme { inner: src } - } -} diff --git a/third_party/rust/http/v0_1/crate/src/uri/tests.rs b/third_party/rust/http/v0_1/crate/src/uri/tests.rs deleted file mode 100644 index 9a9ccc80773b..000000000000 --- a/third_party/rust/http/v0_1/crate/src/uri/tests.rs +++ /dev/null @@ -1,520 +0,0 @@ -use std::str::FromStr; - -use super::{ErrorKind, InvalidUri, Uri, URI_CHARS, Port}; - -#[test] -fn test_char_table() { - for (i, &v) in URI_CHARS.iter().enumerate() { - if v != 0 { - assert_eq!(i, v as usize); - } - } -} - -macro_rules! part { - ($s:expr) => ( - Some(&$s.parse().unwrap()) - ) -} - -macro_rules! test_parse { - ( - $test_name:ident, - $str:expr, - $alt:expr, - $($method:ident = $value:expr,)* - ) => ( - #[test] - fn $test_name() { - let orig_str = $str; - let uri = match Uri::from_str(orig_str) { - Ok(uri) => uri, - Err(err) => { - panic!("parse error {:?} from {:?}", err, orig_str); - }, - }; - $( - assert_eq!(uri.$method(), $value, "{}: uri = {:?}", stringify!($method), uri); - )+ - assert_eq!(uri, orig_str, "partial eq to original str"); - assert_eq!(uri, uri.clone(), "clones are equal"); - - let new_str = uri.to_string(); - let new_uri = Uri::from_str(&new_str).expect("to_string output parses again as a Uri"); - assert_eq!(new_uri, orig_str, "round trip still equals original str"); - - const ALT: &'static [&'static str] = &$alt; - - for &alt in ALT.iter() { - let other: Uri = alt.parse().unwrap(); - assert_eq!(uri, *alt); - assert_eq!(uri, other); - } - } - ); -} - -test_parse! { - test_uri_parse_path_and_query, - "/some/path/here?and=then&hello#and-bye", - [], - - scheme_part = None, - authority_part = None, - path = "/some/path/here", - query = Some("and=then&hello"), - host = None, -} - -test_parse! { - test_uri_parse_absolute_form, - "http://127.0.0.1:61761/chunks", - [], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1:61761"), - path = "/chunks", - query = None, - host = Some("127.0.0.1"), - port_part = Port::from_str("61761").ok(), -} - -test_parse! { - test_uri_parse_absolute_form_without_path, - "https://127.0.0.1:61761", - ["https://127.0.0.1:61761/"], - - scheme_part = part!("https"), - authority_part = part!("127.0.0.1:61761"), - path = "/", - query = None, - host = Some("127.0.0.1"), - port_part = Port::from_str("61761").ok(), -} - -test_parse! { - test_uri_parse_asterisk_form, - "*", - [], - - scheme_part = None, - authority_part = None, - path = "*", - query = None, - host = None, -} - -test_parse! { - test_uri_parse_authority_no_port, - "localhost", - ["LOCALHOST", "LocaLHOSt"], - - scheme_part = None, - authority_part = part!("localhost"), - path = "", - query = None, - port_part = None, - host = Some("localhost"), -} - -test_parse! { - test_uri_authority_only_one_character_issue_197, - "S", - [], - - scheme_part = None, - authority_part = part!("S"), - path = "", - query = None, - port_part = None, - host = Some("S"), -} - -test_parse! { - test_uri_parse_authority_form, - "localhost:3000", - ["localhosT:3000"], - - scheme_part = None, - authority_part = part!("localhost:3000"), - path = "", - query = None, - host = Some("localhost"), - port_part = Port::from_str("3000").ok(), -} - - -test_parse! { - test_uri_parse_absolute_with_default_port_http, - "http://127.0.0.1:80", - ["http://127.0.0.1:80/"], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1:80"), - host = Some("127.0.0.1"), - path = "/", - query = None, - port_part = Port::from_str("80").ok(), -} - -test_parse! { - test_uri_parse_absolute_with_default_port_https, - "https://127.0.0.1:443", - ["https://127.0.0.1:443/"], - - scheme_part = part!("https"), - authority_part = part!("127.0.0.1:443"), - host = Some("127.0.0.1"), - path = "/", - query = None, - port_part = Port::from_str("443").ok(), -} - -test_parse! { - test_uri_parse_fragment_questionmark, - "http://127.0.0.1/#?", - [], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1"), - host = Some("127.0.0.1"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_uri_parse_path_with_terminating_questionmark, - "http://127.0.0.1/path?", - [], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1"), - path = "/path", - query = Some(""), - port_part = None, -} - -test_parse! { - test_uri_parse_absolute_form_with_empty_path_and_nonempty_query, - "http://127.0.0.1?foo=bar", - [], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1"), - path = "/", - query = Some("foo=bar"), - port_part = None, -} - -test_parse! { - test_uri_parse_absolute_form_with_empty_path_and_fragment_with_slash, - "http://127.0.0.1#foo/bar", - [], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_uri_parse_absolute_form_with_empty_path_and_fragment_with_questionmark, - "http://127.0.0.1#foo?bar", - [], - - scheme_part = part!("http"), - authority_part = part!("127.0.0.1"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_uri_parse_long_host_with_no_scheme, - "thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost", - [], - - scheme_part = None, - authority_part = part!("thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost"), - path = "", - query = None, - port_part = None, -} - -test_parse! { - test_uri_parse_long_host_with_port_and_no_scheme, - "thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost:1234", - [], - - scheme_part = None, - authority_part = part!("thequickbrownfoxjumpedoverthelazydogtofindthelargedangerousdragon.localhost:1234"), - path = "", - query = None, - port_part = Port::from_str("1234").ok(), -} - -test_parse! { - test_userinfo1, - "http://a:b@127.0.0.1:1234/", - [], - - scheme_part = part!("http"), - authority_part = part!("a:b@127.0.0.1:1234"), - host = Some("127.0.0.1"), - path = "/", - query = None, - port_part = Port::from_str("1234").ok(), -} - -test_parse! { - test_userinfo2, - "http://a:b@127.0.0.1/", - [], - - scheme_part = part!("http"), - authority_part = part!("a:b@127.0.0.1"), - host = Some("127.0.0.1"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_userinfo3, - "http://a@127.0.0.1/", - [], - - scheme_part = part!("http"), - authority_part = part!("a@127.0.0.1"), - host = Some("127.0.0.1"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_userinfo_with_port, - "user@localhost:3000", - [], - - scheme_part = None, - authority_part = part!("user@localhost:3000"), - path = "", - query = None, - host = Some("localhost"), - port_part = Port::from_str("3000").ok(), -} - -test_parse! { - test_userinfo_pass_with_port, - "user:pass@localhost:3000", - [], - - scheme_part = None, - authority_part = part!("user:pass@localhost:3000"), - path = "", - query = None, - host = Some("localhost"), - port_part = Port::from_str("3000").ok(), -} - -test_parse! { - test_ipv6, - "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/", - [], - - scheme_part = part!("http"), - authority_part = part!("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"), - host = Some("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_ipv6_shorthand, - "http://[::1]/", - [], - - scheme_part = part!("http"), - authority_part = part!("[::1]"), - host = Some("[::1]"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_ipv6_shorthand2, - "http://[::]/", - [], - - scheme_part = part!("http"), - authority_part = part!("[::]"), - host = Some("[::]"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_ipv6_shorthand3, - "http://[2001:db8::2:1]/", - [], - - scheme_part = part!("http"), - authority_part = part!("[2001:db8::2:1]"), - host = Some("[2001:db8::2:1]"), - path = "/", - query = None, - port_part = None, -} - -test_parse! { - test_ipv6_with_port, - "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8008/", - [], - - scheme_part = part!("http"), - authority_part = part!("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8008"), - host = Some("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"), - path = "/", - query = None, - port_part = Port::from_str("8008").ok(), -} - -test_parse! { - test_percentage_encoded_path, - "/echo/abcdefgh_i-j%20/abcdefg_i-j%20478", - [], - - scheme_part = None, - authority_part = None, - host = None, - path = "/echo/abcdefgh_i-j%20/abcdefg_i-j%20478", - query = None, - port_part = None, -} - -test_parse! { - test_path_permissive, - "/foo=bar|baz\\^~%", - [], - - path = "/foo=bar|baz\\^~%", -} - -test_parse! { - test_query_permissive, - "/?foo={bar|baz}\\^`", - [], - - query = Some("foo={bar|baz}\\^`"), -} - -#[test] -fn test_uri_parse_error() { - fn err(s: &str) { - Uri::from_str(s).unwrap_err(); - } - - err("http://"); - err("htt:p//host"); - err("hyper.rs/"); - err("hyper.rs?key=val"); - err("?key=val"); - err("localhost/"); - err("localhost?key=val"); - err("\0"); - err("http://[::1"); - err("http://::1]"); - err("localhost:8080:3030"); - err("@"); - err("http://username:password@/wut"); - - // illegal queries - err("/?foo\rbar"); - err("/?foo\nbar"); - err("/?<"); - err("/?>"); -} - -#[test] -fn test_max_uri_len() { - let mut uri = vec![]; - uri.extend(b"http://localhost/"); - uri.extend(vec![b'a'; 70 * 1024]); - - let uri = String::from_utf8(uri).unwrap(); - let res: Result = uri.parse(); - - assert_eq!(res.unwrap_err().0, ErrorKind::TooLong); -} - -#[test] -fn test_overflowing_scheme() { - let mut uri = vec![]; - uri.extend(vec![b'a'; 256]); - uri.extend(b"://localhost/"); - - let uri = String::from_utf8(uri).unwrap(); - let res: Result = uri.parse(); - - assert_eq!(res.unwrap_err().0, ErrorKind::SchemeTooLong); -} - -#[test] -fn test_max_length_scheme() { - let mut uri = vec![]; - uri.extend(vec![b'a'; 64]); - uri.extend(b"://localhost/"); - - let uri = String::from_utf8(uri).unwrap(); - let uri: Uri = uri.parse().unwrap(); - - assert_eq!(uri.scheme_str().unwrap().len(), 64); -} - -#[test] -fn test_uri_to_path_and_query() { - let cases = vec![ - ("/", "/"), - ("/foo?bar", "/foo?bar"), - ("/foo?bar#nope", "/foo?bar"), - ("http://hyper.rs", "/"), - ("http://hyper.rs/", "/"), - ("http://hyper.rs/path", "/path"), - ("http://hyper.rs?query", "/?query"), - ("*", "*"), - ]; - - for case in cases { - let uri = Uri::from_str(case.0).unwrap(); - let s = uri.path_and_query().unwrap().to_string(); - - assert_eq!(s, case.1); - } -} - -#[test] -fn test_authority_uri_parts_round_trip() { - let s = "hyper.rs"; - let uri = Uri::from_str(s).expect("first parse"); - assert_eq!(uri, s); - assert_eq!(uri.to_string(), s); - - let parts = uri.into_parts(); - let uri2 = Uri::from_parts(parts).expect("from_parts"); - assert_eq!(uri2, s); - assert_eq!(uri2.to_string(), s); -} - -#[test] -fn test_partial_eq_path_with_terminating_questionmark() { - let a = "/path"; - let uri = Uri::from_str("/path?").expect("first parse"); - - assert_eq!(uri, a); -} diff --git a/third_party/rust/http/v0_1/crate/src/version.rs b/third_party/rust/http/v0_1/crate/src/version.rs deleted file mode 100644 index 3229dc32693f..000000000000 --- a/third_party/rust/http/v0_1/crate/src/version.rs +++ /dev/null @@ -1,68 +0,0 @@ -//! HTTP version -//! -//! This module contains a definition of the `Version` type. The `Version` -//! type is intended to be accessed through the root of the crate -//! (`http::Version`) rather than this module. -//! -//! The `Version` type contains constants that represent the various versions -//! of the HTTP protocol. -//! -//! # Examples -//! -//! ``` -//! use http::Version; -//! -//! let http11 = Version::HTTP_11; -//! let http2 = Version::HTTP_2; -//! assert!(http11 != http2); -//! -//! println!("{:?}", http2); -//! ``` - -use std::fmt; - -/// Represents a version of the HTTP spec. -#[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)] -pub struct Version(Http); - -impl Version { - /// `HTTP/0.9` - pub const HTTP_09: Version = Version(Http::Http09); - - /// `HTTP/1.0` - pub const HTTP_10: Version = Version(Http::Http10); - - /// `HTTP/1.1` - pub const HTTP_11: Version = Version(Http::Http11); - - /// `HTTP/2.0` - pub const HTTP_2: Version = Version(Http::H2); -} - -#[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)] -enum Http { - Http09, - Http10, - Http11, - H2, -} - -impl Default for Version { - #[inline] - fn default() -> Version { - Version::HTTP_11 - } -} - -impl fmt::Debug for Version { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Http::*; - - f.write_str(match self.0 { - Http09 => "HTTP/0.9", - Http10 => "HTTP/1.0", - Http11 => "HTTP/1.1", - H2 => "HTTP/2.0", - }) - } -} diff --git a/third_party/rust/http/v0_1/crate/tests/header_map.rs b/third_party/rust/http/v0_1/crate/tests/header_map.rs deleted file mode 100644 index f84c019834d7..000000000000 --- a/third_party/rust/http/v0_1/crate/tests/header_map.rs +++ /dev/null @@ -1,379 +0,0 @@ -extern crate http; - -use http::*; -use http::header::*; - -#[test] -fn smoke() { - let mut headers = HeaderMap::new(); - - assert!(headers.get("hello").is_none()); - - let name: HeaderName = "hello".parse().unwrap(); - - match headers.entry(&name).unwrap() { - Entry::Vacant(e) => { - e.insert("world".parse().unwrap()); - } - _ => panic!(), - } - - assert!(headers.get("hello").is_some()); - - match headers.entry(&name).unwrap() { - Entry::Occupied(mut e) => { - assert_eq!(e.get(), &"world"); - - // Push another value - e.append("zomg".parse().unwrap()); - - let mut i = e.iter(); - - assert_eq!(*i.next().unwrap(), "world"); - assert_eq!(*i.next().unwrap(), "zomg"); - assert!(i.next().is_none()); - } - _ => panic!(), - } -} - -#[test] -#[should_panic] -fn reserve_over_capacity() { - // See https://github.com/hyperium/http/issues/352 - let mut headers = HeaderMap::::with_capacity(32); - headers.reserve(50_000); // over MAX_SIZE -} - -#[test] -#[should_panic] -fn reserve_overflow() { - // See https://github.com/hyperium/http/issues/352 - let mut headers = HeaderMap::::with_capacity(0); - headers.reserve(std::usize::MAX); // next_power_of_two overflows -} - -#[test] -fn drain() { - let mut headers = HeaderMap::new(); - - // Insert a single value - let name: HeaderName = "hello".parse().unwrap(); - headers.insert(name, "world".parse().unwrap()); - - { - let mut iter = headers.drain(); - let (name, values) = iter.next().unwrap(); - assert_eq!(name.as_str(), "hello"); - - let values: Vec<_> = values.collect(); - assert_eq!(values.len(), 1); - assert_eq!(values[0], "world"); - - assert!(iter.next().is_none()); - } - - assert!(headers.is_empty()); - - // Insert two sequential values - headers.insert("hello".parse::().unwrap(), "world".parse().unwrap()); - headers.insert("zomg".parse::().unwrap(), "bar".parse().unwrap()); - headers.append("hello".parse::().unwrap(), "world2".parse().unwrap()); - - // Drain... - { - let mut iter = headers.drain(); - let (name, values) = iter.next().unwrap(); - assert_eq!(name.as_str(), "hello"); - - let values: Vec<_> = values.collect(); - assert_eq!(values.len(), 2); - assert_eq!(values[0], "world"); - assert_eq!(values[1], "world2"); - - let (name, values) = iter.next().unwrap(); - assert_eq!(name.as_str(), "zomg"); - - let values: Vec<_> = values.collect(); - assert_eq!(values.len(), 1); - assert_eq!(values[0], "bar"); - - assert!(iter.next().is_none()); - } -} - -#[test] -fn drain_drop_immediately() { - // test mem::forgetting does not double-free - - let mut headers = HeaderMap::new(); - headers.insert("hello", "world".parse().unwrap()); - headers.insert("zomg", "bar".parse().unwrap()); - headers.append("hello", "world2".parse().unwrap()); - - let iter = headers.drain(); - assert_eq!(iter.size_hint(), (2, Some(2))); - // not consuming `iter` -} - -#[test] -fn drain_forget() { - // test mem::forgetting does not double-free - - let mut headers = HeaderMap::::new(); - headers.insert("hello", "world".parse().unwrap()); - headers.insert("zomg", "bar".parse().unwrap()); - - assert_eq!(headers.len(), 2); - - { - let mut iter = headers.drain(); - assert_eq!(iter.size_hint(), (2, Some(2))); - let _ = iter.next().unwrap(); - std::mem::forget(iter); - } - - assert_eq!(headers.len(), 0); -} - -#[test] -fn drain_entry() { - let mut headers = HeaderMap::new(); - - headers.insert("hello".parse::().unwrap(), "world".parse().unwrap()); - headers.insert("zomg".parse::().unwrap(), "foo".parse().unwrap()); - headers.append("hello".parse::().unwrap(), "world2".parse().unwrap()); - headers.insert("more".parse::().unwrap(), "words".parse().unwrap()); - headers.append("more".parse::().unwrap(), "insertions".parse().unwrap()); - - // Using insert - { - let mut e = match headers.entry("hello").unwrap() { - Entry::Occupied(e) => e, - _ => panic!(), - }; - - let vals: Vec<_> = e.insert_mult("wat".parse().unwrap()).collect(); - assert_eq!(2, vals.len()); - assert_eq!(vals[0], "world"); - assert_eq!(vals[1], "world2"); - } -} - -#[test] -fn eq() { - let mut a = HeaderMap::new(); - let mut b = HeaderMap::new(); - - assert_eq!(a, b); - - a.insert("hello".parse::().unwrap(), "world".parse().unwrap()); - assert_ne!(a, b); - - b.insert("hello".parse::().unwrap(), "world".parse().unwrap()); - assert_eq!(a, b); - - a.insert("foo".parse::().unwrap(), "bar".parse().unwrap()); - a.append("foo".parse::().unwrap(), "baz".parse().unwrap()); - assert_ne!(a, b); - - b.insert("foo".parse::().unwrap(), "bar".parse().unwrap()); - assert_ne!(a, b); - - b.append("foo".parse::().unwrap(), "baz".parse().unwrap()); - assert_eq!(a, b); - - a.append("a".parse::().unwrap(), "a".parse().unwrap()); - a.append("a".parse::().unwrap(), "b".parse().unwrap()); - b.append("a".parse::().unwrap(), "b".parse().unwrap()); - b.append("a".parse::().unwrap(), "a".parse().unwrap()); - - assert_ne!(a, b); -} - -#[test] -fn into_header_name() { - let mut m = HeaderMap::new(); - m.insert(HOST, "localhost".parse().unwrap()); - m.insert(&ACCEPT, "*/*".parse().unwrap()); - m.insert("connection", "keep-alive".parse().unwrap()); - - m.append(LOCATION, "/".parse().unwrap()); - m.append(&VIA, "bob".parse().unwrap()); - m.append("transfer-encoding", "chunked".parse().unwrap()); - - assert_eq!(m.len(), 6); -} - -#[test] -fn as_header_name() { - let mut m = HeaderMap::new(); - let v: HeaderValue = "localhost".parse().unwrap(); - m.insert(HOST, v.clone()); - - let expected = Some(&v); - - assert_eq!(m.get("host"), expected); - assert_eq!(m.get(&HOST), expected); - - let s = String::from("host"); - assert_eq!(m.get(&s), expected); - assert_eq!(m.get(s.as_str()), expected); -} - -#[test] -fn insert_all_std_headers() { - let mut m = HeaderMap::new(); - - for (i, hdr) in STD.iter().enumerate() { - m.insert(hdr.clone(), hdr.as_str().parse().unwrap()); - - for j in 0..(i+1) { - assert_eq!(m[&STD[j]], STD[j].as_str()); - } - - if i != 0 { - for j in (i+1)..STD.len() { - assert!(m.get(&STD[j]).is_none(), "contained {}; j={}", STD[j].as_str(), j); - } - } - } -} - -#[test] -fn insert_79_custom_std_headers() { - let mut h = HeaderMap::new(); - let hdrs = custom_std(79); - - for (i, hdr) in hdrs.iter().enumerate() { - h.insert(hdr.clone(), hdr.as_str().parse().unwrap()); - - for j in 0..(i+1) { - assert_eq!(h[&hdrs[j]], hdrs[j].as_str()); - } - - for j in (i+1)..hdrs.len() { - assert!(h.get(&hdrs[j]).is_none()); - } - } -} - -#[test] -fn append_multiple_values() { - let mut map = HeaderMap::new(); - - map.append(header::CONTENT_TYPE, "json".parse().unwrap()); - map.append(header::CONTENT_TYPE, "html".parse().unwrap()); - map.append(header::CONTENT_TYPE, "xml".parse().unwrap()); - - let vals = map.get_all(&header::CONTENT_TYPE) - .iter() - .collect::>(); - - assert_eq!(&vals, &[&"json", &"html", &"xml"]); -} - -fn custom_std(n: usize) -> Vec { - (0..n).map(|i| { - let s = format!("{}-{}", STD[i % STD.len()].as_str(), i); - s.parse().unwrap() - }).collect() -} - -const STD: &'static [HeaderName] = &[ - ACCEPT, - ACCEPT_CHARSET, - ACCEPT_ENCODING, - ACCEPT_LANGUAGE, - ACCEPT_RANGES, - ACCESS_CONTROL_ALLOW_CREDENTIALS, - ACCESS_CONTROL_ALLOW_HEADERS, - ACCESS_CONTROL_ALLOW_METHODS, - ACCESS_CONTROL_ALLOW_ORIGIN, - ACCESS_CONTROL_EXPOSE_HEADERS, - ACCESS_CONTROL_MAX_AGE, - ACCESS_CONTROL_REQUEST_HEADERS, - ACCESS_CONTROL_REQUEST_METHOD, - AGE, - ALLOW, - ALT_SVC, - AUTHORIZATION, - CACHE_CONTROL, - CONNECTION, - CONTENT_DISPOSITION, - CONTENT_ENCODING, - CONTENT_LANGUAGE, - CONTENT_LENGTH, - CONTENT_LOCATION, - CONTENT_RANGE, - CONTENT_SECURITY_POLICY, - CONTENT_SECURITY_POLICY_REPORT_ONLY, - CONTENT_TYPE, - COOKIE, - DNT, - DATE, - ETAG, - EXPECT, - EXPIRES, - FORWARDED, - FROM, - HOST, - IF_MATCH, - IF_MODIFIED_SINCE, - IF_NONE_MATCH, - IF_RANGE, - IF_UNMODIFIED_SINCE, - LAST_MODIFIED, - LINK, - LOCATION, - MAX_FORWARDS, - ORIGIN, - PRAGMA, - PROXY_AUTHENTICATE, - PROXY_AUTHORIZATION, - PUBLIC_KEY_PINS, - PUBLIC_KEY_PINS_REPORT_ONLY, - RANGE, - REFERER, - REFERRER_POLICY, - RETRY_AFTER, - SERVER, - SET_COOKIE, - STRICT_TRANSPORT_SECURITY, - TE, - TRAILER, - TRANSFER_ENCODING, - USER_AGENT, - UPGRADE, - UPGRADE_INSECURE_REQUESTS, - VARY, - VIA, - WARNING, - WWW_AUTHENTICATE, - X_CONTENT_TYPE_OPTIONS, - X_DNS_PREFETCH_CONTROL, - X_FRAME_OPTIONS, - X_XSS_PROTECTION, -]; - -#[test] -fn get_invalid() { - let mut headers = HeaderMap::new(); - headers.insert("foo", "bar".parse().unwrap()); - assert!(headers.get("Evil\r\nKey").is_none()); -} - -#[test] -#[should_panic] -fn insert_invalid() { - let mut headers = HeaderMap::new(); - headers.insert("evil\r\nfoo", "bar".parse().unwrap()); -} - -#[test] -fn value_htab() { - // RFC 7230 Section 3.2: - // > field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] - HeaderValue::from_static("hello\tworld"); - HeaderValue::from_str("hello\tworld").unwrap(); -} diff --git a/third_party/rust/http/v0_1/crate/tests/header_map_fuzz.rs b/third_party/rust/http/v0_1/crate/tests/header_map_fuzz.rs deleted file mode 100644 index 03318cdf3353..000000000000 --- a/third_party/rust/http/v0_1/crate/tests/header_map_fuzz.rs +++ /dev/null @@ -1,365 +0,0 @@ -extern crate http; -extern crate rand; -extern crate quickcheck; - -use http::*; -use http::header::*; - -use quickcheck::{Arbitrary, Gen, QuickCheck, TestResult}; -use rand::{StdRng, SeedableRng, Rng}; - -use std::collections::HashMap; - -#[test] -fn header_map_fuzz() { - fn prop(fuzz: Fuzz) -> TestResult { - fuzz.run(); - TestResult::from_bool(true) - } - - QuickCheck::new() - .quickcheck(prop as fn(Fuzz) -> TestResult) -} - -#[derive(Debug, Clone)] -struct Fuzz { - // The magic seed that makes the test case reproducible - seed: [usize; 4], - - // Actions to perform - steps: Vec, - - // Number of steps to drop - reduce: usize, -} - -#[derive(Debug)] -struct Weight { - insert: usize, - remove: usize, - append: usize, -} - -#[derive(Debug, Clone)] -struct Step { - action: Action, - expect: AltMap, -} - -#[derive(Debug, Clone)] -enum Action { - Insert { - name: HeaderName, // Name to insert - val: HeaderValue, // Value to insert - old: Option, // Old value - }, - Append { - name: HeaderName, - val: HeaderValue, - ret: bool, - }, - Remove { - name: HeaderName, // Name to remove - val: Option, // Value to get - }, -} - -// An alternate implementation of HeaderMap backed by HashMap -#[derive(Debug, Clone, Default)] -struct AltMap { - map: HashMap>, -} - -impl Fuzz { - fn new(seed: [usize; 4]) -> Fuzz { - // Seed the RNG - let mut rng = StdRng::from_seed(&seed); - - let mut steps = vec![]; - let mut expect = AltMap::default(); - let num = rng.gen_range(5, 500); - - let weight = Weight { - insert: rng.gen_range(1, 10), - remove: rng.gen_range(1, 10), - append: rng.gen_range(1, 10), - }; - - while steps.len() < num { - steps.push(expect.gen_step(&weight, &mut rng)); - } - - Fuzz { - seed: seed, - steps: steps, - reduce: 0, - } - } - - fn run(self) { - // Create a new header map - let mut map = HeaderMap::new(); - - // Number of steps to perform - let take = self.steps.len() - self.reduce; - - for step in self.steps.into_iter().take(take) { - step.action.apply(&mut map); - - step.expect.assert_identical(&map); - } - } -} - -impl Arbitrary for Fuzz { - fn arbitrary(g: &mut G) -> Self { - Fuzz::new(quickcheck::Rng::gen(g)) - } -} - -impl AltMap { - fn gen_step(&mut self, weight: &Weight, rng: &mut StdRng) -> Step { - let action = self.gen_action(weight, rng); - - Step { - action: action, - expect: self.clone(), - } - } - - /// This will also apply the action against `self` - fn gen_action(&mut self, weight: &Weight, rng: &mut StdRng) -> Action { - let sum = weight.insert + - weight.remove + - weight.append; - - let mut num = rng.gen_range(0, sum); - - if num < weight.insert { - return self.gen_insert(rng); - } - - num -= weight.insert; - - if num < weight.remove { - return self.gen_remove(rng); - } - - num -= weight.remove; - - if num < weight.append { - return self.gen_append(rng); - } - - unreachable!(); - } - - fn gen_insert(&mut self, rng: &mut StdRng) -> Action { - let name = self.gen_name(4, rng); - let val = gen_header_value(rng); - let old = self.insert(name.clone(), val.clone()); - - Action::Insert { - name: name, - val: val, - old: old, - } - } - - fn gen_remove(&mut self, rng: &mut StdRng) -> Action { - let name = self.gen_name(-4, rng); - let val = self.remove(&name); - - Action::Remove { - name: name, - val: val, - } - } - - fn gen_append(&mut self, rng: &mut StdRng) -> Action { - let name = self.gen_name(-5, rng); - let val = gen_header_value(rng); - - let vals = self.map.entry(name.clone()) - .or_insert(vec![]); - - let ret = !vals.is_empty(); - vals.push(val.clone()); - - Action::Append { - name: name, - val: val, - ret: ret, - } - } - - /// Negative numbers weigh finding an existing header higher - fn gen_name(&self, weight: i32, rng: &mut StdRng) -> HeaderName { - let mut existing = rng.gen_weighted_bool(weight.abs() as u32); - - if weight < 0 { - existing = !existing; - } - - if existing { - // Existing header - if let Some(name) = self.find_random_name(rng) { - name - } else { - gen_header_name(rng) - } - } else { - gen_header_name(rng) - } - } - - fn find_random_name(&self, rng: &mut StdRng) -> Option { - if self.map.is_empty() { - None - } else { - let n = rng.gen_range(0, self.map.len()); - self.map.keys().nth(n).map(Clone::clone) - } - } - - fn insert(&mut self, name: HeaderName, val: HeaderValue) -> Option { - let old = self.map.insert(name, vec![val]); - old.and_then(|v| v.into_iter().next()) - } - - fn remove(&mut self, name: &HeaderName) -> Option { - self.map.remove(name).and_then(|v| v.into_iter().next()) - } - - fn assert_identical(&self, other: &HeaderMap) { - assert_eq!(self.map.len(), other.keys_len()); - - for (key, val) in &self.map { - // Test get - assert_eq!(other.get(key), val.get(0)); - - // Test get_all - let vals = other.get_all(key); - let actual: Vec<_> = vals.iter().collect(); - assert_eq!(&actual[..], &val[..]); - } - } -} - -impl Action { - fn apply(self, map: &mut HeaderMap) { - match self { - Action::Insert { name, val, old } => { - let actual = map.insert(name, val); - assert_eq!(actual, old); - } - Action::Remove { name, val } => { - // Just to help track the state, load all associated values. - let _ = map.get_all(&name).iter().collect::>(); - - let actual = map.remove(&name); - assert_eq!(actual, val); - } - Action::Append { name, val, ret } => { - assert_eq!(ret, map.append(name, val)); - } - } - } -} - -fn gen_header_name(g: &mut StdRng) -> HeaderName { - if g.gen_weighted_bool(2) { - g.choose(&[ - header::ACCEPT, - header::ACCEPT_CHARSET, - header::ACCEPT_ENCODING, - header::ACCEPT_LANGUAGE, - header::ACCEPT_RANGES, - header::ACCESS_CONTROL_ALLOW_CREDENTIALS, - header::ACCESS_CONTROL_ALLOW_HEADERS, - header::ACCESS_CONTROL_ALLOW_METHODS, - header::ACCESS_CONTROL_ALLOW_ORIGIN, - header::ACCESS_CONTROL_EXPOSE_HEADERS, - header::ACCESS_CONTROL_MAX_AGE, - header::ACCESS_CONTROL_REQUEST_HEADERS, - header::ACCESS_CONTROL_REQUEST_METHOD, - header::AGE, - header::ALLOW, - header::ALT_SVC, - header::AUTHORIZATION, - header::CACHE_CONTROL, - header::CONNECTION, - header::CONTENT_DISPOSITION, - header::CONTENT_ENCODING, - header::CONTENT_LANGUAGE, - header::CONTENT_LENGTH, - header::CONTENT_LOCATION, - header::CONTENT_RANGE, - header::CONTENT_SECURITY_POLICY, - header::CONTENT_SECURITY_POLICY_REPORT_ONLY, - header::CONTENT_TYPE, - header::COOKIE, - header::DNT, - header::DATE, - header::ETAG, - header::EXPECT, - header::EXPIRES, - header::FORWARDED, - header::FROM, - header::HOST, - header::IF_MATCH, - header::IF_MODIFIED_SINCE, - header::IF_NONE_MATCH, - header::IF_RANGE, - header::IF_UNMODIFIED_SINCE, - header::LAST_MODIFIED, - header::LINK, - header::LOCATION, - header::MAX_FORWARDS, - header::ORIGIN, - header::PRAGMA, - header::PROXY_AUTHENTICATE, - header::PROXY_AUTHORIZATION, - header::PUBLIC_KEY_PINS, - header::PUBLIC_KEY_PINS_REPORT_ONLY, - header::RANGE, - header::REFERER, - header::REFERRER_POLICY, - header::RETRY_AFTER, - header::SERVER, - header::SET_COOKIE, - header::STRICT_TRANSPORT_SECURITY, - header::TE, - header::TRAILER, - header::TRANSFER_ENCODING, - header::USER_AGENT, - header::UPGRADE, - header::UPGRADE_INSECURE_REQUESTS, - header::VARY, - header::VIA, - header::WARNING, - header::WWW_AUTHENTICATE, - header::X_CONTENT_TYPE_OPTIONS, - header::X_DNS_PREFETCH_CONTROL, - header::X_FRAME_OPTIONS, - header::X_XSS_PROTECTION, - ]).unwrap().clone() - } else { - let value = gen_string(g, 1, 25); - HeaderName::from_bytes(value.as_bytes()).unwrap() - } -} - -fn gen_header_value(g: &mut StdRng) -> HeaderValue { - let value = gen_string(g, 0, 70); - HeaderValue::from_bytes(value.as_bytes()).unwrap() -} - -fn gen_string(g: &mut StdRng, min: usize, max: usize) -> String { - let bytes: Vec<_> = (min..max).map(|_| { - // Chars to pick from - g.choose(b"ABCDEFGHIJKLMNOPQRSTUVabcdefghilpqrstuvwxyz----").unwrap().clone() - }).collect(); - - String::from_utf8(bytes).unwrap() -} diff --git a/third_party/rust/http/v0_1/crate/tests/status_code.rs b/third_party/rust/http/v0_1/crate/tests/status_code.rs deleted file mode 100644 index 72ba810ca135..000000000000 --- a/third_party/rust/http/v0_1/crate/tests/status_code.rs +++ /dev/null @@ -1,67 +0,0 @@ -extern crate http; - -use http::*; - -#[test] -fn from_bytes() { - for ok in &["100", "101", "199", "200", "250", "299", "321", "399", "499", "599"] { - assert!(StatusCode::from_bytes(ok.as_bytes()).is_ok()); - } - - for not_ok in &["0", "00", "10", "40", "99", "000", "010", "099", "600", "610", "999"] { - assert!(StatusCode::from_bytes(not_ok.as_bytes()).is_err()); - } -} - -#[test] -fn equates_with_u16() { - let status = StatusCode::from_u16(200u16).unwrap(); - assert_eq!(200u16, status); - assert_eq!(status, 200u16); -} - -macro_rules! test_round_trip { - ($($num:expr,)+) => { - #[test] - fn roundtrip() { - $( - let status = StatusCode::from_bytes(stringify!($num).as_bytes()).unwrap(); - let expect = $num; - - assert_eq!(u16::from(status), expect); - )+ - } - } -} - -test_round_trip!( - 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, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, - 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, - 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, - 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, - ); From 8d1c965db2afe4ee95994949f5d7be3fcce46a36 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Wed, 13 Dec 2023 12:44:32 -0800 Subject: [PATCH 5/8] Remove unused Rust bytes v0.4.16 source This is no longer necessary now that all code is using v1.5. --- third_party/rust/Cargo.lock | 6 +- third_party/rust/Cargo.toml | 4 - third_party/rust/bytes/v0_4/BUILD.gn | 65 - third_party/rust/bytes/v0_4/README.chromium | 7 - .../bytes/v0_4/crate/.cargo_vcs_info.json | 5 - .../rust/bytes/v0_4/crate/CHANGELOG.md | 86 - third_party/rust/bytes/v0_4/crate/Cargo.toml | 46 - .../rust/bytes/v0_4/crate/Cargo.toml.orig | 40 - third_party/rust/bytes/v0_4/crate/LICENSE | 25 - third_party/rust/bytes/v0_4/crate/README.md | 45 - .../rust/bytes/v0_4/crate/benches/bytes.rs | 250 -- .../bytes/v0_4/crate/ci/before_deploy.ps1 | 23 - .../rust/bytes/v0_4/crate/ci/before_deploy.sh | 33 - .../rust/bytes/v0_4/crate/ci/install.sh | 31 - .../rust/bytes/v0_4/crate/ci/script.sh | 18 - third_party/rust/bytes/v0_4/crate/ci/tsan | 28 - .../rust/bytes/v0_4/crate/src/buf/buf.rs | 1154 ------- .../rust/bytes/v0_4/crate/src/buf/buf_mut.rs | 1167 ------- .../rust/bytes/v0_4/crate/src/buf/chain.rs | 226 -- .../rust/bytes/v0_4/crate/src/buf/from_buf.rs | 117 - .../rust/bytes/v0_4/crate/src/buf/into_buf.rs | 146 - .../rust/bytes/v0_4/crate/src/buf/iter.rs | 116 - .../rust/bytes/v0_4/crate/src/buf/mod.rs | 38 - .../rust/bytes/v0_4/crate/src/buf/reader.rs | 97 - .../rust/bytes/v0_4/crate/src/buf/take.rs | 155 - .../bytes/v0_4/crate/src/buf/vec_deque.rs | 39 - .../rust/bytes/v0_4/crate/src/buf/writer.rs | 88 - .../rust/bytes/v0_4/crate/src/bytes.rs | 2947 ----------------- .../rust/bytes/v0_4/crate/src/debug.rs | 40 - .../rust/bytes/v0_4/crate/src/either.rs | 89 - third_party/rust/bytes/v0_4/crate/src/lib.rs | 105 - .../rust/bytes/v0_4/crate/src/serde.rs | 82 - .../rust/bytes/v0_4/crate/tests/test_buf.rs | 58 - .../bytes/v0_4/crate/tests/test_buf_mut.rs | 83 - .../rust/bytes/v0_4/crate/tests/test_bytes.rs | 773 ----- .../rust/bytes/v0_4/crate/tests/test_chain.rs | 122 - .../rust/bytes/v0_4/crate/tests/test_debug.rs | 35 - .../bytes/v0_4/crate/tests/test_from_buf.rs | 34 - .../rust/bytes/v0_4/crate/tests/test_iter.rs | 22 - .../bytes/v0_4/crate/tests/test_reader.rs | 28 - .../rust/bytes/v0_4/crate/tests/test_serde.rs | 21 - .../rust/bytes/v0_4/crate/tests/test_take.rs | 13 - 42 files changed, 1 insertion(+), 8506 deletions(-) delete mode 100644 third_party/rust/bytes/v0_4/BUILD.gn delete mode 100644 third_party/rust/bytes/v0_4/README.chromium delete mode 100644 third_party/rust/bytes/v0_4/crate/.cargo_vcs_info.json delete mode 100644 third_party/rust/bytes/v0_4/crate/CHANGELOG.md delete mode 100644 third_party/rust/bytes/v0_4/crate/Cargo.toml delete mode 100644 third_party/rust/bytes/v0_4/crate/Cargo.toml.orig delete mode 100644 third_party/rust/bytes/v0_4/crate/LICENSE delete mode 100644 third_party/rust/bytes/v0_4/crate/README.md delete mode 100644 third_party/rust/bytes/v0_4/crate/benches/bytes.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/ci/before_deploy.ps1 delete mode 100644 third_party/rust/bytes/v0_4/crate/ci/before_deploy.sh delete mode 100644 third_party/rust/bytes/v0_4/crate/ci/install.sh delete mode 100644 third_party/rust/bytes/v0_4/crate/ci/script.sh delete mode 100644 third_party/rust/bytes/v0_4/crate/ci/tsan delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/buf.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/buf_mut.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/chain.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/from_buf.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/into_buf.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/iter.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/mod.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/reader.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/take.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/vec_deque.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/buf/writer.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/bytes.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/debug.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/either.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/lib.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/src/serde.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_buf.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_buf_mut.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_bytes.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_chain.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_debug.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_from_buf.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_iter.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_reader.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_serde.rs delete mode 100644 third_party/rust/bytes/v0_4/crate/tests/test_take.rs diff --git a/third_party/rust/Cargo.lock b/third_party/rust/Cargo.lock index f6d567313ac3..2d5dfff3a76f 100644 --- a/third_party/rust/Cargo.lock +++ b/third_party/rust/Cargo.lock @@ -212,11 +212,7 @@ version = "1.4.3" [[package]] name = "bytes" -version = "0.4.12" -dependencies = [ - "byteorder", - "iovec", -] +version = "1.5.0" [[package]] name = "camino" diff --git a/third_party/rust/Cargo.toml b/third_party/rust/Cargo.toml index 890e594535f7..5b9d930ef7b8 100644 --- a/third_party/rust/Cargo.toml +++ b/third_party/rust/Cargo.toml @@ -308,10 +308,6 @@ package = "bls12_381" path = "byteorder/v1/crate" package = "byteorder" -[patch.crates-io.bytes_v0_4] -path = "bytes/v0_4/crate" -package = "bytes" - [patch.crates-io.bytes_v1] path = "bytes/v1/crate" package = "bytes" diff --git a/third_party/rust/bytes/v0_4/BUILD.gn b/third_party/rust/bytes/v0_4/BUILD.gn deleted file mode 100644 index 40bad8bbe0c7..000000000000 --- a/third_party/rust/bytes/v0_4/BUILD.gn +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2023 The Chromium Authors -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import("//build/rust/cargo_crate.gni") - -cargo_crate("lib") { - crate_name = "bytes" - epoch = "0.4" - crate_type = "rlib" - - # Only for usage from third-party crates. Add the crate to - # third_party.toml to use it from first-party code. - visibility = [ "//brave/third_party/rust/*" ] - crate_root = "crate/src/lib.rs" - sources = [ - "//brave/third_party/rust/bytes/v0_4/crate/benches/bytes.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/buf.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/buf_mut.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/chain.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/from_buf.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/into_buf.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/iter.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/mod.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/reader.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/take.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/vec_deque.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/buf/writer.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/bytes.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/debug.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/either.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/lib.rs", - "//brave/third_party/rust/bytes/v0_4/crate/src/serde.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_buf.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_buf_mut.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_bytes.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_chain.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_debug.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_from_buf.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_iter.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_reader.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_serde.rs", - "//brave/third_party/rust/bytes/v0_4/crate/tests/test_take.rs", - ] - inputs = [ - "//brave/third_party/rust/bytes/v0_4/crate/CHANGELOG.md", - "//brave/third_party/rust/bytes/v0_4/crate/README.md", - ] - - # Unit tests skipped. Generate with --with-tests to include them. - build_native_rust_unit_tests = false - edition = "2015" - cargo_pkg_version = "0.4.12" - cargo_pkg_authors = "Carl Lerche " - cargo_pkg_name = "bytes" - cargo_pkg_description = "Types and traits for working with bytes" - library_configs -= [ "//build/config/compiler:chromium_code" ] - library_configs += [ "//build/config/compiler:no_chromium_code" ] - executable_configs -= [ "//build/config/compiler:chromium_code" ] - executable_configs += [ "//build/config/compiler:no_chromium_code" ] - deps = [ - "//brave/third_party/rust/byteorder/v1:lib", - "//brave/third_party/rust/iovec/v0_1:lib", - ] -} diff --git a/third_party/rust/bytes/v0_4/README.chromium b/third_party/rust/bytes/v0_4/README.chromium deleted file mode 100644 index 4ff47aba8b72..000000000000 --- a/third_party/rust/bytes/v0_4/README.chromium +++ /dev/null @@ -1,7 +0,0 @@ -Name: bytes -URL: https://crates.io/crates/bytes -Description: Types and traits for working with bytes -Version: 0.4.12 -Security Critical: yes -License: MIT -Revision: 4948b1053b1af8f474a107b958dd0086ada06b17 diff --git a/third_party/rust/bytes/v0_4/crate/.cargo_vcs_info.json b/third_party/rust/bytes/v0_4/crate/.cargo_vcs_info.json deleted file mode 100644 index 395b9c2bdffc..000000000000 --- a/third_party/rust/bytes/v0_4/crate/.cargo_vcs_info.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "git": { - "sha1": "4948b1053b1af8f474a107b958dd0086ada06b17" - } -} diff --git a/third_party/rust/bytes/v0_4/crate/CHANGELOG.md b/third_party/rust/bytes/v0_4/crate/CHANGELOG.md deleted file mode 100644 index 881b6f602933..000000000000 --- a/third_party/rust/bytes/v0_4/crate/CHANGELOG.md +++ /dev/null @@ -1,86 +0,0 @@ -# 0.4.12 (March 6, 2018) - -### Added -- Implement `FromIterator<&'a u8>` for `BytesMut`/`Bytes` (#244). -- Implement `Buf` for `VecDeque` (#249). - -# 0.4.11 (November 17, 2018) - -* Use raw pointers for potentially racy loads (#233). -* Implement `BufRead` for `buf::Reader` (#232). -* Documentation tweaks (#234). - -# 0.4.10 (September 4, 2018) - -* impl `Buf` and `BufMut` for `Either` (#225). -* Add `Bytes::slice_ref` (#208). - -# 0.4.9 (July 12, 2018) - -* Add 128 bit number support behind a feature flag (#209). -* Implement `IntoBuf` for `&mut [u8]` - -# 0.4.8 (May 25, 2018) - -* Fix panic in `BytesMut` `FromIterator` implementation. -* Bytes: Recycle space when reserving space in vec mode (#197). -* Bytes: Add resize fn (#203). - -# 0.4.7 (April 27, 2018) - -* Make `Buf` and `BufMut` usable as trait objects (#186). -* impl BorrowMut for BytesMut (#185). -* Improve accessor performance (#195). - -# 0.4.6 (Janary 8, 2018) - -* Implement FromIterator for Bytes/BytesMut (#148). -* Add `advance` fn to Bytes/BytesMut (#166). -* Add `unsplit` fn to `BytesMut` (#162, #173). -* Improvements to Bytes split fns (#92). - -# 0.4.5 (August 12, 2017) - -* Fix range bug in `Take::bytes` -* Misc performance improvements -* Add extra `PartialEq` implementations. -* Add `Bytes::with_capacity` -* Implement `AsMut[u8]` for `BytesMut` - -# 0.4.4 (May 26, 2017) - -* Add serde support behind feature flag -* Add `extend_from_slice` on `Bytes` and `BytesMut` -* Add `truncate` and `clear` on `Bytes` -* Misc additional std trait implementations -* Misc performance improvements - -# 0.4.3 (April 30, 2017) - -* Fix Vec::advance_mut bug -* Bump minimum Rust version to 1.15 -* Misc performance tweaks - -# 0.4.2 (April 5, 2017) - -* Misc performance tweaks -* Improved `Debug` implementation for `Bytes` -* Avoid some incorrect assert panics - -# 0.4.1 (March 15, 2017) - -* Expose `buf` module and have most types available from there vs. root. -* Implement `IntoBuf` for `T: Buf`. -* Add `FromBuf` and `Buf::collect`. -* Add iterator adapter for `Buf`. -* Add scatter/gather support to `Buf` and `BufMut`. -* Add `Buf::chain`. -* Reduce allocations on repeated calls to `BytesMut::reserve`. -* Implement `Debug` for more types. -* Remove `Source` in favor of `IntoBuf`. -* Implement `Extend` for `BytesMut`. - - -# 0.4.0 (February 24, 2017) - -* Initial release diff --git a/third_party/rust/bytes/v0_4/crate/Cargo.toml b/third_party/rust/bytes/v0_4/crate/Cargo.toml deleted file mode 100644 index 96137c151954..000000000000 --- a/third_party/rust/bytes/v0_4/crate/Cargo.toml +++ /dev/null @@ -1,46 +0,0 @@ -# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO -# -# When uploading crates to the registry Cargo will automatically -# "normalize" Cargo.toml files for maximal compatibility -# with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g. crates.io) dependencies -# -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) - -[package] -name = "bytes" -version = "0.4.12" -authors = ["Carl Lerche "] -exclude = [".gitignore", ".travis.yml", "deploy.sh", "bench/**/*", "test/**/*"] -description = "Types and traits for working with bytes" -homepage = "https://github.com/carllerche/bytes" -documentation = "https://docs.rs/bytes/0.4.12/bytes" -readme = "README.md" -keywords = ["buffers", "zero-copy", "io"] -categories = ["network-programming", "data-structures"] -license = "MIT" -repository = "https://github.com/carllerche/bytes" -[package.metadata.docs.rs] -features = ["i128"] -[dependencies.byteorder] -version = "1.1.0" - -[dependencies.either] -version = "1.5" -optional = true -default-features = false - -[dependencies.iovec] -version = "0.1" - -[dependencies.serde] -version = "1.0" -optional = true -[dev-dependencies.serde_test] -version = "1.0" - -[features] -i128 = ["byteorder/i128"] diff --git a/third_party/rust/bytes/v0_4/crate/Cargo.toml.orig b/third_party/rust/bytes/v0_4/crate/Cargo.toml.orig deleted file mode 100644 index 99331b69a7a2..000000000000 --- a/third_party/rust/bytes/v0_4/crate/Cargo.toml.orig +++ /dev/null @@ -1,40 +0,0 @@ -[package] - -name = "bytes" -# When releasing to crates.io: -# - Update html_root_url. -# - Update CHANGELOG.md. -# - Update doc URL. -# - Create "v0.4.x" git tag. -version = "0.4.12" -license = "MIT" -authors = ["Carl Lerche "] -description = "Types and traits for working with bytes" -documentation = "https://docs.rs/bytes/0.4.12/bytes" -homepage = "https://github.com/carllerche/bytes" -repository = "https://github.com/carllerche/bytes" -readme = "README.md" -keywords = ["buffers", "zero-copy", "io"] -exclude = [ - ".gitignore", - ".travis.yml", - "deploy.sh", - "bench/**/*", - "test/**/*" -] -categories = ["network-programming", "data-structures"] - -[package.metadata.docs.rs] -features = ["i128"] - -[dependencies] -byteorder = "1.1.0" -iovec = "0.1" -serde = { version = "1.0", optional = true } -either = { version = "1.5", default-features = false, optional = true } - -[dev-dependencies] -serde_test = "1.0" - -[features] -i128 = ["byteorder/i128"] diff --git a/third_party/rust/bytes/v0_4/crate/LICENSE b/third_party/rust/bytes/v0_4/crate/LICENSE deleted file mode 100644 index 58fb29a12384..000000000000 --- a/third_party/rust/bytes/v0_4/crate/LICENSE +++ /dev/null @@ -1,25 +0,0 @@ -Copyright (c) 2018 Carl Lerche - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/bytes/v0_4/crate/README.md b/third_party/rust/bytes/v0_4/crate/README.md deleted file mode 100644 index 01359742ce4d..000000000000 --- a/third_party/rust/bytes/v0_4/crate/README.md +++ /dev/null @@ -1,45 +0,0 @@ -# Bytes - -A utility library for working with bytes. - -[![Crates.io](https://img.shields.io/crates/v/bytes.svg?maxAge=2592000)](https://crates.io/crates/bytes) -[![Build Status](https://travis-ci.org/carllerche/bytes.svg?branch=master)](https://travis-ci.org/carllerche/bytes) - -[Documentation](https://docs.rs/bytes/0.4.12/bytes/) - -## Usage - -To use `bytes`, first add this to your `Cargo.toml`: - -```toml -[dependencies] -bytes = "0.4.12" -``` - -Next, add this to your crate: - -```rust -extern crate bytes; - -use bytes::{Bytes, BytesMut, Buf, BufMut}; -``` - -## Serde support - -Serde support is optional and disabled by default. To enable use the feature `serde`. - -```toml -[dependencies] -bytes = { version = "0.4.12", features = ["serde"] } -``` - -## License - -This project is licensed under the [MIT license](LICENSE). - -### Contribution - -Unless you explicitly state otherwise, any contribution intentionally submitted -for inclusion in `bytes` by you, shall be licensed as MIT, without any additional -terms or conditions. - diff --git a/third_party/rust/bytes/v0_4/crate/benches/bytes.rs b/third_party/rust/bytes/v0_4/crate/benches/bytes.rs deleted file mode 100644 index 7a338746b04c..000000000000 --- a/third_party/rust/bytes/v0_4/crate/benches/bytes.rs +++ /dev/null @@ -1,250 +0,0 @@ -#![feature(test)] - -extern crate bytes; -extern crate test; - -use test::Bencher; -use bytes::{Bytes, BytesMut, BufMut}; - -#[bench] -fn alloc_small(b: &mut Bencher) { - b.iter(|| { - for _ in 0..1024 { - test::black_box(BytesMut::with_capacity(12)); - } - }) -} - -#[bench] -fn alloc_mid(b: &mut Bencher) { - b.iter(|| { - test::black_box(BytesMut::with_capacity(128)); - }) -} - -#[bench] -fn alloc_big(b: &mut Bencher) { - b.iter(|| { - test::black_box(BytesMut::with_capacity(4096)); - }) -} - -#[bench] -fn split_off_and_drop(b: &mut Bencher) { - b.iter(|| { - for _ in 0..1024 { - let v = vec![10; 200]; - let mut b = Bytes::from(v); - test::black_box(b.split_off(100)); - test::black_box(b); - } - }) -} - -#[bench] -fn deref_unique(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(4096); - buf.put(&[0u8; 1024][..]); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&buf[..]); - } - }) -} - -#[bench] -fn deref_unique_unroll(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(4096); - buf.put(&[0u8; 1024][..]); - - b.iter(|| { - for _ in 0..128 { - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - } - }) -} - -#[bench] -fn deref_shared(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(4096); - buf.put(&[0u8; 1024][..]); - let _b2 = buf.split_off(1024); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&buf[..]); - } - }) -} - -#[bench] -fn deref_inline(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(8); - buf.put(&[0u8; 8][..]); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&buf[..]); - } - }) -} - -#[bench] -fn deref_two(b: &mut Bencher) { - let mut buf1 = BytesMut::with_capacity(8); - buf1.put(&[0u8; 8][..]); - - let mut buf2 = BytesMut::with_capacity(4096); - buf2.put(&[0u8; 1024][..]); - - b.iter(|| { - for _ in 0..512 { - test::black_box(&buf1[..]); - test::black_box(&buf2[..]); - } - }) -} - -#[bench] -fn clone_inline(b: &mut Bencher) { - let bytes = Bytes::from_static(b"hello world"); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&bytes.clone()); - } - }) -} - -#[bench] -fn clone_static(b: &mut Bencher) { - let bytes = Bytes::from_static("hello world 1234567890 and have a good byte 0987654321".as_bytes()); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&bytes.clone()); - } - }) -} - -#[bench] -fn clone_arc(b: &mut Bencher) { - let bytes = Bytes::from("hello world 1234567890 and have a good byte 0987654321".as_bytes()); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&bytes.clone()); - } - }) -} - -#[bench] -fn alloc_write_split_to_mid(b: &mut Bencher) { - b.iter(|| { - let mut buf = BytesMut::with_capacity(128); - buf.put_slice(&[0u8; 64]); - test::black_box(buf.split_to(64)); - }) -} - -#[bench] -fn drain_write_drain(b: &mut Bencher) { - let data = [0u8; 128]; - - b.iter(|| { - let mut buf = BytesMut::with_capacity(1024); - let mut parts = Vec::with_capacity(8); - - for _ in 0..8 { - buf.put(&data[..]); - parts.push(buf.split_to(128)); - } - - test::black_box(parts); - }) -} - -#[bench] -fn fmt_write(b: &mut Bencher) { - use std::fmt::Write; - let mut buf = BytesMut::with_capacity(128); - let s = "foo bar baz quux lorem ipsum dolor et"; - - b.bytes = s.len() as u64; - b.iter(|| { - let _ = write!(buf, "{}", s); - test::black_box(&buf); - unsafe { buf.set_len(0); } - }) -} - -#[bench] -fn from_long_slice(b: &mut Bencher) { - let data = [0u8; 128]; - b.bytes = data.len() as u64; - b.iter(|| { - let buf = BytesMut::from(&data[..]); - test::black_box(buf); - }) -} - -#[bench] -fn slice_empty(b: &mut Bencher) { - b.iter(|| { - let b = Bytes::from(vec![17; 1024]).clone(); - for i in 0..1000 { - test::black_box(b.slice(i % 100, i % 100)); - } - }) -} - -#[bench] -fn slice_short_from_arc(b: &mut Bencher) { - b.iter(|| { - // `clone` is to convert to ARC - let b = Bytes::from(vec![17; 1024]).clone(); - for i in 0..1000 { - test::black_box(b.slice(1, 2 + i % 10)); - } - }) -} - -// Keep in sync with bytes.rs -#[cfg(target_pointer_width = "64")] -const INLINE_CAP: usize = 4 * 8 - 1; -#[cfg(target_pointer_width = "32")] -const INLINE_CAP: usize = 4 * 4 - 1; - -#[bench] -fn slice_avg_le_inline_from_arc(b: &mut Bencher) { - b.iter(|| { - // `clone` is to convert to ARC - let b = Bytes::from(vec![17; 1024]).clone(); - for i in 0..1000 { - // [1, INLINE_CAP] - let len = 1 + i % (INLINE_CAP - 1); - test::black_box(b.slice(i % 10, i % 10 + len)); - } - }) -} - -#[bench] -fn slice_large_le_inline_from_arc(b: &mut Bencher) { - b.iter(|| { - // `clone` is to convert to ARC - let b = Bytes::from(vec![17; 1024]).clone(); - for i in 0..1000 { - // [INLINE_CAP - 10, INLINE_CAP] - let len = INLINE_CAP - 9 + i % 10; - test::black_box(b.slice(i % 10, i % 10 + len)); - } - }) -} diff --git a/third_party/rust/bytes/v0_4/crate/ci/before_deploy.ps1 b/third_party/rust/bytes/v0_4/crate/ci/before_deploy.ps1 deleted file mode 100644 index 191a30b88d75..000000000000 --- a/third_party/rust/bytes/v0_4/crate/ci/before_deploy.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -# This script takes care of packaging the build artifacts that will go in the -# release zipfile - -$SRC_DIR = $PWD.Path -$STAGE = [System.Guid]::NewGuid().ToString() - -Set-Location $ENV:Temp -New-Item -Type Directory -Name $STAGE -Set-Location $STAGE - -$ZIP = "$SRC_DIR\$($Env:CRATE_NAME)-$($Env:APPVEYOR_REPO_TAG_NAME)-$($Env:TARGET).zip" - -# TODO Update this to package the right artifacts -Copy-Item "$SRC_DIR\target\$($Env:TARGET)\release\hello.exe" '.\' - -7z a "$ZIP" * - -Push-AppveyorArtifact "$ZIP" - -Remove-Item *.* -Force -Set-Location .. -Remove-Item $STAGE -Set-Location $SRC_DIR diff --git a/third_party/rust/bytes/v0_4/crate/ci/before_deploy.sh b/third_party/rust/bytes/v0_4/crate/ci/before_deploy.sh deleted file mode 100644 index 026dc2898d07..000000000000 --- a/third_party/rust/bytes/v0_4/crate/ci/before_deploy.sh +++ /dev/null @@ -1,33 +0,0 @@ -# This script takes care of building your crate and packaging it for release - -set -ex - -main() { - local src=$(pwd) \ - stage= - - case $TRAVIS_OS_NAME in - linux) - stage=$(mktemp -d) - ;; - osx) - stage=$(mktemp -d -t tmp) - ;; - esac - - test -f Cargo.lock || cargo generate-lockfile - - # TODO Update this to build the artifacts that matter to you - cross rustc --bin hello --target $TARGET --release -- -C lto - - # TODO Update this to package the right artifacts - cp target/$TARGET/release/hello $stage/ - - cd $stage - tar czf $src/$CRATE_NAME-$TRAVIS_TAG-$TARGET.tar.gz * - cd $src - - rm -rf $stage -} - -main diff --git a/third_party/rust/bytes/v0_4/crate/ci/install.sh b/third_party/rust/bytes/v0_4/crate/ci/install.sh deleted file mode 100644 index 76bb7340d88e..000000000000 --- a/third_party/rust/bytes/v0_4/crate/ci/install.sh +++ /dev/null @@ -1,31 +0,0 @@ -set -ex - -main() { - curl https://sh.rustup.rs -sSf | \ - sh -s -- -y --default-toolchain $TRAVIS_RUST_VERSION - - local target= - if [ $TRAVIS_OS_NAME = linux ]; then - target=x86_64-unknown-linux-gnu - sort=sort - else - target=x86_64-apple-darwin - sort=gsort # for `sort --sort-version`, from brew's coreutils. - fi - - # This fetches latest stable release - local tag=$(git ls-remote --tags --refs --exit-code https://github.com/japaric/cross \ - | cut -d/ -f3 \ - | grep -E '^v[0-9.]+$' \ - | $sort --version-sort \ - | tail -n1) - echo cross version: $tag - curl -LSfs https://japaric.github.io/trust/install.sh | \ - sh -s -- \ - --force \ - --git japaric/cross \ - --tag $tag \ - --target $target -} - -main diff --git a/third_party/rust/bytes/v0_4/crate/ci/script.sh b/third_party/rust/bytes/v0_4/crate/ci/script.sh deleted file mode 100644 index d1ed7f924205..000000000000 --- a/third_party/rust/bytes/v0_4/crate/ci/script.sh +++ /dev/null @@ -1,18 +0,0 @@ -# This script takes care of testing your crate - -set -ex - -main() { - cross build --target $TARGET $EXTRA_ARGS - - if [ ! -z $DISABLE_TESTS ]; then - return - fi - - cross test --target $TARGET $EXTRA_ARGS -} - -# we don't run the "test phase" when doing deploys -if [ -z $TRAVIS_TAG ]; then - main -fi diff --git a/third_party/rust/bytes/v0_4/crate/ci/tsan b/third_party/rust/bytes/v0_4/crate/ci/tsan deleted file mode 100644 index 9cc54841bf39..000000000000 --- a/third_party/rust/bytes/v0_4/crate/ci/tsan +++ /dev/null @@ -1,28 +0,0 @@ -# TSAN suppressions file for `bytes` - -# TSAN does not understand fences and `Arc::drop` is implemented using a fence. -# This causes many false positives. -race:Arc*drop -race:arc*Weak*drop - -# `std` mpsc is not used in any Bytes code base. This race is triggered by some -# rust runtime logic. -race:std*mpsc_queue - -# Some test runtime races. Allocation should be race free -race:alloc::alloc - -# Not sure why this is warning, but it is in the test harness and not the library. -race:TestEvent*clone -race:test::run_tests_console::*closure - -# Probably more fences in std. -race:__call_tls_dtors - -# `is_inline_or_static` is explicitly called concurrently without synchronization. -# The safety explanation can be found in a comment. -race:Inner::is_inline_or_static - -# This ignores a false positive caused by `thread::park()`/`thread::unpark()`. -# See: https://github.com/rust-lang/rust/pull/54806#issuecomment-436193353 -race:pthread_cond_destroy diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/buf.rs b/third_party/rust/bytes/v0_4/crate/src/buf/buf.rs deleted file mode 100644 index dc20567d3b61..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/buf.rs +++ /dev/null @@ -1,1154 +0,0 @@ -use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain}; -use byteorder::{BigEndian, ByteOrder, LittleEndian}; -use iovec::IoVec; - -use std::{cmp, io, ptr}; - -macro_rules! buf_get_impl { - ($this:ident, $size:expr, $conv:path) => ({ - // try to convert directly from the bytes - let ret = { - // this Option trick is to avoid keeping a borrow on self - // when advance() is called (mut borrow) and to call bytes() only once - if let Some(src) = $this.bytes().get(..($size)) { - Some($conv(src)) - } else { - None - } - }; - if let Some(ret) = ret { - // if the direct convertion was possible, advance and return - $this.advance($size); - return ret; - } else { - // if not we copy the bytes in a temp buffer then convert - let mut buf = [0; ($size)]; - $this.copy_to_slice(&mut buf); // (do the advance) - return $conv(&buf); - } - }); - ($this:ident, $buf_size:expr, $conv:path, $len_to_read:expr) => ({ - // The same trick as above does not improve the best case speed. - // It seems to be linked to the way the method is optimised by the compiler - let mut buf = [0; ($buf_size)]; - $this.copy_to_slice(&mut buf[..($len_to_read)]); - return $conv(&buf[..($len_to_read)], $len_to_read); - }); -} - -/// Read bytes from a buffer. -/// -/// A buffer stores bytes in memory such that read operations are infallible. -/// The underlying storage may or may not be in contiguous memory. A `Buf` value -/// is a cursor into the buffer. Reading from `Buf` advances the cursor -/// position. It can be thought of as an efficient `Iterator` for collections of -/// bytes. -/// -/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`. -/// -/// ``` -/// use bytes::Buf; -/// use std::io::Cursor; -/// -/// let mut buf = Cursor::new(b"hello world"); -/// -/// assert_eq!(b'h', buf.get_u8()); -/// assert_eq!(b'e', buf.get_u8()); -/// assert_eq!(b'l', buf.get_u8()); -/// -/// let mut rest = [0; 8]; -/// buf.copy_to_slice(&mut rest); -/// -/// assert_eq!(&rest[..], b"lo world"); -/// ``` -pub trait Buf { - /// Returns the number of bytes between the current position and the end of - /// the buffer. - /// - /// This value is greater than or equal to the length of the slice returned - /// by `bytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world"); - /// - /// assert_eq!(buf.remaining(), 11); - /// - /// buf.get_u8(); - /// - /// assert_eq!(buf.remaining(), 10); - /// ``` - /// - /// # Implementer notes - /// - /// Implementations of `remaining` should ensure that the return value does - /// not change unless a call is made to `advance` or any other function that - /// is documented to change the `Buf`'s current position. - fn remaining(&self) -> usize; - - /// Returns a slice starting at the current position and of length between 0 - /// and `Buf::remaining()`. Note that this *can* return shorter slice (this allows - /// non-continuous internal representation). - /// - /// This is a lower level function. Most operations are done with other - /// functions. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world"); - /// - /// assert_eq!(buf.bytes(), b"hello world"); - /// - /// buf.advance(6); - /// - /// assert_eq!(buf.bytes(), b"world"); - /// ``` - /// - /// # Implementer notes - /// - /// This function should never panic. Once the end of the buffer is reached, - /// i.e., `Buf::remaining` returns 0, calls to `bytes` should return an - /// empty slice. - fn bytes(&self) -> &[u8]; - - /// Fills `dst` with potentially multiple slices starting at `self`'s - /// current position. - /// - /// If the `Buf` is backed by disjoint slices of bytes, `bytes_vec` enables - /// fetching more than one slice at once. `dst` is a slice of `IoVec` - /// references, enabling the slice to be directly used with [`writev`] - /// without any further conversion. The sum of the lengths of all the - /// buffers in `dst` will be less than or equal to `Buf::remaining()`. - /// - /// The entries in `dst` will be overwritten, but the data **contained** by - /// the slices **will not** be modified. If `bytes_vec` does not fill every - /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices - /// in `self. - /// - /// This is a lower level function. Most operations are done with other - /// functions. - /// - /// # Implementer notes - /// - /// This function should never panic. Once the end of the buffer is reached, - /// i.e., `Buf::remaining` returns 0, calls to `bytes_vec` must return 0 - /// without mutating `dst`. - /// - /// Implementations should also take care to properly handle being called - /// with `dst` being a zero length slice. - /// - /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html - fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize { - if dst.is_empty() { - return 0; - } - - if self.has_remaining() { - dst[0] = self.bytes().into(); - 1 - } else { - 0 - } - } - - /// Advance the internal cursor of the Buf - /// - /// The next call to `bytes` will return a slice starting `cnt` bytes - /// further into the underlying buffer. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world"); - /// - /// assert_eq!(buf.bytes(), b"hello world"); - /// - /// buf.advance(6); - /// - /// assert_eq!(buf.bytes(), b"world"); - /// ``` - /// - /// # Panics - /// - /// This function **may** panic if `cnt > self.remaining()`. - /// - /// # Implementer notes - /// - /// It is recommended for implementations of `advance` to panic if `cnt > - /// self.remaining()`. If the implementation does not panic, the call must - /// behave as if `cnt == self.remaining()`. - /// - /// A call with `cnt == 0` should never panic and be a no-op. - fn advance(&mut self, cnt: usize); - - /// Returns true if there are any more bytes to consume - /// - /// This is equivalent to `self.remaining() != 0`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"a"); - /// - /// assert!(buf.has_remaining()); - /// - /// buf.get_u8(); - /// - /// assert!(!buf.has_remaining()); - /// ``` - fn has_remaining(&self) -> bool { - self.remaining() > 0 - } - - /// Copies bytes from `self` into `dst`. - /// - /// The cursor is advanced by the number of bytes copied. `self` must have - /// enough remaining bytes to fill `dst`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world"); - /// let mut dst = [0; 5]; - /// - /// buf.copy_to_slice(&mut dst); - /// assert_eq!(b"hello", &dst); - /// assert_eq!(6, buf.remaining()); - /// ``` - /// - /// # Panics - /// - /// This function panics if `self.remaining() < dst.len()` - fn copy_to_slice(&mut self, dst: &mut [u8]) { - let mut off = 0; - - assert!(self.remaining() >= dst.len()); - - while off < dst.len() { - let cnt; - - unsafe { - let src = self.bytes(); - cnt = cmp::min(src.len(), dst.len() - off); - - ptr::copy_nonoverlapping( - src.as_ptr(), dst[off..].as_mut_ptr(), cnt); - - off += src.len(); - } - - self.advance(cnt); - } - } - - /// Gets an unsigned 8 bit integer from `self`. - /// - /// The current position is advanced by 1. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08 hello"); - /// assert_eq!(8, buf.get_u8()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is no more remaining data in `self`. - fn get_u8(&mut self) -> u8 { - assert!(self.remaining() >= 1); - let ret = self.bytes()[0]; - self.advance(1); - ret - } - - /// Gets a signed 8 bit integer from `self`. - /// - /// The current position is advanced by 1. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08 hello"); - /// assert_eq!(8, buf.get_i8()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is no more remaining data in `self`. - fn get_i8(&mut self) -> i8 { - assert!(self.remaining() >= 1); - let ret = self.bytes()[0] as i8; - self.advance(1); - ret - } - - #[doc(hidden)] - #[deprecated(note="use get_u16_be or get_u16_le")] - fn get_u16(&mut self) -> u16 where Self: Sized { - let mut buf = [0; 2]; - self.copy_to_slice(&mut buf); - T::read_u16(&buf) - } - - /// Gets an unsigned 16 bit integer from `self` in big-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08\x09 hello"); - /// assert_eq!(0x0809, buf.get_u16_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_u16_be(&mut self) -> u16 { - buf_get_impl!(self, 2, BigEndian::read_u16); - } - - /// Gets an unsigned 16 bit integer from `self` in little-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x09\x08 hello"); - /// assert_eq!(0x0809, buf.get_u16_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_u16_le(&mut self) -> u16 { - buf_get_impl!(self, 2, LittleEndian::read_u16); - } - - #[doc(hidden)] - #[deprecated(note="use get_i16_be or get_i16_le")] - fn get_i16(&mut self) -> i16 where Self: Sized { - let mut buf = [0; 2]; - self.copy_to_slice(&mut buf); - T::read_i16(&buf) - } - - /// Gets a signed 16 bit integer from `self` in big-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08\x09 hello"); - /// assert_eq!(0x0809, buf.get_i16_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_i16_be(&mut self) -> i16 { - buf_get_impl!(self, 2, BigEndian::read_i16); - } - - /// Gets a signed 16 bit integer from `self` in little-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x09\x08 hello"); - /// assert_eq!(0x0809, buf.get_i16_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_i16_le(&mut self) -> i16 { - buf_get_impl!(self, 2, LittleEndian::read_i16); - } - - #[doc(hidden)] - #[deprecated(note="use get_u32_be or get_u32_le")] - fn get_u32(&mut self) -> u32 where Self: Sized { - let mut buf = [0; 4]; - self.copy_to_slice(&mut buf); - T::read_u32(&buf) - } - - /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello"); - /// assert_eq!(0x0809A0A1, buf.get_u32_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_u32_be(&mut self) -> u32 { - buf_get_impl!(self, 4, BigEndian::read_u32); - } - - /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello"); - /// assert_eq!(0x0809A0A1, buf.get_u32_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_u32_le(&mut self) -> u32 { - buf_get_impl!(self, 4, LittleEndian::read_u32); - } - - #[doc(hidden)] - #[deprecated(note="use get_i32_be or get_i32_le")] - fn get_i32(&mut self) -> i32 where Self: Sized { - let mut buf = [0; 4]; - self.copy_to_slice(&mut buf); - T::read_i32(&buf) - } - - /// Gets a signed 32 bit integer from `self` in big-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello"); - /// assert_eq!(0x0809A0A1, buf.get_i32_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_i32_be(&mut self) -> i32 { - buf_get_impl!(self, 4, BigEndian::read_i32); - } - - /// Gets a signed 32 bit integer from `self` in little-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello"); - /// assert_eq!(0x0809A0A1, buf.get_i32_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_i32_le(&mut self) -> i32 { - buf_get_impl!(self, 4, LittleEndian::read_i32); - } - - #[doc(hidden)] - #[deprecated(note="use get_u64_be or get_u64_le")] - fn get_u64(&mut self) -> u64 where Self: Sized { - let mut buf = [0; 8]; - self.copy_to_slice(&mut buf); - T::read_u64(&buf) - } - - /// Gets an unsigned 64 bit integer from `self` in big-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"); - /// assert_eq!(0x0102030405060708, buf.get_u64_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_u64_be(&mut self) -> u64 { - buf_get_impl!(self, 8, BigEndian::read_u64); - } - - /// Gets an unsigned 64 bit integer from `self` in little-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"); - /// assert_eq!(0x0102030405060708, buf.get_u64_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_u64_le(&mut self) -> u64 { - buf_get_impl!(self, 8, LittleEndian::read_u64); - } - - #[doc(hidden)] - #[deprecated(note="use get_i64_be or get_i64_le")] - fn get_i64(&mut self) -> i64 where Self: Sized { - let mut buf = [0; 8]; - self.copy_to_slice(&mut buf); - T::read_i64(&buf) - } - - /// Gets a signed 64 bit integer from `self` in big-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"); - /// assert_eq!(0x0102030405060708, buf.get_i64_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_i64_be(&mut self) -> i64 { - buf_get_impl!(self, 8, BigEndian::read_i64); - } - - /// Gets a signed 64 bit integer from `self` in little-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"); - /// assert_eq!(0x0102030405060708, buf.get_i64_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_i64_le(&mut self) -> i64 { - buf_get_impl!(self, 8, LittleEndian::read_i64); - } - - /// Gets an unsigned 128 bit integer from `self` in big-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"); - /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - #[cfg(feature = "i128")] - fn get_u128_be(&mut self) -> u128 { - buf_get_impl!(self, 16, BigEndian::read_u128); - } - - /// Gets an unsigned 128 bit integer from `self` in little-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"); - /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - #[cfg(feature = "i128")] - fn get_u128_le(&mut self) -> u128 { - buf_get_impl!(self, 16, LittleEndian::read_u128); - } - - /// Gets a signed 128 bit integer from `self` in big-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"); - /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - #[cfg(feature = "i128")] - fn get_i128_be(&mut self) -> i128 { - buf_get_impl!(self, 16, BigEndian::read_i128); - } - - /// Gets a signed 128 bit integer from `self` in little-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"); - /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - #[cfg(feature = "i128")] - fn get_i128_le(&mut self) -> i128 { - buf_get_impl!(self, 16, LittleEndian::read_i128); - } - - #[doc(hidden)] - #[deprecated(note="use get_uint_be or get_uint_le")] - fn get_uint(&mut self, nbytes: usize) -> u64 where Self: Sized { - let mut buf = [0; 8]; - self.copy_to_slice(&mut buf[..nbytes]); - T::read_uint(&buf[..nbytes], nbytes) - } - - /// Gets an unsigned n-byte integer from `self` in big-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, BigEndian}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x01\x02\x03 hello"); - /// assert_eq!(0x010203, buf.get_uint_be(3)); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_uint_be(&mut self, nbytes: usize) -> u64 { - buf_get_impl!(self, 8, BigEndian::read_uint, nbytes); - } - - /// Gets an unsigned n-byte integer from `self` in little-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x03\x02\x01 hello"); - /// assert_eq!(0x010203, buf.get_uint_le(3)); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_uint_le(&mut self, nbytes: usize) -> u64 { - buf_get_impl!(self, 8, LittleEndian::read_uint, nbytes); - } - - #[doc(hidden)] - #[deprecated(note="use get_int_be or get_int_le")] - fn get_int(&mut self, nbytes: usize) -> i64 where Self: Sized { - let mut buf = [0; 8]; - self.copy_to_slice(&mut buf[..nbytes]); - T::read_int(&buf[..nbytes], nbytes) - } - - /// Gets a signed n-byte integer from `self` in big-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x01\x02\x03 hello"); - /// assert_eq!(0x010203, buf.get_int_be(3)); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_int_be(&mut self, nbytes: usize) -> i64 { - buf_get_impl!(self, 8, BigEndian::read_int, nbytes); - } - - /// Gets a signed n-byte integer from `self` in little-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x03\x02\x01 hello"); - /// assert_eq!(0x010203, buf.get_int_le(3)); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_int_le(&mut self, nbytes: usize) -> i64 { - buf_get_impl!(self, 8, LittleEndian::read_int, nbytes); - } - - #[doc(hidden)] - #[deprecated(note="use get_f32_be or get_f32_le")] - fn get_f32(&mut self) -> f32 where Self: Sized { - let mut buf = [0; 4]; - self.copy_to_slice(&mut buf); - T::read_f32(&buf) - } - - /// Gets an IEEE754 single-precision (4 bytes) floating point number from - /// `self` in big-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x3F\x99\x99\x9A hello"); - /// assert_eq!(1.2f32, buf.get_f32_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_f32_be(&mut self) -> f32 { - buf_get_impl!(self, 4, BigEndian::read_f32); - } - - /// Gets an IEEE754 single-precision (4 bytes) floating point number from - /// `self` in little-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x9A\x99\x99\x3F hello"); - /// assert_eq!(1.2f32, buf.get_f32_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_f32_le(&mut self) -> f32 { - buf_get_impl!(self, 4, LittleEndian::read_f32); - } - - #[doc(hidden)] - #[deprecated(note="use get_f64_be or get_f64_le")] - fn get_f64(&mut self) -> f64 where Self: Sized { - let mut buf = [0; 8]; - self.copy_to_slice(&mut buf); - T::read_f64(&buf) - } - - /// Gets an IEEE754 double-precision (8 bytes) floating point number from - /// `self` in big-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"); - /// assert_eq!(1.2f64, buf.get_f64_be()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_f64_be(&mut self) -> f64 { - buf_get_impl!(self, 8, BigEndian::read_f64); - } - - /// Gets an IEEE754 double-precision (8 bytes) floating point number from - /// `self` in little-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"); - /// assert_eq!(1.2f64, buf.get_f64_le()); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining data in `self`. - fn get_f64_le(&mut self) -> f64 { - buf_get_impl!(self, 8, LittleEndian::read_f64); - } - - /// Transforms a `Buf` into a concrete buffer. - /// - /// `collect()` can operate on any value that implements `Buf`, and turn it - /// into the relevent concrete buffer type. - /// - /// # Examples - /// - /// Collecting a buffer and loading the contents into a `Vec`. - /// - /// ``` - /// use bytes::{Buf, Bytes, IntoBuf}; - /// - /// let buf = Bytes::from(&b"hello world"[..]).into_buf(); - /// let vec: Vec = buf.collect(); - /// - /// assert_eq!(vec, &b"hello world"[..]); - /// ``` - fn collect(self) -> B - where Self: Sized, - B: FromBuf, - { - B::from_buf(self) - } - - /// Creates an adaptor which will read at most `limit` bytes from `self`. - /// - /// This function returns a new instance of `Buf` which will read at most - /// `limit` bytes. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, BufMut}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new("hello world").take(5); - /// let mut dst = vec![]; - /// - /// dst.put(&mut buf); - /// assert_eq!(dst, b"hello"); - /// - /// let mut buf = buf.into_inner(); - /// dst.clear(); - /// dst.put(&mut buf); - /// assert_eq!(dst, b" world"); - /// ``` - fn take(self, limit: usize) -> Take - where Self: Sized - { - super::take::new(self, limit) - } - - /// Creates an adaptor which will chain this buffer with another. - /// - /// The returned `Buf` instance will first consume all bytes from `self`. - /// Afterwards the output is equivalent to the output of next. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, Buf, IntoBuf}; - /// use bytes::buf::Chain; - /// - /// let buf = Bytes::from(&b"hello "[..]).into_buf() - /// .chain(Bytes::from(&b"world"[..])); - /// - /// let full: Bytes = buf.collect(); - /// assert_eq!(full[..], b"hello world"[..]); - /// ``` - fn chain(self, next: U) -> Chain - where U: IntoBuf, - Self: Sized, - { - Chain::new(self, next.into_buf()) - } - - /// Creates a "by reference" adaptor for this instance of `Buf`. - /// - /// The returned adaptor also implements `Buf` and will simply borrow `self`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, BufMut}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new("hello world"); - /// let mut dst = vec![]; - /// - /// { - /// let mut reference = buf.by_ref(); - /// dst.put(&mut reference.take(5)); - /// assert_eq!(dst, b"hello"); - /// } // drop our &mut reference so we can use `buf` again - /// - /// dst.clear(); - /// dst.put(&mut buf); - /// assert_eq!(dst, b" world"); - /// ``` - fn by_ref(&mut self) -> &mut Self where Self: Sized { - self - } - - /// Creates an adaptor which implements the `Read` trait for `self`. - /// - /// This function returns a new value which implements `Read` by adapting - /// the `Read` trait functions to the `Buf` trait functions. Given that - /// `Buf` operations are infallible, none of the `Read` functions will - /// return with `Err`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, IntoBuf, Bytes}; - /// use std::io::Read; - /// - /// let buf = Bytes::from("hello world").into_buf(); - /// - /// let mut reader = buf.reader(); - /// let mut dst = [0; 1024]; - /// - /// let num = reader.read(&mut dst).unwrap(); - /// - /// assert_eq!(11, num); - /// assert_eq!(&dst[..11], b"hello world"); - /// ``` - fn reader(self) -> Reader where Self: Sized { - super::reader::new(self) - } - - /// Returns an iterator over the bytes contained by the buffer. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, IntoBuf, Bytes}; - /// - /// let buf = Bytes::from(&b"abc"[..]).into_buf(); - /// let mut iter = buf.iter(); - /// - /// assert_eq!(iter.next(), Some(b'a')); - /// assert_eq!(iter.next(), Some(b'b')); - /// assert_eq!(iter.next(), Some(b'c')); - /// assert_eq!(iter.next(), None); - /// ``` - fn iter(self) -> Iter where Self: Sized { - super::iter::new(self) - } -} - -impl<'a, T: Buf + ?Sized> Buf for &'a mut T { - fn remaining(&self) -> usize { - (**self).remaining() - } - - fn bytes(&self) -> &[u8] { - (**self).bytes() - } - - fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize { - (**self).bytes_vec(dst) - } - - fn advance(&mut self, cnt: usize) { - (**self).advance(cnt) - } -} - -impl Buf for Box { - fn remaining(&self) -> usize { - (**self).remaining() - } - - fn bytes(&self) -> &[u8] { - (**self).bytes() - } - - fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize { - (**self).bytes_vec(dst) - } - - fn advance(&mut self, cnt: usize) { - (**self).advance(cnt) - } -} - -impl> Buf for io::Cursor { - fn remaining(&self) -> usize { - let len = self.get_ref().as_ref().len(); - let pos = self.position(); - - if pos >= len as u64 { - return 0; - } - - len - pos as usize - } - - fn bytes(&self) -> &[u8] { - let len = self.get_ref().as_ref().len(); - let pos = self.position() as usize; - - if pos >= len { - return Default::default(); - } - - &(self.get_ref().as_ref())[pos..] - } - - fn advance(&mut self, cnt: usize) { - let pos = (self.position() as usize) - .checked_add(cnt).expect("overflow"); - - assert!(pos <= self.get_ref().as_ref().len()); - - self.set_position(pos as u64); - } -} - -impl Buf for Option<[u8; 1]> { - fn remaining(&self) -> usize { - if self.is_some() { - 1 - } else { - 0 - } - } - - fn bytes(&self) -> &[u8] { - self.as_ref().map(AsRef::as_ref) - .unwrap_or(Default::default()) - } - - fn advance(&mut self, cnt: usize) { - if cnt == 0 { - return; - } - - if self.is_none() { - panic!("overflow"); - } else { - assert_eq!(1, cnt); - *self = None; - } - } -} - -// The existance of this function makes the compiler catch if the Buf -// trait is "object-safe" or not. -fn _assert_trait_object(_b: &Buf) {} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/buf_mut.rs b/third_party/rust/bytes/v0_4/crate/src/buf/buf_mut.rs deleted file mode 100644 index 7f3c1f756fbb..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/buf_mut.rs +++ /dev/null @@ -1,1167 +0,0 @@ -use super::{IntoBuf, Writer}; -use byteorder::{LittleEndian, ByteOrder, BigEndian}; -use iovec::IoVec; - -use std::{cmp, io, ptr, usize}; - -/// A trait for values that provide sequential write access to bytes. -/// -/// Write bytes to a buffer -/// -/// A buffer stores bytes in memory such that write operations are infallible. -/// The underlying storage may or may not be in contiguous memory. A `BufMut` -/// value is a cursor into the buffer. Writing to `BufMut` advances the cursor -/// position. -/// -/// The simplest `BufMut` is a `Vec`. -/// -/// ``` -/// use bytes::BufMut; -/// -/// let mut buf = vec![]; -/// -/// buf.put("hello world"); -/// -/// assert_eq!(buf, b"hello world"); -/// ``` -pub trait BufMut { - /// Returns the number of bytes that can be written from the current - /// position until the end of the buffer is reached. - /// - /// This value is greater than or equal to the length of the slice returned - /// by `bytes_mut`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// use std::io::Cursor; - /// - /// let mut dst = [0; 10]; - /// let mut buf = Cursor::new(&mut dst[..]); - /// - /// assert_eq!(10, buf.remaining_mut()); - /// buf.put("hello"); - /// - /// assert_eq!(5, buf.remaining_mut()); - /// ``` - /// - /// # Implementer notes - /// - /// Implementations of `remaining_mut` should ensure that the return value - /// does not change unless a call is made to `advance_mut` or any other - /// function that is documented to change the `BufMut`'s current position. - fn remaining_mut(&self) -> usize; - - /// Advance the internal cursor of the BufMut - /// - /// The next call to `bytes_mut` will return a slice starting `cnt` bytes - /// further into the underlying buffer. - /// - /// This function is unsafe because there is no guarantee that the bytes - /// being advanced past have been initialized. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = Vec::with_capacity(16); - /// - /// unsafe { - /// buf.bytes_mut()[0] = b'h'; - /// buf.bytes_mut()[1] = b'e'; - /// - /// buf.advance_mut(2); - /// - /// buf.bytes_mut()[0] = b'l'; - /// buf.bytes_mut()[1..3].copy_from_slice(b"lo"); - /// - /// buf.advance_mut(3); - /// } - /// - /// assert_eq!(5, buf.len()); - /// assert_eq!(buf, b"hello"); - /// ``` - /// - /// # Panics - /// - /// This function **may** panic if `cnt > self.remaining_mut()`. - /// - /// # Implementer notes - /// - /// It is recommended for implementations of `advance_mut` to panic if - /// `cnt > self.remaining_mut()`. If the implementation does not panic, - /// the call must behave as if `cnt == self.remaining_mut()`. - /// - /// A call with `cnt == 0` should never panic and be a no-op. - unsafe fn advance_mut(&mut self, cnt: usize); - - /// Returns true if there is space in `self` for more bytes. - /// - /// This is equivalent to `self.remaining_mut() != 0`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// use std::io::Cursor; - /// - /// let mut dst = [0; 5]; - /// let mut buf = Cursor::new(&mut dst); - /// - /// assert!(buf.has_remaining_mut()); - /// - /// buf.put("hello"); - /// - /// assert!(!buf.has_remaining_mut()); - /// ``` - fn has_remaining_mut(&self) -> bool { - self.remaining_mut() > 0 - } - - /// Returns a mutable slice starting at the current BufMut position and of - /// length between 0 and `BufMut::remaining_mut()`. Note that this *can* be shorter than the - /// whole remainder of the buffer (this allows non-continuous implementation). - /// - /// This is a lower level function. Most operations are done with other - /// functions. - /// - /// The returned byte slice may represent uninitialized memory. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = Vec::with_capacity(16); - /// - /// unsafe { - /// buf.bytes_mut()[0] = b'h'; - /// buf.bytes_mut()[1] = b'e'; - /// - /// buf.advance_mut(2); - /// - /// buf.bytes_mut()[0] = b'l'; - /// buf.bytes_mut()[1..3].copy_from_slice(b"lo"); - /// - /// buf.advance_mut(3); - /// } - /// - /// assert_eq!(5, buf.len()); - /// assert_eq!(buf, b"hello"); - /// ``` - /// - /// # Implementer notes - /// - /// This function should never panic. `bytes_mut` should return an empty - /// slice **if and only if** `remaining_mut` returns 0. In other words, - /// `bytes_mut` returning an empty slice implies that `remaining_mut` will - /// return 0 and `remaining_mut` returning 0 implies that `bytes_mut` will - /// return an empty slice. - unsafe fn bytes_mut(&mut self) -> &mut [u8]; - - /// Fills `dst` with potentially multiple mutable slices starting at `self`'s - /// current position. - /// - /// If the `BufMut` is backed by disjoint slices of bytes, `bytes_vec_mut` - /// enables fetching more than one slice at once. `dst` is a slice of - /// mutable `IoVec` references, enabling the slice to be directly used with - /// [`readv`] without any further conversion. The sum of the lengths of all - /// the buffers in `dst` will be less than or equal to - /// `Buf::remaining_mut()`. - /// - /// The entries in `dst` will be overwritten, but the data **contained** by - /// the slices **will not** be modified. If `bytes_vec_mut` does not fill every - /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices - /// in `self. - /// - /// This is a lower level function. Most operations are done with other - /// functions. - /// - /// # Implementer notes - /// - /// This function should never panic. Once the end of the buffer is reached, - /// i.e., `BufMut::remaining_mut` returns 0, calls to `bytes_vec_mut` must - /// return 0 without mutating `dst`. - /// - /// Implementations should also take care to properly handle being called - /// with `dst` being a zero length slice. - /// - /// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html - unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize { - if dst.is_empty() { - return 0; - } - - if self.has_remaining_mut() { - dst[0] = self.bytes_mut().into(); - 1 - } else { - 0 - } - } - - /// Transfer bytes into `self` from `src` and advance the cursor by the - /// number of bytes written. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// - /// buf.put(b'h'); - /// buf.put(&b"ello"[..]); - /// buf.put(" world"); - /// - /// assert_eq!(buf, b"hello world"); - /// ``` - /// - /// # Panics - /// - /// Panics if `self` does not have enough capacity to contain `src`. - fn put(&mut self, src: T) where Self: Sized { - use super::Buf; - - let mut src = src.into_buf(); - - assert!(self.remaining_mut() >= src.remaining()); - - while src.has_remaining() { - let l; - - unsafe { - let s = src.bytes(); - let d = self.bytes_mut(); - l = cmp::min(s.len(), d.len()); - - ptr::copy_nonoverlapping( - s.as_ptr(), - d.as_mut_ptr(), - l); - } - - src.advance(l); - unsafe { self.advance_mut(l); } - } - } - - /// Transfer bytes into `self` from `src` and advance the cursor by the - /// number of bytes written. - /// - /// `self` must have enough remaining capacity to contain all of `src`. - /// - /// ``` - /// use bytes::BufMut; - /// use std::io::Cursor; - /// - /// let mut dst = [0; 6]; - /// - /// { - /// let mut buf = Cursor::new(&mut dst); - /// buf.put_slice(b"hello"); - /// - /// assert_eq!(1, buf.remaining_mut()); - /// } - /// - /// assert_eq!(b"hello\0", &dst); - /// ``` - fn put_slice(&mut self, src: &[u8]) { - let mut off = 0; - - assert!(self.remaining_mut() >= src.len(), "buffer overflow"); - - while off < src.len() { - let cnt; - - unsafe { - let dst = self.bytes_mut(); - cnt = cmp::min(dst.len(), src.len() - off); - - ptr::copy_nonoverlapping( - src[off..].as_ptr(), - dst.as_mut_ptr(), - cnt); - - off += cnt; - - } - - unsafe { self.advance_mut(cnt); } - } - } - - /// Writes an unsigned 8 bit integer to `self`. - /// - /// The current position is advanced by 1. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u8(0x01); - /// assert_eq!(buf, b"\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u8(&mut self, n: u8) { - let src = [n]; - self.put_slice(&src); - } - - /// Writes a signed 8 bit integer to `self`. - /// - /// The current position is advanced by 1. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i8(0x01); - /// assert_eq!(buf, b"\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i8(&mut self, n: i8) { - let src = [n as u8]; - self.put_slice(&src) - } - - #[doc(hidden)] - #[deprecated(note="use put_u16_be or put_u16_le")] - fn put_u16(&mut self, n: u16) where Self: Sized { - let mut buf = [0; 2]; - T::write_u16(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 16 bit integer to `self` in big-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u16_be(0x0809); - /// assert_eq!(buf, b"\x08\x09"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u16_be(&mut self, n: u16) { - let mut buf = [0; 2]; - BigEndian::write_u16(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 16 bit integer to `self` in little-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u16_le(0x0809); - /// assert_eq!(buf, b"\x09\x08"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u16_le(&mut self, n: u16) { - let mut buf = [0; 2]; - LittleEndian::write_u16(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_i16_be or put_i16_le")] - fn put_i16(&mut self, n: i16) where Self: Sized { - let mut buf = [0; 2]; - T::write_i16(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 16 bit integer to `self` in big-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i16_be(0x0809); - /// assert_eq!(buf, b"\x08\x09"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i16_be(&mut self, n: i16) { - let mut buf = [0; 2]; - BigEndian::write_i16(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 16 bit integer to `self` in little-endian byte order. - /// - /// The current position is advanced by 2. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i16_le(0x0809); - /// assert_eq!(buf, b"\x09\x08"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i16_le(&mut self, n: i16) { - let mut buf = [0; 2]; - LittleEndian::write_i16(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_u32_be or put_u32_le")] - fn put_u32(&mut self, n: u32) where Self: Sized { - let mut buf = [0; 4]; - T::write_u32(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 32 bit integer to `self` in big-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u32_be(0x0809A0A1); - /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u32_be(&mut self, n: u32) { - let mut buf = [0; 4]; - BigEndian::write_u32(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 32 bit integer to `self` in little-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u32_le(0x0809A0A1); - /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u32_le(&mut self, n: u32) { - let mut buf = [0; 4]; - LittleEndian::write_u32(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_i32_be or put_i32_le")] - fn put_i32(&mut self, n: i32) where Self: Sized { - let mut buf = [0; 4]; - T::write_i32(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 32 bit integer to `self` in big-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i32_be(0x0809A0A1); - /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i32_be(&mut self, n: i32) { - let mut buf = [0; 4]; - BigEndian::write_i32(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 32 bit integer to `self` in little-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i32_le(0x0809A0A1); - /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i32_le(&mut self, n: i32) { - let mut buf = [0; 4]; - LittleEndian::write_i32(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_u64_be or put_u64_le")] - fn put_u64(&mut self, n: u64) where Self: Sized { - let mut buf = [0; 8]; - T::write_u64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 64 bit integer to `self` in the big-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u64_be(0x0102030405060708); - /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u64_be(&mut self, n: u64) { - let mut buf = [0; 8]; - BigEndian::write_u64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 64 bit integer to `self` in little-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u64_le(0x0102030405060708); - /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_u64_le(&mut self, n: u64) { - let mut buf = [0; 8]; - LittleEndian::write_u64(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_i64_be or put_i64_le")] - fn put_i64(&mut self, n: i64) where Self: Sized { - let mut buf = [0; 8]; - T::write_i64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 64 bit integer to `self` in the big-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i64_be(0x0102030405060708); - /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i64_be(&mut self, n: i64) { - let mut buf = [0; 8]; - BigEndian::write_i64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 64 bit integer to `self` in little-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i64_le(0x0102030405060708); - /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_i64_le(&mut self, n: i64) { - let mut buf = [0; 8]; - LittleEndian::write_i64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 128 bit integer to `self` in the big-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u128_be(0x01020304050607080910111213141516); - /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - #[cfg(feature = "i128")] - fn put_u128_be(&mut self, n: u128) { - let mut buf = [0; 16]; - BigEndian::write_u128(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an unsigned 128 bit integer to `self` in little-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_u128_le(0x01020304050607080910111213141516); - /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - #[cfg(feature = "i128")] - fn put_u128_le(&mut self, n: u128) { - let mut buf = [0; 16]; - LittleEndian::write_u128(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 128 bit integer to `self` in the big-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i128_be(0x01020304050607080910111213141516); - /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - #[cfg(feature = "i128")] - fn put_i128_be(&mut self, n: i128) { - let mut buf = [0; 16]; - BigEndian::write_i128(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes a signed 128 bit integer to `self` in little-endian byte order. - /// - /// **NOTE:** This method requires the `i128` feature. - /// The current position is advanced by 16. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_i128_le(0x01020304050607080910111213141516); - /// assert_eq!(buf, b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - #[cfg(feature = "i128")] - fn put_i128_le(&mut self, n: i128) { - let mut buf = [0; 16]; - LittleEndian::write_i128(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_uint_be or put_uint_le")] - fn put_uint(&mut self, n: u64, nbytes: usize) where Self: Sized { - let mut buf = [0; 8]; - T::write_uint(&mut buf, n, nbytes); - self.put_slice(&buf[0..nbytes]) - } - - /// Writes an unsigned n-byte integer to `self` in big-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_uint_be(0x010203, 3); - /// assert_eq!(buf, b"\x01\x02\x03"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_uint_be(&mut self, n: u64, nbytes: usize) { - let mut buf = [0; 8]; - BigEndian::write_uint(&mut buf, n, nbytes); - self.put_slice(&buf[0..nbytes]) - } - - /// Writes an unsigned n-byte integer to `self` in the little-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_uint_le(0x010203, 3); - /// assert_eq!(buf, b"\x03\x02\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_uint_le(&mut self, n: u64, nbytes: usize) { - let mut buf = [0; 8]; - LittleEndian::write_uint(&mut buf, n, nbytes); - self.put_slice(&buf[0..nbytes]) - } - - #[doc(hidden)] - #[deprecated(note="use put_int_be or put_int_le")] - fn put_int(&mut self, n: i64, nbytes: usize) where Self: Sized { - let mut buf = [0; 8]; - T::write_int(&mut buf, n, nbytes); - self.put_slice(&buf[0..nbytes]) - } - - /// Writes a signed n-byte integer to `self` in big-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_int_be(0x010203, 3); - /// assert_eq!(buf, b"\x01\x02\x03"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_int_be(&mut self, n: i64, nbytes: usize) { - let mut buf = [0; 8]; - BigEndian::write_int(&mut buf, n, nbytes); - self.put_slice(&buf[0..nbytes]) - } - - /// Writes a signed n-byte integer to `self` in little-endian byte order. - /// - /// The current position is advanced by `nbytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_int_le(0x010203, 3); - /// assert_eq!(buf, b"\x03\x02\x01"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_int_le(&mut self, n: i64, nbytes: usize) { - let mut buf = [0; 8]; - LittleEndian::write_int(&mut buf, n, nbytes); - self.put_slice(&buf[0..nbytes]) - } - - #[doc(hidden)] - #[deprecated(note="use put_f32_be or put_f32_le")] - fn put_f32(&mut self, n: f32) where Self: Sized { - let mut buf = [0; 4]; - T::write_f32(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an IEEE754 single-precision (4 bytes) floating point number to - /// `self` in big-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_f32_be(1.2f32); - /// assert_eq!(buf, b"\x3F\x99\x99\x9A"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_f32_be(&mut self, n: f32) { - let mut buf = [0; 4]; - BigEndian::write_f32(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an IEEE754 single-precision (4 bytes) floating point number to - /// `self` in little-endian byte order. - /// - /// The current position is advanced by 4. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_f32_le(1.2f32); - /// assert_eq!(buf, b"\x9A\x99\x99\x3F"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_f32_le(&mut self, n: f32) { - let mut buf = [0; 4]; - LittleEndian::write_f32(&mut buf, n); - self.put_slice(&buf) - } - - #[doc(hidden)] - #[deprecated(note="use put_f64_be or put_f64_le")] - fn put_f64(&mut self, n: f64) where Self: Sized { - let mut buf = [0; 8]; - T::write_f64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an IEEE754 double-precision (8 bytes) floating point number to - /// `self` in big-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_f64_be(1.2f64); - /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_f64_be(&mut self, n: f64) { - let mut buf = [0; 8]; - BigEndian::write_f64(&mut buf, n); - self.put_slice(&buf) - } - - /// Writes an IEEE754 double-precision (8 bytes) floating point number to - /// `self` in little-endian byte order. - /// - /// The current position is advanced by 8. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// - /// let mut buf = vec![]; - /// buf.put_f64_le(1.2f64); - /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F"); - /// ``` - /// - /// # Panics - /// - /// This function panics if there is not enough remaining capacity in - /// `self`. - fn put_f64_le(&mut self, n: f64) { - let mut buf = [0; 8]; - LittleEndian::write_f64(&mut buf, n); - self.put_slice(&buf) - } - - /// Creates a "by reference" adaptor for this instance of `BufMut`. - /// - /// The returned adapter also implements `BufMut` and will simply borrow - /// `self`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// use std::io; - /// - /// let mut buf = vec![]; - /// - /// { - /// let mut reference = buf.by_ref(); - /// - /// // Adapt reference to `std::io::Write`. - /// let mut writer = reference.writer(); - /// - /// // Use the buffer as a writter - /// io::Write::write(&mut writer, &b"hello world"[..]).unwrap(); - /// } // drop our &mut reference so that we can use `buf` again - /// - /// assert_eq!(buf, &b"hello world"[..]); - /// ``` - fn by_ref(&mut self) -> &mut Self where Self: Sized { - self - } - - /// Creates an adaptor which implements the `Write` trait for `self`. - /// - /// This function returns a new value which implements `Write` by adapting - /// the `Write` trait functions to the `BufMut` trait functions. Given that - /// `BufMut` operations are infallible, none of the `Write` functions will - /// return with `Err`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BufMut; - /// use std::io::Write; - /// - /// let mut buf = vec![].writer(); - /// - /// let num = buf.write(&b"hello world"[..]).unwrap(); - /// assert_eq!(11, num); - /// - /// let buf = buf.into_inner(); - /// - /// assert_eq!(*buf, b"hello world"[..]); - /// ``` - fn writer(self) -> Writer where Self: Sized { - super::writer::new(self) - } -} - -impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T { - fn remaining_mut(&self) -> usize { - (**self).remaining_mut() - } - - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - (**self).bytes_mut() - } - - unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize { - (**self).bytes_vec_mut(dst) - } - - unsafe fn advance_mut(&mut self, cnt: usize) { - (**self).advance_mut(cnt) - } -} - -impl BufMut for Box { - fn remaining_mut(&self) -> usize { - (**self).remaining_mut() - } - - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - (**self).bytes_mut() - } - - unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize { - (**self).bytes_vec_mut(dst) - } - - unsafe fn advance_mut(&mut self, cnt: usize) { - (**self).advance_mut(cnt) - } -} - -impl + AsRef<[u8]>> BufMut for io::Cursor { - fn remaining_mut(&self) -> usize { - use Buf; - self.remaining() - } - - /// Advance the internal cursor of the BufMut - unsafe fn advance_mut(&mut self, cnt: usize) { - use Buf; - self.advance(cnt); - } - - /// Returns a mutable slice starting at the current BufMut position and of - /// length between 0 and `BufMut::remaining()`. - /// - /// The returned byte slice may represent uninitialized memory. - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - let len = self.get_ref().as_ref().len(); - let pos = self.position() as usize; - - if pos >= len { - return Default::default(); - } - - &mut (self.get_mut().as_mut())[pos..] - } -} - -impl BufMut for Vec { - #[inline] - fn remaining_mut(&self) -> usize { - usize::MAX - self.len() - } - - #[inline] - unsafe fn advance_mut(&mut self, cnt: usize) { - let len = self.len(); - let remaining = self.capacity() - len; - if cnt > remaining { - // Reserve additional capacity, and ensure that the total length - // will not overflow usize. - self.reserve(cnt); - } - - self.set_len(len + cnt); - } - - #[inline] - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - use std::slice; - - if self.capacity() == self.len() { - self.reserve(64); // Grow the vec - } - - let cap = self.capacity(); - let len = self.len(); - - let ptr = self.as_mut_ptr(); - &mut slice::from_raw_parts_mut(ptr, cap)[len..] - } -} - -// The existance of this function makes the compiler catch if the BufMut -// trait is "object-safe" or not. -fn _assert_trait_object(_b: &BufMut) {} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/chain.rs b/third_party/rust/bytes/v0_4/crate/src/buf/chain.rs deleted file mode 100644 index 7dd44ab0219a..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/chain.rs +++ /dev/null @@ -1,226 +0,0 @@ -use {Buf, BufMut}; -use iovec::IoVec; - -/// A `Chain` sequences two buffers. -/// -/// `Chain` is an adapter that links two underlying buffers and provides a -/// continous view across both buffers. It is able to sequence either immutable -/// buffers ([`Buf`] values) or mutable buffers ([`BufMut`] values). -/// -/// This struct is generally created by calling [`Buf::chain`]. Please see that -/// function's documentation for more detail. -/// -/// # Examples -/// -/// ``` -/// use bytes::{Bytes, Buf, IntoBuf}; -/// use bytes::buf::Chain; -/// -/// let buf = Bytes::from(&b"hello "[..]).into_buf() -/// .chain(Bytes::from(&b"world"[..])); -/// -/// let full: Bytes = buf.collect(); -/// assert_eq!(full[..], b"hello world"[..]); -/// ``` -/// -/// [`Buf::chain`]: trait.Buf.html#method.chain -/// [`Buf`]: trait.Buf.html -/// [`BufMut`]: trait.BufMut.html -#[derive(Debug)] -pub struct Chain { - a: T, - b: U, -} - -impl Chain { - /// Creates a new `Chain` sequencing the provided values. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// use bytes::buf::Chain; - /// - /// let buf = Chain::new( - /// BytesMut::with_capacity(1024), - /// BytesMut::with_capacity(1024)); - /// - /// // Use the chained buffer - /// ``` - pub fn new(a: T, b: U) -> Chain { - Chain { - a: a, - b: b, - } - } - - /// Gets a reference to the first underlying `Buf`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, Buf, IntoBuf}; - /// - /// let buf = Bytes::from(&b"hello"[..]).into_buf() - /// .chain(Bytes::from(&b"world"[..])); - /// - /// assert_eq!(buf.first_ref().get_ref()[..], b"hello"[..]); - /// ``` - pub fn first_ref(&self) -> &T { - &self.a - } - - /// Gets a mutable reference to the first underlying `Buf`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, Buf, IntoBuf}; - /// - /// let mut buf = Bytes::from(&b"hello "[..]).into_buf() - /// .chain(Bytes::from(&b"world"[..])); - /// - /// buf.first_mut().set_position(1); - /// - /// let full: Bytes = buf.collect(); - /// assert_eq!(full[..], b"ello world"[..]); - /// ``` - pub fn first_mut(&mut self) -> &mut T { - &mut self.a - } - - /// Gets a reference to the last underlying `Buf`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, Buf, IntoBuf}; - /// - /// let buf = Bytes::from(&b"hello"[..]).into_buf() - /// .chain(Bytes::from(&b"world"[..])); - /// - /// assert_eq!(buf.last_ref().get_ref()[..], b"world"[..]); - /// ``` - pub fn last_ref(&self) -> &U { - &self.b - } - - /// Gets a mutable reference to the last underlying `Buf`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, Buf, IntoBuf}; - /// - /// let mut buf = Bytes::from(&b"hello "[..]).into_buf() - /// .chain(Bytes::from(&b"world"[..])); - /// - /// buf.last_mut().set_position(1); - /// - /// let full: Bytes = buf.collect(); - /// assert_eq!(full[..], b"hello orld"[..]); - /// ``` - pub fn last_mut(&mut self) -> &mut U { - &mut self.b - } - - /// Consumes this `Chain`, returning the underlying values. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, Buf, IntoBuf}; - /// - /// let buf = Bytes::from(&b"hello"[..]).into_buf() - /// .chain(Bytes::from(&b"world"[..])); - /// - /// let (first, last) = buf.into_inner(); - /// assert_eq!(first.get_ref()[..], b"hello"[..]); - /// assert_eq!(last.get_ref()[..], b"world"[..]); - /// ``` - pub fn into_inner(self) -> (T, U) { - (self.a, self.b) - } -} - -impl Buf for Chain - where T: Buf, - U: Buf, -{ - fn remaining(&self) -> usize { - self.a.remaining() + self.b.remaining() - } - - fn bytes(&self) -> &[u8] { - if self.a.has_remaining() { - self.a.bytes() - } else { - self.b.bytes() - } - } - - fn advance(&mut self, mut cnt: usize) { - let a_rem = self.a.remaining(); - - if a_rem != 0 { - if a_rem >= cnt { - self.a.advance(cnt); - return; - } - - // Consume what is left of a - self.a.advance(a_rem); - - cnt -= a_rem; - } - - self.b.advance(cnt); - } - - fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize { - let mut n = self.a.bytes_vec(dst); - n += self.b.bytes_vec(&mut dst[n..]); - n - } -} - -impl BufMut for Chain - where T: BufMut, - U: BufMut, -{ - fn remaining_mut(&self) -> usize { - self.a.remaining_mut() + self.b.remaining_mut() - } - - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - if self.a.has_remaining_mut() { - self.a.bytes_mut() - } else { - self.b.bytes_mut() - } - } - - unsafe fn advance_mut(&mut self, mut cnt: usize) { - let a_rem = self.a.remaining_mut(); - - if a_rem != 0 { - if a_rem >= cnt { - self.a.advance_mut(cnt); - return; - } - - // Consume what is left of a - self.a.advance_mut(a_rem); - - cnt -= a_rem; - } - - self.b.advance_mut(cnt); - } - - unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize { - let mut n = self.a.bytes_vec_mut(dst); - n += self.b.bytes_vec_mut(&mut dst[n..]); - n - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/from_buf.rs b/third_party/rust/bytes/v0_4/crate/src/buf/from_buf.rs deleted file mode 100644 index 55f5cef31b90..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/from_buf.rs +++ /dev/null @@ -1,117 +0,0 @@ -use {Buf, BufMut, IntoBuf, Bytes, BytesMut}; - -/// Conversion from a [`Buf`] -/// -/// Implementing `FromBuf` for a type defines how it is created from a buffer. -/// This is common for types which represent byte storage of some kind. -/// -/// [`FromBuf::from_buf`] is rarely called explicitly, and it is instead used -/// through [`Buf::collect`]. See [`Buf::collect`] documentation for more examples. -/// -/// See also [`IntoBuf`]. -/// -/// # Examples -/// -/// Basic usage: -/// -/// ``` -/// use bytes::{Bytes, IntoBuf}; -/// use bytes::buf::FromBuf; -/// -/// let buf = Bytes::from(&b"hello world"[..]).into_buf(); -/// let vec = Vec::from_buf(buf); -/// -/// assert_eq!(vec, &b"hello world"[..]); -/// ``` -/// -/// Using [`Buf::collect`] to implicitly use `FromBuf`: -/// -/// ``` -/// use bytes::{Buf, Bytes, IntoBuf}; -/// -/// let buf = Bytes::from(&b"hello world"[..]).into_buf(); -/// let vec: Vec = buf.collect(); -/// -/// assert_eq!(vec, &b"hello world"[..]); -/// ``` -/// -/// Implementing `FromBuf` for your type: -/// -/// ``` -/// use bytes::{BufMut, Bytes}; -/// use bytes::buf::{IntoBuf, FromBuf}; -/// -/// // A sample buffer, that's just a wrapper over Vec -/// struct MyBuffer(Vec); -/// -/// impl FromBuf for MyBuffer { -/// fn from_buf(buf: B) -> Self where B: IntoBuf { -/// let mut v = Vec::new(); -/// v.put(buf.into_buf()); -/// MyBuffer(v) -/// } -/// } -/// -/// // Now we can make a new buf -/// let buf = Bytes::from(&b"hello world"[..]); -/// -/// // And make a MyBuffer out of it -/// let my_buf = MyBuffer::from_buf(buf); -/// -/// assert_eq!(my_buf.0, &b"hello world"[..]); -/// ``` -/// -/// [`Buf`]: trait.Buf.html -/// [`FromBuf::from_buf`]: #method.from_buf -/// [`Buf::collect`]: trait.Buf.html#method.collect -/// [`IntoBuf`]: trait.IntoBuf.html -pub trait FromBuf { - /// Creates a value from a buffer. - /// - /// See the [type-level documentation](#) for more details. - /// - /// # Examples - /// - /// Basic usage: - /// - /// ``` - /// use bytes::{Bytes, IntoBuf}; - /// use bytes::buf::FromBuf; - /// - /// let buf = Bytes::from(&b"hello world"[..]).into_buf(); - /// let vec = Vec::from_buf(buf); - /// - /// assert_eq!(vec, &b"hello world"[..]); - /// ``` - fn from_buf(buf: T) -> Self where T: IntoBuf; -} - -impl FromBuf for Vec { - fn from_buf(buf: T) -> Self - where T: IntoBuf - { - let buf = buf.into_buf(); - let mut ret = Vec::with_capacity(buf.remaining()); - ret.put(buf); - ret - } -} - -impl FromBuf for Bytes { - fn from_buf(buf: T) -> Self - where T: IntoBuf - { - BytesMut::from_buf(buf).freeze() - } -} - -impl FromBuf for BytesMut { - fn from_buf(buf: T) -> Self - where T: IntoBuf - { - let buf = buf.into_buf(); - let mut ret = BytesMut::with_capacity(buf.remaining()); - ret.put(buf); - ret - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/into_buf.rs b/third_party/rust/bytes/v0_4/crate/src/buf/into_buf.rs deleted file mode 100644 index 4c3b4207289f..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/into_buf.rs +++ /dev/null @@ -1,146 +0,0 @@ -use super::{Buf}; - -use std::io; - -/// Conversion into a `Buf` -/// -/// An `IntoBuf` implementation defines how to convert a value into a `Buf`. -/// This is common for types that represent byte storage of some kind. `IntoBuf` -/// may be implemented directly for types or on references for those types. -/// -/// # Examples -/// -/// ``` -/// use bytes::{Buf, IntoBuf, BigEndian}; -/// -/// let bytes = b"\x00\x01hello world"; -/// let mut buf = bytes.into_buf(); -/// -/// assert_eq!(1, buf.get_u16::()); -/// -/// let mut rest = [0; 11]; -/// buf.copy_to_slice(&mut rest); -/// -/// assert_eq!(b"hello world", &rest); -/// ``` -pub trait IntoBuf { - /// The `Buf` type that `self` is being converted into - type Buf: Buf; - - /// Creates a `Buf` from a value. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, IntoBuf, BigEndian}; - /// - /// let bytes = b"\x00\x01hello world"; - /// let mut buf = bytes.into_buf(); - /// - /// assert_eq!(1, buf.get_u16::()); - /// - /// let mut rest = [0; 11]; - /// buf.copy_to_slice(&mut rest); - /// - /// assert_eq!(b"hello world", &rest); - /// ``` - fn into_buf(self) -> Self::Buf; -} - -impl IntoBuf for T { - type Buf = Self; - - fn into_buf(self) -> Self { - self - } -} - -impl<'a> IntoBuf for &'a [u8] { - type Buf = io::Cursor<&'a [u8]>; - - fn into_buf(self) -> Self::Buf { - io::Cursor::new(self) - } -} - -impl<'a> IntoBuf for &'a mut [u8] { - type Buf = io::Cursor<&'a mut [u8]>; - - fn into_buf(self) -> Self::Buf { - io::Cursor::new(self) - } -} - -impl<'a> IntoBuf for &'a str { - type Buf = io::Cursor<&'a [u8]>; - - fn into_buf(self) -> Self::Buf { - self.as_bytes().into_buf() - } -} - -impl IntoBuf for Vec { - type Buf = io::Cursor>; - - fn into_buf(self) -> Self::Buf { - io::Cursor::new(self) - } -} - -impl<'a> IntoBuf for &'a Vec { - type Buf = io::Cursor<&'a [u8]>; - - fn into_buf(self) -> Self::Buf { - io::Cursor::new(&self[..]) - } -} - -// Kind of annoying... but this impl is required to allow passing `&'static -// [u8]` where for<'a> &'a T: IntoBuf is required. -impl<'a> IntoBuf for &'a &'static [u8] { - type Buf = io::Cursor<&'static [u8]>; - - fn into_buf(self) -> Self::Buf { - io::Cursor::new(self) - } -} - -impl<'a> IntoBuf for &'a &'static str { - type Buf = io::Cursor<&'static [u8]>; - - fn into_buf(self) -> Self::Buf { - self.as_bytes().into_buf() - } -} - -impl IntoBuf for String { - type Buf = io::Cursor>; - - fn into_buf(self) -> Self::Buf { - self.into_bytes().into_buf() - } -} - -impl<'a> IntoBuf for &'a String { - type Buf = io::Cursor<&'a [u8]>; - - fn into_buf(self) -> Self::Buf { - self.as_bytes().into_buf() - } -} - -impl IntoBuf for u8 { - type Buf = Option<[u8; 1]>; - - fn into_buf(self) -> Self::Buf { - Some([self]) - } -} - -impl IntoBuf for i8 { - type Buf = Option<[u8; 1]>; - - fn into_buf(self) -> Self::Buf { - Some([self as u8; 1]) - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/iter.rs b/third_party/rust/bytes/v0_4/crate/src/buf/iter.rs deleted file mode 100644 index 9345c05b63d4..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/iter.rs +++ /dev/null @@ -1,116 +0,0 @@ -use Buf; - -/// Iterator over the bytes contained by the buffer. -/// -/// This struct is created by the [`iter`] method on [`Buf`]. -/// -/// # Examples -/// -/// Basic usage: -/// -/// ``` -/// use bytes::{Buf, IntoBuf, Bytes}; -/// -/// let buf = Bytes::from(&b"abc"[..]).into_buf(); -/// let mut iter = buf.iter(); -/// -/// assert_eq!(iter.next(), Some(b'a')); -/// assert_eq!(iter.next(), Some(b'b')); -/// assert_eq!(iter.next(), Some(b'c')); -/// assert_eq!(iter.next(), None); -/// ``` -/// -/// [`iter`]: trait.Buf.html#method.iter -/// [`Buf`]: trait.Buf.html -#[derive(Debug)] -pub struct Iter { - inner: T, -} - -impl Iter { - /// Consumes this `Iter`, returning the underlying value. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, IntoBuf, Bytes}; - /// - /// let buf = Bytes::from(&b"abc"[..]).into_buf(); - /// let mut iter = buf.iter(); - /// - /// assert_eq!(iter.next(), Some(b'a')); - /// - /// let buf = iter.into_inner(); - /// assert_eq!(2, buf.remaining()); - /// ``` - pub fn into_inner(self) -> T { - self.inner - } - - /// Gets a reference to the underlying `Buf`. - /// - /// It is inadvisable to directly read from the underlying `Buf`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, IntoBuf, Bytes}; - /// - /// let buf = Bytes::from(&b"abc"[..]).into_buf(); - /// let mut iter = buf.iter(); - /// - /// assert_eq!(iter.next(), Some(b'a')); - /// - /// assert_eq!(2, iter.get_ref().remaining()); - /// ``` - pub fn get_ref(&self) -> &T { - &self.inner - } - - /// Gets a mutable reference to the underlying `Buf`. - /// - /// It is inadvisable to directly read from the underlying `Buf`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, IntoBuf, BytesMut}; - /// - /// let buf = BytesMut::from(&b"abc"[..]).into_buf(); - /// let mut iter = buf.iter(); - /// - /// assert_eq!(iter.next(), Some(b'a')); - /// - /// iter.get_mut().set_position(0); - /// - /// assert_eq!(iter.next(), Some(b'a')); - /// ``` - pub fn get_mut(&mut self) -> &mut T { - &mut self.inner - } -} - -pub fn new(inner: T) -> Iter { - Iter { inner: inner } -} - -impl Iterator for Iter { - type Item = u8; - - fn next(&mut self) -> Option { - if !self.inner.has_remaining() { - return None; - } - - let b = self.inner.bytes()[0]; - self.inner.advance(1); - Some(b) - } - - fn size_hint(&self) -> (usize, Option) { - let rem = self.inner.remaining(); - (rem, Some(rem)) - } -} - -impl ExactSizeIterator for Iter { } diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/mod.rs b/third_party/rust/bytes/v0_4/crate/src/buf/mod.rs deleted file mode 100644 index 35b4857ecb35..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/mod.rs +++ /dev/null @@ -1,38 +0,0 @@ -//! Utilities for working with buffers. -//! -//! A buffer is any structure that contains a sequence of bytes. The bytes may -//! or may not be stored in contiguous memory. This module contains traits used -//! to abstract over buffers as well as utilities for working with buffer types. -//! -//! # `Buf`, `BufMut` -//! -//! These are the two foundational traits for abstractly working with buffers. -//! They can be thought as iterators for byte structures. They offer additional -//! performance over `Iterator` by providing an API optimized for byte slices. -//! -//! See [`Buf`] and [`BufMut`] for more details. -//! -//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure) -//! [`Buf`]: trait.Buf.html -//! [`BufMut`]: trait.BufMut.html - -mod buf; -mod buf_mut; -mod from_buf; -mod chain; -mod into_buf; -mod iter; -mod reader; -mod take; -mod vec_deque; -mod writer; - -pub use self::buf::Buf; -pub use self::buf_mut::BufMut; -pub use self::from_buf::FromBuf; -pub use self::chain::Chain; -pub use self::into_buf::IntoBuf; -pub use self::iter::Iter; -pub use self::reader::Reader; -pub use self::take::Take; -pub use self::writer::Writer; diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/reader.rs b/third_party/rust/bytes/v0_4/crate/src/buf/reader.rs deleted file mode 100644 index f1154dacebb7..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/reader.rs +++ /dev/null @@ -1,97 +0,0 @@ -use {Buf}; - -use std::{cmp, io}; - -/// A `Buf` adapter which implements `io::Read` for the inner value. -/// -/// This struct is generally created by calling `reader()` on `Buf`. See -/// documentation of [`reader()`](trait.Buf.html#method.reader) for more -/// details. -#[derive(Debug)] -pub struct Reader { - buf: B, -} - -pub fn new(buf: B) -> Reader { - Reader { buf: buf } -} - -impl Reader { - /// Gets a reference to the underlying `Buf`. - /// - /// It is inadvisable to directly read from the underlying `Buf`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::Buf; - /// use std::io::{self, Cursor}; - /// - /// let mut buf = Cursor::new(b"hello world").reader(); - /// - /// assert_eq!(0, buf.get_ref().position()); - /// ``` - pub fn get_ref(&self) -> &B { - &self.buf - } - - /// Gets a mutable reference to the underlying `Buf`. - /// - /// It is inadvisable to directly read from the underlying `Buf`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::Buf; - /// use std::io::{self, Cursor}; - /// - /// let mut buf = Cursor::new(b"hello world").reader(); - /// let mut dst = vec![]; - /// - /// buf.get_mut().set_position(2); - /// io::copy(&mut buf, &mut dst).unwrap(); - /// - /// assert_eq!(*dst, b"llo world"[..]); - /// ``` - pub fn get_mut(&mut self) -> &mut B { - &mut self.buf - } - - /// Consumes this `Reader`, returning the underlying value. - /// - /// # Examples - /// - /// ```rust - /// use bytes::Buf; - /// use std::io::{self, Cursor}; - /// - /// let mut buf = Cursor::new(b"hello world").reader(); - /// let mut dst = vec![]; - /// - /// io::copy(&mut buf, &mut dst).unwrap(); - /// - /// let buf = buf.into_inner(); - /// assert_eq!(0, buf.remaining()); - /// ``` - pub fn into_inner(self) -> B { - self.buf - } -} - -impl io::Read for Reader { - fn read(&mut self, dst: &mut [u8]) -> io::Result { - let len = cmp::min(self.buf.remaining(), dst.len()); - - Buf::copy_to_slice(&mut self.buf, &mut dst[0..len]); - Ok(len) - } -} - -impl io::BufRead for Reader { - fn fill_buf(&mut self) -> io::Result<&[u8]> { - Ok(self.buf.bytes()) - } - fn consume(&mut self, amt: usize) { - self.buf.advance(amt) - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/take.rs b/third_party/rust/bytes/v0_4/crate/src/buf/take.rs deleted file mode 100644 index a0c8ed479e17..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/take.rs +++ /dev/null @@ -1,155 +0,0 @@ -use {Buf}; - -use std::cmp; - -/// A `Buf` adapter which limits the bytes read from an underlying buffer. -/// -/// This struct is generally created by calling `take()` on `Buf`. See -/// documentation of [`take()`](trait.Buf.html#method.take) for more details. -#[derive(Debug)] -pub struct Take { - inner: T, - limit: usize, -} - -pub fn new(inner: T, limit: usize) -> Take { - Take { - inner: inner, - limit: limit, - } -} - -impl Take { - /// Consumes this `Take`, returning the underlying value. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, BufMut}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world").take(2); - /// let mut dst = vec![]; - /// - /// dst.put(&mut buf); - /// assert_eq!(*dst, b"he"[..]); - /// - /// let mut buf = buf.into_inner(); - /// - /// dst.clear(); - /// dst.put(&mut buf); - /// assert_eq!(*dst, b"llo world"[..]); - /// ``` - pub fn into_inner(self) -> T { - self.inner - } - - /// Gets a reference to the underlying `Buf`. - /// - /// It is inadvisable to directly read from the underlying `Buf`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, BufMut}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world").take(2); - /// - /// assert_eq!(0, buf.get_ref().position()); - /// ``` - pub fn get_ref(&self) -> &T { - &self.inner - } - - /// Gets a mutable reference to the underlying `Buf`. - /// - /// It is inadvisable to directly read from the underlying `Buf`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, BufMut}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world").take(2); - /// let mut dst = vec![]; - /// - /// buf.get_mut().set_position(2); - /// - /// dst.put(&mut buf); - /// assert_eq!(*dst, b"ll"[..]); - /// ``` - pub fn get_mut(&mut self) -> &mut T { - &mut self.inner - } - - /// Returns the maximum number of bytes that can be read. - /// - /// # Note - /// - /// If the inner `Buf` has fewer bytes than indicated by this method then - /// that is the actual number of available bytes. - /// - /// # Examples - /// - /// ```rust - /// use bytes::Buf; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world").take(2); - /// - /// assert_eq!(2, buf.limit()); - /// assert_eq!(b'h', buf.get_u8()); - /// assert_eq!(1, buf.limit()); - /// ``` - pub fn limit(&self) -> usize { - self.limit - } - - /// Sets the maximum number of bytes that can be read. - /// - /// # Note - /// - /// If the inner `Buf` has fewer bytes than `lim` then that is the actual - /// number of available bytes. - /// - /// # Examples - /// - /// ```rust - /// use bytes::{Buf, BufMut}; - /// use std::io::Cursor; - /// - /// let mut buf = Cursor::new(b"hello world").take(2); - /// let mut dst = vec![]; - /// - /// dst.put(&mut buf); - /// assert_eq!(*dst, b"he"[..]); - /// - /// dst.clear(); - /// - /// buf.set_limit(3); - /// dst.put(&mut buf); - /// assert_eq!(*dst, b"llo"[..]); - /// ``` - pub fn set_limit(&mut self, lim: usize) { - self.limit = lim - } -} - -impl Buf for Take { - fn remaining(&self) -> usize { - cmp::min(self.inner.remaining(), self.limit) - } - - fn bytes(&self) -> &[u8] { - let bytes = self.inner.bytes(); - &bytes[..cmp::min(bytes.len(), self.limit)] - } - - fn advance(&mut self, cnt: usize) { - assert!(cnt <= self.limit); - self.inner.advance(cnt); - self.limit -= cnt; - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/vec_deque.rs b/third_party/rust/bytes/v0_4/crate/src/buf/vec_deque.rs deleted file mode 100644 index 1cd650f51ddc..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/vec_deque.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::collections::VecDeque; - -use super::Buf; - -impl Buf for VecDeque { - fn remaining(&self) -> usize { - self.len() - } - - fn bytes(&self) -> &[u8] { - let (s1, s2) = self.as_slices(); - if s1.is_empty() { - s2 - } else { - s1 - } - } - - fn advance(&mut self, cnt: usize) { - self.drain(..cnt); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn hello_world() { - let mut buffer: VecDeque = VecDeque::new(); - buffer.extend(b"hello world"); - assert_eq!(11, buffer.remaining()); - assert_eq!(b"hello world", buffer.bytes()); - buffer.advance(6); - assert_eq!(b"world", buffer.bytes()); - buffer.extend(b" piece"); - assert_eq!(b"world piece" as &[u8], &buffer.collect::>()[..]); - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/buf/writer.rs b/third_party/rust/bytes/v0_4/crate/src/buf/writer.rs deleted file mode 100644 index 38a739aa662d..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/buf/writer.rs +++ /dev/null @@ -1,88 +0,0 @@ -use BufMut; - -use std::{cmp, io}; - -/// A `BufMut` adapter which implements `io::Write` for the inner value. -/// -/// This struct is generally created by calling `writer()` on `BufMut`. See -/// documentation of [`writer()`](trait.BufMut.html#method.writer) for more -/// details. -#[derive(Debug)] -pub struct Writer { - buf: B, -} - -pub fn new(buf: B) -> Writer { - Writer { buf: buf } -} - -impl Writer { - /// Gets a reference to the underlying `BufMut`. - /// - /// It is inadvisable to directly write to the underlying `BufMut`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::BufMut; - /// - /// let mut buf = Vec::with_capacity(1024).writer(); - /// - /// assert_eq!(1024, buf.get_ref().capacity()); - /// ``` - pub fn get_ref(&self) -> &B { - &self.buf - } - - /// Gets a mutable reference to the underlying `BufMut`. - /// - /// It is inadvisable to directly write to the underlying `BufMut`. - /// - /// # Examples - /// - /// ```rust - /// use bytes::BufMut; - /// - /// let mut buf = vec![].writer(); - /// - /// buf.get_mut().reserve(1024); - /// - /// assert_eq!(1024, buf.get_ref().capacity()); - /// ``` - pub fn get_mut(&mut self) -> &mut B { - &mut self.buf - } - - /// Consumes this `Writer`, returning the underlying value. - /// - /// # Examples - /// - /// ```rust - /// use bytes::BufMut; - /// use std::io::{self, Cursor}; - /// - /// let mut buf = vec![].writer(); - /// let mut src = Cursor::new(b"hello world"); - /// - /// io::copy(&mut src, &mut buf).unwrap(); - /// - /// let buf = buf.into_inner(); - /// assert_eq!(*buf, b"hello world"[..]); - /// ``` - pub fn into_inner(self) -> B { - self.buf - } -} - -impl io::Write for Writer { - fn write(&mut self, src: &[u8]) -> io::Result { - let n = cmp::min(self.buf.remaining_mut(), src.len()); - - self.buf.put(&src[0..n]); - Ok(n) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/bytes.rs b/third_party/rust/bytes/v0_4/crate/src/bytes.rs deleted file mode 100644 index e1559311b0da..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/bytes.rs +++ /dev/null @@ -1,2947 +0,0 @@ -use {IntoBuf, Buf, BufMut}; -use buf::Iter; -use debug; - -use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize}; -use std::borrow::{Borrow, BorrowMut}; -use std::io::Cursor; -use std::sync::atomic::{self, AtomicUsize, AtomicPtr}; -use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; -use std::iter::{FromIterator, Iterator}; - -/// A reference counted contiguous slice of memory. -/// -/// `Bytes` is an efficient container for storing and operating on contiguous -/// slices of memory. It is intended for use primarily in networking code, but -/// could have applications elsewhere as well. -/// -/// `Bytes` values facilitate zero-copy network programming by allowing multiple -/// `Bytes` objects to point to the same underlying memory. This is managed by -/// using a reference count to track when the memory is no longer needed and can -/// be freed. -/// -/// ``` -/// use bytes::Bytes; -/// -/// let mut mem = Bytes::from(&b"Hello world"[..]); -/// let a = mem.slice(0, 5); -/// -/// assert_eq!(&a[..], b"Hello"); -/// -/// let b = mem.split_to(6); -/// -/// assert_eq!(&mem[..], b"world"); -/// assert_eq!(&b[..], b"Hello "); -/// ``` -/// -/// # Memory layout -/// -/// The `Bytes` struct itself is fairly small, limited to a pointer to the -/// memory and 4 `usize` fields used to track information about which segment of -/// the underlying memory the `Bytes` handle has access to. -/// -/// The memory layout looks like this: -/// -/// ```text -/// +-------+ -/// | Bytes | -/// +-------+ -/// / \_____ -/// | \ -/// v v -/// +-----+------------------------------------+ -/// | Arc | | Data | | -/// +-----+------------------------------------+ -/// ``` -/// -/// `Bytes` keeps both a pointer to the shared `Arc` containing the full memory -/// slice and a pointer to the start of the region visible by the handle. -/// `Bytes` also tracks the length of its view into the memory. -/// -/// # Sharing -/// -/// The memory itself is reference counted, and multiple `Bytes` objects may -/// point to the same region. Each `Bytes` handle point to different sections within -/// the memory region, and `Bytes` handle may or may not have overlapping views -/// into the memory. -/// -/// -/// ```text -/// -/// Arc ptrs +---------+ -/// ________________________ / | Bytes 2 | -/// / +---------+ -/// / +-----------+ | | -/// |_________/ | Bytes 1 | | | -/// | +-----------+ | | -/// | | | ___/ data | tail -/// | data | tail |/ | -/// v v v v -/// +-----+---------------------------------+-----+ -/// | Arc | | | | | -/// +-----+---------------------------------+-----+ -/// ``` -/// -/// # Mutating -/// -/// While `Bytes` handles may potentially represent overlapping views of the -/// underlying memory slice and may not be mutated, `BytesMut` handles are -/// guaranteed to be the only handle able to view that slice of memory. As such, -/// `BytesMut` handles are able to mutate the underlying memory. Note that -/// holding a unique view to a region of memory does not mean that there are no -/// other `Bytes` and `BytesMut` handles with disjoint views of the underlying -/// memory. -/// -/// # Inline bytes -/// -/// As an optimization, when the slice referenced by a `Bytes` or `BytesMut` -/// handle is small enough [^1], `with_capacity` will avoid the allocation by -/// inlining the slice directly in the handle. In this case, a clone is no -/// longer "shallow" and the data will be copied. Converting from a `Vec` will -/// never use inlining. -/// -/// [^1]: Small enough: 31 bytes on 64 bit systems, 15 on 32 bit systems. -/// -pub struct Bytes { - inner: Inner, -} - -/// A unique reference to a contiguous slice of memory. -/// -/// `BytesMut` represents a unique view into a potentially shared memory region. -/// Given the uniqueness guarantee, owners of `BytesMut` handles are able to -/// mutate the memory. It is similar to a `Vec` but with less copies and -/// allocations. -/// -/// For more detail, see [Bytes](struct.Bytes.html). -/// -/// # Growth -/// -/// One key difference from `Vec` is that most operations **do not -/// implicitly grow the buffer**. This means that calling `my_bytes.put("hello -/// world");` could panic if `my_bytes` does not have enough capacity. Before -/// writing to the buffer, ensure that there is enough remaining capacity by -/// calling `my_bytes.remaining_mut()`. In general, avoiding calls to `reserve` -/// is preferable. -/// -/// The only exception is `extend` which implicitly reserves required capacity. -/// -/// # Examples -/// -/// ``` -/// use bytes::{BytesMut, BufMut}; -/// -/// let mut buf = BytesMut::with_capacity(64); -/// -/// buf.put(b'h'); -/// buf.put(b'e'); -/// buf.put("llo"); -/// -/// assert_eq!(&buf[..], b"hello"); -/// -/// // Freeze the buffer so that it can be shared -/// let a = buf.freeze(); -/// -/// // This does not allocate, instead `b` points to the same memory. -/// let b = a.clone(); -/// -/// assert_eq!(&a[..], b"hello"); -/// assert_eq!(&b[..], b"hello"); -/// ``` -pub struct BytesMut { - inner: Inner, -} - -// Both `Bytes` and `BytesMut` are backed by `Inner` and functions are delegated -// to `Inner` functions. The `Bytes` and `BytesMut` shims ensure that functions -// that mutate the underlying buffer are only performed when the data range -// being mutated is only available via a single `BytesMut` handle. -// -// # Data storage modes -// -// The goal of `bytes` is to be as efficient as possible across a wide range of -// potential usage patterns. As such, `bytes` needs to be able to handle buffers -// that are never shared, shared on a single thread, and shared across many -// threads. `bytes` also needs to handle both tiny buffers as well as very large -// buffers. For example, [Cassandra](http://cassandra.apache.org) values have -// been known to be in the hundreds of megabyte, and HTTP header values can be a -// few characters in size. -// -// To achieve high performance in these various situations, `Bytes` and -// `BytesMut` use different strategies for storing the buffer depending on the -// usage pattern. -// -// ## Delayed `Arc` allocation -// -// When a `Bytes` or `BytesMut` is first created, there is only one outstanding -// handle referencing the buffer. Since sharing is not yet required, an `Arc`* is -// not used and the buffer is backed by a `Vec` directly. Using an -// `Arc>` requires two allocations, so if the buffer ends up never being -// shared, that allocation is avoided. -// -// When sharing does become necessary (`clone`, `split_to`, `split_off`), that -// is when the buffer is promoted to being shareable. The `Vec` is moved -// into an `Arc` and both the original handle and the new handle use the same -// buffer via the `Arc`. -// -// * `Arc` is being used to signify an atomically reference counted cell. We -// don't use the `Arc` implementation provided by `std` and instead use our own. -// This ends up simplifying a number of the `unsafe` code snippets. -// -// ## Inlining small buffers -// -// The `Bytes` / `BytesMut` structs require 4 pointer sized fields. On 64 bit -// systems, this ends up being 32 bytes, which is actually a lot of storage for -// cases where `Bytes` is being used to represent small byte strings, such as -// HTTP header names and values. -// -// To avoid any allocation at all in these cases, `Bytes` will use the struct -// itself for storing the buffer, reserving 1 byte for meta data. This means -// that, on 64 bit systems, 31 byte buffers require no allocation at all. -// -// The byte used for metadata stores a 2 bits flag used to indicate that the -// buffer is stored inline as well as 6 bits for tracking the buffer length (the -// return value of `Bytes::len`). -// -// ## Static buffers -// -// `Bytes` can also represent a static buffer, which is created with -// `Bytes::from_static`. No copying or allocations are required for tracking -// static buffers. The pointer to the `&'static [u8]`, the length, and a flag -// tracking that the `Bytes` instance represents a static buffer is stored in -// the `Bytes` struct. -// -// # Struct layout -// -// Both `Bytes` and `BytesMut` are wrappers around `Inner`, which provides the -// data fields as well as all of the function implementations. -// -// The `Inner` struct is carefully laid out in order to support the -// functionality described above as well as being as small as possible. Size is -// important as growing the size of the `Bytes` struct from 32 bytes to 40 bytes -// added as much as 15% overhead in benchmarks using `Bytes` in an HTTP header -// map structure. -// -// The `Inner` struct contains the following fields: -// -// * `ptr: *mut u8` -// * `len: usize` -// * `cap: usize` -// * `arc: AtomicPtr` -// -// ## `ptr: *mut u8` -// -// A pointer to start of the handle's buffer view. When backed by a `Vec`, -// this is always the `Vec`'s pointer. When backed by an `Arc>`, `ptr` -// may have been shifted to point somewhere inside the buffer. -// -// When in "inlined" mode, `ptr` is used as part of the inlined buffer. -// -// ## `len: usize` -// -// The length of the handle's buffer view. When backed by a `Vec`, this is -// always the `Vec`'s length. The slice represented by `ptr` and `len` should -// (ideally) always be initialized memory. -// -// When in "inlined" mode, `len` is used as part of the inlined buffer. -// -// ## `cap: usize` -// -// The capacity of the handle's buffer view. When backed by a `Vec`, this is -// always the `Vec`'s capacity. The slice represented by `ptr+len` and `cap-len` -// may or may not be initialized memory. -// -// When in "inlined" mode, `cap` is used as part of the inlined buffer. -// -// ## `arc: AtomicPtr` -// -// When `Inner` is in allocated mode (backed by Vec or Arc>), this -// will be the pointer to the `Arc` structure tracking the ref count for the -// underlying buffer. When the pointer is null, then the `Arc` has not been -// allocated yet and `self` is the only outstanding handle for the underlying -// buffer. -// -// The lower two bits of `arc` are used to track the storage mode of `Inner`. -// `0b01` indicates inline storage, `0b10` indicates static storage, and `0b11` -// indicates vector storage, not yet promoted to Arc. Since pointers to -// allocated structures are aligned, the lower two bits of a pointer will always -// be 0. This allows disambiguating between a pointer and the two flags. -// -// When in "inlined" mode, the least significant byte of `arc` is also used to -// store the length of the buffer view (vs. the capacity, which is a constant). -// -// The rest of `arc`'s bytes are used as part of the inline buffer, which means -// that those bytes need to be located next to the `ptr`, `len`, and `cap` -// fields, which make up the rest of the inline buffer. This requires special -// casing the layout of `Inner` depending on if the target platform is big or -// little endian. -// -// On little endian platforms, the `arc` field must be the first field in the -// struct. On big endian platforms, the `arc` field must be the last field in -// the struct. Since a deterministic struct layout is required, `Inner` is -// annotated with `#[repr(C)]`. -// -// # Thread safety -// -// `Bytes::clone()` returns a new `Bytes` handle with no copying. This is done -// by bumping the buffer ref count and returning a new struct pointing to the -// same buffer. However, the `Arc` structure is lazily allocated. This means -// that if `Bytes` is stored itself in an `Arc` (`Arc`), the `clone` -// function can be called concurrently from multiple threads. This is why an -// `AtomicPtr` is used for the `arc` field vs. a `*const`. -// -// Care is taken to ensure that the need for synchronization is minimized. Most -// operations do not require any synchronization. -// -#[cfg(target_endian = "little")] -#[repr(C)] -struct Inner { - // WARNING: Do not access the fields directly unless you know what you are - // doing. Instead, use the fns. See implementation comment above. - arc: AtomicPtr, - ptr: *mut u8, - len: usize, - cap: usize, -} - -#[cfg(target_endian = "big")] -#[repr(C)] -struct Inner { - // WARNING: Do not access the fields directly unless you know what you are - // doing. Instead, use the fns. See implementation comment above. - ptr: *mut u8, - len: usize, - cap: usize, - arc: AtomicPtr, -} - -// Thread-safe reference-counted container for the shared storage. This mostly -// the same as `std::sync::Arc` but without the weak counter. The ref counting -// fns are based on the ones found in `std`. -// -// The main reason to use `Shared` instead of `std::sync::Arc` is that it ends -// up making the overall code simpler and easier to reason about. This is due to -// some of the logic around setting `Inner::arc` and other ways the `arc` field -// is used. Using `Arc` ended up requiring a number of funky transmutes and -// other shenanigans to make it work. -struct Shared { - vec: Vec, - original_capacity_repr: usize, - ref_count: AtomicUsize, -} - -// Buffer storage strategy flags. -const KIND_ARC: usize = 0b00; -const KIND_INLINE: usize = 0b01; -const KIND_STATIC: usize = 0b10; -const KIND_VEC: usize = 0b11; -const KIND_MASK: usize = 0b11; - -// The max original capacity value. Any `Bytes` allocated with a greater initial -// capacity will default to this. -const MAX_ORIGINAL_CAPACITY_WIDTH: usize = 17; -// The original capacity algorithm will not take effect unless the originally -// allocated capacity was at least 1kb in size. -const MIN_ORIGINAL_CAPACITY_WIDTH: usize = 10; -// The original capacity is stored in powers of 2 starting at 1kb to a max of -// 64kb. Representing it as such requires only 3 bits of storage. -const ORIGINAL_CAPACITY_MASK: usize = 0b11100; -const ORIGINAL_CAPACITY_OFFSET: usize = 2; - -// When the storage is in the `Vec` representation, the pointer can be advanced -// at most this value. This is due to the amount of storage available to track -// the offset is usize - number of KIND bits and number of ORIGINAL_CAPACITY -// bits. -const VEC_POS_OFFSET: usize = 5; -const MAX_VEC_POS: usize = usize::MAX >> VEC_POS_OFFSET; -const NOT_VEC_POS_MASK: usize = 0b11111; - -// Bit op constants for extracting the inline length value from the `arc` field. -const INLINE_LEN_MASK: usize = 0b11111100; -const INLINE_LEN_OFFSET: usize = 2; - -// Byte offset from the start of `Inner` to where the inline buffer data -// starts. On little endian platforms, the first byte of the struct is the -// storage flag, so the data is shifted by a byte. On big endian systems, the -// data starts at the beginning of the struct. -#[cfg(target_endian = "little")] -const INLINE_DATA_OFFSET: isize = 1; -#[cfg(target_endian = "big")] -const INLINE_DATA_OFFSET: isize = 0; - -#[cfg(target_pointer_width = "64")] -const PTR_WIDTH: usize = 64; -#[cfg(target_pointer_width = "32")] -const PTR_WIDTH: usize = 32; - -// Inline buffer capacity. This is the size of `Inner` minus 1 byte for the -// metadata. -#[cfg(target_pointer_width = "64")] -const INLINE_CAP: usize = 4 * 8 - 1; -#[cfg(target_pointer_width = "32")] -const INLINE_CAP: usize = 4 * 4 - 1; - -/* - * - * ===== Bytes ===== - * - */ - -impl Bytes { - /// Creates a new `Bytes` with the specified capacity. - /// - /// The returned `Bytes` will be able to hold at least `capacity` bytes - /// without reallocating. If `capacity` is under `4 * size_of::() - 1`, - /// then `BytesMut` will not allocate. - /// - /// It is important to note that this function does not specify the length - /// of the returned `Bytes`, but only the capacity. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let mut bytes = Bytes::with_capacity(64); - /// - /// // `bytes` contains no data, even though there is capacity - /// assert_eq!(bytes.len(), 0); - /// - /// bytes.extend_from_slice(&b"hello world"[..]); - /// - /// assert_eq!(&bytes[..], b"hello world"); - /// ``` - #[inline] - pub fn with_capacity(capacity: usize) -> Bytes { - Bytes { - inner: Inner::with_capacity(capacity), - } - } - - /// Creates a new empty `Bytes`. - /// - /// This will not allocate and the returned `Bytes` handle will be empty. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let b = Bytes::new(); - /// assert_eq!(&b[..], b""); - /// ``` - #[inline] - pub fn new() -> Bytes { - Bytes::with_capacity(0) - } - - /// Creates a new `Bytes` from a static slice. - /// - /// The returned `Bytes` will point directly to the static slice. There is - /// no allocating or copying. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let b = Bytes::from_static(b"hello"); - /// assert_eq!(&b[..], b"hello"); - /// ``` - #[inline] - pub fn from_static(bytes: &'static [u8]) -> Bytes { - Bytes { - inner: Inner::from_static(bytes), - } - } - - /// Returns the number of bytes contained in this `Bytes`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let b = Bytes::from(&b"hello"[..]); - /// assert_eq!(b.len(), 5); - /// ``` - #[inline] - pub fn len(&self) -> usize { - self.inner.len() - } - - /// Returns true if the `Bytes` has a length of 0. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let b = Bytes::new(); - /// assert!(b.is_empty()); - /// ``` - #[inline] - pub fn is_empty(&self) -> bool { - self.inner.is_empty() - } - - /// Returns a slice of self for the index range `[begin..end)`. - /// - /// This will increment the reference count for the underlying memory and - /// return a new `Bytes` handle set to the slice. - /// - /// This operation is `O(1)`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let a = Bytes::from(&b"hello world"[..]); - /// let b = a.slice(2, 5); - /// - /// assert_eq!(&b[..], b"llo"); - /// ``` - /// - /// # Panics - /// - /// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing - /// will panic. - pub fn slice(&self, begin: usize, end: usize) -> Bytes { - assert!(begin <= end); - assert!(end <= self.len()); - - if end - begin <= INLINE_CAP { - return Bytes::from(&self[begin..end]); - } - - let mut ret = self.clone(); - - unsafe { - ret.inner.set_end(end); - ret.inner.set_start(begin); - } - - ret - } - - /// Returns a slice of self for the index range `[begin..self.len())`. - /// - /// This will increment the reference count for the underlying memory and - /// return a new `Bytes` handle set to the slice. - /// - /// This operation is `O(1)` and is equivalent to `self.slice(begin, - /// self.len())`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let a = Bytes::from(&b"hello world"[..]); - /// let b = a.slice_from(6); - /// - /// assert_eq!(&b[..], b"world"); - /// ``` - /// - /// # Panics - /// - /// Requires that `begin <= self.len()`, otherwise slicing will panic. - pub fn slice_from(&self, begin: usize) -> Bytes { - self.slice(begin, self.len()) - } - - /// Returns a slice of self for the index range `[0..end)`. - /// - /// This will increment the reference count for the underlying memory and - /// return a new `Bytes` handle set to the slice. - /// - /// This operation is `O(1)` and is equivalent to `self.slice(0, end)`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let a = Bytes::from(&b"hello world"[..]); - /// let b = a.slice_to(5); - /// - /// assert_eq!(&b[..], b"hello"); - /// ``` - /// - /// # Panics - /// - /// Requires that `end <= self.len()`, otherwise slicing will panic. - pub fn slice_to(&self, end: usize) -> Bytes { - self.slice(0, end) - } - - /// Returns a slice of self that is equivalent to the given `subset`. - /// - /// When processing a `Bytes` buffer with other tools, one often gets a - /// `&[u8]` which is in fact a slice of the `Bytes`, i.e. a subset of it. - /// This function turns that `&[u8]` into another `Bytes`, as if one had - /// called `self.slice()` with the offsets that correspond to `subset`. - /// - /// This operation is `O(1)`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let bytes = Bytes::from(&b"012345678"[..]); - /// let as_slice = bytes.as_ref(); - /// let subset = &as_slice[2..6]; - /// let subslice = bytes.slice_ref(&subset); - /// assert_eq!(&subslice[..], b"2345"); - /// ``` - /// - /// # Panics - /// - /// Requires that the given `sub` slice is in fact contained within the - /// `Bytes` buffer; otherwise this function will panic. - pub fn slice_ref(&self, subset: &[u8]) -> Bytes { - let bytes_p = self.as_ptr() as usize; - let bytes_len = self.len(); - - let sub_p = subset.as_ptr() as usize; - let sub_len = subset.len(); - - assert!(sub_p >= bytes_p); - assert!(sub_p + sub_len <= bytes_p + bytes_len); - - let sub_offset = sub_p - bytes_p; - - self.slice(sub_offset, sub_offset + sub_len) - } - - /// Splits the bytes into two at the given index. - /// - /// Afterwards `self` contains elements `[0, at)`, and the returned `Bytes` - /// contains elements `[at, len)`. - /// - /// This is an `O(1)` operation that just increases the reference count and - /// sets a few indices. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let mut a = Bytes::from(&b"hello world"[..]); - /// let b = a.split_off(5); - /// - /// assert_eq!(&a[..], b"hello"); - /// assert_eq!(&b[..], b" world"); - /// ``` - /// - /// # Panics - /// - /// Panics if `at > len`. - pub fn split_off(&mut self, at: usize) -> Bytes { - assert!(at <= self.len()); - - if at == self.len() { - return Bytes::new(); - } - - if at == 0 { - return mem::replace(self, Bytes::new()); - } - - Bytes { - inner: self.inner.split_off(at), - } - } - - /// Splits the bytes into two at the given index. - /// - /// Afterwards `self` contains elements `[at, len)`, and the returned - /// `Bytes` contains elements `[0, at)`. - /// - /// This is an `O(1)` operation that just increases the reference count and - /// sets a few indices. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let mut a = Bytes::from(&b"hello world"[..]); - /// let b = a.split_to(5); - /// - /// assert_eq!(&a[..], b" world"); - /// assert_eq!(&b[..], b"hello"); - /// ``` - /// - /// # Panics - /// - /// Panics if `at > len`. - pub fn split_to(&mut self, at: usize) -> Bytes { - assert!(at <= self.len()); - - if at == self.len() { - return mem::replace(self, Bytes::new()); - } - - if at == 0 { - return Bytes::new(); - } - - Bytes { - inner: self.inner.split_to(at), - } - } - - #[deprecated(since = "0.4.1", note = "use split_to instead")] - #[doc(hidden)] - pub fn drain_to(&mut self, at: usize) -> Bytes { - self.split_to(at) - } - - /// Shortens the buffer, keeping the first `len` bytes and dropping the - /// rest. - /// - /// If `len` is greater than the buffer's current length, this has no - /// effect. - /// - /// The [`split_off`] method can emulate `truncate`, but this causes the - /// excess bytes to be returned instead of dropped. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let mut buf = Bytes::from(&b"hello world"[..]); - /// buf.truncate(5); - /// assert_eq!(buf, b"hello"[..]); - /// ``` - /// - /// [`split_off`]: #method.split_off - pub fn truncate(&mut self, len: usize) { - self.inner.truncate(len); - } - - /// Shortens the buffer, dropping the first `cnt` bytes and keeping the - /// rest. - /// - /// This is the same function as `Buf::advance`, and in the next breaking - /// release of `bytes`, this implementation will be removed in favor of - /// having `Bytes` implement `Buf`. - /// - /// # Panics - /// - /// This function panics if `cnt` is greater than `self.len()` - #[inline] - pub fn advance(&mut self, cnt: usize) { - assert!(cnt <= self.len(), "cannot advance past `remaining`"); - unsafe { self.inner.set_start(cnt); } - } - - /// Clears the buffer, removing all data. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let mut buf = Bytes::from(&b"hello world"[..]); - /// buf.clear(); - /// assert!(buf.is_empty()); - /// ``` - pub fn clear(&mut self) { - self.truncate(0); - } - - /// Attempts to convert into a `BytesMut` handle. - /// - /// This will only succeed if there are no other outstanding references to - /// the underlying chunk of memory. `Bytes` handles that contain inlined - /// bytes will always be convertable to `BytesMut`. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let a = Bytes::from(&b"Mary had a little lamb, little lamb, little lamb..."[..]); - /// - /// // Create a shallow clone - /// let b = a.clone(); - /// - /// // This will fail because `b` shares a reference with `a` - /// let a = a.try_mut().unwrap_err(); - /// - /// drop(b); - /// - /// // This will succeed - /// let mut a = a.try_mut().unwrap(); - /// - /// a[0] = b'b'; - /// - /// assert_eq!(&a[..4], b"bary"); - /// ``` - pub fn try_mut(mut self) -> Result { - if self.inner.is_mut_safe() { - Ok(BytesMut { inner: self.inner }) - } else { - Err(self) - } - } - - /// Appends given bytes to this object. - /// - /// If this `Bytes` object has not enough capacity, it is resized first. - /// If it is shared (`refcount > 1`), it is copied first. - /// - /// This operation can be less effective than the similar operation on - /// `BytesMut`, especially on small additions. - /// - /// # Examples - /// - /// ``` - /// use bytes::Bytes; - /// - /// let mut buf = Bytes::from("aabb"); - /// buf.extend_from_slice(b"ccdd"); - /// buf.extend_from_slice(b"eeff"); - /// - /// assert_eq!(b"aabbccddeeff", &buf[..]); - /// ``` - pub fn extend_from_slice(&mut self, extend: &[u8]) { - if extend.is_empty() { - return; - } - - let new_cap = self.len().checked_add(extend.len()).expect("capacity overflow"); - - let result = match mem::replace(self, Bytes::new()).try_mut() { - Ok(mut bytes_mut) => { - bytes_mut.extend_from_slice(extend); - bytes_mut - }, - Err(bytes) => { - let mut bytes_mut = BytesMut::with_capacity(new_cap); - bytes_mut.put_slice(&bytes); - bytes_mut.put_slice(extend); - bytes_mut - } - }; - - mem::replace(self, result.freeze()); - } -} - -impl IntoBuf for Bytes { - type Buf = Cursor; - - fn into_buf(self) -> Self::Buf { - Cursor::new(self) - } -} - -impl<'a> IntoBuf for &'a Bytes { - type Buf = Cursor; - - fn into_buf(self) -> Self::Buf { - Cursor::new(self) - } -} - -impl Clone for Bytes { - fn clone(&self) -> Bytes { - Bytes { - inner: unsafe { self.inner.shallow_clone(false) }, - } - } -} - -impl AsRef<[u8]> for Bytes { - #[inline] - fn as_ref(&self) -> &[u8] { - self.inner.as_ref() - } -} - -impl ops::Deref for Bytes { - type Target = [u8]; - - #[inline] - fn deref(&self) -> &[u8] { - self.inner.as_ref() - } -} - -impl From for Bytes { - fn from(src: BytesMut) -> Bytes { - src.freeze() - } -} - -impl From> for Bytes { - fn from(src: Vec) -> Bytes { - BytesMut::from(src).freeze() - } -} - -impl From for Bytes { - fn from(src: String) -> Bytes { - BytesMut::from(src).freeze() - } -} - -impl<'a> From<&'a [u8]> for Bytes { - fn from(src: &'a [u8]) -> Bytes { - BytesMut::from(src).freeze() - } -} - -impl<'a> From<&'a str> for Bytes { - fn from(src: &'a str) -> Bytes { - BytesMut::from(src).freeze() - } -} - -impl FromIterator for BytesMut { - fn from_iter>(into_iter: T) -> Self { - let iter = into_iter.into_iter(); - let (min, maybe_max) = iter.size_hint(); - - let mut out = BytesMut::with_capacity(maybe_max.unwrap_or(min)); - - for i in iter { - out.reserve(1); - out.put(i); - } - - out - } -} - -impl FromIterator for Bytes { - fn from_iter>(into_iter: T) -> Self { - BytesMut::from_iter(into_iter).freeze() - } -} - -impl<'a> FromIterator<&'a u8> for BytesMut { - fn from_iter>(into_iter: T) -> Self { - BytesMut::from_iter(into_iter.into_iter().map(|b| *b)) - } -} - -impl<'a> FromIterator<&'a u8> for Bytes { - fn from_iter>(into_iter: T) -> Self { - BytesMut::from_iter(into_iter).freeze() - } -} - -impl PartialEq for Bytes { - fn eq(&self, other: &Bytes) -> bool { - self.inner.as_ref() == other.inner.as_ref() - } -} - -impl PartialOrd for Bytes { - fn partial_cmp(&self, other: &Bytes) -> Option { - self.inner.as_ref().partial_cmp(other.inner.as_ref()) - } -} - -impl Ord for Bytes { - fn cmp(&self, other: &Bytes) -> cmp::Ordering { - self.inner.as_ref().cmp(other.inner.as_ref()) - } -} - -impl Eq for Bytes { -} - -impl Default for Bytes { - #[inline] - fn default() -> Bytes { - Bytes::new() - } -} - -impl fmt::Debug for Bytes { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&debug::BsDebug(&self.inner.as_ref()), fmt) - } -} - -impl hash::Hash for Bytes { - fn hash(&self, state: &mut H) where H: hash::Hasher { - let s: &[u8] = self.as_ref(); - s.hash(state); - } -} - -impl Borrow<[u8]> for Bytes { - fn borrow(&self) -> &[u8] { - self.as_ref() - } -} - -impl IntoIterator for Bytes { - type Item = u8; - type IntoIter = Iter>; - - fn into_iter(self) -> Self::IntoIter { - self.into_buf().iter() - } -} - -impl<'a> IntoIterator for &'a Bytes { - type Item = u8; - type IntoIter = Iter>; - - fn into_iter(self) -> Self::IntoIter { - self.into_buf().iter() - } -} - -impl Extend for Bytes { - fn extend(&mut self, iter: T) where T: IntoIterator { - let iter = iter.into_iter(); - - let (lower, upper) = iter.size_hint(); - - // Avoid possible conversion into mut if there's nothing to add - if let Some(0) = upper { - return; - } - - let mut bytes_mut = match mem::replace(self, Bytes::new()).try_mut() { - Ok(bytes_mut) => bytes_mut, - Err(bytes) => { - let mut bytes_mut = BytesMut::with_capacity(bytes.len() + lower); - bytes_mut.put_slice(&bytes); - bytes_mut - } - }; - - bytes_mut.extend(iter); - - mem::replace(self, bytes_mut.freeze()); - } -} - -impl<'a> Extend<&'a u8> for Bytes { - fn extend(&mut self, iter: T) where T: IntoIterator { - self.extend(iter.into_iter().map(|b| *b)) - } -} - -/* - * - * ===== BytesMut ===== - * - */ - -impl BytesMut { - /// Creates a new `BytesMut` with the specified capacity. - /// - /// The returned `BytesMut` will be able to hold at least `capacity` bytes - /// without reallocating. If `capacity` is under `4 * size_of::() - 1`, - /// then `BytesMut` will not allocate. - /// - /// It is important to note that this function does not specify the length - /// of the returned `BytesMut`, but only the capacity. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BytesMut, BufMut}; - /// - /// let mut bytes = BytesMut::with_capacity(64); - /// - /// // `bytes` contains no data, even though there is capacity - /// assert_eq!(bytes.len(), 0); - /// - /// bytes.put(&b"hello world"[..]); - /// - /// assert_eq!(&bytes[..], b"hello world"); - /// ``` - #[inline] - pub fn with_capacity(capacity: usize) -> BytesMut { - BytesMut { - inner: Inner::with_capacity(capacity), - } - } - - /// Creates a new `BytesMut` with default capacity. - /// - /// Resulting object has length 0 and unspecified capacity. - /// This function does not allocate. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BytesMut, BufMut}; - /// - /// let mut bytes = BytesMut::new(); - /// - /// assert_eq!(0, bytes.len()); - /// - /// bytes.reserve(2); - /// bytes.put_slice(b"xy"); - /// - /// assert_eq!(&b"xy"[..], &bytes[..]); - /// ``` - #[inline] - pub fn new() -> BytesMut { - BytesMut::with_capacity(0) - } - - /// Returns the number of bytes contained in this `BytesMut`. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let b = BytesMut::from(&b"hello"[..]); - /// assert_eq!(b.len(), 5); - /// ``` - #[inline] - pub fn len(&self) -> usize { - self.inner.len() - } - - /// Returns true if the `BytesMut` has a length of 0. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let b = BytesMut::with_capacity(64); - /// assert!(b.is_empty()); - /// ``` - #[inline] - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns the number of bytes the `BytesMut` can hold without reallocating. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let b = BytesMut::with_capacity(64); - /// assert_eq!(b.capacity(), 64); - /// ``` - #[inline] - pub fn capacity(&self) -> usize { - self.inner.capacity() - } - - /// Converts `self` into an immutable `Bytes`. - /// - /// The conversion is zero cost and is used to indicate that the slice - /// referenced by the handle will no longer be mutated. Once the conversion - /// is done, the handle can be cloned and shared across threads. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BytesMut, BufMut}; - /// use std::thread; - /// - /// let mut b = BytesMut::with_capacity(64); - /// b.put("hello world"); - /// let b1 = b.freeze(); - /// let b2 = b1.clone(); - /// - /// let th = thread::spawn(move || { - /// assert_eq!(&b1[..], b"hello world"); - /// }); - /// - /// assert_eq!(&b2[..], b"hello world"); - /// th.join().unwrap(); - /// ``` - #[inline] - pub fn freeze(self) -> Bytes { - Bytes { inner: self.inner } - } - - /// Splits the bytes into two at the given index. - /// - /// Afterwards `self` contains elements `[0, at)`, and the returned - /// `BytesMut` contains elements `[at, capacity)`. - /// - /// This is an `O(1)` operation that just increases the reference count - /// and sets a few indices. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut a = BytesMut::from(&b"hello world"[..]); - /// let mut b = a.split_off(5); - /// - /// a[0] = b'j'; - /// b[0] = b'!'; - /// - /// assert_eq!(&a[..], b"jello"); - /// assert_eq!(&b[..], b"!world"); - /// ``` - /// - /// # Panics - /// - /// Panics if `at > capacity`. - pub fn split_off(&mut self, at: usize) -> BytesMut { - BytesMut { - inner: self.inner.split_off(at), - } - } - - /// Removes the bytes from the current view, returning them in a new - /// `BytesMut` handle. - /// - /// Afterwards, `self` will be empty, but will retain any additional - /// capacity that it had before the operation. This is identical to - /// `self.split_to(self.len())`. - /// - /// This is an `O(1)` operation that just increases the reference count and - /// sets a few indices. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BytesMut, BufMut}; - /// - /// let mut buf = BytesMut::with_capacity(1024); - /// buf.put(&b"hello world"[..]); - /// - /// let other = buf.take(); - /// - /// assert!(buf.is_empty()); - /// assert_eq!(1013, buf.capacity()); - /// - /// assert_eq!(other, b"hello world"[..]); - /// ``` - pub fn take(&mut self) -> BytesMut { - let len = self.len(); - self.split_to(len) - } - - #[deprecated(since = "0.4.1", note = "use take instead")] - #[doc(hidden)] - pub fn drain(&mut self) -> BytesMut { - self.take() - } - - /// Splits the buffer into two at the given index. - /// - /// Afterwards `self` contains elements `[at, len)`, and the returned `BytesMut` - /// contains elements `[0, at)`. - /// - /// This is an `O(1)` operation that just increases the reference count and - /// sets a few indices. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut a = BytesMut::from(&b"hello world"[..]); - /// let mut b = a.split_to(5); - /// - /// a[0] = b'!'; - /// b[0] = b'j'; - /// - /// assert_eq!(&a[..], b"!world"); - /// assert_eq!(&b[..], b"jello"); - /// ``` - /// - /// # Panics - /// - /// Panics if `at > len`. - pub fn split_to(&mut self, at: usize) -> BytesMut { - BytesMut { - inner: self.inner.split_to(at), - } - } - - #[deprecated(since = "0.4.1", note = "use split_to instead")] - #[doc(hidden)] - pub fn drain_to(&mut self, at: usize) -> BytesMut { - self.split_to(at) - } - - /// Shortens the buffer, keeping the first `len` bytes and dropping the - /// rest. - /// - /// If `len` is greater than the buffer's current length, this has no - /// effect. - /// - /// The [`split_off`] method can emulate `truncate`, but this causes the - /// excess bytes to be returned instead of dropped. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut buf = BytesMut::from(&b"hello world"[..]); - /// buf.truncate(5); - /// assert_eq!(buf, b"hello"[..]); - /// ``` - /// - /// [`split_off`]: #method.split_off - pub fn truncate(&mut self, len: usize) { - self.inner.truncate(len); - } - - /// Shortens the buffer, dropping the first `cnt` bytes and keeping the - /// rest. - /// - /// This is the same function as `Buf::advance`, and in the next breaking - /// release of `bytes`, this implementation will be removed in favor of - /// having `BytesMut` implement `Buf`. - /// - /// # Panics - /// - /// This function panics if `cnt` is greater than `self.len()` - #[inline] - pub fn advance(&mut self, cnt: usize) { - assert!(cnt <= self.len(), "cannot advance past `remaining`"); - unsafe { self.inner.set_start(cnt); } - } - - /// Clears the buffer, removing all data. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut buf = BytesMut::from(&b"hello world"[..]); - /// buf.clear(); - /// assert!(buf.is_empty()); - /// ``` - pub fn clear(&mut self) { - self.truncate(0); - } - - /// Resizes the buffer so that `len` is equal to `new_len`. - /// - /// If `new_len` is greater than `len`, the buffer is extended by the - /// difference with each additional byte set to `value`. If `new_len` is - /// less than `len`, the buffer is simply truncated. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut buf = BytesMut::new(); - /// - /// buf.resize(3, 0x1); - /// assert_eq!(&buf[..], &[0x1, 0x1, 0x1]); - /// - /// buf.resize(2, 0x2); - /// assert_eq!(&buf[..], &[0x1, 0x1]); - /// - /// buf.resize(4, 0x3); - /// assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]); - /// ``` - pub fn resize(&mut self, new_len: usize, value: u8) { - self.inner.resize(new_len, value); - } - - /// Sets the length of the buffer. - /// - /// This will explicitly set the size of the buffer without actually - /// modifying the data, so it is up to the caller to ensure that the data - /// has been initialized. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut b = BytesMut::from(&b"hello world"[..]); - /// - /// unsafe { - /// b.set_len(5); - /// } - /// - /// assert_eq!(&b[..], b"hello"); - /// - /// unsafe { - /// b.set_len(11); - /// } - /// - /// assert_eq!(&b[..], b"hello world"); - /// ``` - /// - /// # Panics - /// - /// This method will panic if `len` is out of bounds for the underlying - /// slice or if it comes after the `end` of the configured window. - pub unsafe fn set_len(&mut self, len: usize) { - self.inner.set_len(len) - } - - /// Reserves capacity for at least `additional` more bytes to be inserted - /// into the given `BytesMut`. - /// - /// More than `additional` bytes may be reserved in order to avoid frequent - /// reallocations. A call to `reserve` may result in an allocation. - /// - /// Before allocating new buffer space, the function will attempt to reclaim - /// space in the existing buffer. If the current handle references a small - /// view in the original buffer and all other handles have been dropped, - /// and the requested capacity is less than or equal to the existing - /// buffer's capacity, then the current view will be copied to the front of - /// the buffer and the handle will take ownership of the full buffer. - /// - /// # Examples - /// - /// In the following example, a new buffer is allocated. - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut buf = BytesMut::from(&b"hello"[..]); - /// buf.reserve(64); - /// assert!(buf.capacity() >= 69); - /// ``` - /// - /// In the following example, the existing buffer is reclaimed. - /// - /// ``` - /// use bytes::{BytesMut, BufMut}; - /// - /// let mut buf = BytesMut::with_capacity(128); - /// buf.put(&[0; 64][..]); - /// - /// let ptr = buf.as_ptr(); - /// let other = buf.take(); - /// - /// assert!(buf.is_empty()); - /// assert_eq!(buf.capacity(), 64); - /// - /// drop(other); - /// buf.reserve(128); - /// - /// assert_eq!(buf.capacity(), 128); - /// assert_eq!(buf.as_ptr(), ptr); - /// ``` - /// - /// # Panics - /// - /// Panics if the new capacity overflows `usize`. - pub fn reserve(&mut self, additional: usize) { - self.inner.reserve(additional) - } - - /// Appends given bytes to this object. - /// - /// If this `BytesMut` object has not enough capacity, it is resized first. - /// So unlike `put_slice` operation, `extend_from_slice` does not panic. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut buf = BytesMut::with_capacity(0); - /// buf.extend_from_slice(b"aaabbb"); - /// buf.extend_from_slice(b"cccddd"); - /// - /// assert_eq!(b"aaabbbcccddd", &buf[..]); - /// ``` - pub fn extend_from_slice(&mut self, extend: &[u8]) { - self.reserve(extend.len()); - self.put_slice(extend); - } - - /// Combine splitted BytesMut objects back as contiguous. - /// - /// If `BytesMut` objects were not contiguous originally, they will be extended. - /// - /// # Examples - /// - /// ``` - /// use bytes::BytesMut; - /// - /// let mut buf = BytesMut::with_capacity(64); - /// buf.extend_from_slice(b"aaabbbcccddd"); - /// - /// let splitted = buf.split_off(6); - /// assert_eq!(b"aaabbb", &buf[..]); - /// assert_eq!(b"cccddd", &splitted[..]); - /// - /// buf.unsplit(splitted); - /// assert_eq!(b"aaabbbcccddd", &buf[..]); - /// ``` - pub fn unsplit(&mut self, other: BytesMut) { - let ptr; - - if other.is_empty() { - return; - } - - if self.is_empty() { - *self = other; - return; - } - - unsafe { - ptr = self.inner.ptr.offset(self.inner.len as isize); - } - if ptr == other.inner.ptr && - self.inner.kind() == KIND_ARC && - other.inner.kind() == KIND_ARC - { - debug_assert_eq!(self.inner.arc.load(Acquire), - other.inner.arc.load(Acquire)); - // Contiguous blocks, just combine directly - self.inner.len += other.inner.len; - self.inner.cap += other.inner.cap; - } - else { - self.extend_from_slice(&other); - } - } -} - -impl BufMut for BytesMut { - #[inline] - fn remaining_mut(&self) -> usize { - self.capacity() - self.len() - } - - #[inline] - unsafe fn advance_mut(&mut self, cnt: usize) { - let new_len = self.len() + cnt; - - // This call will panic if `cnt` is too big - self.inner.set_len(new_len); - } - - #[inline] - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - let len = self.len(); - - // This will never panic as `len` can never become invalid - &mut self.inner.as_raw()[len..] - } - - #[inline] - fn put_slice(&mut self, src: &[u8]) { - assert!(self.remaining_mut() >= src.len()); - - let len = src.len(); - - unsafe { - self.bytes_mut()[..len].copy_from_slice(src); - self.advance_mut(len); - } - } - - #[inline] - fn put_u8(&mut self, n: u8) { - self.inner.put_u8(n); - } - - #[inline] - fn put_i8(&mut self, n: i8) { - self.put_u8(n as u8); - } -} - -impl IntoBuf for BytesMut { - type Buf = Cursor; - - fn into_buf(self) -> Self::Buf { - Cursor::new(self) - } -} - -impl<'a> IntoBuf for &'a BytesMut { - type Buf = Cursor<&'a BytesMut>; - - fn into_buf(self) -> Self::Buf { - Cursor::new(self) - } -} - -impl AsRef<[u8]> for BytesMut { - #[inline] - fn as_ref(&self) -> &[u8] { - self.inner.as_ref() - } -} - -impl ops::Deref for BytesMut { - type Target = [u8]; - - #[inline] - fn deref(&self) -> &[u8] { - self.as_ref() - } -} - -impl AsMut<[u8]> for BytesMut { - fn as_mut(&mut self) -> &mut [u8] { - self.inner.as_mut() - } -} - -impl ops::DerefMut for BytesMut { - #[inline] - fn deref_mut(&mut self) -> &mut [u8] { - self.inner.as_mut() - } -} - -impl From> for BytesMut { - fn from(src: Vec) -> BytesMut { - BytesMut { - inner: Inner::from_vec(src), - } - } -} - -impl From for BytesMut { - fn from(src: String) -> BytesMut { - BytesMut::from(src.into_bytes()) - } -} - -impl<'a> From<&'a [u8]> for BytesMut { - fn from(src: &'a [u8]) -> BytesMut { - let len = src.len(); - - if len == 0 { - BytesMut::new() - } else if len <= INLINE_CAP { - unsafe { - let mut inner: Inner = mem::uninitialized(); - - // Set inline mask - inner.arc = AtomicPtr::new(KIND_INLINE as *mut Shared); - inner.set_inline_len(len); - inner.as_raw()[0..len].copy_from_slice(src); - - BytesMut { - inner: inner, - } - } - } else { - BytesMut::from(src.to_vec()) - } - } -} - -impl<'a> From<&'a str> for BytesMut { - fn from(src: &'a str) -> BytesMut { - BytesMut::from(src.as_bytes()) - } -} - -impl From for BytesMut { - fn from(src: Bytes) -> BytesMut { - src.try_mut() - .unwrap_or_else(|src| BytesMut::from(&src[..])) - } -} - -impl PartialEq for BytesMut { - fn eq(&self, other: &BytesMut) -> bool { - self.inner.as_ref() == other.inner.as_ref() - } -} - -impl PartialOrd for BytesMut { - fn partial_cmp(&self, other: &BytesMut) -> Option { - self.inner.as_ref().partial_cmp(other.inner.as_ref()) - } -} - -impl Ord for BytesMut { - fn cmp(&self, other: &BytesMut) -> cmp::Ordering { - self.inner.as_ref().cmp(other.inner.as_ref()) - } -} - -impl Eq for BytesMut { -} - -impl Default for BytesMut { - #[inline] - fn default() -> BytesMut { - BytesMut::new() - } -} - -impl fmt::Debug for BytesMut { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&debug::BsDebug(&self.inner.as_ref()), fmt) - } -} - -impl hash::Hash for BytesMut { - fn hash(&self, state: &mut H) where H: hash::Hasher { - let s: &[u8] = self.as_ref(); - s.hash(state); - } -} - -impl Borrow<[u8]> for BytesMut { - fn borrow(&self) -> &[u8] { - self.as_ref() - } -} - -impl BorrowMut<[u8]> for BytesMut { - fn borrow_mut(&mut self) -> &mut [u8] { - self.as_mut() - } -} - -impl fmt::Write for BytesMut { - #[inline] - fn write_str(&mut self, s: &str) -> fmt::Result { - if self.remaining_mut() >= s.len() { - self.put_slice(s.as_bytes()); - Ok(()) - } else { - Err(fmt::Error) - } - } - - #[inline] - fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result { - fmt::write(self, args) - } -} - -impl Clone for BytesMut { - fn clone(&self) -> BytesMut { - BytesMut::from(&self[..]) - } -} - -impl IntoIterator for BytesMut { - type Item = u8; - type IntoIter = Iter>; - - fn into_iter(self) -> Self::IntoIter { - self.into_buf().iter() - } -} - -impl<'a> IntoIterator for &'a BytesMut { - type Item = u8; - type IntoIter = Iter>; - - fn into_iter(self) -> Self::IntoIter { - self.into_buf().iter() - } -} - -impl Extend for BytesMut { - fn extend(&mut self, iter: T) where T: IntoIterator { - let iter = iter.into_iter(); - - let (lower, _) = iter.size_hint(); - self.reserve(lower); - - for b in iter { - unsafe { - self.bytes_mut()[0] = b; - self.advance_mut(1); - } - } - } -} - -impl<'a> Extend<&'a u8> for BytesMut { - fn extend(&mut self, iter: T) where T: IntoIterator { - self.extend(iter.into_iter().map(|b| *b)) - } -} - -/* - * - * ===== Inner ===== - * - */ - -impl Inner { - #[inline] - fn from_static(bytes: &'static [u8]) -> Inner { - let ptr = bytes.as_ptr() as *mut u8; - - Inner { - // `arc` won't ever store a pointer. Instead, use it to - // track the fact that the `Bytes` handle is backed by a - // static buffer. - arc: AtomicPtr::new(KIND_STATIC as *mut Shared), - ptr: ptr, - len: bytes.len(), - cap: bytes.len(), - } - } - - #[inline] - fn from_vec(mut src: Vec) -> Inner { - let len = src.len(); - let cap = src.capacity(); - let ptr = src.as_mut_ptr(); - - mem::forget(src); - - let original_capacity_repr = original_capacity_to_repr(cap); - let arc = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC; - - Inner { - arc: AtomicPtr::new(arc as *mut Shared), - ptr: ptr, - len: len, - cap: cap, - } - } - - #[inline] - fn with_capacity(capacity: usize) -> Inner { - if capacity <= INLINE_CAP { - unsafe { - // Using uninitialized memory is ~30% faster - let mut inner: Inner = mem::uninitialized(); - inner.arc = AtomicPtr::new(KIND_INLINE as *mut Shared); - inner - } - } else { - Inner::from_vec(Vec::with_capacity(capacity)) - } - } - - /// Return a slice for the handle's view into the shared buffer - #[inline] - fn as_ref(&self) -> &[u8] { - unsafe { - if self.is_inline() { - slice::from_raw_parts(self.inline_ptr(), self.inline_len()) - } else { - slice::from_raw_parts(self.ptr, self.len) - } - } - } - - /// Return a mutable slice for the handle's view into the shared buffer - #[inline] - fn as_mut(&mut self) -> &mut [u8] { - debug_assert!(!self.is_static()); - - unsafe { - if self.is_inline() { - slice::from_raw_parts_mut(self.inline_ptr(), self.inline_len()) - } else { - slice::from_raw_parts_mut(self.ptr, self.len) - } - } - } - - /// Return a mutable slice for the handle's view into the shared buffer - /// including potentially uninitialized bytes. - #[inline] - unsafe fn as_raw(&mut self) -> &mut [u8] { - debug_assert!(!self.is_static()); - - if self.is_inline() { - slice::from_raw_parts_mut(self.inline_ptr(), INLINE_CAP) - } else { - slice::from_raw_parts_mut(self.ptr, self.cap) - } - } - - /// Insert a byte into the next slot and advance the len by 1. - #[inline] - fn put_u8(&mut self, n: u8) { - if self.is_inline() { - let len = self.inline_len(); - assert!(len < INLINE_CAP); - unsafe { - *self.inline_ptr().offset(len as isize) = n; - } - self.set_inline_len(len + 1); - } else { - assert!(self.len < self.cap); - unsafe { - *self.ptr.offset(self.len as isize) = n; - } - self.len += 1; - } - } - - #[inline] - fn len(&self) -> usize { - if self.is_inline() { - self.inline_len() - } else { - self.len - } - } - - /// Pointer to the start of the inline buffer - #[inline] - unsafe fn inline_ptr(&self) -> *mut u8 { - (self as *const Inner as *mut Inner as *mut u8) - .offset(INLINE_DATA_OFFSET) - } - - #[inline] - fn inline_len(&self) -> usize { - let p: &usize = unsafe { mem::transmute(&self.arc) }; - (p & INLINE_LEN_MASK) >> INLINE_LEN_OFFSET - } - - /// Set the length of the inline buffer. This is done by writing to the - /// least significant byte of the `arc` field. - #[inline] - fn set_inline_len(&mut self, len: usize) { - debug_assert!(len <= INLINE_CAP); - let p = self.arc.get_mut(); - *p = ((*p as usize & !INLINE_LEN_MASK) | (len << INLINE_LEN_OFFSET)) as _; - } - - /// slice. - #[inline] - unsafe fn set_len(&mut self, len: usize) { - if self.is_inline() { - assert!(len <= INLINE_CAP); - self.set_inline_len(len); - } else { - assert!(len <= self.cap); - self.len = len; - } - } - - #[inline] - fn is_empty(&self) -> bool { - self.len() == 0 - } - - #[inline] - fn capacity(&self) -> usize { - if self.is_inline() { - INLINE_CAP - } else { - self.cap - } - } - - fn split_off(&mut self, at: usize) -> Inner { - let mut other = unsafe { self.shallow_clone(true) }; - - unsafe { - other.set_start(at); - self.set_end(at); - } - - return other - } - - fn split_to(&mut self, at: usize) -> Inner { - let mut other = unsafe { self.shallow_clone(true) }; - - unsafe { - other.set_end(at); - self.set_start(at); - } - - return other - } - - fn truncate(&mut self, len: usize) { - if len <= self.len() { - unsafe { self.set_len(len); } - } - } - - fn resize(&mut self, new_len: usize, value: u8) { - let len = self.len(); - if new_len > len { - let additional = new_len - len; - self.reserve(additional); - unsafe { - let dst = self.as_raw()[len..].as_mut_ptr(); - ptr::write_bytes(dst, value, additional); - self.set_len(new_len); - } - } else { - self.truncate(new_len); - } - } - - unsafe fn set_start(&mut self, start: usize) { - // Setting the start to 0 is a no-op, so return early if this is the - // case. - if start == 0 { - return; - } - - let kind = self.kind(); - - // Always check `inline` first, because if the handle is using inline - // data storage, all of the `Inner` struct fields will be gibberish. - if kind == KIND_INLINE { - assert!(start <= INLINE_CAP); - - let len = self.inline_len(); - - if len <= start { - self.set_inline_len(0); - } else { - // `set_start` is essentially shifting data off the front of the - // view. Inlined buffers only track the length of the slice. - // So, to update the start, the data at the new starting point - // is copied to the beginning of the buffer. - let new_len = len - start; - - let dst = self.inline_ptr(); - let src = (dst as *const u8).offset(start as isize); - - ptr::copy(src, dst, new_len); - - self.set_inline_len(new_len); - } - } else { - assert!(start <= self.cap); - - if kind == KIND_VEC { - // Setting the start when in vec representation is a little more - // complicated. First, we have to track how far ahead the - // "start" of the byte buffer from the beginning of the vec. We - // also have to ensure that we don't exceed the maximum shift. - let (mut pos, prev) = self.uncoordinated_get_vec_pos(); - pos += start; - - if pos <= MAX_VEC_POS { - self.uncoordinated_set_vec_pos(pos, prev); - } else { - // The repr must be upgraded to ARC. This will never happen - // on 64 bit systems and will only happen on 32 bit systems - // when shifting past 134,217,727 bytes. As such, we don't - // worry too much about performance here. - let _ = self.shallow_clone(true); - } - } - - // Updating the start of the view is setting `ptr` to point to the - // new start and updating the `len` field to reflect the new length - // of the view. - self.ptr = self.ptr.offset(start as isize); - - if self.len >= start { - self.len -= start; - } else { - self.len = 0; - } - - self.cap -= start; - } - } - - unsafe fn set_end(&mut self, end: usize) { - debug_assert!(self.is_shared()); - - // Always check `inline` first, because if the handle is using inline - // data storage, all of the `Inner` struct fields will be gibberish. - if self.is_inline() { - assert!(end <= INLINE_CAP); - let new_len = cmp::min(self.inline_len(), end); - self.set_inline_len(new_len); - } else { - assert!(end <= self.cap); - - self.cap = end; - self.len = cmp::min(self.len, end); - } - } - - /// Checks if it is safe to mutate the memory - fn is_mut_safe(&mut self) -> bool { - let kind = self.kind(); - - // Always check `inline` first, because if the handle is using inline - // data storage, all of the `Inner` struct fields will be gibberish. - if kind == KIND_INLINE { - // Inlined buffers can always be mutated as the data is never shared - // across handles. - true - } else if kind == KIND_VEC { - true - } else if kind == KIND_STATIC { - false - } else { - // Otherwise, the underlying buffer is potentially shared with other - // handles, so the ref_count needs to be checked. - unsafe { (**self.arc.get_mut()).is_unique() } - } - } - - /// Increments the ref count. This should only be done if it is known that - /// it can be done safely. As such, this fn is not public, instead other - /// fns will use this one while maintaining the guarantees. - /// Parameter `mut_self` should only be set to `true` if caller holds - /// `&mut self` reference. - /// - /// "Safely" is defined as not exposing two `BytesMut` values that point to - /// the same byte window. - /// - /// This function is thread safe. - unsafe fn shallow_clone(&self, mut_self: bool) -> Inner { - // Always check `inline` first, because if the handle is using inline - // data storage, all of the `Inner` struct fields will be gibberish. - // - // Additionally, if kind is STATIC, then Arc is *never* changed, making - // it safe and faster to check for it now before an atomic acquire. - - if self.is_inline_or_static() { - // In this case, a shallow_clone still involves copying the data. - let mut inner: Inner = mem::uninitialized(); - ptr::copy_nonoverlapping( - self, - &mut inner, - 1, - ); - inner - } else { - self.shallow_clone_sync(mut_self) - } - } - - - #[cold] - unsafe fn shallow_clone_sync(&self, mut_self: bool) -> Inner { - // The function requires `&self`, this means that `shallow_clone` - // could be called concurrently. - // - // The first step is to load the value of `arc`. This will determine - // how to proceed. The `Acquire` ordering synchronizes with the - // `compare_and_swap` that comes later in this function. The goal is - // to ensure that if `arc` is currently set to point to a `Shared`, - // that the current thread acquires the associated memory. - let arc = self.arc.load(Acquire); - let kind = arc as usize & KIND_MASK; - - if kind == KIND_ARC { - self.shallow_clone_arc(arc) - } else { - assert!(kind == KIND_VEC); - self.shallow_clone_vec(arc as usize, mut_self) - } - } - - unsafe fn shallow_clone_arc(&self, arc: *mut Shared) -> Inner { - debug_assert!(arc as usize & KIND_MASK == KIND_ARC); - - let old_size = (*arc).ref_count.fetch_add(1, Relaxed); - - if old_size == usize::MAX { - abort(); - } - - Inner { - arc: AtomicPtr::new(arc), - .. *self - } - } - - #[cold] - unsafe fn shallow_clone_vec(&self, arc: usize, mut_self: bool) -> Inner { - // If the buffer is still tracked in a `Vec`. It is time to - // promote the vec to an `Arc`. This could potentially be called - // concurrently, so some care must be taken. - - debug_assert!(arc & KIND_MASK == KIND_VEC); - - let original_capacity_repr = - (arc as usize & ORIGINAL_CAPACITY_MASK) >> ORIGINAL_CAPACITY_OFFSET; - - // The vec offset cannot be concurrently mutated, so there - // should be no danger reading it. - let off = (arc as usize) >> VEC_POS_OFFSET; - - // First, allocate a new `Shared` instance containing the - // `Vec` fields. It's important to note that `ptr`, `len`, - // and `cap` cannot be mutated without having `&mut self`. - // This means that these fields will not be concurrently - // updated and since the buffer hasn't been promoted to an - // `Arc`, those three fields still are the components of the - // vector. - let shared = Box::new(Shared { - vec: rebuild_vec(self.ptr, self.len, self.cap, off), - original_capacity_repr: original_capacity_repr, - // Initialize refcount to 2. One for this reference, and one - // for the new clone that will be returned from - // `shallow_clone`. - ref_count: AtomicUsize::new(2), - }); - - let shared = Box::into_raw(shared); - - // The pointer should be aligned, so this assert should - // always succeed. - debug_assert!(0 == (shared as usize & 0b11)); - - // If there are no references to self in other threads, - // expensive atomic operations can be avoided. - if mut_self { - self.arc.store(shared, Relaxed); - return Inner { - arc: AtomicPtr::new(shared), - .. *self - }; - } - - // Try compare & swapping the pointer into the `arc` field. - // `Release` is used synchronize with other threads that - // will load the `arc` field. - // - // If the `compare_and_swap` fails, then the thread lost the - // race to promote the buffer to shared. The `Acquire` - // ordering will synchronize with the `compare_and_swap` - // that happened in the other thread and the `Shared` - // pointed to by `actual` will be visible. - let actual = self.arc.compare_and_swap(arc as *mut Shared, shared, AcqRel); - - if actual as usize == arc { - // The upgrade was successful, the new handle can be - // returned. - return Inner { - arc: AtomicPtr::new(shared), - .. *self - }; - } - - // The upgrade failed, a concurrent clone happened. Release - // the allocation that was made in this thread, it will not - // be needed. - let shared = Box::from_raw(shared); - mem::forget(*shared); - - // Buffer already promoted to shared storage, so increment ref - // count. - self.shallow_clone_arc(actual) - } - - #[inline] - fn reserve(&mut self, additional: usize) { - let len = self.len(); - let rem = self.capacity() - len; - - if additional <= rem { - // The handle can already store at least `additional` more bytes, so - // there is no further work needed to be done. - return; - } - - let kind = self.kind(); - - // Always check `inline` first, because if the handle is using inline - // data storage, all of the `Inner` struct fields will be gibberish. - if kind == KIND_INLINE { - let new_cap = len + additional; - - // Promote to a vector - let mut v = Vec::with_capacity(new_cap); - v.extend_from_slice(self.as_ref()); - - self.ptr = v.as_mut_ptr(); - self.len = v.len(); - self.cap = v.capacity(); - - // Since the minimum capacity is `INLINE_CAP`, don't bother encoding - // the original capacity as INLINE_CAP - self.arc = AtomicPtr::new(KIND_VEC as *mut Shared); - - mem::forget(v); - return; - } - - if kind == KIND_VEC { - // If there's enough free space before the start of the buffer, then - // just copy the data backwards and reuse the already-allocated - // space. - // - // Otherwise, since backed by a vector, use `Vec::reserve` - unsafe { - let (off, prev) = self.uncoordinated_get_vec_pos(); - - // Only reuse space if we stand to gain at least capacity/2 - // bytes of space back - if off >= additional && off >= (self.cap / 2) { - // There's space - reuse it - // - // Just move the pointer back to the start after copying - // data back. - let base_ptr = self.ptr.offset(-(off as isize)); - ptr::copy(self.ptr, base_ptr, self.len); - self.ptr = base_ptr; - self.uncoordinated_set_vec_pos(0, prev); - - // Length stays constant, but since we moved backwards we - // can gain capacity back. - self.cap += off; - } else { - // No space - allocate more - let mut v = rebuild_vec(self.ptr, self.len, self.cap, off); - v.reserve(additional); - - // Update the info - self.ptr = v.as_mut_ptr().offset(off as isize); - self.len = v.len() - off; - self.cap = v.capacity() - off; - - // Drop the vec reference - mem::forget(v); - } - return; - } - } - - let arc = *self.arc.get_mut(); - - debug_assert!(kind == KIND_ARC); - - // Reserving involves abandoning the currently shared buffer and - // allocating a new vector with the requested capacity. - // - // Compute the new capacity - let mut new_cap = len + additional; - let original_capacity; - let original_capacity_repr; - - unsafe { - original_capacity_repr = (*arc).original_capacity_repr; - original_capacity = original_capacity_from_repr(original_capacity_repr); - - // First, try to reclaim the buffer. This is possible if the current - // handle is the only outstanding handle pointing to the buffer. - if (*arc).is_unique() { - // This is the only handle to the buffer. It can be reclaimed. - // However, before doing the work of copying data, check to make - // sure that the vector has enough capacity. - let v = &mut (*arc).vec; - - if v.capacity() >= new_cap { - // The capacity is sufficient, reclaim the buffer - let ptr = v.as_mut_ptr(); - - ptr::copy(self.ptr, ptr, len); - - self.ptr = ptr; - self.cap = v.capacity(); - - return; - } - - // The vector capacity is not sufficient. The reserve request is - // asking for more than the initial buffer capacity. Allocate more - // than requested if `new_cap` is not much bigger than the current - // capacity. - // - // There are some situations, using `reserve_exact` that the - // buffer capacity could be below `original_capacity`, so do a - // check. - new_cap = cmp::max( - cmp::max(v.capacity() << 1, new_cap), - original_capacity); - } else { - new_cap = cmp::max(new_cap, original_capacity); - } - } - - // Create a new vector to store the data - let mut v = Vec::with_capacity(new_cap); - - // Copy the bytes - v.extend_from_slice(self.as_ref()); - - // Release the shared handle. This must be done *after* the bytes are - // copied. - release_shared(arc); - - // Update self - self.ptr = v.as_mut_ptr(); - self.len = v.len(); - self.cap = v.capacity(); - - let arc = (original_capacity_repr << ORIGINAL_CAPACITY_OFFSET) | KIND_VEC; - - self.arc = AtomicPtr::new(arc as *mut Shared); - - // Forget the vector handle - mem::forget(v); - } - - /// Returns true if the buffer is stored inline - #[inline] - fn is_inline(&self) -> bool { - self.kind() == KIND_INLINE - } - - #[inline] - fn is_inline_or_static(&self) -> bool { - // The value returned by `kind` isn't itself safe, but the value could - // inform what operations to take, and unsafely do something without - // synchronization. - // - // KIND_INLINE and KIND_STATIC will *never* change, so branches on that - // information is safe. - let kind = self.kind(); - kind == KIND_INLINE || kind == KIND_STATIC - } - - /// Used for `debug_assert` statements. &mut is used to guarantee that it is - /// safe to check VEC_KIND - #[inline] - fn is_shared(&mut self) -> bool { - match self.kind() { - KIND_VEC => false, - _ => true, - } - } - - /// Used for `debug_assert` statements - #[inline] - fn is_static(&mut self) -> bool { - match self.kind() { - KIND_STATIC => true, - _ => false, - } - } - - #[inline] - fn kind(&self) -> usize { - // This function is going to probably raise some eyebrows. The function - // returns true if the buffer is stored inline. This is done by checking - // the least significant bit in the `arc` field. - // - // Now, you may notice that `arc` is an `AtomicPtr` and this is - // accessing it as a normal field without performing an atomic load... - // - // Again, the function only cares about the least significant bit, and - // this bit is set when `Inner` is created and never changed after that. - // All platforms have atomic "word" operations and won't randomly flip - // bits, so even without any explicit atomic operations, reading the - // flag will be correct. - // - // This is undefind behavior due to a data race, but experimental - // evidence shows that it works in practice (discussion: - // https://internals.rust-lang.org/t/bit-wise-reasoning-for-atomic-accesses/8853). - // - // This function is very critical performance wise as it is called for - // every operation. Performing an atomic load would mess with the - // compiler's ability to optimize. Simple benchmarks show up to a 10% - // slowdown using a `Relaxed` atomic load on x86. - - #[cfg(target_endian = "little")] - #[inline] - fn imp(arc: &AtomicPtr) -> usize { - unsafe { - let p: *const u8 = mem::transmute(arc); - (*p as usize) & KIND_MASK - } - } - - #[cfg(target_endian = "big")] - #[inline] - fn imp(arc: &AtomicPtr) -> usize { - unsafe { - let p: *const usize = mem::transmute(arc); - *p & KIND_MASK - } - } - - imp(&self.arc) - } - - #[inline] - fn uncoordinated_get_vec_pos(&mut self) -> (usize, usize) { - // Similar to above, this is a pretty crazed function. This should only - // be called when in the KIND_VEC mode. This + the &mut self argument - // guarantees that there is no possibility of concurrent calls to this - // function. - let prev = unsafe { - let p: &AtomicPtr = &self.arc; - let p: *const usize = mem::transmute(p); - *p - }; - - (prev >> VEC_POS_OFFSET, prev) - } - - #[inline] - fn uncoordinated_set_vec_pos(&mut self, pos: usize, prev: usize) { - // Once more... crazy - debug_assert!(pos <= MAX_VEC_POS); - - unsafe { - let p: &mut AtomicPtr = &mut self.arc; - let p: &mut usize = mem::transmute(p); - *p = (pos << VEC_POS_OFFSET) | (prev & NOT_VEC_POS_MASK); - } - } -} - -fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) -> Vec { - unsafe { - let ptr = ptr.offset(-(off as isize)); - len += off; - cap += off; - - Vec::from_raw_parts(ptr, len, cap) - } -} - -impl Drop for Inner { - fn drop(&mut self) { - let kind = self.kind(); - - if kind == KIND_VEC { - let (off, _) = self.uncoordinated_get_vec_pos(); - - // Vector storage, free the vector - let _ = rebuild_vec(self.ptr, self.len, self.cap, off); - } else if kind == KIND_ARC { - release_shared(*self.arc.get_mut()); - } - } -} - -fn release_shared(ptr: *mut Shared) { - // `Shared` storage... follow the drop steps from Arc. - unsafe { - if (*ptr).ref_count.fetch_sub(1, Release) != 1 { - return; - } - - // This fence is needed to prevent reordering of use of the data and - // deletion of the data. Because it is marked `Release`, the decreasing - // of the reference count synchronizes with this `Acquire` fence. This - // means that use of the data happens before decreasing the reference - // count, which happens before this fence, which happens before the - // deletion of the data. - // - // As explained in the [Boost documentation][1], - // - // > It is important to enforce any possible access to the object in one - // > thread (through an existing reference) to *happen before* deleting - // > the object in a different thread. This is achieved by a "release" - // > operation after dropping a reference (any access to the object - // > through this reference must obviously happened before), and an - // > "acquire" operation before deleting the object. - // - // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) - atomic::fence(Acquire); - - // Drop the data - Box::from_raw(ptr); - } -} - -impl Shared { - fn is_unique(&self) -> bool { - // The goal is to check if the current handle is the only handle - // that currently has access to the buffer. This is done by - // checking if the `ref_count` is currently 1. - // - // The `Acquire` ordering synchronizes with the `Release` as - // part of the `fetch_sub` in `release_shared`. The `fetch_sub` - // operation guarantees that any mutations done in other threads - // are ordered before the `ref_count` is decremented. As such, - // this `Acquire` will guarantee that those mutations are - // visible to the current thread. - self.ref_count.load(Acquire) == 1 - } -} - -fn original_capacity_to_repr(cap: usize) -> usize { - let width = PTR_WIDTH - ((cap >> MIN_ORIGINAL_CAPACITY_WIDTH).leading_zeros() as usize); - cmp::min(width, MAX_ORIGINAL_CAPACITY_WIDTH - MIN_ORIGINAL_CAPACITY_WIDTH) -} - -fn original_capacity_from_repr(repr: usize) -> usize { - if repr == 0 { - return 0; - } - - 1 << (repr + (MIN_ORIGINAL_CAPACITY_WIDTH - 1)) -} - -#[test] -fn test_original_capacity_to_repr() { - assert_eq!(original_capacity_to_repr(0), 0); - - let max_width = 32; - - for width in 1..(max_width + 1) { - let cap = 1 << width - 1; - - let expected = if width < MIN_ORIGINAL_CAPACITY_WIDTH { - 0 - } else if width < MAX_ORIGINAL_CAPACITY_WIDTH { - width - MIN_ORIGINAL_CAPACITY_WIDTH - } else { - MAX_ORIGINAL_CAPACITY_WIDTH - MIN_ORIGINAL_CAPACITY_WIDTH - }; - - assert_eq!(original_capacity_to_repr(cap), expected); - - if width > 1 { - assert_eq!(original_capacity_to_repr(cap + 1), expected); - } - - // MIN_ORIGINAL_CAPACITY_WIDTH must be bigger than 7 to pass tests below - if width == MIN_ORIGINAL_CAPACITY_WIDTH + 1 { - assert_eq!(original_capacity_to_repr(cap - 24), expected - 1); - assert_eq!(original_capacity_to_repr(cap + 76), expected); - } else if width == MIN_ORIGINAL_CAPACITY_WIDTH + 2 { - assert_eq!(original_capacity_to_repr(cap - 1), expected - 1); - assert_eq!(original_capacity_to_repr(cap - 48), expected - 1); - } - } -} - -#[test] -fn test_original_capacity_from_repr() { - assert_eq!(0, original_capacity_from_repr(0)); - - let min_cap = 1 << MIN_ORIGINAL_CAPACITY_WIDTH; - - assert_eq!(min_cap, original_capacity_from_repr(1)); - assert_eq!(min_cap * 2, original_capacity_from_repr(2)); - assert_eq!(min_cap * 4, original_capacity_from_repr(3)); - assert_eq!(min_cap * 8, original_capacity_from_repr(4)); - assert_eq!(min_cap * 16, original_capacity_from_repr(5)); - assert_eq!(min_cap * 32, original_capacity_from_repr(6)); - assert_eq!(min_cap * 64, original_capacity_from_repr(7)); -} - -unsafe impl Send for Inner {} -unsafe impl Sync for Inner {} - -/* - * - * ===== PartialEq / PartialOrd ===== - * - */ - -impl PartialEq<[u8]> for BytesMut { - fn eq(&self, other: &[u8]) -> bool { - &**self == other - } -} - -impl PartialOrd<[u8]> for BytesMut { - fn partial_cmp(&self, other: &[u8]) -> Option { - (**self).partial_cmp(other) - } -} - -impl PartialEq for [u8] { - fn eq(&self, other: &BytesMut) -> bool { - *other == *self - } -} - -impl PartialOrd for [u8] { - fn partial_cmp(&self, other: &BytesMut) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq for BytesMut { - fn eq(&self, other: &str) -> bool { - &**self == other.as_bytes() - } -} - -impl PartialOrd for BytesMut { - fn partial_cmp(&self, other: &str) -> Option { - (**self).partial_cmp(other.as_bytes()) - } -} - -impl PartialEq for str { - fn eq(&self, other: &BytesMut) -> bool { - *other == *self - } -} - -impl PartialOrd for str { - fn partial_cmp(&self, other: &BytesMut) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq> for BytesMut { - fn eq(&self, other: &Vec) -> bool { - *self == &other[..] - } -} - -impl PartialOrd> for BytesMut { - fn partial_cmp(&self, other: &Vec) -> Option { - (**self).partial_cmp(&other[..]) - } -} - -impl PartialEq for Vec { - fn eq(&self, other: &BytesMut) -> bool { - *other == *self - } -} - -impl PartialOrd for Vec { - fn partial_cmp(&self, other: &BytesMut) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq for BytesMut { - fn eq(&self, other: &String) -> bool { - *self == &other[..] - } -} - -impl PartialOrd for BytesMut { - fn partial_cmp(&self, other: &String) -> Option { - (**self).partial_cmp(other.as_bytes()) - } -} - -impl PartialEq for String { - fn eq(&self, other: &BytesMut) -> bool { - *other == *self - } -} - -impl PartialOrd for String { - fn partial_cmp(&self, other: &BytesMut) -> Option { - other.partial_cmp(self) - } -} - -impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut - where BytesMut: PartialEq -{ - fn eq(&self, other: &&'a T) -> bool { - *self == **other - } -} - -impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut - where BytesMut: PartialOrd -{ - fn partial_cmp(&self, other: &&'a T) -> Option { - self.partial_cmp(*other) - } -} - -impl<'a> PartialEq for &'a [u8] { - fn eq(&self, other: &BytesMut) -> bool { - *other == *self - } -} - -impl<'a> PartialOrd for &'a [u8] { - fn partial_cmp(&self, other: &BytesMut) -> Option { - other.partial_cmp(self) - } -} - -impl<'a> PartialEq for &'a str { - fn eq(&self, other: &BytesMut) -> bool { - *other == *self - } -} - -impl<'a> PartialOrd for &'a str { - fn partial_cmp(&self, other: &BytesMut) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq<[u8]> for Bytes { - fn eq(&self, other: &[u8]) -> bool { - self.inner.as_ref() == other - } -} - -impl PartialOrd<[u8]> for Bytes { - fn partial_cmp(&self, other: &[u8]) -> Option { - self.inner.as_ref().partial_cmp(other) - } -} - -impl PartialEq for [u8] { - fn eq(&self, other: &Bytes) -> bool { - *other == *self - } -} - -impl PartialOrd for [u8] { - fn partial_cmp(&self, other: &Bytes) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq for Bytes { - fn eq(&self, other: &str) -> bool { - self.inner.as_ref() == other.as_bytes() - } -} - -impl PartialOrd for Bytes { - fn partial_cmp(&self, other: &str) -> Option { - self.inner.as_ref().partial_cmp(other.as_bytes()) - } -} - -impl PartialEq for str { - fn eq(&self, other: &Bytes) -> bool { - *other == *self - } -} - -impl PartialOrd for str { - fn partial_cmp(&self, other: &Bytes) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq> for Bytes { - fn eq(&self, other: &Vec) -> bool { - *self == &other[..] - } -} - -impl PartialOrd> for Bytes { - fn partial_cmp(&self, other: &Vec) -> Option { - self.inner.as_ref().partial_cmp(&other[..]) - } -} - -impl PartialEq for Vec { - fn eq(&self, other: &Bytes) -> bool { - *other == *self - } -} - -impl PartialOrd for Vec { - fn partial_cmp(&self, other: &Bytes) -> Option { - other.partial_cmp(self) - } -} - -impl PartialEq for Bytes { - fn eq(&self, other: &String) -> bool { - *self == &other[..] - } -} - -impl PartialOrd for Bytes { - fn partial_cmp(&self, other: &String) -> Option { - self.inner.as_ref().partial_cmp(other.as_bytes()) - } -} - -impl PartialEq for String { - fn eq(&self, other: &Bytes) -> bool { - *other == *self - } -} - -impl PartialOrd for String { - fn partial_cmp(&self, other: &Bytes) -> Option { - other.partial_cmp(self) - } -} - -impl<'a> PartialEq for &'a [u8] { - fn eq(&self, other: &Bytes) -> bool { - *other == *self - } -} - -impl<'a> PartialOrd for &'a [u8] { - fn partial_cmp(&self, other: &Bytes) -> Option { - other.partial_cmp(self) - } -} - -impl<'a> PartialEq for &'a str { - fn eq(&self, other: &Bytes) -> bool { - *other == *self - } -} - -impl<'a> PartialOrd for &'a str { - fn partial_cmp(&self, other: &Bytes) -> Option { - other.partial_cmp(self) - } -} - -impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes - where Bytes: PartialEq -{ - fn eq(&self, other: &&'a T) -> bool { - *self == **other - } -} - -impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes - where Bytes: PartialOrd -{ - fn partial_cmp(&self, other: &&'a T) -> Option { - self.partial_cmp(&**other) - } -} - -impl PartialEq for Bytes -{ - fn eq(&self, other: &BytesMut) -> bool { - &other[..] == &self[..] - } -} - -impl PartialEq for BytesMut -{ - fn eq(&self, other: &Bytes) -> bool { - &other[..] == &self[..] - } -} - -// While there is `std::process:abort`, it's only available in Rust 1.17, and -// our minimum supported version is currently 1.15. So, this acts as an abort -// by triggering a double panic, which always aborts in Rust. -struct Abort; - -impl Drop for Abort { - fn drop(&mut self) { - panic!(); - } -} - -#[inline(never)] -#[cold] -fn abort() { - let _a = Abort; - panic!(); -} diff --git a/third_party/rust/bytes/v0_4/crate/src/debug.rs b/third_party/rust/bytes/v0_4/crate/src/debug.rs deleted file mode 100644 index f8b830a24110..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/debug.rs +++ /dev/null @@ -1,40 +0,0 @@ -use std::fmt; - -/// Alternative implementation of `fmt::Debug` for byte slice. -/// -/// Standard `Debug` implementation for `[u8]` is comma separated -/// list of numbers. Since large amount of byte strings are in fact -/// ASCII strings or contain a lot of ASCII strings (e. g. HTTP), -/// it is convenient to print strings as ASCII when possible. -/// -/// This struct wraps `&[u8]` just to override `fmt::Debug`. -/// -/// `BsDebug` is not a part of public API of bytes crate. -pub struct BsDebug<'a>(pub &'a [u8]); - -impl<'a> fmt::Debug for BsDebug<'a> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { - try!(write!(fmt, "b\"")); - for &c in self.0 { - // https://doc.rust-lang.org/reference.html#byte-escapes - if c == b'\n' { - try!(write!(fmt, "\\n")); - } else if c == b'\r' { - try!(write!(fmt, "\\r")); - } else if c == b'\t' { - try!(write!(fmt, "\\t")); - } else if c == b'\\' || c == b'"' { - try!(write!(fmt, "\\{}", c as char)); - } else if c == b'\0' { - try!(write!(fmt, "\\0")); - // ASCII printable - } else if c >= 0x20 && c < 0x7f { - try!(write!(fmt, "{}", c as char)); - } else { - try!(write!(fmt, "\\x{:02x}", c)); - } - } - try!(write!(fmt, "\"")); - Ok(()) - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/either.rs b/third_party/rust/bytes/v0_4/crate/src/either.rs deleted file mode 100644 index 53a2775996c2..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/either.rs +++ /dev/null @@ -1,89 +0,0 @@ -extern crate either; - -use {Buf, BufMut}; - -use self::either::Either; -use self::either::Either::*; -use iovec::IoVec; - -impl Buf for Either -where - L: Buf, - R: Buf, -{ - fn remaining(&self) -> usize { - match *self { - Left(ref b) => b.remaining(), - Right(ref b) => b.remaining(), - } - } - - fn bytes(&self) -> &[u8] { - match *self { - Left(ref b) => b.bytes(), - Right(ref b) => b.bytes(), - } - } - - fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize { - match *self { - Left(ref b) => b.bytes_vec(dst), - Right(ref b) => b.bytes_vec(dst), - } - } - - fn advance(&mut self, cnt: usize) { - match *self { - Left(ref mut b) => b.advance(cnt), - Right(ref mut b) => b.advance(cnt), - } - } - - fn copy_to_slice(&mut self, dst: &mut [u8]) { - match *self { - Left(ref mut b) => b.copy_to_slice(dst), - Right(ref mut b) => b.copy_to_slice(dst), - } - } -} - -impl BufMut for Either -where - L: BufMut, - R: BufMut, -{ - fn remaining_mut(&self) -> usize { - match *self { - Left(ref b) => b.remaining_mut(), - Right(ref b) => b.remaining_mut(), - } - } - - unsafe fn bytes_mut(&mut self) -> &mut [u8] { - match *self { - Left(ref mut b) => b.bytes_mut(), - Right(ref mut b) => b.bytes_mut(), - } - } - - unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize { - match *self { - Left(ref mut b) => b.bytes_vec_mut(dst), - Right(ref mut b) => b.bytes_vec_mut(dst), - } - } - - unsafe fn advance_mut(&mut self, cnt: usize) { - match *self { - Left(ref mut b) => b.advance_mut(cnt), - Right(ref mut b) => b.advance_mut(cnt), - } - } - - fn put_slice(&mut self, src: &[u8]) { - match *self { - Left(ref mut b) => b.put_slice(src), - Right(ref mut b) => b.put_slice(src), - } - } -} diff --git a/third_party/rust/bytes/v0_4/crate/src/lib.rs b/third_party/rust/bytes/v0_4/crate/src/lib.rs deleted file mode 100644 index a4f1573e0771..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/lib.rs +++ /dev/null @@ -1,105 +0,0 @@ -//! Provides abstractions for working with bytes. -//! -//! The `bytes` crate provides an efficient byte buffer structure -//! ([`Bytes`](struct.Bytes.html)) and traits for working with buffer -//! implementations ([`Buf`], [`BufMut`]). -//! -//! [`Buf`]: trait.Buf.html -//! [`BufMut`]: trait.BufMut.html -//! -//! # `Bytes` -//! -//! `Bytes` is an efficient container for storing and operating on continguous -//! slices of memory. It is intended for use primarily in networking code, but -//! could have applications elsewhere as well. -//! -//! `Bytes` values facilitate zero-copy network programming by allowing multiple -//! `Bytes` objects to point to the same underlying memory. This is managed by -//! using a reference count to track when the memory is no longer needed and can -//! be freed. -//! -//! A `Bytes` handle can be created directly from an existing byte store (such as `&[u8]` -//! or `Vec`), but usually a `BytesMut` is used first and written to. For -//! example: -//! -//! ```rust -//! use bytes::{BytesMut, BufMut, BigEndian}; -//! -//! let mut buf = BytesMut::with_capacity(1024); -//! buf.put(&b"hello world"[..]); -//! buf.put_u16::(1234); -//! -//! let a = buf.take(); -//! assert_eq!(a, b"hello world\x04\xD2"[..]); -//! -//! buf.put(&b"goodbye world"[..]); -//! -//! let b = buf.take(); -//! assert_eq!(b, b"goodbye world"[..]); -//! -//! assert_eq!(buf.capacity(), 998); -//! ``` -//! -//! In the above example, only a single buffer of 1024 is allocated. The handles -//! `a` and `b` will share the underlying buffer and maintain indices tracking -//! the view into the buffer represented by the handle. -//! -//! See the [struct docs] for more details. -//! -//! [struct docs]: struct.Bytes.html -//! -//! # `Buf`, `BufMut` -//! -//! These two traits provide read and write access to buffers. The underlying -//! storage may or may not be in contiguous memory. For example, `Bytes` is a -//! buffer that guarantees contiguous memory, but a [rope] stores the bytes in -//! disjoint chunks. `Buf` and `BufMut` maintain cursors tracking the current -//! position in the underlying byte storage. When bytes are read or written, the -//! cursor is advanced. -//! -//! [rope]: https://en.wikipedia.org/wiki/Rope_(data_structure) -//! -//! ## Relation with `Read` and `Write` -//! -//! At first glance, it may seem that `Buf` and `BufMut` overlap in -//! functionality with `std::io::Read` and `std::io::Write`. However, they -//! serve different purposes. A buffer is the value that is provided as an -//! argument to `Read::read` and `Write::write`. `Read` and `Write` may then -//! perform a syscall, which has the potential of failing. Operations on `Buf` -//! and `BufMut` are infallible. - -#![deny(warnings, missing_docs, missing_debug_implementations)] -#![doc(html_root_url = "https://docs.rs/bytes/0.4.12")] - -extern crate byteorder; -extern crate iovec; - -pub mod buf; -pub use buf::{ - Buf, - BufMut, - IntoBuf, -}; -#[deprecated(since = "0.4.1", note = "moved to `buf` module")] -#[doc(hidden)] -pub use buf::{ - Reader, - Writer, - Take, -}; - -mod bytes; -mod debug; -pub use bytes::{Bytes, BytesMut}; - -#[deprecated] -pub use byteorder::{ByteOrder, BigEndian, LittleEndian}; - -// Optional Serde support -#[cfg(feature = "serde")] -#[doc(hidden)] -pub mod serde; - -// Optional `Either` support -#[cfg(feature = "either")] -mod either; diff --git a/third_party/rust/bytes/v0_4/crate/src/serde.rs b/third_party/rust/bytes/v0_4/crate/src/serde.rs deleted file mode 100644 index d45caff05196..000000000000 --- a/third_party/rust/bytes/v0_4/crate/src/serde.rs +++ /dev/null @@ -1,82 +0,0 @@ -extern crate serde; - -use std::{cmp, fmt}; -use self::serde::{Serialize, Serializer, Deserialize, Deserializer, de}; -use super::{Bytes, BytesMut}; - -macro_rules! serde_impl { - ($ty:ident, $visitor_ty:ident) => ( - impl Serialize for $ty { - #[inline] - fn serialize(&self, serializer: S) -> Result - where S: Serializer - { - serializer.serialize_bytes(&self) - } - } - - struct $visitor_ty; - - impl<'de> de::Visitor<'de> for $visitor_ty { - type Value = $ty; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("byte array") - } - - #[inline] - fn visit_seq(self, mut seq: V) -> Result - where V: de::SeqAccess<'de> - { - let len = cmp::min(seq.size_hint().unwrap_or(0), 4096); - let mut values = Vec::with_capacity(len); - - while let Some(value) = try!(seq.next_element()) { - values.push(value); - } - - Ok(values.into()) - } - - #[inline] - fn visit_bytes(self, v: &[u8]) -> Result - where E: de::Error - { - Ok($ty::from(v)) - } - - #[inline] - fn visit_byte_buf(self, v: Vec) -> Result - where E: de::Error - { - Ok($ty::from(v)) - } - - #[inline] - fn visit_str(self, v: &str) -> Result - where E: de::Error - { - Ok($ty::from(v)) - } - - #[inline] - fn visit_string(self, v: String) -> Result - where E: de::Error - { - Ok($ty::from(v)) - } - } - - impl<'de> Deserialize<'de> for $ty { - #[inline] - fn deserialize(deserializer: D) -> Result<$ty, D::Error> - where D: Deserializer<'de> - { - deserializer.deserialize_byte_buf($visitor_ty) - } - } - ); -} - -serde_impl!(Bytes, BytesVisitor); -serde_impl!(BytesMut, BytesMutVisitor); diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_buf.rs b/third_party/rust/bytes/v0_4/crate/tests/test_buf.rs deleted file mode 100644 index f25c25f2b5de..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_buf.rs +++ /dev/null @@ -1,58 +0,0 @@ -extern crate bytes; -extern crate byteorder; -extern crate iovec; - -use bytes::Buf; -use iovec::IoVec; -use std::io::Cursor; - -#[test] -fn test_fresh_cursor_vec() { - let mut buf = Cursor::new(b"hello".to_vec()); - - assert_eq!(buf.remaining(), 5); - assert_eq!(buf.bytes(), b"hello"); - - buf.advance(2); - - assert_eq!(buf.remaining(), 3); - assert_eq!(buf.bytes(), b"llo"); - - buf.advance(3); - - assert_eq!(buf.remaining(), 0); - assert_eq!(buf.bytes(), b""); -} - -#[test] -fn test_get_u8() { - let mut buf = Cursor::new(b"\x21zomg"); - assert_eq!(0x21, buf.get_u8()); -} - -#[test] -fn test_get_u16() { - let buf = b"\x21\x54zomg"; - assert_eq!(0x2154, Cursor::new(buf).get_u16_be()); - assert_eq!(0x5421, Cursor::new(buf).get_u16_le()); -} - -#[test] -#[should_panic] -fn test_get_u16_buffer_underflow() { - let mut buf = Cursor::new(b"\x21"); - buf.get_u16_be(); -} - -#[test] -fn test_bufs_vec() { - let buf = Cursor::new(b"hello world"); - - let b1: &[u8] = &mut [0]; - let b2: &[u8] = &mut [0]; - - let mut dst: [&IoVec; 2] = - [b1.into(), b2.into()]; - - assert_eq!(1, buf.bytes_vec(&mut dst[..])); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_buf_mut.rs b/third_party/rust/bytes/v0_4/crate/tests/test_buf_mut.rs deleted file mode 100644 index 2c8faa1043fb..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_buf_mut.rs +++ /dev/null @@ -1,83 +0,0 @@ -extern crate bytes; -extern crate byteorder; -extern crate iovec; - -use bytes::{BufMut, BytesMut}; -use iovec::IoVec; -use std::usize; -use std::fmt::Write; - -#[test] -fn test_vec_as_mut_buf() { - let mut buf = Vec::with_capacity(64); - - assert_eq!(buf.remaining_mut(), usize::MAX); - - unsafe { - assert!(buf.bytes_mut().len() >= 64); - } - - buf.put(&b"zomg"[..]); - - assert_eq!(&buf, b"zomg"); - - assert_eq!(buf.remaining_mut(), usize::MAX - 4); - assert_eq!(buf.capacity(), 64); - - for _ in 0..16 { - buf.put(&b"zomg"[..]); - } - - assert_eq!(buf.len(), 68); -} - -#[test] -fn test_put_u8() { - let mut buf = Vec::with_capacity(8); - buf.put::(33); - assert_eq!(b"\x21", &buf[..]); -} - -#[test] -fn test_put_u16() { - let mut buf = Vec::with_capacity(8); - buf.put_u16_be(8532); - assert_eq!(b"\x21\x54", &buf[..]); - - buf.clear(); - buf.put_u16_le(8532); - assert_eq!(b"\x54\x21", &buf[..]); -} - -#[test] -fn test_vec_advance_mut() { - // Regression test for carllerche/bytes#108. - let mut buf = Vec::with_capacity(8); - unsafe { - buf.advance_mut(12); - assert_eq!(buf.len(), 12); - assert!(buf.capacity() >= 12, "capacity: {}", buf.capacity()); - } -} - -#[test] -fn test_clone() { - let mut buf = BytesMut::with_capacity(100); - buf.write_str("this is a test").unwrap(); - let buf2 = buf.clone(); - - buf.write_str(" of our emergecy broadcast system").unwrap(); - assert!(buf != buf2); -} - -#[test] -fn test_bufs_vec_mut() { - use std::mem; - - let mut buf = BytesMut::from(&b"hello world"[..]); - - unsafe { - let mut dst: [&mut IoVec; 2] = mem::zeroed(); - assert_eq!(1, buf.bytes_vec_mut(&mut dst[..])); - } -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_bytes.rs b/third_party/rust/bytes/v0_4/crate/tests/test_bytes.rs deleted file mode 100644 index 4cf340e6a190..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_bytes.rs +++ /dev/null @@ -1,773 +0,0 @@ -extern crate bytes; - -use bytes::{Bytes, BytesMut, BufMut, IntoBuf}; - -const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb"; -const SHORT: &'static [u8] = b"hello world"; - -fn inline_cap() -> usize { - use std::mem; - 4 * mem::size_of::() - 1 -} - -fn is_sync() {} -fn is_send() {} - -#[test] -fn test_bounds() { - is_sync::(); - is_sync::(); - is_send::(); - is_send::(); -} - -#[test] -fn from_slice() { - let a = Bytes::from(&b"abcdefgh"[..]); - assert_eq!(a, b"abcdefgh"[..]); - assert_eq!(a, &b"abcdefgh"[..]); - assert_eq!(a, Vec::from(&b"abcdefgh"[..])); - assert_eq!(b"abcdefgh"[..], a); - assert_eq!(&b"abcdefgh"[..], a); - assert_eq!(Vec::from(&b"abcdefgh"[..]), a); - - let a = BytesMut::from(&b"abcdefgh"[..]); - assert_eq!(a, b"abcdefgh"[..]); - assert_eq!(a, &b"abcdefgh"[..]); - assert_eq!(a, Vec::from(&b"abcdefgh"[..])); - assert_eq!(b"abcdefgh"[..], a); - assert_eq!(&b"abcdefgh"[..], a); - assert_eq!(Vec::from(&b"abcdefgh"[..]), a); -} - -#[test] -fn fmt() { - let a = format!("{:?}", Bytes::from(&b"abcdefg"[..])); - let b = "b\"abcdefg\""; - - assert_eq!(a, b); - - let a = format!("{:?}", BytesMut::from(&b"abcdefg"[..])); - assert_eq!(a, b); -} - -#[test] -fn fmt_write() { - use std::fmt::Write; - use std::iter::FromIterator; - let s = String::from_iter((0..10).map(|_| "abcdefg")); - - let mut a = BytesMut::with_capacity(64); - write!(a, "{}", &s[..64]).unwrap(); - assert_eq!(a, s[..64].as_bytes()); - - - let mut b = BytesMut::with_capacity(64); - write!(b, "{}", &s[..32]).unwrap(); - write!(b, "{}", &s[32..64]).unwrap(); - assert_eq!(b, s[..64].as_bytes()); - - - let mut c = BytesMut::with_capacity(64); - write!(c, "{}", s).unwrap_err(); - assert!(c.is_empty()); -} - -#[test] -fn len() { - let a = Bytes::from(&b"abcdefg"[..]); - assert_eq!(a.len(), 7); - - let a = BytesMut::from(&b"abcdefg"[..]); - assert_eq!(a.len(), 7); - - let a = Bytes::from(&b""[..]); - assert!(a.is_empty()); - - let a = BytesMut::from(&b""[..]); - assert!(a.is_empty()); -} - -#[test] -fn index() { - let a = Bytes::from(&b"hello world"[..]); - assert_eq!(a[0..5], *b"hello"); -} - -#[test] -fn slice() { - let a = Bytes::from(&b"hello world"[..]); - - let b = a.slice(3, 5); - assert_eq!(b, b"lo"[..]); - - let b = a.slice(0, 0); - assert_eq!(b, b""[..]); - - let b = a.slice(3, 3); - assert_eq!(b, b""[..]); - - let b = a.slice(a.len(), a.len()); - assert_eq!(b, b""[..]); - - let b = a.slice_to(5); - assert_eq!(b, b"hello"[..]); - - let b = a.slice_from(3); - assert_eq!(b, b"lo world"[..]); -} - -#[test] -#[should_panic] -fn slice_oob_1() { - let a = Bytes::from(&b"hello world"[..]); - a.slice(5, inline_cap() + 1); -} - -#[test] -#[should_panic] -fn slice_oob_2() { - let a = Bytes::from(&b"hello world"[..]); - a.slice(inline_cap() + 1, inline_cap() + 5); -} - -#[test] -fn split_off() { - let mut hello = Bytes::from(&b"helloworld"[..]); - let world = hello.split_off(5); - - assert_eq!(hello, &b"hello"[..]); - assert_eq!(world, &b"world"[..]); - - let mut hello = BytesMut::from(&b"helloworld"[..]); - let world = hello.split_off(5); - - assert_eq!(hello, &b"hello"[..]); - assert_eq!(world, &b"world"[..]); -} - -#[test] -#[should_panic] -fn split_off_oob() { - let mut hello = Bytes::from(&b"helloworld"[..]); - hello.split_off(inline_cap() + 1); -} - -#[test] -fn split_off_uninitialized() { - let mut bytes = BytesMut::with_capacity(1024); - let other = bytes.split_off(128); - - assert_eq!(bytes.len(), 0); - assert_eq!(bytes.capacity(), 128); - - assert_eq!(other.len(), 0); - assert_eq!(other.capacity(), 896); -} - -#[test] -fn split_off_to_loop() { - let s = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - for i in 0..(s.len() + 1) { - { - let mut bytes = Bytes::from(&s[..]); - let off = bytes.split_off(i); - assert_eq!(i, bytes.len()); - let mut sum = Vec::new(); - sum.extend(&bytes); - sum.extend(&off); - assert_eq!(&s[..], &sum[..]); - } - { - let mut bytes = BytesMut::from(&s[..]); - let off = bytes.split_off(i); - assert_eq!(i, bytes.len()); - let mut sum = Vec::new(); - sum.extend(&bytes); - sum.extend(&off); - assert_eq!(&s[..], &sum[..]); - } - { - let mut bytes = Bytes::from(&s[..]); - let off = bytes.split_to(i); - assert_eq!(i, off.len()); - let mut sum = Vec::new(); - sum.extend(&off); - sum.extend(&bytes); - assert_eq!(&s[..], &sum[..]); - } - { - let mut bytes = BytesMut::from(&s[..]); - let off = bytes.split_to(i); - assert_eq!(i, off.len()); - let mut sum = Vec::new(); - sum.extend(&off); - sum.extend(&bytes); - assert_eq!(&s[..], &sum[..]); - } - } -} - -#[test] -fn split_to_1() { - // Inline - let mut a = Bytes::from(SHORT); - let b = a.split_to(4); - - assert_eq!(SHORT[4..], a); - assert_eq!(SHORT[..4], b); - - // Allocated - let mut a = Bytes::from(LONG); - let b = a.split_to(4); - - assert_eq!(LONG[4..], a); - assert_eq!(LONG[..4], b); - - let mut a = Bytes::from(LONG); - let b = a.split_to(30); - - assert_eq!(LONG[30..], a); - assert_eq!(LONG[..30], b); -} - -#[test] -fn split_to_2() { - let mut a = Bytes::from(LONG); - assert_eq!(LONG, a); - - let b = a.split_to(1); - - assert_eq!(LONG[1..], a); - drop(b); -} - -#[test] -#[should_panic] -fn split_to_oob() { - let mut hello = Bytes::from(&b"helloworld"[..]); - hello.split_to(inline_cap() + 1); -} - -#[test] -#[should_panic] -fn split_to_oob_mut() { - let mut hello = BytesMut::from(&b"helloworld"[..]); - hello.split_to(inline_cap() + 1); -} - -#[test] -fn split_to_uninitialized() { - let mut bytes = BytesMut::with_capacity(1024); - let other = bytes.split_to(128); - - assert_eq!(bytes.len(), 0); - assert_eq!(bytes.capacity(), 896); - - assert_eq!(other.len(), 0); - assert_eq!(other.capacity(), 128); -} - -#[test] -fn split_off_to_at_gt_len() { - fn make_bytes() -> Bytes { - let mut bytes = BytesMut::with_capacity(100); - bytes.put_slice(&[10, 20, 30, 40]); - bytes.freeze() - } - - use std::panic; - - make_bytes().split_to(4); - make_bytes().split_off(4); - - assert!(panic::catch_unwind(move || { - make_bytes().split_to(5); - }).is_err()); - - assert!(panic::catch_unwind(move || { - make_bytes().split_off(5); - }).is_err()); -} - -#[test] -fn fns_defined_for_bytes_mut() { - let mut bytes = BytesMut::from(&b"hello world"[..]); - - bytes.as_ptr(); - bytes.as_mut_ptr(); - - // Iterator - let v: Vec = bytes.iter().map(|b| *b).collect(); - assert_eq!(&v[..], bytes); -} - -#[test] -fn mut_into_buf() { - let mut v = vec![0, 0, 0, 0]; - let s = &mut v[..]; - s.into_buf().put_u32_le(42); -} - -#[test] -fn reserve_convert() { - // Inline -> Vec - let mut bytes = BytesMut::with_capacity(8); - bytes.put("hello"); - bytes.reserve(40); - assert_eq!(bytes.capacity(), 45); - assert_eq!(bytes, "hello"); - - // Inline -> Inline - let mut bytes = BytesMut::with_capacity(inline_cap()); - bytes.put("abcdefghijkl"); - - let a = bytes.split_to(10); - bytes.reserve(inline_cap() - 3); - assert_eq!(inline_cap(), bytes.capacity()); - - assert_eq!(bytes, "kl"); - assert_eq!(a, "abcdefghij"); - - // Vec -> Vec - let mut bytes = BytesMut::from(LONG); - bytes.reserve(64); - assert_eq!(bytes.capacity(), LONG.len() + 64); - - // Arc -> Vec - let mut bytes = BytesMut::from(LONG); - let a = bytes.split_to(30); - - bytes.reserve(128); - assert!(bytes.capacity() >= bytes.len() + 128); - - drop(a); -} - -#[test] -fn reserve_growth() { - let mut bytes = BytesMut::with_capacity(64); - bytes.put("hello world"); - let _ = bytes.take(); - - bytes.reserve(65); - assert_eq!(bytes.capacity(), 128); -} - -#[test] -fn reserve_allocates_at_least_original_capacity() { - let mut bytes = BytesMut::with_capacity(1024); - - for i in 0..1020 { - bytes.put(i as u8); - } - - let _other = bytes.take(); - - bytes.reserve(16); - assert_eq!(bytes.capacity(), 1024); -} - -#[test] -fn reserve_max_original_capacity_value() { - const SIZE: usize = 128 * 1024; - - let mut bytes = BytesMut::with_capacity(SIZE); - - for _ in 0..SIZE { - bytes.put(0u8); - } - - let _other = bytes.take(); - - bytes.reserve(16); - assert_eq!(bytes.capacity(), 64 * 1024); -} - -// Without either looking at the internals of the BytesMut or doing weird stuff -// with the memory allocator, there's no good way to automatically verify from -// within the program that this actually recycles memory. Instead, just exercise -// the code path to ensure that the results are correct. -#[test] -fn reserve_vec_recycling() { - let mut bytes = BytesMut::from(Vec::with_capacity(16)); - assert_eq!(bytes.capacity(), 16); - bytes.put("0123456789012345"); - bytes.advance(10); - assert_eq!(bytes.capacity(), 6); - bytes.reserve(8); - assert_eq!(bytes.capacity(), 16); -} - -#[test] -fn reserve_in_arc_unique_does_not_overallocate() { - let mut bytes = BytesMut::with_capacity(1000); - bytes.take(); - - // now bytes is Arc and refcount == 1 - - assert_eq!(1000, bytes.capacity()); - bytes.reserve(2001); - assert_eq!(2001, bytes.capacity()); -} - -#[test] -fn reserve_in_arc_unique_doubles() { - let mut bytes = BytesMut::with_capacity(1000); - bytes.take(); - - // now bytes is Arc and refcount == 1 - - assert_eq!(1000, bytes.capacity()); - bytes.reserve(1001); - assert_eq!(2000, bytes.capacity()); -} - -#[test] -fn reserve_in_arc_nonunique_does_not_overallocate() { - let mut bytes = BytesMut::with_capacity(1000); - let _copy = bytes.take(); - - // now bytes is Arc and refcount == 2 - - assert_eq!(1000, bytes.capacity()); - bytes.reserve(2001); - assert_eq!(2001, bytes.capacity()); -} - -#[test] -fn inline_storage() { - let mut bytes = BytesMut::with_capacity(inline_cap()); - let zero = [0u8; 64]; - - bytes.put(&zero[0..inline_cap()]); - assert_eq!(*bytes, zero[0..inline_cap()]); -} - -#[test] -fn extend_mut() { - let mut bytes = BytesMut::with_capacity(0); - bytes.extend(LONG); - assert_eq!(*bytes, LONG[..]); -} - -#[test] -fn extend_shr() { - let mut bytes = Bytes::new(); - bytes.extend(LONG); - assert_eq!(*bytes, LONG[..]); -} - -#[test] -fn extend_from_slice_mut() { - for &i in &[3, 34] { - let mut bytes = BytesMut::new(); - bytes.extend_from_slice(&LONG[..i]); - bytes.extend_from_slice(&LONG[i..]); - assert_eq!(LONG[..], *bytes); - } -} - -#[test] -fn extend_from_slice_shr() { - for &i in &[3, 34] { - let mut bytes = Bytes::new(); - bytes.extend_from_slice(&LONG[..i]); - bytes.extend_from_slice(&LONG[i..]); - assert_eq!(LONG[..], *bytes); - } -} - -#[test] -fn from_static() { - let mut a = Bytes::from_static(b"ab"); - let b = a.split_off(1); - - assert_eq!(a, b"a"[..]); - assert_eq!(b, b"b"[..]); -} - -#[test] -fn advance_inline() { - let mut a = Bytes::from(&b"hello world"[..]); - a.advance(6); - assert_eq!(a, &b"world"[..]); -} - -#[test] -fn advance_static() { - let mut a = Bytes::from_static(b"hello world"); - a.advance(6); - assert_eq!(a, &b"world"[..]); -} - -#[test] -fn advance_vec() { - let mut a = BytesMut::from(b"hello world boooo yah world zomg wat wat".to_vec()); - a.advance(16); - assert_eq!(a, b"o yah world zomg wat wat"[..]); - - a.advance(4); - assert_eq!(a, b"h world zomg wat wat"[..]); - - // Reserve some space. - a.reserve(1024); - assert_eq!(a, b"h world zomg wat wat"[..]); - - a.advance(6); - assert_eq!(a, b"d zomg wat wat"[..]); -} - -#[test] -#[should_panic] -fn advance_past_len() { - let mut a = BytesMut::from(b"hello world".to_vec()); - a.advance(20); -} - -#[test] -// Only run these tests on little endian systems. CI uses qemu for testing -// little endian... and qemu doesn't really support threading all that well. -#[cfg(target_endian = "little")] -fn stress() { - // Tests promoting a buffer from a vec -> shared in a concurrent situation - use std::sync::{Arc, Barrier}; - use std::thread; - - const THREADS: usize = 8; - const ITERS: usize = 1_000; - - for i in 0..ITERS { - let data = [i as u8; 256]; - let buf = Arc::new(Bytes::from(&data[..])); - - let barrier = Arc::new(Barrier::new(THREADS)); - let mut joins = Vec::with_capacity(THREADS); - - for _ in 0..THREADS { - let c = barrier.clone(); - let buf = buf.clone(); - - joins.push(thread::spawn(move || { - c.wait(); - let buf: Bytes = (*buf).clone(); - drop(buf); - })); - } - - for th in joins { - th.join().unwrap(); - } - - assert_eq!(*buf, data[..]); - } -} - -#[test] -fn partial_eq_bytesmut() { - let bytes = Bytes::from(&b"The quick red fox"[..]); - let bytesmut = BytesMut::from(&b"The quick red fox"[..]); - assert!(bytes == bytesmut); - assert!(bytesmut == bytes); - let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]); - assert!(bytes2 != bytesmut); - assert!(bytesmut != bytes2); -} - -#[test] -fn unsplit_basic() { - let mut buf = BytesMut::with_capacity(64); - buf.extend_from_slice(b"aaabbbcccddd"); - - let splitted = buf.split_off(6); - assert_eq!(b"aaabbb", &buf[..]); - assert_eq!(b"cccddd", &splitted[..]); - - buf.unsplit(splitted); - assert_eq!(b"aaabbbcccddd", &buf[..]); -} - -#[test] -fn unsplit_empty_other() { - let mut buf = BytesMut::with_capacity(64); - buf.extend_from_slice(b"aaabbbcccddd"); - - // empty other - let other = BytesMut::new(); - - buf.unsplit(other); - assert_eq!(b"aaabbbcccddd", &buf[..]); -} - -#[test] -fn unsplit_empty_self() { - // empty self - let mut buf = BytesMut::new(); - - let mut other = BytesMut::with_capacity(64); - other.extend_from_slice(b"aaabbbcccddd"); - - buf.unsplit(other); - assert_eq!(b"aaabbbcccddd", &buf[..]); -} - -#[test] -fn unsplit_inline_arc() { - let mut buf = BytesMut::with_capacity(8); //inline - buf.extend_from_slice(b"aaaabbbb"); - - let mut buf2 = BytesMut::with_capacity(64); - buf2.extend_from_slice(b"ccccddddeeee"); - - buf2.split_off(8); //arc - - buf.unsplit(buf2); - assert_eq!(b"aaaabbbbccccdddd", &buf[..]); -} - -#[test] -fn unsplit_arc_inline() { - let mut buf = BytesMut::with_capacity(64); - buf.extend_from_slice(b"aaaabbbbeeee"); - - buf.split_off(8); //arc - - let mut buf2 = BytesMut::with_capacity(8); //inline - buf2.extend_from_slice(b"ccccdddd"); - - buf.unsplit(buf2); - assert_eq!(b"aaaabbbbccccdddd", &buf[..]); - -} - -#[test] -fn unsplit_both_inline() { - let mut buf = BytesMut::with_capacity(16); //inline - buf.extend_from_slice(b"aaaabbbbccccdddd"); - - let splitted = buf.split_off(8); // both inline - assert_eq!(b"aaaabbbb", &buf[..]); - assert_eq!(b"ccccdddd", &splitted[..]); - - buf.unsplit(splitted); - assert_eq!(b"aaaabbbbccccdddd", &buf[..]); -} - - -#[test] -fn unsplit_arc_different() { - let mut buf = BytesMut::with_capacity(64); - buf.extend_from_slice(b"aaaabbbbeeee"); - - buf.split_off(8); //arc - - let mut buf2 = BytesMut::with_capacity(64); - buf2.extend_from_slice(b"ccccddddeeee"); - - buf2.split_off(8); //arc - - buf.unsplit(buf2); - assert_eq!(b"aaaabbbbccccdddd", &buf[..]); -} - -#[test] -fn unsplit_arc_non_contiguous() { - let mut buf = BytesMut::with_capacity(64); - buf.extend_from_slice(b"aaaabbbbeeeeccccdddd"); - - let mut buf2 = buf.split_off(8); //arc - - let buf3 = buf2.split_off(4); //arc - - buf.unsplit(buf3); - assert_eq!(b"aaaabbbbccccdddd", &buf[..]); -} - -#[test] -fn unsplit_two_split_offs() { - let mut buf = BytesMut::with_capacity(64); - buf.extend_from_slice(b"aaaabbbbccccdddd"); - - let mut buf2 = buf.split_off(8); //arc - let buf3 = buf2.split_off(4); //arc - - buf2.unsplit(buf3); - buf.unsplit(buf2); - assert_eq!(b"aaaabbbbccccdddd", &buf[..]); -} - -#[test] -fn from_iter_no_size_hint() { - use std::iter; - - let mut expect = vec![]; - - let actual: Bytes = iter::repeat(b'x') - .scan(100, |cnt, item| { - if *cnt >= 1 { - *cnt -= 1; - expect.push(item); - Some(item) - } else { - None - } - }) - .collect(); - - assert_eq!(&actual[..], &expect[..]); -} - -fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) { - let slice = &(bytes.as_ref()[start..end]); - let sub = bytes.slice_ref(&slice); - assert_eq!(&sub[..], expected); -} - -#[test] -fn slice_ref_works() { - let bytes = Bytes::from(&b"012345678"[..]); - - test_slice_ref(&bytes, 0, 0, b""); - test_slice_ref(&bytes, 0, 3, b"012"); - test_slice_ref(&bytes, 2, 6, b"2345"); - test_slice_ref(&bytes, 7, 9, b"78"); - test_slice_ref(&bytes, 9, 9, b""); -} - - -#[test] -fn slice_ref_empty() { - let bytes = Bytes::from(&b""[..]); - let slice = &(bytes.as_ref()[0..0]); - - let sub = bytes.slice_ref(&slice); - assert_eq!(&sub[..], b""); -} - -#[test] -#[should_panic] -fn slice_ref_catches_not_a_subset() { - let bytes = Bytes::from(&b"012345678"[..]); - let slice = &b"012345"[0..4]; - - bytes.slice_ref(slice); -} - -#[test] -#[should_panic] -fn slice_ref_catches_not_an_empty_subset() { - let bytes = Bytes::from(&b"012345678"[..]); - let slice = &b""[0..0]; - - bytes.slice_ref(slice); -} - -#[test] -#[should_panic] -fn empty_slice_ref_catches_not_an_empty_subset() { - let bytes = Bytes::from(&b""[..]); - let slice = &b""[0..0]; - - bytes.slice_ref(slice); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_chain.rs b/third_party/rust/bytes/v0_4/crate/tests/test_chain.rs deleted file mode 100644 index 2789e7c0602d..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_chain.rs +++ /dev/null @@ -1,122 +0,0 @@ -extern crate bytes; -extern crate iovec; - -use bytes::{Buf, BufMut, Bytes, BytesMut}; -use bytes::buf::Chain; -use iovec::IoVec; -use std::io::Cursor; - -#[test] -fn collect_two_bufs() { - let a = Cursor::new(Bytes::from(&b"hello"[..])); - let b = Cursor::new(Bytes::from(&b"world"[..])); - - let res: Vec = a.chain(b).collect(); - assert_eq!(res, &b"helloworld"[..]); -} - -#[test] -fn writing_chained() { - let mut a = BytesMut::with_capacity(64); - let mut b = BytesMut::with_capacity(64); - - { - let mut buf = Chain::new(&mut a, &mut b); - - for i in 0..128 { - buf.put(i as u8); - } - } - - assert_eq!(64, a.len()); - assert_eq!(64, b.len()); - - for i in 0..64 { - let expect = i as u8; - assert_eq!(expect, a[i]); - assert_eq!(expect + 64, b[i]); - } -} - -#[test] -fn iterating_two_bufs() { - let a = Cursor::new(Bytes::from(&b"hello"[..])); - let b = Cursor::new(Bytes::from(&b"world"[..])); - - let res: Vec = a.chain(b).iter().collect(); - assert_eq!(res, &b"helloworld"[..]); -} - -#[test] -fn vectored_read() { - let a = Cursor::new(Bytes::from(&b"hello"[..])); - let b = Cursor::new(Bytes::from(&b"world"[..])); - - let mut buf = a.chain(b); - - { - let b1: &[u8] = &mut [0]; - let b2: &[u8] = &mut [0]; - let b3: &[u8] = &mut [0]; - let b4: &[u8] = &mut [0]; - let mut iovecs: [&IoVec; 4] = - [b1.into(), b2.into(), b3.into(), b4.into()]; - - assert_eq!(2, buf.bytes_vec(&mut iovecs)); - assert_eq!(iovecs[0][..], b"hello"[..]); - assert_eq!(iovecs[1][..], b"world"[..]); - assert_eq!(iovecs[2][..], b"\0"[..]); - assert_eq!(iovecs[3][..], b"\0"[..]); - } - - buf.advance(2); - - { - let b1: &[u8] = &mut [0]; - let b2: &[u8] = &mut [0]; - let b3: &[u8] = &mut [0]; - let b4: &[u8] = &mut [0]; - let mut iovecs: [&IoVec; 4] = - [b1.into(), b2.into(), b3.into(), b4.into()]; - - assert_eq!(2, buf.bytes_vec(&mut iovecs)); - assert_eq!(iovecs[0][..], b"llo"[..]); - assert_eq!(iovecs[1][..], b"world"[..]); - assert_eq!(iovecs[2][..], b"\0"[..]); - assert_eq!(iovecs[3][..], b"\0"[..]); - } - - buf.advance(3); - - { - let b1: &[u8] = &mut [0]; - let b2: &[u8] = &mut [0]; - let b3: &[u8] = &mut [0]; - let b4: &[u8] = &mut [0]; - let mut iovecs: [&IoVec; 4] = - [b1.into(), b2.into(), b3.into(), b4.into()]; - - assert_eq!(1, buf.bytes_vec(&mut iovecs)); - assert_eq!(iovecs[0][..], b"world"[..]); - assert_eq!(iovecs[1][..], b"\0"[..]); - assert_eq!(iovecs[2][..], b"\0"[..]); - assert_eq!(iovecs[3][..], b"\0"[..]); - } - - buf.advance(3); - - { - let b1: &[u8] = &mut [0]; - let b2: &[u8] = &mut [0]; - let b3: &[u8] = &mut [0]; - let b4: &[u8] = &mut [0]; - let mut iovecs: [&IoVec; 4] = - [b1.into(), b2.into(), b3.into(), b4.into()]; - - assert_eq!(1, buf.bytes_vec(&mut iovecs)); - assert_eq!(iovecs[0][..], b"ld"[..]); - assert_eq!(iovecs[1][..], b"\0"[..]); - assert_eq!(iovecs[2][..], b"\0"[..]); - assert_eq!(iovecs[3][..], b"\0"[..]); - } -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_debug.rs b/third_party/rust/bytes/v0_4/crate/tests/test_debug.rs deleted file mode 100644 index 9945a2835bb1..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_debug.rs +++ /dev/null @@ -1,35 +0,0 @@ -extern crate bytes; - -use bytes::Bytes; - -#[test] -fn fmt() { - let vec: Vec<_> = (0..0x100).map(|b| b as u8).collect(); - - let expected = "b\"\ - \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\ - \\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\ - \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\ - \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f\ - \x20!\\\"#$%&'()*+,-./0123456789:;<=>?\ - @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_\ - `abcdefghijklmnopqrstuvwxyz{|}~\\x7f\ - \\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\ - \\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\ - \\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\ - \\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\ - \\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\ - \\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\ - \\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\ - \\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\ - \\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\ - \\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\ - \\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\ - \\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\ - \\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\ - \\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\ - \\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\ - \\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff\""; - - assert_eq!(expected, format!("{:?}", Bytes::from(vec))); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_from_buf.rs b/third_party/rust/bytes/v0_4/crate/tests/test_from_buf.rs deleted file mode 100644 index 216bf1232805..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_from_buf.rs +++ /dev/null @@ -1,34 +0,0 @@ -extern crate bytes; - -use bytes::{Buf, Bytes, BytesMut}; -use std::io::Cursor; - -const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb"; -const SHORT: &'static [u8] = b"hello world"; - -#[test] -fn collect_to_vec() { - let buf: Vec = Cursor::new(SHORT).collect(); - assert_eq!(buf, SHORT); - - let buf: Vec = Cursor::new(LONG).collect(); - assert_eq!(buf, LONG); -} - -#[test] -fn collect_to_bytes() { - let buf: Bytes = Cursor::new(SHORT).collect(); - assert_eq!(buf, SHORT); - - let buf: Bytes = Cursor::new(LONG).collect(); - assert_eq!(buf, LONG); -} - -#[test] -fn collect_to_bytes_mut() { - let buf: BytesMut = Cursor::new(SHORT).collect(); - assert_eq!(buf, SHORT); - - let buf: BytesMut = Cursor::new(LONG).collect(); - assert_eq!(buf, LONG); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_iter.rs b/third_party/rust/bytes/v0_4/crate/tests/test_iter.rs deleted file mode 100644 index c16dbf694ba4..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_iter.rs +++ /dev/null @@ -1,22 +0,0 @@ -extern crate bytes; - -use bytes::{Buf, IntoBuf, Bytes}; - -#[test] -fn iter_len() { - let buf = Bytes::from(&b"hello world"[..]).into_buf(); - let iter = buf.iter(); - - assert_eq!(iter.size_hint(), (11, Some(11))); - assert_eq!(iter.len(), 11); -} - - -#[test] -fn empty_iter_len() { - let buf = Bytes::from(&b""[..]).into_buf(); - let iter = buf.iter(); - - assert_eq!(iter.size_hint(), (0, Some(0))); - assert_eq!(iter.len(), 0); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_reader.rs b/third_party/rust/bytes/v0_4/crate/tests/test_reader.rs deleted file mode 100644 index 7103f3592ae8..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_reader.rs +++ /dev/null @@ -1,28 +0,0 @@ -extern crate bytes; - -use std::io::{BufRead, Cursor, Read}; - -use bytes::Buf; - -#[test] -fn read() { - let buf1 = Cursor::new(b"hello "); - let buf2 = Cursor::new(b"world"); - let buf = Buf::chain(buf1, buf2); // Disambiguate with Read::chain - let mut buffer = Vec::new(); - buf.reader().read_to_end(&mut buffer).unwrap(); - assert_eq!(b"hello world", &buffer[..]); -} - -#[test] -fn buf_read() { - let buf1 = Cursor::new(b"hell"); - let buf2 = Cursor::new(b"o\nworld"); - let mut reader = Buf::chain(buf1, buf2).reader(); - let mut line = String::new(); - reader.read_line(&mut line).unwrap(); - assert_eq!("hello\n", &line); - line.clear(); - reader.read_line(&mut line).unwrap(); - assert_eq!("world", &line); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_serde.rs b/third_party/rust/bytes/v0_4/crate/tests/test_serde.rs deleted file mode 100644 index ff440242f454..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_serde.rs +++ /dev/null @@ -1,21 +0,0 @@ -#![cfg(feature = "serde")] - -extern crate bytes; -extern crate serde_test; -use serde_test::{Token, assert_tokens}; - -#[test] -fn test_ser_de_empty() { - let b = bytes::Bytes::new(); - assert_tokens(&b, &[Token::Bytes(b"")]); - let b = bytes::BytesMut::with_capacity(0); - assert_tokens(&b, &[Token::Bytes(b"")]); -} - -#[test] -fn test_ser_de() { - let b = bytes::Bytes::from(&b"bytes"[..]); - assert_tokens(&b, &[Token::Bytes(b"bytes")]); - let b = bytes::BytesMut::from(&b"bytes"[..]); - assert_tokens(&b, &[Token::Bytes(b"bytes")]); -} diff --git a/third_party/rust/bytes/v0_4/crate/tests/test_take.rs b/third_party/rust/bytes/v0_4/crate/tests/test_take.rs deleted file mode 100644 index 93e0c6c5ab8a..000000000000 --- a/third_party/rust/bytes/v0_4/crate/tests/test_take.rs +++ /dev/null @@ -1,13 +0,0 @@ -extern crate bytes; - -use bytes::Buf; -use std::io::Cursor; - -#[test] -fn long_take() { - // Tests that take with a size greater than the buffer length will not - // overrun the buffer. Regression test for #138. - let buf = Cursor::new(b"hello world").take(100); - assert_eq!(11, buf.remaining()); - assert_eq!(b"hello world", buf.bytes()); -} From 8c4ce30fcda524539fc1d829cb99138c917981e6 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Wed, 13 Dec 2023 12:48:19 -0800 Subject: [PATCH 6/8] Remove unused Rust iovec v0.1.4 source This was a transitive dependency of `bytes v0.4` which we no longer use. --- third_party/rust/Cargo.lock | 7 - third_party/rust/Cargo.toml | 4 - third_party/rust/iovec/v0_1/BUILD.gn | 46 ---- third_party/rust/iovec/v0_1/README.chromium | 8 - .../iovec/v0_1/crate/.cargo_vcs_info.json | 5 - third_party/rust/iovec/v0_1/crate/.gitignore | 2 - third_party/rust/iovec/v0_1/crate/.travis.yml | 25 --- .../rust/iovec/v0_1/crate/CHANGELOG.md | 19 -- third_party/rust/iovec/v0_1/crate/Cargo.toml | 26 --- .../rust/iovec/v0_1/crate/Cargo.toml.orig | 17 -- .../rust/iovec/v0_1/crate/LICENSE-APACHE | 201 ------------------ third_party/rust/iovec/v0_1/crate/LICENSE-MIT | 25 --- third_party/rust/iovec/v0_1/crate/README.md | 35 --- .../rust/iovec/v0_1/crate/appveyor.yml | 16 -- third_party/rust/iovec/v0_1/crate/src/lib.rs | 161 -------------- .../rust/iovec/v0_1/crate/src/sys/mod.rs | 26 --- .../rust/iovec/v0_1/crate/src/sys/unix.rs | 52 ----- .../rust/iovec/v0_1/crate/src/sys/unknown.rs | 57 ----- .../rust/iovec/v0_1/crate/src/sys/windows.rs | 68 ------ third_party/rust/iovec/v0_1/crate/src/unix.rs | 68 ------ .../rust/iovec/v0_1/crate/src/windows.rs | 0 21 files changed, 868 deletions(-) delete mode 100644 third_party/rust/iovec/v0_1/BUILD.gn delete mode 100644 third_party/rust/iovec/v0_1/README.chromium delete mode 100644 third_party/rust/iovec/v0_1/crate/.cargo_vcs_info.json delete mode 100644 third_party/rust/iovec/v0_1/crate/.gitignore delete mode 100644 third_party/rust/iovec/v0_1/crate/.travis.yml delete mode 100644 third_party/rust/iovec/v0_1/crate/CHANGELOG.md delete mode 100644 third_party/rust/iovec/v0_1/crate/Cargo.toml delete mode 100644 third_party/rust/iovec/v0_1/crate/Cargo.toml.orig delete mode 100644 third_party/rust/iovec/v0_1/crate/LICENSE-APACHE delete mode 100644 third_party/rust/iovec/v0_1/crate/LICENSE-MIT delete mode 100644 third_party/rust/iovec/v0_1/crate/README.md delete mode 100644 third_party/rust/iovec/v0_1/crate/appveyor.yml delete mode 100644 third_party/rust/iovec/v0_1/crate/src/lib.rs delete mode 100644 third_party/rust/iovec/v0_1/crate/src/sys/mod.rs delete mode 100644 third_party/rust/iovec/v0_1/crate/src/sys/unix.rs delete mode 100644 third_party/rust/iovec/v0_1/crate/src/sys/unknown.rs delete mode 100644 third_party/rust/iovec/v0_1/crate/src/sys/windows.rs delete mode 100644 third_party/rust/iovec/v0_1/crate/src/unix.rs delete mode 100644 third_party/rust/iovec/v0_1/crate/src/windows.rs diff --git a/third_party/rust/Cargo.lock b/third_party/rust/Cargo.lock index 2d5dfff3a76f..469403123735 100644 --- a/third_party/rust/Cargo.lock +++ b/third_party/rust/Cargo.lock @@ -1109,13 +1109,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "iovec" -version = "0.1.4" -dependencies = [ - "libc", -] - [[package]] name = "itertools" version = "0.10.5" diff --git a/third_party/rust/Cargo.toml b/third_party/rust/Cargo.toml index 5b9d930ef7b8..ae0a5e2313da 100644 --- a/third_party/rust/Cargo.toml +++ b/third_party/rust/Cargo.toml @@ -676,10 +676,6 @@ package = "idna" path = "indexmap/v1/crate" package = "indexmap" -[patch.crates-io.iovec_v0_1] -path = "iovec/v0_1/crate" -package = "iovec" - [patch.crates-io.itertools_v0_10] path = "itertools/v0_10/crate" package = "itertools" diff --git a/third_party/rust/iovec/v0_1/BUILD.gn b/third_party/rust/iovec/v0_1/BUILD.gn deleted file mode 100644 index 5417082f9704..000000000000 --- a/third_party/rust/iovec/v0_1/BUILD.gn +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2023 The Chromium Authors -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import("//build/rust/cargo_crate.gni") - -cargo_crate("lib") { - crate_name = "iovec" - epoch = "0.1" - crate_type = "rlib" - - # Only for usage from third-party crates. Add the crate to - # third_party.toml to use it from first-party code. - visibility = [ "//brave/third_party/rust/*" ] - crate_root = "crate/src/lib.rs" - sources = [ - "//brave/third_party/rust/iovec/v0_1/crate/src/lib.rs", - "//brave/third_party/rust/iovec/v0_1/crate/src/sys/mod.rs", - "//brave/third_party/rust/iovec/v0_1/crate/src/sys/unix.rs", - "//brave/third_party/rust/iovec/v0_1/crate/src/sys/unknown.rs", - "//brave/third_party/rust/iovec/v0_1/crate/src/sys/windows.rs", - "//brave/third_party/rust/iovec/v0_1/crate/src/unix.rs", - "//brave/third_party/rust/iovec/v0_1/crate/src/windows.rs", - ] - inputs = [ - "//brave/third_party/rust/iovec/v0_1/crate/CHANGELOG.md", - "//brave/third_party/rust/iovec/v0_1/crate/README.md", - ] - - # Unit tests skipped. Generate with --with-tests to include them. - build_native_rust_unit_tests = false - edition = "2015" - cargo_pkg_version = "0.1.4" - cargo_pkg_authors = "Carl Lerche " - cargo_pkg_name = "iovec" - cargo_pkg_description = - "Portable buffer type for scatter/gather I/O operations" - library_configs -= [ "//build/config/compiler:chromium_code" ] - library_configs += [ "//build/config/compiler:no_chromium_code" ] - executable_configs -= [ "//build/config/compiler:chromium_code" ] - executable_configs += [ "//build/config/compiler:no_chromium_code" ] - deps = [] - if (!is_win) { - deps += [ "//third_party/rust/libc/v0_2:lib" ] - } -} diff --git a/third_party/rust/iovec/v0_1/README.chromium b/third_party/rust/iovec/v0_1/README.chromium deleted file mode 100644 index f06cedbd49f6..000000000000 --- a/third_party/rust/iovec/v0_1/README.chromium +++ /dev/null @@ -1,8 +0,0 @@ -Name: iovec -URL: https://crates.io/crates/iovec -Description: Portable buffer type for scatter/gather I/O operations - -Version: 0.1.4 -Security Critical: yes -License: Apache 2.0 -Revision: 400b8ec64f5d7cdb13db6d9cc8e981e8b84d1b34 diff --git a/third_party/rust/iovec/v0_1/crate/.cargo_vcs_info.json b/third_party/rust/iovec/v0_1/crate/.cargo_vcs_info.json deleted file mode 100644 index 5efd5a81eff4..000000000000 --- a/third_party/rust/iovec/v0_1/crate/.cargo_vcs_info.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "git": { - "sha1": "400b8ec64f5d7cdb13db6d9cc8e981e8b84d1b34" - } -} diff --git a/third_party/rust/iovec/v0_1/crate/.gitignore b/third_party/rust/iovec/v0_1/crate/.gitignore deleted file mode 100644 index a9d37c560c6a..000000000000 --- a/third_party/rust/iovec/v0_1/crate/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target -Cargo.lock diff --git a/third_party/rust/iovec/v0_1/crate/.travis.yml b/third_party/rust/iovec/v0_1/crate/.travis.yml deleted file mode 100644 index a21a72399ce5..000000000000 --- a/third_party/rust/iovec/v0_1/crate/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ ---- -language: rust -sudo: false - -matrix: - include: - # Oldest supported Rust (this should track Mio) - - rust: 1.18.0 - - rust: stable - # OS X support - - rust: stable - os: osx - # WebAssembly support. - - rust: beta - script: - - rustup target add wasm32-unknown-unknown - - cargo build --target=wasm32-unknown-unknown - -script: - - cargo build - - cargo test - -notifications: - email: - on_success: never diff --git a/third_party/rust/iovec/v0_1/crate/CHANGELOG.md b/third_party/rust/iovec/v0_1/crate/CHANGELOG.md deleted file mode 100644 index 73d6300a6451..000000000000 --- a/third_party/rust/iovec/v0_1/crate/CHANGELOG.md +++ /dev/null @@ -1,19 +0,0 @@ -# 0.1.4 (October 9, 2019) - -* Add missing `repr(C)`. - -# 0.1.3 (yanked) (October 8, 2019) - -* Remove dependency on old winapi 0.2 (#16) - -# 0.1.2 (January 26th, 2018) - -* Add support for non-windows/unix targets (#10) - -# 0.1.1 (October 5th, 2017) - -* Fix soundness bug: Assert slice lengths are always > 0 (#5) - -# 0.1.0 (March 14th, 2017) - -* Initial release diff --git a/third_party/rust/iovec/v0_1/crate/Cargo.toml b/third_party/rust/iovec/v0_1/crate/Cargo.toml deleted file mode 100644 index e95031835838..000000000000 --- a/third_party/rust/iovec/v0_1/crate/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO -# -# When uploading crates to the registry Cargo will automatically -# "normalize" Cargo.toml files for maximal compatibility -# with all versions of Cargo and also rewrite `path` dependencies -# to registry (e.g., crates.io) dependencies -# -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) - -[package] -name = "iovec" -version = "0.1.4" -authors = ["Carl Lerche "] -description = "Portable buffer type for scatter/gather I/O operations\n" -homepage = "https://github.com/carllerche/iovec" -documentation = "https://docs.rs/iovec" -readme = "README.md" -keywords = ["scatter", "gather", "vectored", "io", "networking"] -categories = ["network-programming", "api-bindings"] -license = "MIT/Apache-2.0" -repository = "https://github.com/carllerche/iovec" -[target."cfg(unix)".dependencies.libc] -version = "0.2" diff --git a/third_party/rust/iovec/v0_1/crate/Cargo.toml.orig b/third_party/rust/iovec/v0_1/crate/Cargo.toml.orig deleted file mode 100644 index 37312f51a80f..000000000000 --- a/third_party/rust/iovec/v0_1/crate/Cargo.toml.orig +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "iovec" -version = "0.1.4" -authors = ["Carl Lerche "] -license = "MIT/Apache-2.0" -readme = "README.md" -keywords = ["scatter", "gather", "vectored", "io", "networking"] -repository = "https://github.com/carllerche/iovec" -homepage = "https://github.com/carllerche/iovec" -documentation = "https://docs.rs/iovec" -description = """ -Portable buffer type for scatter/gather I/O operations -""" -categories = ["network-programming", "api-bindings"] - -[target.'cfg(unix)'.dependencies] -libc = "0.2" diff --git a/third_party/rust/iovec/v0_1/crate/LICENSE-APACHE b/third_party/rust/iovec/v0_1/crate/LICENSE-APACHE deleted file mode 100644 index 87d73e7f9191..000000000000 --- a/third_party/rust/iovec/v0_1/crate/LICENSE-APACHE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - -Copyright 2017 Carl Lerche - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/third_party/rust/iovec/v0_1/crate/LICENSE-MIT b/third_party/rust/iovec/v0_1/crate/LICENSE-MIT deleted file mode 100644 index 6c296bec80df..000000000000 --- a/third_party/rust/iovec/v0_1/crate/LICENSE-MIT +++ /dev/null @@ -1,25 +0,0 @@ -Copyright (c) 2017 Carl Lerche - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/iovec/v0_1/crate/README.md b/third_party/rust/iovec/v0_1/crate/README.md deleted file mode 100644 index 6d2ea7bd73bc..000000000000 --- a/third_party/rust/iovec/v0_1/crate/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# IoVec - -A specialized byte slice type for performing vectored I/O operations. - -[![Crates.io](https://img.shields.io/crates/v/iovec.svg?maxAge=2592000)](https://crates.io/crates/iovec) -[![Build Status](https://travis-ci.org/carllerche/iovec.svg?branch=master)](https://travis-ci.org/carllerche/iovec) - -[Documentation](https://docs.rs/iovec) - -## Usage - -To use `iovec`, first add this to your `Cargo.toml`: - -```toml -[dependencies] -iovec = "0.1" -``` - -Next, add this to your crate: - -```rust -extern crate iovec; - -use iovec::IoVec; -``` - -For more detail, see [documentation](https://docs.rs/iovec). - -# License - -`iovec` is primarily distributed under the terms of both the MIT license and the -Apache License (Version 2.0), with portions covered by various BSD-like -licenses. - -See LICENSE-APACHE, and LICENSE-MIT for details. diff --git a/third_party/rust/iovec/v0_1/crate/appveyor.yml b/third_party/rust/iovec/v0_1/crate/appveyor.yml deleted file mode 100644 index 4428146b9851..000000000000 --- a/third_party/rust/iovec/v0_1/crate/appveyor.yml +++ /dev/null @@ -1,16 +0,0 @@ -environment: - matrix: - - TARGET: x86_64-pc-windows-msvc - -install: - - curl -sSf -o rustup-init.exe https://win.rustup.rs/ - - rustup-init.exe -y --default-host %TARGET% - - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - - rustc -V - - cargo -V - -build: false - -test_script: - - cargo build - - cargo test diff --git a/third_party/rust/iovec/v0_1/crate/src/lib.rs b/third_party/rust/iovec/v0_1/crate/src/lib.rs deleted file mode 100644 index 1e091a46c9bf..000000000000 --- a/third_party/rust/iovec/v0_1/crate/src/lib.rs +++ /dev/null @@ -1,161 +0,0 @@ -//! A specialized byte slice type for performing vectored I/O operations. -//! -//! For more detail, see [`IoVec`] documentation. -//! -//! [`IoVec`]: struct.IoVec.html - -#[cfg(unix)] -extern crate libc; - -mod sys; - -use std::{ops, mem}; - -#[cfg(unix)] -pub mod unix; - -/// Max length of an `IoVec` slice. -/// -/// Attempts to convert slices longer than this value will result in a panic. -pub const MAX_LENGTH: usize = sys::MAX_LENGTH; - -/// A specialized byte slice type for performing vectored I/O operations. -/// -/// On all systems, the types needed to perform vectored I/O systems have the -/// same size as Rust's [`slice`]. However, the layout is not necessarily the -/// same. `IoVec` provides a portable compatibility layer. -/// -/// The `IoVec` behaves like a Rust [`slice`], providing the same functions. -/// It also provides conversion functions to and from the OS specific vectored -/// types. -/// -/// [`slice`]: https://doc.rust-lang.org/std/primitive.slice.html -/// -/// # Examples -/// -/// ``` -/// use iovec::IoVec; -/// -/// let mut data = vec![]; -/// data.extend_from_slice(b"hello"); -/// -/// let iovec: &IoVec = data.as_slice().into(); -/// -/// assert_eq!(&iovec[..], &b"hello"[..]); -/// ``` -/// -/// # Panics -/// -/// Attempting to convert a zero-length slice or a slice longer than -/// [`MAX_LENGTH`] to an `IoVec` will result in a panic. -/// -/// [`MAX_LENGTH`]: constant.MAX_LENGTH.html -pub struct IoVec { - sys: sys::IoVec, -} - -impl IoVec { - pub fn from_bytes(slice: &[u8]) -> Option<&IoVec> { - if slice.len() == 0 { - return None - } - unsafe { - let iovec: &sys::IoVec = slice.into(); - Some(mem::transmute(iovec)) - } - } - - pub fn from_bytes_mut(slice: &mut [u8]) -> Option<&mut IoVec> { - if slice.len() == 0 { - return None - } - unsafe { - let iovec: &mut sys::IoVec = slice.into(); - Some(mem::transmute(iovec)) - } - } - - #[deprecated(since = "0.1.0", note = "deref instead")] - #[doc(hidden)] - pub fn as_bytes(&self) -> &[u8] { - &**self - } - - #[deprecated(since = "0.1.0", note = "deref instead")] - #[doc(hidden)] - pub fn as_mut_bytes(&mut self) -> &mut [u8] { - &mut **self - } -} - -impl ops::Deref for IoVec { - type Target = [u8]; - - fn deref(&self) -> &[u8] { - &self.sys.as_ref() - } -} - -impl ops::DerefMut for IoVec { - fn deref_mut(&mut self) -> &mut [u8] { - self.sys.as_mut() - } -} - -#[doc(hidden)] -impl<'a> From<&'a [u8]> for &'a IoVec { - fn from(bytes: &'a [u8]) -> &'a IoVec { - IoVec::from_bytes(bytes) - .expect("this crate accidentally accepted 0-sized slices \ - originally but this was since discovered as a soundness \ - hole, it's recommended to use the `from_bytes` \ - function instead") - } -} - -#[doc(hidden)] -impl<'a> From<&'a mut [u8]> for &'a mut IoVec { - fn from(bytes: &'a mut [u8]) -> &'a mut IoVec { - IoVec::from_bytes_mut(bytes) - .expect("this crate accidentally accepted 0-sized slices \ - originally but this was since discovered as a soundness \ - hole, it's recommended to use the `from_bytes_mut` \ - function instead") - } -} - -#[doc(hidden)] -impl<'a> Default for &'a IoVec { - fn default() -> Self { - panic!("this implementation was accidentally provided but is \ - unfortunately unsound, it's recommended to stop using \ - `IoVec::default` or construct a vector with a nonzero length"); - } -} - -#[doc(hidden)] -impl<'a> Default for &'a mut IoVec { - fn default() -> Self { - panic!("this implementation was accidentally provided but is \ - unfortunately unsound, it's recommended to stop using \ - `IoVec::default` or construct a vector with a nonzero length"); - } -} - -#[cfg(test)] -mod test { - use super::IoVec; - - #[test] - fn convert_ref() { - let buf: &IoVec = (&b"hello world"[..]).into(); - assert_eq!(buf[..], b"hello world"[..]); - } - - #[test] - fn convert_mut() { - let mut buf: Vec = b"hello world".to_vec(); - let buf: &mut IoVec = (&mut buf[..]).into(); - assert_eq!(buf[..], b"hello world"[..]); - } -} diff --git a/third_party/rust/iovec/v0_1/crate/src/sys/mod.rs b/third_party/rust/iovec/v0_1/crate/src/sys/mod.rs deleted file mode 100644 index 3e0efc99e876..000000000000 --- a/third_party/rust/iovec/v0_1/crate/src/sys/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -#[cfg(unix)] -mod unix; - -#[cfg(unix)] -pub use self::unix::{ - IoVec, - MAX_LENGTH, -}; - -#[cfg(windows)] -mod windows; - -#[cfg(windows)] -pub use self::windows::{ - IoVec, - MAX_LENGTH, -}; - -#[cfg(not(any(windows, unix)))] -mod unknown; - -#[cfg(not(any(windows, unix)))] -pub use self::unknown::{ - IoVec, - MAX_LENGTH, -}; diff --git a/third_party/rust/iovec/v0_1/crate/src/sys/unix.rs b/third_party/rust/iovec/v0_1/crate/src/sys/unix.rs deleted file mode 100644 index 4dbc0674f85b..000000000000 --- a/third_party/rust/iovec/v0_1/crate/src/sys/unix.rs +++ /dev/null @@ -1,52 +0,0 @@ -use libc; -use std::{mem, slice, usize}; - -pub struct IoVec { - inner: [u8], -} - -pub const MAX_LENGTH: usize = usize::MAX; - -impl IoVec { - pub fn as_ref(&self) -> &[u8] { - unsafe { - let vec = self.iovec(); - slice::from_raw_parts(vec.iov_base as *const u8, vec.iov_len) - } - } - - pub fn as_mut(&mut self) -> &mut [u8] { - unsafe { - let vec = self.iovec(); - slice::from_raw_parts_mut(vec.iov_base as *mut u8, vec.iov_len) - } - } - - unsafe fn iovec(&self) -> libc::iovec { - mem::transmute(&self.inner) - } -} - -impl<'a> From<&'a [u8]> for &'a IoVec { - fn from(src: &'a [u8]) -> Self { - assert!(src.len() > 0); - unsafe { - mem::transmute(libc::iovec { - iov_base: src.as_ptr() as *mut _, - iov_len: src.len(), - }) - } - } -} - -impl<'a> From<&'a mut [u8]> for &'a mut IoVec { - fn from(src: &'a mut [u8]) -> Self { - assert!(src.len() > 0); - unsafe { - mem::transmute(libc::iovec { - iov_base: src.as_ptr() as *mut _, - iov_len: src.len(), - }) - } - } -} diff --git a/third_party/rust/iovec/v0_1/crate/src/sys/unknown.rs b/third_party/rust/iovec/v0_1/crate/src/sys/unknown.rs deleted file mode 100644 index 37acedd78bc6..000000000000 --- a/third_party/rust/iovec/v0_1/crate/src/sys/unknown.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::{mem, slice, usize}; - -#[derive(Clone)] -pub struct WasmIoVec { - ptr: *const u8, - len: usize, -} - -pub struct IoVec { - inner: [u8], -} - -pub const MAX_LENGTH: usize = usize::MAX; - -impl IoVec { - pub fn as_ref(&self) -> &[u8] { - unsafe { - let vec = self.iovec(); - slice::from_raw_parts(vec.ptr as *const u8, vec.len) - } - } - - pub fn as_mut(&mut self) -> &mut [u8] { - unsafe { - let vec = self.iovec(); - slice::from_raw_parts_mut(vec.ptr as *mut u8, vec.len) - } - } - - unsafe fn iovec(&self) -> WasmIoVec { - mem::transmute(&self.inner) - } -} - -impl<'a> From<&'a [u8]> for &'a IoVec { - fn from(src: &'a [u8]) -> Self { - assert!(src.len() > 0); - unsafe { - mem::transmute(WasmIoVec { - ptr: src.as_ptr() as *mut _, - len: src.len(), - }) - } - } -} - -impl<'a> From<&'a mut [u8]> for &'a mut IoVec { - fn from(src: &'a mut [u8]) -> Self { - assert!(src.len() > 0); - unsafe { - mem::transmute(WasmIoVec { - ptr: src.as_ptr() as *mut _, - len: src.len(), - }) - } - } -} diff --git a/third_party/rust/iovec/v0_1/crate/src/sys/windows.rs b/third_party/rust/iovec/v0_1/crate/src/sys/windows.rs deleted file mode 100644 index fc5b8fb42769..000000000000 --- a/third_party/rust/iovec/v0_1/crate/src/sys/windows.rs +++ /dev/null @@ -1,68 +0,0 @@ -use std::{mem, slice, u32}; - -// declare the types we need directly here to avoid bringing -// in the old and slow winapi 0.2 dependency. - -type DWORD = u32; -type ULONG = u32; -type CHAR = i8; - -#[repr(C)] -struct WSABUF { - pub len: ULONG, - pub buf: *mut CHAR, -} - -pub struct IoVec { - inner: [u8], -} - -pub const MAX_LENGTH: usize = u32::MAX as usize; - -impl IoVec { - pub fn as_ref(&self) -> &[u8] { - unsafe { - let vec = self.wsabuf(); - slice::from_raw_parts(vec.buf as *const u8, vec.len as usize) - } - } - - pub fn as_mut(&mut self) -> &mut [u8] { - unsafe { - let vec = self.wsabuf(); - slice::from_raw_parts_mut(vec.buf as *mut u8, vec.len as usize) - } - } - - unsafe fn wsabuf(&self) -> WSABUF { - mem::transmute(&self.inner) - } -} - -impl<'a> From<&'a [u8]> for &'a IoVec { - fn from(src: &'a [u8]) -> Self { - assert!(src.len() > 0); - assert!(src.len() <= MAX_LENGTH); - - unsafe { - mem::transmute(WSABUF { - buf: src.as_ptr() as *mut _, - len: src.len() as DWORD, - }) - } - } -} - -impl<'a> From<&'a mut [u8]> for &'a mut IoVec { - fn from(src: &'a mut [u8]) -> Self { - assert!(src.len() > 0); - assert!(src.len() <= MAX_LENGTH); - - unsafe { - mem::transmute(WSABUF { - buf: src.as_ptr() as *mut _, - len: src.len() as DWORD, - }) - } - } -} diff --git a/third_party/rust/iovec/v0_1/crate/src/unix.rs b/third_party/rust/iovec/v0_1/crate/src/unix.rs deleted file mode 100644 index 3ef3728bb0a2..000000000000 --- a/third_party/rust/iovec/v0_1/crate/src/unix.rs +++ /dev/null @@ -1,68 +0,0 @@ -//! IoVec extensions for Unix platforms. -//! -//! These functions provide conversions to unix specific representations of the -//! vectored data. -//! -//! # Examples -//! -//! ``` -//! use iovec::IoVec; -//! use iovec::unix; -//! -//! let a = b"hello".to_vec(); -//! let b = b"world".to_vec(); -//! -//! let bufs: &[&IoVec] = &[(&a[..]).into(), (&b[..]).into()]; -//! let os_bufs = unix::as_os_slice(&bufs[..]); -//! -//! // Use the `os_bufs` slice with `writev`. -//! ``` - -use IoVec; -use libc; - -use std::mem; - -/// Convert a slice of `IoVec` refs to a slice of `libc::iovec`. -/// -/// The return value can be passed to `writev` bindings. -/// -/// # Examples -/// -/// ``` -/// use iovec::IoVec; -/// use iovec::unix; -/// -/// let a = b"hello".to_vec(); -/// let b = b"world".to_vec(); -/// -/// let bufs: &[&IoVec] = &[a[..].into(), b[..].into()]; -/// let os_bufs = unix::as_os_slice(bufs); -/// -/// // Use the `os_bufs` slice with `writev`. -/// ``` -pub fn as_os_slice<'a>(iov: &'a [&IoVec]) -> &'a [libc::iovec] { - unsafe { mem::transmute(iov) } -} - -/// Convert a mutable slice of `IoVec` refs to a mutable slice of `libc::iovec`. -/// -/// The return value can be passed to `readv` bindings. -/// -/// # Examples -/// -/// ``` -/// use iovec::IoVec; -/// use iovec::unix; -/// -/// let mut a = [0; 10]; -/// let mut b = [0; 10]; -/// -/// let bufs: &mut [&mut IoVec] = &mut [(&mut a[..]).into(), (&mut b[..]).into()]; -/// let os_bufs = unix::as_os_slice_mut(bufs); -/// -/// // Use the `os_bufs` slice with `readv`. -/// ``` -pub fn as_os_slice_mut<'a>(iov: &'a mut [&mut IoVec]) -> &'a mut [libc::iovec] { - unsafe { mem::transmute(iov) } -} diff --git a/third_party/rust/iovec/v0_1/crate/src/windows.rs b/third_party/rust/iovec/v0_1/crate/src/windows.rs deleted file mode 100644 index e69de29bb2d1..000000000000 From b89095b70c9de9f8b2639a4a8a660e5007c0e240 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Wed, 13 Dec 2023 12:59:12 -0800 Subject: [PATCH 7/8] Skus: add license header Address `npm run presubmit` lint. --- components/skus/browser/rs/lib/src/sdk/orders.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/skus/browser/rs/lib/src/sdk/orders.rs b/components/skus/browser/rs/lib/src/sdk/orders.rs index 758913663b2a..b9a9338ac714 100644 --- a/components/skus/browser/rs/lib/src/sdk/orders.rs +++ b/components/skus/browser/rs/lib/src/sdk/orders.rs @@ -1,3 +1,8 @@ +// Copyright (c) 2021 The Brave Authors. All rights reserved. +// 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 https://mozilla.org/MPL/2.0/. + use core::convert::{TryFrom, TryInto}; use chrono::{DateTime, Utc}; From e45a991ce0d013152bc9fae68928cde5cf899431 Mon Sep 17 00:00:00 2001 From: Ralph Giles Date: Wed, 3 Jan 2024 12:00:13 -0800 Subject: [PATCH 8/8] Skus: Improve error message Clarify what 'valid header means' so it's easier to interpret failures. --- components/skus/browser/rs/cxx/src/httpclient.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/components/skus/browser/rs/cxx/src/httpclient.rs b/components/skus/browser/rs/cxx/src/httpclient.rs index 4a67ff7ddb33..48829484cfb7 100644 --- a/components/skus/browser/rs/cxx/src/httpclient.rs +++ b/components/skus/browser/rs/cxx/src/httpclient.rs @@ -63,7 +63,15 @@ impl From> for Result>, InternalErr .debug_unwrap()?; let (key, value) = header.split_at(idx); let key = http::HeaderName::try_from(key).map_err(|_| { - InternalError::InvalidCall("must pass a valid header name".to_string()) + InternalError::InvalidCall( + concat!( + "must pass a valid HTTP header name,", + " i.e. ASCII charaters with no whitespace", + " or separator punctuation.", + "\nSee RFC 9110 section 5.6.2 for details.", + ) + .to_string(), + ) })?; let value = value .get(1..) @@ -73,7 +81,7 @@ impl From> for Result>, InternalErr .debug_unwrap()?; let value = http::HeaderValue::try_from(value).map_err(|_| { InternalError::InvalidCall( - "must pass a valid (ASCII-printable) header value".to_string(), + "must pass a valid (ASCII-printable) HTTP header value".to_string(), ) })?;