Skip to content

Commit

Permalink
remove BodyEncoding trait (actix#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede authored Jan 3, 2022
1 parent 19a46e3 commit 0bc4ae9
Show file tree
Hide file tree
Showing 20 changed files with 361 additions and 409 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/clippy-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
components: rustfmt
- name: Check with rustfmt
uses: actions-rs/cargo@v1
Expand All @@ -30,10 +31,18 @@ jobs:
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
components: clippy
override: true

- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with: { command: generate-lockfile }
- name: Cache Dependencies
uses: Swatinem/[email protected]

- name: Check with Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --workspace --all-features --tests
args: --workspace --tests --examples --all-features
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

### Removed
- `Compress::new`; restricting compression algorithm is done through feature flags. [#2501]
- `BodyEncoding` trait; signalling content encoding is now only done via the `Content-Encoding` header. [#2565]

[#2501]: https://github.com/actix/actix-web/pull/2501
[#2565]: https://github.com/actix/actix-web/pull/2565


## 4.0.0-beta.18 - 2021-12-29
Expand Down
3 changes: 2 additions & 1 deletion actix-files/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ mod tests {
.to_request();
let res = test::call_service(&srv, request).await;
assert_eq!(res.status(), StatusCode::OK);
assert!(!res.headers().contains_key(header::CONTENT_ENCODING));
assert!(res.headers().contains_key(header::CONTENT_ENCODING));
assert!(!test::read_body(res).await.is_empty());
}

#[actix_rt::test]
Expand Down
67 changes: 34 additions & 33 deletions actix-files/src/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ use std::{
use actix_service::{Service, ServiceFactory};
use actix_web::{
body::{self, BoxBody, SizedStream},
dev::{
AppService, BodyEncoding, HttpServiceFactory, ResourceDef, ServiceRequest,
ServiceResponse,
},
dev::{AppService, HttpServiceFactory, ResourceDef, ServiceRequest, ServiceResponse},
http::{
header::{
self, Charset, ContentDisposition, ContentEncoding, DispositionParam,
DispositionType, ExtendedValue,
DispositionType, ExtendedValue, HeaderValue,
},
StatusCode,
},
Expand Down Expand Up @@ -224,14 +221,14 @@ impl NamedFile {
})
}

#[cfg(not(feature = "experimental-io-uring"))]
/// Attempts to open a file in read-only mode.
///
/// # Examples
/// ```
/// use actix_files::NamedFile;
/// let file = NamedFile::open("foo.txt");
/// ```
#[cfg(not(feature = "experimental-io-uring"))]
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<NamedFile> {
let file = File::open(&path)?;
Self::from_file(file, path)
Expand Down Expand Up @@ -295,23 +292,21 @@ impl NamedFile {
self
}

/// Set the MIME Content-Type for serving this file. By default
/// the Content-Type is inferred from the filename extension.
/// Set the MIME Content-Type for serving this file. By default the Content-Type is inferred
/// from the filename extension.
#[inline]
pub fn set_content_type(mut self, mime_type: mime::Mime) -> Self {
self.content_type = mime_type;
self
}

/// Set the Content-Disposition for serving this file. This allows
/// changing the inline/attachment disposition as well as the filename
/// sent to the peer.
/// Set the Content-Disposition for serving this file. This allows changing the
/// `inline/attachment` disposition as well as the filename sent to the peer.
///
/// By default the disposition is `inline` for `text/*`, `image/*`, `video/*` and
/// `application/{javascript, json, wasm}` mime types, and `attachment` otherwise,
/// and the filename is taken from the path provided in the `open` method
/// after converting it to UTF-8 using.
/// [`std::ffi::OsStr::to_string_lossy`]
/// `application/{javascript, json, wasm}` mime types, and `attachment` otherwise, and the
/// filename is taken from the path provided in the `open` method after converting it to UTF-8
/// (using `to_string_lossy`).
#[inline]
pub fn set_content_disposition(mut self, cd: header::ContentDisposition) -> Self {
self.content_disposition = cd;
Expand All @@ -337,7 +332,7 @@ impl NamedFile {
self
}

/// Specifies whether to use ETag or not.
/// Specifies whether to return `ETag` header in response.
///
/// Default is true.
#[inline]
Expand All @@ -346,7 +341,7 @@ impl NamedFile {
self
}

/// Specifies whether to use Last-Modified or not.
/// Specifies whether to return `Last-Modified` header in response.
///
/// Default is true.
#[inline]
Expand All @@ -364,7 +359,7 @@ impl NamedFile {
self
}

/// Creates a etag in a format is similar to Apache's.
/// Creates an `ETag` in a format is similar to Apache's.
pub(crate) fn etag(&self) -> Option<header::EntityTag> {
self.modified.as_ref().map(|mtime| {
let ino = {
Expand All @@ -386,7 +381,7 @@ impl NamedFile {
.duration_since(UNIX_EPOCH)
.expect("modification time must be after epoch");

header::EntityTag::strong(format!(
header::EntityTag::new_strong(format!(
"{:x}:{:x}:{:x}:{:x}",
ino,
self.md.len(),
Expand All @@ -405,12 +400,13 @@ impl NamedFile {
if self.status_code != StatusCode::OK {
let mut res = HttpResponse::build(self.status_code);

if self.flags.contains(Flags::PREFER_UTF8) {
let ct = equiv_utf8_text(self.content_type.clone());
res.insert_header((header::CONTENT_TYPE, ct.to_string()));
let ct = if self.flags.contains(Flags::PREFER_UTF8) {
equiv_utf8_text(self.content_type.clone())
} else {
res.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
}
self.content_type
};

res.insert_header((header::CONTENT_TYPE, ct.to_string()));

if self.flags.contains(Flags::CONTENT_DISPOSITION) {
res.insert_header((
Expand All @@ -420,7 +416,7 @@ impl NamedFile {
}

if let Some(current_encoding) = self.encoding {
res.encode_with(current_encoding);
res.insert_header((header::CONTENT_ENCODING, current_encoding.as_str()));
}

let reader = chunked::new_chunked_read(self.md.len(), 0, self.file);
Expand Down Expand Up @@ -478,12 +474,13 @@ impl NamedFile {

let mut res = HttpResponse::build(self.status_code);

if self.flags.contains(Flags::PREFER_UTF8) {
let ct = equiv_utf8_text(self.content_type.clone());
res.insert_header((header::CONTENT_TYPE, ct.to_string()));
let ct = if self.flags.contains(Flags::PREFER_UTF8) {
equiv_utf8_text(self.content_type.clone())
} else {
res.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
}
self.content_type
};

res.insert_header((header::CONTENT_TYPE, ct.to_string()));

if self.flags.contains(Flags::CONTENT_DISPOSITION) {
res.insert_header((
Expand All @@ -492,9 +489,8 @@ impl NamedFile {
));
}

// default compressing
if let Some(current_encoding) = self.encoding {
res.encode_with(current_encoding);
res.insert_header((header::CONTENT_ENCODING, current_encoding.as_str()));
}

if let Some(lm) = last_modified {
Expand All @@ -517,7 +513,12 @@ impl NamedFile {
length = ranges[0].length;
offset = ranges[0].start;

res.encode_with(ContentEncoding::Identity);
// don't allow compression middleware to modify partial content
res.insert_header((
header::CONTENT_ENCODING,
HeaderValue::from_static("identity"),
));

res.insert_header((
header::CONTENT_RANGE,
format!("bytes {}-{}/{}", offset, offset + length - 1, self.md.len()),
Expand Down
2 changes: 2 additions & 0 deletions actix-http/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `QualityItem::min` semantics changed with `QualityItem::MIN`. [#2501]
- Rename `ContentEncoding::{Br => Brotli}`. [#2501]
- Minimum supported Rust version (MSRV) is now 1.54.
- Rename `header::EntityTag::{weak => new_weak, strong => new_strong}`. [#2565]

### Fixed
- `ContentEncoding::Identity` can now be parsed from a string. [#2501]
Expand All @@ -23,6 +24,7 @@
- `ContentEncoding::is_compression()`. [#2501]

[#2501]: https://github.com/actix/actix-web/pull/2501
[#2565]: https://github.com/actix/actix-web/pull/2565


## 3.0.0-beta.17 - 2021-12-27
Expand Down
2 changes: 1 addition & 1 deletion actix-http/src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ahash::AHashMap;
use http::header::{HeaderName, HeaderValue};
use smallvec::{smallvec, SmallVec};

use crate::header::AsHeaderName;
use super::AsHeaderName;

/// A multi-map of HTTP headers.
///
Expand Down
4 changes: 2 additions & 2 deletions actix-http/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ pub use self::utils::{

/// An interface for types that already represent a valid header.
pub trait Header: TryIntoHeaderValue {
/// Returns the name of the header field
/// Returns the name of the header field.
fn name() -> HeaderName;

/// Parse a header
/// Parse the header from a HTTP message.
fn parse<M: HttpMessage>(msg: &M) -> Result<Self, ParseError>;
}

Expand Down
1 change: 1 addition & 0 deletions actix-http/src/header/shared/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl ContentEncoding {
}

impl Default for ContentEncoding {
#[inline]
fn default() -> Self {
Self::Identity
}
Expand Down
2 changes: 2 additions & 0 deletions awc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ actix-utils = "3.0.0"
actix-web = { version = "4.0.0-beta.18", features = ["openssl"] }

brotli2 = "0.3.2"
const-str = "0.3"
env_logger = "0.9"
flate2 = "1.0.13"
futures-util = { version = "0.3.7", default-features = false }
static_assertions = "1.1"
rcgen = "0.8"
rustls-pemfile = "0.2"
zstd = "0.9"

[[example]]
name = "client"
Expand Down
Loading

0 comments on commit 0bc4ae9

Please sign in to comment.