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 super::{
14 append_extensions, configure_extensions, expect_ascii_header, with_first_header, Error, WebSocketKey,
15 MAX_NUM_HEADERS, SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_PROTOCOL,
16 };
17 use crate::connection::{self, Mode};
18 use crate::extension::Extension;
19 use bytes::BytesMut;
20 use futures::prelude::*;
21 use std::{mem, str};
22
23 // Most HTTP servers default to 8KB limit on headers
24 const MAX_HEADERS_SIZE: usize = 8 * 1024;
25 const BLOCK_SIZE: usize = 8 * 1024;
26
27 /// Websocket handshake server.
28 #[derive(Debug)]
29 pub struct Server<'a, T> {
30 socket: T,
31 /// Protocols the server supports.
32 protocols: Vec<&'a str>,
33 /// Extensions the server supports.
34 extensions: Vec<Box<dyn Extension + Send>>,
35 /// Encoding/decoding buffer.
36 buffer: BytesMut,
37 }
38
39 impl<'a, T: AsyncRead + AsyncWrite + Unpin> Server<'a, T> {
40 /// Create a new server handshake.
41 pub fn new(socket: T) -> Self {
42 Server { socket, protocols: Vec::new(), extensions: Vec::new(), buffer: BytesMut::new() }
43 }
44
45 /// Override the buffer to use for request/response handling.
46 pub fn set_buffer(&mut self, b: BytesMut) -> &mut Self {
47 self.buffer = b;
48 self
49 }
50
51 /// Extract the buffer.
52 pub fn take_buffer(&mut self) -> BytesMut {
53 mem::take(&mut self.buffer)
54 }
55
56 /// Add a protocol the server supports.
57 pub fn add_protocol(&mut self, p: &'a str) -> &mut Self {
58 self.protocols.push(p);
59 self
60 }
61
62 /// Add an extension the server supports.
63 pub fn add_extension(&mut self, e: Box<dyn Extension + Send>) -> &mut Self {
64 self.extensions.push(e);
65 self
66 }
67
68 /// Get back all extensions.
69 pub fn drain_extensions(&mut self) -> impl Iterator<Item = Box<dyn Extension + Send>> + '_ {
70 self.extensions.drain(..)
71 }
72
73 /// Await an incoming client handshake request.
74 pub async fn receive_request(&mut self) -> Result<ClientRequest<'_>, Error> {
75 self.buffer.clear();
76
77 let mut skip = 0;
78
79 loop {
80 crate::read(&mut self.socket, &mut self.buffer, BLOCK_SIZE).await?;
81
82 let limit = std::cmp::min(self.buffer.len(), 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.buffer[skip..limit].windows(4).rev().any(|w| w == 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 == 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.buffer.len().saturating_sub(4);
100 }
101
102 self.decode_request()
103 }
104
105 /// Respond to the client.
106 pub async fn send_response(&mut self, r: &Response<'_>) -> Result<(), Error> {
107 self.buffer.clear();
108 self.encode_response(r);
109 self.socket.write_all(&self.buffer).await?;
110 self.socket.flush().await?;
111 self.buffer.clear();
112 Ok(())
113 }
114
115 /// Turn this handshake into a [`connection::Builder`].
116 pub fn into_builder(mut self) -> connection::Builder<T> {
117 let mut builder = connection::Builder::new(self.socket, Mode::Server);
118 builder.set_buffer(self.buffer);
119 builder.add_extensions(self.extensions.drain(..));
120 builder
121 }
122
123 /// Get out the inner socket of the server.
124 pub fn into_inner(self) -> T {
125 self.socket
126 }
127
128 // Decode client handshake request.
129 fn decode_request(&mut self) -> Result<ClientRequest, Error> {
130 let mut header_buf = [httparse::EMPTY_HEADER; MAX_NUM_HEADERS];
131 let mut request = httparse::Request::new(&mut header_buf);
132
133 match request.parse(self.buffer.as_ref()) {
134 Ok(httparse::Status::Complete(_)) => (),
135 Ok(httparse::Status::Partial) => return Err(Error::IncompleteHttpRequest),
136 Err(e) => return Err(Error::Http(Box::new(e))),
137 };
138 if request.method != Some("GET") {
139 return Err(Error::InvalidRequestMethod);
140 }
141 if request.version != Some(1) {
142 return Err(Error::UnsupportedHttpVersion);
143 }
144
145 let host = with_first_header(&request.headers, "Host", Ok)?;
146
147 expect_ascii_header(request.headers, "Upgrade", "websocket")?;
148 expect_ascii_header(request.headers, "Connection", "upgrade")?;
149 expect_ascii_header(request.headers, "Sec-WebSocket-Version", "13")?;
150
151 let origin =
152 request.headers.iter().find_map(
153 |h| {
154 if h.name.eq_ignore_ascii_case("Origin") {
155 Some(h.value)
156 } else {
157 None
158 }
159 },
160 );
161 let headers = RequestHeaders { host, origin };
162
163 let ws_key = with_first_header(&request.headers, "Sec-WebSocket-Key", |k| {
164 WebSocketKey::try_from(k).map_err(|_| Error::SecWebSocketKeyInvalidLength(k.len()))
165 })?;
166
167 for h in request.headers.iter().filter(|h| h.name.eq_ignore_ascii_case(SEC_WEBSOCKET_EXTENSIONS)) {
168 configure_extensions(&mut self.extensions, std::str::from_utf8(h.value)?)?
169 }
170
171 let mut protocols = Vec::new();
172 for p in request.headers.iter().filter(|h| h.name.eq_ignore_ascii_case(SEC_WEBSOCKET_PROTOCOL)) {
173 if let Some(&p) = self.protocols.iter().find(|x| x.as_bytes() == p.value) {
174 protocols.push(p)
175 }
176 }
177
178 let path = request.path.unwrap_or("/");
179
180 Ok(ClientRequest { ws_key, protocols, path, headers })
181 }
182
183 // Encode server handshake response.
184 fn encode_response(&mut self, response: &Response<'_>) {
185 match response {
186 Response::Accept { key, protocol } => {
187 let accept_value = super::generate_accept_key(&key);
188 self.buffer.extend_from_slice(
189 concat![
190 "HTTP/1.1 101 Switching Protocols",
191 "\r\nServer: soketto-",
192 env!("CARGO_PKG_VERSION"),
193 "\r\nUpgrade: websocket",
194 "\r\nConnection: upgrade",
195 "\r\nSec-WebSocket-Accept: ",
196 ]
197 .as_bytes(),
198 );
199 self.buffer.extend_from_slice(&accept_value);
200 if let Some(p) = protocol {
201 self.buffer.extend_from_slice(b"\r\nSec-WebSocket-Protocol: ");
202 self.buffer.extend_from_slice(p.as_bytes())
203 }
204 append_extensions(self.extensions.iter().filter(|e| e.is_enabled()), &mut self.buffer);
205 self.buffer.extend_from_slice(b"\r\n\r\n")
206 }
207 Response::Reject { status_code } => {
208 self.buffer.extend_from_slice(b"HTTP/1.1 ");
209 let (_, reason) = if let Ok(i) = STATUSCODES.binary_search_by_key(status_code, |(n, _)| *n) {
210 STATUSCODES[i]
211 } else {
212 (500, "500 Internal Server Error")
213 };
214 self.buffer.extend_from_slice(reason.as_bytes());
215 self.buffer.extend_from_slice(b"\r\n\r\n")
216 }
217 }
218 }
219 }
220
221 /// Handshake request received from the client.
222 #[derive(Debug)]
223 pub struct ClientRequest<'a> {
224 ws_key: WebSocketKey,
225 protocols: Vec<&'a str>,
226 path: &'a str,
227 headers: RequestHeaders<'a>,
228 }
229
230 /// Select HTTP headers sent by the client.
231 #[derive(Debug, Copy, Clone)]
232 pub struct RequestHeaders<'a> {
233 /// The [`Host`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host) header.
234 pub host: &'a [u8],
235 /// The [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin) header, if provided.
236 pub origin: Option<&'a [u8]>,
237 }
238
239 impl<'a> ClientRequest<'a> {
240 /// The `Sec-WebSocket-Key` header nonce value.
241 pub fn key(&self) -> WebSocketKey {
242 self.ws_key
243 }
244
245 /// The protocols the client is proposing.
246 pub fn protocols(&self) -> impl Iterator<Item = &str> {
247 self.protocols.iter().cloned()
248 }
249
250 /// The path the client is requesting.
251 pub fn path(&self) -> &str {
252 self.path
253 }
254
255 /// Select HTTP headers sent by the client.
256 pub fn headers(&self) -> RequestHeaders {
257 self.headers
258 }
259 }
260
261 /// Handshake response the server sends back to the client.
262 #[derive(Debug)]
263 pub enum Response<'a> {
264 /// The server accepts the handshake request.
265 Accept { key: WebSocketKey, protocol: Option<&'a str> },
266 /// The server rejects the handshake request.
267 Reject { status_code: u16 },
268 }
269
270 /// Known status codes and their reason phrases.
271 const 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 ];