ziglang/zig

HTTP client fails to make HTTPS requests via a proxy

timothyqiu opened this issue · 0 comments

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

  1. Run a local proxy server, say mitmdump
  2. Setup environment variables for http(s) proxy
    export https_proxy=http://127.0.0.1:8080
    
  3. Make sure the proxy server is setup correctly. For example curl https://httpbun.com/get should work and mitmdump should show the request.
  4. Then if you run this:
    const std = @import("std");
    
    pub fn main() !void {
        var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
        defer arena.deinit();
        const allocator = arena.allocator();
    
        var client = std.http.Client{ .allocator = allocator };
        defer client.deinit();
    
        client.initDefaultProxies(allocator) catch |err| {
            std.debug.print("Unable to initialize default proxies: {}\n", .{err});
        };
    
        var response = std.ArrayList(u8).init(allocator);
        defer response.deinit();
    
        // Can't use httpbin.org, see #14172.
        const result = try client.fetch(.{
            .method = .GET,
            .location = .{ .url = "https://httpbun.com/get" },
            .response_storage = .{ .dynamic = &response },
        });
        std.debug.print("Result: {}\n", .{result});
        std.debug.print("Response: {s}\n", .{response.items});
    }
  5. It's a bad request:
    Result: http.Client.FetchResult{ .status = http.Status.bad_request }
    Response: Client sent an HTTP request to an HTTPS server.
    

Expected Behavior

The request is successful, through the proxy.

Result: http.Client.FetchResult{ .status = http.Status.ok }
Response: {
  "method": "GET",
  "args": {},
  "headers": {
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbun.com",
    "User-Agent": "zig/0.12.0 (std.http)"
  },
  "origin": "123.456.789.012",
  "url": "http://httpbun.com/get",
  "form": {},
  "data": "",
  "json": null,
  "files": {}
}