@voxgig/sdkgen 4.48.1 → 4.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/voxgig-sdkgen +1 -1
- package/package.json +1 -1
- package/project/.sdk/model/feature/secrets.aontu +3 -0
- package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
- package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
- package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
- package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
- package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
- package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
- package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
- package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
- package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
- package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
- package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
- package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
- package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
- package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
- package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
- package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
- package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
- package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
- package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
- package/project/.sdk/tm/c/feature/cache.c +16 -1
- package/project/.sdk/tm/c/feature/secrets.c +149 -18
- package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
- package/project/.sdk/tm/c/tests/feature_test.c +31 -0
- package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
- package/project/.sdk/tm/clojure/src/sdk/core.clj +8 -1
- package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
- package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
- package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
- package/project/.sdk/tm/cpp/core/types.hpp +1 -11
- package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
- package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
- package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
- package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
- package/project/.sdk/tm/csharp/core/Control.cs +0 -5
- package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
- package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
- package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
- package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
- package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
- package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
- package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
- package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
- package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
- package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
- package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
- package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
- package/project/.sdk/tm/go/test/feature_test.go +32 -0
- package/project/.sdk/tm/go/utility/media.go +2 -2
- package/project/.sdk/tm/java/core/Control.java +0 -5
- package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
- package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
- package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
- package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
- package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
- package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
- package/project/.sdk/tm/java/utility/Media.java +2 -2
- package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
- package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
- package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
- package/project/.sdk/tm/js/test/feature.test.js +17 -0
- package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
- package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
- package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
- package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
- package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
- package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
- package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
- package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
- package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
- package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
- package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
- package/project/.sdk/tm/lua/utility/clean.lua +1 -0
- package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
- package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
- package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
- package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
- package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
- package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
- package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
- package/project/.sdk/tm/perl/t/feature.t +24 -0
- package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
- package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
- package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
- package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
- package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
- package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
- package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
- package/project/.sdk/tm/py/test/test_feature.py +18 -0
- package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
- package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
- package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
- package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
- package/project/.sdk/tm/rust/feature/cache.rs +15 -1
- package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
- package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
- package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
- package/project/.sdk/tm/scala/core/Control.scala +0 -5
- package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
- package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
- package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
- package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
- package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
- package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
- package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
- package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
- package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
- package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
- package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
- package/project/.sdk/tm/zig/feature/cache.zig +15 -1
- package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
- package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
- package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
- package/project/sdkgen-package.json +1 -1
|
@@ -63,6 +63,8 @@
|
|
|
63
63
|
|
|
64
64
|
(def ^:private ERROR-CODE "secrets_provider")
|
|
65
65
|
|
|
66
|
+
(def ^:private REDIRECT-MAX 5)
|
|
67
|
+
|
|
66
68
|
|
|
67
69
|
;; ---------------------------------------------------------------------------
|
|
68
70
|
;; Option reading. Options arrive as vendored struct nodes (LinkedHashMap /
|
|
@@ -169,6 +171,22 @@
|
|
|
169
171
|
(let [o (liveopts fa)] (when (vs/ismap o) (vs/getprop o "auth"))))
|
|
170
172
|
|
|
171
173
|
|
|
174
|
+
;; u-prepare-auth's decision: auth the config declares, not suppressed by
|
|
175
|
+
;; `auth: nil`.
|
|
176
|
+
(defn- authon? [fa]
|
|
177
|
+
(and (st fa :declared) (some? (auth-of fa))))
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
;; Under Basic the token is the key half, as apikey is: u-prepare-auth's pair.
|
|
181
|
+
(defn- pair! [fa token]
|
|
182
|
+
(let [secret (vs/getprop (liveopts fa) "secret")
|
|
183
|
+
joined (.encodeToString (java.util.Base64/getEncoder)
|
|
184
|
+
(.getBytes (str token ":" (if (string? secret) secret ""))
|
|
185
|
+
"UTF-8"))]
|
|
186
|
+
(register! fa joined)
|
|
187
|
+
joined))
|
|
188
|
+
|
|
189
|
+
|
|
172
190
|
;; The resolved credential ("" when none). Callers and tests read it here
|
|
173
191
|
;; rather than from the options map, which this feature never mutates.
|
|
174
192
|
(defn credential [fa] (or (st fa :cred) ""))
|
|
@@ -198,6 +216,25 @@
|
|
|
198
216
|
;; The access-token exchange.
|
|
199
217
|
;; ---------------------------------------------------------------------------
|
|
200
218
|
|
|
219
|
+
(defn- origin [^java.net.URI u]
|
|
220
|
+
(let [scheme (str/lower-case (str (.getScheme u)))
|
|
221
|
+
port (.getPort u)]
|
|
222
|
+
[scheme (str/lower-case (str (.getHost u)))
|
|
223
|
+
(if (neg? port) (get {"http" 80 "https" 443} scheme -1) (long port))]))
|
|
224
|
+
|
|
225
|
+
;; Where a redirect keeping method and body (307 or 308) points, on the
|
|
226
|
+
;; exchange's own origin. Any other would drop the body or leave the origin.
|
|
227
|
+
(defn- moved [url at res]
|
|
228
|
+
(let [status (core/to-int (vs/getprop res "status"))
|
|
229
|
+
location (vs/getprop (vs/getprop res "headers") "location")]
|
|
230
|
+
(when (and (contains? #{307 308} status) (string? location) (seq location))
|
|
231
|
+
(try
|
|
232
|
+
(let [next (core/resolve-location (java.net.URI/create at) location)]
|
|
233
|
+
(when (= (origin next) (origin (java.net.URI/create url)))
|
|
234
|
+
(str next)))
|
|
235
|
+
(catch Exception _ nil)))))
|
|
236
|
+
|
|
237
|
+
|
|
201
238
|
(defn- buy-once [fa]
|
|
202
239
|
(let [x (st fa :exchange)
|
|
203
240
|
refresh (st fa :refresh)]
|
|
@@ -228,11 +265,21 @@
|
|
|
228
265
|
;; a configured request-field name) carrying a quote, backslash or
|
|
229
266
|
;; newline must arrive as that literal value, not as broken JSON.
|
|
230
267
|
body (vs/jsonify (vs/jm (:request x) refresh) (vs/jm "indent" 0))
|
|
231
|
-
;; Never followed: a redirect would carry the refresh token on.
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
268
|
+
;; Never followed by the client: a redirect would carry the refresh token on.
|
|
269
|
+
fetchdef (vs/jm "method" (:method x)
|
|
270
|
+
"headers" (vs/jm "content-type" "application/json")
|
|
271
|
+
"body" body
|
|
272
|
+
"redirect" "manual")
|
|
273
|
+
;; By option alone, and only while the exchange stays on its own origin.
|
|
274
|
+
[res err] (loop [at url
|
|
275
|
+
answer (fetch url fetchdef)
|
|
276
|
+
hops 0]
|
|
277
|
+
(let [next (when (and (= "same-origin" (:redirect x)) (< hops REDIRECT-MAX)
|
|
278
|
+
(nil? (second answer)))
|
|
279
|
+
(moved url at (first answer)))]
|
|
280
|
+
(if next
|
|
281
|
+
(recur next (fetch next fetchdef) (inc hops))
|
|
282
|
+
answer)))]
|
|
236
283
|
|
|
237
284
|
(when (some? err)
|
|
238
285
|
(throw (sekreto/sekretoerror
|
|
@@ -284,7 +331,7 @@
|
|
|
284
331
|
;; boolean is the whole of what resolve! needs to tell a cacheable HIT from
|
|
285
332
|
;; a miss it must not keep.
|
|
286
333
|
(defn- resolve-once [fa]
|
|
287
|
-
(if-let [s (st fa :sek)]
|
|
334
|
+
(if-let [s (and (authon? fa) (st fa :sek))]
|
|
288
335
|
;; tryget: nil for a MISS, THROWS for a provider ERROR.
|
|
289
336
|
(let [found (sekreto/tryget s (st fa :secretname))]
|
|
290
337
|
(register! fa found)
|
|
@@ -313,16 +360,6 @@
|
|
|
313
360
|
(cond
|
|
314
361
|
(seq apikey) true
|
|
315
362
|
|
|
316
|
-
;; `auth: nil` is the documented way to send NO credential,
|
|
317
|
-
;; and a purchase is a credential-bearing call: the refresh
|
|
318
|
-
;; token goes to the token endpoint in the request body.
|
|
319
|
-
;; with-refresh honours suppression for the RETRY, but it runs
|
|
320
|
-
;; after this - by then the refresh token has already left the
|
|
321
|
-
;; process, and no later check can call it back. The
|
|
322
|
-
;; suppression has to be honoured here, before the first
|
|
323
|
-
;; purchase, or it only ever half-held.
|
|
324
|
-
(nil? (auth-of fa)) false
|
|
325
|
-
|
|
326
363
|
:else
|
|
327
364
|
;; No starting access token: buy one now. A failure here is a
|
|
328
365
|
;; failure of the operation - fail closed.
|
|
@@ -377,16 +414,18 @@
|
|
|
377
414
|
name (if (and (string? given) (not= "" given))
|
|
378
415
|
(.toLowerCase ^String given java.util.Locale/ROOT)
|
|
379
416
|
core/HEADER-AUTH)]
|
|
380
|
-
(if (
|
|
417
|
+
(if (not (authon? fa))
|
|
381
418
|
;; Suppressed auth means NO header, the same answer u-prepare-auth
|
|
382
|
-
;; gives.
|
|
383
|
-
;; suppressed - but this is the function that writes the
|
|
384
|
-
;; credential, so it is where the rule has to hold.
|
|
419
|
+
;; gives.
|
|
385
420
|
(vs/delprop headers name)
|
|
386
|
-
(let [
|
|
421
|
+
(let [value (if (and (st fa :basic)
|
|
422
|
+
(= true (vs/getpath (liveopts fa) "auth.basic")))
|
|
423
|
+
(pair! fa token)
|
|
424
|
+
token)
|
|
425
|
+
prefix (let [p (vs/getpath (liveopts fa) "auth.prefix")]
|
|
387
426
|
(if (string? p) p ""))]
|
|
388
427
|
(.put ^java.util.Map headers name
|
|
389
|
-
(if (= "" prefix)
|
|
428
|
+
(if (= "" prefix) value (str prefix " " value)))))))))
|
|
390
429
|
|
|
391
430
|
|
|
392
431
|
(defn- spent? [fa res]
|
|
@@ -400,11 +439,9 @@
|
|
|
400
439
|
;; Buy a token and try the request again when the API says the current one
|
|
401
440
|
;; is spent.
|
|
402
441
|
(defn- with-refresh [fa ctx url fetchdef inner]
|
|
403
|
-
;;
|
|
404
|
-
;;
|
|
405
|
-
|
|
406
|
-
;; credential the caller withheld.
|
|
407
|
-
(if (nil? (auth-of fa))
|
|
442
|
+
;; A refusal of an unauthenticated request is not an expired token, and
|
|
443
|
+
;; retrying would transmit the credential that auth off withholds.
|
|
444
|
+
(if (not (authon? fa))
|
|
408
445
|
(inner ctx url fetchdef)
|
|
409
446
|
(let [max (:retries (st fa :exchange))]
|
|
410
447
|
(loop [attempt 0]
|
|
@@ -468,7 +505,8 @@
|
|
|
468
505
|
(if (and (vs/islist s) (pos? (vs/size s)))
|
|
469
506
|
(mapv (fn [v] (core/to-int v)) (vec s))
|
|
470
507
|
[401]))
|
|
471
|
-
:retries (let [r (vs/getprop xopts "retries")] (if (number? r) (long r) 1))
|
|
508
|
+
:retries (let [r (vs/getprop xopts "retries")] (if (number? r) (long r) 1))
|
|
509
|
+
:redirect (if (= "same-origin" (vs/getprop xopts "redirect")) "same-origin" "refuse")})))
|
|
472
510
|
|
|
473
511
|
|
|
474
512
|
(defn- init! [fa ctx options]
|
|
@@ -479,6 +517,13 @@
|
|
|
479
517
|
:liveopts (core/oget ctx :options)
|
|
480
518
|
:active (= true (vs/getprop options "active")))
|
|
481
519
|
|
|
520
|
+
;; What u-prepare-auth was generated from: the config declares auth only
|
|
521
|
+
;; where it places a credential, and Basic only where it composes one.
|
|
522
|
+
(let [declared (vs/getpath (core/oget ctx :config) "options.auth")]
|
|
523
|
+
(swap! fa assoc
|
|
524
|
+
:declared (vs/ismap declared)
|
|
525
|
+
:basic (and (vs/ismap declared) (= true (vs/getprop declared "basic")))))
|
|
526
|
+
|
|
482
527
|
(when (st fa :active)
|
|
483
528
|
(let [secretname (sopt options "name" "apikey")
|
|
484
529
|
cache (not= false (vs/getprop options "cache"))
|
|
@@ -541,7 +586,7 @@
|
|
|
541
586
|
([] (secrets-feature nil))
|
|
542
587
|
([plugins]
|
|
543
588
|
(let [fa (atom {:name "secrets" :active false :version "0.1.0" :_options nil
|
|
544
|
-
:client nil :options (vs/jm) :liveopts nil
|
|
589
|
+
:client nil :options (vs/jm) :liveopts nil :declared false :basic false
|
|
545
590
|
:plugins (vec (or plugins []))
|
|
546
591
|
:secretname "apikey" :cache true :sek nil
|
|
547
592
|
:exchange nil :refresh "" :cred "" :initerr nil :resolved false
|
|
@@ -2019,6 +2019,13 @@
|
|
|
2019
2019
|
(boolean (or (contains? CREDENTIAL-HEADERS (.toLowerCase (str name) java.util.Locale/ROOT))
|
|
2020
2020
|
((if (fn? secret) secret u-clean-secret) ctx name value)))))
|
|
2021
2021
|
|
|
2022
|
+
;; Where a Location points from `from`, as RFC 3986 resolves it: URI.resolve
|
|
2023
|
+
;; drops the last path segment for a Location of a query alone.
|
|
2024
|
+
(defn resolve-location ^URI [^URI from ^String location]
|
|
2025
|
+
(if (str/starts-with? location "?")
|
|
2026
|
+
(URI/create (str (first (str/split (str from) #"[?#]" 2)) location))
|
|
2027
|
+
(.resolve from (URI/create location))))
|
|
2028
|
+
|
|
2022
2029
|
;; The request a followed redirect sends next, or nil where the response is the
|
|
2023
2030
|
;; answer. As the client rewrote it: a 303, and a 301 or 302 answering a POST,
|
|
2024
2031
|
;; become a GET without the body. A body is not sent on to another origin.
|
|
@@ -2027,7 +2034,7 @@
|
|
|
2027
2034
|
location (mget (vs/getprop res "headers") "location")
|
|
2028
2035
|
from (URI/create url)
|
|
2029
2036
|
^URI to (when (and (contains? REDIRECT-STATUS status) (string? location))
|
|
2030
|
-
(try (
|
|
2037
|
+
(try (resolve-location from location) (catch Exception _ nil)))
|
|
2031
2038
|
scheme (when to (.getScheme to))]
|
|
2032
2039
|
;; As the client's NORMAL policy: never from https to http.
|
|
2033
2040
|
(when (and scheme (or (.equalsIgnoreCase ^String scheme (.getScheme from))
|
|
@@ -481,6 +481,14 @@
|
|
|
481
481
|
cacheable? (fn [res] (and (vs/ismap res)
|
|
482
482
|
(not (true? (vs/getprop res "unreadable")))
|
|
483
483
|
(let [s (vs/getprop res "status")] (and (number? s) (>= s 200) (< s 300)))))
|
|
484
|
+
json? (fn [res]
|
|
485
|
+
(let [h (vs/getprop res "headers")
|
|
486
|
+
type (or (when (vs/ismap h)
|
|
487
|
+
(some (fn [item] (when (= "content-type" (str/lower-case (str (vs/getprop item 0))))
|
|
488
|
+
(str (vs/getprop item 1))))
|
|
489
|
+
(or (vs/items h) [])))
|
|
490
|
+
"")]
|
|
491
|
+
(or (= "" type) (core/json-media? type))))
|
|
484
492
|
snapshot (fn [res]
|
|
485
493
|
(let [jf (vs/getprop res "json")
|
|
486
494
|
data (when (fn? jf) (try (jf) (catch Throwable _ nil)))
|
|
@@ -509,14 +517,20 @@
|
|
|
509
517
|
(if (and (some? hit) (> (mget hit "expiry") now))
|
|
510
518
|
(do (track! "hit") [(replay (mget hit "snapshot")) nil])
|
|
511
519
|
(let [[res err] (inner ctx url fetchdef)]
|
|
512
|
-
(
|
|
520
|
+
(cond
|
|
521
|
+
;; A body that declares another type than JSON passes by, unread.
|
|
522
|
+
(and (nil? err) (cacheable? res) (not (json? res)))
|
|
523
|
+
(do (track! "bypass") [res err])
|
|
524
|
+
|
|
525
|
+
(and (nil? err) (cacheable? res))
|
|
513
526
|
(let [snap (snapshot res)
|
|
514
527
|
ttl (opt fa "ttl" 5000)]
|
|
515
528
|
(evict!)
|
|
516
529
|
(swap! fa update :store assoc key (vs/jm "expiry" (+ now ttl) "snapshot" snap))
|
|
517
530
|
(track! "miss")
|
|
518
531
|
[(replay snap) nil])
|
|
519
|
-
|
|
532
|
+
|
|
533
|
+
:else (do (track! "bypass") [res err]))))))))]
|
|
520
534
|
(swap! fa assoc
|
|
521
535
|
"init" (fn [ctx options]
|
|
522
536
|
(swap! fa assoc :client (core/oget ctx :client)
|
|
@@ -83,6 +83,9 @@
|
|
|
83
83
|
n (inc (count @calls))]
|
|
84
84
|
(swap! calls conj {:url url
|
|
85
85
|
:auth (when (some? auth) (str auth))
|
|
86
|
+
:values (if (vs/ismap headers)
|
|
87
|
+
(str/join "\n" (map str (vals headers)))
|
|
88
|
+
"")
|
|
86
89
|
:method (vs/getprop fetchdef "method")
|
|
87
90
|
:body (vs/getprop fetchdef "body")
|
|
88
91
|
:redirect (vs/getprop fetchdef "redirect")})
|
|
@@ -153,11 +156,12 @@
|
|
|
153
156
|
;; so core/u-fetcher really does call system.fetch.
|
|
154
157
|
;; ---------------------------------------------------------------------------
|
|
155
158
|
|
|
156
|
-
(defn- live-sdk [stub & {:keys [apikey auth-nil secrets]}]
|
|
159
|
+
(defn- live-sdk [stub & {:keys [apikey auth-nil secret secrets]}]
|
|
157
160
|
(let [opts (vs/jm "base" BASE
|
|
158
161
|
"allow" (vs/jm "op" ALLOWOP)
|
|
159
162
|
"system" (vs/jm "fetch" (:fetch stub)))]
|
|
160
163
|
(when apikey (.put ^java.util.Map opts "apikey" apikey))
|
|
164
|
+
(when secret (.put ^java.util.Map opts "secret" secret))
|
|
161
165
|
(when auth-nil (.put ^java.util.Map opts "auth" nil))
|
|
162
166
|
(when secrets (.put ^java.util.Map opts "feature" (vs/jm "secrets" secrets)))
|
|
163
167
|
(client/make-sdk opts)))
|
|
@@ -172,10 +176,6 @@
|
|
|
172
176
|
(deref (core/client-features sdk)))))
|
|
173
177
|
|
|
174
178
|
|
|
175
|
-
;; The credential header carries the SPEC's credential prefix, which a
|
|
176
|
-
;; TEMPLATE cannot know: an OpenAPI `http`/`bearer` scheme gives
|
|
177
|
-
;; `Bearer <token>`, an apiKey scheme the raw token. So assert on the
|
|
178
|
-
;; CREDENTIAL and let the prefix be whatever this SDK's API declares.
|
|
179
179
|
(defn- listener
|
|
180
180
|
"A server on 127.0.0.1 that records each request in `seen` and answers it
|
|
181
181
|
as `answer` says, [status location body]."
|
|
@@ -184,7 +184,9 @@
|
|
|
184
184
|
(.createContext server "/"
|
|
185
185
|
(reify com.sun.net.httpserver.HttpHandler
|
|
186
186
|
(handle [_ ex]
|
|
187
|
-
(let [
|
|
187
|
+
(let [uri (.getRequestURI ex)
|
|
188
|
+
;; The target with its query: a query alone is another request.
|
|
189
|
+
path (str (.getRawPath uri) (when-let [q (.getRawQuery uri)] (str "?" q)))
|
|
188
190
|
sent (String. (.readAllBytes (.getRequestBody ex)) "UTF-8")
|
|
189
191
|
[code to text] (answer path)
|
|
190
192
|
body (.getBytes ^String text "UTF-8")
|
|
@@ -198,11 +200,56 @@
|
|
|
198
200
|
(.start server)
|
|
199
201
|
server))
|
|
200
202
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
;; The header value u-prepare-auth writes for a token, in the form the
|
|
204
|
+
;; DEFAULT client's auth gives: under Basic the token is the key half of the
|
|
205
|
+
;; encoded pair, and a prefix is space-joined.
|
|
206
|
+
(defn- credential [token secret]
|
|
207
|
+
(let [auth (vs/getprop (core/client-options-map (client/test-sdk nil nil)) "auth")
|
|
208
|
+
prefix (let [p (vs/getprop auth "prefix")] (if (string? p) p ""))
|
|
209
|
+
value (if (= true (vs/getprop auth "basic"))
|
|
210
|
+
(.encodeToString (java.util.Base64/getEncoder)
|
|
211
|
+
(.getBytes (str token ":" secret) "UTF-8"))
|
|
212
|
+
token)]
|
|
213
|
+
(if (= "" prefix) value (str prefix " " value))))
|
|
214
|
+
|
|
215
|
+
(defn- credential-is
|
|
216
|
+
([got token msg] (credential-is got token msg ""))
|
|
217
|
+
([got token msg secret]
|
|
218
|
+
(t/is-eq (str (or got "")) (credential token secret)
|
|
219
|
+
(str msg ": the " CREDHEADER " header carries " token))))
|
|
220
|
+
|
|
221
|
+
(defn- with-origins
|
|
222
|
+
"Runs `run` with two listeners: the origin answers the exchange as the atom
|
|
223
|
+
`:answer` says and records the rest in `:seen`; `:elsewhere` is the other's."
|
|
224
|
+
[run]
|
|
225
|
+
(let [elsewhere (java.util.Collections/synchronizedList (java.util.ArrayList.))
|
|
226
|
+
seen (java.util.Collections/synchronizedList (java.util.ArrayList.))
|
|
227
|
+
answer (atom [307 ""])
|
|
228
|
+
other (listener elsewhere (fn [_] [200 nil "{\"access_token\":\"ELSEWHERE01\"}"]))
|
|
229
|
+
origin (listener seen (fn [path]
|
|
230
|
+
(if (= path "/api/auth/token")
|
|
231
|
+
(conj @answer "")
|
|
232
|
+
[200 nil "{\"access_token\":\"MOVED01\"}"])))
|
|
233
|
+
at (str "http://127.0.0.1:" (.getPort (.getAddress origin)))]
|
|
234
|
+
(try
|
|
235
|
+
(run {:elsewhere elsewhere :seen seen :answer answer :origin at :base (str at "/api")
|
|
236
|
+
:away (str "http://127.0.0.1:" (.getPort (.getAddress other)) "/api/auth/token")})
|
|
237
|
+
(finally
|
|
238
|
+
(.stop origin 0)
|
|
239
|
+
(.stop other 0)))))
|
|
240
|
+
|
|
241
|
+
(defn- redirect-direct [o status location xextra]
|
|
242
|
+
(.clear ^java.util.List (:elsewhere o))
|
|
243
|
+
(.clear ^java.util.List (:seen o))
|
|
244
|
+
(reset! (:answer o) [status location])
|
|
245
|
+
(let [exchange (vs/jm "active" true "refresh" "REFRESH01")]
|
|
246
|
+
(doseq [[k v] xextra] (vs/setprop exchange k v))
|
|
247
|
+
(client/direct (client/make-sdk
|
|
248
|
+
(vs/jm "base" (:base o)
|
|
249
|
+
"allow" (vs/jm "op" ALLOWOP)
|
|
250
|
+
"feature" (vs/jm "secrets" (sekopts "name" "refresh_token"
|
|
251
|
+
"exchange" exchange))))
|
|
252
|
+
(vs/jm "path" "/thing"))))
|
|
206
253
|
|
|
207
254
|
|
|
208
255
|
;; ---------------------------------------------------------------------------
|
|
@@ -288,6 +335,16 @@
|
|
|
288
335
|
(t/is-eq (sekreto/getfrom (secrets/sek f) "options" "apikey")
|
|
289
336
|
"OPTKEY01" "directed read of the options store")))))
|
|
290
337
|
|
|
338
|
+
(chk "secrets-a-resolved-key-is-placed-as-prepare-auth-places-it"
|
|
339
|
+
(fn []
|
|
340
|
+
(let [stub (counting-fetch)
|
|
341
|
+
sdk (live-sdk stub :secret "PASS01"
|
|
342
|
+
:secrets (sekopts "providers"
|
|
343
|
+
(vs/jt (fixed-provider "apikey" "CHAINKEY05"))))
|
|
344
|
+
res (client/direct sdk (vs/jm "path" "/thing"))]
|
|
345
|
+
(t/is-true (= true (vs/getprop res "ok")) (str "direct failed: " (brief res)))
|
|
346
|
+
(credential-is (:auth (call-at stub 0)) "CHAINKEY05" "the secret kept" "PASS01"))))
|
|
347
|
+
|
|
291
348
|
(chk "secrets-omitted-apikey-defers-to-the-chain"
|
|
292
349
|
(fn []
|
|
293
350
|
(let [stub (counting-fetch)
|
|
@@ -579,6 +636,24 @@
|
|
|
579
636
|
(credential-is (:auth (call-at stub 1)) "ACCESS01" "attempt 1")
|
|
580
637
|
(credential-is (:auth (call-at stub 3)) "ACCESS02" "the retry"))))
|
|
581
638
|
|
|
639
|
+
(chk "secrets-exchange-a-token-bought-on-a-refusal-is-placed-as-prepare-auth-places-it"
|
|
640
|
+
(fn []
|
|
641
|
+
(let [issued (atom 0)
|
|
642
|
+
apicalls (atom 0)
|
|
643
|
+
stub (counting-fetch
|
|
644
|
+
(fn [_n url _fd]
|
|
645
|
+
(if (str/ends-with? url tokenpath)
|
|
646
|
+
[(response 200 (vs/jm "access_token"
|
|
647
|
+
(str "ACCESS0" (swap! issued inc)))) nil]
|
|
648
|
+
(if (= 1 (swap! apicalls inc))
|
|
649
|
+
[(response 401 (vs/jm "err" "expired")) nil]
|
|
650
|
+
[(response 200 (vs/jm "ok" true)) nil]))))
|
|
651
|
+
sdk (live-sdk stub :secret "PASS01" :secrets (xsecrets))]
|
|
652
|
+
(client/direct sdk (vs/jm "path" "/thing"))
|
|
653
|
+
(t/is-eq (sent stub) 4 (str "token, 401, token, retry: " (wire stub)))
|
|
654
|
+
(credential-is (:auth (call-at stub 1)) "ACCESS01" "attempt 1, the secret kept" "PASS01")
|
|
655
|
+
(credential-is (:auth (call-at stub 3)) "ACCESS02" "the retry, the secret kept" "PASS01"))))
|
|
656
|
+
|
|
582
657
|
(chk "secrets-exchange-with-auth-nil-never-buys-or-retries"
|
|
583
658
|
(fn []
|
|
584
659
|
(let [stub (counting-fetch
|
|
@@ -604,51 +679,76 @@
|
|
|
604
679
|
;; Real listeners and core's own raw HTTP: a stub follows nothing.
|
|
605
680
|
(chk "secrets-exchange-a-redirect-is-a-failure-and-nothing-follows-it"
|
|
606
681
|
(fn []
|
|
607
|
-
(
|
|
608
|
-
|
|
609
|
-
answer (atom [307 ""])
|
|
610
|
-
other (listener elsewhere (fn [_] [200 nil "{\"access_token\":\"ELSEWHERE01\"}"]))
|
|
611
|
-
origin (listener seen (fn [path]
|
|
612
|
-
(if (= path "/api/auth/token")
|
|
613
|
-
(conj @answer "")
|
|
614
|
-
[200 nil "{\"access_token\":\"MOVED01\"}"])))
|
|
615
|
-
base (str "http://127.0.0.1:" (.getPort (.getAddress origin)) "/api")
|
|
616
|
-
away (str "http://127.0.0.1:" (.getPort (.getAddress other)) "/api/auth/token")]
|
|
617
|
-
(try
|
|
682
|
+
(with-origins
|
|
683
|
+
(fn [{:keys [elsewhere seen base away] :as o}]
|
|
618
684
|
(let [results
|
|
619
685
|
(doall
|
|
620
686
|
(for [[status location] [[307 away] [308 away] [302 away] [307 "/api/moved/token"]]]
|
|
621
|
-
(
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
"refresh" "REFRESH01")))))
|
|
632
|
-
res (client/direct sdk (vs/jm "path" "/thing"))
|
|
633
|
-
err (vs/getprop res "err")
|
|
634
|
-
message (if (some? err) (str (core/err-msg err)) (brief res))
|
|
635
|
-
followed (concat (vec elsewhere)
|
|
636
|
-
(remove #(= "/api/auth/token" (:path %)) (vec seen)))]
|
|
637
|
-
{:label (str status " to " location)
|
|
638
|
-
:refused (str/includes? message (str "token exchange failed: " status
|
|
639
|
-
" from " base "/auth/token"))
|
|
640
|
-
:message message
|
|
641
|
-
:followed (vec followed)}))))
|
|
687
|
+
(let [res (redirect-direct o status location nil)
|
|
688
|
+
err (vs/getprop res "err")
|
|
689
|
+
message (if (some? err) (str (core/err-msg err)) (brief res))
|
|
690
|
+
followed (concat (vec elsewhere)
|
|
691
|
+
(remove #(= "/api/auth/token" (:path %)) (vec seen)))]
|
|
692
|
+
{:label (str status " to " location)
|
|
693
|
+
:refused (str/includes? message (str "token exchange failed: " status
|
|
694
|
+
" from " base "/auth/token"))
|
|
695
|
+
:message message
|
|
696
|
+
:followed (vec followed)})))
|
|
642
697
|
refused (count (filter :refused results))
|
|
643
698
|
followed (reduce + (map (comp count :followed) results))]
|
|
644
699
|
(println (str "secrets: exchange refused " refused " redirect(s), " followed " followed"))
|
|
645
700
|
(doseq [r results]
|
|
646
701
|
(t/is-eq (:followed r) [] (str (:label r) ": followed"))
|
|
647
702
|
(t/is-true (:refused r) (str (:label r) ": not a failed exchange: " (:message r))))
|
|
648
|
-
(t/is-eq refused 4 "refused"))
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
703
|
+
(t/is-eq refused 4 "refused"))))))
|
|
704
|
+
|
|
705
|
+
(chk "secrets-exchange-with-the-option-a-redirect-on-its-own-origin-is-followed-and-no-other"
|
|
706
|
+
(fn []
|
|
707
|
+
(with-origins
|
|
708
|
+
(fn [{:keys [elsewhere seen base away origin] :as o}]
|
|
709
|
+
(let [moved (fn [path] {:method "POST" :path path
|
|
710
|
+
:body "{\"refresh_token\":\"REFRESH01\"}"})
|
|
711
|
+
;; Each case: the status, the Location, and the target the exchange
|
|
712
|
+
;; is asked again at, or nil where the redirect is refused.
|
|
713
|
+
results
|
|
714
|
+
(doall
|
|
715
|
+
(for [[status location path] [[307 "/api/moved/token" "/api/moved/token"]
|
|
716
|
+
[308 (str origin "/api/moved/token") "/api/moved/token"]
|
|
717
|
+
;; A Location of a query alone keeps the path that answered.
|
|
718
|
+
[307 "?step=2" "/api/auth/token?step=2"]
|
|
719
|
+
[307 away nil]
|
|
720
|
+
[302 "/api/moved/token" nil]
|
|
721
|
+
[307 "/api/auth/token" nil]]]
|
|
722
|
+
(let [res (redirect-direct o status location {"redirect" "same-origin"})
|
|
723
|
+
err (vs/getprop res "err")
|
|
724
|
+
message (if (some? err) (str (core/err-msg err)) (brief res))
|
|
725
|
+
mine (remove #(= "/api/auth/token" (:path %)) (vec seen))
|
|
726
|
+
verdict (cond
|
|
727
|
+
(and path (= true (vs/getprop res "ok"))
|
|
728
|
+
(= 2 (count mine)) (= (moved path) (first mine)))
|
|
729
|
+
:followed
|
|
730
|
+
(and (nil? path) (empty? mine)
|
|
731
|
+
(str/includes? message (str "token exchange failed: " status
|
|
732
|
+
" from " base "/auth/token")))
|
|
733
|
+
:refused
|
|
734
|
+
:else (str status " to " location ": " message " " (vec mine)))]
|
|
735
|
+
{:verdict verdict
|
|
736
|
+
:location location
|
|
737
|
+
:exchanged (count (filter #(= "/api/auth/token" (:path %)) (vec seen)))
|
|
738
|
+
:elsewhere (count elsewhere)})))
|
|
739
|
+
followed (count (filter #(= :followed (:verdict %)) results))
|
|
740
|
+
refused (count (filter #(= :refused (:verdict %)) results))
|
|
741
|
+
elsewhere (reduce + (map :elsewhere results))
|
|
742
|
+
looped (:exchanged (first (filter #(= "/api/auth/token" (:location %)) results)))]
|
|
743
|
+
(println (str "secrets: exchange opt-in followed " followed
|
|
744
|
+
" redirect(s) on its own origin, refused " refused ", "
|
|
745
|
+
elsewhere " elsewhere"))
|
|
746
|
+
(t/is-eq elsewhere 0 "the exchange reached another origin")
|
|
747
|
+
(t/is-eq (vec (remove keyword? (map :verdict results))) [] "wrong")
|
|
748
|
+
(t/is-eq followed 3 "followed")
|
|
749
|
+
(t/is-eq refused 3 "refused")
|
|
750
|
+
;; A redirect to itself stops at the hop limit.
|
|
751
|
+
(t/is-eq looped 6 "the loop's exchange count"))))))
|
|
652
752
|
|
|
653
753
|
(chk "secrets-exchange-with-no-refresh-token-fails-closed"
|
|
654
754
|
(fn []
|
|
@@ -664,10 +764,53 @@
|
|
|
664
764
|
"the missing-refresh-token message")))))))
|
|
665
765
|
|
|
666
766
|
|
|
767
|
+
;; Off by the config where it declares no auth, else by `auth: nil`: either
|
|
768
|
+
;; way the feature places no credential, buys no token and retries nothing.
|
|
769
|
+
(defn- auth-off [rec declared]
|
|
770
|
+
(t/run-check
|
|
771
|
+
rec "secrets-auth-off-places-no-credential-and-buys-no-token"
|
|
772
|
+
(fn []
|
|
773
|
+
(let [secrets ["OFFKEY01" "OFFKEY02" "OFFREFRESH01" "ACCESS01"]
|
|
774
|
+
carries (fn [c] (or (some? (:auth c))
|
|
775
|
+
(boolean (some (fn [s] (str/includes? (str (:values c)) s)) secrets))))
|
|
776
|
+
tokenpath "auth/token"
|
|
777
|
+
token? (fn [c] (str/ends-with? (str (:url c)) tokenpath))
|
|
778
|
+
scenarios [["a key the chain resolves" nil
|
|
779
|
+
(sekopts "providers" (vs/jt (fixed-provider "apikey" "OFFKEY01"))) 200]
|
|
780
|
+
["an explicit apikey" "OFFKEY02" (sekopts) 200]
|
|
781
|
+
["a refusal" nil
|
|
782
|
+
(sekopts "name" "refresh_token"
|
|
783
|
+
"providers" (vs/jt (fixed-provider "refresh_token" "OFFREFRESH01"))
|
|
784
|
+
"exchange" (vs/jm "active" true)) 401]]
|
|
785
|
+
totals (atom {:sent [] :bought 0})]
|
|
786
|
+
(doseq [[what apikey secrets-opts status] scenarios]
|
|
787
|
+
(let [stub (counting-fetch
|
|
788
|
+
(fn [_n url _fd]
|
|
789
|
+
(if (str/ends-with? url tokenpath)
|
|
790
|
+
[(response 200 (vs/jm "access_token" "ACCESS01")) nil]
|
|
791
|
+
[(response status (vs/jm "ok" (< status 400))) nil])))
|
|
792
|
+
sdk (live-sdk stub :apikey apikey :auth-nil declared :secrets secrets-opts)]
|
|
793
|
+
(client/direct sdk (vs/jm "path" "/thing"))
|
|
794
|
+
(let [calls (deref (:calls stub))
|
|
795
|
+
api (remove token? calls)
|
|
796
|
+
bought (count (filter token? calls))]
|
|
797
|
+
(t/is-eq (count api) 1 (str what ": one request, not retried"))
|
|
798
|
+
(t/is-eq bought 0 (str what ": no token bought"))
|
|
799
|
+
(t/is-true (not-any? carries api) (str what ": no credential sent: " (wire stub)))
|
|
800
|
+
(t/is-eq (secrets/credential (secrets/of sdk)) "" (str what ": nothing resolved"))
|
|
801
|
+
(swap! totals (fn [t] (-> t (update :sent into api) (update :bought + bought)))))))
|
|
802
|
+
(println (str "secrets: auth off by " (if declared "option" "config") ": "
|
|
803
|
+
(count (:sent @totals)) " request(s), "
|
|
804
|
+
(count (filter carries (:sent @totals))) " credential(s), "
|
|
805
|
+
(:bought @totals) " token(s)"))))))
|
|
806
|
+
|
|
807
|
+
|
|
667
808
|
;; Read from the config, never from a probe: an SDK whose config declares no
|
|
668
809
|
;; auth places no credential, and these tests assert on the one it places.
|
|
669
810
|
(defn run [rec]
|
|
670
811
|
(if (vs/ismap (vs/getprop (vs/getprop (config/make-config) "options") "auth"))
|
|
671
|
-
(run-checks rec)
|
|
812
|
+
(do (run-checks rec)
|
|
813
|
+
(auth-off rec true))
|
|
672
814
|
(do (println "SKIP secrets: this SDK's config declares no auth, so it places no credential")
|
|
815
|
+
(auth-off rec false)
|
|
673
816
|
:skipped)))
|
|
@@ -333,6 +333,22 @@
|
|
|
333
333
|
(h-op h :path "/w") (h-op h :path "/w")
|
|
334
334
|
(t/is-eq (count @calls) 2 "two calls")
|
|
335
335
|
(t/is-eq (mget (h-track h "_cache") "bypass") 2 "bypass"))))
|
|
336
|
+
(check "cache-passes-a-body-that-declares-another-type-than-json-by-unread" "cache"
|
|
337
|
+
(fn [] (let [sent (atom [])
|
|
338
|
+
[server calls] (recording-server
|
|
339
|
+
(fn [_n _fd]
|
|
340
|
+
(let [res (make-response 200 nil (vs/jm "content-type" "audio/wav"))]
|
|
341
|
+
(swap! sent conj res)
|
|
342
|
+
[res nil])))
|
|
343
|
+
h (make-harness [(fspec "cache")] :server server)
|
|
344
|
+
a (h-op h :path "/w/1/audio") b (h-op h :path "/w/1/audio")
|
|
345
|
+
;; The harness rebuilds the response; its json fn is carried as given.
|
|
346
|
+
handed (fn [r i] (identical? (:json @(core/oget (get r "ctx") :response))
|
|
347
|
+
(vs/getprop (nth @sent i nil) "json")))]
|
|
348
|
+
(t/is-eq (count @calls) 2 "the body was stored")
|
|
349
|
+
(t/is-true (handed a 0) "the cache handed on a copy it had read")
|
|
350
|
+
(t/is-true (handed b 1) "the second read was handed on as returned")
|
|
351
|
+
(t/is-eq (mget (h-track h "_cache") "bypass") 2 "bypass"))))
|
|
336
352
|
(check "cache-refetches-after-ttl" "cache"
|
|
337
353
|
(fn [] (let [c (make-clock)
|
|
338
354
|
[server calls] (recording-server nil)
|
|
@@ -743,8 +743,7 @@ public:
|
|
|
743
743
|
// materialises the whole result). This runtime is synchronous, so the
|
|
744
744
|
// returned vector is a materialised cursor the caller iterates. `callopts`
|
|
745
745
|
// parameterises the call: inbound yields the streaming feature's items when
|
|
746
|
-
// active, else the materialised items;
|
|
747
|
-
// to the request (reqdata `body$`); `ctrl` threads pipeline control.
|
|
746
|
+
// active, else the materialised items; `ctrl` threads pipeline control.
|
|
748
747
|
std::vector<Value> stream(const std::string& action,
|
|
749
748
|
const Value& args = Value::undef(),
|
|
750
749
|
const Value& callopts = Value::undef());
|
|
@@ -1431,15 +1430,6 @@ inline std::vector<Value> EntityBase::stream(const std::string& action,
|
|
|
1431
1430
|
cs.reqmatch = args.is_map() ? args : vmap();
|
|
1432
1431
|
CtxPtr ctx = u->makeContext(cs, entctx);
|
|
1433
1432
|
|
|
1434
|
-
// Outbound: attach a caller iterable `body` so the transport can stream a
|
|
1435
|
-
// request payload.
|
|
1436
|
-
Value body = getp(streamOpts, "body");
|
|
1437
|
-
if (!is_nullish(body)) {
|
|
1438
|
-
Value reqdata = ctx->reqdata.is_map() ? ctx->reqdata : vmap();
|
|
1439
|
-
map_put(reqdata, "body$", body);
|
|
1440
|
-
ctx->reqdata = reqdata;
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
1433
|
try {
|
|
1444
1434
|
u->featureHook(ctx, "PrePoint");
|
|
1445
1435
|
Value point = u->makePoint(ctx);
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
#include <vector>
|
|
17
17
|
|
|
18
18
|
#include "../core/types.hpp"
|
|
19
|
+
#include "../utility/pipeline.hpp"
|
|
19
20
|
#include "base.hpp"
|
|
20
21
|
#include "options.hpp"
|
|
21
22
|
|
|
@@ -110,7 +111,7 @@ private:
|
|
|
110
111
|
throw err;
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
if (storable(res)) {
|
|
114
|
+
if (storable(res) && json(res)) {
|
|
114
115
|
CacheSnapshot snap = snapshot(res);
|
|
115
116
|
int ttl = fopt::foptInt(options, "ttl", 5000);
|
|
116
117
|
evict();
|
|
@@ -134,6 +135,18 @@ private:
|
|
|
134
135
|
return status >= 200 && status < 300 && !(unreadable.is_bool() && unreadable.as_bool());
|
|
135
136
|
}
|
|
136
137
|
|
|
138
|
+
// A body that declares another type than JSON passes by, unread.
|
|
139
|
+
bool json(const Value& res) {
|
|
140
|
+
Value headers = getp(res, "headers");
|
|
141
|
+
if (!headers.is_map()) return true;
|
|
142
|
+
for (const auto& kv : *headers.as_map()) {
|
|
143
|
+
if (util::mediaLower(kv.first) != "content-type" || !kv.second.is_string()) continue;
|
|
144
|
+
return std::string::npos == kv.second.as_string().find_first_not_of(" \t") ||
|
|
145
|
+
util::isJsonMedia(kv.second);
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
137
150
|
CacheSnapshot snapshot(const Value& res) {
|
|
138
151
|
CacheSnapshot snap;
|
|
139
152
|
|