about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 00:04:50 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 00:04:50 +0100
commitd9e430c8147c8c2d81b4ca5405786269b2cfc94d (patch)
tree91ddb6edbedc6e8310fedc13106b8b3bdcec2cdc /src/io
parent7a3fb32bff44a581d6926bfaf5f070f8ef062338 (diff)
downloadchawan-d9e430c8147c8c2d81b4ca5405786269b2cfc94d.tar.gz
Add all sorts of config options and cookies
Diffstat (limited to 'src/io')
-rw-r--r--src/io/loader.nim7
-rw-r--r--src/io/request.nim43
2 files changed, 27 insertions, 23 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
index dbca256b..671b9ca3 100644
--- a/src/io/loader.nim
+++ b/src/io/loader.nim
@@ -26,6 +26,7 @@ import io/urlfilter
 import ips/serialize
 import ips/serversocket
 import ips/socketstream
+import types/cookie
 import types/mime
 import types/url
 import utils/twtstr
@@ -84,7 +85,7 @@ proc loadResource(request: Request, ostream: Stream) =
     ostream.flush()
 
 var ssock: ServerSocket
-proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter) =
+proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter, cookiejar: CookieJar) =
   if curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK:
     raise newException(Defect, "Failed to initialize libcurl.")
   ssock = initServerSocket()
@@ -116,6 +117,10 @@ proc runFileLoader*(fd: cint, defaultHeaders: HeaderList, filter: URLFilter) =
           for k, v in defaultHeaders.table:
             if k notin request.headers.table:
               request.headers.table[k] = v
+          if cookiejar != nil and cookiejar.cookies.len > 0:
+            if request.url.host == cookiejar.location.host:
+              if "Cookie" notin request.headers.table:
+                request.headers["Cookie"] = $cookiejar
           loadResource(request, stream)
         stream.close()
       of QUIT:
diff --git a/src/io/request.nim b/src/io/request.nim
index 9332f9ff..7587b2e0 100644
--- a/src/io/request.nim
+++ b/src/io/request.nim
@@ -128,30 +128,29 @@ func newHeaderList*(table: Table[string, string]): HeaderList =
     else:
       result.table[k] = @[v]
 
-func newRequest*(url: Url,
-                 httpmethod = HTTP_GET,
-                 headers: seq[(string, string)] = @[],
-                 body = none(string),
+func newRequest*(url: Url, httpmethod: HttpMethod, headers: HeaderList,
+                 body = none(string), multipart = none(MimeData)): Request =
+  return Request(
+    url: url,
+    httpmethod: httpmethod,
+    headers: headers,
+    body: body,
+    multipart: multipart
+  )
+
+func newRequest*(url: Url, httpmethod = HTTP_GET,
+                 headers: seq[(string, string)] = @[], body = none(string),
                  multipart = none(MimeData)): Request {.jsctor.} =
-  new(result)
-  result.httpmethod = httpmethod
-  result.url = url
-  result.headers = newHeaderList()
-  for it in headers:
-    if it[1] != "": #TODO not sure if this is a good idea, options would probably work better
-      result.headers.table[it[0]] = @[it[1]]
-  result.body = body
-  result.multipart = multipart
-
-func newRequest*(url: Url,
-                 httpmethod: HttpMethod,
-                 headers: openarray[(string, string)],
-                 body = none(string),
+  let hl = newHeaderList()
+  for pair in headers:
+    let (k, v) = pair
+    hl.table[k] = @[v]
+  return newRequest(url, httpmethod, hl, body, multipart)
+
+func newRequest*(url: Url, httpmethod: HttpMethod,
+                 headers: openarray[(string, string)], body = none(string),
                  multipart = none(MimeData)): Request =
-  var s: seq[(string, string)]
-  for it in headers:
-    s.add(it)
-  return newRequest(url, httpmethod, s, body, multipart)
+  return newRequest(url, httpmethod, @headers, body, multipart)
 
 proc `[]=`*(multipart: var MimeData, k, v: string) =
   multipart.content.add(MimePart(name: k, content: v))