Debian Codegraph @

src/handshake/client.rs

raw symbols SCIP
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 client [handshake].
10 //!
11 //! [handshake]: https://tools.ietf.org/html/rfc6455#section-4
12
13 use superrust-analyzer cargo soketto 0.8.1 handshake/::{
14 append_extensionsrust-analyzer cargo soketto 0.8.1 handshake/append_extensions()., configure_extensionsrust-analyzer cargo soketto 0.8.1 handshake/configure_extensions()., expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header()., with_first_headerrust-analyzer cargo soketto 0.8.1 handshake/with_first_header()., Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#, WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey#, KEYrust-analyzer cargo soketto 0.8.1 handshake/KEY.,
15 MAX_NUM_HEADERSrust-analyzer cargo soketto 0.8.1 handshake/MAX_NUM_HEADERS., SEC_WEBSOCKET_EXTENSIONSrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_EXTENSIONS., SEC_WEBSOCKET_PROTOCOLrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_PROTOCOL.,
16 };
17 use craterust-analyzer cargo soketto 0.8.1 crate/::connectionrust-analyzer cargo soketto 0.8.1 connection/::{selfrust-analyzer cargo soketto 0.8.1 connection/, Moderust-analyzer cargo soketto 0.8.1 connection/Mode#};
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#, Parsingrust-analyzer cargo soketto 0.8.1 Parsing#};
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/::{Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#, BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#};
21 use futuresrust-analyzer cargo futures 0.3.34 crate/::preluderust-analyzer cargo futures 0.3.34 prelude/::*;
22 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#};
23 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/, strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/};
24
25 pub use httparserust-analyzer cargo httparse 1.10.1 crate/::Headerrust-analyzer cargo httparse 1.10.1 Header#;
26
27 const BLOCK_SIZErust-analyzer cargo soketto 0.8.1 handshake/client/BLOCK_SIZE.: usize = 8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024;
28
29 /// Websocket client handshake.
30 #[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:)]
31 pub struct Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#<'a, T> {
32 /// The underlying async I/O resource.
33 socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket.: T,
34 /// The HTTP host to send the handshake to.
35 hostrust-analyzer cargo soketto 0.8.1 handshake/client/Client#host.: &'a str,
36 /// The HTTP host resource.
37 resourcerust-analyzer cargo soketto 0.8.1 handshake/client/Client#resource.: &'a str,
38 /// The HTTP headers.
39 headersrust-analyzer cargo soketto 0.8.1 handshake/client/Client#headers.: &'a [Headerrust-analyzer cargo httparse 1.10.1 Header#<'a>],
40 /// A buffer holding the base-64 encoded request nonce.
41 noncerust-analyzer cargo soketto 0.8.1 handshake/client/Client#nonce.: WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey#,
42 /// The protocols to include in the handshake.
43 protocolsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#protocols.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&'a str>,
44 /// The extensions the client wishes to include in the request.
45 extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<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#>>,
46 /// Encoding/decoding buffer.
47 bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer.: BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#,
48 }
49
50 impl<'a, T: AsyncReadrust-analyzer cargo futures-io 0.3.34 if_std/AsyncRead# + AsyncWriterust-analyzer cargo futures-io 0.3.34 if_std/AsyncWrite# + Unpinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Unpin#> Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client#<'a, T> {
51 /// Create a new client handshake for some host and resource.
52 pub fn newrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]new().(socket: T, host: &'a str, resource: &'a str) -> Selfrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`] {
53 Clientrust-analyzer cargo soketto 0.8.1 handshake/client/Client# {
54 socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket.,
55 hostrust-analyzer cargo soketto 0.8.1 handshake/client/Client#host.,
56 resourcerust-analyzer cargo soketto 0.8.1 handshake/client/Client#resource.,
57 headersrust-analyzer cargo soketto 0.8.1 handshake/client/Client#headers.: &[],
58 noncerust-analyzer cargo soketto 0.8.1 handshake/client/Client#nonce.: [0; 24],
59 protocolsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#protocols.: 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().(),
60 extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions.: 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().(),
61 bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer.: BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::newrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]new().(),
62 }
63 }
64
65 /// Override the buffer to use for request/response handling.
66 pub fn set_bufferrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]set_buffer().(&mut self, b: BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> &mut Selfrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`] {
67 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer. = b;
68 self
69 }
70
71 /// Extract the buffer.
72 pub fn take_bufferrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]take_buffer().(&mut self) -> BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut# {
73 memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/take().(&mut self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer.)
74 }
75
76 /// Set connection headers to a slice. These headers are not checked for validity,
77 /// the caller of this method is responsible for verification as well as avoiding
78 /// conflicts with internally set headers.
79 pub fn set_headersrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]set_headers().(&mut self, h: &'a [Headerrust-analyzer cargo httparse 1.10.1 Header#]) -> &mut Selfrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`] {
80 self.headersrust-analyzer cargo soketto 0.8.1 handshake/client/Client#headers. = h;
81 self
82 }
83
84 /// Add a protocol to be included in the handshake.
85 pub fn add_protocolrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]add_protocol().(&mut self, p: &'a str) -> &mut Selfrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`] {
86 self.protocolsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#protocols..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(p);
87 self
88 }
89
90 /// Add an extension to be included in the handshake.
91 pub fn add_extensionrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]add_extension().(&mut self, e: 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#>) -> &mut Selfrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`] {
92 self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(e);
93 self
94 }
95
96 /// Get back all extensions.
97 pub fn drain_extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]drain_extensions().(&mut self) -> impl 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# = 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#>> + '_ {
98 self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions..drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]drain().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#)
99 }
100
101 /// Initiate client handshake request to server and get back the response.
102 pub async fn handshakerust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]handshake().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#, Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> {
103 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..clearrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]clear().();
104 self.encode_requestrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]encode_request().();
105 self.socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket..write_allrust-analyzer cargo futures-util 0.3.34 io/AsyncWriteExt#write_all().(&self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer.).rust-analyzer cargo futures-util 0.3.34 io/write_all/impl#[`WriteAll<'_, W>`][Future]poll().awaitrust-analyzer cargo futures-util 0.3.34 io/write_all/impl#[`WriteAll<'_, W>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
106 self.socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket..flushrust-analyzer cargo futures-util 0.3.34 io/AsyncWriteExt#flush().().rust-analyzer cargo futures-util 0.3.34 io/flush/impl#[`Flush<'_, W>`][Future]poll().awaitrust-analyzer cargo futures-util 0.3.34 io/flush/impl#[`Flush<'_, W>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
107 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..clearrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]clear().();
108
109 loop {
110 craterust-analyzer cargo soketto 0.8.1 crate/::readrust-analyzer cargo soketto 0.8.1 read().(&mut self.socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket., &mut self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer., BLOCK_SIZErust-analyzer cargo soketto 0.8.1 handshake/client/BLOCK_SIZE.).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
111 if let Parsingrust-analyzer cargo soketto 0.8.1 Parsing#::Donerust-analyzer cargo soketto 0.8.1 Parsing#Done# { valuerust-analyzer cargo soketto 0.8.1 Parsing#Done#value., offsetrust-analyzer cargo soketto 0.8.1 Parsing#Done#offset. } = self.decode_responserust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]decode_response().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
112 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..advancerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][Buf]advance().(offset);
113 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value);
114 }
115 }
116 }
117
118 /// Turn this handshake into a [`connection::Builder`].
119 pub fn into_builderrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]into_builder().(mut self) -> connectionrust-analyzer cargo soketto 0.8.1 connection/::Builderrust-analyzer cargo soketto 0.8.1 connection/Builder#<T> {
120 let mut builder = connectionrust-analyzer cargo soketto 0.8.1 connection/::Builderrust-analyzer cargo soketto 0.8.1 connection/Builder#::newrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]new().(self.socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket., Moderust-analyzer cargo soketto 0.8.1 connection/Mode#::Clientrust-analyzer cargo soketto 0.8.1 connection/Mode#Client#);
121 builder.set_bufferrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]set_buffer().(self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer.);
122 builder.add_extensionsrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]add_extensions().(self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions..drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]drain().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#));
123 builder
124 }
125
126 /// Get out the inner socket of the client.
127 pub fn into_innerrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]into_inner().(self) -> T {
128 self.socketrust-analyzer cargo soketto 0.8.1 handshake/client/Client#socket.
129 }
130
131 /// Encode the client handshake as a request, ready to be sent to the server.
132 fn encode_requestrust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]encode_request().(&mut self) {
133 let nonce: [u8; 16] = randrust-analyzer cargo rand 0.8.7 crate/::randomrust-analyzer cargo rand 0.8.7 random().();
134 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.
135 .encode_slicerust-analyzer cargo base64 0.22.1 engine/Engine#encode_slice().(nonce, &mut self.noncerust-analyzer cargo soketto 0.8.1 handshake/client/Client#nonce.)
136 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("encoding to base64 is exactly 16 bytes; qed");
137 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"GET ");
138 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(self.resourcerust-analyzer cargo soketto 0.8.1 handshake/client/Client#resource..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
139 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b" HTTP/1.1");
140 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nHost: ");
141 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(self.hostrust-analyzer cargo soketto 0.8.1 handshake/client/Client#host..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
142 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nUpgrade: websocket\r\nConnection: Upgrade");
143 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nSec-WebSocket-Key: ");
144 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(&self.noncerust-analyzer cargo soketto 0.8.1 handshake/client/Client#nonce.);
145 self.headersrust-analyzer cargo soketto 0.8.1 handshake/client/Client#headers..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]for_each().(|h| {
146 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\n");
147 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(h.namerust-analyzer cargo httparse 1.10.1 Header#name..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
148 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b": ");
149 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(h.valuerust-analyzer cargo httparse 1.10.1 Header#value.);
150 });
151 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((last, prefix)) = self.protocolsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#protocols..split_lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_last().() {
152 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nSec-WebSocket-Protocol: ");
153 for p in prefix {
154 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(p.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
155 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b",")
156 }
157 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(last.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
158 }
159 append_extensionsrust-analyzer cargo soketto 0.8.1 handshake/append_extensions().(&self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions., &mut self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer.);
160 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nSec-WebSocket-Version: 13\r\n\r\n")
161 }
162
163 /// Decode the server response to this client request.
164 fn decode_responserust-analyzer cargo soketto 0.8.1 handshake/client/impl#[`Client<'a, T>`]decode_response().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Parsingrust-analyzer cargo soketto 0.8.1 Parsing#<ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#>, Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> {
165 let mut header_buf = [httparserust-analyzer cargo httparse 1.10.1 crate/::EMPTY_HEADERrust-analyzer cargo httparse 1.10.1 EMPTY_HEADER.; MAX_NUM_HEADERSrust-analyzer cargo soketto 0.8.1 handshake/MAX_NUM_HEADERS.];
166 let mut response = httparserust-analyzer cargo httparse 1.10.1 crate/::Responserust-analyzer cargo httparse 1.10.1 Response#::newrust-analyzer cargo httparse 1.10.1 impl#[`Response<'h, 'b>`]new().(&mut header_buf);
167
168 let offset = match response.parserust-analyzer cargo httparse 1.10.1 impl#[`Response<'h, 'b>`]parse().(self.bufferrust-analyzer cargo soketto 0.8.1 handshake/client/Client#buffer..as_refrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][`AsRef<[u8]>`]as_ref().()) {
169 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(httparserust-analyzer cargo httparse 1.10.1 crate/::Statusrust-analyzer cargo httparse 1.10.1 Status#::Completerust-analyzer cargo httparse 1.10.1 Status#Complete#(off)) => off,
170 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(httparserust-analyzer cargo httparse 1.10.1 crate/::Statusrust-analyzer cargo httparse 1.10.1 Status#::Partialrust-analyzer cargo httparse 1.10.1 Status#Partial#) => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parsingrust-analyzer cargo soketto 0.8.1 Parsing#::NeedMorerust-analyzer cargo soketto 0.8.1 Parsing#NeedMore#(())),
171 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::Httprust-analyzer cargo soketto 0.8.1 handshake/Error#Http#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(e))),
172 };
173
174 if response.versionrust-analyzer cargo httparse 1.10.1 Response#version. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1) {
175 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsupportedHttpVersionrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsupportedHttpVersion#);
176 }
177
178 match response.coderust-analyzer cargo httparse 1.10.1 Response#code. {
179 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(101) => (),
180 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(code @ (301..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#303)) | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(code @ 307) | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(code @ 308) => {
181 // redirect response
182 let location =
183 with_first_headerrust-analyzer cargo soketto 0.8.1 handshake/with_first_header().(response.headersrust-analyzer cargo httparse 1.10.1 Response#headers., "Location", |loc| Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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().(loc)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().)))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
184 let response = ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#::Redirectrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Redirect# { status_coderust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Redirect#status_code.: code, locationrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Redirect#location. };
185 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parsingrust-analyzer cargo soketto 0.8.1 Parsing#::Donerust-analyzer cargo soketto 0.8.1 Parsing#Done# { valuerust-analyzer cargo soketto 0.8.1 Parsing#Done#value.: response, offsetrust-analyzer cargo soketto 0.8.1 Parsing#Done#offset. });
186 }
187 other => {
188 let response = ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#::Rejectedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Rejected# { status_coderust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Rejected#status_code.: other.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0) };
189 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parsingrust-analyzer cargo soketto 0.8.1 Parsing#::Donerust-analyzer cargo soketto 0.8.1 Parsing#Done# { valuerust-analyzer cargo soketto 0.8.1 Parsing#Done#value.: response, offsetrust-analyzer cargo soketto 0.8.1 Parsing#Done#offset. });
190 }
191 }
192
193 expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(response.headersrust-analyzer cargo httparse 1.10.1 Response#headers., "Upgrade", "websocket")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
194 expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(response.headersrust-analyzer cargo httparse 1.10.1 Response#headers., "Connection", "upgrade")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
195
196 with_first_headerrust-analyzer cargo soketto 0.8.1 handshake/with_first_header().(&response.headersrust-analyzer cargo httparse 1.10.1 Response#headers., "Sec-WebSocket-Accept", |theirs| {
197 let mut digest = Sha1rust-analyzer cargo sha1 0.10.7 Sha1#::newrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]new().();
198 digest.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(&self.noncerust-analyzer cargo soketto 0.8.1 handshake/client/Client#nonce.);
199 digest.updaterust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]update().(KEYrust-analyzer cargo soketto 0.8.1 handshake/KEY.);
200 let ours = 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..encoderust-analyzer cargo base64 0.22.1 engine/Engine#encode().(digest.finalizerust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]finalize().());
201 if ours.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().theirs {
202 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::InvalidSecWebSocketAcceptrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidSecWebSocketAccept#);
203 }
204 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
205 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
206
207 // Parse `Sec-WebSocket-Extensions` headers.
208
209 for h in response.headersrust-analyzer cargo httparse 1.10.1 Response#headers..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().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().(SEC_WEBSOCKET_EXTENSIONSrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_EXTENSIONS.)) {
210 configure_extensionsrust-analyzer cargo soketto 0.8.1 handshake/configure_extensions().(&mut self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#extensions., stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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().(h.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().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
211 }
212
213 // Match `Sec-WebSocket-Protocol` header.
214
215 let mut selected_proto = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
216 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tp) = response.headersrust-analyzer cargo httparse 1.10.1 Response#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().(SEC_WEBSOCKET_PROTOCOLrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_PROTOCOL.)) {
217 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&p) = self.protocolsrust-analyzer cargo soketto 0.8.1 handshake/client/Client#protocols..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().(|x| x.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().tp.valuerust-analyzer cargo httparse 1.10.1 Header#value.) {
218 selected_proto = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().(p))
219 } else {
220 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::UnsolicitedProtocolrust-analyzer cargo soketto 0.8.1 handshake/Error#UnsolicitedProtocol#);
221 }
222 }
223
224 let response = ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#::Acceptedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted# { protocolrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted#protocol.: selected_proto };
225 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parsingrust-analyzer cargo soketto 0.8.1 Parsing#::Donerust-analyzer cargo soketto 0.8.1 Parsing#Done# { valuerust-analyzer cargo soketto 0.8.1 Parsing#Done#value.: response, offsetrust-analyzer cargo soketto 0.8.1 Parsing#Done#offset. })
226 }
227 }
228
229 /// Handshake response received from the server.
230 #[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:)]
231 pub enum ServerResponserust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse# {
232 /// The server has accepted our request.
233 Acceptedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted# {
234 /// The protocol (if any) the server has selected.
235 protocolrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Accepted#protocol.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
236 },
237 /// The server is redirecting us to some other location.
238 Redirectrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Redirect# {
239 /// The HTTP response status code.
240 status_coderust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Redirect#status_code.: u16,
241 /// The location URL we should go to.
242 locationrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Redirect#location.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
243 },
244 /// The server rejected our request.
245 Rejectedrust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Rejected# {
246 /// HTTP response status code.
247 status_coderust-analyzer cargo soketto 0.8.1 handshake/client/ServerResponse#Rejected#status_code.: u16,
248 },
249 }