diff options
author | bptato <nincsnevem662@gmail.com> | 2023-07-01 23:31:39 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-07-01 23:36:15 +0200 |
commit | 51da99365959f4fd35cef7b5f8f919386f2cf4ca (patch) | |
tree | 224426e877284c6199afa84291f07e530693f381 | |
parent | 2f74b628f3c91860860cc19fc07db5c304e8e6ed (diff) | |
download | chawan-51da99365959f4fd35cef7b5f8f919386f2cf4ca.tar.gz |
Factor out headers into separate module
-rw-r--r-- | src/config/config.nim | 2 | ||||
-rw-r--r-- | src/display/client.nim | 2 | ||||
-rw-r--r-- | src/display/pager.nim | 1 | ||||
-rw-r--r-- | src/html/env.nim | 2 | ||||
-rw-r--r-- | src/io/about.nim | 1 | ||||
-rw-r--r-- | src/io/file.nim | 2 | ||||
-rw-r--r-- | src/io/headers.nim | 64 | ||||
-rw-r--r-- | src/io/http.nim | 1 | ||||
-rw-r--r-- | src/io/loader.nim | 1 | ||||
-rw-r--r-- | src/io/request.nim | 59 | ||||
-rw-r--r-- | src/io/response.nim | 1 | ||||
-rw-r--r-- | src/ips/forkserver.nim | 2 |
12 files changed, 77 insertions, 61 deletions
diff --git a/src/config/config.nim b/src/config/config.nim index 9d0a9350..7fc0b2b1 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -6,7 +6,7 @@ import streams import buffer/cell import config/toml import data/charset -import io/request +import io/headers import io/urlfilter import js/javascript import js/regex diff --git a/src/display/client.nim b/src/display/client.nim index 3f4039df..2f99a7d5 100644 --- a/src/display/client.nim +++ b/src/display/client.nim @@ -23,6 +23,7 @@ import display/pager import display/term import html/dom import html/htmlparser +import io/headers import io/lineedit import io/loader import io/posixstream @@ -587,6 +588,7 @@ proc newClient*(config: Config, dispatcher: Dispatcher): Client = ctx.addIntlModule() ctx.addBlobModule() ctx.addFormDataModule() + ctx.addHeadersModule() ctx.addRequestModule() ctx.addResponseModule() ctx.addLineEditModule() diff --git a/src/display/pager.nim b/src/display/pager.nim index b95fa6ea..a0437a97 100644 --- a/src/display/pager.nim +++ b/src/display/pager.nim @@ -14,6 +14,7 @@ import buffer/container import config/config import data/charset import display/term +import io/headers import io/lineedit import io/loader import io/promise diff --git a/src/html/env.nim b/src/html/env.nim index 09cf1c5c..174f0a12 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -3,6 +3,7 @@ import streams import html/dom import html/htmlparser +import io/headers import io/loader import io/promise import io/request @@ -123,6 +124,7 @@ proc addScripting*(window: Window, selector: Selector[int]) = ctx.addIntlModule() ctx.addBlobModule() ctx.addFormDataModule() + ctx.addHeadersModule() ctx.addRequestModule() ctx.addResponseModule() diff --git a/src/io/about.nim b/src/io/about.nim index b852c556..97e01133 100644 --- a/src/io/about.nim +++ b/src/io/about.nim @@ -1,6 +1,7 @@ import streams import tables +import io/headers import io/request import ips/serialize import types/url diff --git a/src/io/file.nim b/src/io/file.nim index 24dea51c..b9fe0ce2 100644 --- a/src/io/file.nim +++ b/src/io/file.nim @@ -3,7 +3,7 @@ import os import streams import tables -import io/request +import io/headers import ips/serialize import types/url diff --git a/src/io/headers.nim b/src/io/headers.nim new file mode 100644 index 00000000..3bc3659b --- /dev/null +++ b/src/io/headers.nim @@ -0,0 +1,64 @@ +import tables + +import js/javascript +import utils/twtstr + +type Headers* = ref object + table* {.jsget.}: Table[string, seq[string]] + +jsDestructor(Headers) + +proc fill*(headers: Headers, ctx: JSContext, val: JSValue) = + if isSequence(ctx, val): + let x = fromJS[seq[(string, string)]](ctx, val) + if x.isSome: + for (k, v) in x.get: + if k in headers.table: + headers.table[k].add(v) + else: + headers.table[k] = @[v] + else: + let x = fromJS[Table[string, string]](ctx, val) + if x.isSome: + for k, v in x.get: + if k in headers.table: + headers.table[k].add(v) + else: + headers.table[k] = @[v] + +func newHeaders*(): Headers = + new(result) + +func newHeaders*(ctx: JSContext, obj = none(JSValue)): Headers {.jsctor.} = + new(result) + if obj.isSome: + result.fill(ctx, obj.get) + +func newHeaders*(table: Table[string, string]): Headers = + new(result) + for k, v in table: + let k = k.toHeaderCase() + if k in result.table: + result.table[k].add(v) + else: + result.table[k] = @[v] + +proc add*(headers: var Headers, k, v: string) = + let k = k.toHeaderCase() + if k notin headers.table: + headers.table[k] = @[v] + else: + headers.table[k].add(v) + +proc `[]=`*(headers: var Headers, k, v: string) = + headers.table[k.toHeaderCase()] = @[v] + +func getOrDefault*(headers: Headers, k: string, default = ""): string = + let k = k.toHeaderCase() + if k in headers.table: + headers.table[k][0] + else: + default + +proc addHeadersModule*(ctx: JSContext) = + ctx.registerType(Headers) diff --git a/src/io/http.nim b/src/io/http.nim index eaff6aa7..1ebcaf72 100644 --- a/src/io/http.nim +++ b/src/io/http.nim @@ -3,6 +3,7 @@ import streams import strutils import bindings/curl +import io/headers import io/request import ips/serialize import types/blob diff --git a/src/io/loader.nim b/src/io/loader.nim index 0ce19c38..e88a4a69 100644 --- a/src/io/loader.nim +++ b/src/io/loader.nim @@ -22,6 +22,7 @@ when defined(posix): import bindings/curl import io/about import io/file +import io/headers import io/http import io/posixstream import io/promise diff --git a/src/io/request.nim b/src/io/request.nim index 93ec9b7d..a5a0158f 100644 --- a/src/io/request.nim +++ b/src/io/request.nim @@ -4,11 +4,11 @@ import strutils import tables import bindings/quickjs +import io/headers import js/exception import js/javascript import types/formdata import types/url -import utils/twtstr type HttpMethod* = enum @@ -80,11 +80,7 @@ type buf: string isend: bool - Headers* = ref object - table* {.jsget.}: Table[string, seq[string]] - jsDestructor(Request) -jsDestructor(Headers) proc js_url(this: Request): string {.jsfget: "url".} = return $this.url @@ -153,41 +149,6 @@ proc newReadableStream*(isource: Stream): ReadableStream = else: result.isource.readStr(len, result.buf) -proc fill(headers: Headers, ctx: JSContext, val: JSValue) = - if isSequence(ctx, val): - let x = fromJS[seq[(string, string)]](ctx, val) - if x.isSome: - for (k, v) in x.get: - if k in headers.table: - headers.table[k].add(v) - else: - headers.table[k] = @[v] - else: - let x = fromJS[Table[string, string]](ctx, val) - if x.isSome: - for k, v in x.get: - if k in headers.table: - headers.table[k].add(v) - else: - headers.table[k] = @[v] - -func newHeaders*(): Headers = - new(result) - -func newHeaders*(ctx: JSContext, obj = none(JSValue)): Headers {.jsctor.} = - new(result) - if obj.isSome: - result.fill(ctx, obj.get) - -func newHeaders*(table: Table[string, string]): Headers = - new(result) - for k, v in table: - let k = k.toHeaderCase() - if k in result.table: - result.table[k].add(v) - else: - result.table[k] = @[v] - func newRequest*(url: URL, httpmethod = HTTP_GET, headers = newHeaders(), body = opt(string), multipart = opt(FormData), mode = RequestMode.NO_CORS, credentialsMode = CredentialsMode.SAME_ORIGIN, @@ -267,23 +228,6 @@ func newRequest*(ctx: JSContext, resource: string, return ok(newRequest(url, httpMethod, hl, body, multipart, mode, credentials, proxy = proxyUrl.get(nil))) -proc add*(headers: var Headers, k, v: string) = - let k = k.toHeaderCase() - if k notin headers.table: - headers.table[k] = @[v] - else: - headers.table[k].add(v) - -proc `[]=`*(headers: var Headers, k, v: string) = - headers.table[k.toHeaderCase()] = @[v] - -func getOrDefault*(headers: Headers, k: string, default = ""): string = - let k = k.toHeaderCase() - if k in headers.table: - headers.table[k][0] - else: - default - func credentialsMode*(attribute: CORSAttribute): CredentialsMode = case attribute of NO_CORS, ANONYMOUS: @@ -293,4 +237,3 @@ func credentialsMode*(attribute: CORSAttribute): CredentialsMode = proc addRequestModule*(ctx: JSContext) = ctx.registerType(Request) - ctx.registerType(Headers) diff --git a/src/io/response.nim b/src/io/response.nim index 5c0253e9..321f43d0 100644 --- a/src/io/response.nim +++ b/src/io/response.nim @@ -1,6 +1,7 @@ import streams import bindings/quickjs +import io/headers import io/promise import io/request import js/exception diff --git a/src/ips/forkserver.nim b/src/ips/forkserver.nim index 1a5c2ea0..e2e825c1 100644 --- a/src/ips/forkserver.nim +++ b/src/ips/forkserver.nim @@ -6,9 +6,9 @@ when defined(posix): import buffer/buffer import config/config +import io/headers import io/loader import io/posixstream -import io/request import io/urlfilter import io/window import ips/serialize |