authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-11 22:44:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
logf46447e6a1cda2b3b2e0ee90a68b2cca112f8742
treec86cad17aecbf5f7757560046da5d9ecc7dc8689
parent00acf8a66dec78743d26d62e86391b8794a1c627

std.http.Client.fetch: add redirect behavior to options


1 files changed, 40 insertions(+), 25 deletions(-)

lib/std/http/Client.zig+40-25
......@@ -580,11 +580,7 @@ pub const Request = struct {
580580 /// The transfer encoding of the request body.
581581 transfer_encoding: RequestTransfer = .none,
582582
583 /// The redirect quota left for this request.
584 redirects_left: u32,
585
586 /// Whether the request should follow redirects.
587 handle_redirects: bool,
583 redirect_behavior: RedirectBehavior,
588584
589585 /// Whether the request should handle a 100-continue response before sending the request body.
590586 handle_continue: bool,
......@@ -597,6 +593,25 @@ pub const Request = struct {
597593 /// Used as a allocator for resolving redirects locations.
598594 arena: std.heap.ArenaAllocator,
599595
596 /// Any value other than `not_allowed` or `unhandled` means that integer represents
597 /// how many remaining redirects are allowed.
598 pub const RedirectBehavior = enum(u16) {
599 /// The next redirect will cause an error.
600 not_allowed = 0,
601 /// Redirects are passed to the client to analyze the redirect response
602 /// directly.
603 unhandled = std.math.maxInt(u16),
604 _,
605
606 pub fn subtractOne(rb: *RedirectBehavior) void {
607 switch (rb.*) {
608 .not_allowed => unreachable,
609 .unhandled => unreachable,
610 _ => rb.* = @enumFromInt(@intFromEnum(rb.*) - 1),
611 }
612 }
613 };
614
600615 /// Frees all resources associated with the request.
601616 pub fn deinit(req: *Request) void {
602617 switch (req.response.compression) {
......@@ -621,8 +636,9 @@ pub const Request = struct {
621636 req.* = undefined;
622637 }
623638
624 // This function must deallocate all resources associated with the request, or keep those which will be used
625 // This needs to be kept in sync with deinit and request
639 // This function must deallocate all resources associated with the request,
640 // or keep those which will be used.
641 // This needs to be kept in sync with deinit and request.
626642 fn redirect(req: *Request, uri: Uri) !void {
627643 assert(req.response.parser.state == .complete);
628644
......@@ -647,7 +663,7 @@ pub const Request = struct {
647663
648664 req.uri = uri;
649665 req.connection = try req.client.connect(host, port, protocol);
650 req.redirects_left -= 1;
666 req.redirect_behavior.subtractOne();
651667 req.response.headers.clearRetainingCapacity();
652668 req.response.parser.reset();
653669
......@@ -819,7 +835,7 @@ pub const Request = struct {
819835 /// Waits for a response from the server and parses any headers that are sent.
820836 /// This function will block until the final response is received.
821837 ///
822 /// If `handle_redirects` is true and the request has no payload, then this
838 /// If handling redirects and the request has no payload, then this
823839 /// function will automatically follow redirects. If a request payload is
824840 /// present, then this function will error with
825841 /// error.RedirectRequiresResend.
......@@ -897,15 +913,14 @@ pub const Request = struct {
897913 req.response.parser.next_chunk_length = std.math.maxInt(u64);
898914 }
899915
900 if (req.response.status.class() == .redirect and req.handle_redirects) {
916 if (req.response.status.class() == .redirect and req.redirect_behavior != .unhandled) {
901917 req.response.skip = true;
902918
903919 // skip the body of the redirect response, this will at least
904920 // leave the connection in a known good state.
905 const empty = @as([*]u8, undefined)[0..0];
906 assert(try req.transferRead(empty) == 0); // we're skipping, no buffer is necessary
921 assert(try req.transferRead(&.{}) == 0); // we're skipping, no buffer is necessary
907922
908 if (req.redirects_left == 0) return error.TooManyHttpRedirects;
923 if (req.redirect_behavior == .not_allowed) return error.TooManyHttpRedirects;
909924
910925 const location = req.response.headers.getFirstValue("location") orelse
911926 return error.HttpRedirectMissingLocation;
......@@ -1380,7 +1395,7 @@ pub fn connectTunnel(
13801395
13811396 var buffer: [8096]u8 = undefined;
13821397 var req = client.open(.CONNECT, uri, proxy.headers, .{
1383 .handle_redirects = false,
1398 .redirect_behavior = .unhandled,
13841399 .connection = conn,
13851400 .server_header_buffer = &buffer,
13861401 }) catch |err| {
......@@ -1481,13 +1496,13 @@ pub const RequestOptions = struct {
14811496 /// you finish the request, then the request *will* deadlock.
14821497 handle_continue: bool = true,
14831498
1484 /// Automatically follow redirects. This will only follow redirects for
1485 /// repeatable requests (ie. with no payload or the server has acknowledged
1486 /// the payload).
1487 handle_redirects: bool = true,
1499 /// This field specifies whether to automatically follow redirects, and if
1500 /// so, how many redirects to follow before returning an error.
1501 ///
1502 /// This will only follow redirects for repeatable requests (ie. with no
1503 /// payload or the server has acknowledged the payload).
1504 redirect_behavior: Request.RedirectBehavior = @enumFromInt(3),
14881505
1489 /// How many redirects to follow before returning an error.
1490 max_redirects: u32 = 3,
14911506 /// Externally-owned memory used to store the server's entire HTTP header.
14921507 /// `error.HttpHeadersOversize` is returned from read() when a
14931508 /// client sends too many bytes of HTTP headers.
......@@ -1548,8 +1563,7 @@ pub fn open(
15481563 .headers = try headers.clone(client.allocator), // Headers must be cloned to properly handle header transformations in redirects.
15491564 .method = method,
15501565 .version = options.version,
1551 .redirects_left = options.max_redirects,
1552 .handle_redirects = options.handle_redirects,
1566 .redirect_behavior = options.redirect_behavior,
15531567 .handle_continue = options.handle_continue,
15541568 .response = .{
15551569 .status = undefined,
......@@ -1600,6 +1614,7 @@ pub const FetchOptions = struct {
16001614
16011615 server_header_buffer: ?[]u8 = null,
16021616 response_strategy: ResponseStrategy = .{ .storage = .{ .dynamic = 16 * 1024 * 1024 } },
1617 redirect_behavior: ?Request.RedirectBehavior = null,
16031618
16041619 location: Location,
16051620 method: http.Method = .GET,
......@@ -1642,7 +1657,8 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
16421657
16431658 var req = try open(client, options.method, uri, options.headers, .{
16441659 .server_header_buffer = options.server_header_buffer orelse &server_header_buffer,
1645 .handle_redirects = options.payload == .none,
1660 .redirect_behavior = options.redirect_behavior orelse
1661 if (options.payload == .none) @enumFromInt(3) else .unhandled,
16461662 });
16471663 defer req.deinit();
16481664
......@@ -1694,8 +1710,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
16941710 .none => { // Take advantage of request internals to discard the response body and make the connection available for another request.
16951711 req.response.skip = true;
16961712
1697 const empty = @as([*]u8, undefined)[0..0];
1698 assert(try req.transferRead(empty) == 0); // we're skipping, no buffer is necessary
1713 assert(try req.transferRead(&.{}) == 0); // we're skipping, no buffer is necessary
16991714 },
17001715 }
17011716