about summary refs log tree commit diff stats
path: root/src/loader/loaderhandle.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-10 20:20:34 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-10 20:25:29 +0100
commitd8c4b0979c6d1ff9f6edea650e3aeb1ca1e4a104 (patch)
treee585452d7e855f7e66d69dd37ffe1f7758fe9e7f /src/loader/loaderhandle.nim
parent7cb7ea1db2b4566fde492c1e0cc4c42f245dea63 (diff)
downloadchawan-d8c4b0979c6d1ff9f6edea650e3aeb1ca1e4a104.tar.gz
loader: fixes & cleanup
* LoaderHandle.fd is no more, we now check ostream's fd
* setBlocking converted to a PosixStream method
* SocketStream now sets fd variable
* handle sostream/fd redirection properly
* fix suspend/resume

This fixes non-HTML resource loading, mostly. However, tee is still
broken :/
Diffstat (limited to 'src/loader/loaderhandle.nim')
-rw-r--r--src/loader/loaderhandle.nim66
1 files changed, 10 insertions, 56 deletions
diff --git a/src/loader/loaderhandle.nim b/src/loader/loaderhandle.nim
index 06149e23..15336964 100644
--- a/src/loader/loaderhandle.nim
+++ b/src/loader/loaderhandle.nim
@@ -2,7 +2,6 @@ import std/deques
 import std/net
 import std/streams
 
-import io/multistream
 import io/posixstream
 import io/serialize
 import io/socketstream
@@ -20,6 +19,8 @@ type
 
   LoaderBuffer* = ptr LoaderBufferObj
 
+  OutputHandle* = object
+
   LoaderHandle* = ref object
     ostream*: PosixStream #TODO un-extern
     # Stream for taking input
@@ -28,9 +29,7 @@ type
     # redirect the first handle and b) async redirects would result in race
     # conditions that would be difficult to untangle.
     canredir: bool
-    sostream: Stream # saved ostream when redirected
-    sostream_suspend: Stream # saved ostream when suspended
-    fd*: int # ostream fd
+    sostream*: PosixStream # saved ostream when redirected
     currentBuffer*: LoaderBuffer
     currentBufferIdx*: int
     buffers: Deque[LoaderBuffer]
@@ -41,8 +40,7 @@ type
 proc newLoaderHandle*(ostream: PosixStream, canredir: bool): LoaderHandle =
   return LoaderHandle(
     ostream: ostream,
-    canredir: canredir,
-    fd: int(SocketStream(ostream).source.getFd())
+    canredir: canredir
   )
 
 func `[]`*(buffer: LoaderBuffer, i: int): var uint8 {.inline.} =
@@ -78,30 +76,10 @@ proc bufferCleared*(handle: LoaderHandle) =
     handle.currentBuffer = nil
 
 proc addOutputStream*(handle: LoaderHandle, stream: Stream) =
-  if likely(handle.sostream_suspend != nil):
-    let ms = newMultiStream(handle.sostream_suspend, stream)
-    handle.sostream_suspend = ms
-  else:
-    # In buffer, addOutputStream is used as follows:
-    # * suspend handle
-    # * tee handle (-> call addOutputStream)
-    # * resume handle
-    # This means that this code path will never be executed, as
-    # sostream_suspend is never nil when the function is called.
-    # (Feel free to remove this assertion if this changes.)
-    doAssert false
-    #TODO TODO TODO fix this
-    #let ms = newMultiStream(handle.ostream, stream)
-    #handle.ostream = ms
-
-proc setBlocking*(handle: LoaderHandle, blocking: bool) =
-  #TODO this is stupid
-  if handle.sostream_suspend != nil and handle.sostream_suspend of SocketStream:
-    SocketStream(handle.sostream_suspend).setBlocking(blocking)
-  elif handle.sostream != nil and handle.sostream of SocketStream:
-    SocketStream(handle.sostream).setBlocking(blocking)
-  else:
-    SocketStream(handle.ostream).setBlocking(blocking)
+  doAssert false
+  #TODO TODO TODO fix this
+  #let ms = newMultiStream(handle.ostream, stream)
+  #handle.ostream = ms
 
 proc sendResult*(handle: LoaderHandle, res: int, msg = "") =
   handle.ostream.swrite(res)
@@ -121,37 +99,13 @@ proc sendHeaders*(handle: LoaderHandle, headers: Headers) =
     if redir:
       let fd = SocketStream(handle.ostream).recvFileHandle()
       handle.sostream = handle.ostream
-      let stream = newPosixStream(fd)
-      handle.ostream = stream
+      handle.ostream = newPosixStream(fd)
 
 proc sendData*(handle: LoaderHandle, p: pointer, nmemb: int): int =
   return handle.ostream.sendData(p, nmemb)
 
-proc suspend*(handle: LoaderHandle) =
-  #TODO TODO TODO fix suspend
-  doAssert false
-  handle.sostream_suspend = handle.ostream
-  #handle.ostream = newStringStream()
-
-proc resume*(handle: LoaderHandle) =
-  #TODO TODO TODO fix resume
-  doAssert false
-  #[
-  let ss = handle.ostream
-  handle.ostream = handle.sostream_suspend
-  handle.sostream_suspend = nil
-  handle.sendData(ss.readAll())
-  ss.close()
-  ]#
-
 proc close*(handle: LoaderHandle) =
-  if handle.sostream != nil:
-    try:
-      handle.sostream.swrite(true)
-    except IOError:
-      # ignore error, that just means the buffer has already closed the stream
-      discard
-    handle.sostream.close()
+  assert handle.sostream == nil
   if handle.ostream != nil:
     handle.ostream.close()
     handle.ostream = nil
109'>109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292