mirror of https://github.com/fafhrd91/actix-web
start prioritising unit body responses
This commit is contained in:
parent
ab96ebb3e3
commit
a0c751f1d6
|
@ -1,6 +1,6 @@
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
use actix_http::{Error, HttpService, Request, Response};
|
use actix_http::{http::StatusCode, Error, HttpService, Request, Response};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_util::StreamExt as _;
|
use futures_util::StreamExt as _;
|
||||||
|
@ -25,7 +25,7 @@ async fn main() -> io::Result<()> {
|
||||||
|
|
||||||
info!("request body: {:?}", body);
|
info!("request body: {:?}", body);
|
||||||
Ok::<_, Error>(
|
Ok::<_, Error>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((
|
.insert_header((
|
||||||
"x-head",
|
"x-head",
|
||||||
HeaderValue::from_static("dummy value!"),
|
HeaderValue::from_static("dummy value!"),
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
use actix_http::{body::Body, http::HeaderValue};
|
use actix_http::{
|
||||||
use actix_http::{Error, HttpService, Request, Response};
|
body::Body,
|
||||||
|
http::{HeaderValue, StatusCode},
|
||||||
|
Error, HttpService, Request, Response,
|
||||||
|
};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_util::StreamExt as _;
|
use futures_util::StreamExt as _;
|
||||||
|
@ -14,7 +17,7 @@ async fn handle_request(mut req: Request) -> Result<Response<Body>, Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("request body: {:?}", body);
|
info!("request body: {:?}", body);
|
||||||
Ok(Response::Ok()
|
Ok(Response::builder(StatusCode::OK)
|
||||||
.insert_header(("x-head", HeaderValue::from_static("dummy value!")))
|
.insert_header(("x-head", HeaderValue::from_static("dummy value!")))
|
||||||
.body(body))
|
.body(body))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{env, io};
|
use std::{env, io};
|
||||||
|
|
||||||
use actix_http::{HttpService, Response};
|
use actix_http::{http::StatusCode, HttpService, Response};
|
||||||
use actix_server::Server;
|
use actix_server::Server;
|
||||||
use actix_utils::future;
|
use actix_utils::future;
|
||||||
use http::header::HeaderValue;
|
use http::header::HeaderValue;
|
||||||
|
@ -18,7 +18,7 @@ async fn main() -> io::Result<()> {
|
||||||
.client_disconnect(1000)
|
.client_disconnect(1000)
|
||||||
.finish(|_req| {
|
.finish(|_req| {
|
||||||
info!("{:?}", _req);
|
info!("{:?}", _req);
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::builder(StatusCode::OK);
|
||||||
res.insert_header((
|
res.insert_header((
|
||||||
"x-head",
|
"x-head",
|
||||||
HeaderValue::from_static("dummy value!"),
|
HeaderValue::from_static("dummy value!"),
|
||||||
|
|
|
@ -526,7 +526,7 @@ where
|
||||||
if let Some(resp) = resp.borrow_mut().take() {
|
if let Some(resp) = resp.borrow_mut().take() {
|
||||||
resp
|
resp
|
||||||
} else {
|
} else {
|
||||||
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
Response::with_body(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1021,8 +1021,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_internal_error() {
|
fn test_internal_error() {
|
||||||
let err =
|
let err = InternalError::from_response(
|
||||||
InternalError::from_response(ParseError::Method, Response::Ok().into());
|
ParseError::Method,
|
||||||
|
Response::builder(StatusCode::OK).into(),
|
||||||
|
);
|
||||||
let resp: Response<Body> = err.error_response();
|
let resp: Response<Body> = err.error_response();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ use actix_service::Service;
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use bytes::{Buf, BytesMut};
|
use bytes::{Buf, BytesMut};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
|
use http::StatusCode;
|
||||||
use log::{error, trace};
|
use log::{error, trace};
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
|
|
||||||
|
@ -562,7 +563,7 @@ where
|
||||||
);
|
);
|
||||||
this.flags.insert(Flags::READ_DISCONNECT);
|
this.flags.insert(Flags::READ_DISCONNECT);
|
||||||
this.messages.push_back(DispatcherMessage::Error(
|
this.messages.push_back(DispatcherMessage::Error(
|
||||||
Response::InternalServerError().finish().drop_body(),
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR),
|
||||||
));
|
));
|
||||||
*this.error = Some(DispatchError::InternalError);
|
*this.error = Some(DispatchError::InternalError);
|
||||||
break;
|
break;
|
||||||
|
@ -575,7 +576,7 @@ where
|
||||||
error!("Internal server error: unexpected eof");
|
error!("Internal server error: unexpected eof");
|
||||||
this.flags.insert(Flags::READ_DISCONNECT);
|
this.flags.insert(Flags::READ_DISCONNECT);
|
||||||
this.messages.push_back(DispatcherMessage::Error(
|
this.messages.push_back(DispatcherMessage::Error(
|
||||||
Response::InternalServerError().finish().drop_body(),
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR),
|
||||||
));
|
));
|
||||||
*this.error = Some(DispatchError::InternalError);
|
*this.error = Some(DispatchError::InternalError);
|
||||||
break;
|
break;
|
||||||
|
@ -597,9 +598,10 @@ where
|
||||||
payload.set_error(PayloadError::Overflow);
|
payload.set_error(PayloadError::Overflow);
|
||||||
}
|
}
|
||||||
// Requests overflow buffer size should be responded with 431
|
// Requests overflow buffer size should be responded with 431
|
||||||
this.messages.push_back(DispatcherMessage::Error(
|
this.messages
|
||||||
Response::RequestHeaderFieldsTooLarge().finish().drop_body(),
|
.push_back(DispatcherMessage::Error(Response::new(
|
||||||
));
|
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE,
|
||||||
|
)));
|
||||||
this.flags.insert(Flags::READ_DISCONNECT);
|
this.flags.insert(Flags::READ_DISCONNECT);
|
||||||
*this.error = Some(ParseError::TooLarge.into());
|
*this.error = Some(ParseError::TooLarge.into());
|
||||||
break;
|
break;
|
||||||
|
@ -610,9 +612,10 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
// Malformed requests should be responded with 400
|
// Malformed requests should be responded with 400
|
||||||
this.messages.push_back(DispatcherMessage::Error(
|
this.messages
|
||||||
Response::BadRequest().finish().drop_body(),
|
.push_back(DispatcherMessage::Error(Response::new(
|
||||||
));
|
StatusCode::BAD_REQUEST,
|
||||||
|
)));
|
||||||
this.flags.insert(Flags::READ_DISCONNECT);
|
this.flags.insert(Flags::READ_DISCONNECT);
|
||||||
*this.error = Some(err.into());
|
*this.error = Some(err.into());
|
||||||
break;
|
break;
|
||||||
|
@ -684,7 +687,7 @@ where
|
||||||
if !this.flags.contains(Flags::STARTED) {
|
if !this.flags.contains(Flags::STARTED) {
|
||||||
trace!("Slow request timeout");
|
trace!("Slow request timeout");
|
||||||
let _ = self.as_mut().send_response(
|
let _ = self.as_mut().send_response(
|
||||||
Response::RequestTimeout().finish().drop_body(),
|
Response::new(StatusCode::REQUEST_TIMEOUT),
|
||||||
ResponseBody::Other(Body::Empty),
|
ResponseBody::Other(Body::Empty),
|
||||||
);
|
);
|
||||||
this = self.project();
|
this = self.project();
|
||||||
|
@ -951,6 +954,7 @@ mod tests {
|
||||||
|
|
||||||
use actix_service::fn_service;
|
use actix_service::fn_service;
|
||||||
use actix_utils::future::{ready, Ready};
|
use actix_utils::future::{ready, Ready};
|
||||||
|
use bytes::Bytes;
|
||||||
use futures_util::future::lazy;
|
use futures_util::future::lazy;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -978,20 +982,22 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ok_service() -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
fn ok_service() -> impl Service<Request, Response = Response<()>, Error = Error> {
|
||||||
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::Ok().finish())))
|
fn_service(|_req: Request| ready(Ok::<_, Error>(Response::ok())))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn echo_path_service(
|
fn echo_path_service(
|
||||||
) -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
) -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
||||||
fn_service(|req: Request| {
|
fn_service(|req: Request| {
|
||||||
let path = req.path().as_bytes();
|
let path = req.path().as_bytes();
|
||||||
ready(Ok::<_, Error>(Response::Ok().body(Body::from_slice(path))))
|
ready(Ok::<_, Error>(
|
||||||
|
Response::ok().set_body(Body::from_slice(path)),
|
||||||
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn echo_payload_service(
|
fn echo_payload_service(
|
||||||
) -> impl Service<Request, Response = Response<Body>, Error = Error> {
|
) -> impl Service<Request, Response = Response<Bytes>, Error = Error> {
|
||||||
fn_service(|mut req: Request| {
|
fn_service(|mut req: Request| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
use futures_util::stream::StreamExt as _;
|
use futures_util::stream::StreamExt as _;
|
||||||
|
@ -1002,7 +1008,7 @@ mod tests {
|
||||||
body.extend_from_slice(chunk.unwrap().chunk())
|
body.extend_from_slice(chunk.unwrap().chunk())
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok::<_, Error>(Response::Ok().body(body))
|
Ok::<_, Error>(Response::ok().set_body(body.freeze()))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -630,8 +630,7 @@ mod tests {
|
||||||
async fn test_no_content_length() {
|
async fn test_no_content_length() {
|
||||||
let mut bytes = BytesMut::with_capacity(2048);
|
let mut bytes = BytesMut::with_capacity(2048);
|
||||||
|
|
||||||
let mut res: Response<()> =
|
let mut res: Response<()> = Response::new(StatusCode::SWITCHING_PROTOCOLS);
|
||||||
Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>();
|
|
||||||
res.headers_mut().insert(DATE, HeaderValue::from_static(""));
|
res.headers_mut().insert(DATE, HeaderValue::from_static(""));
|
||||||
res.headers_mut()
|
res.headers_mut()
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
||||||
|
|
|
@ -28,29 +28,46 @@ pub struct Response<B> {
|
||||||
error: Option<Error>,
|
error: Option<Error>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response<Body> {
|
impl Response<()> {
|
||||||
/// Create HTTP response builder with specific status.
|
|
||||||
#[inline]
|
|
||||||
pub fn build(status: StatusCode) -> ResponseBuilder {
|
|
||||||
ResponseBuilder::new(status)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create HTTP response builder
|
|
||||||
#[inline]
|
|
||||||
pub fn build_from<T: Into<ResponseBuilder>>(source: T) -> ResponseBuilder {
|
|
||||||
source.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Constructs a response
|
/// Constructs a response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(status: StatusCode) -> Response<Body> {
|
pub fn new(status: StatusCode) -> Response<()> {
|
||||||
Response {
|
Response {
|
||||||
head: BoxedResponseHead::new(status),
|
head: BoxedResponseHead::new(status),
|
||||||
body: ResponseBody::Body(Body::Empty),
|
body: ResponseBody::Body(()),
|
||||||
error: None,
|
error: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create HTTP response builder with specific status.
|
||||||
|
#[inline]
|
||||||
|
pub fn builder(status: StatusCode) -> ResponseBuilder {
|
||||||
|
ResponseBuilder::new(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// just a couple frequently used shortcuts
|
||||||
|
// this list should not grow as larger than 3 or 4
|
||||||
|
|
||||||
|
/// Creates a new response with status 200 OK.
|
||||||
|
#[inline]
|
||||||
|
pub fn ok() -> Response<()> {
|
||||||
|
Response::new(StatusCode::OK)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new response with status 400 Bad Request.
|
||||||
|
#[inline]
|
||||||
|
pub fn bad_request() -> Response<()> {
|
||||||
|
Response::new(StatusCode::OK)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new response with status 404 Not Found.
|
||||||
|
#[inline]
|
||||||
|
pub fn not_found() -> Response<()> {
|
||||||
|
Response::new(StatusCode::NOT_FOUND)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Response<Body> {
|
||||||
/// Constructs an error response
|
/// Constructs an error response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_error(error: Error) -> Response<Body> {
|
pub fn from_error(error: Error) -> Response<Body> {
|
||||||
|
@ -557,7 +574,7 @@ impl From<ResponseBuilder> for Response<Body> {
|
||||||
|
|
||||||
impl From<&'static str> for Response<Body> {
|
impl From<&'static str> for Response<Body> {
|
||||||
fn from(val: &'static str) -> Self {
|
fn from(val: &'static str) -> Self {
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.content_type(mime::TEXT_PLAIN_UTF_8)
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
||||||
.body(val)
|
.body(val)
|
||||||
}
|
}
|
||||||
|
@ -565,7 +582,7 @@ impl From<&'static str> for Response<Body> {
|
||||||
|
|
||||||
impl From<&'static [u8]> for Response<Body> {
|
impl From<&'static [u8]> for Response<Body> {
|
||||||
fn from(val: &'static [u8]) -> Self {
|
fn from(val: &'static [u8]) -> Self {
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.content_type(mime::APPLICATION_OCTET_STREAM)
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
||||||
.body(val)
|
.body(val)
|
||||||
}
|
}
|
||||||
|
@ -573,7 +590,7 @@ impl From<&'static [u8]> for Response<Body> {
|
||||||
|
|
||||||
impl From<String> for Response<Body> {
|
impl From<String> for Response<Body> {
|
||||||
fn from(val: String) -> Self {
|
fn from(val: String) -> Self {
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.content_type(mime::TEXT_PLAIN_UTF_8)
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
||||||
.body(val)
|
.body(val)
|
||||||
}
|
}
|
||||||
|
@ -581,7 +598,7 @@ impl From<String> for Response<Body> {
|
||||||
|
|
||||||
impl<'a> From<&'a String> for Response<Body> {
|
impl<'a> From<&'a String> for Response<Body> {
|
||||||
fn from(val: &'a String) -> Self {
|
fn from(val: &'a String) -> Self {
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.content_type(mime::TEXT_PLAIN_UTF_8)
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
||||||
.body(val)
|
.body(val)
|
||||||
}
|
}
|
||||||
|
@ -589,7 +606,7 @@ impl<'a> From<&'a String> for Response<Body> {
|
||||||
|
|
||||||
impl From<Bytes> for Response<Body> {
|
impl From<Bytes> for Response<Body> {
|
||||||
fn from(val: Bytes) -> Self {
|
fn from(val: Bytes) -> Self {
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.content_type(mime::APPLICATION_OCTET_STREAM)
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
||||||
.body(val)
|
.body(val)
|
||||||
}
|
}
|
||||||
|
@ -597,7 +614,7 @@ impl From<Bytes> for Response<Body> {
|
||||||
|
|
||||||
impl From<BytesMut> for Response<Body> {
|
impl From<BytesMut> for Response<Body> {
|
||||||
fn from(val: BytesMut) -> Self {
|
fn from(val: BytesMut) -> Self {
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.content_type(mime::APPLICATION_OCTET_STREAM)
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
||||||
.body(val)
|
.body(val)
|
||||||
}
|
}
|
||||||
|
@ -611,7 +628,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debug() {
|
fn test_debug() {
|
||||||
let resp = Response::Ok()
|
let resp = Response::builder(StatusCode::OK)
|
||||||
.append_header((COOKIE, HeaderValue::from_static("cookie1=value1; ")))
|
.append_header((COOKIE, HeaderValue::from_static("cookie1=value1; ")))
|
||||||
.append_header((COOKIE, HeaderValue::from_static("cookie2=value2; ")))
|
.append_header((COOKIE, HeaderValue::from_static("cookie2=value2; ")))
|
||||||
.finish();
|
.finish();
|
||||||
|
@ -621,13 +638,15 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_basic_builder() {
|
fn test_basic_builder() {
|
||||||
let resp = Response::Ok().insert_header(("X-TEST", "value")).finish();
|
let resp = Response::builder(StatusCode::OK)
|
||||||
|
.insert_header(("X-TEST", "value"))
|
||||||
|
.finish();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_upgrade() {
|
fn test_upgrade() {
|
||||||
let resp = Response::build(StatusCode::OK)
|
let resp = Response::builder(StatusCode::OK)
|
||||||
.upgrade("websocket")
|
.upgrade("websocket")
|
||||||
.finish();
|
.finish();
|
||||||
assert!(resp.upgrade());
|
assert!(resp.upgrade());
|
||||||
|
@ -639,13 +658,13 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_force_close() {
|
fn test_force_close() {
|
||||||
let resp = Response::build(StatusCode::OK).force_close().finish();
|
let resp = Response::builder(StatusCode::OK).force_close().finish();
|
||||||
assert!(!resp.keep_alive())
|
assert!(!resp.keep_alive())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_content_type() {
|
fn test_content_type() {
|
||||||
let resp = Response::build(StatusCode::OK)
|
let resp = Response::builder(StatusCode::OK)
|
||||||
.content_type("text/plain")
|
.content_type("text/plain")
|
||||||
.body(Body::Empty);
|
.body(Body::Empty);
|
||||||
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
||||||
|
@ -741,7 +760,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn response_builder_header_insert_kv() {
|
fn response_builder_header_insert_kv() {
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::builder(StatusCode::OK);
|
||||||
res.insert_header(("Content-Type", "application/octet-stream"));
|
res.insert_header(("Content-Type", "application/octet-stream"));
|
||||||
let res = res.finish();
|
let res = res.finish();
|
||||||
|
|
||||||
|
@ -753,7 +772,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn response_builder_header_insert_typed() {
|
fn response_builder_header_insert_typed() {
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::builder(StatusCode::OK);
|
||||||
res.insert_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
|
res.insert_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
|
||||||
let res = res.finish();
|
let res = res.finish();
|
||||||
|
|
||||||
|
@ -765,7 +784,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn response_builder_header_append_kv() {
|
fn response_builder_header_append_kv() {
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::builder(StatusCode::OK);
|
||||||
res.append_header(("Content-Type", "application/octet-stream"));
|
res.append_header(("Content-Type", "application/octet-stream"));
|
||||||
res.append_header(("Content-Type", "application/json"));
|
res.append_header(("Content-Type", "application/json"));
|
||||||
let res = res.finish();
|
let res = res.finish();
|
||||||
|
@ -778,7 +797,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn response_builder_header_append_typed() {
|
fn response_builder_header_append_typed() {
|
||||||
let mut res = Response::Ok();
|
let mut res = Response::builder(StatusCode::OK);
|
||||||
res.append_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
|
res.append_header((header::CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM));
|
||||||
res.append_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
|
res.append_header((header::CONTENT_TYPE, mime::APPLICATION_JSON));
|
||||||
let res = res.finish();
|
let res = res.finish();
|
||||||
|
|
|
@ -203,7 +203,7 @@ pub fn handshake_response(req: &RequestHead) -> ResponseBuilder {
|
||||||
proto::hash_key(key.as_ref())
|
proto::hash_key(key.as_ref())
|
||||||
};
|
};
|
||||||
|
|
||||||
Response::build(StatusCode::SWITCHING_PROTOCOLS)
|
Response::builder(StatusCode::SWITCHING_PROTOCOLS)
|
||||||
.upgrade("websocket")
|
.upgrade("websocket")
|
||||||
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
||||||
.insert_header((
|
.insert_header((
|
||||||
|
|
|
@ -33,7 +33,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
async fn test_h1_v2() {
|
async fn test_h1_v2() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
|
.finish(|_| future::ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -61,7 +61,7 @@ async fn test_h1_v2() {
|
||||||
async fn test_connection_close() {
|
async fn test_connection_close() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
|
.finish(|_| future::ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -77,9 +77,9 @@ async fn test_with_query_parameter() {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|req: Request| {
|
.finish(|req: Request| {
|
||||||
if req.uri().query().unwrap().contains("qp=") {
|
if req.uri().query().unwrap().contains("qp=") {
|
||||||
future::ok::<_, ()>(Response::Ok().finish())
|
future::ok::<_, ()>(Response::ok())
|
||||||
} else {
|
} else {
|
||||||
future::ok::<_, ()>(Response::BadRequest().finish())
|
future::ok::<_, ()>(Response::bad_request())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
|
@ -112,7 +112,7 @@ async fn test_h1_expect() {
|
||||||
let str = std::str::from_utf8(&buf).unwrap();
|
let str = std::str::from_utf8(&buf).unwrap();
|
||||||
assert_eq!(str, "expect body");
|
assert_eq!(str, "expect body");
|
||||||
|
|
||||||
Ok::<_, ()>(Response::Ok().finish())
|
Ok::<_, ()>(Response::ok())
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
|
|
|
@ -71,7 +71,7 @@ fn tls_config() -> SslAcceptor {
|
||||||
async fn test_h2() -> io::Result<()> {
|
async fn test_h2() -> io::Result<()> {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, Error>(Response::Ok().finish()))
|
.h2(|_| ok::<_, Error>(Response::ok()))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -89,7 +89,7 @@ async fn test_h2_1() -> io::Result<()> {
|
||||||
.finish(|req: Request| {
|
.finish(|req: Request| {
|
||||||
assert!(req.peer_addr().is_some());
|
assert!(req.peer_addr().is_some());
|
||||||
assert_eq!(req.version(), Version::HTTP_2);
|
assert_eq!(req.version(), Version::HTTP_2);
|
||||||
ok::<_, Error>(Response::Ok().finish())
|
ok::<_, Error>(Response::ok())
|
||||||
})
|
})
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
@ -108,7 +108,7 @@ async fn test_h2_body() -> io::Result<()> {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|mut req: Request<_>| async move {
|
.h2(|mut req: Request<_>| async move {
|
||||||
let body = load_body(req.take_payload()).await?;
|
let body = load_body(req.take_payload()).await?;
|
||||||
Ok::<_, Error>(Response::Ok().body(body))
|
Ok::<_, Error>(Response::ok().set_body(body))
|
||||||
})
|
})
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
@ -186,7 +186,7 @@ async fn test_h2_headers() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
let data = data.clone();
|
let data = data.clone();
|
||||||
HttpService::build().h2(move |_| {
|
HttpService::build().h2(move |_| {
|
||||||
let mut builder = Response::Ok();
|
let mut builder = Response::builder(StatusCode::OK);
|
||||||
for idx in 0..90 {
|
for idx in 0..90 {
|
||||||
builder.insert_header(
|
builder.insert_header(
|
||||||
(format!("X-TEST-{}", idx).as_str(),
|
(format!("X-TEST-{}", idx).as_str(),
|
||||||
|
@ -245,7 +245,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
async fn test_h2_body2() {
|
async fn test_h2_body2() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -263,7 +263,7 @@ async fn test_h2_body2() {
|
||||||
async fn test_h2_head_empty() {
|
async fn test_h2_head_empty() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.finish(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -287,7 +287,7 @@ async fn test_h2_head_empty() {
|
||||||
async fn test_h2_head_binary() {
|
async fn test_h2_head_binary() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -310,7 +310,7 @@ async fn test_h2_head_binary() {
|
||||||
async fn test_h2_head_binary2() {
|
async fn test_h2_head_binary2() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -332,7 +332,8 @@ async fn test_h2_body_length() {
|
||||||
.h2(|_| {
|
.h2(|_| {
|
||||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok().body(SizedStream::new(STR.len() as u64, body)),
|
Response::builder(StatusCode::OK)
|
||||||
|
.body(SizedStream::new(STR.len() as u64, body)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
|
@ -355,7 +356,7 @@ async fn test_h2_body_chunked_explicit() {
|
||||||
.h2(|_| {
|
.h2(|_| {
|
||||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
||||||
.streaming(body),
|
.streaming(body),
|
||||||
)
|
)
|
||||||
|
@ -383,7 +384,7 @@ async fn test_h2_response_http_error_handling() {
|
||||||
.h2(fn_service(|_| {
|
.h2(fn_service(|_| {
|
||||||
let broken_header = Bytes::from_static(b"\0\0\0");
|
let broken_header = Bytes::from_static(b"\0\0\0");
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((header::CONTENT_TYPE, broken_header))
|
.insert_header((header::CONTENT_TYPE, broken_header))
|
||||||
.body(STR),
|
.body(STR),
|
||||||
)
|
)
|
||||||
|
@ -428,7 +429,7 @@ async fn test_h2_on_connect() {
|
||||||
})
|
})
|
||||||
.h2(|req: Request| {
|
.h2(|req: Request| {
|
||||||
assert!(req.extensions().contains::<isize>());
|
assert!(req.extensions().contains::<isize>());
|
||||||
ok::<_, ()>(Response::Ok().finish())
|
ok::<_, ()>(Response::ok())
|
||||||
})
|
})
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
|
|
@ -56,7 +56,7 @@ fn tls_config() -> RustlsServerConfig {
|
||||||
async fn test_h1() -> io::Result<()> {
|
async fn test_h1() -> io::Result<()> {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, Error>(Response::Ok().finish()))
|
.h1(|_| ok::<_, Error>(Response::ok()))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -70,7 +70,7 @@ async fn test_h1() -> io::Result<()> {
|
||||||
async fn test_h2() -> io::Result<()> {
|
async fn test_h2() -> io::Result<()> {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, Error>(Response::Ok().finish()))
|
.h2(|_| ok::<_, Error>(Response::ok()))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -87,7 +87,7 @@ async fn test_h1_1() -> io::Result<()> {
|
||||||
.h1(|req: Request| {
|
.h1(|req: Request| {
|
||||||
assert!(req.peer_addr().is_some());
|
assert!(req.peer_addr().is_some());
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
ok::<_, Error>(Response::Ok().finish())
|
ok::<_, Error>(Response::ok())
|
||||||
})
|
})
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
|
@ -105,7 +105,7 @@ async fn test_h2_1() -> io::Result<()> {
|
||||||
.finish(|req: Request| {
|
.finish(|req: Request| {
|
||||||
assert!(req.peer_addr().is_some());
|
assert!(req.peer_addr().is_some());
|
||||||
assert_eq!(req.version(), Version::HTTP_2);
|
assert_eq!(req.version(), Version::HTTP_2);
|
||||||
ok::<_, Error>(Response::Ok().finish())
|
ok::<_, Error>(Response::ok())
|
||||||
})
|
})
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
|
@ -123,7 +123,7 @@ async fn test_h2_body1() -> io::Result<()> {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|mut req: Request<_>| async move {
|
.h2(|mut req: Request<_>| async move {
|
||||||
let body = load_body(req.take_payload()).await?;
|
let body = load_body(req.take_payload()).await?;
|
||||||
Ok::<_, Error>(Response::Ok().body(body))
|
Ok::<_, Error>(Response::ok().set_body(body))
|
||||||
})
|
})
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
|
@ -199,7 +199,7 @@ async fn test_h2_headers() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
let data = data.clone();
|
let data = data.clone();
|
||||||
HttpService::build().h2(move |_| {
|
HttpService::build().h2(move |_| {
|
||||||
let mut config = Response::Ok();
|
let mut config = Response::builder(StatusCode::OK);
|
||||||
for idx in 0..90 {
|
for idx in 0..90 {
|
||||||
config.insert_header((
|
config.insert_header((
|
||||||
format!("X-TEST-{}", idx).as_str(),
|
format!("X-TEST-{}", idx).as_str(),
|
||||||
|
@ -257,7 +257,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
async fn test_h2_body2() {
|
async fn test_h2_body2() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -274,7 +274,7 @@ async fn test_h2_body2() {
|
||||||
async fn test_h2_head_empty() {
|
async fn test_h2_head_empty() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.finish(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -300,7 +300,7 @@ async fn test_h2_head_empty() {
|
||||||
async fn test_h2_head_binary() {
|
async fn test_h2_head_binary() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -325,7 +325,7 @@ async fn test_h2_head_binary() {
|
||||||
async fn test_h2_head_binary2() {
|
async fn test_h2_head_binary2() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -349,7 +349,7 @@ async fn test_h2_body_length() {
|
||||||
.h2(|_| {
|
.h2(|_| {
|
||||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok().body(SizedStream::new(STR.len() as u64, body)),
|
Response::ok().set_body(SizedStream::new(STR.len() as u64, body)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
|
@ -371,7 +371,7 @@ async fn test_h2_body_chunked_explicit() {
|
||||||
.h2(|_| {
|
.h2(|_| {
|
||||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
||||||
.streaming(body),
|
.streaming(body),
|
||||||
)
|
)
|
||||||
|
@ -399,7 +399,7 @@ async fn test_h2_response_http_error_handling() {
|
||||||
ok::<_, ()>(fn_service(|_| {
|
ok::<_, ()>(fn_service(|_| {
|
||||||
let broken_header = Bytes::from_static(b"\0\0\0");
|
let broken_header = Bytes::from_static(b"\0\0\0");
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((http::header::CONTENT_TYPE, broken_header))
|
.insert_header((http::header::CONTENT_TYPE, broken_header))
|
||||||
.body(STR),
|
.body(STR),
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,8 +14,9 @@ use regex::Regex;
|
||||||
use actix_http::HttpMessage;
|
use actix_http::HttpMessage;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::{Body, SizedStream},
|
body::{Body, SizedStream},
|
||||||
error, http,
|
error,
|
||||||
http::header,
|
http::header,
|
||||||
|
http::{self, StatusCode},
|
||||||
Error, HttpService, KeepAlive, Request, Response,
|
Error, HttpService, KeepAlive, Request, Response,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -28,7 +29,7 @@ async fn test_h1() {
|
||||||
.client_disconnect(1000)
|
.client_disconnect(1000)
|
||||||
.h1(|req: Request| {
|
.h1(|req: Request| {
|
||||||
assert!(req.peer_addr().is_some());
|
assert!(req.peer_addr().is_some());
|
||||||
ok::<_, ()>(Response::Ok().finish())
|
ok::<_, ()>(Response::ok())
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
|
@ -48,7 +49,7 @@ async fn test_h1_2() {
|
||||||
.finish(|req: Request| {
|
.finish(|req: Request| {
|
||||||
assert!(req.peer_addr().is_some());
|
assert!(req.peer_addr().is_some());
|
||||||
assert_eq!(req.version(), http::Version::HTTP_11);
|
assert_eq!(req.version(), http::Version::HTTP_11);
|
||||||
ok::<_, ()>(Response::Ok().finish())
|
ok::<_, ()>(Response::ok())
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
|
@ -69,7 +70,7 @@ async fn test_expect_continue() {
|
||||||
err(error::ErrorPreconditionFailed("error"))
|
err(error::ErrorPreconditionFailed("error"))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.finish(|_| ok::<_, ()>(Response::Ok().finish()))
|
.finish(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -100,7 +101,7 @@ async fn test_expect_continue_h1() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
.h1(fn_service(|_| ok::<_, ()>(Response::Ok().finish())))
|
.h1(fn_service(|_| ok::<_, ()>(Response::ok())))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -134,7 +135,9 @@ async fn test_chunked_payload() {
|
||||||
})
|
})
|
||||||
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
.fold(0usize, |acc, chunk| ready(acc + chunk.len()))
|
||||||
.map(|req_size| {
|
.map(|req_size| {
|
||||||
Ok::<_, Error>(Response::Ok().body(format!("size={}", req_size)))
|
Ok::<_, Error>(
|
||||||
|
Response::ok().set_body(format!("size={}", req_size)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
.tcp()
|
.tcp()
|
||||||
|
@ -179,7 +182,7 @@ async fn test_slow_request() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.client_timeout(100)
|
.client_timeout(100)
|
||||||
.finish(|_| ok::<_, ()>(Response::Ok().finish()))
|
.finish(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -195,7 +198,7 @@ async fn test_slow_request() {
|
||||||
async fn test_http1_malformed_request() {
|
async fn test_http1_malformed_request() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -211,7 +214,7 @@ async fn test_http1_malformed_request() {
|
||||||
async fn test_http1_keepalive() {
|
async fn test_http1_keepalive() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -233,7 +236,7 @@ async fn test_http1_keepalive_timeout() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(1)
|
.keep_alive(1)
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -254,7 +257,7 @@ async fn test_http1_keepalive_timeout() {
|
||||||
async fn test_http1_keepalive_close() {
|
async fn test_http1_keepalive_close() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -275,7 +278,7 @@ async fn test_http1_keepalive_close() {
|
||||||
async fn test_http10_keepalive_default_close() {
|
async fn test_http10_keepalive_default_close() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -295,7 +298,7 @@ async fn test_http10_keepalive_default_close() {
|
||||||
async fn test_http10_keepalive() {
|
async fn test_http10_keepalive() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -323,7 +326,7 @@ async fn test_http1_keepalive_disabled() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.keep_alive(KeepAlive::Disabled)
|
.keep_alive(KeepAlive::Disabled)
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().finish()))
|
.h1(|_| ok::<_, ()>(Response::ok()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -394,7 +397,7 @@ async fn test_h1_headers() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
let data = data.clone();
|
let data = data.clone();
|
||||||
HttpService::build().h1(move |_| {
|
HttpService::build().h1(move |_| {
|
||||||
let mut builder = Response::Ok();
|
let mut builder = Response::builder(StatusCode::OK);
|
||||||
for idx in 0..90 {
|
for idx in 0..90 {
|
||||||
builder.insert_header((
|
builder.insert_header((
|
||||||
format!("X-TEST-{}", idx).as_str(),
|
format!("X-TEST-{}", idx).as_str(),
|
||||||
|
@ -451,7 +454,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
async fn test_h1_body() {
|
async fn test_h1_body() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -468,7 +471,7 @@ async fn test_h1_body() {
|
||||||
async fn test_h1_head_empty() {
|
async fn test_h1_head_empty() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -493,7 +496,7 @@ async fn test_h1_head_empty() {
|
||||||
async fn test_h1_head_binary() {
|
async fn test_h1_head_binary() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -518,7 +521,7 @@ async fn test_h1_head_binary() {
|
||||||
async fn test_h1_head_binary2() {
|
async fn test_h1_head_binary2() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::Ok().body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -542,7 +545,7 @@ async fn test_h1_body_length() {
|
||||||
.h1(|_| {
|
.h1(|_| {
|
||||||
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
let body = once(ok(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok().body(SizedStream::new(STR.len() as u64, body)),
|
Response::ok().set_body(SizedStream::new(STR.len() as u64, body)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
|
@ -564,7 +567,7 @@ async fn test_h1_body_chunked_explicit() {
|
||||||
.h1(|_| {
|
.h1(|_| {
|
||||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
||||||
.streaming(body),
|
.streaming(body),
|
||||||
)
|
)
|
||||||
|
@ -598,7 +601,7 @@ async fn test_h1_body_chunked_implicit() {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| {
|
.h1(|_| {
|
||||||
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
||||||
ok::<_, ()>(Response::Ok().streaming(body))
|
ok::<_, ()>(Response::builder(StatusCode::OK).streaming(body))
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
|
@ -628,7 +631,7 @@ async fn test_h1_response_http_error_handling() {
|
||||||
.h1(fn_service(|_| {
|
.h1(fn_service(|_| {
|
||||||
let broken_header = Bytes::from_static(b"\0\0\0");
|
let broken_header = Bytes::from_static(b"\0\0\0");
|
||||||
ok::<_, ()>(
|
ok::<_, ()>(
|
||||||
Response::Ok()
|
Response::builder(StatusCode::OK)
|
||||||
.insert_header((http::header::CONTENT_TYPE, broken_header))
|
.insert_header((http::header::CONTENT_TYPE, broken_header))
|
||||||
.body(STR),
|
.body(STR),
|
||||||
)
|
)
|
||||||
|
@ -671,7 +674,7 @@ async fn test_h1_on_connect() {
|
||||||
})
|
})
|
||||||
.h1(|req: Request| {
|
.h1(|req: Request| {
|
||||||
assert!(req.extensions().contains::<isize>());
|
assert!(req.extensions().contains::<isize>());
|
||||||
ok::<_, ()>(Response::Ok().finish())
|
ok::<_, ()>(Response::ok())
|
||||||
})
|
})
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
|
|
|
@ -91,7 +91,7 @@ async fn test_simple() {
|
||||||
let ws_service = ws_service.clone();
|
let ws_service = ws_service.clone();
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.upgrade(fn_factory(move || future::ok::<_, ()>(ws_service.clone())))
|
.upgrade(fn_factory(move || future::ok::<_, ()>(ws_service.clone())))
|
||||||
.finish(|_| future::ok::<_, ()>(Response::NotFound()))
|
.finish(|_| future::ok::<_, ()>(Response::not_found()))
|
||||||
.tcp()
|
.tcp()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -36,7 +36,7 @@ async fn test_simple() {
|
||||||
ws::Dispatcher::with(framed, ws_service).await
|
ws::Dispatcher::with(framed, ws_service).await
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.finish(|_| ok::<_, Error>(Response::NotFound()))
|
.finish(|_| ok::<_, Error>(Response::not_found()))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -1,20 +1,23 @@
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::{Extensions, Request, Response};
|
use actix_http::{Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Router, Url};
|
use actix_router::{Path, ResourceDef, Router, Url};
|
||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||||
use actix_service::{fn_service, Service, ServiceFactory};
|
use actix_service::{fn_service, Service, ServiceFactory};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
|
||||||
use crate::config::{AppConfig, AppService};
|
|
||||||
use crate::data::FnDataFactory;
|
use crate::data::FnDataFactory;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::guard::Guard;
|
use crate::guard::Guard;
|
||||||
use crate::request::{HttpRequest, HttpRequestPool};
|
use crate::request::{HttpRequest, HttpRequestPool};
|
||||||
use crate::rmap::ResourceMap;
|
use crate::rmap::ResourceMap;
|
||||||
use crate::service::{AppServiceFactory, ServiceRequest, ServiceResponse};
|
use crate::service::{AppServiceFactory, ServiceRequest, ServiceResponse};
|
||||||
|
use crate::{
|
||||||
|
config::{AppConfig, AppService},
|
||||||
|
HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
||||||
|
@ -64,7 +67,7 @@ where
|
||||||
// if no user defined default service exists.
|
// if no user defined default service exists.
|
||||||
let default = self.default.clone().unwrap_or_else(|| {
|
let default = self.default.clone().unwrap_or_else(|| {
|
||||||
Rc::new(boxed::factory(fn_service(|req: ServiceRequest| async {
|
Rc::new(boxed::factory(fn_service(|req: ServiceRequest| async {
|
||||||
Ok(req.into_response(Response::NotFound().finish()))
|
Ok(req.into_response(HttpResponse::NotFound()))
|
||||||
})))
|
})))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::fmt;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::{Error, Extensions, Response};
|
use actix_http::{Error, Extensions};
|
||||||
use actix_router::IntoPattern;
|
use actix_router::IntoPattern;
|
||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
|
@ -13,7 +13,6 @@ use actix_service::{
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
|
||||||
use crate::data::Data;
|
|
||||||
use crate::dev::{insert_slash, AppService, HttpServiceFactory, ResourceDef};
|
use crate::dev::{insert_slash, AppService, HttpServiceFactory, ResourceDef};
|
||||||
use crate::extract::FromRequest;
|
use crate::extract::FromRequest;
|
||||||
use crate::guard::Guard;
|
use crate::guard::Guard;
|
||||||
|
@ -21,6 +20,7 @@ use crate::handler::Handler;
|
||||||
use crate::responder::Responder;
|
use crate::responder::Responder;
|
||||||
use crate::route::{Route, RouteService};
|
use crate::route::{Route, RouteService};
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
use crate::service::{ServiceRequest, ServiceResponse};
|
||||||
|
use crate::{data::Data, HttpResponse};
|
||||||
|
|
||||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
||||||
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
||||||
|
@ -71,7 +71,7 @@ impl Resource {
|
||||||
guards: Vec::new(),
|
guards: Vec::new(),
|
||||||
app_data: None,
|
app_data: None,
|
||||||
default: boxed::factory(fn_service(|req: ServiceRequest| async {
|
default: boxed::factory(fn_service(|req: ServiceRequest| async {
|
||||||
Ok(req.into_response(Response::MethodNotAllowed().finish()))
|
Ok(req.into_response(HttpResponse::MethodNotAllowed()))
|
||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ impl HttpResponse<Body> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(status: StatusCode) -> Self {
|
pub fn new(status: StatusCode) -> Self {
|
||||||
Self {
|
Self {
|
||||||
res: Response::new(status),
|
res: Response::with_body(status, Body::Empty),
|
||||||
error: None,
|
error: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,7 +297,7 @@ impl Future for HttpResponse {
|
||||||
|
|
||||||
Poll::Ready(Ok(mem::replace(
|
Poll::Ready(Ok(mem::replace(
|
||||||
&mut self.res,
|
&mut self.res,
|
||||||
Response::new(StatusCode::default()),
|
Response::with_body(StatusCode::OK, Body::Empty),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue