From 6df2e357ae616076e09dfe83c8fcc6c286bfe87c Mon Sep 17 00:00:00 2001 From: floscodes Date: Sat, 29 Apr 2023 12:59:28 +0200 Subject: [PATCH] add client ip to Response struct, avoid server listen error --- src/server.zig | 20 +++++++------------- src/types.zig | 2 ++ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/server.zig b/src/server.zig index a33f1e4..ec0323d 100644 --- a/src/server.zig +++ b/src/server.zig @@ -24,22 +24,14 @@ pub const Server = struct { defer server.deinit(); const addr = try std.net.Address.parseIp(ip, port); - while (true) { - if (server.listen(addr)) |_| { - break; - } else |_| { - server.close(); - continue; - } - } + try server.listen(addr); // Handling connections while (true) { const conn = if (server.accept()) |conn| conn else |_| continue; defer conn.stream.close(); - // const client_ip = try std.fmt.allocPrint(allocator, "{}", .{conn.address}); - // std.debug.print("Client-IP:{s}\n", .{client_ip}); + const client_ip = try std.fmt.allocPrint(allocator, "{}", .{conn.address}); var buffer = std.ArrayList(u8).init(allocator); defer buffer.deinit(); @@ -56,7 +48,7 @@ pub const Server = struct { // Build the Request const req_stream = try buffer.toOwnedSlice(); defer allocator.free(req_stream); - var req = try buildRequest(req_stream, allocator); + var req = try buildRequest(client_ip, req_stream, allocator); defer allocator.free(req.headers); // if there ist a path set in the uri trim the trailing slash in order to accept it later during the matching check. @@ -90,8 +82,9 @@ pub const Server = struct { }; // Function that build the request from stream -fn buildRequest(bytes: []const u8, allocator: std.mem.Allocator) !Request { +fn buildRequest(client_ip: []const u8, bytes: []const u8, allocator: std.mem.Allocator) !Request { var req: Request = undefined; + req.ip = client_ip; var parts = std.mem.split(u8, bytes, "\r\n"); const header = parts.first(); var header_lines = std.mem.split(u8, header, "\n"); @@ -123,7 +116,8 @@ fn buildRequest(bytes: []const u8, allocator: std.mem.Allocator) !Request { test "build a Request" { const bytes = "GET /test HTTP/1.1\nHost: localhost:8080\nUser-Agent: Testbot\r\nThis is the test body!"; const allocator = std.testing.allocator; - const req = try buildRequest(bytes, allocator); + const client_ip = "127.0.0.1"; + const req = try buildRequest(client_ip, bytes, allocator); defer allocator.free(req.headers); try std.testing.expect(req.method == Method.GET); try std.testing.expect(req.httpVersion == HTTP_Version.HTTP1_1); diff --git a/src/types.zig b/src/types.zig index 024b73e..5125e61 100644 --- a/src/types.zig +++ b/src/types.zig @@ -85,6 +85,8 @@ pub const Request = struct { method: Method, /// HTTP-Version of the Request sent by the client httpVersion: HTTP_Version, + /// Represents the client's IP-Address. + ip: []const u8, /// Represents the request headers sent by the client headers: []const Header, /// The Request URI