From b0f1d9f46745dec40f8d8d22812eb38302874755 Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Thu, 28 Jul 2016 11:30:05 +0200 Subject: make httpclient close opened Socket on error --- lib/pure/httpclient.nim | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'lib/pure') 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, -- cgit 1.4.1-2-gfad0 From 6a2ad716a4b7dd9439971524047c2617347d4d80 Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Thu, 28 Jul 2016 15:37:34 +0200 Subject: remove superfluous `except: raise` --- lib/pure/httpclient.nim | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index b00012d50..eeecd753c 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -446,8 +446,6 @@ proc request*(url: string, httpMethod: string, extraHeaders = "", s.send(body) result = parseResponse(s, httpMethod != "httpHEAD", timeout) - except: - raise finally: s.close() -- cgit 1.4.1-2-gfad0 From 4bef9d020d8ca974c376ae1213e083b20ebf29ab Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Thu, 28 Jul 2016 15:50:34 +0200 Subject: use `defer` instead of `try ... finally` --- lib/pure/httpclient.nim | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index eeecd753c..41d4b0ee7 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -422,32 +422,29 @@ proc request*(url: string, httpMethod: string, extraHeaders = "", add(headers, "Proxy-Authorization: basic " & auth & "\c\L") add(headers, extraHeaders) add(headers, "\c\L") - 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) - - if timeout == -1: - s.connect(r.hostname, port) + var s = newSocket() + defer: s.close() + 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: - s.connect(r.hostname, port, timeout) - s.send(headers) - if body != "": - s.send(body) - - result = parseResponse(s, httpMethod != "httpHEAD", timeout) - finally: - s.close() + 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) + + result = parseResponse(s, httpMethod != "httpHEAD", timeout) proc request*(url: string, httpMethod = httpGET, extraHeaders = "", body = "", sslContext = defaultSSLContext, timeout = -1, -- cgit 1.4.1-2-gfad0