From 9b6a93d72c45a494cada48034f726b027d68843d Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Sat, 17 Aug 2024 23:09:59 +0100 Subject: [PATCH 1/8] TryIntoHeaderValue for Uri --- actix-http/src/header/into_value.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/actix-http/src/header/into_value.rs b/actix-http/src/header/into_value.rs index 253900633d0..688580420a0 100644 --- a/actix-http/src/header/into_value.rs +++ b/actix-http/src/header/into_value.rs @@ -1,7 +1,7 @@ //! [`TryIntoHeaderValue`] trait and implementations. use bytes::Bytes; -use http::{header::InvalidHeaderValue, Error as HttpError, HeaderValue}; +use http::{header::InvalidHeaderValue, Error as HttpError, HeaderValue, Uri}; use mime::Mime; /// An interface for types that can be converted into a [`HeaderValue`]. @@ -129,3 +129,12 @@ impl TryIntoHeaderValue for Mime { HeaderValue::from_str(self.as_ref()) } } + +impl TryIntoHeaderValue for Uri { + type Error = InvalidHeaderValue; + + #[inline] + fn try_into_value(self) -> Result { + HeaderValue::from_str(&self.to_string()) + } +} From 342242a0e7c790c77e51e159d6d6b6dc66dabb00 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Sat, 17 Aug 2024 23:11:42 +0100 Subject: [PATCH 2/8] add content-location typed header --- actix-web/CHANGES.md | 1 + actix-web/src/http/header/content_location.rs | 36 +++++++++++++++++++ actix-web/src/http/header/mod.rs | 3 ++ 3 files changed, 40 insertions(+) create mode 100644 actix-web/src/http/header/content_location.rs diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 36e8b62dd42..1bed42976ba 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -3,6 +3,7 @@ ## Unreleased - Minimum supported Rust version (MSRV) is now 1.75. +- Add `http::header::ContentLocation` typed header. ## 4.9.0 diff --git a/actix-web/src/http/header/content_location.rs b/actix-web/src/http/header/content_location.rs new file mode 100644 index 00000000000..e36ce3b71d0 --- /dev/null +++ b/actix-web/src/http/header/content_location.rs @@ -0,0 +1,36 @@ +use super::{Uri, CONTENT_LOCATION}; + +crate::http::header::common_header! { + /// `Content-Location` header, defined + /// in [RFC 7231 §3.1.4.2](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.4.2) + /// + /// The "Content-Location" header field references a URI that can be used + /// as an identifier for a specific resource corresponding to the + /// representation in this message's payload. + /// + /// # ABNF + /// ```plain + /// Content-Location = absolute-URI / partial-URI + /// ``` + /// + /// # Example Values + /// * `http://www.example.org/hypertext/Overview.html` + /// + /// # Examples + /// + /// ``` + /// use actix_web::HttpResponse; + /// use actix_http::Uri; + /// use actix_web::http::header::ContentLocation; + /// + /// let mut builder = HttpResponse::Created(); + /// builder.insert_header( + /// ContentLocation("http://www.example.org".parse::().unwrap()) + /// ); + /// ``` + (ContentLocation, CONTENT_LOCATION) => [Uri] + + test_parse_and_format { + crate::http::header::common_header_test!(test1, [b"http://www.example.org/hypertext/Overview.html"]); + } +} diff --git a/actix-web/src/http/header/mod.rs b/actix-web/src/http/header/mod.rs index 51ac4fcfd90..756880acc85 100644 --- a/actix-web/src/http/header/mod.rs +++ b/actix-web/src/http/header/mod.rs @@ -14,6 +14,7 @@ use std::fmt; // - the few typed headers from actix-http // - header parsing utils pub use actix_http::header::*; +pub use actix_http::Uri; use bytes::{Bytes, BytesMut}; mod accept; @@ -25,6 +26,7 @@ mod cache_control; mod content_disposition; mod content_language; mod content_length; +mod content_location; mod content_range; mod content_type; mod date; @@ -55,6 +57,7 @@ pub use self::{ content_disposition::{ContentDisposition, DispositionParam, DispositionType}, content_language::ContentLanguage, content_length::ContentLength, + content_location::ContentLocation, content_range::{ContentRange, ContentRangeSpec}, content_type::ContentType, date::Date, From f5d340878c5007e6cb341533bc3db6e69e7b33ad Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Sat, 17 Aug 2024 23:12:05 +0100 Subject: [PATCH 3/8] add location typed header --- actix-web/CHANGES.md | 1 + actix-web/src/http/header/location.rs | 37 +++++++++++++++++++++++++++ actix-web/src/http/header/mod.rs | 2 ++ 3 files changed, 40 insertions(+) create mode 100644 actix-web/src/http/header/location.rs diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index 1bed42976ba..a79f6a9c147 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -4,6 +4,7 @@ - Minimum supported Rust version (MSRV) is now 1.75. - Add `http::header::ContentLocation` typed header. +- Add `http::header::Location` typed header. ## 4.9.0 diff --git a/actix-web/src/http/header/location.rs b/actix-web/src/http/header/location.rs new file mode 100644 index 00000000000..97c7ea78a45 --- /dev/null +++ b/actix-web/src/http/header/location.rs @@ -0,0 +1,37 @@ +use super::{Uri, LOCATION}; + +crate::http::header::common_header! { + /// `Location` header, defined + /// in [RFC 7231 §7.1.2](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.2) + /// + /// The "Location" header field is used in some responses to refer to a + /// specific resource in relation to the response. The type of + /// relationship is defined by the combination of request method and + /// status code semantics. + /// + /// # ABNF + /// ```plain + /// Location = URI-reference + /// ``` + /// + /// # Example Values + /// * `http://www.example.org/hypertext/Overview.html` + /// + /// # Examples + /// + /// ``` + /// use actix_web::HttpResponse; + /// use actix_http::Uri; + /// use actix_web::http::header::Location; + /// + /// let mut builder = HttpResponse::Ok(); + /// builder.insert_header( + /// Location("http://www.example.org".parse::().unwrap()) + /// ); + /// ``` + (Location, LOCATION) => [Uri] + + test_parse_and_format { + crate::http::header::common_header_test!(test1, [b"http://www.example.org/hypertext/Overview.html"]); + } +} diff --git a/actix-web/src/http/header/mod.rs b/actix-web/src/http/header/mod.rs index 756880acc85..44e47e82520 100644 --- a/actix-web/src/http/header/mod.rs +++ b/actix-web/src/http/header/mod.rs @@ -40,6 +40,7 @@ mod if_none_match; mod if_range; mod if_unmodified_since; mod last_modified; +mod location; mod macros; mod preference; mod range; @@ -71,6 +72,7 @@ pub use self::{ if_range::IfRange, if_unmodified_since::IfUnmodifiedSince, last_modified::LastModified, + location::Location, preference::Preference, range::{ByteRangeSpec, Range}, }; From 3ff861eb296b4ff8276466d7d7c60b5583c25d43 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Sat, 17 Aug 2024 23:12:17 +0100 Subject: [PATCH 4/8] add referer typed header --- actix-web/CHANGES.md | 1 + actix-web/src/http/header/mod.rs | 2 ++ actix-web/src/http/header/referer.rs | 36 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 actix-web/src/http/header/referer.rs diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index a79f6a9c147..b1f1d7abcbb 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -5,6 +5,7 @@ - Minimum supported Rust version (MSRV) is now 1.75. - Add `http::header::ContentLocation` typed header. - Add `http::header::Location` typed header. +- Add `http::header::Referer` typed header. ## 4.9.0 diff --git a/actix-web/src/http/header/mod.rs b/actix-web/src/http/header/mod.rs index 44e47e82520..653c44ce53a 100644 --- a/actix-web/src/http/header/mod.rs +++ b/actix-web/src/http/header/mod.rs @@ -44,6 +44,7 @@ mod location; mod macros; mod preference; mod range; +mod referer; #[cfg(test)] pub(crate) use self::macros::common_header_test; @@ -75,6 +76,7 @@ pub use self::{ location::Location, preference::Preference, range::{ByteRangeSpec, Range}, + referer::Referer, }; /// Format writer ([`fmt::Write`]) for a [`BytesMut`]. diff --git a/actix-web/src/http/header/referer.rs b/actix-web/src/http/header/referer.rs new file mode 100644 index 00000000000..f81355324ce --- /dev/null +++ b/actix-web/src/http/header/referer.rs @@ -0,0 +1,36 @@ +use super::{Uri, REFERER}; + +crate::http::header::common_header! { + /// `Referer` header, defined + /// in [RFC 7231 §5.5.2](https://datatracker.ietf.org/doc/html/rfc7231#section-5.5.2) + /// + /// The "Referer" (sic) header field allows the user agent to specify a + /// URI reference for the resource from which the target URI was obtained + /// (i.e., the "referrer", though the field name is misspelled). + /// + /// # ABNF + /// ```plain + /// Referer = absolute-URI / partial-URI + /// ``` + /// + /// # Example Values + /// * `http://www.example.org/hypertext/Overview.html` + /// + /// # Examples + /// + /// ``` + /// use actix_web::HttpResponse; + /// use actix_http::Uri; + /// use actix_web::http::header::Referer; + /// + /// let mut builder = HttpResponse::Ok(); + /// builder.insert_header( + /// Referer("http://www.example.org".parse::().unwrap()) + /// ); + /// ``` + (Referer, REFERER) => [Uri] + + test_parse_and_format { + crate::http::header::common_header_test!(test1, [b"http://www.example.org/hypertext/Overview.html"]); + } +} From bbb7258e7b229eb36df72fc4ac5420e2207ed702 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Mon, 19 Aug 2024 14:07:08 +0100 Subject: [PATCH 5/8] add changelog entry for TryIntoHeaderValue Uri --- actix-web/CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/actix-web/CHANGES.md b/actix-web/CHANGES.md index b1f1d7abcbb..3751d11d65a 100644 --- a/actix-web/CHANGES.md +++ b/actix-web/CHANGES.md @@ -3,6 +3,7 @@ ## Unreleased - Minimum supported Rust version (MSRV) is now 1.75. +- Add `TryIntoHeaderValue` for `Uri` type. - Add `http::header::ContentLocation` typed header. - Add `http::header::Location` typed header. - Add `http::header::Referer` typed header. From b66866d2d10c714ad4661e6355dab35034760673 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Mon, 19 Aug 2024 14:07:22 +0100 Subject: [PATCH 6/8] update references and text to RFC 9110 --- actix-web/src/http/header/content_location.rs | 4 ++-- actix-web/src/http/header/location.rs | 8 ++++---- actix-web/src/http/header/referer.rs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/actix-web/src/http/header/content_location.rs b/actix-web/src/http/header/content_location.rs index e36ce3b71d0..9f0ea505f9f 100644 --- a/actix-web/src/http/header/content_location.rs +++ b/actix-web/src/http/header/content_location.rs @@ -2,11 +2,11 @@ use super::{Uri, CONTENT_LOCATION}; crate::http::header::common_header! { /// `Content-Location` header, defined - /// in [RFC 7231 §3.1.4.2](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.4.2) + /// in [RFC 9110 §8.7](https://datatracker.ietf.org/doc/html/rfc9110#section-8.7) /// /// The "Content-Location" header field references a URI that can be used /// as an identifier for a specific resource corresponding to the - /// representation in this message's payload. + /// representation in this message's content. /// /// # ABNF /// ```plain diff --git a/actix-web/src/http/header/location.rs b/actix-web/src/http/header/location.rs index 97c7ea78a45..3a759152ddd 100644 --- a/actix-web/src/http/header/location.rs +++ b/actix-web/src/http/header/location.rs @@ -2,12 +2,12 @@ use super::{Uri, LOCATION}; crate::http::header::common_header! { /// `Location` header, defined - /// in [RFC 7231 §7.1.2](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.2) + /// in [RFC 9110 §10.2.2](https://datatracker.ietf.org/doc/html/rfc9110#section-10.2.2) /// /// The "Location" header field is used in some responses to refer to a - /// specific resource in relation to the response. The type of - /// relationship is defined by the combination of request method and - /// status code semantics. + /// specific resource in relation to the response. The type of relationship + /// is defined by the combination of request method and status code + /// semantics. /// /// # ABNF /// ```plain diff --git a/actix-web/src/http/header/referer.rs b/actix-web/src/http/header/referer.rs index f81355324ce..503f7e4ffad 100644 --- a/actix-web/src/http/header/referer.rs +++ b/actix-web/src/http/header/referer.rs @@ -2,11 +2,11 @@ use super::{Uri, REFERER}; crate::http::header::common_header! { /// `Referer` header, defined - /// in [RFC 7231 §5.5.2](https://datatracker.ietf.org/doc/html/rfc7231#section-5.5.2) + /// in [RFC 9110 §10.1.3](https://datatracker.ietf.org/doc/html/rfc9110#section-10.1.3) /// - /// The "Referer" (sic) header field allows the user agent to specify a - /// URI reference for the resource from which the target URI was obtained - /// (i.e., the "referrer", though the field name is misspelled). + /// The "Referer" [sic] header field allows the user agent to specify a URI + /// reference for the resource from which the target URI was obtained (i.e., + /// the "referrer", though the field name is misspelled). /// /// # ABNF /// ```plain From f96a21f1faf26ede3e61b1eac431f7610c4a643d Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Mon, 19 Aug 2024 14:07:36 +0100 Subject: [PATCH 7/8] make Uri in http::header private --- actix-web/src/http/header/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web/src/http/header/mod.rs b/actix-web/src/http/header/mod.rs index 653c44ce53a..e0e25049dde 100644 --- a/actix-web/src/http/header/mod.rs +++ b/actix-web/src/http/header/mod.rs @@ -14,7 +14,7 @@ use std::fmt; // - the few typed headers from actix-http // - header parsing utils pub use actix_http::header::*; -pub use actix_http::Uri; +use actix_http::Uri; use bytes::{Bytes, BytesMut}; mod accept; From 4f8819d277a6a16faad056e8dd30030d3705beca Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Wed, 21 Aug 2024 00:18:09 +0100 Subject: [PATCH 8/8] use parens to appease linter --- actix-web/src/http/header/referer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web/src/http/header/referer.rs b/actix-web/src/http/header/referer.rs index 503f7e4ffad..0087b10fd78 100644 --- a/actix-web/src/http/header/referer.rs +++ b/actix-web/src/http/header/referer.rs @@ -4,7 +4,7 @@ crate::http::header::common_header! { /// `Referer` header, defined /// in [RFC 9110 §10.1.3](https://datatracker.ietf.org/doc/html/rfc9110#section-10.1.3) /// - /// The "Referer" [sic] header field allows the user agent to specify a URI + /// The "Referer" (sic) header field allows the user agent to specify a URI /// reference for the resource from which the target URI was obtained (i.e., /// the "referrer", though the field name is misspelled). ///