Skip to content

Commit

Permalink
std.http.Client: support HTTP redirects
Browse files Browse the repository at this point in the history
 * std.http.Status.Class: add a "nonstandard" enum tag. Instead of
   having `class` return an optional value, it can potentially return
   nonstandard.
 * extract out std.http.Client.Connection from std.http.Client.Request
   - this code abstracts over plain/TLS only
   - this is the type that will potentially be stored in a client's LRU
     connection map
 * introduce two-staged HTTP header parsing
   - API users can rely on a heap-allocated buffer with a maximum limit,
     which defaults to 16 KB, or they can provide a static buffer that
     is borrowed by the Request instance.
   - The entire HTTP header is buffered because there are strings in
     there and they must be accessed later, such as with the case of
     HTTP redirects.
   - When buffering the HTTP header, the parser only looks for the
     \r\n\r\n pattern. Further validation is done later.
   - After the full HTTP header is buffered, it is parsed into
     components such as Content-Length and Location.
 * HTTP redirects are handled, with a maximum redirect count option that
   defaults to 3.
   - Connection: close is always used for now; implementing keep-alive
     connections and an LRU connection pool in std.http.Client is a task
     for another day.

see #2007
  • Loading branch information
andrewrk committed Jan 5, 2023
1 parent 079f628 commit 8248fdb
Show file tree
Hide file tree
Showing 3 changed files with 470 additions and 263 deletions.
7 changes: 3 additions & 4 deletions lib/std/http.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub const Client = @import("http/Client.zig");
pub const Headers = @import("http/Headers.zig");

pub const Version = enum {
@"HTTP/1.0",
Expand Down Expand Up @@ -219,21 +218,22 @@ pub const Status = enum(u10) {
}

pub const Class = enum {
nonstandard,
informational,
success,
redirect,
client_error,
server_error,
};

pub fn class(self: Status) ?Class {
pub fn class(self: Status) Class {
return switch (@enumToInt(self)) {
100...199 => .informational,
200...299 => .success,
300...399 => .redirect,
400...499 => .client_error,
500...599 => .server_error,
else => null,
else => .nonstandard,
};
}

Expand All @@ -254,5 +254,4 @@ test {
_ = Client;
_ = Method;
_ = Status;
_ = Headers;
}
Loading

0 comments on commit 8248fdb

Please sign in to comment.