about summary refs log tree commit diff stats
path: root/src/io/loader.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-01-23 01:02:44 +0100
committerbptato <nincsnevem662@gmail.com>2022-01-23 01:02:44 +0100
commit010185f7c306f2465b691a82fcbfe1c76513f8fc (patch)
treeb755627c1ff776485cf04a469d3dcdeebe2639c8 /src/io/loader.nim
parent6ff61c5ad2ad2af36195b83582ed98be57b93f18 (diff)
downloadchawan-010185f7c306f2465b691a82fcbfe1c76513f8fc.tar.gz
Support external stylesheets
Diffstat (limited to 'src/io/loader.nim')
-rw-r--r--src/io/loader.nim33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/io/loader.nim b/src/io/loader.nim
new file mode 100644
index 00000000..3ef5320e
--- /dev/null
+++ b/src/io/loader.nim
@@ -0,0 +1,33 @@
+import httpclient
+import options
+import streams
+
+import types/mime
+import types/url
+import utils/twtstr
+
+type
+  FileLoader* = ref object
+    http: HttpClient
+
+  LoadResult* = object
+    s*: Stream
+    contenttype*: string
+
+proc newFileLoader*(): FileLoader =
+  new(result)
+  result.http = newHttpClient()
+
+proc getPage*(loader: FileLoader, url: Url): LoadResult =
+  if url.scheme == "file":
+    let path = url.path.serialize_unicode()
+    result.contenttype = guessContentType(path)
+    result.s = newFileStream(path, fmRead)
+  elif url.scheme == "http" or url.scheme == "https":
+    let resp = loader.http.get(url.serialize(true))
+    let ct = resp.contentType()
+    if ct != "":
+      result.contenttype = ct.until(';')
+    else:
+      result.contenttype = guessContentType(url.path.serialize())
+    result.s = resp.bodyStream