|
|
@@ -1,5 +1,5 @@
|
|
|
|
import std/[json, jsonutils, options, sequtils, strtabs, strutils]
|
|
|
|
import std/[json, jsonutils, options, sequtils, strtabs, strutils]
|
|
|
|
import mummy, webby
|
|
|
|
import mummy, webby, uuids
|
|
|
|
|
|
|
|
|
|
|
|
import std/httpcore except HttpHeaders
|
|
|
|
import std/httpcore except HttpHeaders
|
|
|
|
|
|
|
|
|
|
|
@@ -47,6 +47,7 @@ func `$`*(r: ApiResponse): string = $(%r)
|
|
|
|
proc makeCorsHeaders*(
|
|
|
|
proc makeCorsHeaders*(
|
|
|
|
allowedMethods: seq[string],
|
|
|
|
allowedMethods: seq[string],
|
|
|
|
allowedOrigins: seq[string],
|
|
|
|
allowedOrigins: seq[string],
|
|
|
|
|
|
|
|
allowedHeaders: Option[seq[string]],
|
|
|
|
reqOrigin = none[string]()): HttpHeaders =
|
|
|
|
reqOrigin = none[string]()): HttpHeaders =
|
|
|
|
|
|
|
|
|
|
|
|
result =
|
|
|
|
result =
|
|
|
@@ -55,7 +56,12 @@ proc makeCorsHeaders*(
|
|
|
|
"Access-Control-Allow-Origin": reqOrigin.get,
|
|
|
|
"Access-Control-Allow-Origin": reqOrigin.get,
|
|
|
|
"Access-Control-Allow-Credentials": "true",
|
|
|
|
"Access-Control-Allow-Credentials": "true",
|
|
|
|
"Access-Control-Allow-Methods": allowedMethods.join(","),
|
|
|
|
"Access-Control-Allow-Methods": allowedMethods.join(","),
|
|
|
|
"Access-Control-Allow-Headers": "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-TOKEN"
|
|
|
|
"Access-Control-Allow-Headers":
|
|
|
|
|
|
|
|
if allowedHeaders.isSome: allowedHeaders.get.join(",")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
"DNT,User-Agent,X-Requested-With,If-Modified-Since," &
|
|
|
|
|
|
|
|
"Cache-Control,Content-Type,Range,Authorization,X-CSRF-TOKEN," &
|
|
|
|
|
|
|
|
"traceparent,tracestate,X-Request-ID,X-Correlation-ID",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if reqOrigin.isSome:
|
|
|
|
if reqOrigin.isSome:
|
|
|
@@ -67,8 +73,16 @@ proc makeCorsHeaders*(
|
|
|
|
proc makeCorsHeaders*(
|
|
|
|
proc makeCorsHeaders*(
|
|
|
|
allowedMethods: seq[HttpMethod],
|
|
|
|
allowedMethods: seq[HttpMethod],
|
|
|
|
allowedOrigins: seq[string],
|
|
|
|
allowedOrigins: seq[string],
|
|
|
|
|
|
|
|
allowedHeaders: Option[seq[string]],
|
|
|
|
reqOrigin = none[string]()): HttpHeaders =
|
|
|
|
reqOrigin = none[string]()): HttpHeaders =
|
|
|
|
makeCorsHeaders(allowedMethods.mapIt($it), allowedOrigins, reqOrigin )
|
|
|
|
makeCorsHeaders(allowedMethods.mapIt($it), allowedOrigins, allowedHeaders, reqOrigin )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc makeCorsHeaders*[T: HttpMethod or string](
|
|
|
|
|
|
|
|
allowedMethods: seq[T],
|
|
|
|
|
|
|
|
allowedOrigins: seq[string],
|
|
|
|
|
|
|
|
reqOrigin = none[string]()): HttpHeaders =
|
|
|
|
|
|
|
|
makeCorsHeaders(allowedMethods, allowedOrigins, none[seq[string]](), reqOrigin )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func origin*(req: Request): Option[string] =
|
|
|
|
func origin*(req: Request): Option[string] =
|
|
|
@@ -76,6 +90,40 @@ func origin*(req: Request): Option[string] =
|
|
|
|
else: none[string]()
|
|
|
|
else: none[string]()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func traceparent*(req: Request): Option[string] =
|
|
|
|
|
|
|
|
## Extract the traceparent from the request headers, if present.
|
|
|
|
|
|
|
|
if req.headers.contains("traceparent"):
|
|
|
|
|
|
|
|
return some(req.headers["traceparent"])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return none[string]()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc makeTraceContextHeaders*(req: Request, traceParentId: string): HttpHeaders =
|
|
|
|
|
|
|
|
var headers = HttpHeaders(@[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if req.headers.contains("traceparent"):
|
|
|
|
|
|
|
|
# If the traceparent header is present, we should update it with our
|
|
|
|
|
|
|
|
# parent-id.
|
|
|
|
|
|
|
|
let traceparentParts = req.headers["traceparent"].split("-")
|
|
|
|
|
|
|
|
if traceparentParts.len != 4:
|
|
|
|
|
|
|
|
headers["traceparent"] = "00-$#-$#-00" % [
|
|
|
|
|
|
|
|
replace($genUUID(), "-", ""), # trace-id
|
|
|
|
|
|
|
|
traceParentId, # parent-id
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
headers["traceparent"] = "00-$#-$#-$#" % [
|
|
|
|
|
|
|
|
traceparentParts[1], # trace-id
|
|
|
|
|
|
|
|
traceParentId, # parent-id
|
|
|
|
|
|
|
|
traceparentParts[3], # flags
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if req.headers.contains("tracestate"):
|
|
|
|
|
|
|
|
headers["tracestate"] = req.headers["tracestate"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return headers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc respondWithRawJson*(
|
|
|
|
proc respondWithRawJson*(
|
|
|
|
req: Request,
|
|
|
|
req: Request,
|
|
|
|
body: JsonNode,
|
|
|
|
body: JsonNode,
|
|
|
|