about summary refs log tree commit diff stats
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/buffer.nim33
-rw-r--r--src/server/forkserver.nim89
2 files changed, 56 insertions, 66 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index a3c94bf3..71deed82 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -27,6 +27,7 @@ import html/enums
 import html/env
 import html/event
 import html/formdata as formdata_impl
+import io/bufreader
 import io/bufstream
 import io/bufwriter
 import io/posixstream
@@ -121,11 +122,9 @@ type
     charset: Charset
     cacheId: int
     outputId: int
-    dummyStream: StringStream
 
   InterfaceOpaque = ref object
     stream: SocketStream
-    dummyStream: StringStream
     len: int
 
   BufferInterface* = ref object
@@ -146,16 +145,12 @@ type
 proc getFromOpaque[T](opaque: pointer, res: var T) =
   let opaque = cast[InterfaceOpaque](opaque)
   if opaque.len != 0:
-    let dummyStream = opaque.dummyStream
-    dummyStream.setPosition(0)
-    dummyStream.data = newString(opaque.len)
-    let n = opaque.stream.readData(addr dummyStream.data[0], opaque.len)
-    assert n == opaque.len
-    dummyStream.sread(res)
+    var r = opaque.stream.initReader(opaque.len)
+    r.sread(res)
 
 proc newBufferInterface*(stream: SocketStream, registerFun: proc(fd: int)):
     BufferInterface =
-  let opaque = InterfaceOpaque(stream: stream, dummyStream: newStringStream())
+  let opaque = InterfaceOpaque(stream: stream)
   result = BufferInterface(
     map: newPromiseMap(cast[pointer](opaque)),
     packetid: 1, # ids below 1 are invalid
@@ -976,7 +971,7 @@ proc clone*(buffer: Buffer, newurl: URL): int {.proxy.} =
       # We ignore errors; not much we can do with them here :/
       discard buffer.rewind(buffer.bytesRead, unregister = false)
     buffer.pstream.close()
-    let ssock = initServerSocket(myPid, buffered = false)
+    let ssock = initServerSocket(myPid)
     buffer.ssock = ssock
     ps.write(char(0))
     buffer.url = newurl
@@ -1712,7 +1707,7 @@ proc markURL*(buffer: Buffer; schemes: seq[string]) {.proxy.} =
   buffer.do_reshape()
 
 macro bufferDispatcher(funs: static ProxyMap; buffer: Buffer;
-    cmd: BufferCommand; packetid: int) =
+    cmd: BufferCommand; packetid: int; r: var BufferedReader) =
   let switch = newNimNode(nnkCaseStmt)
   switch.add(ident("cmd"))
   for k, v in funs:
