diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-23 02:04:42 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-23 02:04:42 +0200 |
commit | 860a1e77e40469753dbef080ee6fcc24f82dc847 (patch) | |
tree | 2f3abe60c1fe1fb408f7b20da80f2d575199bccf /src/server/buffer.nim | |
parent | aa8f96765d1ddd85d0273d01cc9524514b6fe21f (diff) | |
download | chawan-860a1e77e40469753dbef080ee6fcc24f82dc847.tar.gz |
buffer: make readFromFd work with pipes
Diffstat (limited to 'src/server/buffer.nim')
-rw-r--r-- | src/server/buffer.nim | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim index af735c41..4ab09c20 100644 --- a/src/server/buffer.nim +++ b/src/server/buffer.nim @@ -750,8 +750,8 @@ proc connect2*(buffer: Buffer) {.proxy.} = buffer.selector.registerHandle(buffer.fd, {Read}, 0) proc redirectToFd*(buffer: Buffer, fd: FileHandle, wait: bool) {.proxy.} = - #TODO also clone & fd - if buffer.source.t == LOAD_REQUEST: + case buffer.source.t + of LOAD_REQUEST: let ss = SocketStream(buffer.istream) ss.swrite(true) ss.sendFileHandle(fd) @@ -765,6 +765,20 @@ proc redirectToFd*(buffer: Buffer, fd: FileHandle, wait: bool) {.proxy.} = ss.sread(dummy) discard close(fd) ss.close() + of LOAD_PIPE: + let ps = newPosixStream(fd) + let bfd = cint(buffer.fd) + #TODO make it work without wait + discard fcntl(bfd, F_SETFL, fcntl(bfd, F_GETFL, 0) and not O_NONBLOCK) + var buf: array[4096, uint8] + while not buffer.istream.atEnd: + let n = buffer.istream.readData(addr buf[0], buf.len) + ps.writeData(addr buf[0], n) + ps.close() + buffer.fd = -1 + buffer.istream.close() + of CLONE: + discard proc readFromFd*(buffer: Buffer, fd: FileHandle, ishtml: bool) {.proxy.} = let contentType = if ishtml: @@ -781,8 +795,7 @@ proc readFromFd*(buffer: Buffer, fd: FileHandle, ishtml: bool) {.proxy.} = buffer.contenttype = contentType discard fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) or O_NONBLOCK) let ps = newPosixStream(fd) - buffer.istream = newTeeStream(ps, buffer.sstream, - closedest = false) + buffer.istream = newTeeStream(ps, buffer.sstream, closedest = false) buffer.fd = fd buffer.selector.registerHandle(buffer.fd, {Read}, 0) |