Skip to content

Commit

Permalink
vweb: add an optional parameter to the .redirect/2 method, to be able…
Browse files Browse the repository at this point in the history
… to set the http code for the redirects (#20082)
  • Loading branch information
balloondude2 committed Dec 4, 2023
1 parent d72264c commit d9e9c71
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion vlib/vweb/vweb.v
Expand Up @@ -33,6 +33,11 @@ pub const http_302 = http.new_response(
body: '302 Found'
header: headers_close
)
pub const http_303 = http.new_response(
status: .see_other
body: '303 See Other'
header: headers_close
)
pub const http_400 = http.new_response(
status: .bad_request
body: '400 Bad Request'
Expand Down Expand Up @@ -303,13 +308,21 @@ pub fn (mut ctx Context) server_error(ecode int) Result {
return Result{}
}

@[params]
pub struct RedirectParams {
status_code int = 302
}

// Redirect to an url
pub fn (mut ctx Context) redirect(url string) Result {
pub fn (mut ctx Context) redirect(url string, params RedirectParams) Result {
if ctx.done {
return Result{}
}
ctx.done = true
mut resp := vweb.http_302
if params.status_code == 303 {
resp = vweb.http_303
}
resp.header = resp.header.join(ctx.header)
resp.header.add(.location, url)
send_string(mut ctx.conn, resp.bytestr()) or { return Result{} }
Expand Down

0 comments on commit d9e9c71

Please sign in to comment.