mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
http: use Header struct for headers (#9462)
This commit is contained in:
@ -19,8 +19,7 @@ pub struct Request {
|
||||
pub mut:
|
||||
version Version = .v1_1
|
||||
method Method
|
||||
headers map[string]string // original requset headers
|
||||
lheaders map[string]string // same as headers, but with normalized lowercased keys (for received requests)
|
||||
header Header
|
||||
cookies map[string]string
|
||||
data string
|
||||
url string
|
||||
@ -34,9 +33,9 @@ pub mut:
|
||||
pub struct FetchConfig {
|
||||
pub mut:
|
||||
method Method
|
||||
header Header
|
||||
data string
|
||||
params map[string]string
|
||||
headers map[string]string
|
||||
cookies map[string]string
|
||||
user_agent string = 'v.http'
|
||||
verbose bool
|
||||
@ -46,8 +45,7 @@ pub mut:
|
||||
pub struct Response {
|
||||
pub:
|
||||
text string
|
||||
headers map[string]string // original response headers, 'Set-Cookie' or 'set-Cookie', etc.
|
||||
lheaders map[string]string // same as headers, but with normalized lowercased keys, like 'set-cookie'
|
||||
header Header
|
||||
cookies map[string]string
|
||||
status_code int
|
||||
}
|
||||
@ -76,9 +74,7 @@ pub fn get(url string) ?Response {
|
||||
pub fn post(url string, data string) ?Response {
|
||||
return fetch_with_method(.post, url,
|
||||
data: data
|
||||
headers: map{
|
||||
'Content-Type': http.content_type_default
|
||||
}
|
||||
header: new_header({key: .content_type, value: http.content_type_default})
|
||||
)
|
||||
}
|
||||
|
||||
@ -86,18 +82,14 @@ pub fn post(url string, data string) ?Response {
|
||||
pub fn post_json(url string, data string) ?Response {
|
||||
return fetch_with_method(.post, url,
|
||||
data: data
|
||||
headers: map{
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
header: new_header({key: .content_type, value: 'application/json'})
|
||||
)
|
||||
}
|
||||
|
||||
// post_form sends a POST HTTP request to the URL with X-WWW-FORM-URLENCODED data
|
||||
pub fn post_form(url string, data map[string]string) ?Response {
|
||||
return fetch_with_method(.post, url,
|
||||
headers: map{
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
return fetch_with_method(.post, url,
|
||||
header: new_header({key: .content_type, value: 'application/x-www-form-urlencoded'})
|
||||
data: url_encode_form_data(data)
|
||||
)
|
||||
}
|
||||
@ -106,9 +98,7 @@ pub fn post_form(url string, data map[string]string) ?Response {
|
||||
pub fn put(url string, data string) ?Response {
|
||||
return fetch_with_method(.put, url,
|
||||
data: data
|
||||
headers: map{
|
||||
'Content-Type': http.content_type_default
|
||||
}
|
||||
header: new_header({key: .content_type, value: http.content_type_default})
|
||||
)
|
||||
}
|
||||
|
||||
@ -116,9 +106,7 @@ pub fn put(url string, data string) ?Response {
|
||||
pub fn patch(url string, data string) ?Response {
|
||||
return fetch_with_method(.patch, url,
|
||||
data: data
|
||||
headers: map{
|
||||
'Content-Type': http.content_type_default
|
||||
}
|
||||
header: new_header({key: .content_type, value: http.content_type_default})
|
||||
)
|
||||
}
|
||||
|
||||
@ -143,7 +131,7 @@ pub fn fetch(_url string, config FetchConfig) ?Response {
|
||||
method: config.method
|
||||
url: url
|
||||
data: data
|
||||
headers: config.headers
|
||||
header: config.header
|
||||
cookies: config.cookies
|
||||
user_agent: config.user_agent
|
||||
ws_func: 0
|
||||
@ -195,32 +183,23 @@ fn build_url_from_fetch(_url string, config FetchConfig) ?string {
|
||||
}
|
||||
|
||||
fn (mut req Request) free() {
|
||||
unsafe { req.headers.free() }
|
||||
unsafe { req.header.free() }
|
||||
}
|
||||
|
||||
fn (mut resp Response) free() {
|
||||
unsafe { resp.headers.free() }
|
||||
unsafe { resp.header.data.free() }
|
||||
}
|
||||
|
||||
// add_header adds the key and value of an HTTP request header
|
||||
pub fn (mut req Request) add_header(key string, val string) {
|
||||
req.headers[key] = val
|
||||
// To add a custom header, use add_custom_header
|
||||
pub fn (mut req Request) add_header(key CommonHeader, val string) {
|
||||
req.header.add(key, val)
|
||||
}
|
||||
|
||||
// parse_headers parses HTTP header strings to mapped data
|
||||
pub fn parse_headers(lines []string) map[string]string {
|
||||
mut headers := map[string]string{}
|
||||
for i, line in lines {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
words := line.split(': ')
|
||||
if words.len != 2 {
|
||||
continue
|
||||
}
|
||||
headers[words[0]] = words[1]
|
||||
}
|
||||
return headers
|
||||
// add_custom_header adds the key and value of an HTTP request header
|
||||
// This method may fail if the key contains characters that are not permitted
|
||||
pub fn (mut req Request) add_custom_header(key string, val string) ? {
|
||||
return req.header.add_custom(key, val)
|
||||
}
|
||||
|
||||
// do will send the HTTP request and returns `http.Response` as soon as the response is recevied
|
||||
@ -239,7 +218,7 @@ pub fn (req &Request) do() ?Response {
|
||||
break
|
||||
}
|
||||
// follow any redirects
|
||||
mut redirect_url := resp.lheaders['location']
|
||||
mut redirect_url := resp.header.get(.location) or { '' }
|
||||
if redirect_url.len > 0 && redirect_url[0] == `/` {
|
||||
url.set_path(redirect_url) or {
|
||||
return error('http.request.do: invalid path in redirect: "$redirect_url"')
|
||||
@ -283,9 +262,7 @@ fn (req &Request) method_and_url_to_response(method Method, url urllib.URL) ?Res
|
||||
}
|
||||
|
||||
fn parse_response(resp string) Response {
|
||||
// TODO: Header data type
|
||||
mut headers := map[string]string{}
|
||||
mut lheaders := map[string]string{}
|
||||
mut header := new_header()
|
||||
// TODO: Cookie data type
|
||||
mut cookies := map[string]string{}
|
||||
first_header := resp.all_before('\n')
|
||||
@ -295,7 +272,7 @@ fn parse_response(resp string) Response {
|
||||
status_code = val.int()
|
||||
}
|
||||
mut text := ''
|
||||
// Build resp headers map and separate the body
|
||||
// Build resp header map and separate the body
|
||||
mut nl_pos := 3
|
||||
mut i := 1
|
||||
for {
|
||||
@ -312,27 +289,21 @@ fn parse_response(resp string) Response {
|
||||
}
|
||||
i++
|
||||
pos := h.index(':') or { continue }
|
||||
// if h.contains('Content-Type') {
|
||||
// continue
|
||||
// }
|
||||
mut key := h[..pos]
|
||||
lkey := key.to_lower()
|
||||
val := h[pos + 2..]
|
||||
if lkey == 'set-cookie' {
|
||||
parts := val.trim_space().split('=')
|
||||
cookies[parts[0]] = parts[1]
|
||||
}
|
||||
tval := val.trim_space()
|
||||
headers[key] = tval
|
||||
lheaders[lkey] = tval
|
||||
val := h[pos + 2..].trim_space()
|
||||
header.add_custom(key, val) or { eprintln('error parsing header: $err') }
|
||||
}
|
||||
if lheaders['transfer-encoding'] == 'chunked' || lheaders['content-length'] == '' {
|
||||
// set cookies
|
||||
for cookie in header.values(.set_cookie) {
|
||||
parts := cookie.split_nth('=', 2)
|
||||
cookies[parts[0]] = parts[1]
|
||||
}
|
||||
if header.get(.transfer_encoding) or { '' } == 'chunked' || header.get(.content_length) or { '' } == '' {
|
||||
text = chunked.decode(text)
|
||||
}
|
||||
return Response{
|
||||
status_code: status_code
|
||||
headers: headers
|
||||
lheaders: lheaders
|
||||
header: header
|
||||
cookies: cookies
|
||||
text: text
|
||||
}
|
||||
@ -341,19 +312,20 @@ fn parse_response(resp string) Response {
|
||||
fn (req &Request) build_request_headers(method Method, host_name string, path string) string {
|
||||
ua := req.user_agent
|
||||
mut uheaders := []string{}
|
||||
if 'Host' !in req.headers {
|
||||
if !req.header.contains(.host) {
|
||||
uheaders << 'Host: $host_name\r\n'
|
||||
}
|
||||
if 'User-Agent' !in req.headers {
|
||||
if !req.header.contains(.user_agent) {
|
||||
uheaders << 'User-Agent: $ua\r\n'
|
||||
}
|
||||
if req.data.len > 0 && 'Content-Length' !in req.headers {
|
||||
if req.data.len > 0 && !req.header.contains(.content_length) {
|
||||
uheaders << 'Content-Length: $req.data.len\r\n'
|
||||
}
|
||||
for key, val in req.headers {
|
||||
if key == 'Cookie' {
|
||||
for key in req.header.keys() {
|
||||
if key == CommonHeader.cookie.str() {
|
||||
continue
|
||||
}
|
||||
val := req.header.custom_values(key).join('; ')
|
||||
uheaders << '$key: $val\r\n'
|
||||
}
|
||||
uheaders << req.build_request_cookies_header()
|
||||
@ -369,9 +341,7 @@ fn (req &Request) build_request_cookies_header() string {
|
||||
for key, val in req.cookies {
|
||||
cookie << '$key=$val'
|
||||
}
|
||||
if 'Cookie' in req.headers && req.headers['Cookie'] != '' {
|
||||
cookie << req.headers['Cookie']
|
||||
}
|
||||
cookie << req.header.values(.cookie)
|
||||
return 'Cookie: ' + cookie.join('; ') + '\r\n'
|
||||
}
|
||||
|
||||
@ -415,5 +385,5 @@ fn (req &Request) http_do(host string, method Method, path string) ?Response {
|
||||
|
||||
// referer returns 'Referer' header value of the given request
|
||||
pub fn (req &Request) referer() string {
|
||||
return req.headers['Referer']
|
||||
return req.header.get(.referer) or { '' }
|
||||
}
|
||||
|
@ -77,12 +77,12 @@ fn test_http_fetch_with_params() {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_http_fetch_with_headers() {
|
||||
fn test_http_fetch_with_headers() ? {
|
||||
$if !network ? { return }
|
||||
mut header := new_header()
|
||||
header.add_custom('Test-Header', 'hello world') ?
|
||||
responses := http_fetch_mock([], {
|
||||
headers: {
|
||||
'Test-Header': 'hello world'
|
||||
}
|
||||
header: header
|
||||
}) or {
|
||||
panic(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user