Skip to content

Commit

Permalink
update http code of config_gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Feb 25, 2024
1 parent 8cca7a1 commit 845a9a0
Showing 1 changed file with 54 additions and 16 deletions.
70 changes: 54 additions & 16 deletions src/config_gen/config_gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -940,29 +940,67 @@ const Response = union(enum) {
};

fn httpGET(allocator: std.mem.Allocator, uri: std.Uri) !Response {
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
try client.ca_bundle.rescan(allocator);
// TODO remove duplicate logic once https://github.com/ziglang/zig/issues/19071 has been fixed
comptime std.debug.assert(zig_builtin.zig_version.order(.{ .major = 0, .minor = 12, .patch = 0 }) == .lt);

// https://github.com/ziglang/zig/pull/18955
const zig_version_at_18955 = comptime std.SemanticVersion.parse("0.12.0-dev.2918+cfce81f7d") catch unreachable;
const zig_is_pre_18955 = comptime zig_builtin.zig_version.order(zig_version_at_18955) == .lt;
if (zig_is_pre_18955) {
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
try client.ca_bundle.rescan(allocator);

if (@hasDecl(std.http.Client, "loadDefaultProxies"))
try client.loadDefaultProxies();

var request = try client.open(.GET, uri, .{ .allocator = allocator }, .{});
defer request.deinit();

try request.send(.{});
// try request.finish();
try request.wait();

if (request.response.status.class() != .success) {
return .{
.other = request.response.status,
};
}

if (@hasDecl(std.http.Client, "loadDefaultProxies"))
try client.loadDefaultProxies();
return .{
.success = try request.reader().readAllAlloc(allocator, std.math.maxInt(usize)),
};
} else {
var arena_allocator = std.heap.ArenaAllocator.init(allocator);
defer arena_allocator.deinit();

var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
try client.initDefaultProxies(arena_allocator.allocator());

var request = try client.open(.GET, uri, .{ .allocator = allocator }, .{});
defer request.deinit();
var server_header_buffer: [1024]u8 = undefined;

try request.send(.{});
// try request.finish();
try request.wait();
var request = try client.open(
.GET,
uri,
.{ .server_header_buffer = &server_header_buffer },
);
defer request.deinit();

try request.send(.{});
try request.finish();
try request.wait();

if (request.response.status.class() != .success) {
return .{
.other = request.response.status,
};
}

if (request.response.status.class() != .success) {
return .{
.other = request.response.status,
.success = try request.reader().readAllAlloc(allocator, std.math.maxInt(usize)),
};
}

return .{
.success = try request.reader().readAllAlloc(allocator, std.math.maxInt(usize)),
};
}

pub fn main() !void {
Expand Down

0 comments on commit 845a9a0

Please sign in to comment.