Skip to content

Commit

Permalink
Extract request and response body by using the charset
Browse files Browse the repository at this point in the history
  • Loading branch information
IshikaDawda committed Nov 19, 2024
1 parent 12d1c7c commit b9cfda2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityExcepti
import com.newrelic.api.agent.security.schema.operation.RXSSOperation
import com.newrelic.api.agent.security.schema.policy.AgentPolicy
import com.newrelic.api.agent.security.utils.logging.LogLevel
import fs2.text.decodeWithCharset
import org.http4s.Charset.`UTF-8`
import org.http4s.{Headers, Message, Request, Response}

import java.util
Expand Down Expand Up @@ -109,7 +111,12 @@ object RequestProcessor {
}

private def extractBody[F[_]: Sync](msg: Message[F]): F[String] = {
msg.bodyText.compile.string
if (msg.contentType.nonEmpty && msg.contentType.get.charset.nonEmpty) {
val charset = msg.contentType.get.charset.get;
msg.body.through(decodeWithCharset(charset.nioCharset)).compile.string
} else {
msg.bodyText.compile.string
}
}

private def processRequestHeaders(headers: Headers, securityRequest: HttpRequest): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityExcepti
import com.newrelic.api.agent.security.schema.operation.RXSSOperation
import com.newrelic.api.agent.security.schema.policy.AgentPolicy
import com.newrelic.api.agent.security.utils.logging.LogLevel
import fs2.text.decodeWithCharset
import org.http4s.{Headers, Message, Request, Response}

import java.util
Expand Down Expand Up @@ -82,7 +83,12 @@ object RequestProcessor {
}

private def extractBody[F[_]: Sync](msg: Message[F]): F[String] = {
msg.bodyText.compile.string
if (msg.contentType.nonEmpty && msg.contentType.get.charset.nonEmpty) {
val charset = msg.contentType.get.charset.get;
msg.body.through(decodeWithCharset(charset.nioCharset)).compile.string
} else {
msg.bodyText.compile.string
}
}

private def postProcessSecurityHook[F[_]: Sync](isLockAcquired: Boolean, response: Response[F], body: String): F[Unit] = construct {
Expand Down

0 comments on commit b9cfda2

Please sign in to comment.