1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: refactor HTTP request parsing (#8874)

This commit is contained in:
Miccah
2021-02-27 16:18:25 -06:00
committed by GitHub
parent 2f9687d29b
commit 7e08e84bc1
7 changed files with 225 additions and 151 deletions

View File

@ -16,8 +16,10 @@ const (
pub struct Request {
pub mut:
version Version = .v1_1
method Method
headers map[string]string
headers map[string]string // original requset headers
lheaders map[string]string // same as headers, but with normalized lowercased keys (for received requests)
cookies map[string]string
data string
url string
@ -340,7 +342,8 @@ fn (req &Request) build_request_headers(method Method, host_name string, path st
uheaders << '$key: $val\r\n'
}
uheaders << req.build_request_cookies_header()
return '$method $path HTTP/1.1\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' + req.data
version := if req.version == .unknown { Version.v1_1 } else { req.version }
return '$method $path $version\r\n' + uheaders.join('') + 'Connection: close\r\n\r\n' + req.data
}
fn (req &Request) build_request_cookies_header() string {

30
vlib/net/http/version.v Normal file
View File

@ -0,0 +1,30 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module http
// The versions listed here are the most common ones.
pub enum Version {
unknown
v1_1
v2_0
v1_0
}
pub fn (v Version) str() string {
return match v {
.v1_1 { 'HTTP/1.1' }
.v2_0 { 'HTTP/2.0' }
.v1_0 { 'HTTP/1.0' }
.unknown { 'unknown' }
}
}
pub fn version_from_str(v string) Version {
return match v.to_lower() {
'http/1.1' { Version.v1_1 }
'http/2.0' { Version.v2_0 }
'http/1.0' { Version.v1_0 }
else { Version.unknown }
}
}