Skip to content

Commit

Permalink
body ergonomics v3 (actix#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Dec 4, 2021
1 parent a2d5c5a commit c7c02ef
Show file tree
Hide file tree
Showing 84 changed files with 2,114 additions and 1,665 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
### Added
* Methods on `AcceptLanguage`: `ranked` and `preference`. [#2480]
* `AcceptEncoding` typed header. [#2482]
* `HttpResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468]
* `ServiceResponse::map_into_{left,right}_body` and `HttpResponse::map_into_boxed_body`. [#2468]

### Changed
* Rename `Accept::{mime_precedence => ranked}`. [#2480]
* Rename `Accept::{mime_preference => preference}`. [#2480]

### Fixed
* Accept wildcard `*` items in `AcceptLanguage`. [#2480]
* Re-exports `dev::{BodySize, MessageBody, SizedStream}`. They are exposed through the `body` module. [#2468]
* Typed headers containing lists that require one or more items now enforce this minimum. [#2482]

[#2468]: https://github.com/actix/actix-web/pull/2468
[#2480]: https://github.com/actix/actix-web/pull/2480
[#2482]: https://github.com/actix/actix-web/pull/2482

Expand Down
3 changes: 1 addition & 2 deletions actix-files/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::{
task::{Context, Poll},
};

use actix_web::error::Error;
use bytes::Bytes;
use actix_web::{error::Error, web::Bytes};
use futures_core::{ready, Stream};
use pin_project_lite::pin_project;

Expand Down
21 changes: 14 additions & 7 deletions actix-files/src/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use std::{
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

use actix_http::body::AnyBody;
use actix_service::{Service, ServiceFactory};
use actix_web::{
body::{self, BoxBody, SizedStream},
dev::{
AppService, BodyEncoding, HttpServiceFactory, ResourceDef, ServiceRequest,
ServiceResponse, SizedStream,
ServiceResponse,
},
http::{
header::{
Expand Down Expand Up @@ -113,6 +113,8 @@ pub(crate) use std::fs::File;
#[cfg(feature = "experimental-io-uring")]
pub(crate) use tokio_uring::fs::File;

use super::chunked;

impl NamedFile {
/// Creates an instance from a previously opened file.
///
Expand Down Expand Up @@ -394,7 +396,7 @@ impl NamedFile {
}

/// Creates an `HttpResponse` with file as a streaming body.
pub fn into_response(self, req: &HttpRequest) -> HttpResponse {
pub fn into_response(self, req: &HttpRequest) -> HttpResponse<BoxBody> {
if self.status_code != StatusCode::OK {
let mut res = HttpResponse::build(self.status_code);

Expand All @@ -416,7 +418,7 @@ impl NamedFile {
res.encoding(current_encoding);
}

let reader = super::chunked::new_chunked_read(self.md.len(), 0, self.file);
let reader = chunked::new_chunked_read(self.md.len(), 0, self.file);

return res.streaming(reader);
}
Expand Down Expand Up @@ -527,10 +529,13 @@ impl NamedFile {
if precondition_failed {
return resp.status(StatusCode::PRECONDITION_FAILED).finish();
} else if not_modified {
return resp.status(StatusCode::NOT_MODIFIED).body(AnyBody::None);
return resp
.status(StatusCode::NOT_MODIFIED)
.body(body::None::new())
.map_into_boxed_body();
}

let reader = super::chunked::new_chunked_read(length, offset, self.file);
let reader = chunked::new_chunked_read(length, offset, self.file);

if offset != 0 || length != self.md.len() {
resp.status(StatusCode::PARTIAL_CONTENT);
Expand Down Expand Up @@ -595,7 +600,9 @@ impl DerefMut for NamedFile {
}

impl Responder for NamedFile {
fn respond_to(self, req: &HttpRequest) -> HttpResponse {
type Body = BoxBody;

fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
self.into_response(req)
}
}
Expand Down
24 changes: 23 additions & 1 deletion actix-http/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@
## Unreleased - 2021-xx-xx
### Added
* Add timeout for canceling HTTP/2 server side connection handshake. Default to 5 seconds. [#2483]
* HTTP/2 handshake timeout can be configured with `ServiceConfig::client_timeout`. [#2483]
* HTTP/2 handshake timeout can be configured with `ServiceConfig::client_timeout`. [#2483]
* `Response::map_into_boxed_body`. [#2468]
* `body::EitherBody` enum. [#2468]
* `body::None` struct. [#2468]
* Impl `MessageBody` for `bytestring::ByteString`. [#2468]
* `impl Clone for ws::HandshakeError`. [#2468]

### Changed
* Rename `body::BoxBody::{from_body => new}`. [#2468]
* Body type for `Responses` returned from `Response::{new, ok, etc...}` is now `BoxBody`. [#2468]
* The `Error` associated type on `MessageBody` type now requires `impl Error` (or similar). [#2468]
* Error types using in service builders now require `Into<Response<BoxBody>>`. [#2468]
* `From` implementations on error types now return a `Response<BoxBody>`. [#2468]
* `ResponseBuilder::body(B)` now returns `Response<EitherBody<B>>`. [#2468]
* `ResponseBuilder::finish()` now returns `Response<EitherBody<()>>`. [#2468]

### Removed
* `ResponseBuilder::streaming`. [#2468]
* `impl Future` for `ResponseBuilder`. [#2468]
* Remove unnecessary `MessageBody` bound on types passed to `body::AnyBody::new`. [#2468]
* Move `body::AnyBody` to `awc`. Replaced with `EitherBody` and `BoxBody`. [#2468]

[#2483]: https://github.com/actix/actix-web/pull/2483
[#2468]: https://github.com/actix/actix-web/pull/2468


## 3.0.0-beta.14 - 2021-11-30
### Changed
Expand Down
8 changes: 5 additions & 3 deletions actix-http/examples/echo2.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::io;

use actix_http::{body::AnyBody, http::HeaderValue, http::StatusCode};
use actix_http::{Error, HttpService, Request, Response};
use actix_http::{
body::MessageBody, http::HeaderValue, http::StatusCode, Error, HttpService, Request,
Response,
};
use actix_server::Server;
use bytes::BytesMut;
use futures_util::StreamExt as _;

async fn handle_request(mut req: Request) -> Result<Response<AnyBody>, Error> {
async fn handle_request(mut req: Request) -> Result<Response<impl MessageBody>, Error> {
let mut body = BytesMut::new();
while let Some(item) = req.payload().next().await {
body.extend_from_slice(&item?)
Expand Down
Loading

0 comments on commit c7c02ef

Please sign in to comment.