Skip to content

Commit

Permalink
[release 16.3.2] Format code before release
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed Apr 26, 2023
1 parent 622cf96 commit e65b9dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
43 changes: 23 additions & 20 deletions otoroshi/app/next/plugins/wasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ object BodyHelper {
def extractBodyFrom(doc: JsValue): ByteString = extractBodyFromOpt(doc).getOrElse(ByteString.empty)
def extractBodyFromOpt(doc: JsValue): Option[ByteString] = {
val bodyAsBytes = doc.select("body_bytes").asOpt[Array[Byte]].map(bytes => ByteString(bytes))
val bodyBase64 = doc.select("body_base64").asOpt[String].map(str => ByteString(str).decodeBase64)
val bodyJson = doc
val bodyBase64 = doc.select("body_base64").asOpt[String].map(str => ByteString(str).decodeBase64)
val bodyJson = doc
.select("body_json")
.asOpt[JsValue]
.filter {
case JsNull => false
case _ => true
case _ => true
}
.map(str => ByteString(str.stringify))
val bodyStr = doc
val bodyStr = doc
.select("body_str")
.asOpt[String]
.orElse(doc.select("body").asOpt[String])
Expand Down Expand Up @@ -111,7 +111,7 @@ class WasmPreRoute extends NgPreRouting {
case Success(response) => {
val error = response.select("error").asOpt[Boolean].getOrElse(false)
if (error) {
val body = BodyHelper.extractBodyFrom(response)
val body = BodyHelper.extractBodyFrom(response)
val headers: Map[String, String] = response
.select("headers")
.asOpt[Map[String, String]]
Expand Down Expand Up @@ -165,15 +165,15 @@ class WasmBackend extends NgBackendCall {
.flatMap(input => WasmUtils.execute(config, "call_backend", input, ctx.some, ctx.attrs.some))
.map {
case Right(output) =>
val response =
val response =
try {
Json.parse(output)
} catch {
case e: Exception =>
logger.error("error during json parsing", e)
Json.obj()
}
val body = BodyHelper.extractBodyFrom(response)
val body = BodyHelper.extractBodyFrom(response)
bodyResponse(
status = response.select("status").asOpt[Int].getOrElse(200),
headers = response
Expand Down Expand Up @@ -279,13 +279,14 @@ class WasmRequestTransformer extends NgRequestTransformer {
case Right(res) =>
val response = Json.parse(res)
if (response.select("error").asOpt[Boolean].getOrElse(false)) {
val status = response.select("status").asOpt[Int].getOrElse(500)
val headers = (response \ "headers").asOpt[Map[String, String]].getOrElse(Map.empty)
val cookies = WasmUtils.convertJsonPlayCookies(response).getOrElse(Seq.empty)
val status = response.select("status").asOpt[Int].getOrElse(500)
val headers = (response \ "headers").asOpt[Map[String, String]].getOrElse(Map.empty)
val cookies = WasmUtils.convertJsonPlayCookies(response).getOrElse(Seq.empty)
val contentType = headers.getIgnoreCase("Content-Type").getOrElse("application/octet-stream")
val body = BodyHelper.extractBodyFrom(response)
val body = BodyHelper.extractBodyFrom(response)
Left(
Results.Status(status)(body)
Results
.Status(status)(body)
.withCookies(cookies: _*)
.withHeaders(headers.toSeq: _*)
.as(contentType)
Expand All @@ -299,7 +300,7 @@ class WasmRequestTransformer extends NgRequestTransformer {
url = (response \ "url").asOpt[String].getOrElse(ctx.otoroshiRequest.url),
headers = (response \ "headers").asOpt[Map[String, String]].getOrElse(ctx.otoroshiRequest.headers),
cookies = WasmUtils.convertJsonCookies(response).getOrElse(ctx.otoroshiRequest.cookies),
body = body.map(_.chunks(16 * 1024)).getOrElse(ctx.otoroshiRequest.body),
body = body.map(_.chunks(16 * 1024)).getOrElse(ctx.otoroshiRequest.body)
)
)
}
Expand Down Expand Up @@ -342,13 +343,14 @@ class WasmResponseTransformer extends NgRequestTransformer {
case Right(res) =>
val response = Json.parse(res)
if (response.select("error").asOpt[Boolean].getOrElse(false)) {
val status = response.select("status").asOpt[Int].getOrElse(500)
val headers = (response \ "headers").asOpt[Map[String, String]].getOrElse(Map.empty)
val cookies = WasmUtils.convertJsonPlayCookies(response).getOrElse(Seq.empty)
val status = response.select("status").asOpt[Int].getOrElse(500)
val headers = (response \ "headers").asOpt[Map[String, String]].getOrElse(Map.empty)
val cookies = WasmUtils.convertJsonPlayCookies(response).getOrElse(Seq.empty)
val contentType = headers.getIgnoreCase("Content-Type").getOrElse("application/octet-stream")
val body = BodyHelper.extractBodyFrom(response)
val body = BodyHelper.extractBodyFrom(response)
Left(
Results.Status(status)(body)
Results
.Status(status)(body)
.withCookies(cookies: _*)
.withHeaders(headers.toSeq: _*)
.as(contentType)
Expand All @@ -360,8 +362,9 @@ class WasmResponseTransformer extends NgRequestTransformer {
headers = (response \ "headers").asOpt[Map[String, String]].getOrElse(ctx.otoroshiResponse.headers),
status = (response \ "status").asOpt[Int].getOrElse(200),
cookies = WasmUtils.convertJsonCookies(response).getOrElse(ctx.otoroshiResponse.cookies),
body = body.map(_.chunks(16 * 1024)).getOrElse(ctx.otoroshiResponse.body),
).right
body = body.map(_.chunks(16 * 1024)).getOrElse(ctx.otoroshiResponse.body)
)
.right
}
case Left(value) => Left(Results.BadRequest(value))
}
Expand Down
2 changes: 1 addition & 1 deletion otoroshi/app/wasm/wasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ object WasmUtils {

attrsOpt match {
case None => {
val slot = createPlugin()
val slot = createPlugin()
val output = if (config.opa) {
OPA.evaluate(slot.plugin, input.stringify)
} else {
Expand Down

0 comments on commit e65b9dd

Please sign in to comment.