mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
http: handle and print socket errors
This commit is contained in:
parent
4f4ac45670
commit
a5ccc4673b
@ -3855,6 +3855,7 @@ fn (p mut Parser) assert_statement() {
|
||||
if (!$tmp) {
|
||||
println(tos2((byte *)"\\x1B[31mFAILED: $p.cur_fn.name() in $filename:$p.scanner.line_nr\\x1B[0m"));
|
||||
g_test_fails++;
|
||||
return;
|
||||
// TODO
|
||||
// Maybe print all vars in a test function if it fails?
|
||||
} else {
|
||||
|
@ -32,7 +32,7 @@ fn init() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
fn (req &Request) ssl_do(port int, method, host_name, path string) Response {
|
||||
fn (req &Request) ssl_do(port int, method, host_name, path string) ?Response {
|
||||
//ssl_method := C.SSLv23_method()
|
||||
ssl_method := C.TLSv1_2_method()
|
||||
if isnil(method) {
|
||||
|
@ -16,7 +16,7 @@ fn C.new_tls_context() C.TlsContext
|
||||
|
||||
fn init() int { return 1 }
|
||||
|
||||
fn (req &Request) ssl_do(port int, method, host_name, path string) Response {
|
||||
fn (req &Request) ssl_do(port int, method, host_name, path string) ?Response {
|
||||
mut ctx := C.new_tls_context()
|
||||
C.vschannel_init(&ctx)
|
||||
|
||||
|
@ -39,14 +39,20 @@ pub fn get(url string) ?Response {
|
||||
req := new_request('GET', url, '') or {
|
||||
return error(err)
|
||||
}
|
||||
return req.do()
|
||||
res := req.do() or {
|
||||
return error(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn post(url, data string) ?Response {
|
||||
req := new_request('POST', url, data) or {
|
||||
return error(err)
|
||||
}
|
||||
return req.do()
|
||||
res := req.do() or {
|
||||
return error(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
pub fn new_request(typ, _url, _data string) ?Request {
|
||||
@ -151,10 +157,16 @@ fn (req &Request) method_and_url_to_response(method string, url net_dot_urllib.U
|
||||
//println('fetch $method, $scheme, $host_name, $nport, $path ')
|
||||
if scheme == 'https' {
|
||||
//println('ssl_do( $nport, $method, $host_name, $path )')
|
||||
return req.ssl_do( nport, method, host_name, path )
|
||||
res := req.ssl_do( nport, method, host_name, path ) or {
|
||||
return error(err)
|
||||
}
|
||||
return res
|
||||
} else if scheme == 'http' {
|
||||
//println('http_do( $nport, $method, $host_name, $path )')
|
||||
return req.http_do(nport, method, host_name, path )
|
||||
res := req.http_do(nport, method, host_name, path ) or {
|
||||
return error(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
return error('http.request.do: unsupported scheme: $scheme')
|
||||
}
|
||||
|
@ -4,5 +4,10 @@ module net
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
fn init() int { return 1 }
|
||||
|
||||
fn error_code() int {
|
||||
return C.errno
|
||||
}
|
||||
|
@ -29,3 +29,7 @@ fn init() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
fn error_code() int {
|
||||
return C.WSAGetLastError()
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
module net
|
||||
|
||||
import os
|
||||
|
||||
struct Socket {
|
||||
pub:
|
||||
sockfd int
|
||||
@ -162,11 +164,13 @@ pub fn (s Socket) connect(address string, port int) ?int {
|
||||
sport := '$port'
|
||||
info_res := C.getaddrinfo(address.str, sport.str, &hints, &info)
|
||||
if info_res != 0 {
|
||||
return error('socket: connect failed')
|
||||
error_message := os.get_error_msg(net.error_code())
|
||||
return error('socket: getaddrinfo failed ($error_message)')
|
||||
}
|
||||
res := int(C.connect(s.sockfd, info.ai_addr, info.ai_addrlen))
|
||||
if res < 0 {
|
||||
return error('socket: connect failed')
|
||||
error_message := os.get_error_msg(net.error_code())
|
||||
return error('socket: connect failed ($error_message)')
|
||||
}
|
||||
return int(res)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user