Skip to content

Commit d07e6d4

Browse files
committed
Reading User-agents
1 parent d435858 commit d07e6d4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/main.zig

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ fn handleRequest(request: *http.Server.Request, allocator: std.mem.Allocator) !v
5252
_ = echo.next();
5353
const respEcho = echo.next().?;
5454
try request.respond(respEcho, .{ .extra_headers = &.{.{ .name = "Content-Type", .value = "text/plain" }} });
55+
} else if (std.mem.startsWith(u8, request.head.target, "/user-agent")) {
56+
var it = request.iterateHeaders();
57+
var respBody: []const u8 = undefined;
58+
while (it.next()) |header| {
59+
if (std.mem.eql(u8, header.name, "User-Agent")) {
60+
respBody = header.value;
61+
}
62+
}
63+
try request.respond(respBody, .{ .extra_headers = &.{.{ .name = "Content-Type", .value = "text/plain" }} });
5564
} else {
5665
try request.respond("", .{ .status = .not_found });
5766
}

0 commit comments

Comments
 (0)