net.http: support `-d trace_http_request` and `-d trace_http_response`

pull/9541/head
Delyan Angelov 2021-03-30 18:11:00 +03:00
parent 205fb88d90
commit 683eaad66f
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 36 additions and 14 deletions

View File

@ -37,6 +37,9 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
res = C.SSL_get_verify_result(voidptr(ssl)) res = C.SSL_get_verify_result(voidptr(ssl))
// ///// // /////
req_headers := req.build_request_headers(method, host_name, path) req_headers := req.build_request_headers(method, host_name, path)
$if trace_http_request ? {
eprintln('> $req_headers')
}
// println(req_headers) // println(req_headers)
C.BIO_puts(web, req_headers.str) C.BIO_puts(web, req_headers.str)
mut content := strings.new_builder(100) mut content := strings.new_builder(100)
@ -63,5 +66,9 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
if ctx != 0 { if ctx != 0 {
C.SSL_CTX_free(ctx) C.SSL_CTX_free(ctx)
} }
return parse_response(content.str()) response_text := content.str()
$if trace_http_response ? {
eprintln('< $response_text')
}
return parse_response(response_text)
} }

View File

@ -6,15 +6,23 @@ module http
#flag windows -I @VROOT/thirdparty/vschannel #flag windows -I @VROOT/thirdparty/vschannel
#flag -l ws2_32 -l crypt32 -l secur32 -l user32 #flag -l ws2_32 -l crypt32 -l secur32 -l user32
#include "vschannel.c" #include "vschannel.c"
fn C.new_tls_context() C.TlsContext fn C.new_tls_context() C.TlsContext
fn (req &Request) ssl_do(port int, method Method, host_name string, path string) ?Response { fn (req &Request) ssl_do(port int, method Method, host_name string, path string) ?Response {
mut ctx := C.new_tls_context() mut ctx := C.new_tls_context()
C.vschannel_init(&ctx) C.vschannel_init(&ctx)
mut buff := unsafe {malloc(C.vsc_init_resp_buff_size)} mut buff := unsafe { malloc(C.vsc_init_resp_buff_size) }
addr := host_name addr := host_name
sdata := req.build_request_headers(method, host_name, path) sdata := req.build_request_headers(method, host_name, path)
$if trace_http_request ? {
eprintln('> $sdata')
}
length := int(C.request(&ctx, port, addr.to_wide(), sdata.str, &buff)) length := int(C.request(&ctx, port, addr.to_wide(), sdata.str, &buff))
C.vschannel_cleanup(&ctx) C.vschannel_cleanup(&ctx)
return parse_response(unsafe {buff.vstring_with_len(length)}) response_text := unsafe { buff.vstring_with_len(length) }
$if trace_http_response ? {
eprintln('< $response_text')
}
return parse_response(response_text)
} }

View File

@ -74,9 +74,9 @@ pub fn get(url string) ?Response {
// post sends a POST HTTP request to the URL with a string data // post sends a POST HTTP request to the URL with a string data
pub fn post(url string, data string) ?Response { pub fn post(url string, data string) ?Response {
return fetch_with_method(.post, url, return fetch_with_method(.post, url,
data: data data: data
headers: { headers: map{
'Content-Type': http.content_type_default 'Content-Type': http.content_type_default
} }
) )
@ -84,9 +84,9 @@ pub fn post(url string, data string) ?Response {
// post_json sends a POST HTTP request to the URL with a JSON data // post_json sends a POST HTTP request to the URL with a JSON data
pub fn post_json(url string, data string) ?Response { pub fn post_json(url string, data string) ?Response {
return fetch_with_method(.post, url, return fetch_with_method(.post, url,
data: data data: data
headers: { headers: map{
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
) )
@ -94,8 +94,8 @@ pub fn post_json(url string, data string) ?Response {
// post_form sends a POST HTTP request to the URL with X-WWW-FORM-URLENCODED data // 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 { pub fn post_form(url string, data map[string]string) ?Response {
return fetch_with_method(.post, url, return fetch_with_method(.post, url,
headers: { headers: map{
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Type': 'application/x-www-form-urlencoded'
} }
data: url_encode_form_data(data) data: url_encode_form_data(data)
@ -104,9 +104,9 @@ pub fn post_form(url string, data map[string]string) ?Response {
// put sends a PUT HTTP request to the URL with a string data // put sends a PUT HTTP request to the URL with a string data
pub fn put(url string, data string) ?Response { pub fn put(url string, data string) ?Response {
return fetch_with_method(.put, url, return fetch_with_method(.put, url,
data: data data: data
headers: { headers: map{
'Content-Type': http.content_type_default 'Content-Type': http.content_type_default
} }
) )
@ -114,9 +114,9 @@ pub fn put(url string, data string) ?Response {
// patch sends a PATCH HTTP request to the URL with a string data // patch sends a PATCH HTTP request to the URL with a string data
pub fn patch(url string, data string) ?Response { pub fn patch(url string, data string) ?Response {
return fetch_with_method(.patch, url, return fetch_with_method(.patch, url,
data: data data: data
headers: { headers: map{
'Content-Type': http.content_type_default 'Content-Type': http.content_type_default
} }
) )
@ -401,9 +401,16 @@ fn (req &Request) http_do(host string, method Method, path string) ?Response {
mut client := net.dial_tcp(host) ? mut client := net.dial_tcp(host) ?
// TODO this really needs to be exposed somehow // TODO this really needs to be exposed somehow
client.write(s.bytes()) ? client.write(s.bytes()) ?
$if trace_http_request ? {
eprintln('> $s')
}
mut bytes := io.read_all(reader: client) ? mut bytes := io.read_all(reader: client) ?
client.close() ? client.close() ?
return parse_response(bytes.bytestr()) response_text := bytes.bytestr()
$if trace_http_response ? {
eprintln('< $response_text')
}
return parse_response(response_text)
} }
// referer returns 'Referer' header value of the given request // referer returns 'Referer' header value of the given request