From 885a3493b6cad4b4247a200928fe61e41883aaba Mon Sep 17 00:00:00 2001 From: bptato Date: Tue, 13 Aug 2024 22:48:12 +0200 Subject: xhr: progress * fix header case sensitivity issues -> probably still wrong as it discards the original casing. better than nothing, anyway * fix fulfill on generic promises * support standard open() async parameter weirdness * refactor loader response body reading (so bodyRead is no longer mandatory) * actually read response body still missing: response body getters --- src/loader/headers.nim | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/loader/headers.nim') diff --git a/src/loader/headers.nim b/src/loader/headers.nim index 6b598cc2..e09d2267 100644 --- a/src/loader/headers.nim +++ b/src/loader/headers.nim @@ -5,6 +5,7 @@ import monoucha/fromjs import monoucha/javascript import monoucha/jserror import monoucha/quickjs +import monoucha/tojs import types/opt import utils/twtstr @@ -99,7 +100,8 @@ func isForbiddenRequestHeader*(name, value: string): bool = return false func isForbiddenResponseHeaderName*(name: string): bool = - return name in ["Set-Cookie", "Set-Cookie2"] + return name.equalsIgnoreCase("Set-Cookie") or + name.equalsIgnoreCase("Set-Cookie2") proc validate(this: Headers; name, value: string): JSResult[bool] = if not name.isValidHeaderName() or not value.isValidHeaderValue(): @@ -118,7 +120,6 @@ func isNoCorsSafelistedName(name: string): bool = name.equalsIgnoreCase("Content-Language") or name.equalsIgnoreCase("Content-Type") - const CorsUnsafeRequestByte = { char(0x00)..char(0x08), char(0x10)..char(0x1F), '"', '(', ')', ':', '<', '>', '?', '@', '[', '\\', ']', '{', '}', '\e' @@ -145,12 +146,14 @@ func isNoCorsSafelisted(name, value: string): bool = func get0(this: Headers; name: string): string = return this.table[name].join(", ") -proc get(this: Headers; name: string): JSResult[Option[string]] {.jsfunc.} = +proc get*(ctx: JSContext; this: Headers; name: string): JSValue {.jsfunc.} = if not name.isValidHeaderName(): - return errTypeError("Invalid header name") + JS_ThrowTypeError(ctx, "Invalid header name") + return JS_EXCEPTION + let name = name.toHeaderCase() if name notin this.table: - return ok(none(string)) - return ok(some(this.get0(name))) + return JS_NULL + return ctx.toJS(this.get0(name)) proc removeRange(this: Headers) = if this.guard == hgRequestNoCors: @@ -162,18 +165,21 @@ proc append(this: Headers; name, value: string): JSResult[void] {.jsfunc.} = let value = value.strip(chars = HTTPWhitespace) if not ?this.validate(name, value): return ok() + let name = name.toHeaderCase() if this.guard == hgRequestNoCors: if name in this.table: let tmp = this.get0(name) & ", " & value if not name.isNoCorsSafelisted(tmp): return ok() - this.table[name].add(value) - else: - this.table[name] = @[value] + if name in this.table: + this.table[name].add(value) + else: + this.table[name] = @[value] this.removeRange() ok() proc delete(this: Headers; name: string): JSResult[void] {.jsfunc.} = + let name = name.toHeaderCase() if not ?this.validate(name, "") or name notin this.table: return ok() if not name.isNoCorsSafelistedName() and not name.equalsIgnoreCase("Range"): @@ -185,6 +191,7 @@ proc delete(this: Headers; name: string): JSResult[void] {.jsfunc.} = proc has(this: Headers; name: string): JSResult[bool] {.jsfunc.} = if not name.isValidHeaderName(): return errTypeError("Invalid header name") + let name = name.toHeaderCase() return ok(name in this.table) proc set(this: Headers; name, value: string): JSResult[void] {.jsfunc.} = @@ -193,8 +200,7 @@ proc set(this: Headers; name, value: string): JSResult[void] {.jsfunc.} = return if this.guard == hgRequestNoCors and not name.isNoCorsSafelisted(value): return - #TODO do this case insensitively - this.table[name] = @[value] + this.table[name.toHeaderCase()] = @[value] this.removeRange() proc fill(headers: Headers; s: seq[(string, string)]): JSResult[void] = -- cgit 1.4.1-2-gfad0 d='n41' href='#n41'>41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232