Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Feb 14, 2025
1 parent 2c72930 commit 4f0bc89
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
33 changes: 6 additions & 27 deletions otoroshi/app/next/plugins/cors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ case class NgCorsSettings(
maxAge = maxAge,
allowCredentials = allowCredentials
)

def asHeaders: Seq[(String, String)] = {
var headers = Map(
"Access-Control-Allow-Origin" -> allowOrigin,
"Access-Control-Allow-Credentials" -> allowCredentials.toString
)
if (exposeHeaders.nonEmpty) {
headers = headers + ("Access-Control-Expose-Headers" -> exposeHeaders.mkString(", "))
}
if (allowHeaders.nonEmpty) {
headers = headers + ("Access-Control-Allow-Headers" -> allowHeaders.mkString(", "))
}
if (allowMethods.nonEmpty) {
headers = headers + ("Access-Control-Allow-Methods" -> allowMethods.mkString(", "))
}
maxAge.foreach { age =>
headers = headers + ("Access-Control-Max-Age" -> age.toSeconds.toString)
}
headers.toSeq
}
}

object NgCorsSettings {
Expand Down Expand Up @@ -157,14 +137,11 @@ class Cors extends NgRequestTransformer with NgPreRouting {
ctx: NgTransformerResponseContext
)(implicit env: Env, ec: ExecutionContext, mat: Materializer): Either[Result, NgPluginHttpResponse] = {
val req = ctx.request
// val cors =
// CorsSettings.fromJson(ctx.config).getOrElse(CorsSettings()).copy(enabled = true, excludedPatterns = Seq.empty)

val cors = ctx.cachedConfig(internalName)(configReads).getOrElse(NgCorsSettings())

println(cors)
val corsHeaders = cors
.asHeaders
.legacy
.asHeaders(req)
.filter(t => t._1.trim.nonEmpty && t._2.trim.nonEmpty)
.map(v =>
(
Expand All @@ -190,9 +167,11 @@ class Cors extends NgRequestTransformer with NgPreRouting {
ctx: NgTransformerErrorContext
)(implicit env: Env, ec: ExecutionContext, mat: Materializer): Future[NgPluginHttpResponse] = {
val req = ctx.request
val cors =
CorsSettings.fromJson(ctx.config).getOrElse(CorsSettings()).copy(enabled = true, excludedPatterns = Seq.empty)

val cors = ctx.cachedConfig(internalName)(configReads).getOrElse(NgCorsSettings())

val corsHeaders = cors
.legacy
.asHeaders(req)
.filter(t => t._1.trim.nonEmpty && t._2.trim.nonEmpty)
.map(v =>
Expand Down
1 change: 1 addition & 0 deletions otoroshi/app/next/plugins/headers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class OverrideHost extends NgRequestTransformer {
env
)
val headers = ctx.otoroshiRequest.headers.-("Host").-("host").+("Host" -> host)

val request = ctx.otoroshiRequest.copy(headers = headers)
Right(request)
}
Expand Down

0 comments on commit 4f0bc89

Please sign in to comment.