diff options
author | Hendrik Richter <info@prokapi.com> | 2016-07-28 11:30:05 +0200 |
---|---|---|
committer | Hendrik Richter <info@prokapi.com> | 2016-07-28 11:30:05 +0200 |
commit | b0f1d9f46745dec40f8d8d22812eb38302874755 (patch) | |
tree | c0883a93a1b021507a6daa7f3a5a2209c8112175 /lib/pure | |
parent | b9865e8ad02a17742b3e33e9c931c2b48bbd79ae (diff) | |
download | Nim-b0f1d9f46745dec40f8d8d22812eb38302874755.tar.gz |
make httpclient close opened Socket on error
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/httpclient.nim | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index b3a59551d..b00012d50 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -422,29 +422,34 @@ proc request*(url: string, httpMethod: string, extraHeaders = "", add(headers, "Proxy-Authorization: basic " & auth & "\c\L") add(headers, extraHeaders) add(headers, "\c\L") - var s = newSocket() - if s == nil: raiseOSError(osLastError()) - var port = net.Port(80) - if r.scheme == "https": - 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: - s.connect(r.hostname, port, timeout) - s.send(headers) - if body != "": - s.send(body) + var s: Socket + try: + s = newSocket() + if s == nil: raiseOSError(osLastError()) + var port = net.Port(80) + if r.scheme == "https": + 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) - result = parseResponse(s, httpMethod != "httpHEAD", timeout) - s.close() + if timeout == -1: + s.connect(r.hostname, port) + else: + s.connect(r.hostname, port, timeout) + s.send(headers) + if body != "": + s.send(body) + + result = parseResponse(s, httpMethod != "httpHEAD", timeout) + except: + raise + finally: + s.close() proc request*(url: string, httpMethod = httpGET, extraHeaders = "", body = "", sslContext = defaultSSLContext, timeout = -1, |