Compare commits

...

7 Commits

Author SHA1 Message Date
vinibuzzacaro ac880b16df
Merge 9ea97a676a into fe890a73b2 2025-11-17 10:51:10 +00:00
dependabot[bot] fe890a73b2
build(deps): bump taiki-e/install-action from 2.62.49 to 2.62.51 (#3831)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-11-17 10:51:05 +00:00
Vinicius Buzzacaro 9ea97a676a Content type is now being compared ignoring case 2025-11-10 22:25:04 -03:00
Vinicius Buzzacaro 5037f9acd7 Updated documentation 2025-11-10 21:52:40 -03:00
Vinicius Buzzacaro 1c8c860e7d Minor tweaks to match new signature 2025-11-10 21:41:08 -03:00
Vinicius Buzzacaro a992de888e Updated the tests to match the new signature 2025-11-10 21:40:42 -03:00
Vinicius Buzzacaro 539e86a544 Change the return type of the method content_type from &str to Option<&str> 2025-11-10 21:40:11 -03:00
8 changed files with 28 additions and 20 deletions

View File

@ -49,7 +49,7 @@ jobs:
toolchain: ${{ matrix.version.version }} toolchain: ${{ matrix.version.version }}
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49 uses: taiki-e/install-action@0be4756f42223b67aa4b7df5effad59010cbf4b9 # v2.62.51
with: with:
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
@ -83,7 +83,7 @@ jobs:
uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2 uses: actions-rust-lang/setup-rust-toolchain@1780873c7b576612439a134613cc4cc74ce5538c # v1.15.2
- name: Install just, cargo-hack - name: Install just, cargo-hack
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49 uses: taiki-e/install-action@0be4756f42223b67aa4b7df5effad59010cbf4b9 # v2.62.51
with: with:
tool: just,cargo-hack tool: just,cargo-hack

View File

@ -64,7 +64,7 @@ jobs:
toolchain: ${{ matrix.version.version }} toolchain: ${{ matrix.version.version }}
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean - name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49 uses: taiki-e/install-action@0be4756f42223b67aa4b7df5effad59010cbf4b9 # v2.62.51
with: with:
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
@ -117,7 +117,7 @@ jobs:
toolchain: nightly toolchain: nightly
- name: Install just - name: Install just
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49 uses: taiki-e/install-action@0be4756f42223b67aa4b7df5effad59010cbf4b9 # v2.62.51
with: with:
tool: just tool: just

View File

@ -24,7 +24,7 @@ jobs:
components: llvm-tools components: llvm-tools
- name: Install just, cargo-llvm-cov, cargo-nextest - name: Install just, cargo-llvm-cov, cargo-nextest
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49 uses: taiki-e/install-action@0be4756f42223b67aa4b7df5effad59010cbf4b9 # v2.62.51
with: with:
tool: just,cargo-llvm-cov,cargo-nextest tool: just,cargo-llvm-cov,cargo-nextest

View File

@ -77,7 +77,7 @@ jobs:
toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }} toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
- name: Install just - name: Install just
uses: taiki-e/install-action@44c6d64aa62cd779e873306675c7a58e86d6d532 # v2.62.49 uses: taiki-e/install-action@0be4756f42223b67aa4b7df5effad59010cbf4b9 # v2.62.51
with: with:
tool: just tool: just

View File

@ -2,6 +2,10 @@
## Unreleased ## Unreleased
## 3.11.3
- Update `HttpMessage`s `content_type` method to return `Option<&str>` instead of `&str`.
## 3.11.2 ## 3.11.2
- Properly wake Payload receivers when feeding errors or EOF. - Properly wake Payload receivers when feeding errors or EOF.

View File

@ -46,13 +46,11 @@ pub trait HttpMessage: Sized {
/// Read the request content type. If request did not contain a *Content-Type* header, an empty /// Read the request content type. If request did not contain a *Content-Type* header, an empty
/// string is returned. /// string is returned.
fn content_type(&self) -> &str { fn content_type(&self) -> Option<&str> {
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) { self.headers()
if let Ok(content_type) = content_type.to_str() { .get(header::CONTENT_TYPE)
return content_type.split(';').next().unwrap().trim(); .and_then(|ct| ct.to_str().ok())
} .map(|ct| ct.split(';').next().unwrap().trim())
}
""
} }
/// Get content type encoding. /// Get content type encoding.
@ -142,13 +140,13 @@ mod tests {
let req = TestRequest::default() let req = TestRequest::default()
.insert_header(("content-type", "text/plain")) .insert_header(("content-type", "text/plain"))
.finish(); .finish();
assert_eq!(req.content_type(), "text/plain"); assert_eq!(req.content_type(), Some("text/plain"));
let req = TestRequest::default() let req = TestRequest::default()
.insert_header(("content-type", "application/json; charset=utf-8")) .insert_header(("content-type", "application/json; charset=utf-8"))
.finish(); .finish();
assert_eq!(req.content_type(), "application/json"); assert_eq!(req.content_type(), Some("application/json"));
let req = TestRequest::default().finish(); let req = TestRequest::default().finish();
assert_eq!(req.content_type(), ""); assert_eq!(req.content_type(), None);
} }
#[test] #[test]

View File

@ -505,7 +505,10 @@ mod tests {
.set_form(&payload) .set_form(&payload)
.to_request(); .to_request();
assert_eq!(req.content_type(), "application/x-www-form-urlencoded"); assert_eq!(
req.content_type().unwrap(),
"application/x-www-form-urlencoded"
);
let result: Person = call_and_read_body_json(&app, req).await; let result: Person = call_and_read_body_json(&app, req).await;
assert_eq!(&result.id, "12345"); assert_eq!(&result.id, "12345");
@ -549,7 +552,7 @@ mod tests {
.set_json(&payload) .set_json(&payload)
.to_request(); .to_request();
assert_eq!(req.content_type(), "application/json"); assert_eq!(req.content_type(), Some("application/json"));
let result: Person = call_and_read_body_json(&app, req).await; let result: Person = call_and_read_body_json(&app, req).await;
assert_eq!(&result.id, "12345"); assert_eq!(&result.id, "12345");

View File

@ -289,14 +289,17 @@ impl<T> UrlEncoded<T> {
/// Create a new future to decode a URL encoded request payload. /// Create a new future to decode a URL encoded request payload.
pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self { pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
// check content type // check content type
if req.content_type().to_lowercase() != "application/x-www-form-urlencoded" { let encoding_error = req
.content_type()
.map(|ct| !ct.eq_ignore_ascii_case("application/x-www-form-urlencoded"))
.unwrap_or(true);
if encoding_error {
return Self::err(UrlencodedError::ContentType); return Self::err(UrlencodedError::ContentType);
} }
let encoding = match req.encoding() { let encoding = match req.encoding() {
Ok(enc) => enc, Ok(enc) => enc,
Err(_) => return Self::err(UrlencodedError::ContentType), Err(_) => return Self::err(UrlencodedError::ContentType),
}; };
let mut len = None; let mut len = None;
if let Some(l) = req.headers().get(&CONTENT_LENGTH) { if let Some(l) = req.headers().get(&CONTENT_LENGTH) {
if let Ok(s) = l.to_str() { if let Ok(s) = l.to_str() {