Skip to content

Commit 91cc5ae

Browse files
authored
Merge pull request #170 from hyperware-ai/set-response-body
Set response body
2 parents d3bb304 + 1b2d5d1 commit 91cc5ae

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/hyperapp.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@ pub fn get_http_method() -> Option<String> {
8383
})
8484
}
8585

86+
// Get a specific header from the current HTTP request
87+
// Returns None if not in HTTP context or header doesn't exist
88+
pub fn get_request_header(name: &str) -> Option<String> {
89+
APP_HELPERS.with(|helpers| {
90+
helpers
91+
.borrow()
92+
.current_http_context
93+
.as_ref()
94+
.and_then(|ctx| {
95+
// Convert string to HeaderName using process_lib's re-exported type
96+
let header_name = http::HeaderName::from_bytes(name.as_bytes()).ok()?;
97+
ctx.request
98+
.headers()
99+
.get(&header_name)
100+
.and_then(|value| value.to_str().ok())
101+
.map(|s| s.to_string())
102+
})
103+
})
104+
}
105+
106+
// Get the full URL of the current HTTP request
107+
// Returns None if not in an HTTP context
108+
pub fn get_request_url() -> Option<String> {
109+
APP_HELPERS.with(|helpers| {
110+
helpers
111+
.borrow()
112+
.current_http_context
113+
.as_ref()
114+
.and_then(|ctx| ctx.request.url().ok())
115+
.map(|url| url.to_string())
116+
})
117+
}
118+
86119
// Set response headers that will be included in the HTTP response
87120
pub fn set_response_headers(headers: HashMap<String, String>) {
88121
APP_HELPERS.with(|helpers| {
@@ -128,22 +161,16 @@ pub fn source() -> Address {
128161
})
129162
}
130163

131-
/// Get query parameters from the current HTTP request path
132-
/// Returns None if not in an HTTP context or no query parameters present
164+
/// Get the pre-parsed query parameters from the current HTTP request
165+
/// Returns None if not in an HTTP context
166+
/// This accesses the query_params field that Hyperware already parsed (includes URL decoding)
133167
pub fn get_query_params() -> Option<HashMap<String, String>> {
134-
get_path().map(|path| {
135-
let mut params = HashMap::new();
136-
if let Some(query_start) = path.find('?') {
137-
let query = &path[query_start + 1..];
138-
for pair in query.split('&') {
139-
if let Some(eq_pos) = pair.find('=') {
140-
let key = pair[..eq_pos].to_string();
141-
let value = pair[eq_pos + 1..].to_string();
142-
params.insert(key, value);
143-
}
144-
}
145-
}
146-
params
168+
APP_HELPERS.with(|helpers| {
169+
helpers
170+
.borrow()
171+
.current_http_context
172+
.as_ref()
173+
.map(|ctx| ctx.request.query_params().clone())
147174
})
148175
}
149176

0 commit comments

Comments
 (0)