use defer instead of try ... finally
This commit is contained in:
parent
6a2ad716a4
commit
4bef9d020d
1 changed files with 21 additions and 24 deletions
|
|
@ -422,32 +422,29 @@ 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: Socket
|
var s = newSocket()
|
||||||
try:
|
defer: s.close()
|
||||||
s = newSocket()
|
if s == nil: raiseOSError(osLastError())
|
||||||
if s == nil: raiseOSError(osLastError())
|
var port = net.Port(80)
|
||||||
var port = net.Port(80)
|
if r.scheme == "https":
|
||||||
if r.scheme == "https":
|
when defined(ssl):
|
||||||
when defined(ssl):
|
sslContext.wrapSocket(s)
|
||||||
sslContext.wrapSocket(s)
|
port = net.Port(443)
|
||||||
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:
|
||||||
s.connect(r.hostname, port, timeout)
|
raise newException(HttpRequestError,
|
||||||
s.send(headers)
|
"SSL support is not available. Cannot connect over SSL.")
|
||||||
if body != "":
|
if r.port != "":
|
||||||
s.send(body)
|
port = net.Port(r.port.parseInt)
|
||||||
|
|
||||||
result = parseResponse(s, httpMethod != "httpHEAD", timeout)
|
if timeout == -1:
|
||||||
finally:
|
s.connect(r.hostname, port)
|
||||||
s.close()
|
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 = "",
|
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