simplify adding headers and improve consistency for RemoteHttpPlugin (#15680)
# Objective - a follow up pr for #15651 ## Solution - rename add to insert cuz insert make more sense when using a HashMap and new value overrides previous value based on key (quote: https://github.com/bevyengine/bevy/pull/15651#discussion_r1788851778) - use `TryInto<>` for add(insert) as well when constructing `Headers`. Doing so user won't need to interact with hyper APIs, and `with_headers` will align better with `with_header` (quote: https://github.com/bevyengine/bevy/pull/15651#discussion_r1788687251) - move example usage of Headers to `with_headers` method (quote: https://github.com/bevyengine/bevy/pull/15651#discussion_r1788989500) ## Testing - the same as I tested my previous pr
This commit is contained in:
parent
5a0bd23106
commit
e7c1c997ac
@ -17,7 +17,7 @@ use bevy_ecs::system::{Res, Resource};
|
|||||||
use bevy_tasks::IoTaskPool;
|
use bevy_tasks::IoTaskPool;
|
||||||
use core::net::{IpAddr, Ipv4Addr};
|
use core::net::{IpAddr, Ipv4Addr};
|
||||||
use http_body_util::{BodyExt as _, Full};
|
use http_body_util::{BodyExt as _, Full};
|
||||||
pub use hyper::header::{HeaderName, HeaderValue};
|
use hyper::header::{HeaderName, HeaderValue};
|
||||||
use hyper::{
|
use hyper::{
|
||||||
body::{Bytes, Incoming},
|
body::{Bytes, Incoming},
|
||||||
server::conn::http1,
|
server::conn::http1,
|
||||||
@ -55,9 +55,19 @@ impl Headers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a key value pair to the `Headers` instance.
|
/// Insert a key value pair to the `Headers` instance.
|
||||||
pub fn add(mut self, key: HeaderName, value: HeaderValue) -> Self {
|
pub fn insert(
|
||||||
self.headers.insert(key, value);
|
mut self,
|
||||||
|
name: impl TryInto<HeaderName>,
|
||||||
|
value: impl TryInto<HeaderValue>,
|
||||||
|
) -> Self {
|
||||||
|
let Ok(header_name) = name.try_into() else {
|
||||||
|
panic!("Invalid header name")
|
||||||
|
};
|
||||||
|
let Ok(header_value) = value.try_into() else {
|
||||||
|
panic!("Invalid header value")
|
||||||
|
};
|
||||||
|
self.headers.insert(header_name, header_value);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,24 +87,6 @@ impl Default for Headers {
|
|||||||
/// - [`DEFAULT_ADDR`] : 127.0.0.1.
|
/// - [`DEFAULT_ADDR`] : 127.0.0.1.
|
||||||
/// - [`DEFAULT_PORT`] : 15702.
|
/// - [`DEFAULT_PORT`] : 15702.
|
||||||
///
|
///
|
||||||
/// /// # Example
|
|
||||||
///
|
|
||||||
/// ```ignore
|
|
||||||
///
|
|
||||||
/// // Create CORS headers
|
|
||||||
/// let cors_headers = Headers::new()
|
|
||||||
/// .add(HeaderName::from_static("Access-Control-Allow-Origin"), HeaderValue::from_static("*"))
|
|
||||||
/// .add(HeaderName::from_static("Access-Control-Allow-Headers"), HeaderValue::from_static("Content-Type, Authorization"));
|
|
||||||
///
|
|
||||||
/// // Create the Bevy app and add the RemoteHttpPlugin with CORS headers
|
|
||||||
/// fn main() {
|
|
||||||
/// App::new()
|
|
||||||
/// .add_plugins(DefaultPlugins)
|
|
||||||
/// .add_plugins(RemoteHttpPlugin::default()
|
|
||||||
/// .with_headers(cors_headers))
|
|
||||||
/// .run();
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub struct RemoteHttpPlugin {
|
pub struct RemoteHttpPlugin {
|
||||||
/// The address that Bevy will bind to.
|
/// The address that Bevy will bind to.
|
||||||
address: IpAddr,
|
address: IpAddr,
|
||||||
@ -137,6 +129,26 @@ impl RemoteHttpPlugin {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
/// Set the extra headers that the response will include.
|
/// Set the extra headers that the response will include.
|
||||||
|
///
|
||||||
|
/// ////// /// # Example
|
||||||
|
///
|
||||||
|
/// ```ignore
|
||||||
|
///
|
||||||
|
/// // Create CORS headers
|
||||||
|
/// let cors_headers = Headers::new()
|
||||||
|
/// .insert("Access-Control-Allow-Origin", "*")
|
||||||
|
/// .insert("Access-Control-Allow-Headers", "Content-Type");
|
||||||
|
///
|
||||||
|
/// // Create the Bevy app and add the RemoteHttpPlugin with CORS headers
|
||||||
|
/// fn main() {
|
||||||
|
/// App::new()
|
||||||
|
/// .add_plugins(DefaultPlugins)
|
||||||
|
/// .add_plugins(RemotePlugin::default())
|
||||||
|
/// .add_plugins(RemoteHttpPlugin::default()
|
||||||
|
/// .with_headers(cors_headers))
|
||||||
|
/// .run();
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn with_headers(mut self, headers: Headers) -> Self {
|
pub fn with_headers(mut self, headers: Headers) -> Self {
|
||||||
self.headers = headers;
|
self.headers = headers;
|
||||||
@ -149,13 +161,7 @@ impl RemoteHttpPlugin {
|
|||||||
name: impl TryInto<HeaderName>,
|
name: impl TryInto<HeaderName>,
|
||||||
value: impl TryInto<HeaderValue>,
|
value: impl TryInto<HeaderValue>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let Ok(header_name) = name.try_into() else {
|
self.headers = self.headers.insert(name, value);
|
||||||
panic!("Invalid header name")
|
|
||||||
};
|
|
||||||
let Ok(header_value) = value.try_into() else {
|
|
||||||
panic!("Invalid header value")
|
|
||||||
};
|
|
||||||
self.headers = self.headers.add(header_name, header_value);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user