Skip to content

Commit 42be972

Browse files
committed
std.http: remove 'done' flag
This is a state machine that already has a `state` field. No need to additionally store "done" - it just makes things unnecessarily complicated and buggy.
1 parent 1d145fd commit 42be972

File tree

4 files changed

+152
-85
lines changed

4 files changed

+152
-85
lines changed

lib/std/http/Client.zig

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ pub const Request = struct {
610610
req.response.headers.deinit();
611611

612612
if (req.connection) |connection| {
613-
if (!req.response.parser.done) {
613+
if (req.response.parser.state != .complete) {
614614
// If the response wasn't fully read, then we need to close the connection.
615615
connection.closing = true;
616616
}
@@ -624,7 +624,7 @@ pub const Request = struct {
624624
// This function must deallocate all resources associated with the request, or keep those which will be used
625625
// This needs to be kept in sync with deinit and request
626626
fn redirect(req: *Request, uri: Uri) !void {
627-
assert(req.response.parser.done);
627+
assert(req.response.parser.state == .complete);
628628

629629
switch (req.response.compression) {
630630
.none => {},
@@ -794,12 +794,12 @@ pub const Request = struct {
794794
}
795795

796796
fn transferRead(req: *Request, buf: []u8) TransferReadError!usize {
797-
if (req.response.parser.done) return 0;
797+
if (req.response.parser.state == .complete) return 0;
798798

799799
var index: usize = 0;
800800
while (index == 0) {
801801
const amt = try req.response.parser.read(req.connection.?, buf[index..], req.response.skip);
802-
if (amt == 0 and req.response.parser.done) break;
802+
if (amt == 0 and req.response.parser.state == .complete) break;
803803
index += amt;
804804
}
805805

@@ -840,7 +840,7 @@ pub const Request = struct {
840840
try req.response.parse(req.response.parser.get(), false);
841841

842842
if (req.response.status == .@"continue") {
843-
req.response.parser.done = true; // we're done parsing the continue response, reset to prepare for the real response
843+
req.response.parser.state = .complete; // we're done parsing the continue response, reset to prepare for the real response
844844
req.response.parser.reset();
845845

846846
if (req.handle_continue)
@@ -852,7 +852,7 @@ pub const Request = struct {
852852
// we're switching protocols, so this connection is no longer doing http
853853
if (req.method == .CONNECT and req.response.status.class() == .success) {
854854
req.connection.?.closing = false;
855-
req.response.parser.done = true;
855+
req.response.parser.state = .complete;
856856

857857
return; // the connection is not HTTP past this point, return to the caller
858858
}
@@ -872,8 +872,10 @@ pub const Request = struct {
872872
// Any response to a HEAD request and any response with a 1xx (Informational), 204 (No Content), or 304 (Not Modified)
873873
// status code is always terminated by the first empty line after the header fields, regardless of the header fields
874874
// present in the message
875-
if (req.method == .HEAD or req.response.status.class() == .informational or req.response.status == .no_content or req.response.status == .not_modified) {
876-
req.response.parser.done = true;
875+
if (req.method == .HEAD or req.response.status.class() == .informational or
876+
req.response.status == .no_content or req.response.status == .not_modified)
877+
{
878+
req.response.parser.state = .complete;
877879

878880
return; // the response is empty, no further setup or redirection is necessary
879881
}
@@ -889,7 +891,7 @@ pub const Request = struct {
889891
} else if (req.response.content_length) |cl| {
890892
req.response.parser.next_chunk_length = cl;
891893

892-
if (cl == 0) req.response.parser.done = true;
894+
if (cl == 0) req.response.parser.state = .complete;
893895
} else {
894896
// read until the connection is closed
895897
req.response.parser.next_chunk_length = std.math.maxInt(u64);
@@ -947,7 +949,7 @@ pub const Request = struct {
947949
try req.send(.{});
948950
} else {
949951
req.response.skip = false;
950-
if (!req.response.parser.done) {
952+
if (req.response.parser.state != .complete) {
951953
switch (req.response.transfer_compression) {
952954
.identity => req.response.compression = .none,
953955
.compress, .@"x-compress" => return error.CompressionNotSupported,

lib/std/http/Server.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ pub const Response = struct {
395395
return .reset;
396396
}
397397

398-
if (!res.request.parser.done) {
398+
if (res.request.parser.state != .complete) {
399399
// If the response wasn't fully read, then we need to close the connection.
400400
res.connection.closing = true;
401401
return .closing;
@@ -534,12 +534,12 @@ pub const Response = struct {
534534
}
535535

536536
fn transferRead(res: *Response, buf: []u8) TransferReadError!usize {
537-
if (res.request.parser.done) return 0;
537+
if (res.request.parser.state == .complete) return 0;
538538

539539
var index: usize = 0;
540540
while (index == 0) {
541541
const amt = try res.request.parser.read(&res.connection, buf[index..], false);
542-
if (amt == 0 and res.request.parser.done) break;
542+
if (amt == 0 and res.request.parser.state == .complete) break;
543543
index += amt;
544544
}
545545

@@ -596,12 +596,12 @@ pub const Response = struct {
596596
} else if (res.request.content_length) |cl| {
597597
res.request.parser.next_chunk_length = cl;
598598

599-
if (cl == 0) res.request.parser.done = true;
599+
if (cl == 0) res.request.parser.state = .complete;
600600
} else {
601-
res.request.parser.done = true;
601+
res.request.parser.state = .complete;
602602
}
603603

604-
if (!res.request.parser.done) {
604+
if (res.request.parser.state != .complete) {
605605
switch (res.request.transfer_compression) {
606606
.identity => res.request.compression = .none,
607607
.compress, .@"x-compress" => return error.CompressionNotSupported,

0 commit comments

Comments
 (0)