diff options
author | bptato <nincsnevem662@gmail.com> | 2023-04-30 12:19:24 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-04-30 12:19:24 +0200 |
commit | a02c408f933aea6f405ed3c64ab151b01b33ae9e (patch) | |
tree | 4946f6b13da782a92a1ecc0f03402abacf2614b3 /src/io/request.nim | |
parent | 05b64a1d8fa95381d756231f665c0b8c79787b67 (diff) | |
download | chawan-a02c408f933aea6f405ed3c64ab151b01b33ae9e.tar.gz |
WIP fetch
Diffstat (limited to 'src/io/request.nim')
-rw-r--r-- | src/io/request.nim | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/io/request.nim b/src/io/request.nim index 8837a1f9..c499ca8e 100644 --- a/src/io/request.nim +++ b/src/io/request.nim @@ -2,6 +2,7 @@ import options import streams import tables +import bindings/quickjs import types/url import js/javascript import utils/twtstr @@ -47,11 +48,14 @@ type Response* = ref object body*: Stream + bodyUsed* {.jsget.}: bool res* {.jsget.}: int contenttype* {.jsget.}: string status* {.jsget.}: int headers* {.jsget.}: HeaderList redirect*: Request + url*: URL #TODO should be urllist? + unregisterFun*: proc() ReadableStream* = ref object of Stream isource*: Stream @@ -203,12 +207,31 @@ func getOrDefault*(headers: HeaderList, k: string, default = ""): string = else: default -proc readAll*(response: Response): string {.jsfunc.} = +proc text*(response: Response): string {.jsfunc.} = + #TODO: this looks pretty unsafe. result = response.body.readAll() response.body.close() + response.bodyUsed = true + response.unregisterFun() + +#TODO: get rid of this +proc readAll*(response: Response): string {.jsfunc.} = + return response.text() + +proc Response_json*(ctx: JSContext, this: JSValue, argc: cint, argv: ptr JSValue): JSValue {.cdecl.} = + let op = getOpaque0(this) + if unlikely(not ctx.isInstanceOf(this, "Response") or op == nil): + return JS_ThrowTypeError(ctx, "Value is not an instance of %s", "Response") + let response = cast[Response](op) + var s = response.text() + return JS_ParseJSON(ctx, addr s[0], cast[csize_t](s.len), cstring"<input>") +#TODO: this should be a property of body proc close*(response: Response) {.jsfunc.} = + #TODO: this looks pretty unsafe response.body.close() + response.bodyUsed = true + response.unregisterFun() func credentialsMode*(attribute: CORSAttribute): CredentialsMode = case attribute @@ -219,5 +242,5 @@ func credentialsMode*(attribute: CORSAttribute): CredentialsMode = proc addRequestModule*(ctx: JSContext) = ctx.registerType(Request) - ctx.registerType(Response) + ctx.registerType(Response, extra_funcs = [TabFunc(name: "json", fun: Response_json)]) ctx.registerType(HeaderList) |