Skip to content

Commit d7261e2

Browse files
committed
Move basic http types to separeate crate
1 parent 49520c0 commit d7261e2

File tree

16 files changed

+141
-49
lines changed

16 files changed

+141
-49
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"ntex-bytes",
55
"ntex-codec",
66
"ntex-io",
7+
"ntex-http",
78
"ntex-router",
89
"ntex-rt",
910
"ntex-service",
@@ -20,6 +21,7 @@ ntex = { path = "ntex" }
2021
ntex-bytes = { path = "ntex-bytes" }
2122
ntex-codec = { path = "ntex-codec" }
2223
ntex-io = { path = "ntex-io" }
24+
ntex-http = { path = "ntex-http" }
2325
ntex-router = { path = "ntex-router" }
2426
ntex-rt = { path = "ntex-rt" }
2527
ntex-service = { path = "ntex-service" }

ntex-bytes/src/string.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ impl ByteString {
1414
ByteString(Bytes::new())
1515
}
1616

17+
/// Get a str slice.
18+
#[inline]
19+
pub fn as_str(&self) -> &str {
20+
&*self
21+
}
22+
1723
/// Get a reference to the underlying bytes.
1824
#[inline]
1925
pub fn as_slice(&self) -> &[u8] {

ntex-http/CHANGES.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changes
2+
3+
## [0.1.0] - 2022-xx-xx
4+
5+
* Move http types to separate crate

ntex-http/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "ntex-http"
3+
version = "0.1.0"
4+
authors = ["ntex contributors <team@ntex.rs>"]
5+
description = "Http types for ntex framework"
6+
keywords = ["network", "framework", "async", "futures"]
7+
homepage = "https://ntex.rs"
8+
repository = "https://github.com/ntex-rs/ntex.git"
9+
documentation = "https://docs.rs/ntex-http/"
10+
categories = ["network-programming", "asynchronous"]
11+
license = "MIT"
12+
edition = "2018"
13+
14+
[lib]
15+
name = "ntex_http"
16+
path = "src/lib.rs"
17+
18+
[dependencies]
19+
http = "0.2"
20+
log = "0.4"
21+
fxhash = "0.2.1"

ntex-http/LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE

ntex-http/src/lib.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Http protocol support.
2+
// pub mod body;
3+
mod map;
4+
5+
pub use self::map::HeaderMap;
6+
#[doc(hidden)]
7+
pub use self::map::Value;
8+
9+
// re-exports
10+
pub use http::header::{HeaderName, HeaderValue};
11+
pub use http::uri::{self, Uri};
12+
pub use http::{Method, StatusCode, Version};
13+
14+
pub mod error {
15+
pub use http::header::{InvalidHeaderName, InvalidHeaderValue};
16+
pub use http::method::InvalidMethod;
17+
pub use http::status::InvalidStatusCode;
18+
}
19+
20+
/// Convert http::HeaderMap to a HeaderMap
21+
impl From<http::HeaderMap> for HeaderMap {
22+
fn from(map: http::HeaderMap) -> HeaderMap {
23+
let mut new_map = HeaderMap::with_capacity(map.capacity());
24+
for (h, v) in map.iter() {
25+
new_map.append(h.clone(), v.clone());
26+
}
27+
new_map
28+
}
29+
}
30+
31+
pub mod header {
32+
//! Various http headers
33+
34+
#[doc(hidden)]
35+
pub use crate::map::{AsName, Either, GetAll, Iter, Value};
36+
37+
pub use http::header::{
38+
HeaderName, HeaderValue, InvalidHeaderName, InvalidHeaderValue,
39+
};
40+
pub use http::header::{
41+
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
42+
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
43+
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
44+
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
45+
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC,
46+
AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING,
47+
CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_RANGE,
48+
CONTENT_SECURITY_POLICY, CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE,
49+
DATE, DNT, ETAG, EXPECT, EXPIRES, FORWARDED, FROM, HOST, IF_MATCH,
50+
IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED,
51+
LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA, PROXY_AUTHENTICATE,
52+
PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY, RANGE, REFERER,
53+
REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT,
54+
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
55+
SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE, STRICT_TRANSPORT_SECURITY, TE, TRAILER,
56+
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA,
57+
WARNING, WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL,
58+
X_FRAME_OPTIONS, X_XSS_PROTECTION,
59+
};
60+
}

ntex/src/http/header/map.rs ntex-http/src/map.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
use std::collections::hash_map::{self, Entry};
1+
use std::collections::{self, hash_map, hash_map::Entry};
22
use std::convert::TryFrom;
33

44
use http::header::{HeaderName, HeaderValue};
55

6-
use crate::util::{Either, HashMap};
6+
type HashMap<K, V> = collections::HashMap<K, V, fxhash::FxBuildHasher>;
7+
8+
/// Combines two different futures, streams, or sinks having the same associated types into a single
9+
/// type.
10+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
11+
pub enum Either<A, B> {
12+
/// First branch of the type
13+
Left(A),
14+
/// Second branch of the type
15+
Right(B),
16+
}
717

818
/// A set of HTTP headers
919
///
@@ -16,7 +26,7 @@ pub struct HeaderMap {
1626
}
1727

1828
#[derive(Debug, Clone)]
19-
pub(crate) enum Value {
29+
pub enum Value {
2030
One(HeaderValue),
2131
Multi(Vec<HeaderValue>),
2232
}
@@ -197,6 +207,11 @@ impl HeaderMap {
197207
Iter::new(self.inner.iter())
198208
}
199209

210+
#[doc(hidden)]
211+
pub fn iter_inner(&self) -> hash_map::Iter<'_, HeaderName, Value> {
212+
self.inner.iter()
213+
}
214+
200215
/// An iterator visiting all keys.
201216
///
202217
/// The iteration order is arbitrary, but consistent across platforms for

ntex-io/src/dispatcher.rs

+9
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ where
215215
DispatchItem::Item(el)
216216
}
217217
Err(RecvError::KeepAlive) => {
218+
log::trace!("keep-alive error, stopping dispatcher");
218219
slf.st.set(DispatcherState::Stop);
219220
DispatchItem::KeepAliveTimeout
220221
}
@@ -229,10 +230,18 @@ where
229230
DispatchItem::WBackPressureEnabled
230231
}
231232
Err(RecvError::Decoder(err)) => {
233+
log::trace!(
234+
"decoder error, stopping dispatcher: {:?}",
235+
err
236+
);
232237
slf.st.set(DispatcherState::Stop);
233238
DispatchItem::DecoderError(err)
234239
}
235240
Err(RecvError::PeerGone(err)) => {
241+
log::trace!(
242+
"peer is gone, stopping dispatcher: {:?}",
243+
err
244+
);
236245
slf.st.set(DispatcherState::Stop);
237246
DispatchItem::Disconnect(err)
238247
}