@@ -1727,7 +1722,7 @@ macro bufferDispatcher(funs: static ProxyMap; buffer: Buffer;
         let typ = param[^2]
         stmts.add(quote do:
           var `id`: `typ`
-          `buffer`.dummyStream.sread(`id`)
+          `r`.sread(`id`)
         )
         call.add(id)
     var rval: NimNode
@@ -1773,13 +1768,10 @@ proc readCommand(buffer: Buffer) =
   var len: int
   var packetid: int
   buffer.pstream.sread(len)
-  buffer.dummyStream.setPosition(0)
-  buffer.dummyStream.data = newString(len)
-  let n = buffer.pstream.readData(addr buffer.dummyStream.data[0], len)
-  assert n == len
-  buffer.dummyStream.sread(cmd)
-  buffer.dummyStream.sread(packetid)
-  bufferDispatcher(ProxyFunctions, buffer, cmd, packetid)
+  var r = buffer.pstream.initReader(len)
+  r.sread(cmd)
+  r.sread(packetid)
+  bufferDispatcher(ProxyFunctions, buffer, cmd, packetid, r)
 
 proc handleRead(buffer: Buffer, fd: int): bool =
   if fd == buffer.rfd:
@@ -1869,8 +1861,7 @@ proc launchBuffer*(config: BufferConfig; url: URL; request: Request;
     url: url,
     charsetStack: charsetStack,
     cacheId: -1,
-    outputId: -1,
-    dummyStream: newStringStream()
+    outputId: -1
   )
   buffer.charset = buffer.charsetStack.pop()
   socks.sread(buffer.loader.key)
diff --git a/src/server/forkserver.nim b/src/server/forkserver.nim
index 4a616a2e..60981213 100644
--- a/src/server/forkserver.nim
+++ b/src/server/forkserver.nim
@@ -5,6 +5,7 @@ import std/streams
 import std/tables
 
 import config/config
+import io/bufreader
 import io/bufwriter
 import io/posixstream
 import io/serialize
@@ -24,7 +25,7 @@ type
     fcForkBuffer, fcForkLoader, fcRemoveChild, fcLoadConfig
 
   ForkServer* = ref object
-    istream: Stream
+    istream: PosixStream
     ostream: PosixStream
     estream*: PosixStream
 
@@ -35,7 +36,7 @@ type
     loaderPid: int
 
 proc newFileLoader*(forkserver: ForkServer; config: LoaderConfig): FileLoader =
-  forkserver.ostream.withWriter w:
+  forkserver.ostream.withPacketWriter w:
     w.swrite(fcForkLoader)
     w.swrite(config)
   var process: int
@@ -43,19 +44,19 @@ proc newFileLoader*(forkserver: ForkServer; config: LoaderConfig): FileLoader =
   return FileLoader(process: process, clientPid: getCurrentProcessId())
 
 proc loadForkServerConfig*(forkserver: ForkServer, config: Config) =
-  forkserver.ostream.withWriter w:
+  forkserver.ostream.withPacketWriter w:
     w.swrite(fcLoadConfig)
     w.swrite(config.getForkServerConfig())
 
 proc removeChild*(forkserver: ForkServer, pid: int) =
-  forkserver.ostream.withWriter w:
+  forkserver.ostream.withPacketWriter w:
     w.swrite(fcRemoveChild)
     w.swrite(pid)
 
 proc forkBuffer*(forkserver: ForkServer; config: BufferConfig; url: URL;
     request: Request; attrs: WindowAttributes; ishtml: bool;
     charsetStack: seq[Charset]): int =
-  forkserver.ostream.withWriter w:
+  forkserver.ostream.withPacketWriter w:
     w.swrite(fcForkBuffer)
     w.swrite(config)
     w.swrite(url)
@@ -109,19 +110,19 @@ proc forkLoader(ctx: var ForkServerContext, config: LoaderConfig): int =
   return pid
 
 var gssock: ServerSocket
-proc forkBuffer(ctx: var ForkServerContext): int =
+proc forkBuffer(ctx: var ForkServerContext; r: var BufferedReader): int =
   var config: BufferConfig
   var url: URL
   var request: Request
   var attrs: WindowAttributes
   var ishtml: bool
   var charsetStack: seq[Charset]
-  ctx.istream.sread(config)
-  ctx.istream.sread(url)
-  ctx.istream.sread(request)
-  ctx.istream.sread(attrs)
-  ctx.istream.sread(ishtml)
-  ctx.istream.sread(charsetStack)
+  r.sread(config)
+  r.sread(url)
+  r.sread(request)
+  r.sread(attrs)
+  r.sread(ishtml)
+  r.sread(charsetStack)
   var pipefd: array[2, cint]
   if pipe(pipefd) == -1:
     raise newException(Defect, "Failed to open pipe.")
@@ -137,7 +138,7 @@ proc forkBuffer(ctx: var ForkServerContext): int =
     zeroMem(addr ctx, sizeof(ctx))
     discard close(pipefd[0]) # close read
     let pid = getCurrentProcessId()
-    let ssock = initServerSocket(pid, buffered = false)
+    let ssock = initServerSocket(pid)
     gssock = ssock
     onSignal SIGTERM:
       # This will be overridden after buffer has been set up; it is only
@@ -182,34 +183,35 @@ proc runForkServer() =
   signal(SIGCHLD, SIG_IGN)
   while true:
     try:
-      var cmd: ForkCommand
-      ctx.istream.sread(cmd)
-      case cmd
-      of fcRemoveChild:
-        var pid: int
-        ctx.istream.sread(pid)
-        let i = ctx.children.find(pid)
-        if i != -1:
-          ctx.children.del(i)
-      of fcForkBuffer:
-        let r = ctx.forkBuffer()
-        ctx.ostream.withWriter w:
-          w.swrite(r)
-      of fcForkLoader:
-        assert ctx.loaderPid == 0
-        var config: LoaderConfig
-        ctx.istream.sread(config)
-        let pid = ctx.forkLoader(config)
-        ctx.ostream.withWriter w:
-          w.swrite(pid)
-        ctx.loaderPid = pid
-        ctx.children.add(pid)
-      of fcLoadConfig:
-        var config: ForkServerConfig
-        ctx.istream.sread(config)
-        set_cjk_ambiguous(config.ambiguous_double)
-        SocketDirectory = config.tmpdir
-      ctx.ostream.flush()
+      ctx.istream.withPacketReader r:
+        var cmd: ForkCommand
+        r.sread(cmd)
+        case cmd
+        of fcRemoveChild:
+          var pid: int
+          r.sread(pid)
+          let i = ctx.children.find(pid)
+          if i != -1:
+            ctx.children.del(i)
+        of fcForkBuffer:
+          let r = ctx.forkBuffer(r)
+          ctx.ostream.withWriter w:
+            w.swrite(r)
+        of fcForkLoader:
+          assert ctx.loaderPid == 0
+          var config: LoaderConfig
+          r.sread(config)
+          let pid = ctx.forkLoader(config)
+          ctx.ostream.withWriter w:
+            w.swrite(pid)
+          ctx.loaderPid = pid
+          ctx.children.add(pid)
+        of fcLoadConfig:
+          var config: ForkServerConfig
+          r.sread(config)
+          set_cjk_ambiguous(config.ambiguous_double)
+          SocketDirectory = config.tmpdir
+        ctx.ostream.flush()
     except EOFError:
       # EOF
       break
@@ -255,13 +257,10 @@ proc newForkServer*(): ForkServer =
     discard close(pipefd_in[0]) # close read
     discard close(pipefd_out[1]) # close write
     discard close(pipefd_err[1]) # close write
-    var readf: File
-    if not open(readf, pipefd_out[0], fmRead):
-      raise newException(Defect, "Failed to open input handle")
     let estream = newPosixStream(pipefd_err[0])
     estream.setBlocking(false)
     return ForkServer(
       ostream: newPosixStream(pipefd_in[1]),
-      istream: newFileStream(readf),
+      istream: newPosixStream(pipefd_out[0]),
       estream: estream
     )