Skip to content

Commit

Permalink
fix #1689
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed Aug 28, 2023
1 parent f03ef7d commit 9bef728
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
48 changes: 26 additions & 22 deletions otoroshi/app/el/el.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,48 +50,52 @@ object GlobalExpressionLanguage {
context.getOrElse(s"params.$field", s"no-params-$field")

// legacy notation
case "date" => DateTime.now().toString()
case r"date.format\('$format@(.*)'\)" => DateTime.now().toString(format)
case r"date.epoch_ms" => DateTime.now().getMillis.toString
case r"date.epoch_sec" => TimeUnit.MILLISECONDS.toSeconds(DateTime.now().getMillis).toString

// specific date notation
case r"date($date@(.*)).format\('$format@(.*)'\)" => DateTime.parse(date).getMillis.toString
case r"date($date@(.*)).epoch_ms" => DateTime.parse(date).getMillis.toString
case r"date($date@(.*)).epoch_sec" =>
TimeUnit.MILLISECONDS.toSeconds(DateTime.parse(date).getMillis).toString
case r"date($date@(.*)).plus_ms($field@(.*))" => DateTime.parse(date).plusMillis(field.toInt).toString()
case r"date($date@(.*)).plus_ms($field@(.*)).format\('$format@(.*)'\)" =>
case r"date\($date@(.*)\).plus_ms\($field@(.*)\)" => DateTime.parse(date).plusMillis(field.toInt).toString()
case r"date\($date@(.*)\).plus_ms\($field@(.*)\).format\('$format@(.*)'\)" =>
DateTime.parse(date).plusMillis(field.toInt).toString(format)
case r"date($date@(.*)).plus_ms($field@(.*)).epoch_ms" =>
case r"date\($date@(.*)\).plus_ms\($field@(.*)\).epoch_ms" =>
DateTime.parse(date).plusMillis(field.toInt).getMillis.toString
case r"date($date@(.*)).plus_ms($field@(.*)).epoch_sec" =>
case r"date\($date@(.*)\).plus_ms\($field@(.*)\).epoch_sec" =>
TimeUnit.MILLISECONDS.toSeconds(DateTime.parse(date).plusMillis(field.toInt).getMillis).toString
case r"date($date@(.*)).minus_ms($field@(.*))" => DateTime.parse(date).minusMillis(field.toInt).toString()
case r"date($date@(.*)).minus_ms($field@(.*)).format\('$format@(.*)'\)" =>
case r"date\($date@(.*)\).minus_ms\($field@(.*)\)" => DateTime.parse(date).minusMillis(field.toInt).toString()
case r"date\($date@(.*)\).minus_ms\($field@(.*)\).format\('$format@(.*)'\)" =>
DateTime.parse(date).minusMillis(field.toInt).toString(format)
case r"date($date@(.*)).minus_ms($field@(.*)).epoch_ms" =>
case r"date\($date@(.*)\).minus_ms\($field@(.*)\).epoch_ms" =>
DateTime.parse(date).minusMillis(field.toInt).getMillis.toString
case r"date($date@(.*)).minus_ms($field@(.*)).epoch_sec" =>
case r"date\($date@(.*)\).minus_ms\($field@(.*)\).epoch_sec" =>
TimeUnit.MILLISECONDS.toSeconds(DateTime.parse(date).minusMillis(field.toInt).getMillis).toString
case r"date\($date@(.*)\).format\('$format@(.*)'\)" => DateTime.parse(date).getMillis.toString
case r"date\($date@(.*)\).epoch_ms" => DateTime.parse(date).getMillis.toString
case r"date\($date@(.*)\).epoch_sec" =>
TimeUnit.MILLISECONDS.toSeconds(DateTime.parse(date).getMillis).toString

case "date" => DateTime.now().toString()

// relative date notation
case "now" => DateTime.now().toString()
case r"now.format\('$format@(.*)'\)" => DateTime.now().toString(format)
case r"now.epoch_ms" => DateTime.now().getMillis.toString
case r"now.epoch_sec" => TimeUnit.MILLISECONDS.toSeconds(DateTime.now().getMillis).toString
case r"now.plus_ms($field@(.*))" => DateTime.now().plusMillis(field.toInt).toString()
case r"now.plus_ms($field@(.*)).format\('$format@(.*)'\)" =>

case r"now.plus_ms\($field@(.*)\).format\('$format@(.*)'\)" =>
DateTime.now().plusMillis(field.toInt).toString(format)
case r"now.plus_ms($field@(.*)).epoch_ms" => DateTime.now().plusMillis(field.toInt).getMillis.toString
case r"now.plus_ms($field@(.*)).epoch_sec" =>
case r"now.plus_ms\($field@(.*)\).epoch_ms" => DateTime.now().plusMillis(field.toInt).getMillis.toString
case r"now.plus_ms\($field@(.*)\).epoch_sec" =>
TimeUnit.MILLISECONDS.toSeconds(DateTime.now().plusMillis(field.toInt).getMillis).toString
case r"now.minus_ms($field@(.*))" => DateTime.now().minusMillis(field.toInt).toString()
case r"now.minus_ms($field@(.*)).format\('$format@(.*)'\)" =>
case r"now.plus_ms\($field@(.*)\)" => DateTime.now().plusMillis(field.toInt).toString()

case r"now.minus_ms\($field@(.*)\).format\('$format@(.*)'\)" =>
DateTime.now().minusMillis(field.toInt).toString(format)
case r"now.minus_ms($field@(.*)).epoch_ms" => DateTime.now().minusMillis(field.toInt).getMillis.toString
case r"now.minus_ms($field@(.*)).epoch_sec" =>
case r"now.minus_ms\($field@(.*)\).epoch_ms" => DateTime.now().minusMillis(field.toInt).getMillis.toString
case r"now.minus_ms\($field@(.*)\).epoch_sec" =>
TimeUnit.MILLISECONDS.toSeconds(DateTime.now().minusMillis(field.toInt).getMillis).toString
case r"now.minus_ms\($field@(.*)\)" => DateTime.now().minusMillis(field.toInt).toString()

case "now" => DateTime.now().toString()

case "service.domain" if service.isDefined => service.get._domain
case "service.subdomain" if service.isDefined => service.get.subdomain
Expand Down
12 changes: 12 additions & 0 deletions otoroshi/app/next/plugins/headers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class OtoroshiHeadersIn extends NgRequestTransformer {
.get
.toString("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
)
val context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty)
val newHeaders = ctx.otoroshiRequest.headers
.removeAllArgs(
env.Headers.OtoroshiProxiedHost,
Expand All @@ -179,6 +180,17 @@ class OtoroshiHeadersIn extends NgRequestTransformer {
env.Headers.OtoroshiGatewayParentRequest
)
.appendAll(additionalHeaders)
.mapValues(v => otoroshi.el.GlobalExpressionLanguage(
value = v,
req = ctx.request.some,
service = ctx.route.legacy.some,
route = ctx.route.some,
apiKey = ctx.apikey,
user = ctx.user,
context = context,
attrs = ctx.attrs,
env = env,
))
Right(ctx.otoroshiRequest.copy(headers = newHeaders))
}
}
Expand Down

0 comments on commit 9bef728

Please sign in to comment.