| 1 | // Copyright (c) 2019 Parity Technologies (UK) Ltd. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 |
| 4 | // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT |
| 5 | // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. All files in the project carrying such notice may not be copied, |
| 7 | // modified, or distributed except according to those terms. |
| 8 | |
| 9 | //! Websocket [handshake]s. |
| 10 | //! |
| 11 | //! [handshake]: https://tools.ietf.org/html/rfc6455#section-4 |
| 12 | |
| 13 | pub mod clientrust-analyzer cargo soketto 0.8.1 handshake/client/; |
| 14 | #[cfg(feature = "http")] |
| 15 | pub mod httprust-analyzer cargo soketto 0.8.1 handshake/http/; |
| 16 | pub mod serverrust-analyzer cargo soketto 0.8.1 handshake/server/; |
| 17 | |
| 18 | use craterust-analyzer cargo soketto 0.8.1 crate/::extensionrust-analyzer cargo soketto 0.8.1 extension/::{Extensionrust-analyzer cargo soketto 0.8.1 extension/Extension#, Paramrust-analyzer cargo soketto 0.8.1 extension/Param#}; |
| 19 | use base64rust-analyzer cargo base64 0.22.1 crate/::Enginerust-analyzer cargo base64 0.22.1 engine/Engine#; |
| 20 | use bytesrust-analyzer cargo bytes 1.12.1 crate/::BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#; |
| 21 | use sha1rust-analyzer cargo sha1 0.10.7 crate/::{Digestrust-analyzer cargo digest 0.10.7 digest/Digest#, Sha1rust-analyzer cargo sha1 0.10.7 Sha1#}; |
| 22 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/}; |
| 23 | |
| 24 | pub use clientrust-analyzer cargo soketto 0.8.1 handshake/client/::{Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#, ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#}; |
| 25 | pub use serverrust-analyzer cargo soketto 0.8.1 handshake/server/::{ClientRequestrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#, Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#}; |
| 26 | |
| 27 | // Defined in RFC 6455 and used to generate the `Sec-WebSocket-Accept` header |
| 28 | // in the server handshake response. |
| 29 | const KEYrust-analyzer cargo soketto 0.8.1 handshake/KEY.: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 30 | |
| 31 | // How many HTTP headers do we support during parsing? |
| 32 | const MAX_NUM_HEADERSrust-analyzer cargo soketto 0.8.1 handshake/MAX_NUM_HEADERS.: usize = 32; |
| 33 | |
| 34 | // Some HTTP headers we need to check during parsing. |
| 35 | const SEC_WEBSOCKET_EXTENSIONSrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_EXTENSIONS.: &str = "Sec-WebSocket-Extensions"; |
| 36 | const SEC_WEBSOCKET_PROTOCOLrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_PROTOCOL.: &str = "Sec-WebSocket-Protocol"; |
| 37 | |
| 38 | /// Check a set of headers contains a specific one. |
| 39 | fn expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers: &[httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header#], name: &str, ours: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> { |
| 40 | enum Staterust-analyzer cargo soketto 0.8.1 handshake/State# { |
| 41 | Initrust-analyzer cargo soketto 0.8.1 handshake/State#Init#, // Start state |
| 42 | Namerust-analyzer cargo soketto 0.8.1 handshake/State#Name#, // Header name found |
| 43 | Matchrust-analyzer cargo soketto 0.8.1 handshake/State#Match#, // Header value matches |
| 44 | } |
| 45 | |
| 46 | headers |
| 47 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 48 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|h| h.namerust-analyzer cargo httparse 1.10.1 Header#name..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]eq_ignore_ascii_case().(name)) |
| 49 | .foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]fold().(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Initrust-analyzer cargo soketto 0.8.1 handshake/State#Init#), |result, header| { |
| 50 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Matchrust-analyzer cargo soketto 0.8.1 handshake/State#Match#) = result { |
| 51 | return result; |
| 52 | } |
| 53 | if strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(header.valuerust-analyzer cargo httparse 1.10.1 Header#value.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(',').anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|v| v.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]eq_ignore_ascii_case().(ours)) { |
| 54 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Matchrust-analyzer cargo soketto 0.8.1 handshake/State#Match#); |
| 55 | } |
| 56 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Namerust-analyzer cargo soketto 0.8.1 handshake/State#Name#) |
| 57 | }) |
| 58 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|state| match state { |
| 59 | Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Initrust-analyzer cargo soketto 0.8.1 handshake/State#Init# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::HeaderNotFoundrust-analyzer cargo soketto 0.8.1 handshake/Error#HeaderNotFound#(name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())), |
| 60 | Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Namerust-analyzer cargo soketto 0.8.1 handshake/State#Name# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnexpectedHeaderrust-analyzer cargo soketto 0.8.1 handshake/Error#UnexpectedHeader#(name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())), |
| 61 | Staterust-analyzer cargo soketto 0.8.1 handshake/State#::Matchrust-analyzer cargo soketto 0.8.1 handshake/State#Match# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 62 | }) |
| 63 | } |
| 64 | |
| 65 | /// Pick the first header with the given name and apply the given closure to it. |
| 66 | fn with_first_headerrust-analyzer cargo soketto 0.8.1 handshake/with_first_header().<'a, F, R>(headers: &[httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header#<'a>], name: &str, f: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<R, Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> |
| 67 | where |
| 68 | F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&'a [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<R, Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#>, |
| 69 | { |
| 70 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(h) = headers.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|h| h.namerust-analyzer cargo httparse 1.10.1 Header#name..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]eq_ignore_ascii_case().(name)) { |
| 71 | f(h.valuerust-analyzer cargo httparse 1.10.1 Header#value.) |
| 72 | } else { |
| 73 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::HeaderNotFoundrust-analyzer cargo soketto 0.8.1 handshake/Error#HeaderNotFound#(name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Configure all extensions with parsed parameters. |
| 78 | fn configure_extensionsrust-analyzer cargo soketto 0.8.1 handshake/configure_extensions().(extensions: &mut [Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Extensionrust-analyzer cargo soketto 0.8.1 extension/Extension# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>], line: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> { |
| 79 | for e in line.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(',') { |
| 80 | let mut ext_parts = e.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(';'); |
| 81 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) = ext_parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().() { |
| 82 | let name = name.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 83 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ext) = extensions.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]find().(|x| x.namerust-analyzer cargo soketto 0.8.1 extension/impl#[`Box<E>`][Extension]name().().eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]eq_ignore_ascii_case().(name)) { |
| 84 | let mut params = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 85 | for p in ext_parts { |
| 86 | let mut key_value = p.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('='); |
| 87 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(key) = key_value.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(str::trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().) { |
| 88 | let val = key_value.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|v| v.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().().trim_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_matches().('"')); |
| 89 | let mut p = Paramrust-analyzer cargo soketto 0.8.1 extension/Param#::newrust-analyzer cargo soketto 0.8.1 extension/impl#[`Param<'a>`]new().(key); |
| 90 | p.set_valuerust-analyzer cargo soketto 0.8.1 extension/impl#[`Param<'a>`]set_value().(val); |
| 91 | params.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(p) |
| 92 | } |
| 93 | } |
| 94 | ext.configurerust-analyzer cargo soketto 0.8.1 extension/impl#[`Box<E>`][Extension]configure().(¶ms).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Extensionrust-analyzer cargo soketto 0.8.1 handshake/Error#Extension#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 99 | } |
| 100 | |
| 101 | // Write all extensions to the given buffer. |
| 102 | fn append_extensionsrust-analyzer cargo soketto 0.8.1 handshake/append_extensions().<'a, I>(extensions: I, bytes: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) |
| 103 | where |
| 104 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = &'a Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Extensionrust-analyzer cargo soketto 0.8.1 extension/Extension# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>>, |
| 105 | { |
| 106 | let mut iter = extensions.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(); |
| 107 | |
| 108 | if iter.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 109 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nSec-WebSocket-Extensions: ") |
| 110 | } |
| 111 | |
| 112 | append_extension_header_valuerust-analyzer cargo soketto 0.8.1 handshake/append_extension_header_value().(iter, bytes) |
| 113 | } |
| 114 | |
| 115 | // Write the extension header value to the given buffer. |
| 116 | fn append_extension_header_valuerust-analyzer cargo soketto 0.8.1 handshake/append_extension_header_value().<'a, I>(mut extensions_iter: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/Peekable#<I>, bytes: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) |
| 117 | where |
| 118 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'a Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Extensionrust-analyzer cargo soketto 0.8.1 extension/Extension# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>>, |
| 119 | { |
| 120 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e) = extensions_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 121 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(e.namerust-analyzer cargo soketto 0.8.1 extension/impl#[`Box<E>`][Extension]name().().as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 122 | for p in e.paramsrust-analyzer cargo soketto 0.8.1 extension/impl#[`Box<E>`][Extension]params().() { |
| 123 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"; "); |
| 124 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(p.namerust-analyzer cargo soketto 0.8.1 extension/impl#[`Param<'a>`]name().().as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 125 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v) = p.valuerust-analyzer cargo soketto 0.8.1 extension/impl#[`Param<'a>`]value().() { |
| 126 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"="); |
| 127 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(v.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()) |
| 128 | } |
| 129 | } |
| 130 | if extensions_iter.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 131 | bytes.extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b", ") |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // This function takes a 16 byte key (base64 encoded, and so 24 bytes of input) that is expected via |
| 137 | // the `Sec-WebSocket-Key` header during a websocket handshake, and writes the response that's expected |
| 138 | // to be handed back in the response header `Sec-WebSocket-Accept`. |
| 139 | // |
| 140 | // The response is a base64 encoding of a 160bit hash. base64 encoding uses 1 ascii character per 6 bits, |
| 141 | // and 160 / 6 = 26.66 characters. The output is padded with '=' to the nearest 4 characters, so we need 28 |
| 142 | // bytes in total for all of the characters. |
| 143 | // |
| 144 | // See https://datatracker.ietf.org/doc/html/rfc6455#section-1.3 for more information on this. |
| 145 | fn generate_accept_keyrust-analyzer cargo soketto 0.8.1 handshake/generate_accept_key().<'k>(key_base64: &WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey#) -> [u8; 28] { |
| 146 | let mut digest = Sha1rust-analyzer cargo sha1 0.10.7 Sha1#::newrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]new().(); |
| 147 | digest.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(key_base64); |
| 148 | digest.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(KEYrust-analyzer cargo soketto 0.8.1 handshake/KEY.); |
| 149 | let d = digest.finalizerust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]finalize().(); |
| 150 | |
| 151 | let mut output_buf = [0; 28]; |
| 152 | let n = base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD. |
| 153 | .encode_slicerust-analyzer cargo base64 0.22.1 engine/Engine#encode_slice().(d, &mut output_buf) |
| 154 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("encoding to base64 is exactly 28 bytes; qed"); |
| 155 | debug_assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert_eq!!(n, 28, "encoding to base64 should be exactly 28 bytes"); |
| 156 | output_buf |
| 157 | } |
| 158 | |
| 159 | /// Enumeration of possible handshake errors. |
| 160 | #[non_exhaustive] |
| 161 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 162 | pub enum Errorrust-analyzer cargo soketto 0.8.1 handshake/Error# { |
| 163 | /// An I/O error has been encountered. |
| 164 | Iorust-analyzer cargo soketto 0.8.1 handshake/Error#Io#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#), |
| 165 | /// An HTTP version =/= 1.1 was encountered. |
| 166 | UnsupportedHttpVersionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsupportedHttpVersion#, |
| 167 | /// An incomplete HTTP request. |
| 168 | IncompleteHttpRequestrust-analyzer cargo soketto 0.8.1 handshake/Error#IncompleteHttpRequest#, |
| 169 | /// The value of the `Sec-WebSocket-Key` header is of unexpected length. |
| 170 | SecWebSocketKeyInvalidLengthrust-analyzer cargo soketto 0.8.1 handshake/Error#SecWebSocketKeyInvalidLength#(usize), |
| 171 | /// The handshake request was not a GET request. |
| 172 | InvalidRequestMethodrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidRequestMethod#, |
| 173 | /// An HTTP header has not been present. |
| 174 | HeaderNotFoundrust-analyzer cargo soketto 0.8.1 handshake/Error#HeaderNotFound#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 175 | /// An HTTP header value was not expected. |
| 176 | UnexpectedHeaderrust-analyzer cargo soketto 0.8.1 handshake/Error#UnexpectedHeader#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 177 | /// The Sec-WebSocket-Accept header value did not match. |
| 178 | InvalidSecWebSocketAcceptrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidSecWebSocketAccept#, |
| 179 | /// The server returned an extension we did not ask for. |
| 180 | UnsolicitedExtensionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedExtension#, |
| 181 | /// The server returned a protocol we did not ask for. |
| 182 | UnsolicitedProtocolrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedProtocol#, |
| 183 | /// An extension produced an error while encoding or decoding. |
| 184 | Extensionrust-analyzer cargo soketto 0.8.1 handshake/Error#Extension#(craterust-analyzer cargo soketto 0.8.1 crate/::BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#), |
| 185 | /// The HTTP entity could not be parsed successfully. |
| 186 | Httprust-analyzer cargo soketto 0.8.1 handshake/Error#Http#(craterust-analyzer cargo soketto 0.8.1 crate/::BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#), |
| 187 | /// UTF-8 decoding failed. |
| 188 | Utf8rust-analyzer cargo soketto 0.8.1 handshake/Error#Utf8#(strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::Utf8Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/Utf8Error#), |
| 189 | } |
| 190 | |
| 191 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo soketto 0.8.1 handshake/Error# { |
| 192 | fn fmtrust-analyzer cargo soketto 0.8.1 handshake/impl#[Error][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 193 | match self { |
| 194 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Iorust-analyzer cargo soketto 0.8.1 handshake/Error#Io#(e) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "i/o error: {}", e), |
| 195 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsupportedHttpVersionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsupportedHttpVersion# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("http version was not 1.1"), |
| 196 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::IncompleteHttpRequestrust-analyzer cargo soketto 0.8.1 handshake/Error#IncompleteHttpRequest# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("http request was incomplete"), |
| 197 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::SecWebSocketKeyInvalidLengthrust-analyzer cargo soketto 0.8.1 handshake/Error#SecWebSocketKeyInvalidLength#(len) => { |
| 198 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "Sec-WebSocket-Key header was {} bytes long, expected 24", len) |
| 199 | } |
| 200 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::InvalidRequestMethodrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidRequestMethod# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("handshake was not a GET request"), |
| 201 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::HeaderNotFoundrust-analyzer cargo soketto 0.8.1 handshake/Error#HeaderNotFound#(name) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "header {} not found", name), |
| 202 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnexpectedHeaderrust-analyzer cargo soketto 0.8.1 handshake/Error#UnexpectedHeader#(name) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "header {} had an unexpected value", name), |
| 203 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::InvalidSecWebSocketAcceptrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidSecWebSocketAccept# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("websocket key mismatch"), |
| 204 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsolicitedExtensionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedExtension# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("unsolicited extension returned"), |
| 205 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsolicitedProtocolrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedProtocol# => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("unsolicited protocol returned"), |
| 206 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Extensionrust-analyzer cargo soketto 0.8.1 handshake/Error#Extension#(e) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "extension error: {}", e), |
| 207 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Httprust-analyzer cargo soketto 0.8.1 handshake/Error#Http#(e) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "http parser error: {}", e), |
| 208 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Utf8rust-analyzer cargo soketto 0.8.1 handshake/Error#Utf8#(e) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "utf-8 decoding error: {}", e), |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo soketto 0.8.1 handshake/Error# { |
| 214 | fn sourcerust-analyzer cargo soketto 0.8.1 handshake/impl#[Error][Error]source().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&(dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + 'static)> { |
| 215 | match self { |
| 216 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Iorust-analyzer cargo soketto 0.8.1 handshake/Error#Io#(e) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e), |
| 217 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Extensionrust-analyzer cargo soketto 0.8.1 handshake/Error#Extension#(e) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().e), |
| 218 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Httprust-analyzer cargo soketto 0.8.1 handshake/Error#Http#(e) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().e), |
| 219 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Utf8rust-analyzer cargo soketto 0.8.1 handshake/Error#Utf8#(e) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(e), |
| 220 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsupportedHttpVersionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsupportedHttpVersion# |
| 221 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::IncompleteHttpRequestrust-analyzer cargo soketto 0.8.1 handshake/Error#IncompleteHttpRequest# |
| 222 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::SecWebSocketKeyInvalidLengthrust-analyzer cargo soketto 0.8.1 handshake/Error#SecWebSocketKeyInvalidLength#(_) |
| 223 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::InvalidRequestMethodrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidRequestMethod# |
| 224 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::HeaderNotFoundrust-analyzer cargo soketto 0.8.1 handshake/Error#HeaderNotFound#(_) |
| 225 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnexpectedHeaderrust-analyzer cargo soketto 0.8.1 handshake/Error#UnexpectedHeader#(_) |
| 226 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::InvalidSecWebSocketAcceptrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidSecWebSocketAccept# |
| 227 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsolicitedExtensionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedExtension# |
| 228 | | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsolicitedProtocolrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedProtocol# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> for Errorrust-analyzer cargo soketto 0.8.1 handshake/Error# { |
| 234 | fn fromrust-analyzer cargo soketto 0.8.1 handshake/impl#[Error][`From<io::Error>`]from().(e: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#) -> Selfrust-analyzer cargo soketto 0.8.1 handshake/impl#[Error][`From<io::Error>`] { |
| 235 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Iorust-analyzer cargo soketto 0.8.1 handshake/Error#Io#(e) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::Utf8Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/Utf8Error#> for Errorrust-analyzer cargo soketto 0.8.1 handshake/Error# { |
| 240 | fn fromrust-analyzer cargo soketto 0.8.1 handshake/impl#[Error][`From<str::Utf8Error>`]from().(e: strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::Utf8Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/error/Utf8Error#) -> Selfrust-analyzer cargo soketto 0.8.1 handshake/impl#[Error][`From<str::Utf8Error>`] { |
| 241 | Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Utf8rust-analyzer cargo soketto 0.8.1 handshake/Error#Utf8#(e) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// Owned value of the `Sec-WebSocket-Key` header. |
| 246 | /// |
| 247 | /// Per [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455#section-4.1): |
| 248 | /// |
| 249 | /// ```text |
| 250 | /// (...) The value of this header field MUST be a |
| 251 | /// nonce consisting of a randomly selected 16-byte value that has |
| 252 | /// been base64-encoded (see Section 4 of [RFC4648]). (...) |
| 253 | /// ``` |
| 254 | /// |
| 255 | /// Base64 encoding of the nonce produces 24 ASCII bytes, padding included. |
| 256 | pub type WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey# = [u8; 24]; |
| 257 | |
| 258 | #[cfg(test)] |
| 259 | mod testsrust-analyzer cargo soketto 0.8.1 handshake/tests/ { |
| 260 | use superrust-analyzer cargo soketto 0.8.1 handshake/::expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().; |
| 261 | |
| 262 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 263 | fn header_matchrust-analyzer cargo soketto 0.8.1 handshake/tests/header_match().() { |
| 264 | let headers = &[ |
| 265 | httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header# { namerust-analyzer cargo httparse 1.10.1 Header#name.: "foo", valuerust-analyzer cargo httparse 1.10.1 Header#value.: b"a,b,c,d" }, |
| 266 | httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header# { namerust-analyzer cargo httparse 1.10.1 Header#name.: "foo", valuerust-analyzer cargo httparse 1.10.1 Header#value.: b"x" }, |
| 267 | httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header# { namerust-analyzer cargo httparse 1.10.1 Header#name.: "foo", valuerust-analyzer cargo httparse 1.10.1 Header#value.: b"y, z, a" }, |
| 268 | httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header# { namerust-analyzer cargo httparse 1.10.1 Header#name.: "bar", valuerust-analyzer cargo httparse 1.10.1 Header#value.: b"xxx" }, |
| 269 | httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header# { namerust-analyzer cargo httparse 1.10.1 Header#name.: "bar", valuerust-analyzer cargo httparse 1.10.1 Header#value.: b"sdfsdf 423 42 424" }, |
| 270 | httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header# { namerust-analyzer cargo httparse 1.10.1 Header#name.: "baz", valuerust-analyzer cargo httparse 1.10.1 Header#value.: b"123" }, |
| 271 | ]; |
| 272 | |
| 273 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "a").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 274 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "b").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 275 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "c").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 276 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "d").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 277 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "x").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 278 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "y").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 279 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "z").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 280 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "foo", "a").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 281 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "bar", "xxx").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 282 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "bar", "sdfsdf 423 42 424").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 283 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "baz", "123").is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 284 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "baz", "???").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 285 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(headers, "???", "x").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 286 | } |
| 287 | } |