1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-30 03:50:42 +00:00
This commit is contained in:
Nikolay Kim 2018-08-21 21:11:16 -07:00
parent 0f8cd0f44d
commit 29490e6c13
8 changed files with 18 additions and 216 deletions

View file

@ -37,4 +37,4 @@ build: false
# Equivalent to Travis' `script` phase
test_script:
- cargo test --no-default-features --features="flate2-rust"
- cargo test --no-default-features

View file

@ -32,12 +32,12 @@ script:
- |
if [[ "$TRAVIS_RUST_VERSION" != "stable" ]]; then
cargo clean
cargo test --features="alpn,tls,rust-tls" -- --nocapture
cargo test --features="ssl,tls,rust-tls" -- --nocapture
fi
- |
if [[ "$TRAVIS_RUST_VERSION" == "stable" ]]; then
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install -f cargo-tarpaulin
cargo tarpaulin --features="alpn,tls,rust-tls" --out Xml --no-count
cargo tarpaulin --features="ssl,tls,rust-tls" --out Xml --no-count
bash <(curl -s https://codecov.io/bash)
echo "Uploaded code coverage"
fi
@ -46,7 +46,7 @@ script:
after_success:
- |
if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" = "false" && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_RUST_VERSION" == "beta" ]]; then
cargo doc --features "alpn, tls, rust-tls, session" --no-deps &&
cargo doc --features "ssl, tls, rust-tls, session" --no-deps &&
echo "<meta http-equiv=refresh content=0;url=os_balloon/index.html>" > target/doc/index.html &&
git clone https://github.com/davisp/ghp-import.git &&
./ghp-import/ghp_import.py -n -p -f -m "Documentation upload" -r https://"$GH_TOKEN"@github.com/"$TRAVIS_REPO_SLUG.git" target/doc &&

View file

@ -2,16 +2,13 @@
name = "actix-net"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix net utils"
description = "Actix net - framework for the compisible network services for Rust"
readme = "README.md"
keywords = ["http", "framework", "async", "futures"]
keywords = ["network", "framework", "async", "futures"]
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
documentation = "https://actix.rs/api/actix-net/stable/actix_web/"
categories = ["network-programming", "asynchronous",
"web-programming::http-server",
"web-programming::http-client",
"web-programming::websocket"]
categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
@ -44,19 +41,14 @@ actix = "0.7.0"
log = "0.4"
num_cpus = "1.0"
rand = "0.5"
time = "0.1"
parking_lot = "0.6"
failure = "^0.1.2"
# io
mio = "^0.6.13"
net2 = "0.2"
bytes = "0.4"
byteorder = "1.2"
futures = "0.1"
slab = "0.4"
tokio = "0.1"
tokio-io = "0.1"
tokio-tcp = "0.1"
tokio-timer = "0.2"

View file

@ -83,6 +83,9 @@ fn main() {
Ok::<_, io::Error>(ServiceState { num: num.clone() })
}));
// bind socket address and start workers. By default server uses number of
// available logical cpu as threads count. actix net start separate
// instances of service pipeline in each worker.
Server::default().bind("0.0.0.0:8443", srv).unwrap().start();
sys.run();

View file

@ -1,115 +0,0 @@
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt;
use std::hash::{BuildHasherDefault, Hasher};
struct IdHasher {
id: u64,
}
impl Default for IdHasher {
fn default() -> IdHasher {
IdHasher { id: 0 }
}
}
impl Hasher for IdHasher {
fn write(&mut self, bytes: &[u8]) {
for &x in bytes {
self.id.wrapping_add(u64::from(x));
}
}
fn write_u64(&mut self, u: u64) {
self.id = u;
}
fn finish(&self) -> u64 {
self.id
}
}
type AnyMap = HashMap<TypeId, Box<Any>, BuildHasherDefault<IdHasher>>;
/// A type map of request extensions.
#[derive(Default)]
pub struct Extensions {
map: AnyMap,
}
impl Extensions {
/// Create an empty `Extensions`.
#[inline]
pub fn new() -> Extensions {
Extensions {
map: HashMap::default(),
}
}
/// Insert a type into this `Extensions`.
///
/// If a extension of this type already existed, it will
/// be returned.
pub fn insert<T: 'static>(&mut self, val: T) {
self.map.insert(TypeId::of::<T>(), Box::new(val));
}
/// Get a reference to a type previously inserted on this `Extensions`.
pub fn get<T: 'static>(&self) -> Option<&T> {
self.map
.get(&TypeId::of::<T>())
.and_then(|boxed| (&**boxed as &(Any + 'static)).downcast_ref())
}
/// Get a mutable reference to a type previously inserted on this
/// `Extensions`.
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.map
.get_mut(&TypeId::of::<T>())
.and_then(|boxed| (&mut **boxed as &mut (Any + 'static)).downcast_mut())
}
/// Remove a type from this `Extensions`.
///
/// If a extension of this type existed, it will be returned.
pub fn remove<T: 'static>(&mut self) -> Option<T> {
self.map.remove(&TypeId::of::<T>()).and_then(|boxed| {
(boxed as Box<Any + 'static>)
.downcast()
.ok()
.map(|boxed| *boxed)
})
}
/// Clear the `Extensions` of all inserted extensions.
#[inline]
pub fn clear(&mut self) {
self.map.clear();
}
}
impl fmt::Debug for Extensions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Extensions").finish()
}
}
#[test]
fn test_extensions() {
#[derive(Debug, PartialEq)]
struct MyType(i32);
let mut extensions = Extensions::new();
extensions.insert(5i32);
extensions.insert(MyType(10));
assert_eq!(extensions.get(), Some(&5i32));
assert_eq!(extensions.get_mut(), Some(&mut 5i32));
assert_eq!(extensions.remove::<i32>(), Some(5i32));
assert!(extensions.get::<i32>().is_none());
assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));
}

