http: handle and print socket errors

pull/2293/head
vitalyster 2019-10-10 20:24:36 +03:00 committed by Alexander Medvednikov
parent 4f4ac45670
commit a5ccc4673b
7 changed files with 34 additions and 8 deletions

View File

@ -3855,6 +3855,7 @@ fn (p mut Parser) assert_statement() {
if (!$tmp) { if (!$tmp) {
println(tos2((byte *)"\\x1B[31mFAILED: $p.cur_fn.name() in $filename:$p.scanner.line_nr\\x1B[0m")); println(tos2((byte *)"\\x1B[31mFAILED: $p.cur_fn.name() in $filename:$p.scanner.line_nr\\x1B[0m"));
g_test_fails++; g_test_fails++;
return;
// TODO // TODO
// Maybe print all vars in a test function if it fails? // Maybe print all vars in a test function if it fails?
} else { } else {

View File

@ -32,7 +32,7 @@ fn init() int {
return 1 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.SSLv23_method()
ssl_method := C.TLSv1_2_method() ssl_method := C.TLSv1_2_method()
if isnil(method) { if isnil(method) {

View File

@ -16,7 +16,7 @@ fn C.new_tls_context() C.TlsContext
fn init() int { return 1 } 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() mut ctx := C.new_tls_context()
C.vschannel_init(&ctx) C.vschannel_init(&ctx)

View File

@ -39,14 +39,20 @@ pub fn get(url string) ?Response {
req := new_request('GET', url, '') or { req := new_request('GET', url, '') or {
return error(err) return error(err)
} }
return req.do() res := req.do() or {
return error(err)
}
return res
} }
pub fn post(url, data string) ?Response { pub fn post(url, data string) ?Response {
req := new_request('POST', url, data) or { req := new_request('POST', url, data) or {
return error(err) return error(err)
} }
return req.do() res := req.do() or {
return error(err)
}
return res
} }
pub fn new_request(typ, _url, _data string) ?Request { 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 ') //println('fetch $method, $scheme, $host_name, $nport, $path ')
if scheme == 'https' { if scheme == 'https' {
//println('ssl_do( $nport, $method, $host_name, $path )') //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' { } else if scheme == 'http' {
//println('http_do( $nport, $method, $host_name, $path )') //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') return error('http.request.do: unsupported scheme: $scheme')
} }

View File

@ -4,5 +4,10 @@ module net
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <errno.h>
fn init() int { return 1 } fn init() int { return 1 }
fn error_code() int {
return C.errno
}

View File

@ -29,3 +29,7 @@ fn init() int {
return 1 return 1
} }
fn error_code() int {
return C.WSAGetLastError()
}

View File

@ -1,5 +1,7 @@
module net module net
import os
struct Socket { struct Socket {
pub: pub:
sockfd int sockfd int
@ -162,11 +164,13 @@ pub fn (s Socket) connect(address string, port int) ?int {
sport := '$port' sport := '$port'
info_res := C.getaddrinfo(address.str, sport.str, &hints, &info) info_res := C.getaddrinfo(address.str, sport.str, &hints, &info)
if info_res != 0 { 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)) res := int(C.connect(s.sockfd, info.ai_addr, info.ai_addrlen))
if res < 0 { 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) return int(res)
} }