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 /src/io/request.nim | |
parent | 2f74b628f3c91860860cc19fc07db5c304e8e6ed (diff) | |
download | chawan-51da99365959f4fd35cef7b5f8f919386f2cf4ca.tar.gz |
Factor out headers into separate module
Diffstat (limited to 'src/io/request.nim')
-rw-r--r-- | src/io/request.nim | 59 |
1 files changed, 1 insertions, 58 deletions
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) |