1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
use crate::net::Reactor;
use crate::{executor, Body, Request, Response};
use std::convert::Infallible;
use std::future::Future;
use std::io;
use std::net::{TcpListener, ToSocketAddrs};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use hyper::rt::Executor;
use hyper::server::conn::Http;
/// An HTTP server.
///
/// ```no_run
/// use astra::{Body, Request, Response, Server};
///
/// Server::bind("localhost:3000")
/// .serve(|mut req: Request| {
/// println!("incoming {:?}", req.uri());
/// Response::new(Body::new("Hello World!"))
/// })
/// .expect("failed to start server");
/// ```
///
/// See the [crate-level documentation](crate#how-does-it-work) for details.
#[derive(Default)]
pub struct Server {
listener: Option<TcpListener>,
http1_keep_alive: Option<bool>,
http1_half_close: Option<bool>,
http1_max_buf_size: Option<usize>,
http1_pipeline_flush: Option<bool>,
http1_writev: Option<bool>,
http1_title_case_headers: Option<bool>,
http1_preserve_header_case: Option<bool>,
http1_only: Option<bool>,
http2_only: Option<bool>,
http2_initial_stream_window_size: Option<u32>,
http2_initial_connection_window_size: Option<u32>,
http2_adaptive_window: Option<bool>,
http2_max_frame_size: Option<u32>,
http2_max_concurrent_streams: Option<u32>,
http2_max_send_buf_size: Option<usize>,
worker_keep_alive: Option<Duration>,
max_workers: Option<usize>,
}
/// A service capable of responding to an HTTP request.
///
/// This trait is automatically implemented for functions
/// from a [`Request`] to a [`Response`], but implementing
/// it manually allows for stateful services:
///
/// ```no_run
/// use astra::{Request, Response, Server, Service, Body};
/// use std::sync::Mutex;
///
/// struct MyService {
/// count: Mutex<usize>,
/// }
///
/// impl Service for MyService {
/// fn call(&self, request: Request) -> Response {
/// let mut count = self.count.lock().unwrap();
/// *count += 1;
/// println!("request #{}", *count);
/// Response::new(Body::new("Hello world"))
/// }
/// }
///
///
/// Server::bind("localhost:3000")
/// .serve(MyService { count: Mutex::new(0) })
/// .expect("failed to start server");
/// ```
pub trait Service: Send + Sync + 'static {
fn call(&self, request: Request) -> Response;
}
impl<F> Service for F
where
F: Fn(Request) -> Response + Send + Sync + 'static,
{
fn call(&self, request: Request) -> Response {
(self)(request)
}
}
impl Server {
/// Binds a server to the provided address.
///
/// ```no_run
/// use astra::Server;
/// use std::net::SocketAddr;
///
/// let server = Server::bind("localhost:3000");
/// let server = Server::bind(SocketAddr::from(([127, 0, 0, 1], 3000)));
/// ```
///
/// # Panics
///
/// This method will panic if binding to the address fails.
pub fn bind(addr: impl ToSocketAddrs) -> Server {
let listener = std::net::TcpListener::bind(addr).expect("failed to bind listener");
Server {
listener: Some(listener),
..Default::default()
}
}
/// Serve incoming connections with the provided service.
///
/// ```no_run
/// use astra::{Body, Request, Response, Server};
///
/// Server::bind("localhost:3000")
/// .serve(|mut req: Request| {
/// println!("incoming {:?}", req.uri());
/// Response::new(Body::new("Hello World!"))
/// })
/// .expect("failed to start server");
/// ```
pub fn serve<S>(self, service: S) -> io::Result<()>
where
S: Service,
{
let executor = executor::Executor::new(self.max_workers, self.worker_keep_alive);
let mut http = Http::new().with_executor(executor.clone());
self.configure(&mut http);
let service = Arc::new(service);
let reactor = Reactor::new().expect("failed to create reactor");
for conn in self.listener.unwrap().incoming() {
let conn = conn.and_then(|stream| reactor.register(stream))?;
let service = service.clone();
let builder = http.clone();
executor.execute(async move {
if let Err(err) = builder
.clone()
.serve_connection(conn, service::HyperService(service))
.await
{
log::error!("error serving connection: {}", err);
}
});
}
Ok(())
}
/// Sets the maximum number of threads in the worker pool.
///
/// By default, the limit is 15 threads per CPU core.
pub fn max_workers(mut self, val: usize) -> Self {
self.max_workers = Some(val);
self
}
/// Sets how long to keep alive an idle thread in the worker pool.
///
/// By default, the timeout is set to 6 seconds.
pub fn worker_keep_alive(mut self, val: Duration) -> Self {
self.worker_keep_alive = Some(val);
self
}
/// Sets whether to use keep-alive for HTTP/1 connections.
///
/// Default is `true`.
pub fn http1_keep_alive(mut self, val: bool) -> Self {
self.http1_keep_alive = Some(val);
self
}
/// Set whether HTTP/1 connections should support half-closures.
///
/// Clients can chose to shutdown their write-side while waiting
/// for the server to respond. Setting this to `true` will
/// prevent closing the connection immediately if `read`
/// detects an EOF in the middle of a request.
///
/// Default is `false`.
pub fn http1_half_close(mut self, val: bool) -> Self {
self.http1_half_close = Some(val);
self
}
/// Set the maximum buffer size.
///
/// Default is ~ 400kb.
pub fn http1_max_buf_size(mut self, val: usize) -> Self {
self.http1_max_buf_size = Some(val);
self
}
/// Sets whether to bunch up HTTP/1 writes until the read buffer is empty.
///
/// This isn't really desirable in most cases, only really being useful in
/// silly pipeline benchmarks.
pub fn http1_pipeline_flush(mut self, val: bool) -> Self {
self.http1_pipeline_flush = Some(val);
self
}
/// Set whether HTTP/1 connections should try to use vectored writes,
/// or always flatten into a single buffer.
///
/// Note that setting this to false may mean more copies of body data,
/// but may also improve performance when an IO transport doesn't
/// support vectored writes well, such as most TLS implementations.
///
/// Setting this to true will force hyper to use queued strategy
/// which may eliminate unnecessary cloning on some TLS backends
///
/// Default is `auto`. In this mode hyper will try to guess which
/// mode to use
pub fn http1_writev(mut self, enabled: bool) -> Self {
self.http1_writev = Some(enabled);
self
}
/// Set whether HTTP/1 connections will write header names as title case at
/// the socket level.
///
/// Note that this setting does not affect HTTP/2.
///
/// Default is false.
pub fn http1_title_case_headers(mut self, val: bool) -> Self {
self.http1_title_case_headers = Some(val);
self
}
/// Set whether to support preserving original header cases.
///
/// Currently, this will record the original cases received, and store them
/// in a private extension on the `Request`. It will also look for and use
/// such an extension in any provided `Response`.
///
/// Since the relevant extension is still private, there is no way to
/// interact with the original cases. The only effect this can have now is
/// to forward the cases in a proxy-like fashion.
///
/// Note that this setting does not affect HTTP/2.
///
/// Default is false.
pub fn http1_preserve_header_case(mut self, val: bool) -> Self {
self.http1_preserve_header_case = Some(val);
self
}
/// Sets whether HTTP/1 is required.
///
/// Default is `false`.
pub fn http1_only(mut self, val: bool) -> Self {
self.http1_only = Some(val);
self
}
/// Sets whether HTTP/2 is required.
///
/// Default is `false`.
pub fn http2_only(mut self, val: bool) -> Self {
self.http2_only = Some(val);
self
}
/// Sets the [`SETTINGS_INITIAL_WINDOW_SIZE`][spec] option for HTTP2
/// stream-level flow control.
///
/// Passing `None` will do nothing.
///
/// If not set, hyper will use a default.
///
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_INITIAL_WINDOW_SIZE
pub fn http2_initial_stream_window_size(mut self, sz: impl Into<Option<u32>>) -> Self {
self.http2_initial_stream_window_size = sz.into();
self
}
/// Sets the max connection-level flow control for HTTP2
///
/// Passing `None` will do nothing.
///
/// If not set, hyper will use a default.
pub fn http2_initial_connection_window_size(mut self, sz: impl Into<Option<u32>>) -> Self {
self.http2_initial_connection_window_size = sz.into();
self
}
/// Sets whether to use an adaptive flow control.
///
/// Enabling this will override the limits set in
/// `http2_initial_stream_window_size` and
/// `http2_initial_connection_window_size`.
pub fn http2_adaptive_window(mut self, enabled: bool) -> Self {
self.http2_adaptive_window = Some(enabled);
self
}
/// Sets the maximum frame size to use for HTTP2.
///
/// Passing `None` will do nothing.
///
/// If not set, hyper will use a default.
pub fn http2_max_frame_size(mut self, sz: impl Into<Option<u32>>) -> Self {
self.http2_max_frame_size = sz.into();
self
}
/// Sets the [`SETTINGS_MAX_CONCURRENT_STREAMS`][spec] option for HTTP2
/// connections.
///
/// Default is no limit (`std::u32::MAX`). Passing `None` will do nothing.
///
/// [spec]: https://http2.github.io/http2-spec/#SETTINGS_MAX_CONCURRENT_STREAMS
pub fn http2_max_concurrent_streams(mut self, max: impl Into<Option<u32>>) -> Self {
self.http2_max_concurrent_streams = max.into();
self
}
/// Set the maximum write buffer size for each HTTP/2 stream.
///
/// Default is currently ~400KB, but may change.
///
/// # Panics
///
/// The value must be no larger than `u32::MAX`.
pub fn http2_max_send_buf_size(mut self, max: usize) -> Self {
self.http2_max_send_buf_size = Some(max);
self
}
fn configure<T>(&self, http: &mut Http<T>) {
macro_rules! configure {
($self:ident, $other:expr, [$($option:ident),* $(,)?], [$($other_option:ident => $this_option:ident),* $(,)?]) => {{
$(
if let Some(val) = $self.$option {
$other.$option(val);
}
)*
$(
if let Some(val) = $self.$this_option {
$other.$other_option(val);
}
)*
}};
}
configure!(
self,
http,
[
http1_keep_alive,
http1_half_close,
http1_writev,
http1_title_case_headers,
http1_preserve_header_case,
http1_only,
http2_only,
http2_initial_stream_window_size,
http2_initial_connection_window_size,
http2_adaptive_window,
http2_max_frame_size,
http2_max_concurrent_streams,
http2_max_send_buf_size
],
[
max_buf_size => http1_max_buf_size,
pipeline_flush => http1_pipeline_flush,
]
);
}
}
mod service {
use super::*;
type HyperRequest = hyper::Request<hyper::Body>;
pub struct HyperService<S>(pub Arc<S>);
impl<S> hyper::service::Service<HyperRequest> for HyperService<S>
where
S: Service,
{
type Response = Response;
type Error = Infallible;
type Future = Lazy<S>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: HyperRequest) -> Self::Future {
Lazy(self.0.clone(), Some(req))
}
}
pub struct Lazy<S>(Arc<S>, Option<HyperRequest>);
impl<S> Future for Lazy<S>
where
S: Service,
{
type Output = Result<Response, Infallible>;
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
let (parts, body) = self.1.take().unwrap().into_parts();
let response = self.0.call(Request::from_parts(parts, Body(body)));
Poll::Ready(Ok(response))
}
}
}