Catch SSL shutdown errors when closing clients

This commit is contained in:
Zed 2020-01-01 17:24:24 +01:00
parent b09798f2cf
commit ee43d8cfb1
3 changed files with 11 additions and 3 deletions

View file

@ -26,7 +26,9 @@ proc genHeaders*(agent: string; referer: Uri; lang=true;
template newClient*() {.dirty.} =
var client = newAsyncHttpClient()
defer: client.close()
defer:
try: client.close()
except: discard
client.headers = headers
proc fetchHtml*(url: Uri; headers: HttpHeaders; jsonKey = ""): Future[XmlNode] {.async.} =

View file

@ -31,7 +31,7 @@ proc createMediaRouter*(cfg: Config) =
let client = newAsyncHttpClient()
try:
await client.downloadFile($uri, filename)
client.close()
client.safeClose()
except HttpRequestError:
removeFile(filename)
resp Http404

View file

@ -2,6 +2,8 @@ import strutils, sequtils, asyncdispatch, httpclient
import ../utils, ../prefs
export utils, prefs
from net import SslError
template cookiePrefs*(): untyped {.dirty.} =
getPrefs(request.cookies.getOrDefault("preferences"), cfg)
@ -14,8 +16,12 @@ template refPath*(): untyped {.dirty.} =
proc getNames*(name: string): seq[string] =
name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
proc safeClose*(client: AsyncHttpClient) =
try: client.close()
except SslError: discard
proc safeFetch*(url: string): Future[string] {.async.} =
let client = newAsyncHttpClient()
try: result = await client.getContent(url)
except: discard
client.close()
client.safeClose()