diff options
author | Hendrik Richter <info@prokapi.com> | 2016-07-28 15:50:34 +0200 |
---|---|---|
committer | Hendrik Richter <info@prokapi.com> | 2016-07-28 15:50:34 +0200 |
commit | 4bef9d020d8ca974c376ae1213e083b20ebf29ab (patch) | |
tree | f41d40d5c01d73d090bc723a1cb300cce4060709 /lib | |
parent | 6a2ad716a4b7dd9439971524047c2617347d4d80 (diff) | |
download | Nim-4bef9d020d8ca974c376ae1213e083b20ebf29ab.tar.gz |
use `defer` instead of `try ... finally`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/httpclient.nim | 47 |
1 files changed, 22 insertions, 25 deletions
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, |