From 3138255bc76927c7b488708de81eca5c2c89601d Mon Sep 17 00:00:00 2001 From: floscodes Date: Fri, 19 May 2023 14:34:56 +0200 Subject: [PATCH] fix accept GET query string in URI --- src/server.zig | 6 +++++- src/zerve.zig | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server.zig b/src/server.zig index 947cb88..bbff6f0 100644 --- a/src/server.zig +++ b/src/server.zig @@ -97,6 +97,9 @@ pub const Server = struct { // PREPARE FOR BUILDING THE RESPONSE // if there ist a path set in the uri trim the trailing slash in order to accept it later during the matching check. if (req.uri.len > 1) req.uri = std.mem.trimRight(u8, req.uri, "/"); + // Declare new URI variable and cut off a possible request string in order to accept it in a GET Request + var uri_parts = std.mem.split(u8, req.uri, "?"); + const uri_string = uri_parts.first(); // BUILDING THE RESPONSE // First initialize a notfound Response that is being changed if a Route path matches with Request URI. @@ -108,7 +111,7 @@ pub const Server = struct { // Trim a possible trailing slash from Route path in order to accept it during the matching process. if (req_path.len > 1) req_path = std.mem.trimRight(u8, req_path, "/"); // Check if there is a match - if (eql(u8, req_path, req.uri)) { + if (eql(u8, req_path, uri_string)) { // Change response with handling function in case of match. res = r[1](&req); // Exit loop in case of match @@ -119,6 +122,7 @@ pub const Server = struct { const response_string = try stringifyResponse(res, allocator); // Free memory after writing Response and sending it to client. defer allocator.free(response_string); + // SENDING THE RESPONSE // Write stringified Response and send it to client. _ = try conn.stream.write(response_string); } diff --git a/src/zerve.zig b/src/zerve.zig index bbe6397..1ca0760 100644 --- a/src/zerve.zig +++ b/src/zerve.zig @@ -37,7 +37,7 @@ fn handlefn(req: *types.Request) types.Response { cookies.appendSlice(cookie.value) catch {}; cookies.appendSlice("\n") catch {}; } - const res_string = std.fmt.allocPrint(alloc, "

Run Server Test OK!


Sent headers:


{s}

Sent Cookies:


{s}

Request body:


{s}", .{ headers.items, cookies.items, req.body }) catch "Memory error"; + const res_string = std.fmt.allocPrint(alloc, "

Run Server Test OK!


URI: {s}


Sent headers:


{s}

Sent Cookies:


{s}

Request body:


{s}", .{ req.uri, headers.items, cookies.items, req.body }) catch "Memory error"; var res = types.Response{ .body = res_string, .cookies = &[_]Response.Cookie{.{ .name = "Test-Cookie", .value = "Test", .maxAge = 60 * 3 }} }; return res; }