From 8a1077a68991c9b3be0b780d27fb4f025899a41f Mon Sep 17 00:00:00 2001 From: Brama Udi Date: Wed, 23 Aug 2023 01:40:07 +0700 Subject: [PATCH] vweb: add a user_agent utility method to the vweb context (#19204) --- vlib/vweb/README.md | 13 +++++++++++++ vlib/vweb/vweb.v | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/vlib/vweb/README.md b/vlib/vweb/README.md index 3c96e0cdac991d..dcb2cfd430b81c 100644 --- a/vlib/vweb/README.md +++ b/vlib/vweb/README.md @@ -1195,6 +1195,19 @@ fn main() { ### Others +#### -user_agent + +Returns the user-agent from the current user + +**Example:** + +```v ignore +pub fn (mut app App) user_agent() vweb.Result { + ua := app.user_agent() + return app.text('User-Agent: $ua') +} +``` + #### -ip Returns the ip address from the current user diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 25ed9eb7e19fb9..8311c2f4df87c9 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -1035,6 +1035,11 @@ pub fn (mut ctx Context) host_serve_static(host string, url string, file_path st ctx.static_hosts[url] = host } +// user_agent returns the user-agent header for the current client +pub fn (ctx &Context) user_agent() string { + return ctx.req.header.get(.user_agent) or { '' } +} + // Returns the ip address from the current user pub fn (ctx &Context) ip() string { mut ip := ctx.req.header.get(.x_forwarded_for) or { '' }