diff options
author | bptato <nincsnevem662@gmail.com> | 2023-10-14 16:32:40 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-10-14 16:32:40 +0200 |
commit | 204ec2eb1fee0a02f83b6f7ac16a0da1a3238f21 (patch) | |
tree | 163ba0011db16dbb0552616a56ab5639c46c9834 /src/xhr/xmlhttprequest.nim | |
parent | 15461c544e9718fde1e2627c082e91e848fe8d42 (diff) | |
download | chawan-204ec2eb1fee0a02f83b6f7ac16a0da1a3238f21.tar.gz |
XHR progress
still non-functional
Diffstat (limited to 'src/xhr/xmlhttprequest.nim')
-rw-r--r-- | src/xhr/xmlhttprequest.nim | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/src/xhr/xmlhttprequest.nim b/src/xhr/xmlhttprequest.nim index ef0e6601..28fbd50a 100644 --- a/src/xhr/xmlhttprequest.nim +++ b/src/xhr/xmlhttprequest.nim @@ -1,5 +1,13 @@ +import std/options +import std/strutils + import html/event +import js/domexception import js/javascript +import loader/headers +import loader/request +import loader/response +import types/url type XMLHttpRequestResponseType = enum @@ -10,6 +18,16 @@ type TYPE_JSON = "json" TYPE_TEXT = "text" + XMLHttpRequestState = enum + UNSENT = 0u16 + OPENED = 1u16 + HEADERS_RECEIVED = 2u16 + LOADING = 3u16 + DONE = 4u16 + + XMLHttpRequestFlag = enum + SEND_FLAG, UPLOAD_LISTENER_FLAG, SYNC_FLAG + XMLHttpRequestEventTarget = ref object of EventTarget onloadstart {.jsgetset.}: EventHandler onprogress {.jsgetset.}: EventHandler @@ -23,8 +41,14 @@ type XMLHttpRequest = ref object of XMLHttpRequestEventTarget onreadystatechange {.jsgetset.}: EventHandler - readyState {.jsget.}: uint16 + readyState: XMLHttpRequestState upload {.jsget.}: XMLHttpRequestUpload + flags: set[XMLHttpRequestFlag] + requestMethod: HttpMethod + requestURL: URL + authorRequestHeaders: Headers + response: Response + responseType {.jsget.}: XMLHttpRequestResponseType jsDestructor(XMLHttpRequestEventTarget) jsDestructor(XMLHttpRequestUpload) @@ -33,14 +57,54 @@ jsDestructor(XMLHttpRequest) func newXMLHttpRequest(): XMLHttpRequest {.jsctor.} = let upload = XMLHttpRequestUpload() return XMLHttpRequest( - upload: upload + upload: upload, + authorRequestHeaders: newHeaders() ) -proc open(this: XMLHttpRequest, httpMethod, url: string) {.jsfunc.} = - discard #TODO implement +func readyState(this: XMLHttpRequest): uint16 {.jsfget.} = + return uint16(this.readyState) + +proc parseMethod(s: string): DOMResult[HttpMethod] = + return case s.toLowerAscii() + of "get": ok(HTTP_GET) + of "delete": ok(HTTP_DELETE) + of "head": ok(HTTP_HEAD) + of "options": ok(HTTP_OPTIONS) + of "patch": ok(HTTP_PATCH) + of "post": ok(HTTP_POST) + of "put": ok(HTTP_PUT) + of "connect", "trace", "track": + err(newDOMException("Forbidden method", "SecurityError")) + else: + err(newDOMException("Invalid method", "SyntaxError")) + +proc open(this: XMLHttpRequest, httpMethod, url: string): Err[DOMException] + {.jsfunc.} = + let httpMethod = ?parseMethod(httpMethod) + let x = parseURL(url) + if x.isNone: + return err(newDOMException("Invalid URL", "SyntaxError")) + let parsedURL = x.get + #TODO async, username, password arguments + let async = true + #TODO if async is false... probably just throw. + #TODO terminate fetch controller + this.flags.excl(SEND_FLAG) + this.flags.excl(UPLOAD_LISTENER_FLAG) + #TODO set if not async + if async: + this.flags.excl(SYNC_FLAG) + else: + this.flags.incl(SYNC_FLAG) + this.requestMethod = httpMethod + this.authorRequestHeaders = newHeaders() + this.response = makeNetworkError() + this.requestURL = parsedURL + return ok() proc addXMLHttpRequestModule*(ctx: JSContext) = let eventTargetCID = ctx.getClass("EventTarget") let xhretCID = ctx.registerType(XMLHttpRequestEventTarget, eventTargetCID) ctx.registerType(XMLHttpRequestUpload, xhretCID) - ctx.registerType(XMLHttpRequest, xhretCID) + let xhrCID = ctx.registerType(XMLHttpRequest, xhretCID) + ctx.defineConsts(xhrCID, XMLHttpRequestState, uint16) |