diff --git a/examples/basic.rs b/examples/basic.rs index 42d29e8ad..9beecbcde 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -87,8 +87,7 @@ fn main() { .resource("/", |r| r.method(Method::GET).f(|req| { println!("{:?}", req); - httpcodes::HTTPFound - .build() + HttpResponse::Found() .header("LOCATION", "/index.html") .finish() }))) diff --git a/src/info.rs b/src/info.rs index 76cf678ea..92ffa4d6c 100644 --- a/src/info.rs +++ b/src/info.rs @@ -8,9 +8,6 @@ const X_FORWARDED_PROTO: &str = "X-FORWARDED-PROTO"; /// `HttpRequest` connection information -/// -/// While it is possible to create `ConnectionInfo` directly, -/// consider using `HttpRequest::load_connection_info()` which cache result. pub struct ConnectionInfo<'a> { scheme: &'a str, host: &'a str, @@ -138,6 +135,7 @@ impl<'a> ConnectionInfo<'a> { /// - X-Forwarded-Host /// - Host /// - Uri + /// - Server hostname pub fn host(&self) -> &str { self.host } diff --git a/src/server.rs b/src/server.rs index d1ce80a6a..e5ee5c50e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -53,11 +53,13 @@ impl Default for ServerSettings { impl ServerSettings { /// Crate server settings instance - fn new(addr: Option, secure: bool) -> Self { - let host = if let Some(ref addr) = addr { + fn new(addr: Option, host: &Option, secure: bool) -> Self { + let host = if let Some(ref host) = *host { + host.clone() + } else if let Some(ref addr) = addr { format!("{}", addr) } else { - "unknown".to_owned() + "localhost".to_owned() }; ServerSettings { addr: addr, @@ -97,6 +99,7 @@ pub struct HttpServer addr: PhantomData, threads: usize, backlog: i32, + host: Option, keep_alive: Option, factory: Arc U + Send + Sync>, workers: Vec>>, @@ -134,6 +137,7 @@ impl HttpServer addr: PhantomData, threads: num_cpus::get(), backlog: 2048, + host: None, keep_alive: None, factory: Arc::new(factory), workers: Vec::new(), @@ -175,6 +179,16 @@ impl HttpServer self } + /// Set server host name. + /// + /// Host name is used by application router aa a hostname for url generation. + /// Check [ConnectionInfo](./dev/struct.ConnectionInfo.html#method.host) documentation + /// for more information. + pub fn server_hostname(mut self, val: String) -> Self { + self.host = Some(val); + self + } + /// The socket address to bind /// /// To mind multiple addresses this method can be call multiple times. @@ -291,7 +305,7 @@ impl HttpServer panic!("HttpServer::bind() has to be called befor start()"); } else { let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect(); - let settings = ServerSettings::new(Some(addrs[0].0), false); + let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false); let workers = self.start_workers(&settings, &StreamHandlerType::Normal); // start acceptors threads @@ -395,7 +409,7 @@ impl HttpServer { if !self.sockets.is_empty() { let addrs: Vec<(net::SocketAddr, Socket)> = self.sockets.drain().collect(); - let settings = ServerSettings::new(Some(addrs[0].0), false); + let settings = ServerSettings::new(Some(addrs[0].0), &self.host, false); let workers = self.start_workers(&settings, &StreamHandlerType::Normal); // start acceptors threads @@ -407,7 +421,7 @@ impl HttpServer // set server settings let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap(); - let settings = ServerSettings::new(Some(addr), secure); + let settings = ServerSettings::new(Some(addr), &self.host, secure); let mut apps: Vec<_> = (*self.factory)().into_iter().map(|h| h.into_handler()).collect(); for app in &mut apps { app.server_settings(settings.clone());