about summary refs log tree commit diff stats
path: root/src/html/formdata.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-05-11 19:56:25 +0200
committerbptato <nincsnevem662@gmail.com>2024-05-11 20:06:10 +0200
commitb8345d19efdecb27139e011e92f89efbb7618c08 (patch)
tree5d0f3de089ef40f1f653ee386771e326f9847cd6 /src/html/formdata.nim
parent2c1d1899e424c5e055214d3647979f7f0ba4dcfe (diff)
downloadchawan-b8345d19efdecb27139e011e92f89efbb7618c08.tar.gz
buffer: fix multipart forms
* fix enctype not getting picked up
* fix form data constructor requiring open() syscall (which gets blocked
  by our seccomp filter)
* add closing boundary to multipart end
* pass fds instead of path names through WebFile/Blob and send those
  through bufwriter/bufreader
Diffstat (limited to 'src/html/formdata.nim')
-rw-r--r--src/html/formdata.nim22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/html/formdata.nim b/src/html/formdata.nim
index 335e36b6..7f099acd 100644
--- a/src/html/formdata.nim
+++ b/src/html/formdata.nim
@@ -1,8 +1,8 @@
-import std/streams
-
 import html/catom
 import html/dom
 import html/enums
+import io/dynstream
+import io/posixstream
 import js/base64
 import js/domexception
 import js/javascript
@@ -16,11 +16,12 @@ import chame/tags
 proc constructEntryList*(form: HTMLFormElement; submitter: Element = nil;
     encoding = "UTF-8"): seq[FormDataEntry]
 
+var urandom* {.global.}: PosixStream
+
 proc generateBoundary(): string =
-  let urandom = newFileStream("/dev/urandom")
-  let s = urandom.readStr(32)
-  urandom.close()
-  # 32 * 4 / 3 (padded) = 44 + prefix string is 22 bytes = 66 bytes
+  var s: array[33, uint8]
+  urandom.recvDataLoop(s)
+  # 33 * 4 / 3 = 44 + prefix string is 22 bytes = 66 bytes
   return "----WebKitFormBoundary" & btoa(s)
 
 proc newFormData0*(): FormData =
@@ -147,8 +148,13 @@ proc constructEntryList*(form: HTMLFormElement; submitter: Element = nil;
           "on"
         entrylist.add((name, value))
       of itFile:
-        #TODO file
-        discard
+        if field.file != nil:
+          entrylist.add(FormDataEntry(
+            name: name,
+            filename: field.file.name,
+            isstr: false,
+            value: field.file
+          ))
       of itHidden:
         if name.equalsIgnoreCase("_charset_"):
           entrylist.add((name, encoding))