Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230const std = @import("std");
const abi = @import("../abi.zig");const config = @import("../config.zig");const http_executor = @import("http_executor.zig");const template = @import("../template.zig");
const c = @cImport({ @cInclude("grpc_cpp_client.h");});
pub const RequestExecution = http_executor.RequestExecution;pub const TlsContext = http_executor.TlsContext;
pub const Client = struct { handle: ?*c.xburner_grpc_client_t = null,
pub fn init() Client { return .{}; }
pub fn deinit(self: *Client) void { if (self.handle) |handle| c.xburner_grpc_client_destroy(handle); self.* = undefined; }
pub fn executeRequest( self: *Client, allocator: std.mem.Allocator, store: *const template.VarStore, tls_context: *const TlsContext, target: []const u8, path_template: []const u8, header_templates: []const config.HeaderTemplate, body_template: ?[]const u8, body_override: ?[]const u8, connect_timeout_ms: u32, timeout_ms: u32, delay_before_ms: u32, capture_body_max_bytes: u32, ) !RequestExecution { try self.ensureInitialized(allocator, target, tls_context);
const rendered_path = try template.render(allocator, path_template, store, .path); defer allocator.free(rendered_path);
const rendered_body = if (body_override) |body| try allocator.dupe(u8, body) else if (body_template) |body| try template.render(allocator, body, store, .body) else try allocator.dupe(u8, ""); defer allocator.free(rendered_body);
var rendered_headers = std.ArrayList(RenderedHeader).empty; defer { for (rendered_headers.items) |header| allocator.free(header.value); rendered_headers.deinit(allocator); }
for (header_templates) |header| { if (isManagedMetadata(header.key)) continue; const value = try template.render(allocator, header.value, store, .header); try rendered_headers.append(allocator, .{ .key = header.key, .value = value, }); }
if (delay_before_ms != 0) { std.Thread.sleep(@as(u64, delay_before_ms) * std.time.ns_per_ms); }
var c_headers = try allocator.alloc(c.xburner_grpc_header_t, rendered_headers.items.len); defer allocator.free(c_headers); for (rendered_headers.items, 0..) |header, index| { c_headers[index] = .{ .key_ptr = header.key.ptr, .key_len = @intCast(header.key.len), .value_ptr = header.value.ptr, .value_len = @intCast(header.value.len), }; }
var result: c.xburner_grpc_unary_result_t = std.mem.zeroes(c.xburner_grpc_unary_result_t); var body_ptr: [*c]const u8 = null; var body_len: u32 = 0; var grpc_message_ptr: [*c]const u8 = null; var grpc_message_len: u32 = 0; var err_buf: [256]u8 = undefined;
const rc = c.xburner_grpc_client_unary( self.handle.?, rendered_path.ptr, @intCast(rendered_path.len), if (c_headers.len == 0) null else c_headers.ptr, @intCast(c_headers.len), rendered_body.ptr, @intCast(rendered_body.len), timeout_ms, connect_timeout_ms, capture_body_max_bytes, &result, &body_ptr, &body_len, &grpc_message_ptr, &grpc_message_len, &err_buf, err_buf.len, ); if (rc != 0) return error.GrpcBackendFailed;
defer if (body_ptr != null) c.xburner_grpc_buffer_free(body_ptr); defer if (grpc_message_ptr != null) c.xburner_grpc_buffer_free(grpc_message_ptr);
return .{ .status_code = result.http_status, .duration_nanos = result.duration_nanos, .bytes_sent = result.bytes_sent, .bytes_received = result.bytes_received, .success = result.success != 0, .transport_code = @enumFromInt(result.transport_code), .timeout_phase = @enumFromInt(result.timeout_phase), .grpc_status = if (result.grpc_status == c.XBURNER_GRPC_STATUS_NONE) null else @intCast(result.grpc_status), .deadline_exceeded = result.deadline_exceeded != 0, .grpc_message = if (grpc_message_ptr != null and grpc_message_len != 0) try allocator.dupe(u8, grpc_message_ptr[0..grpc_message_len]) else null, .body_truncated = result.body_truncated != 0, .captured_body = if (body_ptr != null and body_len != 0) try allocator.dupe(u8, body_ptr[0..body_len]) else null, }; }
fn ensureInitialized(self: *Client, allocator: std.mem.Allocator, target: []const u8, tls_context: *const TlsContext) !void { if (self.handle != null) return;
const parsed_target = try parseTarget(allocator, target, tls_context.force_tls); defer allocator.free(parsed_target.authority);
const server_name = if (tls_context.server_name) |name| try allocator.dupeZ(u8, name) else null; defer if (server_name) |value| allocator.free(value);
const ca_file = if (tls_context.ca_file) |path| try allocator.dupeZ(u8, path) else null; defer if (ca_file) |value| allocator.free(value);
var handle: ?*c.xburner_grpc_client_t = null; var err_buf: [256]u8 = undefined; const rc = c.xburner_grpc_client_create( &.{ .authority = parsed_target.authority.ptr, .use_tls = @intFromBool(parsed_target.use_tls), .insecure_skip_verify = @intFromBool(tls_context.insecure_skip_verify), .server_name = if (server_name) |value| value.ptr else null, .ca_file = if (ca_file) |value| value.ptr else null, }, &handle, &err_buf, err_buf.len, ); if (rc != 0) return error.GrpcBackendCreateFailed;
self.handle = handle; }};
const RenderedHeader = struct { key: []const u8, value: []u8,};
const ParsedTarget = struct { authority: [:0]u8, use_tls: bool,};
fn parseTarget(allocator: std.mem.Allocator, raw_target: []const u8, force_tls: bool) !ParsedTarget { const normalized = if (force_tls and std.mem.startsWith(u8, raw_target, "http://")) try std.fmt.allocPrint(allocator, "https://{s}", .{raw_target["http://".len..]}) else try allocator.dupe(u8, raw_target); defer allocator.free(normalized);
const uri = try std.Uri.parse(normalized); var host_buf: [std.Uri.host_name_max]u8 = undefined; const host = try uri.getHost(&host_buf); const use_tls = force_tls or std.mem.eql(u8, uri.scheme, "https"); const port: u16 = uri.port orelse if (use_tls) 443 else 80; const authority = if (std.mem.indexOfScalar(u8, host, ':') != null) blk: { const rendered = try std.fmt.allocPrint(allocator, "[{s}]:{d}", .{ host, port }); defer allocator.free(rendered); break :blk try allocator.dupeZ(u8, rendered); } else blk: { const rendered = try std.fmt.allocPrint(allocator, "{s}:{d}", .{ host, port }); defer allocator.free(rendered); break :blk try allocator.dupeZ(u8, rendered); };
return .{ .authority = authority, .use_tls = use_tls, };}
fn isManagedMetadata(key: []const u8) bool { if (std.ascii.eqlIgnoreCase(key, "content-type")) return true; if (std.ascii.eqlIgnoreCase(key, "te")) return true; return key.len >= 5 and std.ascii.eqlIgnoreCase(key[0..5], "grpc-");}
test "parse target extracts authority and tls choice" { const parsed = try parseTarget(std.testing.allocator, "https://example.internal:8443", false); defer std.testing.allocator.free(parsed.authority);
try std.testing.expect(parsed.use_tls); try std.testing.expectEqualStrings("example.internal:8443", parsed.authority);}
test "grpc managed metadata is filtered from user metadata" { try std.testing.expect(isManagedMetadata("content-type")); try std.testing.expect(isManagedMetadata("grpc-timeout")); try std.testing.expect(!isManagedMetadata("authorization"));}