Skip to content

Conversation

@karlseguin
Copy link
Collaborator

@karlseguin karlseguin commented Mar 20, 2025

#466

The two main goals are:
1 - Have a single HTTP client that does both sync and async. This makes it easier
to add things like proxy support, and might help with things like cookies.

2 - Maintaining our own HTTP client isn't trivial, especially given that
we're likely to encounter non-conforming servers. But, this removes the need
to maintain zig-async-io, which is a fork of [a small part] of the stdlib and
tls.zig. This itself is non-trivial. Also, if we do run into weird server
behavior, we'd have to maintain that in std.http.Client unless we could get
a PR accepted.

What's currently missing
1 - Redirect handling (critical to add this)
2 - Chunked encoding response (critical to add this)
3 - Connection pooling (could be added after)
4 - Compression (could be added after)

If you try "fetch" or "serve" a page that doesn't do a direct and that doesn't
use chunked encoding, it should work. Puppeteer demo works.

This is built on the zig-0.14 branch. I initially started working against main
(Zig 0.13) but then I noticed that tls.zig has very useful support for a
generic async loop in its master. It's a pretty big game changer and makes the
integration with our event loop trivial (I'm currently pointing to my own fork,
but I'm hopeful that my PR (ianic/tls.zig#8)
will be accepted, and if not, it's a simple change to accommodate tls.zig as-is).

The async API is simpler, with as single callback. A simplified example:

fn send (self *XMLHttpRequest, loop: *Loop) !void {
    const request = try self.client.request(self.method, self.uri);
    errdefer request.deinit();

    for (self.headers.list.items) |hdr| {
        try request.addHeader(hdr.name, hdr.value, .{});
    }
    request.body = self.payload;
    try request.sendAsync(loop, self, .{});
}

pub fn onHttpResponse(self: *XMLHttpRequest, progress_: http.Error!http.Progress) !void {
      const progress = try progress_;

      if (progress.first) {
          // the first emitted chunk,
          // get the headers, get the status
      }

      if (progress.data) |data| {
          // we have part of the body,
          // can still get the headers/status here if needed
          self.body.append(data);
      }

      if (progress.done) {
        // we're done!
      }
}

Generally, the integration with browser an XMLHttpRequest was rushed. The focus
has been on src/http/client.zig.

Finally, I've added the tls.zig dependency as a Zig package (build.zig.zon)
rather than a submodule. I assume submodules were chosen back when Zig didn't
have a package manager.

@karlseguin karlseguin changed the title Http client Replace zig-async-io with a custom HTTP client Mar 20, 2025
@karlseguin karlseguin marked this pull request as draft March 21, 2025 06:49
@karlseguin karlseguin marked this pull request as ready for review March 23, 2025 06:13
Drain response prior to redirect.
Switch to non-blocking sockets.

Fix TLS handshake/receive/send ordering
Improve test server handshake performance, allowing for a few more fuzz
iterations without making tests unbearably slow.
Add more fuzz tests around async tls.
Retry on test timeout for slower machines (i.e. CI build), while also reducing
wait time for faster builds.
Copy link
Member

@krichprollsch krichprollsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome 👏 Thank you so much @karlseguin 🙏

Switch generic http_client error level from warn to err
@krichprollsch krichprollsch merged commit 3a1a582 into lightpanda-io:main Mar 27, 2025
10 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2025
@karlseguin karlseguin deleted the http_client branch March 31, 2025 10:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants