diff options
author | bptato <nincsnevem662@gmail.com> | 2024-02-14 15:44:32 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-02-14 15:44:32 +0100 |
commit | 547a492611a8d6831d88b697e2785658a9c06177 (patch) | |
tree | c5fa38683acc29e4d70396d5f4d96346f453b469 /src/loader | |
parent | c2fbe0993228b2b62f8a916bae5271097254ce5c (diff) | |
download | chawan-547a492611a8d6831d88b697e2785658a9c06177.tar.gz |
Various refactorings & fixes
* disallow Stream interface usage on non-blocking PosixStreams * do not read estream of forkserver byte-by-byte (it's slow) * do not call writeData with a zero len in formdata * do not quote numbers in mailcap quoteFile * remove some unused stuff
Diffstat (limited to 'src/loader')
-rw-r--r-- | src/loader/loader.nim | 12 | ||||
-rw-r--r-- | src/loader/request.nim | 62 |
2 files changed, 6 insertions, 68 deletions
diff --git a/src/loader/loader.nim b/src/loader/loader.nim index 11ddefcc..f2d38d61 100644 --- a/src/loader/loader.nim +++ b/src/loader/loader.nim @@ -608,7 +608,7 @@ proc fetch*(loader: FileLoader, input: Request): FetchPromise = stream.swrite(LOAD) stream.swrite(input) stream.flush() - let fd = int(stream.source.getFd()) + let fd = int(stream.fd) loader.registerFun(fd) let promise = FetchPromise() loader.connecting[fd] = ConnectData( @@ -624,7 +624,7 @@ proc reconnect*(loader: FileLoader, data: ConnectData) = stream.swrite(LOAD) stream.swrite(data.request) stream.flush() - let fd = int(stream.source.getFd()) + let fd = int(stream.fd) loader.registerFun(fd) loader.connecting[fd] = ConnectData( promise: data.promise, @@ -638,7 +638,7 @@ proc switchStream*(data: var ConnectData, stream: SocketStream) = proc switchStream*(loader: FileLoader, data: var OngoingData, stream: SocketStream) = data.response.body = stream - let fd = int(stream.source.getFd()) + let fd = int(stream.fd) let realCloseImpl = stream.closeImpl stream.closeImpl = nil data.response.unregisterFun = proc() = @@ -704,7 +704,7 @@ proc onConnected*(loader: FileLoader, fd: int) = response: response, bodyRead: response.bodyRead ) - stream.source.getFd().setBlocking(false) + stream.setBlocking(false) promise.resolve(JSResult[Response].ok(response)) else: var msg: string @@ -724,7 +724,7 @@ proc onRead*(loader: FileLoader, fd: int) = let olen = buffer[].buf.len try: buffer[].buf.setLen(olen + BufferSize) - let n = response.body.readData(addr buffer[].buf[olen], BufferSize) + let n = response.body.recvData(addr buffer[].buf[olen], BufferSize) buffer[].buf.setLen(olen + n) if n == 0: break @@ -743,7 +743,7 @@ proc onError*(loader: FileLoader, fd: int) = when defined(debug): var lbuf {.noinit.}: array[BufferSize, char] if not response.body.atEnd(): - let n = response.body.readData(addr lbuf[0], lbuf.len) + let n = response.body.recvData(addr lbuf[0], lbuf.len) assert n == 0 assert response.body.atEnd() buffer[].bodyRead.resolve(buffer[].buf) diff --git a/src/loader/request.nim b/src/loader/request.nim index fcad1681..1d0e31cd 100644 --- a/src/loader/request.nim +++ b/src/loader/request.nim @@ -1,5 +1,4 @@ import std/options -import std/streams import std/strutils import std/tables @@ -84,11 +83,6 @@ type fromcache*: bool clientId*: StreamId - ReadableStream* = ref object of Stream - isource*: Stream - buf: string - isend: bool - jsDestructor(Request) proc js_url(this: Request): string {.jsfget: "url".} = @@ -105,62 +99,6 @@ iterator pairs*(headers: Headers): (string, string) = for v in vs: yield (k, v) -proc rsReadData(s: Stream, buffer: pointer, bufLen: int): int = - var s = ReadableStream(s) - if s.atEnd: - return 0 - while s.buf.len < bufLen: - var len: int - s.isource.read(len) - if len == 0: - result = s.buf.len - copyMem(buffer, addr(s.buf[0]), result) - s.buf = s.buf.substr(result) - s.isend = true - return - var nbuf: string - s.isource.readStr(len, nbuf) - s.buf &= nbuf - assert s.buf.len >= bufLen - result = bufLen - copyMem(buffer, addr(s.buf[0]), result) - s.buf = s.buf.substr(result) - if s.buf.len == 0: - var len: int - s.isource.read(len) - if len == 0: - s.isend = true - else: - s.isource.readStr(len, s.buf) - -proc rsAtEnd(s: Stream): bool = - ReadableStream(s).isend - -proc rsClose(s: Stream) = {.cast(tags: [WriteIOEffect]).}: #TODO TODO TODO ew. - var s = ReadableStream(s) - if s.isend: return - s.buf = "" - while true: - var len: int - s.isource.read(len) - if len == 0: - s.isend = true - break - s.isource.setPosition(s.isource.getPosition() + len) - -proc newReadableStream*(isource: Stream): ReadableStream = - var len: int - isource.read(len) - result = ReadableStream( - isource: isource, - readDataImpl: rsReadData, - atEndImpl: rsAtEnd, - closeImpl: rsClose, - isend: len == 0 - ) - if len != 0: - isource.readStr(len, result.buf) - func newRequest*(url: URL, httpMethod = HTTP_GET, headers = newHeaders(), body = opt(string), multipart = opt(FormData), mode = RequestMode.NO_CORS, credentialsMode = CredentialsMode.SAME_ORIGIN, |