Skip to content

Commit 7802222

Browse files
support setting a response body
1 parent b21dd64 commit 7802222

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/http/server.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,13 @@ impl HttpServer {
10801080

10811081
/// Send an HTTP response to an incoming HTTP request ([`HttpServerRequest::Http`]).
10821082
pub fn send_response(status: StatusCode, headers: Option<HashMap<String, String>>, body: Vec<u8>) {
1083+
// Check if there's a manual body override in the HTTP context
1084+
let final_body = crate::hyperapp::APP_HELPERS.with(|helpers| {
1085+
helpers.borrow().current_http_context.as_ref()
1086+
.and_then(|ctx| ctx.response_body.clone())
1087+
.unwrap_or(body) // Use override if present, otherwise use the parameter
1088+
});
1089+
10831090
KiResponse::new()
10841091
.body(
10851092
serde_json::to_vec(&HttpResponse {
@@ -1088,7 +1095,7 @@ pub fn send_response(status: StatusCode, headers: Option<HashMap<String, String>
10881095
})
10891096
.unwrap(),
10901097
)
1091-
.blob_bytes(body)
1098+
.blob_bytes(final_body)
10921099
.send()
10931100
.unwrap()
10941101
}

src/hyperapp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct HttpRequestContext {
3838
pub request: IncomingHttpRequest,
3939
pub response_headers: HashMap<String, String>,
4040
pub response_status: http::StatusCode,
41+
pub response_body: Option<Vec<u8>>,
4142
}
4243

4344
pub struct AppContext {
@@ -105,6 +106,15 @@ pub fn set_response_status(status: http::StatusCode) {
105106
})
106107
}
107108

109+
// Set the HTTP response body directly (bypasses Result serialization)
110+
pub fn set_response_body(body: Vec<u8>) {
111+
APP_HELPERS.with(|helpers| {
112+
if let Some(ctx) = &mut helpers.borrow_mut().current_http_context {
113+
ctx.response_body = Some(body);
114+
}
115+
})
116+
}
117+
108118
pub fn clear_http_request_context() {
109119
APP_HELPERS.with(|helpers| {
110120
helpers.borrow_mut().current_http_context = None;

0 commit comments

Comments
 (0)