diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-27 03:06:24 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-27 03:06:24 +0200 |
commit | 14baa050d3ab4e45879582a8f87eeb1e42407909 (patch) | |
tree | ba16ced8c763e169ccf1c54f8a85a85e12e61caf /src | |
parent | ac3e737867245ac65adfa2352ce6e7fdacc57b18 (diff) | |
download | chawan-14baa050d3ab4e45879582a8f87eeb1e42407909.tar.gz |
Response.text: encode/decode properly
also, use blob() for images
Diffstat (limited to 'src')
-rw-r--r-- | src/loader/response.nim | 18 | ||||
-rw-r--r-- | src/server/buffer.nim | 13 |
2 files changed, 24 insertions, 7 deletions
diff --git a/src/loader/response.nim b/src/loader/response.nim index 8ad98f84..2be709c7 100644 --- a/src/loader/response.nim +++ b/src/loader/response.nim @@ -1,4 +1,5 @@ import streams +import unicode import bindings/quickjs import io/promise @@ -10,6 +11,8 @@ import types/blob import types/url import chakasu/charset +import chakasu/decoderstream +import chakasu/encoderstream type Response* = ref object @@ -62,9 +65,20 @@ proc text*(response: Response): Promise[JSResult[string]] {.jsfunc.} = let bodyRead = response.bodyRead response.bodyRead = nil return bodyRead.then(proc(s: string): JSResult[string] = - ok(s)) + if response.charset == CHARSET_UTF_8 and s.validateUtf8() == -1: + ok(s) + else: + let ss = newStringStream(s) + let cs = if response.charset == CHARSET_UNKNOWN: + CHARSET_UTF_8 + else: + response.charset + let ds = newDecoderStream(ss, cs) + let es = newEncoderStream(ds, CHARSET_UTF_8) + return ok(es.readAll()) + ) -proc blob(response: Response): Promise[JSResult[Blob]] {.jsfunc.} = +proc blob*(response: Response): Promise[JSResult[Blob]] {.jsfunc.} = if response.bodyRead == nil: let p = newPromise[JSResult[Blob]]() let err = JSResult[Blob] diff --git a/src/server/buffer.nim b/src/server/buffer.nim index b1c67f78..f7ead4e5 100644 --- a/src/server/buffer.nim +++ b/src/server/buffer.nim @@ -40,6 +40,7 @@ import loader/connecterror import loader/loader import render/renderdocument import render/rendertext +import types/blob import types/buffersource import types/cell import types/color @@ -644,18 +645,20 @@ proc loadResource(buffer: Buffer, elem: HTMLImageElement): EmptyPromise = if url.isSome: let url = url.get return buffer.loader.fetch(newRequest(url)) - .then(proc(res: JSResult[Response]): Promise[JSResult[string]] = + .then(proc(res: JSResult[Response]): Promise[JSResult[Blob]] = if res.isErr: return let res = res.get if res.contentType == "image/png": - #TODO using text() for PNG is wrong - return res.text() - ).then(proc(pngData: JSResult[string]) = + return res.blob() + ).then(proc(pngData: JSResult[Blob]) = if pngData.isErr: return let pngData = pngData.get - elem.bitmap = fromPNG(toOpenArrayByte(pngData, 0, pngData.high))) + let buffer = cast[ptr UncheckedArray[uint8]](pngData.buffer) + let high = int(pngData.size - 1) + elem.bitmap = fromPNG(toOpenArray(buffer, 0, high)) + ) proc loadResources(buffer: Buffer): EmptyPromise = let document = buffer.document |