Skip to content

Commit 97340a9

Browse files
authored
veb: fix unset Content-Length header, in responses with an empty body (#26431)
1 parent 15c48d4 commit 97340a9

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎vlib/veb/context.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin
115115
if custom_mimetype != '' {
116116
ctx.res.header.set(.content_type, custom_mimetype)
117117
}
118-
if ctx.res.body != '' {
118+
if !ctx.res.header.contains(.content_length) {
119119
ctx.res.header.set(.content_length, ctx.res.body.len.str())
120120
}
121121
// send veb's closing headers

‎vlib/veb/tests/veb_test.v‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ fn test_host() {
297297
assert x.status() == .ok
298298
}
299299

300+
fn test_empty_response_body_has_content_length() {
301+
req := http.Request{
302+
url: 'http://${localserver}/empty_response_body'
303+
method: .get
304+
}
305+
306+
mut x := req.do()!
307+
assert x.status() == .ok
308+
assert x.header.get(.content_length)! == '0'
309+
}
310+
300311
fn test_http_client_shutdown_does_not_work_without_a_cookie() {
301312
x := http.get('http://${localserver}/shutdown') or {
302313
assert err.msg() == ''

‎vlib/veb/tests/veb_test_server.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ pub fn (mut app ServerApp) with_host(mut ctx ServerContext) veb.Result {
136136
return ctx.ok('')
137137
}
138138

139+
@['/empty_response_body']
140+
pub fn (mut app ServerApp) empty_response_body(mut ctx ServerContext) veb.Result {
141+
return ctx.ok('')
142+
}
143+
139144
pub fn (mut app ServerApp) shutdown(mut ctx ServerContext) veb.Result {
140145
session_key := ctx.get_cookie('skey') or { return ctx.not_found() }
141146
if session_key != 'superman' {

0 commit comments

Comments
 (0)