about summary refs log tree commit diff stats
path: root/src/server
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-11 00:40:51 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-11 00:40:51 +0100
commitb0583b4760a11bd062781e0ea948c61c8b66ff8f (patch)
tree5bfb46cb1aa57d62fb40d6b6869ac8a875071561 /src/server
parentd0690cfea6a87c7b7d801b968b5a1c85d1e99b4f (diff)
downloadchawan-b0583b4760a11bd062781e0ea948c61c8b66ff8f.tar.gz
Get rid of LOAD_PIPE BufferSource
Instead, use a stream: scheme and associate hostnames with file
descriptors directly from the pager.
Diffstat (limited to 'src/server')
-rw-r--r--src/server/buffer.nim39
-rw-r--r--src/server/forkserver.nim11
2 files changed, 17 insertions, 33 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 851f04f1..817dae82 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -64,8 +64,8 @@ type
     LOAD, RENDER, WINDOW_CHANGE, FIND_ANCHOR, READ_SUCCESS, READ_CANCELED,
     CLICK, FIND_NEXT_LINK, FIND_PREV_LINK, FIND_NTH_LINK, FIND_REV_NTH_LINK,
     FIND_NEXT_MATCH, FIND_PREV_MATCH, GET_SOURCE, GET_LINES, UPDATE_HOVER,
-    PASS_FD, CONNECT, CONNECT2, GOTO_ANCHOR, CANCEL, GET_TITLE, SELECT,
-    REDIRECT_TO_FD, READ_FROM_FD, SET_CONTENT_TYPE, CLONE, FIND_PREV_PARAGRAPH,
+    CONNECT, CONNECT2, GOTO_ANCHOR, CANCEL, GET_TITLE, SELECT, REDIRECT_TO_FD,
+    READ_FROM_FD, SET_CONTENT_TYPE, CLONE, FIND_PREV_PARAGRAPH,
     FIND_NEXT_PARAGRAPH
 
   # LOADING_PAGE: istream open
@@ -774,12 +774,6 @@ proc connect*(buffer: Buffer): ConnectResult {.proxy.} =
       return ConnectResult(code: ERROR_SOURCE_NOT_FOUND)
     if buffer.source.contentType.isNone:
       buffer.source.contentType = some("text/plain")
-  of LOAD_PIPE:
-    discard fcntl(source.fd, F_SETFL, fcntl(source.fd, F_GETFL, 0) or O_NONBLOCK)
-    buffer.istream = newPosixStream(source.fd)
-    buffer.fd = source.fd
-    if buffer.source.contentType.isNone:
-      buffer.source.contentType = some("text/plain")
   of LOAD_REQUEST:
     let request = source.request
     let response = buffer.loader.doRequest(request, blocking = true, canredir = true)
@@ -844,37 +838,27 @@ 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.} =
+proc readFromFd*(buffer: Buffer, url: URL, ishtml: bool) {.proxy.} =
   let contentType = if ishtml:
     "text/html"
   else:
     "text/plain"
+  let request = newRequest(url)
   buffer.source = BufferSource(
-    t: LOAD_PIPE,
-    fd: fd,
+    t: LOAD_REQUEST,
+    request: request,
     location: buffer.source.location,
     contentType: some(contentType),
     charset: buffer.source.charset
   )
   buffer.setHTML(ishtml)
-  discard fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) or O_NONBLOCK)
-  buffer.istream = newPosixStream(fd)
-  buffer.fd = fd
+  let response = buffer.loader.doRequest(request, blocking = true,
+    canredir = false)
+  buffer.istream = response.body
+  buffer.fd = int(SocketStream(response.body).source.getFd())
   buffer.selector.registerHandle(buffer.fd, {Read}, 0)
 
 proc setContentType*(buffer: Buffer, contentType: string) {.proxy.} =
@@ -1635,9 +1619,6 @@ proc getLines*(buffer: Buffer, w: Slice[int]): GetLinesResult {.proxy.} =
     result.lines.add(line)
   result.numLines = buffer.lines.len
 
-proc passFd*(buffer: Buffer, fd: FileHandle) {.proxy.} =
-  buffer.source.fd = fd
-
 #TODO this is mostly broken
 proc getSource*(buffer: Buffer) {.proxy.} =
   let ssock = initServerSocket()
diff --git a/src/server/forkserver.nim b/src/server/forkserver.nim
index 9ad9c8a9..ced32341 100644
--- a/src/server/forkserver.nim
+++ b/src/server/forkserver.nim
@@ -63,15 +63,18 @@ proc removeChild*(forkserver: ForkServer, pid: Pid) =
   forkserver.ostream.flush()
 
 proc forkBuffer*(forkserver: ForkServer, source: BufferSource,
-    config: BufferConfig, attrs: WindowAttributes): Pid =
+    config: BufferConfig, attrs: WindowAttributes):
+    tuple[process, loaderPid: Pid] =
   forkserver.ostream.swrite(FORK_BUFFER)
   forkserver.ostream.swrite(source)
   forkserver.ostream.swrite(config)
   forkserver.ostream.swrite(attrs)
   forkserver.ostream.flush()
   var process: Pid
+  var loaderPid: Pid
   forkserver.istream.sread(process)
-  return process
+  forkserver.istream.sread(loaderPid)
+  return (process, loaderPid)
 
 proc trapSIGINT() =
   # trap SIGINT, so e.g. an external editor receiving an interrupt in the
@@ -114,7 +117,7 @@ proc forkLoader(ctx: var ForkServerContext, config: LoaderConfig): Pid =
   return pid
 
 var gssock: ServerSocket
-proc forkBuffer(ctx: var ForkServerContext): Pid =
+proc forkBuffer(ctx: var ForkServerContext): tuple[process, loaderPid: Pid] =
   var source: BufferSource
   var config: BufferConfig
   var attrs: WindowAttributes
@@ -164,7 +167,7 @@ proc forkBuffer(ctx: var ForkServerContext): Pid =
   assert c == char(0)
   ps.close()
   ctx.children.add((pid, loaderPid))
-  return pid
+  return (pid, loaderPid)
 
 proc runForkServer() =
   var ctx = ForkServerContext(