Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

vweb: add user-agent utility method called ua #19204

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions vlib/vweb/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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