Skip to content

Commit

Permalink
fix(http): Fixed http client + added collector for Request
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Feb 29, 2024
1 parent 86a0e64 commit c0f543b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
40 changes: 37 additions & 3 deletions src/lib/buzz_http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub export fn HttpClientDeinit(ctx: *api.NativeCtx) c_int {
const client = @as(*http.Client, @ptrCast(@alignCast(@as(*anyopaque, @ptrFromInt(userdata)))));

client.deinit();
api.VM.allocator.destroy(client);

return 0;
}
Expand Down Expand Up @@ -66,7 +67,7 @@ pub export fn HttpClientSend(ctx: *api.NativeCtx) c_int {
}

const request = api.VM.allocator.create(http.Client.Request) catch @panic("Out of memory");
const server_header_buffer = api.VM.allocator.alloc(u8, 1024) catch @panic("Out of memory"); // FIXME: what do i do we this??
const server_header_buffer = api.VM.allocator.alloc(u8, 16 * 1024) catch @panic("Out of memory");

request.* = client.open(
method,
Expand Down Expand Up @@ -103,7 +104,14 @@ pub export fn HttpClientSend(ctx: *api.NativeCtx) c_int {
pub export fn HttpRequestWait(ctx: *api.NativeCtx) c_int {
const userdata_value = ctx.vm.bz_peek(0);
const userdata = userdata_value.bz_valueToUserData();
const request = @as(*http.Client.Request, @ptrCast(@alignCast(@as(*anyopaque, @ptrFromInt(userdata)))));
const request = @as(
*http.Client.Request,
@ptrCast(
@alignCast(
@as(*anyopaque, @ptrFromInt(userdata)),
),
),
);

request.wait() catch |err| {
handleWaitError(ctx, err);
Expand All @@ -116,10 +124,36 @@ pub export fn HttpRequestWait(ctx: *api.NativeCtx) c_int {
return 1;
}

pub export fn HttpRequestDeinit(ctx: *api.NativeCtx) c_int {
const userdata_value = ctx.vm.bz_peek(0);
const userdata = userdata_value.bz_valueToUserData();
const request = @as(
*http.Client.Request,
@ptrCast(
@alignCast(
@as(*anyopaque, @ptrFromInt(userdata)),
),
),
);

api.VM.allocator.free(request.response.parser.header_bytes_buffer);
request.deinit();
api.VM.allocator.destroy(request);

return 0;
}

pub export fn HttpRequestRead(ctx: *api.NativeCtx) c_int {
const userdata_value = ctx.vm.bz_peek(0);
const userdata = userdata_value.bz_valueToUserData();
const request = @as(*http.Client.Request, @ptrCast(@alignCast(@as(*anyopaque, @ptrFromInt(userdata)))));
const request = @as(
*http.Client.Request,
@ptrCast(
@alignCast(
@as(*anyopaque, @ptrFromInt(userdata)),
),
),
);

var body_raw = std.ArrayList(u8).init(api.VM.allocator);
defer body_raw.deinit();
Expand Down
10 changes: 9 additions & 1 deletion src/lib/http.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum HttpError {
HttpConnectionHeaderUnsupported,
HttpHeaderContinuationsUnsupported,
HttpHeadersInvalid,
HttpHeadersOversize
HttpHeadersOversize,
HttpRedirectLocationInvalid,
HttpRedirectLocationMissing,
HttpTransferEncodingUnsupported,
Expand Down Expand Up @@ -72,6 +72,8 @@ extern fun HttpClientSend(ud client, Method method, str uri, {str: str} headers)
extern fun HttpRequestWait(ud request) > void !> HttpError;
|| @private
extern fun HttpRequestRead(ud request) > Response !> HttpError;
|| @private
extern fun HttpRequestDeinit(ud request) > void;

const {int: str} reasons = {
100: "Continue",
Expand Down Expand Up @@ -228,6 +230,12 @@ export object Request {

return "";
}

fun collect() > void {
if (this.request -> request) {
HttpRequestDeinit(request);
}
}
}

export object Response {
Expand Down

0 comments on commit c0f543b

Please sign in to comment.