Skip to content

Commit b3e495a

Browse files
authored
Merge pull request #14202 from ziglang/std.http
std.http.Client: support HTTP redirects
2 parents 6ad9210 + 3055ab7 commit b3e495a

File tree

4 files changed

+534
-167
lines changed

4 files changed

+534
-167
lines changed

lib/std/http.zig

+7-53
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
pub const Client = @import("http/Client.zig");
22

3+
pub const Version = enum {
4+
@"HTTP/1.0",
5+
@"HTTP/1.1",
6+
};
7+
38
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
49
/// https://datatracker.ietf.org/doc/html/rfc7231#section-4 Initial definiton
510
/// https://datatracker.ietf.org/doc/html/rfc5789#section-2 PATCH
@@ -220,14 +225,13 @@ pub const Status = enum(u10) {
220225
server_error,
221226
};
222227

223-
pub fn class(self: Status) ?Class {
228+
pub fn class(self: Status) Class {
224229
return switch (@enumToInt(self)) {
225230
100...199 => .informational,
226231
200...299 => .success,
227232
300...399 => .redirect,
228233
400...499 => .client_error,
229-
500...599 => .server_error,
230-
else => null,
234+
else => .server_error,
231235
};
232236
}
233237

@@ -242,60 +246,10 @@ pub const Status = enum(u10) {
242246
}
243247
};
244248

245-
pub const Headers = struct {
246-
state: State = .start,
247-
invalid_index: u32 = undefined,
248-
249-
pub const State = enum { invalid, start, line, nl_r, nl_n, nl2_r, finished };
250-
251-
/// Returns how many bytes are processed into headers. Always less than or
252-
/// equal to bytes.len. If the amount returned is less than bytes.len, it
253-
/// means the headers ended and the first byte after the double \r\n\r\n is
254-
/// located at `bytes[result]`.
255-
pub fn feed(h: *Headers, bytes: []const u8) usize {
256-
for (bytes) |b, i| {
257-
switch (h.state) {
258-
.start => switch (b) {
259-
'\r' => h.state = .nl_r,
260-
'\n' => return invalid(h, i),
261-
else => {},
262-
},
263-
.nl_r => switch (b) {
264-
'\n' => h.state = .nl_n,
265-
else => return invalid(h, i),
266-
},
267-
.nl_n => switch (b) {
268-
'\r' => h.state = .nl2_r,
269-
else => h.state = .line,
270-
},
271-
.nl2_r => switch (b) {
272-
'\n' => h.state = .finished,
273-
else => return invalid(h, i),
274-
},
275-
.line => switch (b) {
276-
'\r' => h.state = .nl_r,
277-
'\n' => return invalid(h, i),
278-
else => {},
279-
},
280-
.invalid => return i,
281-
.finished => return i,
282-
}
283-
}
284-
return bytes.len;
285-
}
286-
287-
fn invalid(h: *Headers, i: usize) usize {
288-
h.invalid_index = @intCast(u32, i);
289-
h.state = .invalid;
290-
return i;
291-
}
292-
};
293-
294249
const std = @import("std.zig");
295250

296251
test {
297252
_ = Client;
298253
_ = Method;
299254
_ = Status;
300-
_ = Headers;
301255
}

0 commit comments

Comments
 (0)