View file

@ -1,35 +1,22 @@
//! Actix web is a small, pragmatic, and extremely fast web framework
//! for Rust.
//! Actix net - framework for the compisible network services for Rust.
//!
//! ## Package feature
//!
//! * `tls` - enables ssl support via `native-tls` crate
//! * `alpn` - enables ssl support via `openssl` crate, require for `http/2`
//! support
//! * `ssl` - enables ssl support via `openssl` crate
//! * `rust-tls` - enables ssl support via `rustls` crate
//!
// #![warn(missing_docs)]
// #![allow(
// dead_code,
// unused_variables,
// unused_imports,
// patterns_in_fns_without_body
// )]
#[macro_use]
extern crate log;
extern crate byteorder;
extern crate bytes;
extern crate failure;
extern crate futures;
extern crate mio;
extern crate net2;
extern crate num_cpus;
extern crate parking_lot;
extern crate slab;
extern crate time;
extern crate tokio;
extern crate tokio_io;
extern crate tokio_reactor;
extern crate tokio_tcp;
@ -56,19 +43,12 @@ extern crate webpki;
#[cfg(feature = "rust-tls")]
extern crate webpki_roots;
use std::io;
use std::net::Shutdown;
use std::rc::Rc;
use actix::Message;
use bytes::{BufMut, BytesMut};
use futures::{Async, Poll};
use tokio_io::{AsyncRead, AsyncWrite};
/// re-export for convinience. as a note, actix-net does not use `tower_service::NewService` trait.
pub use tower_service::Service;
pub(crate) mod accept;
mod extensions;
mod server;
pub mod server_config;
mod server_service;
@ -76,12 +56,10 @@ pub mod service;
pub mod ssl;
mod worker;
pub use self::server::{ConnectionRateTag, ConnectionTag, Connections, Server};
pub use server::Server;
pub use server_config::Config;
pub use service::{IntoNewService, IntoService, NewService};
pub use extensions::Extensions;
/// Pause accepting incoming connections
///
/// If socket contains some pending connection, they might be dropped.
@ -107,60 +85,4 @@ impl Message for StopServer {
/// Socket id token
#[derive(Clone, Copy)]
pub struct Token(usize);
// impl Token {
// pub(crate) fn new(val: usize) -> Token {
// Token(val)
// }
// }
const LW_BUFFER_SIZE: usize = 4096;
const HW_BUFFER_SIZE: usize = 32_768;
#[doc(hidden)]
/// Low-level io stream operations
pub trait IoStream: AsyncRead + AsyncWrite + 'static {
fn shutdown(&mut self, how: Shutdown) -> io::Result<()>;
fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()>;
fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()>;
fn read_available(&mut self, buf: &mut BytesMut) -> Poll<bool, io::Error> {
let mut read_some = false;
loop {
if buf.remaining_mut() < LW_BUFFER_SIZE {
buf.reserve(HW_BUFFER_SIZE);
}
unsafe {
match self.read(buf.bytes_mut()) {
Ok(n) => {
if n == 0 {
return Ok(Async::Ready(!read_some));
} else {
read_some = true;
buf.advance_mut(n);
}
}
Err(e) => {
return if e.kind() == io::ErrorKind::WouldBlock {
if read_some {
Ok(Async::Ready(false))
} else {
Ok(Async::NotReady)
}
} else {
Err(e)
};
}
}
}
}
}
/// Extra io stream extensions
fn extensions(&self) -> Option<Rc<Extensions>> {
None
}
}
pub(crate) struct Token(usize);

View file

@ -70,7 +70,7 @@ impl<C: Config> Server<C> {
/// Set number of workers to start.
///
/// By default http server uses number of available logical cpu as threads
/// By default server uses number of available logical cpu as threads
/// count.
pub fn workers(mut self, num: usize) -> Self {
self.threads = num;

View file

@ -207,7 +207,7 @@ where
type InitError = IErr;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, cfg: Cfg) -> Self::Future {
fn new_service(&self, _: Cfg) -> Self::Future {
future::ok(FnService::new(self.f.clone()))
}
}
@ -336,7 +336,7 @@ where
type InitError = Err2;
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
fn new_service(&self, cfg: Cfg) -> Self::Future {
fn new_service(&self, _: Cfg) -> Self::Future {
let f = self.f.clone();
Box::new(
(self.state)()