From 860a1e77e40469753dbef080ee6fcc24f82dc847 Mon Sep 17 00:00:00 2001 From: bptato Date: Sat, 23 Sep 2023 02:04:42 +0200 Subject: buffer: make readFromFd work with pipes --- src/server/buffer.nim | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/server/buffer.nim') 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) -- cgit 1.4.1-2-gfad0