ntex-util/src/future/either.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::{error, fmt, future::Future, pin::Pin, task::Context, task::Poll};
55
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
66
pub enum Either<A, B> {
77
/// First branch of the type
8-
Left(/* #[pin] */ A),
8+
Left(A),
99
/// Second branch of the type
10-
Right(/* #[pin] */ B),
10+
Right(B),
1111
}
1212

1313
impl<A, B> Either<A, B> {

ntex/CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [0.5.19] - 2022-xx-xx
4+
5+
* http: move basic types to separeate crate
6+
37
## [0.5.18] - 2022-06-03
48

59
* http: Refactor client pool management

ntex/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ async-std = ["ntex-rt/async-std", "ntex-async-std"]
4949

5050
[dependencies]
5151
ntex-codec = "0.6.2"
52+
ntex-http = "0.1.0"
5253
ntex-router = "0.5.1"
5354
ntex-service = "0.3.1"
5455
ntex-macros = "0.1.3"

ntex/src/http/h1/encoder.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cell::Cell, cmp, io, io::Write, mem, ptr, ptr::copy_nonoverlapping, sl
33

44
use crate::http::body::BodySize;
55
use crate::http::config::DateService;
6-
use crate::http::header::{map, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
6+
use crate::http::header::{Value, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
77
use crate::http::helpers;
88
use crate::http::message::{ConnectionType, RequestHeadType};
99
use crate::http::response::Response;
@@ -106,10 +106,9 @@ pub(super) trait MessageType: Sized {
106106
let extra_headers = self.extra_headers().unwrap_or(&empty_headers);
107107
let headers = self
108108
.headers()
109-
.inner
110-
.iter()
109+
.iter_inner()
111110
.filter(|(name, _)| !extra_headers.contains_key(*name))
112-
.chain(extra_headers.inner.iter());
111+
.chain(extra_headers.iter_inner());
113112

114113
// write headers
115114
let mut pos = 0;
@@ -127,7 +126,7 @@ pub(super) trait MessageType: Sized {
127126
}
128127
let k = key.as_str().as_bytes();
129128
match value {
130-
map::Value::One(ref val) => {
129+
Value::One(ref val) => {
131130
let v = val.as_ref();
132131
let v_len = v.len();
133132
let k_len = k.len();
@@ -153,7 +152,7 @@ pub(super) trait MessageType: Sized {
153152
pos += len;
154153
remaining -= len;
155154
}
156-
map::Value::Multi(ref vec) => {
155+
Value::Multi(ref vec) => {
157156
for val in vec {
158157
let v = val.as_ref();
159158
let v_len = v.len();

ntex/src/http/header/mod.rs ntex/src/http/header.rs

+3-35
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
33
pub use http::header::{HeaderName, HeaderValue, InvalidHeaderValue};
44

5-
pub(crate) mod map;
6-
7-
pub use self::map::HeaderMap;
5+
pub use http::header::*;
86
#[doc(hidden)]
9-
pub use self::map::{AsName, GetAll};
7+
pub use ntex_http::header::{AsName, GetAll, Value};
8+
pub use ntex_http::HeaderMap;
109

1110
/// Represents supported types of content encodings
1211
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -69,37 +68,6 @@ impl<'a> From<&'a str> for ContentEncoding {
6968
}
7069
}
7170

72-
/// Convert http::HeaderMap to a HeaderMap
73-
impl From<http::HeaderMap> for HeaderMap {
74-
fn from(map: http::HeaderMap) -> HeaderMap {
75-
let mut new_map = HeaderMap::with_capacity(map.capacity());
76-
for (h, v) in map.iter() {
77-
new_map.append(h.clone(), v.clone());
78-
}
79-
new_map
80-
}
81-
}
82-
83-
pub use http::header::{
84-
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
85-
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
86-
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
87-
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_HEADERS,
88-
ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC, AUTHORIZATION, CACHE_CONTROL,
89-
CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH,
90-
CONTENT_LOCATION, CONTENT_RANGE, CONTENT_SECURITY_POLICY,
91-
CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE, DATE, DNT, ETAG, EXPECT,
92-
EXPIRES, FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE,
93-
IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA,
94-
PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY,
95-
RANGE, REFERER, REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT,
96-
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
97-
SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE, STRICT_TRANSPORT_SECURITY, TE, TRAILER,
98-
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA, WARNING,
99-
WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL, X_FRAME_OPTIONS,
100-
X_XSS_PROTECTION,
101-
};
102-
10371
#[cfg(test)]
10472
mod tests {
10573
use super::*;

ntex/src/http/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub use self::builder::HttpServiceBuilder;
2626
pub use self::client::Client;
2727
pub use self::config::{DateService, KeepAlive, ServiceConfig};
2828
pub use self::error::ResponseError;
29-
pub use self::header::HeaderMap;
3029
pub use self::httpmessage::HttpMessage;
3130
pub use self::message::{ConnectionType, RequestHead, RequestHeadType, ResponseHead};
3231
pub use self::payload::{Payload, PayloadStream};
@@ -36,5 +35,5 @@ pub use self::service::HttpService;
3635
pub use crate::io::types::HttpProtocol;
3736

3837
// re-exports
39-
pub use http::uri::{self, Uri};
40-
pub use http::{Method, StatusCode, Version};
38+
pub use ntex_http::uri::{self, Uri};
39+
pub use ntex_http::{HeaderMap, Method, StatusCode, Version};

ntex/src/ws/proto.rs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ mod test {
259259
opcode_from!(OpCode::Pong => 10);
260260
}
261261

262+
#[cfg(not(target_os = "macos"))]
262263
#[test]
263264
#[should_panic]
264265
fn test_from_opcode_debug() {

ntex/tests/server.rs

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ fn test_on_worker_start() {
187187

188188
#[test]
189189
#[cfg(feature = "tokio")]
190+
#[cfg(not(target_os = "macos"))]
190191
#[allow(unreachable_code)]
191192
fn test_panic_in_worker() {
192193
let counter = Arc::new(AtomicUsize::new(0));

0 commit comments

Comments
 (0)