|
| 1 | +const std = @import("std"); |
| 2 | +const http = std.http; |
| 3 | +const ArrayList = std.ArrayList; |
| 4 | +const Allocator = std.mem.Allocator; |
| 5 | +const Uri = std.Uri; |
| 6 | +const httpfiles = @import("./parser"); |
| 7 | + |
| 8 | +pub const HttpResponse = struct { |
| 9 | + status_code: u16, |
| 10 | + headers: ArrayList(http.Header), |
| 11 | + body: []u8, |
| 12 | + allocator: Allocator, |
| 13 | + |
| 14 | + pub fn init(allocator: Allocator) HttpResponse { |
| 15 | + return .{ |
| 16 | + .status_code = 0, |
| 17 | + .headers = ArrayList(http.Header).init(allocator), |
| 18 | + .body = &[_]u8{}, |
| 19 | + .allocator = allocator, |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + pub fn deinit(self: *HttpResponse) void { |
| 24 | + for (self.headers.items) |header| { |
| 25 | + self.allocator.free(header.name); |
| 26 | + self.allocator.free(header.value); |
| 27 | + } |
| 28 | + self.headers.deinit(); |
| 29 | + if (self.body.len > 0) { |
| 30 | + self.allocator.free(self.body); |
| 31 | + } |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +pub const HttpClient = struct { |
| 36 | + allocator: Allocator, |
| 37 | + client: http.Client, |
| 38 | + |
| 39 | + pub fn init(allocator: Allocator) HttpClient { |
| 40 | + return .{ |
| 41 | + .allocator = allocator, |
| 42 | + .client = http.Client{ .allocator = allocator }, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + pub fn deinit(self: *HttpClient) void { |
| 47 | + self.client.deinit(); |
| 48 | + } |
| 49 | + |
| 50 | + pub fn execute(self: *HttpClient, request: *const httpfiles.HttpRequest) !HttpResponse { |
| 51 | + if (request.method == null) { |
| 52 | + return error.MissingHttpMethod; |
| 53 | + } |
| 54 | + |
| 55 | + // Parse the URL |
| 56 | + const uri = try Uri.parse(request.url); |
| 57 | + |
| 58 | + // Create the HTTP request |
| 59 | + var req = try self.client.open(request.method.?.toStdMethod(), uri, .{ |
| 60 | + .server_header_buffer = try self.allocator.alloc(u8, 8192), |
| 61 | + }); |
| 62 | + defer req.deinit(); |
| 63 | + |
| 64 | + // Add headers |
| 65 | + for (request.headers.items) |header| { |
| 66 | + try req.headers.append(header.name, header.value); |
| 67 | + } |
| 68 | + |
| 69 | + // Set content length if we have a body |
| 70 | + if (request.body) |body| { |
| 71 | + req.transfer_encoding = .{ .content_length = body.len }; |
| 72 | + } else { |
| 73 | + req.transfer_encoding = .{ .content_length = 0 }; |
| 74 | + } |
| 75 | + |
| 76 | + // Send the request |
| 77 | + try req.send(); |
| 78 | + |
| 79 | + // Send body if present |
| 80 | + if (request.body) |body| { |
| 81 | + try req.writeAll(body); |
| 82 | + } |
| 83 | + |
| 84 | + try req.finish(); |
| 85 | + |
| 86 | + // Wait for response |
| 87 | + try req.wait(); |
| 88 | + |
| 89 | + // Read the response |
| 90 | + var response = HttpResponse.init(self.allocator); |
| 91 | + response.status_code = req.response.status.code(); |
| 92 | + |
| 93 | + // Copy response headers |
| 94 | + var header_iterator = req.response.iterateHeaders(); |
| 95 | + while (header_iterator.next()) |header| { |
| 96 | + const name = try self.allocator.dupe(u8, header.name); |
| 97 | + const value = try self.allocator.dupe(u8, header.value); |
| 98 | + try response.headers.append(.{ .name = name, .value = value }); |
| 99 | + } |
| 100 | + |
| 101 | + // Read response body |
| 102 | + const body_reader = req.reader(); |
| 103 | + const body_content = try body_reader.readAllAlloc(self.allocator, std.math.maxInt(usize)); |
| 104 | + response.body = body_content; |
| 105 | + |
| 106 | + return response; |
| 107 | + } |
| 108 | + |
| 109 | + pub fn executeRequests(self: *HttpClient, requests: []const httpfiles.HttpRequest) !ArrayList(HttpResponse) { |
| 110 | + var responses = ArrayList(HttpResponse).init(self.allocator); |
| 111 | + errdefer { |
| 112 | + for (responses.items) |*response| { |
| 113 | + response.deinit(); |
| 114 | + } |
| 115 | + responses.deinit(); |
| 116 | + } |
| 117 | + |
| 118 | + for (requests) |*request| { |
| 119 | + const response = try self.execute(request); |
| 120 | + try responses.append(response); |
| 121 | + } |
| 122 | + |
| 123 | + return responses; |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +// Utility function to print response details |
| 128 | +pub fn printResponse(response: *const HttpResponse) void { |
| 129 | + std.debug.print("Status: {d}\n", .{response.status_code}); |
| 130 | + std.debug.print("Headers:\n"); |
| 131 | + for (response.headers.items) |header| { |
| 132 | + std.debug.print(" {s}: {s}\n", .{ header.name, header.value }); |
| 133 | + } |
| 134 | + std.debug.print("Body ({d} bytes):\n{s}\n", .{ response.body.len, response.body }); |
| 135 | + std.debug.print("---\n"); |
| 136 | +} |
| 137 | + |
| 138 | +test "HttpClient basic functionality" { |
| 139 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 140 | + defer _ = gpa.deinit(); |
| 141 | + const allocator = gpa.allocator(); |
| 142 | + |
| 143 | + var client = HttpClient.init(allocator); |
| 144 | + defer client.deinit(); |
| 145 | + |
| 146 | + // Create a simple GET request |
| 147 | + var request = httpfiles.HttpRequest.init(allocator); |
| 148 | + defer request.deinit(allocator); |
| 149 | + |
| 150 | + request.method = .GET; |
| 151 | + request.url = "https://httpbin.org/status/200"; |
| 152 | + |
| 153 | + // Execute the request |
| 154 | + var response = try client.execute(&request); |
| 155 | + defer response.deinit(); |
| 156 | + |
| 157 | + // Check that we got a 200 status |
| 158 | + try std.testing.expectEqual(@as(u16, 200), response.status_code); |
| 159 | +} |
0 commit comments