fix accept GET query string in URI

main
floscodes 1 year ago
parent c733270df5
commit 3138255bc7

@ -97,6 +97,9 @@ pub const Server = struct {
// PREPARE FOR BUILDING THE RESPONSE // 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 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, "/"); 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 // BUILDING THE RESPONSE
// First initialize a notfound Response that is being changed if a Route path matches with Request URI. // 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. // 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, "/"); if (req_path.len > 1) req_path = std.mem.trimRight(u8, req_path, "/");
// Check if there is a match // 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. // Change response with handling function in case of match.
res = r[1](&req); res = r[1](&req);
// Exit loop in case of match // Exit loop in case of match
@ -119,6 +122,7 @@ pub const Server = struct {
const response_string = try stringifyResponse(res, allocator); const response_string = try stringifyResponse(res, allocator);
// Free memory after writing Response and sending it to client. // Free memory after writing Response and sending it to client.
defer allocator.free(response_string); defer allocator.free(response_string);
// SENDING THE RESPONSE
// Write stringified Response and send it to client. // Write stringified Response and send it to client.
_ = try conn.stream.write(response_string); _ = try conn.stream.write(response_string);
} }

@ -37,7 +37,7 @@ fn handlefn(req: *types.Request) types.Response {
cookies.appendSlice(cookie.value) catch {}; cookies.appendSlice(cookie.value) catch {};
cookies.appendSlice("\n") catch {}; cookies.appendSlice("\n") catch {};
} }
const res_string = std.fmt.allocPrint(alloc, "<h1>Run Server Test OK!</h1><br><h3>Sent headers:</h3><br><pre><code>{s}</code></pre><br><h3>Sent Cookies:</h3><br><pre><code>{s}</code></pre><br><h3>Request body:</h3><br>{s}", .{ headers.items, cookies.items, req.body }) catch "Memory error"; const res_string = std.fmt.allocPrint(alloc, "<h1>Run Server Test OK!</h1><br><h3>URI: {s}</h3><br><h3>Sent headers:</h3><br><pre><code>{s}</code></pre><br><h3>Sent Cookies:</h3><br><pre><code>{s}</code></pre><br><h3>Request body:</h3><br>{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 }} }; var res = types.Response{ .body = res_string, .cookies = &[_]Response.Cookie{.{ .name = "Test-Cookie", .value = "Test", .maxAge = 60 * 3 }} };
return res; return res;
} }

Loading…
Cancel
Save