All MicroEvals
const std = @import("std"); const sock = std.posix; pub con...
Create MicroEval
Header image for const std = @import("std");
const sock = std.posix;

pub con...

const std = @import("std"); const sock = std.posix; pub con...

Prompt

const std = @import("std"); const sock = std.posix; pub const Response = struct { status: u16, headers: std.StringHashMap([]const u8), body: []const u8, }; pub const HttpClient = struct { host: []const u8, port: u16, path: []const u8, use_tls: bool, ssl_ctx: ?*SSL_CTX = null, timeout_ms: u64, const SSL_CTX = opaque {}; const SSL = opaque {}; extern fn SSL_CTX_new(method: ?*anyopaque) ?*SSL_CTX; extern fn SSL_CTX_free(ctx: ?*SSL_CTX) void; extern fn SSL_new(ctx: ?*SSL_CTX) ?*SSL; extern fn SSL_free(ssl: ?*SSL) void; extern fn SSL_set_fd(ssl: ?*SSL, fd: c_int) c_int; extern fn SSL_connect(ssl: ?*SSL) c_int; extern fn SSL_read(ssl: ?*SSL, buf: [*]u8, len: c_int) c_int; extern fn SSL_write(ssl: ?*SSL, buf: [*]const u8, len: c_int) c_int; extern fn SSL_CTX_set_verify(ctx: ?*SSL_CTX, mode: c_int, verify_cb: ?*const anyopaque) void; extern fn OPENSSL_init_ssl(opts: c_ulong, settings: ?*anyopaque) c_int; pub fn init(url: []const u8, timeout_ms: u64) !HttpClient { try OPENSSL_init_ssl(0, null); var host: [256]u8 = undefined; var port: [8]u8 = undefined; var path: [4096]u8 = undefined; var use_tls: bool = undefined; const parsed = try parseUrl(url, &host, &port, &path, &use_tls); var ssl_ctx: ?*SSL_CTX = null; if (use_tls) { ssl_ctx = SSL_CTX_new(null); if (ssl_ctx) |ctx| { SSL_CTX_set_verify(ctx, 0, null); } } return HttpClient{ .host = parsed.host, .port = parsed.port, .path = parsed.path, .use_tls = use_tls, .ssl_ctx = ssl_ctx, .timeout_ms = timeout_ms, }; } pub fn deinit(self: *HttpClient) void { if (self.ssl_ctx) |ctx| { SSL_CTX_free(ctx); } } pub fn get(self: *const HttpClient, path: []const u8, headers: []const []const u8, allocator: std.mem.Allocator) !Response { return self.request("GET", path, null, "application/octet-stream", headers, allocator); } pub fn post(self: *const HttpClient, path: []const u8, body: []const u8, content_type: []const u8, headers: []const []const u8, allocator: std.mem.Allocator) !Response { return self.request("POST", path, body, content_type, headers, allocator); } fn request(self: *const HttpClient, method: []const u8, req_path: []const u8, body: ?[]const u8, content_type: []const u8, extra_headers: []const []const u8, allocator: std.mem.Allocator) !Response { const sock_type: c_int = sock.SOCK_STREAM; const protocol: c_int = if (self.use_tls) 0 else sock.IPPROTO_TCP; var hints: sock.addrinfo_hints = .{ .family = .inet, .socktype = sock_type, .protocol = protocol, .address_info = .{}, }; const port_str = try std.fmt.allocPrint(allocator, "{d}", .{self.port}); defer allocator.free(port_str); const addr_info = try sock.getaddrinfo(self.host, port_str, &hints); defer addr_info.deinit(); if (addr_info.addrs.len == 0) return error.NoAddressesFound; const addr = addr_info.addrs[0].addr; const fd = try sock.socket(addr_info.addrs[0].family, sock_type, protocol); defer sock.close(fd); var timeout: std.posix.timeval = .{ .tv_sec = @intCast(self.timeout_ms / 1000), .tv_usec = @intCast((self.timeout_ms % 1000) * 1000), }; try sock.setsockopt(fd, sock.SOL.SOCKET, sock.SO.RCVTIMEO, std.mem.asBytes(&timeout)); try sock.setsockopt(fd, sock.SOL.SOCKET, sock.SO.SNDTIMEO, std.mem.asBytes(&timeout)); try sock.connect(fd, &addr); var ssl: ?*SSL = null; defer { if (ssl) |s| SSL_free(s); } if (self.use_tls and self.ssl_ctx) |ctx| { ssl = SSL_new(ctx); if (ssl) |s| { try sock.setsockopt(fd, sock.SOL.SOCKET, sock.SO.KEEPALIVE, std.mem.asBytes(&@as(c_int, 1))); _ = SSL_set_fd(s, fd); _ = SSL_connect(s); } } var req_buffer = std.ArrayList(u8).init(allocator); defer req_buffer.deinit(); try req_buffer.writer().print("{s} {s} HTTP/1.1\r\n", .{method, req_path}); try req_buffer.writer().print("Host: {s}\r\n", .{self.host}); try req_buffer.writer().print("User-Agent: SearchPlatformBot/0.1\r\n", .{}); if (body) |b| { try req_buffer.writer().print("Content-Type: {s}\r\n", .{content_type}); try req_buffer.writer().print("Content-Length: {d}\r\n", .{b.len}); } for (0..extra_headers.len / 2) |i| { try req_buffer.writer().print("{s}: {s}\r\n", .{ extra_headers[i * 2], extra_headers[i * 2 + 1] }); } try req_buffer.appendSlice("Connection: close\r\n"); try req_buffer.appendSlice("\r\n"); if (body) |b| { try req_buffer.appendSlice(b); } const write_fn = if (ssl) |s| struct { fn write(ssl: *SSL, data: []const u8) !void { var total: usize = 0; while (total < data.len) { const written = SSL_write(ssl, data[total..].ptr, @intCast(data.len - total)); if (written <= 0) return error.WriteFailed; total += @as(usize, @intCast(written)); } } }.write else struct { fn write(fd: std.posix.fd_t, data: []const u8) !void { try sock.send(fd, data); } }.write; if (ssl) |s| { try write_fn(s, req_buffer.items); } else { try write_fn(fd, req_buffer.items); } var resp_buffer = std.ArrayList(u8).init(allocator); defer resp_buffer.deinit(); const read_fn = if (ssl) |s| struct { fn read(ssl: *SSL, buf: *[8192]u8) !usize { const n = SSL_read(ssl, buf, 8192); if (n <= 0) return error.ReadFailed; return @as(usize, @intCast(n)); } }.read else struct { fn read(fd: std.posix.fd_t, buf: *[8192]u8) !usize { return sock.read(fd, buf); } }.read; while (true) { var buf: [8192]u8 = undefined; const n = if (ssl) |s| try read_fn(s, &buf) else try read_fn(fd, &buf); if (n == 0) break; try resp_buffer.appendSlice(buf[0..n]); } return try parseHttpResponse(resp_buffer.items, allocator); } fn parseUrl(url: []const u8, host: *[256]u8, port: *[8]u8, path: *[4096]u8, use_tls: *bool) !struct { host: []const u8, port: u16, path: []const u8 } { if (std.mem.startsWith(u8, url, "https://")) { use_tls.* = true; var rest = url["https://".len..]; const path_start = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; const host_part = rest[0..path_start]; const colon_idx = std.mem.indexOfScalar(u8, host_part, ':'); if (colon_idx) |idx| { @memcpy(host[0..idx], host_part[0..idx]); const port_str = host_part[idx + 1..]; @memcpy(port[0..port_str.len], port_str); port[port_str.len] = 0; } else { @memcpy(host[0..host_part.len], host_part); @memcpy(port, "443"); } const path_part = if (path_start < rest.len) rest[path_start..] else "/"; @memcpy(path[0..path_part.len], path_part); return .{ .host = host[0..std.mem.indexOfScalar(u8, host, 0).?], .port = try std.fmt.parseInt(u16, port[0..std.mem.indexOfScalar(u8, port, 0).?], 10), .path = path[0..std.mem.indexOfScalar(u8, path, 0).?], }; } else if (std.mem.startsWith(u8, url, "http://")) { use_tls.* = false; var rest = url["http://".len..]; const path_start = std.mem.indexOfScalar(u8, rest, '/') orelse rest.len; const host_part = rest[0..path_start]; const colon_idx = std.mem.indexOfScalar(u8, host_part, ':'); if (colon_idx) |idx| { @memcpy(host[0..idx], host_part[0..idx]); const port_str = host_part[idx + 1..]; @memcpy(port[0..port_str.len], port_str); port[port_str.len] = 0; } else { @memcpy(host[0..host_part.len], host_part); @memcpy(port, "80"); } const path_part = if (path_start < rest.len) rest[path_start..] else "/"; @memcpy(path[0..path_part.len], path_part); return .{ .host = host[0..std.mem.indexOfScalar(u8, host, 0).?], .port = try std.fmt.parseInt(u16, port[0..std.mem.indexOfScalar(u8, port, 0).?], 10), .path = path[0..std.mem.indexOfScalar(u8, path, 0).?], }; } return error.InvalidUrl; } fn parseHttpResponse(data: []const u8, allocator: std.mem.Allocator) !Response { const header_end = std.mem.indexOf(u8, data, "\r\n\r\n") orelse return error.InvalidResponse; const status_line = data[0..header_end]; var headers = std.StringHashMap([]const u8).init(allocator); errdefer headers.deinit(); const lines = std.mem.split(u8, status_line, "\r\n"); const first_line = lines.next() orelse return error.InvalidResponse; const parts = std.mem.split(u8, first_line, " "); _ = parts.next(); const status_str = parts.next() orelse return error.InvalidResponse; const status = try std.fmt.parseInt(u16, status_str, 10); while (lines.next()) |line| { const colon_idx = std.mem.indexOfScalar(u8, line, ':') orelse continue; const key = std.mem.trim(u8, line[0..colon_idx], " "); const value = std.mem.trim(u8, line[colon_idx + 1..], " "); try headers.put(try allocator.dupe(u8, key), try allocator.dupe(u8, value)); } var body = data[header_end + 4..]; if (headers.get("Content-Encoding")) |encoding| { if (std.mem.eql(u8, encoding, "gzip")) { body = try decompressGzip(body, allocator); } else if (std.mem.eql(u8, encoding, "deflate")) { body = try decompressDeflate(body, allocator); } } return Response{ .status = status, .headers = headers, .body = body, }; } fn decompressGzip(data: []const u8, allocator: std.mem.Allocator) ![]u8 { return try decompressDeflate(data[10..], allocator); } fn decompressDeflate(data: []const u8, allocator: std.mem.Allocator) ![]u8 { var out = std.ArrayList(u8).init(allocator); defer out.deinit(); return out.toOwnedSlice(); } }; Send back the complete code with all the fixes. Fix each of the listed errors one by one, making sure to actually correct them so that there are 0 errors remaining. Keep the original imports, since the files exist. Write out every single character; do not abbreviate anything. Fix every error. There must be exactly one file. Do not write anything else; just output the complete code, and it must not contain any comments. Never, under any circumstances, use simplified, substitute, dummy, simulated, or fake code. Write the entire file as complete, unabridged, production-ready code in a single code block. It must be 100% error-free, a complete, error-free file, and must be submitted as a downloadable file. These requirements are mandatory and must be strictly adhered to. If no list of errors is provided, you must find all the errors and fix them. If there were comments in the original code, delete them. And most importantly: YOU MUST NEVER SIMPLIFY!

Drag to resize
Drag to resize
Drag to resize