about summary refs log tree commit diff stats
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/types')
-rw-r--r--src/types/blob.nim38
-rw-r--r--src/types/formdata.nim25
-rw-r--r--src/types/url.nim26
3 files changed, 89 insertions, 0 deletions
diff --git a/src/types/blob.nim b/src/types/blob.nim
index c0278bde..cc69fd7c 100644
--- a/src/types/blob.nim
+++ b/src/types/blob.nim
@@ -2,6 +2,8 @@ import std/options
 import std/posix
 import std/strutils
 
+import io/bufreader
+import io/bufwriter
 import monoucha/fromjs
 import monoucha/javascript
 import monoucha/jstypes
@@ -25,6 +27,42 @@ type
 jsDestructor(Blob)
 jsDestructor(WebFile)
 
+# Forward declarations
+proc deallocBlob*(opaque, p: pointer) {.raises: [].}
+
+#TODO it would be nice if we had a separate fd type that does sendAux;
+# this solution fails when blob isn't swritten by some module that does
+# not import it (just as a transitive dependency).
+proc swrite*(writer: var BufferedWriter; blob: Blob) =
+  if blob.fd.isSome:
+    writer.sendAux.add(blob.fd.get)
+  writer.swrite(blob of WebFile)
+  if blob of WebFile:
+    writer.swrite(WebFile(blob).name)
+  writer.swrite(blob.fd.isSome)
+  writer.swrite(blob.ctype)
+  writer.swrite(blob.size)
+  if blob.size > 0:
+    writer.writeData(blob.buffer, int(blob.size))
+
+proc sread*(reader: var BufferedReader; blob: var Blob) =
+  var isWebFile: bool
+  reader.sread(isWebFile)
+  blob = if isWebFile: WebFile() else: Blob()
+  if isWebFile:
+    reader.sread(WebFile(blob).name)
+  var hasFd: bool
+  reader.sread(hasFd)
+  if hasFd:
+    blob.fd = some(reader.recvAux.pop())
+  reader.sread(blob.ctype)
+  reader.sread(blob.size)
+  if blob.size > 0:
+    let buffer = alloc(blob.size)
+    reader.readData(blob.buffer, int(blob.size))
+    blob.buffer = buffer
+    blob.deallocFun = deallocBlob
+
 proc newBlob*(buffer: pointer; size: int; ctype: string;
     deallocFun: DeallocFun; opaque: pointer = nil): Blob =
   return Blob(
diff --git a/src/types/formdata.nim b/src/types/formdata.nim
index 9ce881f0..c57eda10 100644
--- a/src/types/formdata.nim
+++ b/src/types/formdata.nim
@@ -1,5 +1,7 @@
 import std/strutils
 
+import io/bufreader
+import io/bufwriter
 import io/dynstream
 import monoucha/javascript
 import types/blob
@@ -21,6 +23,29 @@ type
 
 jsDestructor(FormData)
 
+proc swrite*(writer: var BufferedWriter; part: FormDataEntry) =
+  writer.swrite(part.isstr)
+  writer.swrite(part.name)
+  writer.swrite(part.filename)
+  if part.isstr:
+    writer.swrite(part.svalue)
+  else:
+    writer.swrite(part.value)
+
+proc sread*(reader: var BufferedReader; part: var FormDataEntry) =
+  var isstr: bool
+  reader.sread(isstr)
+  if isstr:
+    part = FormDataEntry(isstr: true)
+  else:
+    part = FormDataEntry(isstr: false)
+  reader.sread(part.name)
+  reader.sread(part.filename)
+  if part.isstr:
+    reader.sread(part.svalue)
+  else:
+    reader.sread(part.value)
+
 iterator items*(this: FormData): FormDataEntry {.inline.} =
   for entry in this.entries:
     yield entry
diff --git a/src/types/url.nim b/src/types/url.nim
index 9e323847..b676a2b5 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -5,6 +5,8 @@ import std/strutils
 import std/tables
 import std/unicode
 
+import io/bufreader
+import io/bufwriter
 import lib/punycode
 import monoucha/fromjs
 import monoucha/javascript
@@ -85,6 +87,30 @@ type
 jsDestructor(URL)
 jsDestructor(URLSearchParams)
 
+# Forward declarations
+proc parseURL*(input: string; base = none(URL); override = none(URLState)):
+    Option[URL]
+func serialize*(url: URL; excludefragment = false; excludepassword = false):
+    string
+
+proc swrite*(writer: var BufferedWriter; url: URL) =
+  if url != nil:
+    writer.swrite(url.serialize())
+  else:
+    writer.swrite("")
+
+proc sread*(reader: var BufferedReader; url: var URL) =
+  var s: string
+  reader.sread(s)
+  if s == "":
+    url = nil
+  else:
+    let x = parseURL(s)
+    if x.isSome:
+      url = x.get
+    else:
+      url = nil
+
 const EmptyPath = URLPath(opaque: true, s: "")
 const EmptyHost = Host(t: htDomain, domain: "")