add client ip to Response struct, avoid server listen error

This commit is contained in:
floscodes 2023-04-29 12:59:28 +02:00
parent e7960eba28
commit 6df2e357ae
2 changed files with 9 additions and 13 deletions

View file

@ -24,22 +24,14 @@ pub const Server = struct {
defer server.deinit(); defer server.deinit();
const addr = try std.net.Address.parseIp(ip, port); const addr = try std.net.Address.parseIp(ip, port);
while (true) { try server.listen(addr);
if (server.listen(addr)) |_| {
break;
} else |_| {
server.close();
continue;
}
}
// Handling connections // Handling connections
while (true) { while (true) {
const conn = if (server.accept()) |conn| conn else |_| continue; const conn = if (server.accept()) |conn| conn else |_| continue;
defer conn.stream.close(); defer conn.stream.close();
// const client_ip = try std.fmt.allocPrint(allocator, "{}", .{conn.address}); const client_ip = try std.fmt.allocPrint(allocator, "{}", .{conn.address});
// std.debug.print("Client-IP:{s}\n", .{client_ip});
var buffer = std.ArrayList(u8).init(allocator); var buffer = std.ArrayList(u8).init(allocator);
defer buffer.deinit(); defer buffer.deinit();
@ -56,7 +48,7 @@ pub const Server = struct {
// Build the Request // Build the Request
const req_stream = try buffer.toOwnedSlice(); const req_stream = try buffer.toOwnedSlice();
defer allocator.free(req_stream); 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); 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. // 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 // 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; var req: Request = undefined;
req.ip = client_ip;
var parts = std.mem.split(u8, bytes, "\r\n"); var parts = std.mem.split(u8, bytes, "\r\n");
const header = parts.first(); const header = parts.first();
var header_lines = std.mem.split(u8, header, "\n"); 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" { test "build a Request" {
const bytes = "GET /test HTTP/1.1\nHost: localhost:8080\nUser-Agent: Testbot\r\nThis is the test body!"; 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 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); defer allocator.free(req.headers);
try std.testing.expect(req.method == Method.GET); try std.testing.expect(req.method == Method.GET);
try std.testing.expect(req.httpVersion == HTTP_Version.HTTP1_1); try std.testing.expect(req.httpVersion == HTTP_Version.HTTP1_1);

View file

@ -85,6 +85,8 @@ pub const Request = struct {
method: Method, method: Method,
/// HTTP-Version of the Request sent by the client /// HTTP-Version of the Request sent by the client
httpVersion: HTTP_Version, httpVersion: HTTP_Version,
/// Represents the client's IP-Address.
ip: []const u8,
/// Represents the request headers sent by the client /// Represents the request headers sent by the client
headers: []const Header, headers: []const Header,
/// The Request URI /// The Request URI