make httpclient close opened Socket on error
This commit is contained in:
parent
b9865e8ad0
commit
b0f1d9f467
1 changed files with 26 additions and 21 deletions
|
|
@ -422,29 +422,34 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
|
||||||
add(headers, "Proxy-Authorization: basic " & auth & "\c\L")
|
add(headers, "Proxy-Authorization: basic " & auth & "\c\L")
|
||||||
add(headers, extraHeaders)
|
add(headers, extraHeaders)
|
||||||
add(headers, "\c\L")
|
add(headers, "\c\L")
|
||||||
var s = newSocket()
|
var s: Socket
|
||||||
if s == nil: raiseOSError(osLastError())
|
try:
|
||||||
var port = net.Port(80)
|
s = newSocket()
|
||||||
if r.scheme == "https":
|
if s == nil: raiseOSError(osLastError())
|
||||||
when defined(ssl):
|
var port = net.Port(80)
|
||||||
sslContext.wrapSocket(s)
|
if r.scheme == "https":
|
||||||
port = net.Port(443)
|
when defined(ssl):
|
||||||
|
sslContext.wrapSocket(s)
|
||||||
|
port = net.Port(443)
|
||||||
|
else:
|
||||||
|
raise newException(HttpRequestError,
|
||||||
|
"SSL support is not available. Cannot connect over SSL.")
|
||||||
|
if r.port != "":
|
||||||
|
port = net.Port(r.port.parseInt)
|
||||||
|
|
||||||
|
if timeout == -1:
|
||||||
|
s.connect(r.hostname, port)
|
||||||
else:
|
else:
|
||||||
raise newException(HttpRequestError,
|
s.connect(r.hostname, port, timeout)
|
||||||
"SSL support is not available. Cannot connect over SSL.")
|
s.send(headers)
|
||||||
if r.port != "":
|
if body != "":
|
||||||
port = net.Port(r.port.parseInt)
|
s.send(body)
|
||||||
|
|
||||||
if timeout == -1:
|
result = parseResponse(s, httpMethod != "httpHEAD", timeout)
|
||||||
s.connect(r.hostname, port)
|
except:
|
||||||
else:
|
raise
|
||||||
s.connect(r.hostname, port, timeout)
|
finally:
|
||||||
s.send(headers)
|
s.close()
|
||||||
if body != "":
|
|
||||||
s.send(body)
|
|
||||||
|
|
||||||
result = parseResponse(s, httpMethod != "httpHEAD", timeout)
|
|
||||||
s.close()
|
|
||||||
|
|
||||||
proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
|
proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
|
||||||
body = "", sslContext = defaultSSLContext, timeout = -1,
|
body = "", sslContext = defaultSSLContext, timeout = -1,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue