Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions native/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use crate::json_text::escape_json_text;
pub fn wt_http_request(method: impl AsRef<str>, url: impl AsRef<str>, headers_json: impl AsRef<str>, body: impl AsRef<str>) -> String {
let client = match checked_client() {
Ok(client) => client,
Err(reason) => return format!("{{\"error\":\"client error: {}\"}}", reason),
Err(reason) => return format!("{{\"error\":\"client error: {}\"}}", escape_json_text(&reason.to_string())),
};
let Some(mut request) = request_for(&client, method.as_ref(), url.as_ref()) else {
return format!("{{\"error\":\"unsupported method: {}\"}}", method.as_ref());
return format!("{{\"error\":\"unsupported method: {}\"}}", escape_json_text(method.as_ref()));
};
request = with_headers(request, headers_json.as_ref());
if !body.as_ref().is_empty() { request = request.body(body.as_ref().to_string()); }
match request.send() {
Ok(response) => encoded_response(response),
Err(error) => format!("{{\"error\":\"request failed: {}\"}}", error),
Err(error) => format!("{{\"error\":\"request failed: {}\"}}", escape_json_text(&error.to_string())),
}
}

Expand Down Expand Up @@ -62,6 +62,6 @@ fn encoded_response(response: reqwest::blocking::Response) -> String {
let status = response.status().as_u16();
match response.text() {
Ok(text) => format!("{{\"status\":{},\"body\":\"{}\"}}", status, escape_json_text(&text)),
Err(error) => format!("{{\"error\":\"read error: {}\"}}", error),
Err(error) => format!("{{\"error\":\"read error: {}\"}}", escape_json_text(&error.to_string())),
}
}
9 changes: 9 additions & 0 deletions src/wasm_rt_test.almd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ test "wt_http_request: invalid URL" {
}
}

test "wt_http_request: unsupported method is valid JSON" {
// Rejected before a request is sent, even when the method contains JSON
// delimiters, control characters or Unicode.
let method = "BAD" + string.from_bytes([34, 92, 10, 13, 9, 0, 1, 8, 12, 31]) + "日"
let resp = wasm_rt.wt_http_request(method, "http://127.0.0.1", "{}", "")!
let parsed = json.parse(resp)!
assert_eq(json.get_string(parsed, "error") ?? "", "unsupported method: " + method)
}

// --- wt_exec_command ---

test "wt_exec_command: echo" {
Expand Down
Loading