@@ -97,6 +97,7 @@ pub const XMLHttpRequest = struct {
97
97
JSON ,
98
98
};
99
99
100
+ alloc : std.mem.Allocator ,
100
101
proto : XMLHttpRequestEventTarget ,
101
102
cli : Client ,
102
103
impl : YieldImpl ,
@@ -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,6 +308,7 @@ 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
@@ -297,6 +317,8 @@ pub const XMLHttpRequest = struct {
297
317
self .response_headers .sort ();
298
318
299
319
var buf : std .ArrayListUnmanaged (u8 ) = .{};
320
+ errdefer buf .deinit (alloc );
321
+
300
322
const w = buf .writer (alloc );
301
323
302
324
for (self .response_headers .list .items ) | entry | {
@@ -332,6 +354,7 @@ pub fn testExecFn(
332
354
// So the url has been retrieved.
333
355
.{ .src = "nb" , .ex = "1" },
334
356
.{ .src = "req.getAllResponseHeaders()" , .ex = "undefined" },
357
+ .{ .src = "req.responseText" , .ex = "undefined" },
335
358
};
336
359
try checkCases (js_env , & send );
337
360
}
0 commit comments