diff options
author | bptato <nincsnevem662@gmail.com> | 2022-01-23 01:02:44 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-01-23 01:02:44 +0100 |
commit | 010185f7c306f2465b691a82fcbfe1c76513f8fc (patch) | |
tree | b755627c1ff776485cf04a469d3dcdeebe2639c8 /src/io/loader.nim | |
parent | 6ff61c5ad2ad2af36195b83582ed98be57b93f18 (diff) | |
download | chawan-010185f7c306f2465b691a82fcbfe1c76513f8fc.tar.gz |
Support external stylesheets
Diffstat (limited to 'src/io/loader.nim')
-rw-r--r-- | src/io/loader.nim | 33 |
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 |