-
-
Save jrfondren/ff770a2ab3518f560ca534ec1f084ebd to your computer and use it in GitHub Desktop.
early exit from httpclient.get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/[asyncdispatch, asyncnet, sugar] | |
import myclient | |
proc showN(n: int, url: string) {.async.} = | |
var client = newAsyncHttpClient() | |
client.onRecv = () => client.contentProgress > n | |
let response = await client.get(url) | |
let body = response.body.await | |
echo body.substr(0, n) | |
waitFor showN(200, "https://some-large-url/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- httpclient.nim 2022-08-04 13:52:29.194959085 -0500 | |
+++ myclient.nim 2022-08-04 14:21:58.681593014 -0500 | |
@@ -534,6 +534,7 @@ | |
ProgressChangedProc*[ReturnType] = | |
proc (total, progress, speed: BiggestInt): | |
ReturnType {.closure, gcsafe.} | |
+ OnRecvProc* = proc (): bool {.closure, gcsafe.} | |
HttpClientBase*[SocketType] = ref object | |
socket: SocketType | |
@@ -549,10 +550,11 @@ | |
onProgressChanged*: ProgressChangedProc[void] | |
else: | |
onProgressChanged*: ProgressChangedProc[Future[void]] | |
+ onRecv*: OnRecvProc | |
when defined(ssl): | |
sslContext: net.SslContext | |
contentTotal: BiggestInt | |
- contentProgress: BiggestInt | |
+ contentProgress*: BiggestInt | |
oneSecondProgress: BiggestInt | |
lastProgressReport: MonoTime | |
when SocketType is AsyncSocket: | |
@@ -704,6 +706,9 @@ | |
await client.bodyStream.write(data) | |
await reportProgress(client, data.len) | |
+ if not client.onRecv.isNil: | |
+ if client.onRecv(): | |
+ return readLen | |
return readLen | |
@@ -769,9 +774,10 @@ | |
if recvLen == 0: | |
client.close() | |
httpError("Got disconnected while trying to read body.") | |
- if recvLen != length: | |
- httpError("Received length doesn't match expected length. Wanted " & | |
- $length & " got: " & $recvLen) | |
+ # move errors like this into recvFull | |
+ #if recvLen != length: | |
+ # httpError("Received length doesn't match expected length. Wanted " & | |
+ # $length & " got: " & $recvLen) | |
else: | |
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.4 TODO | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment