Skip to content

Commit

Permalink
Move basic http types to separeate crate
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jun 13, 2022
1 parent 49520c0 commit d7261e2
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 49 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"ntex-bytes",
"ntex-codec",
"ntex-io",
"ntex-http",
"ntex-router",
"ntex-rt",
"ntex-service",
Expand All @@ -20,6 +21,7 @@ ntex = { path = "ntex" }
ntex-bytes = { path = "ntex-bytes" }
ntex-codec = { path = "ntex-codec" }
ntex-io = { path = "ntex-io" }
ntex-http = { path = "ntex-http" }
ntex-router = { path = "ntex-router" }
ntex-rt = { path = "ntex-rt" }
ntex-service = { path = "ntex-service" }
Expand Down
6 changes: 6 additions & 0 deletions ntex-bytes/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ impl ByteString {
ByteString(Bytes::new())
}

/// Get a str slice.
#[inline]
pub fn as_str(&self) -> &str {
&*self
}

/// Get a reference to the underlying bytes.
#[inline]
pub fn as_slice(&self) -> &[u8] {
Expand Down
5 changes: 5 additions & 0 deletions ntex-http/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changes

## [0.1.0] - 2022-xx-xx

* Move http types to separate crate
21 changes: 21 additions & 0 deletions ntex-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "ntex-http"
version = "0.1.0"
authors = ["ntex contributors <[email protected]>"]
description = "Http types for ntex framework"
keywords = ["network", "framework", "async", "futures"]
homepage = "https://ntex.rs"
repository = "https://github.com/ntex-rs/ntex.git"
documentation = "https://docs.rs/ntex-http/"
categories = ["network-programming", "asynchronous"]
license = "MIT"
edition = "2018"

[lib]
name = "ntex_http"
path = "src/lib.rs"

[dependencies]
http = "0.2"
log = "0.4"
fxhash = "0.2.1"
1 change: 1 addition & 0 deletions ntex-http/LICENSE
60 changes: 60 additions & 0 deletions ntex-http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Http protocol support.
// pub mod body;
mod map;

pub use self::map::HeaderMap;
#[doc(hidden)]
pub use self::map::Value;

// re-exports
pub use http::header::{HeaderName, HeaderValue};
pub use http::uri::{self, Uri};
pub use http::{Method, StatusCode, Version};

pub mod error {
pub use http::header::{InvalidHeaderName, InvalidHeaderValue};
pub use http::method::InvalidMethod;
pub use http::status::InvalidStatusCode;
}

/// Convert http::HeaderMap to a HeaderMap
impl From<http::HeaderMap> for HeaderMap {
fn from(map: http::HeaderMap) -> HeaderMap {
let mut new_map = HeaderMap::with_capacity(map.capacity());
for (h, v) in map.iter() {
new_map.append(h.clone(), v.clone());
}
new_map
}
}

pub mod header {
//! Various http headers

#[doc(hidden)]
pub use crate::map::{AsName, Either, GetAll, Iter, Value};

pub use http::header::{
HeaderName, HeaderValue, InvalidHeaderName, InvalidHeaderValue,
};
pub use http::header::{
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE,
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC,
AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING,
CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_RANGE,
CONTENT_SECURITY_POLICY, CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE,
DATE, DNT, ETAG, EXPECT, EXPIRES, FORWARDED, FROM, HOST, IF_MATCH,
IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED,
LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA, PROXY_AUTHENTICATE,
PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY, RANGE, REFERER,
REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT,
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE, STRICT_TRANSPORT_SECURITY, TE, TRAILER,
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA,
WARNING, WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL,
X_FRAME_OPTIONS, X_XSS_PROTECTION,
};
}
21 changes: 18 additions & 3 deletions ntex/src/http/header/map.rs → ntex-http/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use std::collections::hash_map::{self, Entry};
use std::collections::{self, hash_map, hash_map::Entry};
use std::convert::TryFrom;

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

use crate::util::{Either, HashMap};
type HashMap<K, V> = collections::HashMap<K, V, fxhash::FxBuildHasher>;

/// Combines two different futures, streams, or sinks having the same associated types into a single
/// type.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Either<A, B> {
/// First branch of the type
Left(A),
/// Second branch of the type
Right(B),
}

