Make gzip handling more robust

This commit is contained in:
Zed 2021-12-30 01:39:00 +01:00
parent e3f6c72bf6
commit ddc2be8439
2 changed files with 8 additions and 7 deletions

View file

@ -49,11 +49,15 @@ proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
let headers = genHeaders(token) let headers = genHeaders(token)
try: try:
var resp: AsyncResponse var resp: AsyncResponse
let body = pool.use(headers): var body = pool.use(headers):
resp = await c.get($url) resp = await c.get($url)
let raw = await resp.body await resp.body
if raw.len == 0: ""
else: uncompress(raw) if body.len > 0:
if resp.headers.getOrDefault("content-encoding") == "gzip":
body = uncompress(body, dfGzip)
else:
echo "non-gzip body, url: ", url, ", body: ", body
if body.startsWith('{') or body.startsWith('['): if body.startsWith('{') or body.startsWith('['):
result = parseJson(body) result = parseJson(body)

View file

@ -78,9 +78,6 @@ proc fetchSingleTimeline*(after: string; query: Query; skipRail=false):
return (profile, timeline, await rail) return (profile, timeline, await rail)
proc get*(req: Request; key: string): string =
params(req).getOrDefault(key)
proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs; proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs;
rss, after: string): Future[string] {.async.} = rss, after: string): Future[string] {.async.} =
if query.fromUser.len != 1: if query.fromUser.len != 1: