Skip to content

Commit 8248fdb

Browse files
committed
std.http.Client: support HTTP redirects
* 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
1 parent 079f628 commit 8248fdb

3 files changed

Lines changed: 470 additions & 263 deletions

File tree

lib/std/http.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub const Client = @import("http/Client.zig");
2-
pub const Headers = @import("http/Headers.zig");
32

43
pub const Version = enum {
54
@"HTTP/1.0",
@@ -219,21 +218,22 @@ pub const Status = enum(u10) {
219218
}
220219

221220
pub const Class = enum {
221+
nonstandard,
222222
informational,
223223
success,
224224
redirect,
225225
client_error,
226226
server_error,
227227
};
228228

229-
pub fn class(self: Status) ?Class {
229+
pub fn class(self: Status) Class {
230230
return switch (@enumToInt(self)) {
231231
100...199 => .informational,
232232
200...299 => .success,
233233
300...399 => .redirect,
234234
400...499 => .client_error,
235235
500...599 => .server_error,
236-
else => null,
236+
else => .nonstandard,
237237
};
238238
}
239239

@@ -254,5 +254,4 @@ test {
254254
_ = Client;
255255
_ = Method;
256256
_ = Status;
257-
_ = Headers;
258257
}

0 commit comments

Comments
 (0)