Skip to content

Commit 8bdc86f

Browse files
authored
Merge pull request #11 from stevefan1999-personal/patch-upgrade
update the dependencies to latest version
2 parents b0fd461 + aa03019 commit 8bdc86f

File tree

4 files changed

+24
-131
lines changed

4 files changed

+24
-131
lines changed

Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ categories = ["embedded", "no-std", "network-programming"]
1111
readme = "README.md"
1212

1313
[dependencies]
14-
sha1 = "0.6"
15-
heapless = "0.5"
16-
byteorder = { version = "1.4", default-features = false }
17-
httparse = { version = "1.4", default-features = false }
18-
rand_core = "0.6"
14+
sha1 = { version = "0.10.1", default-features = false }
15+
heapless = "0.7.14"
16+
byteorder = { version = "1.4.3", default-features = false }
17+
httparse = { version = "1.7.1", default-features = false }
18+
rand_core = "0.6.3"
19+
base64 = { version = "0.13.0", default-features = false }
1920

2021
[dev-dependencies]
21-
rand = "0.8.3"
22+
rand = "0.8.5"
2223

2324
# see readme for no_std support
2425
[features]
2526
default = ["std"]
2627
# default = []
27-
std = []
28+
std = []

src/base64.rs

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/http.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use heapless::{String, Vec};
55
/// Websocket details extracted from the http header
66
pub struct WebSocketContext {
77
/// The list of sub protocols is restricted to a maximum of 3
8-
pub sec_websocket_protocol_list: Vec<WebSocketSubProtocol, U3>,
8+
pub sec_websocket_protocol_list: Vec<WebSocketSubProtocol, 3>,
99
/// The websocket key user to build the accept string to complete the opening handshake
1010
pub sec_websocket_key: WebSocketKey,
1111
}
@@ -40,7 +40,7 @@ pub struct WebSocketContext {
4040
pub fn read_http_header<'a>(
4141
headers: impl Iterator<Item = (&'a str, &'a [u8])>,
4242
) -> Result<Option<WebSocketContext>> {
43-
let mut sec_websocket_protocol_list: Vec<String<U24>, U3> = Vec::new();
43+
let mut sec_websocket_protocol_list: Vec<String<24>, 3> = Vec::new();
4444
let mut is_websocket_request = false;
4545
let mut sec_websocket_key = String::new();
4646

@@ -130,13 +130,13 @@ pub fn build_connect_handshake_request(
130130
rng: &mut impl RngCore,
131131
to: &mut [u8],
132132
) -> Result<(usize, WebSocketKey)> {
133-
let mut http_request: String<U1024> = String::new();
133+
let mut http_request: String<1024> = String::new();
134134
let mut key_as_base64: [u8; 24] = [0; 24];
135135

136136
let mut key: [u8; 16] = [0; 16];
137137
rng.fill_bytes(&mut key);
138-
base64::encode(&key, &mut key_as_base64);
139-
let sec_websocket_key: String<U24> = String::from(str::from_utf8(&key_as_base64)?);
138+
base64::encode_config_slice(&key, base64::STANDARD, &mut key_as_base64);
139+
let sec_websocket_key: String<24> = String::from(str::from_utf8(&key_as_base64)?);
140140

141141
http_request.push_str("GET ")?;
142142
http_request.push_str(websocket_options.path)?;
@@ -177,7 +177,7 @@ pub fn build_connect_handshake_response(
177177
sec_websocket_protocol: Option<&WebSocketSubProtocol>,
178178
to: &mut [u8],
179179
) -> Result<usize> {
180-
let mut http_response: String<U1024> = String::new();
180+
let mut http_response: String<1024> = String::new();
181181
http_response.push_str(
182182
"HTTP/1.1 101 Switching Protocols\r\n\
183183
Connection: Upgrade\r\nUpgrade: websocket\r\n",
@@ -204,13 +204,14 @@ pub fn build_connect_handshake_response(
204204

205205
pub fn build_accept_string(sec_websocket_key: &WebSocketKey, output: &mut [u8]) -> Result<()> {
206206
// concatenate the key with a known websocket GUID (as per the spec)
207-
let mut accept_string: String<U64> = String::new();
207+
let mut accept_string: String<64> = String::new();
208208
accept_string.push_str(sec_websocket_key)?;
209209
accept_string.push_str("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")?;
210210

211211
// calculate the base64 encoded sha1 hash of the accept string above
212-
let sha1 = Sha1::from(&accept_string);
213-
let input = sha1.digest().bytes();
214-
base64::encode(&input, output); // no need for slices since the output WILL be 28 bytes
212+
let mut sha1 = Sha1::new();
213+
sha1.update(&accept_string);
214+
let input = sha1.finalize();
215+
base64::encode_config_slice(&input, base64::STANDARD, output); // no need for slices since the output WILL be 28 bytes
215216
Ok(())
216217
}

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515

1616
use byteorder::{BigEndian, ByteOrder};
1717
use core::{cmp, result, str};
18-
use heapless::consts::{U1024, U24, U256, U3, U64};
1918
use heapless::{String, Vec};
2019
use rand_core::RngCore;
21-
use sha1::Sha1;
20+
use sha1::{Digest, Sha1};
2221

23-
mod base64;
2422
mod http;
2523
pub mod random;
2624
pub use self::http::{read_http_header, WebSocketContext};
@@ -36,10 +34,10 @@ const MASK_KEY_LEN: usize = 4;
3634
pub type Result<T> = result::Result<T, Error>;
3735

3836
/// A fixed length 24-character string used to hold a websocket key for the opening handshake
39-
pub type WebSocketKey = String<U24>;
37+
pub type WebSocketKey = String<24>;
4038

4139
/// A maximum sized 24-character string used to store a sub protocol (e.g. `chat`)
42-
pub type WebSocketSubProtocol = String<U24>;
40+
pub type WebSocketSubProtocol = String<24>;
4341

4442
/// Websocket send message type used when sending a websocket frame
4543
#[derive(PartialEq, Debug, Copy, Clone)]
@@ -680,7 +678,7 @@ where
680678
if self.state == WebSocketState::Open {
681679
self.state = WebSocketState::CloseSent;
682680
if let Some(status_description) = status_description {
683-
let mut from_buffer: Vec<u8, U256> = Vec::new();
681+
let mut from_buffer: Vec<u8, 256> = Vec::new();
684682
BigEndian::write_u16(&mut from_buffer, close_status.to_u16());
685683

686684
// restrict the max size of the status_description
@@ -690,7 +688,7 @@ where
690688
254
691689
};
692690

693-
from_buffer.extend(status_description[..len].as_bytes());
691+
from_buffer.extend_from_slice(status_description[..len].as_bytes())?;
694692
self.write_frame(&from_buffer, to, WebSocketOpCode::ConnectionClose, true)
695693
} else {
696694
let mut from_buffer: [u8; 2] = [0; 2];

0 commit comments

Comments
 (0)