mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net.http: support -d trace_http_request
and -d trace_http_response
This commit is contained in:
parent
205fb88d90
commit
683eaad66f
@ -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))
|
||||
// /////
|
||||
req_headers := req.build_request_headers(method, host_name, path)
|
||||
$if trace_http_request ? {
|
||||
eprintln('> $req_headers')
|
||||
}
|
||||
// println(req_headers)
|
||||
C.BIO_puts(web, req_headers.str)
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
@ -6,15 +6,23 @@ module http
|
||||
#flag windows -I @VROOT/thirdparty/vschannel
|
||||
#flag -l ws2_32 -l crypt32 -l secur32 -l user32
|
||||
#include "vschannel.c"
|
||||
|
||||
fn C.new_tls_context() C.TlsContext
|
||||
|
||||
fn (req &Request) ssl_do(port int, method Method, host_name string, path string) ?Response {
|
||||
mut ctx := C.new_tls_context()
|
||||
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
|
||||
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))
|
||||
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)
|
||||
}
|
||||
|
@ -74,9 +74,9 @@ pub fn get(url string) ?Response {
|
||||
|
||||
// post sends a POST HTTP request to the URL with a string data
|
||||
pub fn post(url string, data string) ?Response {
|
||||
return fetch_with_method(.post, url,
|
||||
return fetch_with_method(.post, url,
|
||||
data: data
|
||||
headers: {
|
||||
headers: map{
|
||||
'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
|
||||
pub fn post_json(url string, data string) ?Response {
|
||||
return fetch_with_method(.post, url,
|
||||
return fetch_with_method(.post, url,
|
||||
data: data
|
||||
headers: {
|
||||
headers: map{
|
||||
'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
|
||||
pub fn post_form(url string, data map[string]string) ?Response {
|
||||
return fetch_with_method(.post, url,
|
||||
headers: {
|
||||
return fetch_with_method(.post, url,
|
||||
headers: map{
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
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
|
||||
pub fn put(url string, data string) ?Response {
|
||||
return fetch_with_method(.put, url,
|
||||
return fetch_with_method(.put, url,
|
||||
data: data
|
||||
headers: {
|
||||
headers: map{
|
||||
'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
|
||||
pub fn patch(url string, data string) ?Response {
|
||||
return fetch_with_method(.patch, url,
|
||||
return fetch_with_method(.patch, url,
|
||||
data: data
|
||||
headers: {
|
||||
headers: map{
|
||||
'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) ?
|
||||
// TODO this really needs to be exposed somehow
|
||||
client.write(s.bytes()) ?
|
||||
$if trace_http_request ? {
|
||||
eprintln('> $s')
|
||||
}
|
||||
mut bytes := io.read_all(reader: client) ?
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user