Skip to content

Commit d295c3f

Browse files
committed
rsscript: expose http response bytes
1 parent c3e1af2 commit d295c3f

5 files changed

Lines changed: 21 additions & 0 deletions

File tree

crates/rsscript/src/reg_vm/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,7 @@ enum RegIntrinsic {
21462146
HttpRequestWithHeader,
21472147
HttpRequestWithRetry,
21482148
HttpRequestWithTimeout,
2149+
HttpResponseBytes,
21492150
HttpResponseIsSuccess,
21502151
HttpResponseLines,
21512152
HttpResponseStatus,
@@ -4248,6 +4249,7 @@ impl RegLowerer<'_> {
42484249
("HttpRequest", "with_header") => RegIntrinsic::HttpRequestWithHeader,
42494250
("HttpRequest", "with_retry") => RegIntrinsic::HttpRequestWithRetry,
42504251
("HttpRequest", "with_timeout") => RegIntrinsic::HttpRequestWithTimeout,
4252+
("HttpResponse", "bytes") => RegIntrinsic::HttpResponseBytes,
42514253
("HttpResponse", "is_success") => RegIntrinsic::HttpResponseIsSuccess,
42524254
("HttpResponse", "lines") => RegIntrinsic::HttpResponseLines,
42534255
("HttpResponse", "status") => RegIntrinsic::HttpResponseStatus,
@@ -9882,6 +9884,12 @@ impl RegVm {
98829884
request.timeout_ms = timeout_ms;
98839885
Ok(request.to_value())
98849886
}
9887+
RegIntrinsic::HttpResponseBytes => {
9888+
let response = intrinsic_arg(&self.stack, base, args, 0)?;
9889+
let text = read_field_ref(response, "body")?;
9890+
let text = expect_string_ref(&text)?;
9891+
Ok(VmValue::Bytes(Rc::new(text.as_bytes().to_vec())))
9892+
}
98859893
RegIntrinsic::HttpResponseIsSuccess => {
98869894
let response = intrinsic_arg(&self.stack, base, args, 0)?;
98879895
let status = expect_int_ref(&read_field_ref(response, "status")?)?;

crates/rsscript/src/runtime_abi.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@ const RUNTIME_INTRINSICS: &[RuntimeIntrinsic] = &[
734734
"is_success",
735735
"rsscript_runtime::http_response_is_success",
736736
),
737+
runtime_intrinsic(
738+
"HttpResponse",
739+
"bytes",
740+
"rsscript_runtime::http_response_bytes",
741+
),
737742
runtime_intrinsic(
738743
"HttpResponse",
739744
"lines",

crates/rsscript/tests/vm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,6 +4232,7 @@ async fn main() -> Result<Unit, String> {
42324232
match Http.get(url: read url) {
42334233
Ok(response) => {
42344234
Log.write(message: read HttpResponse.text(response: read response))
4235+
Log.write(message: read String.from_int(value: Bytes.len(value: read HttpResponse.bytes(response: read response))))
42354236
}
42364237
Err(error) => {
42374238
Log.write(message: read HttpError.message(error: read error))

crates/runtime/src/domain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,10 @@ pub fn http_response_text(response: &Response) -> String {
745745
response.body.clone()
746746
}
747747

748+
pub fn http_response_bytes(response: &Response) -> Vec<u8> {
749+
response.body.as_bytes().to_vec()
750+
}
751+
748752
pub fn http_response_lines(response: &Response) -> Vec<String> {
749753
response.body.lines().map(str::to_string).collect()
750754
}

stdlib/http/client.rssi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub native fn HttpResponse.status(response: read HttpResponse) -> Int
4242
pub native fn HttpResponse.text(response: read HttpResponse) -> fresh String
4343
effects(native)
4444

45+
pub native fn HttpResponse.bytes(response: read HttpResponse) -> fresh Bytes
46+
effects(native)
47+
4548
pub native fn HttpResponse.lines(response: read HttpResponse) -> fresh List<String>
4649
effects(native)
4750

0 commit comments

Comments
 (0)