@@ -98,6 +98,7 @@ pub const XMLHttpRequest = struct {
98
98
};
99
99
100
100
proto : XMLHttpRequestEventTarget ,
101
+ alloc : std.mem.Allocator ,
101
102
cli : Client ,
102
103
impl : YieldImpl ,
103
104
@@ -120,6 +121,7 @@ pub const XMLHttpRequest = struct {
120
121
121
122
pub fn constructor (alloc : std.mem.Allocator , loop : * Loop ) ! XMLHttpRequest {
122
123
return .{
124
+ .alloc = alloc ,
123
125
.proto = try XMLHttpRequestEventTarget .constructor (),
124
126
.headers = .{ .allocator = alloc , .owned = true },
125
127
.response_headers = .{ .allocator = alloc , .owned = true },
@@ -270,6 +272,23 @@ pub const XMLHttpRequest = struct {
270
272
271
273
self .state = LOADING ;
272
274
275
+ var buf : std .ArrayListUnmanaged (u8 ) = .{};
276
+
277
+ const reader = req .reader ();
278
+ var buffer : [1024 ]u8 = undefined ;
279
+ var ln = buffer .len ;
280
+ while (ln > 0 ) {
281
+ ln = reader .read (& buffer ) catch | e | {
282
+ buf .deinit (self .alloc );
283
+ return self .onerr (e );
284
+ };
285
+ buf .appendSlice (self .alloc , buffer [0.. ln ]) catch | e | {
286
+ buf .deinit (self .alloc );
287
+ return self .onerr (e );
288
+ };
289
+ }
290
+ self .response_bytes = buf .items ;
291
+
273
292
self .state = DONE ;
274
293
275
294
// TODO use events instead
@@ -289,14 +308,20 @@ pub const XMLHttpRequest = struct {
289
308
pub fn get_responseText (self : * XMLHttpRequest ) ! []const u8 {
290
309
if (self .state != LOADING and self .state != DONE ) return DOMError .InvalidState ;
291
310
if (self .response_type != .Empty and self .response_type != .Text ) return DOMError .InvalidState ;
311
+
292
312
return if (self .response_bytes ) | v | v else "" ;
293
313
}
294
314
295
- // the caller owns the string.
315
+ // The caller owns the string returned.
316
+ // TODO change the return type to express the string ownership and let
317
+ // jsruntime free the string once copied to v8.
318
+ // see https://github.com/lightpanda-io/jsruntime-lib/issues/195
296
319
pub fn _getAllResponseHeaders (self : * XMLHttpRequest , alloc : std.mem.Allocator ) ! []const u8 {
297
320
self .response_headers .sort ();
298
321
299
322
var buf : std .ArrayListUnmanaged (u8 ) = .{};
323
+ errdefer buf .deinit (alloc );
324
+
300
325
const w = buf .writer (alloc );
301
326
302
327
for (self .response_headers .list .items ) | entry | {
@@ -331,7 +356,8 @@ pub fn testExecFn(
331
356
// Each case executed waits for all loop callaback calls.
332
357
// So the url has been retrieved.
333
358
.{ .src = "nb" , .ex = "1" },
334
- .{ .src = "req.getAllResponseHeaders()" , .ex = "undefined" },
359
+ .{ .src = "req.getAllResponseHeaders().length > 1024" , .ex = "true" },
360
+ .{ .src = "req.responseText.length > 1024" , .ex = "true" },
335
361
};
336
362
try checkCases (js_env , & send );
337
363
}
0 commit comments