make httpclient close opened Socket on error

This commit is contained in:
Hendrik Richter 2016-07-28 11:30:05 +02:00
commit b0f1d9f467

View file

@ -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,