Something went wrong. Try again.
xburner is an experimental HTTP and gRPC load generator written in Zig.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209const std = @import("std");
const config = @import("../config.zig");const http_common = @import("http_exec_common.zig");const http_curl = @import("http_exec_curl.zig");const http_state = @import("http_state.zig");const http_tls = @import("http_exec_tls.zig");const template = @import("../template.zig");const transport = @import("transport.zig");
const http = std.http;
const CookieJar = http_state.CookieJar;
pub const TimeoutPhase = http_common.TimeoutPhase;pub const RequestExecution = http_common.RequestExecution;pub const default_check_body_capture_max_bytes = http_common.default_check_body_capture_max_bytes;pub const TlsContext = http_tls.TlsContext;
pub const describeRequestError = http_common.describeRequestError;pub const effectiveCaptureBodyMaxBytes = http_common.effectiveCaptureBodyMaxBytes;pub const evaluateBuiltInChecks = http_common.evaluateBuiltInChecks;pub const configuredCheckCount = http_common.configuredCheckCount;
pub fn executeRequest( allocator: std.mem.Allocator, client: *http.Client, store: *const template.VarStore, tls_context: *TlsContext, cookie_jar: ?*CookieJar, target: []const u8, method: config.HttpMethod, 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 { 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 null; defer if (rendered_body) |body| allocator.free(body);
const raw_url = try std.fmt.allocPrint(allocator, "{s}{s}", .{ target, rendered_path }); defer allocator.free(raw_url); const normalized_url = try http_tls.normalizeUrlForTls(allocator, raw_url, tls_context.force_tls); defer allocator.free(normalized_url);
const cookie_header = if (cookie_jar) |jar| try jar.headerValue(allocator) else null;
const extra_header_len = header_templates.len + if (cookie_header != null) @as(usize, 1) else 0; var extra_headers = try allocator.alloc(http.Header, extra_header_len); defer allocator.free(extra_headers);
var header_values = try allocator.alloc([]u8, extra_header_len); defer allocator.free(header_values);
for (header_templates, 0..) |header, index| { const value = try template.render(allocator, header.value, store, .header); header_values[index] = value; extra_headers[index] = .{ .name = header.key, .value = value }; } if (cookie_header) |header| { const cookie_index = header_templates.len; header_values[cookie_index] = header; extra_headers[cookie_index] = .{ .name = "Cookie", .value = header }; } defer for (header_values) |value| allocator.free(value);
if (delay_before_ms != 0) { std.Thread.sleep(@as(u64, delay_before_ms) * std.time.ns_per_ms); }
var timer = try std.time.Timer.start(); const uri = try std.Uri.parse(normalized_url);
if (tls_context.shouldUseCurl(uri.scheme)) { return http_curl.executeRequestCurl( allocator, tls_context, cookie_jar, normalized_url, method, extra_headers, rendered_body, connect_timeout_ms, timeout_ms, capture_body_max_bytes, ); }
if (tls_context.shouldUseManual(uri.scheme, connect_timeout_ms)) { return http_tls.executeRequestManualTls( allocator, tls_context, cookie_jar, uri, methodToStd(method), extra_headers, rendered_body, connect_timeout_ms, timeout_ms, capture_body_max_bytes, ); }
if (connect_timeout_ms != 0 and std.mem.eql(u8, uri.scheme, "http")) { return http_tls.executeRequestManualPlain( allocator, cookie_jar, uri, methodToStd(method), extra_headers, rendered_body, connect_timeout_ms, timeout_ms, capture_body_max_bytes, ); }
var request = try client.request(methodToStd(method), uri, .{ .keep_alive = true, .extra_headers = extra_headers, }); defer request.deinit(); try http_tls.applyRequestTimeoutToConnection(request.connection.?.stream_reader.getStream(), timeout_ms);
if (rendered_body) |body| { try request.sendBodyComplete(body); } else { try request.sendBodiless(); }
var response = try request.receiveHead(&.{}); var transfer_buffer: [1024]u8 = undefined; const copied_headers = try http_common.copyResponseHeaders(allocator, response.head); if (cookie_jar) |jar| try jar.updateFromHeaders(copied_headers);
const duration_nanos = timer.read(); const success = response.head.status.class() == .success;
if (capture_body_max_bytes == 0) { const reader = response.reader(&transfer_buffer); _ = reader.discardRemaining() catch {}; return .{ .status_code = @intFromEnum(response.head.status), .duration_nanos = duration_nanos, .bytes_sent = if (rendered_body) |body| body.len else 0, .bytes_received = response.head.content_length orelse 0, .success = success, .headers = copied_headers, }; }
const capture_cap: usize = capture_body_max_bytes; const reader = response.reader(&transfer_buffer); var body_buffer = std.ArrayList(u8).empty; defer body_buffer.deinit(allocator); var body_truncated = false; reader.appendRemaining(allocator, &body_buffer, .limited(capture_cap)) catch |err| switch (err) { error.StreamTooLong => { body_truncated = true; _ = reader.discardRemaining() catch {}; }, else => return err, };
return .{ .status_code = @intFromEnum(response.head.status), .duration_nanos = duration_nanos, .bytes_sent = if (rendered_body) |payload| payload.len else 0, .bytes_received = body_buffer.items.len, .success = success, .body_truncated = body_truncated, .headers = copied_headers, .captured_body = try allocator.dupe(u8, body_buffer.items), };}
pub fn failedRequestExecution(start_nanos: i128, body_template: ?[]const u8, err: anyerror) RequestExecution { return .{ .status_code = 0, .duration_nanos = @intCast(@max(std.time.nanoTimestamp() - start_nanos, 0)), .bytes_sent = if (body_template) |body| body.len else 0, .bytes_received = 0, .success = false, .transport_code = transport.classifyError(err), .timeout_phase = transport.classifyTimeoutPhase(err), };}
fn methodToStd(method: config.HttpMethod) http.Method { return switch (method) { .GET => .GET, .POST => .POST, .PUT => .PUT, .DELETE => .DELETE, };}