const std = @import("std"); const abi = @import("../abi.zig"); const config = @import("../config.zig"); const http_state = @import("http_state.zig"); const transport = @import("transport.zig"); const http = std.http; const OwnedHeader = http_state.OwnedHeader; const clearOwnedHeaders = http_state.clearOwnedHeaders; pub const TimeoutPhase = transport.TimeoutPhase; pub const RequestExecution = struct { status_code: u16, duration_nanos: u64, bytes_sent: u64, bytes_received: u64, success: bool, checks_passed: u32 = 0, checks_failed: u32 = 0, transport_code: abi.TransportCode = .none, timeout_phase: TimeoutPhase = .none, grpc_status: ?u8 = null, deadline_exceeded: bool = false, grpc_message: ?[]u8 = null, body_truncated: bool = false, headers: []OwnedHeader = &.{}, captured_body: ?[]u8 = null, pub fn deinit(self: *RequestExecution, allocator: std.mem.Allocator) void { if (self.captured_body) |body| allocator.free(body); if (self.grpc_message) |message| allocator.free(message); clearOwnedHeaders(allocator, self.headers); self.* = undefined; } }; pub const default_check_body_capture_max_bytes: u32 = 64 * 1024; pub fn describeRequestError(allocator: std.mem.Allocator, err: anyerror, http_config: *const config.HttpConfig) ![]u8 { if (isFdExhaustionError(err)) { if (http_config.max_connections) |limit| { return try std.fmt.allocPrint( allocator, "{s}; consider lowering http.max_connections (current {d}) to cap pooled TLS connections", .{ @errorName(err), limit }, ); } return try std.fmt.allocPrint( allocator, "{s}; consider setting http.max_connections to cap pooled TLS connections", .{@errorName(err)}, ); } return try allocator.dupe(u8, @errorName(err)); } pub fn effectiveCaptureBodyMaxBytes(request: *const config.HttpRequestTemplate) u32 { if (request.check_body_contains != null and request.capture_body_max_bytes == 0) { return default_check_body_capture_max_bytes; } return request.capture_body_max_bytes; } pub fn evaluateBuiltInChecks(request: *const config.HttpRequestTemplate, execution: *RequestExecution) void { var passed: u32 = 0; var failed: u32 = 0; if (request.check_status_code) |expected_status| { if (execution.status_code == expected_status) { passed += 1; } else { failed += 1; } } if (request.check_body_contains) |needle| { const body = execution.captured_body orelse ""; if (std.mem.indexOf(u8, body, needle) != null) { passed += 1; } else { failed += 1; } } if (request.check_response_header) |header_name| { if (hasResponseHeader(execution.headers, header_name)) { passed += 1; } else { failed += 1; } } execution.checks_passed = passed; execution.checks_failed = failed; if (failed != 0) execution.success = false; } pub fn configuredCheckCount(request: *const config.HttpRequestTemplate) u32 { var count: u32 = 0; if (request.check_status_code != null) count += 1; if (request.check_body_contains != null) count += 1; if (request.check_response_header != null) count += 1; return count; } pub fn copyResponseHeaders(allocator: std.mem.Allocator, head: http.Client.Response.Head) ![]OwnedHeader { var iterator = head.iterateHeaders(); var headers = std.ArrayList(OwnedHeader).empty; defer headers.deinit(allocator); while (iterator.next()) |header| { try headers.append(allocator, .{ .name = try allocator.dupe(u8, header.name), .value = try allocator.dupe(u8, header.value), }); } return try headers.toOwnedSlice(allocator); } fn hasResponseHeader(headers: []const OwnedHeader, name: []const u8) bool { for (headers) |header| { if (std.ascii.eqlIgnoreCase(header.name, name)) return true; } return false; } fn isFdExhaustionError(err: anyerror) bool { return switch (err) { error.ProcessFdQuotaExceeded, error.SystemFdQuotaExceeded, => true, else => false, }; } test "fd exhaustion hint recommends max_connections" { const allocator = std.testing.allocator; const without_limit = try describeRequestError(allocator, error.ProcessFdQuotaExceeded, &.{ .max_connections = null, .defaults = .{}, .requests = &.{}, }); defer allocator.free(without_limit); try std.testing.expect(std.mem.indexOf(u8, without_limit, "http.max_connections") != null); const with_limit = try describeRequestError(allocator, error.SystemFdQuotaExceeded, &.{ .max_connections = 16, .defaults = .{}, .requests = &.{}, }); defer allocator.free(with_limit); try std.testing.expect(std.mem.indexOf(u8, with_limit, "current 16") != null); } test "built-in checks update execution success and counters" { var execution = RequestExecution{ .status_code = 200, .duration_nanos = 1, .bytes_sent = 0, .bytes_received = 2, .success = true, .headers = try std.testing.allocator.dupe(OwnedHeader, &.{.{ .name = try std.testing.allocator.dupe(u8, "x-trace-id"), .value = try std.testing.allocator.dupe(u8, "abc"), }}), .captured_body = try std.testing.allocator.dupe(u8, "ok"), }; defer execution.deinit(std.testing.allocator); const request = config.HttpRequestTemplate{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{}, .check_status_code = 200, .check_body_contains = "ok", .check_response_header = "x-trace-id", }; evaluateBuiltInChecks(&request, &execution); try std.testing.expect(execution.success); try std.testing.expectEqual(@as(u32, 3), execution.checks_passed); try std.testing.expectEqual(@as(u32, 0), execution.checks_failed); } test "effective capture size promotes body checks" { const request = config.HttpRequestTemplate{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{}, .check_body_contains = "ok", }; try std.testing.expectEqual(default_check_body_capture_max_bytes, effectiveCaptureBodyMaxBytes(&request)); try std.testing.expectEqual(@as(u32, 3), configuredCheckCount(&.{ .id = 1, .name = "health", .method = .GET, .path = "/health", .headers = &.{}, .check_status_code = 200, .check_body_contains = "ok", .check_response_header = "x-trace-id", })); }