Skip to content

Commit

Permalink
Unix domain sockets (HttpServer::bind_uds) actix#92
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jul 18, 2019
1 parent d032962 commit fbdda8a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Changes

## [1.0.5] - ?
## [1.0.5] - 2019-07-xx

### Added

* Unix domain sockets (HttpServer::bind_uds) #92

* Actix now logs errors resulting in "internal server error" responses always, with the `error`
logging level

### Fixed

* Restored logging of errors through the `Logger` middleware


## [1.0.4] - 2019-07-17

### Added
Expand Down
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018"

[package.metadata.docs.rs]
features = ["ssl", "brotli", "flate2-zlib", "secure-cookies", "client", "rust-tls"]
features = ["ssl", "brotli", "flate2-zlib", "secure-cookies", "client", "rust-tls", "uds"]

[badges]
travis-ci = { repository = "actix/actix-web", branch = "master" }
Expand Down Expand Up @@ -68,6 +68,9 @@ ssl = ["openssl", "actix-server/ssl", "awc/ssl"]
# rustls
rust-tls = ["rustls", "actix-server/rust-tls"]

# unix domain sockets support
uds = ["actix-server/uds"]

[dependencies]
actix-codec = "0.1.2"
actix-service = "0.4.1"
Expand All @@ -76,8 +79,8 @@ actix-router = "0.1.5"
actix-rt = "0.2.4"
actix-web-codegen = "0.1.2"
actix-http = "0.2.6"
actix-server = "0.5.1"
actix-server-config = "0.1.1"
actix-server = "0.6.0"
actix-server-config = "0.1.2"
actix-threadpool = "0.1.1"
awc = { version = "0.2.1", optional = true }

Expand Down
11 changes: 8 additions & 3 deletions actix-multipart/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ impl InnerMultipart {
}
// read boundary
InnerState::Boundary => {
match InnerMultipart::read_boundary(&mut *payload, &self.boundary)? {
match InnerMultipart::read_boundary(
&mut *payload,
&self.boundary,
)? {
None => return Ok(Async::NotReady),
Some(eof) => {
if eof {
Expand Down Expand Up @@ -411,7 +414,8 @@ impl Stream for Field {
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
if self.safety.current() {
let mut inner = self.inner.borrow_mut();
if let Some(mut payload) = inner.payload.as_ref().unwrap().get_mut(&self.safety)
if let Some(mut payload) =
inner.payload.as_ref().unwrap().get_mut(&self.safety)
{
payload.poll_stream()?;
}
Expand Down Expand Up @@ -582,7 +586,8 @@ impl InnerField {
return Ok(Async::Ready(None));
}

let result = if let Some(mut payload) = self.payload.as_ref().unwrap().get_mut(s) {
let result = if let Some(mut payload) = self.payload.as_ref().unwrap().get_mut(s)
{
if !self.eof {
let res = if let Some(ref mut len) = self.length {
InnerField::read_len(&mut *payload, len)?
Expand Down
49 changes: 49 additions & 0 deletions examples/uds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use futures::IntoFuture;

use actix_web::{
get, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};

#[get("/resource1/{name}/index.html")]
fn index(req: HttpRequest, name: web::Path<String>) -> String {
println!("REQ: {:?}", req);
format!("Hello: {}!\r\n", name)
}

fn index_async(req: HttpRequest) -> impl IntoFuture<Item = &'static str, Error = Error> {
println!("REQ: {:?}", req);
Ok("Hello world!\r\n")
}

#[get("/")]
fn no_params() -> &'static str {
"Hello world!\r\n"
}

fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
env_logger::init();

HttpServer::new(|| {
App::new()
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
.wrap(middleware::Compress::default())
.wrap(middleware::Logger::default())
.service(index)
.service(no_params)
.service(
web::resource("/resource2/index.html")
.wrap(
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
)
.default_service(
web::route().to(|| HttpResponse::MethodNotAllowed()),
)
.route(web::get().to_async(index_async)),
)
.service(web::resource("/test1.html").to(|| "Test\r\n"))
})
.bind_uds("/Users/fafhrd91/uds-test")?
.workers(1)
.run()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
//! `c` compiler (default enabled)
//! * `flate2-rust` - experimental rust based implementation for
//! `gzip`, `deflate` compression.
//! * `uds` - Unix domain support, enables `HttpServer::bind_uds()` method.
//!
#![allow(clippy::type_complexity, clippy::new_without_default)]

Expand Down
32 changes: 32 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,38 @@ where
}
Ok(self)
}

#[cfg(feature = "uds")]
/// Start listening for incoming unix domain connections.
///
/// This method is available with `uds` feature.
pub fn bind_uds<A>(mut self, addr: A) -> io::Result<Self>
where
A: AsRef<std::path::Path>,
{
let cfg = self.config.clone();
let factory = self.factory.clone();
self.sockets.push(Socket {
scheme: "http",
addr: net::SocketAddr::new(
net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)),
8080,
),
});

self.builder = self.builder.bind_uds(
format!("actix-web-service-{:?}", addr.as_ref()),
addr,
move || {
let c = cfg.lock();
HttpService::build()
.keep_alive(c.keep_alive)
.client_timeout(c.client_timeout)
.finish(factory())
},
)?;
Ok(self)
}
}

impl<F, I, S, B> HttpServer<F, I, S, B>
Expand Down
4 changes: 4 additions & 0 deletions test-server/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.2.4] - 2019-07-18

* Update actix-server to 0.6

## [0.2.3] - 2019-07-16

* Add `delete`, `options`, `patch` methods to `TestServerRunner`
Expand Down
4 changes: 2 additions & 2 deletions test-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-http-test"
version = "0.2.3"
version = "0.2.4"
authors = ["Nikolay Kim <[email protected]>"]
description = "Actix http test server"
readme = "README.md"
Expand Down Expand Up @@ -33,7 +33,7 @@ ssl = ["openssl", "actix-server/ssl", "awc/ssl"]
actix-codec = "0.1.2"
actix-rt = "0.2.2"
actix-service = "0.4.1"
actix-server = "0.5.1"
actix-server = "0.6.0"
actix-utils = "0.4.1"
awc = "0.2.2"

Expand Down
3 changes: 2 additions & 1 deletion test-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use futures::future::lazy;
use futures::{Future, IntoFuture, Stream};
use http::Method;
use net2::TcpBuilder;
use tokio_tcp::TcpStream;

thread_local! {
static RT: RefCell<Inner> = {
Expand Down Expand Up @@ -109,7 +110,7 @@ pub struct TestServerRuntime {
impl TestServer {
#[allow(clippy::new_ret_no_self)]
/// Start new test server with application factory
pub fn new<F: StreamServiceFactory>(factory: F) -> TestServerRuntime {
pub fn new<F: StreamServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
let (tx, rx) = mpsc::channel();

// run server in separate thread
Expand Down

0 comments on commit fbdda8a

Please sign in to comment.