/// A set of HTTP headers
///
Expand All @@ -16,7 +26,7 @@ pub struct HeaderMap {
}

#[derive(Debug, Clone)]
pub(crate) enum Value {
pub enum Value {
One(HeaderValue),
Multi(Vec<HeaderValue>),
}
Expand Down Expand Up @@ -197,6 +207,11 @@ impl HeaderMap {
Iter::new(self.inner.iter())
}

#[doc(hidden)]
pub fn iter_inner(&self) -> hash_map::Iter<'_, HeaderName, Value> {
self.inner.iter()
}

/// An iterator visiting all keys.
///
/// The iteration order is arbitrary, but consistent across platforms for
Expand Down
9 changes: 9 additions & 0 deletions ntex-io/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ where
DispatchItem::Item(el)
}
Err(RecvError::KeepAlive) => {
log::trace!("keep-alive error, stopping dispatcher");
slf.st.set(DispatcherState::Stop);
DispatchItem::KeepAliveTimeout
}
Expand All @@ -229,10 +230,18 @@ where
DispatchItem::WBackPressureEnabled
}
Err(RecvError::Decoder(err)) => {
log::trace!(
"decoder error, stopping dispatcher: {:?}",
err
);
slf.st.set(DispatcherState::Stop);
DispatchItem::DecoderError(err)
}
Err(RecvError::PeerGone(err)) => {
log::trace!(
"peer is gone, stopping dispatcher: {:?}",
err
);
slf.st.set(DispatcherState::Stop);
DispatchItem::Disconnect(err)
}
Expand Down
4 changes: 2 additions & 2 deletions ntex-util/src/future/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{error, fmt, future::Future, pin::Pin, task::Context, task::Poll};
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Either<A, B> {
/// First branch of the type
Left(/* #[pin] */ A),
Left(A),
/// Second branch of the type
Right(/* #[pin] */ B),
Right(B),
}

impl<A, B> Either<A, B> {
Expand Down
4 changes: 4 additions & 0 deletions ntex/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.5.19] - 2022-xx-xx

* http: move basic types to separeate crate

## [0.5.18] - 2022-06-03

* http: Refactor client pool management
Expand Down
1 change: 1 addition & 0 deletions ntex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async-std = ["ntex-rt/async-std", "ntex-async-std"]

[dependencies]
ntex-codec = "0.6.2"
ntex-http = "0.1.0"
ntex-router = "0.5.1"
ntex-service = "0.3.1"
ntex-macros = "0.1.3"
Expand Down
11 changes: 5 additions & 6 deletions ntex/src/http/h1/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cell::Cell, cmp, io, io::Write, mem, ptr, ptr::copy_nonoverlapping, sl

