Debian Codegraph @

src/handshake/server.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 server [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#,
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#;
19 use bytesrust-analyzer cargo bytes 1.12.1 crate/::BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#;
20 use futuresrust-analyzer cargo futures 0.3.34 crate/::preluderust-analyzer cargo futures 0.3.34 prelude/::*;
21 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/};
22
23 // Most HTTP servers default to 8KB limit on headers
24 const MAX_HEADERS_SIZErust-analyzer cargo soketto 0.8.1 handshake/server/MAX_HEADERS_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;
25 const BLOCK_SIZErust-analyzer cargo soketto 0.8.1 handshake/server/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;
26
27 /// Websocket handshake server.
28 #[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:)]
29 pub struct Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#<'a, T> {
30 socketrust-analyzer cargo soketto 0.8.1 handshake/server/Server#socket.: T,
31 /// Protocols the server supports.
32 protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#protocols.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&'a str>,
33 /// Extensions the server supports.
34 extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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#>>,
35 /// Encoding/decoding buffer.
36 bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer.: BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#,
37 }
38
39 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#> Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#<'a, T> {
40 /// Create a new server handshake.
41 pub fn newrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]new().(socket: T) -> Selfrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`] {
42 Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server# { socketrust-analyzer cargo soketto 0.8.1 handshake/server/Server#socket., protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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().(), extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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().(), bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer.: BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::newrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]new().() }
43 }
44
45 /// Override the buffer to use for request/response handling.
46 pub fn set_bufferrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'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/server/impl#[`Server<'a, T>`] {
47 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer. = b;
48 self
49 }
50
51 /// Extract the buffer.
52 pub fn take_bufferrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]take_buffer().(&mut self) -> BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut# {
53 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/server/Server#buffer.)
54 }
55
56 /// Add a protocol the server supports.
57 pub fn add_protocolrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]add_protocol().(&mut self, p: &'a str) -> &mut Selfrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`] {
58 self.protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#protocols..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(p);
59 self
60 }
61
62 /// Add an extension the server supports.
63 pub fn add_extensionrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'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/server/impl#[`Server<'a, T>`] {
64 self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#extensions..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(e);
65 self
66 }
67
68 /// Get back all extensions.
69 pub fn drain_extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'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#>> + '_ {
70 self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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#)
71 }
72
73 /// Await an incoming client handshake request.
74 pub async fn receive_requestrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]receive_request().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ClientRequestrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#<'_>, Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> {
75 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..clearrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]clear().();
76
77 let mut skip = 0;
78
79 loop {
80 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/server/Server#socket., &mut self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer., BLOCK_SIZErust-analyzer cargo soketto 0.8.1 handshake/server/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().;
81
82 let limit = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().(), MAX_HEADERS_SIZErust-analyzer cargo soketto 0.8.1 handshake/server/MAX_HEADERS_SIZE.);
83
84 // We don't expect body, so can search for the CRLF headers tail from
85 // the end of the buffer.
86 if self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().skip..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#limit]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..windowsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]windows().(4).revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|w| w 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().b"\r\n\r\n") {
87 break;
88 }
89
90 // Give up if we've reached the limit. We could emit a specific error here,
91 // but httparse will produce meaningful error for us regardless.
92 if limit rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().MAX_HEADERS_SIZErust-analyzer cargo soketto 0.8.1 handshake/server/MAX_HEADERS_SIZE. {
93 break;
94 }
95
96 // Skip bytes that did not contain CRLF in the next iteration.
97 // If we only read a partial CRLF sequence, we would miss it if we skipped the full buffer
98 // length, hence backing off the full 4 bytes.
99 skip = self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().().saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(4);
100 }
101
102 self.decode_requestrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]decode_request().()
103 }
104
105 /// Respond to the client.
106 pub async fn send_responserust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]send_response().(&mut self, r: &Responserust-analyzer cargo soketto 0.8.1 handshake/server/Response#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> {
107 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..clearrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]clear().();
108 self.encode_responserust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]encode_response().(r);
109 self.socketrust-analyzer cargo soketto 0.8.1 handshake/server/Server#socket..write_allrust-analyzer cargo futures-util 0.3.34 io/AsyncWriteExt#write_all().(&self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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().;
110 self.socketrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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().;
111 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..clearrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]clear().();
112 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
113 }
114
115 /// Turn this handshake into a [`connection::Builder`].
116 pub fn into_builderrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]into_builder().(mut self) -> connectionrust-analyzer cargo soketto 0.8.1 connection/::Builderrust-analyzer cargo soketto 0.8.1 connection/Builder#<T> {
117 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/server/Server#socket., Moderust-analyzer cargo soketto 0.8.1 connection/Mode#::Serverrust-analyzer cargo soketto 0.8.1 connection/Mode#Server#);
118 builder.set_bufferrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]set_buffer().(self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer.);
119 builder.add_extensionsrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]add_extensions().(self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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#));
120 builder
121 }
122
123 /// Get out the inner socket of the server.
124 pub fn into_innerrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]into_inner().(self) -> T {
125 self.socketrust-analyzer cargo soketto 0.8.1 handshake/server/Server#socket.
126 }
127
128 // Decode client handshake request.
129 fn decode_requestrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]decode_request().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ClientRequestrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#, Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#> {
130 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.];
131 let mut request = httparserust-analyzer cargo httparse 1.10.1 crate/::Requestrust-analyzer cargo httparse 1.10.1 Request#::newrust-analyzer cargo httparse 1.10.1 impl#[`Request<'h, 'b>`]new().(&mut header_buf);
132
133 match request.parserust-analyzer cargo httparse 1.10.1 impl#[`Request<'h, 'b>`]parse().(self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..as_refrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][`AsRef<[u8]>`]as_ref().()) {
134 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#(_)) => (),
135 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 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo soketto 0.8.1 handshake/Error#::IncompleteHttpRequestrust-analyzer cargo soketto 0.8.1 handshake/Error#IncompleteHttpRequest#),
136 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))),
137 };
138 if request.methodrust-analyzer cargo httparse 1.10.1 Request#method. 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#("GET") {
139 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#::InvalidRequestMethodrust-analyzer cargo soketto 0.8.1 handshake/Error#InvalidRequestMethod#);
140 }
141 if request.versionrust-analyzer cargo httparse 1.10.1 Request#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) {
142 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#);
143 }
144
145 let host = with_first_headerrust-analyzer cargo soketto 0.8.1 handshake/with_first_header().(&request.headersrust-analyzer cargo httparse 1.10.1 Request#headers., "Host", Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
146
147 expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(request.headersrust-analyzer cargo httparse 1.10.1 Request#headers., "Upgrade", "websocket")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
148 expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(request.headersrust-analyzer cargo httparse 1.10.1 Request#headers., "Connection", "upgrade")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
149 expect_ascii_headerrust-analyzer cargo soketto 0.8.1 handshake/expect_ascii_header().(request.headersrust-analyzer cargo httparse 1.10.1 Request#headers., "Sec-WebSocket-Version", "13")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
150
151 let origin =
152 request.headersrust-analyzer cargo httparse 1.10.1 Request#headers..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().find_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find_map().(
153 |h| {
154 if 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().("Origin") {
155 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(h.valuerust-analyzer cargo httparse 1.10.1 Header#value.)
156 } else {
157 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
158 }
159 },
160 );
161 let headers = RequestHeadersrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders# { hostrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders#host., originrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders#origin. };
162
163 let ws_key = with_first_headerrust-analyzer cargo soketto 0.8.1 handshake/with_first_header().(&request.headersrust-analyzer cargo httparse 1.10.1 Request#headers., "Sec-WebSocket-Key", |k| {
164 WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey#::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`TryFrom<&[T]>`]try_from().(k).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#::SecWebSocketKeyInvalidLengthrust-analyzer cargo soketto 0.8.1 handshake/Error#SecWebSocketKeyInvalidLength#(k.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()))
165 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
166
167 for h in request.headersrust-analyzer cargo httparse 1.10.1 Request#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.)) {
168 configure_extensionsrust-analyzer cargo soketto 0.8.1 handshake/configure_extensions().(&mut self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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().
169 }
170
171 let mut 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().();
172 for p in request.headersrust-analyzer cargo httparse 1.10.1 Request#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_PROTOCOLrust-analyzer cargo soketto 0.8.1 handshake/SEC_WEBSOCKET_PROTOCOL.)) {
173 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/server/Server#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().p.valuerust-analyzer cargo httparse 1.10.1 Header#value.) {
174 protocols.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(p)
175 }
176 }
177
178 let path = request.pathrust-analyzer cargo httparse 1.10.1 Request#path..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().("/");
179
180 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ClientRequestrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest# { ws_keyrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#ws_key., protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#protocols., pathrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#path., headersrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#headers. })
181 }
182
183 // Encode server handshake response.
184 fn encode_responserust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]encode_response().(&mut self, response: &Responserust-analyzer cargo soketto 0.8.1 handshake/server/Response#<'_>) {
185 match response {
186 Responserust-analyzer cargo soketto 0.8.1 handshake/server/Response#::Acceptrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept# { keyrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept#key., protocolrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept#protocol. } => {
187 let accept_value = superrust-analyzer cargo soketto 0.8.1 handshake/::generate_accept_keyrust-analyzer cargo soketto 0.8.1 handshake/generate_accept_key().(&key);
188 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(
189 concatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/concat!![
190 "HTTP/1.1 101 Switching Protocols",
191 "\r\nServer: soketto-",
192 envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_PKG_VERSION"),
193 "\r\nUpgrade: websocket",
194 "\r\nConnection: upgrade",
195 "\r\nSec-WebSocket-Accept: ",
196 ]
197 .as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(),
198 );
199 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(&accept_value);
200 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(p) = protocol {
201 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\nSec-WebSocket-Protocol: ");
202 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#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().())
203 }
204 append_extensionsrust-analyzer cargo soketto 0.8.1 handshake/append_extensions().(self.extensionsrust-analyzer cargo soketto 0.8.1 handshake/server/Server#extensions..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().(|e| e.is_enabledrust-analyzer cargo soketto 0.8.1 extension/impl#[`Box<E>`][Extension]is_enabled().()), &mut self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer.);
205 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\n\r\n")
206 }
207 Responserust-analyzer cargo soketto 0.8.1 handshake/server/Response#::Rejectrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Reject# { status_coderust-analyzer cargo soketto 0.8.1 handshake/server/Response#Reject#status_code. } => {
208 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"HTTP/1.1 ");
209 let (_, reason) = if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(i) = STATUSCODESrust-analyzer cargo soketto 0.8.1 handshake/server/STATUSCODES..binary_search_by_keyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search_by_key().(status_code, |(n, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().n) {
210 STATUSCODESrust-analyzer cargo soketto 0.8.1 handshake/server/STATUSCODES.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
211 } else {
212 (500, "500 Internal Server Error")
213 };
214 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(reason.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
215 self.bufferrust-analyzer cargo soketto 0.8.1 handshake/server/Server#buffer..extend_from_slicerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]extend_from_slice().(b"\r\n\r\n")
216 }
217 }
218 }
219 }
220
221 /// Handshake request received from the client.
222 #[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:)]
223 pub struct ClientRequestrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#<'a> {
224 ws_keyrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#ws_key.: WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey#,
225 protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#protocols.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&'a str>,
226 pathrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#path.: &'a str,
227 headersrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#headers.: RequestHeadersrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders#<'a>,
228 }
229
230 /// Select HTTP headers sent by the client.
231 #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
232 pub struct RequestHeadersrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders#<'a> {
233 /// The [`Host`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host) header.
234 pub hostrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders#host.: &'a [u8],
235 /// The [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin) header, if provided.
236 pub originrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders#origin.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a [u8]>,
237 }
238
239 impl<'a> ClientRequestrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#<'a> {
240 /// The `Sec-WebSocket-Key` header nonce value.
241 pub fn keyrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`ClientRequest<'a>`]key().(&self) -> WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey# {
242 self.ws_keyrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#ws_key.
243 }
244
245 /// The protocols the client is proposing.
246 pub fn protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`ClientRequest<'a>`]protocols().(&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# = &str> {
247 self.protocolsrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#protocols..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().()
248 }
249
250 /// The path the client is requesting.
251 pub fn pathrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`ClientRequest<'a>`]path().(&self) -> &str {
252 self.pathrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#path.
253 }
254
255 /// Select HTTP headers sent by the client.
256 pub fn headersrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`ClientRequest<'a>`]headers().(&self) -> RequestHeadersrust-analyzer cargo soketto 0.8.1 handshake/server/RequestHeaders# {
257 self.headersrust-analyzer cargo soketto 0.8.1 handshake/server/ClientRequest#headers.
258 }
259 }
260
261 /// Handshake response the server sends back to the client.
262 #[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:)]
263 pub enum Responserust-analyzer cargo soketto 0.8.1 handshake/server/Response#<'a> {
264 /// The server accepts the handshake request.
265 Acceptrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept# { keyrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept#key.: WebSocketKeyrust-analyzer cargo soketto 0.8.1 handshake/WebSocketKey#, protocolrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept#protocol.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str> },
266 /// The server rejects the handshake request.
267 Rejectrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Reject# { status_coderust-analyzer cargo soketto 0.8.1 handshake/server/Response#Reject#status_code.: u16 },
268 }
269
270 /// Known status codes and their reason phrases.
271 const STATUSCODESrust-analyzer cargo soketto 0.8.1 handshake/server/STATUSCODES.: &[(u16, &str)] = &[
272 (100, "100 Continue"),
273 (101, "101 Switching Protocols"),
274 (102, "102 Processing"),
275 (200, "200 OK"),
276 (201, "201 Created"),
277 (202, "202 Accepted"),
278 (203, "203 Non Authoritative Information"),
279 (204, "204 No Content"),
280 (205, "205 Reset Content"),
281 (206, "206 Partial Content"),
282 (207, "207 Multi-Status"),
283 (208, "208 Already Reported"),
284 (226, "226 IM Used"),
285 (300, "300 Multiple Choices"),
286 (301, "301 Moved Permanently"),
287 (302, "302 Found"),
288 (303, "303 See Other"),
289 (304, "304 Not Modified"),
290 (305, "305 Use Proxy"),
291 (307, "307 Temporary Redirect"),
292 (308, "308 Permanent Redirect"),
293 (400, "400 Bad Request"),
294 (401, "401 Unauthorized"),
295 (402, "402 Payment Required"),
296 (403, "403 Forbidden"),
297 (404, "404 Not Found"),
298 (405, "405 Method Not Allowed"),
299 (406, "406 Not Acceptable"),
300 (407, "407 Proxy Authentication Required"),
301 (408, "408 Request Timeout"),
302 (409, "409 Conflict"),
303 (410, "410 Gone"),
304 (411, "411 Length Required"),
305 (412, "412 Precondition Failed"),
306 (413, "413 Payload Too Large"),
307 (414, "414 URI Too Long"),
308 (415, "415 Unsupported Media Type"),
309 (416, "416 Range Not Satisfiable"),
310 (417, "417 Expectation Failed"),
311 (418, "418 I'm a teapot"),
312 (421, "421 Misdirected Request"),
313 (422, "422 Unprocessable Entity"),
314 (423, "423 Locked"),
315 (424, "424 Failed Dependency"),
316 (426, "426 Upgrade Required"),
317 (428, "428 Precondition Required"),
318 (429, "429 Too Many Requests"),
319 (431, "431 Request Header Fields Too Large"),
320 (451, "451 Unavailable For Legal Reasons"),
321 (500, "500 Internal Server Error"),
322 (501, "501 Not Implemented"),
323 (502, "502 Bad Gateway"),
324 (503, "503 Service Unavailable"),
325 (504, "504 Gateway Timeout"),
326 (505, "505 HTTP Version Not Supported"),
327 (506, "506 Variant Also Negotiates"),
328 (507, "507 Insufficient Storage"),
329 (508, "508 Loop Detected"),
330 (510, "510 Not Extended"),
331 (511, "511 Network Authentication Required"),
332 ];