mirror of
https://github.com/vlang/v.git
synced 2025-09-11 16:36:20 -04:00
vweb: add an optional parameter to the .redirect/2 method, to be able to set the http code for the redirects (#20082)
This commit is contained in:
parent
d72264ccb0
commit
d9e9c71b29
@ -33,6 +33,11 @@ pub const http_302 = http.new_response(
|
|||||||
body: '302 Found'
|
body: '302 Found'
|
||||||
header: headers_close
|
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(
|
pub const http_400 = http.new_response(
|
||||||
status: .bad_request
|
status: .bad_request
|
||||||
body: '400 Bad Request'
|
body: '400 Bad Request'
|
||||||
@ -303,13 +308,21 @@ pub fn (mut ctx Context) server_error(ecode int) Result {
|
|||||||
return Result{}
|
return Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
pub struct RedirectParams {
|
||||||
|
status_code int = 302
|
||||||
|
}
|
||||||
|
|
||||||
// Redirect to an url
|
// 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 {
|
if ctx.done {
|
||||||
return Result{}
|
return Result{}
|
||||||
}
|
}
|
||||||
ctx.done = true
|
ctx.done = true
|
||||||
mut resp := vweb.http_302
|
mut resp := vweb.http_302
|
||||||
|
if params.status_code == 303 {
|
||||||
|
resp = vweb.http_303
|
||||||
|
}
|
||||||
resp.header = resp.header.join(ctx.header)
|
resp.header = resp.header.join(ctx.header)
|
||||||
resp.header.add(.location, url)
|
resp.header.add(.location, url)
|
||||||
send_string(mut ctx.conn, resp.bytestr()) or { return Result{} }
|
send_string(mut ctx.conn, resp.bytestr()) or { return Result{} }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user