This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function
If handling redirects and the request has no payload, then this function will automatically follow redirects.
If a request payload is present, then this function will error with error.RedirectRequiresResend.
This function takes an auxiliary buffer to store the arbitrarily large URI which may need to be merged with the previous URI, and that data needs to survive across different connections, which is where the input buffer lives.
redirect_buffer must outlive accesses to Request.uri. If this buffer capacity would be exceeded, error.HttpRedirectLocationOversize is returned instead. This buffer may be empty if no redirects are to be handled. RFC 9110 recommends making this at least 8000 bytes.
If this fails with error.ReadFailed then the Connection.getReadError method of r.connection can be used to get more detailed information.
pubfnreceiveHead(r: *Request, redirect_buffer: []u8) ReceiveHeadError!Response {
varaux_buf = redirect_buffer;
while (true) {
// This while loop is for handling redirects, which means the request's// connection may be different than the previous iteration. However, it// is still guaranteed to be non-null with each iteration of this loop.constconnection = r.connection.?;
consthead_buffer = r.reader.receiveHead() catch |err| {
// Failure here means the connection can no longer be reused.connection.closing = true;
returnerr;
};
constresponse: Response = .{
.request = r,
.head = Response.Head.parse(head_buffer) catchreturnerror.HttpHeadersInvalid,
};
consthead = &response.head;
if (head.status == .@"continue") {
if (r.handle_continue) continue;
r.response_transfer_encoding = head.transfer_encoding;
r.response_content_length = head.content_length;
returnresponse; // we're not handling the 100-continue
}
if (r.method == .CONNECTandhead.status.class() == .success) {
// This connection is no longer doing HTTP.connection.closing = false;
r.response_transfer_encoding = head.transfer_encoding;
r.response_content_length = head.content_length;
returnresponse;
}
connection.closing = !head.keep_aliveor !r.keep_alive;
// Any response to a HEAD request and any response with a 1xx// (Informational), 204 (No Content), or 304 (Not Modified) status// code is always terminated by the first empty line after the// header fields, regardless of the header fields present in the// message.if (r.method == .HEADorhead.status.class() == .informationalorhead.status == .no_contentorhead.status == .not_modified)
{
r.response_transfer_encoding = head.transfer_encoding;
r.response_content_length = head.content_length;
returnresponse;
}
if (head.status.class() == .redirectandr.redirect_behavior != .unhandled) {
if (r.redirect_behavior == .not_allowed) {
// Connection can still be reused by skipping the body.constreader = r.reader.bodyReader(&.{}, head.transfer_encoding, head.content_length);
_ = reader.discardRemaining() catch |err| switch (err) {
error.ReadFailed => connection.closing = true,
};
returnerror.TooManyHttpRedirects;
}
tryr.redirect(head, &aux_buf);
tryr.sendBodiless();
continue;
}
if (!r.accept_encoding[@backingInt(head.content_encoding)])
returnerror.HttpContentEncodingUnsupported;
r.response_transfer_encoding = head.transfer_encoding;
r.response_content_length = head.content_length;
returnresponse;
}
}
If handling redirects and the request has no payload, then this function will automatically follow redirects.
If a request payload is present, then this function will error with
error.RedirectRequiresResend.This function takes an auxiliary buffer to store the arbitrarily large URI which may need to be merged with the previous URI, and that data needs to survive across different connections, which is where the input buffer lives.
redirect_buffermust outlive accesses toRequest.uri. If this buffer capacity would be exceeded,error.HttpRedirectLocationOversizeis returned instead. This buffer may be empty if no redirects are to be handled. RFC 9110 recommends making this at least 8000 bytes.If this fails with
error.ReadFailedthen theConnection.getReadErrormethod ofr.connectioncan be used to get more detailed information.