use crate::http::body::BodySize;
use crate::http::config::DateService;
use crate::http::header::{map, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
use crate::http::header::{Value, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
use crate::http::helpers;
use crate::http::message::{ConnectionType, RequestHeadType};
use crate::http::response::Response;
Expand Down Expand Up @@ -106,10 +106,9 @@ pub(super) trait MessageType: Sized {
let extra_headers = self.extra_headers().unwrap_or(&empty_headers);
let headers = self
.headers()
.inner
.iter()
.iter_inner()
.filter(|(name, _)| !extra_headers.contains_key(*name))
.chain(extra_headers.inner.iter());
.chain(extra_headers.iter_inner());

// write headers
let mut pos = 0;
Expand All @@ -127,7 +126,7 @@ pub(super) trait MessageType: Sized {
}
let k = key.as_str().as_bytes();
match value {
map::Value::One(ref val) => {
Value::One(ref val) => {
let v = val.as_ref();
let v_len = v.len();
let k_len = k.len();
Expand All @@ -153,7 +152,7 @@ pub(super) trait MessageType: Sized {
pos += len;
remaining -= len;
}
map::Value::Multi(ref vec) => {
Value::Multi(ref vec) => {
for val in vec {
let v = val.as_ref();
let v_len = v.len();
Expand Down
38 changes: 3 additions & 35 deletions ntex/src/http/header/mod.rs → ntex/src/http/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

pub use http::header::{HeaderName, HeaderValue, InvalidHeaderValue};

pub(crate) mod map;

pub use self::map::HeaderMap;
pub use http::header::*;
#[doc(hidden)]
pub use self::map::{AsName, GetAll};
pub use ntex_http::header::{AsName, GetAll, Value};
pub use ntex_http::HeaderMap;

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

/// Convert http::HeaderMap to a HeaderMap
impl From<http::HeaderMap> for HeaderMap {
fn from(map: http::HeaderMap) -> HeaderMap {
let mut new_map = HeaderMap::with_capacity(map.capacity());
for (h, v) in map.iter() {
new_map.append(h.clone(), v.clone());
}
new_map
}
}

pub use http::header::{
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES,
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS,
ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN,
ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_HEADERS,
ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, ALT_SVC, AUTHORIZATION, CACHE_CONTROL,
CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH,
CONTENT_LOCATION, CONTENT_RANGE, CONTENT_SECURITY_POLICY,
CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TYPE, COOKIE, DATE, DNT, ETAG, EXPECT,
EXPIRES, FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE,
IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA,
PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, PUBLIC_KEY_PINS, PUBLIC_KEY_PINS_REPORT_ONLY,
RANGE, REFERER, REFERRER_POLICY, REFRESH, RETRY_AFTER, SEC_WEBSOCKET_ACCEPT,
SEC_WEBSOCKET_EXTENSIONS, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_PROTOCOL,
SEC_WEBSOCKET_VERSION, SERVER, SET_COOKIE, STRICT_TRANSPORT_SECURITY, TE, TRAILER,
TRANSFER_ENCODING, UPGRADE, UPGRADE_INSECURE_REQUESTS, USER_AGENT, VARY, VIA, WARNING,
WWW_AUTHENTICATE, X_CONTENT_TYPE_OPTIONS, X_DNS_PREFETCH_CONTROL, X_FRAME_OPTIONS,
X_XSS_PROTECTION,
};

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 2 additions & 3 deletions ntex/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub use self::builder::HttpServiceBuilder;
pub use self::client::Client;
pub use self::config::{DateService, KeepAlive, ServiceConfig};
pub use self::error::ResponseError;
pub use self::header::HeaderMap;
pub use self::httpmessage::HttpMessage;
pub use self::message::{ConnectionType, RequestHead, RequestHeadType, ResponseHead};
pub use self::payload::{Payload, PayloadStream};
Expand All @@ -36,5 +35,5 @@ pub use self::service::HttpService;
pub use crate::io::types::HttpProtocol;

// re-exports
pub use http::uri::{self, Uri};
pub use http::{Method, StatusCode, Version};
pub use ntex_http::uri::{self, Uri};
pub use ntex_http::{HeaderMap, Method, StatusCode, Version};
1 change: 1 addition & 0 deletions ntex/src/ws/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ mod test {
opcode_from!(OpCode::Pong => 10);
}

#[cfg(not(target_os = "macos"))]
#[test]
#[should_panic]
fn test_from_opcode_debug() {
Expand Down
1 change: 1 addition & 0 deletions ntex/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ fn test_on_worker_start() {

#[test]
#[cfg(feature = "tokio")]
#[cfg(not(target_os = "macos"))]
#[allow(unreachable_code)]
fn test_panic_in_worker() {
let counter = Arc::new(AtomicUsize::new(0));
Expand Down

0 comments on commit d7261e2

Please sign in to comment.