Skip to content

Commit

Permalink
vweb: add a user_agent utility method to the vweb context (#19204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramaudi committed Aug 22, 2023
1 parent ffea6eb commit 8a1077a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions vlib/vweb/README.md
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions vlib/vweb/vweb.v
Expand Up @@ -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 { '' }
Expand Down

0 comments on commit 8a1077a

Please sign in to comment.