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