Skip to content

Commit

Permalink
simplify adding headers and improve consistency for RemoteHttpPlugin (#…
Browse files Browse the repository at this point in the history
…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:
#15651 (comment))
- 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:
#15651 (comment))
- move example usage of Headers to `with_headers` method (quote:
#15651 (comment))

## Testing

- the same as I tested my previous pr
  • Loading branch information
spacemen0 authored Oct 6, 2024
1 parent 5a0bd23 commit e7c1c99
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions crates/bevy_remote/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_ecs::system::{Res, Resource};
use bevy_tasks::IoTaskPool;
use core::net::{IpAddr, Ipv4Addr};
use http_body_util::{BodyExt as _, Full};
pub use hyper::header::{HeaderName, HeaderValue};
use hyper::header::{HeaderName, HeaderValue};
use hyper::{
body::{Bytes, Incoming},
server::conn::http1,
Expand Down Expand Up @@ -55,9 +55,19 @@ impl Headers {
}
}

/// Add a key value pair to the `Headers` instance.
pub fn add(mut self, key: HeaderName, value: HeaderValue) -> Self {
self.headers.insert(key, value);
/// Insert a key value pair to the `Headers` instance.
pub fn insert(
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
}
}
Expand All @@ -77,24 +87,6 @@ impl Default for Headers {
/// - [`DEFAULT_ADDR`] : 127.0.0.1.
/// - [`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 {
/// The address that Bevy will bind to.
address: IpAddr,
Expand Down Expand Up @@ -137,6 +129,26 @@ impl RemoteHttpPlugin {
self
}
/// 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]
pub fn with_headers(mut self, headers: Headers) -> Self {
self.headers = headers;
Expand All @@ -149,13 +161,7 @@ impl RemoteHttpPlugin {
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 = self.headers.add(header_name, header_value);
self.headers = self.headers.insert(name, value);
self
}
}
Expand Down

0 comments on commit e7c1c99

Please sign in to comment.