Skip to content

Commit

Permalink
code optimisation and clean up in ember server
Browse files Browse the repository at this point in the history
  • Loading branch information
IshikaDawda committed Nov 8, 2024
1 parent 28ca5c8 commit 2e85a53
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 163 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ object RequestProcessor {
val result = construct((): Unit)
.redeemWith(_ => httpApp(request),
_ => for {
_ <- preprocessHttpRequest(request)
isLockAcquired <- preprocessHttpRequest(request)
resp <- httpApp(request)
_ <- postProcessSecurityHook(resp)
_ <- postProcessSecurityHook(isLockAcquired, resp)
} yield resp
)
result
}

private def preprocessHttpRequest[F[_]: Sync](request: Request[F]): F[Unit] = construct {
val isLockAcquired = EmberUtils.acquireLockIfPossible()
private def preprocessHttpRequest[F[_]: Sync](request: Request[F]): F[Boolean] = construct {
val isLockAcquired = GenericHelper.acquireLockIfPossible("HTTP4S-EMBER-REQUEST_LOCK", request.hashCode())
try {
if (NewRelicSecurity.isHookProcessingActive && isLockAcquired && !NewRelicSecurity.getAgent.getSecurityMetaData.getRequest.isRequestParsed){

Expand All @@ -50,7 +50,12 @@ object RequestProcessor {
securityRequest.setMethod(request.method.name)
securityRequest.setServerPort((request.serverPort).get.asInstanceOf[Port].value)
securityRequest.setClientIP(request.remoteAddr.get.toString)
securityRequest.setProtocol(EmberUtils.getProtocol(request.isSecure.get))
if(request.isSecure.get){
securityRequest.setProtocol("https")
} else {
securityRequest.setProtocol("http")
}

securityRequest.setUrl(request.uri.toString)

if (securityRequest.getClientIP != null && securityRequest.getClientIP.trim.nonEmpty) {
Expand All @@ -59,8 +64,8 @@ object RequestProcessor {
}

processRequestHeaders(request.headers, securityRequest)
securityMetaData.setTracingHeaderValue(EmberUtils.getTraceHeader(securityRequest.getHeaders))
securityRequest.setContentType(EmberUtils.getContentType(securityRequest.getHeaders))
securityMetaData.setTracingHeaderValue(getTraceHeader(securityRequest.getHeaders))
securityRequest.setContentType(getContentType(securityRequest.getHeaders))

// TODO extract request body & user class detection

Expand All @@ -71,20 +76,17 @@ object RequestProcessor {

} catch {
case e: Throwable => NewRelicSecurity.getAgent.log(LogLevel.WARNING, String.format(GenericHelper.ERROR_GENERATING_HTTP_REQUEST, HTTP_4S_EMBER_SERVER_2_12_0_23, e.getMessage), e, this.getClass.getName)
} finally {
if (isLockAcquired) {
EmberUtils.releaseLock()
}
}
isLockAcquired
}

private def postProcessSecurityHook[F[_]: Sync](response: Response[F]): F[Unit] = construct {
private def postProcessSecurityHook[F[_]: Sync](isLockAcquired: Boolean, response: Response[F]): F[Unit] = construct {
try {
if (NewRelicSecurity.isHookProcessingActive) {
if (isLockAcquired && NewRelicSecurity.isHookProcessingActive) {
val securityResponse = NewRelicSecurity.getAgent.getSecurityMetaData.getResponse
securityResponse.setResponseCode(response.status.code)
processResponseHeaders(response.headers, securityResponse)
securityResponse.setResponseContentType(EmberUtils.getContentType(securityResponse.getHeaders))
securityResponse.setResponseContentType(getContentType(securityResponse.getHeaders))

// TODO extract response body

Expand Down Expand Up @@ -154,5 +156,20 @@ object RequestProcessor {
})
}

private def getContentType(headers: util.Map[String, String]): String = {
var contentType = StringUtils.EMPTY
if (headers.containsKey("content-type")) contentType = headers.get("content-type")
contentType
}

private def getTraceHeader(headers: util.Map[String, String]): String = {
var data = StringUtils.EMPTY
if (headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER) || headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase)) {
data = headers.get(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER)
if (data == null || data.trim.isEmpty) data = headers.get(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase)
}
data
}

private def construct[F[_] : Sync, T](t: => T): F[T] = Sync[F].delay(t)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
implementation("org.scala-lang:scala-library:2.13.3")
implementation('org.http4s:http4s-ember-server_2.13:0.23.12')
implementation("org.typelevel:cats-effect_2.13:3.3.12"){transitive = false}
testImplementation("org.http4s:http4s-dsl_2.12:0.23.12")
testImplementation("org.http4s:http4s-dsl_2.13:0.23.12")
}

jar {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ object RequestProcessor {
val result = construct((): Unit)
.redeemWith(_ => httpApp(request),
_ => for {
_ <- preprocessHttpRequest(request)
isLockAcquired <- preprocessHttpRequest(request)
resp <- httpApp(request)
_ <- postProcessSecurityHook(resp)
_ <- postProcessSecurityHook(isLockAcquired, resp)
} yield resp
)
result
}

private def preprocessHttpRequest[F[_]: Sync](request: Request[F]): F[Unit] = construct {
val isLockAcquired = EmberUtils.acquireLockIfPossible()
private def preprocessHttpRequest[F[_]: Sync](request: Request[F]): F[Boolean] = construct {
val isLockAcquired = GenericHelper.acquireLockIfPossible("HTTP4S-EMBER-REQUEST_LOCK", request.hashCode())
try {
if (NewRelicSecurity.isHookProcessingActive && isLockAcquired && !NewRelicSecurity.getAgent.getSecurityMetaData.getRequest.isRequestParsed){

Expand All @@ -50,7 +50,11 @@ object RequestProcessor {
securityRequest.setMethod(request.method.name)
securityRequest.setServerPort((request.serverPort).get.asInstanceOf[Port].value)
securityRequest.setClientIP(request.remoteAddr.get.toString)
securityRequest.setProtocol(EmberUtils.getProtocol(request.isSecure.get))
if(request.isSecure.get){
securityRequest.setProtocol("https")
} else {
securityRequest.setProtocol("http")
}
securityRequest.setUrl(request.uri.toString)

if (securityRequest.getClientIP != null && securityRequest.getClientIP.trim.nonEmpty) {
Expand All @@ -59,8 +63,8 @@ object RequestProcessor {
}

processRequestHeaders(request.headers, securityRequest)
securityMetaData.setTracingHeaderValue(EmberUtils.getTraceHeader(securityRequest.getHeaders))
securityRequest.setContentType(EmberUtils.getContentType(securityRequest.getHeaders))
securityMetaData.setTracingHeaderValue(getTraceHeader(securityRequest.getHeaders))
securityRequest.setContentType(getContentType(securityRequest.getHeaders))

// TODO extract request body & user class detection

Expand All @@ -71,20 +75,17 @@ object RequestProcessor {

} catch {
case e: Throwable => NewRelicSecurity.getAgent.log(LogLevel.WARNING, String.format(GenericHelper.ERROR_GENERATING_HTTP_REQUEST, HTTP_4S_EMBER_SERVER_2_13_0_23, e.getMessage), e, this.getClass.getName)
} finally {
if (isLockAcquired) {
EmberUtils.releaseLock()
}
}
isLockAcquired
}

private def postProcessSecurityHook[F[_]: Sync](response: Response[F]): F[Unit] = construct {
private def postProcessSecurityHook[F[_]: Sync](isLockAcquired: Boolean, response: Response[F]): F[Unit] = construct {
try {
if (NewRelicSecurity.isHookProcessingActive) {
if (isLockAcquired && NewRelicSecurity.isHookProcessingActive) {
val securityResponse = NewRelicSecurity.getAgent.getSecurityMetaData.getResponse
securityResponse.setResponseCode(response.status.code)
processResponseHeaders(response.headers, securityResponse)
securityResponse.setResponseContentType(EmberUtils.getContentType(securityResponse.getHeaders))
securityResponse.setResponseContentType(getContentType(securityResponse.getHeaders))

// TODO extract response body

Expand Down Expand Up @@ -154,5 +155,20 @@ object RequestProcessor {
})
}

private def getContentType(headers: util.Map[String, String]): String = {
var contentType = StringUtils.EMPTY
if (headers.containsKey("content-type")) contentType = headers.get("content-type")
contentType
}

private def getTraceHeader(headers: util.Map[String, String]): String = {
var data = StringUtils.EMPTY
if (headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER) || headers.containsKey(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase)) {
data = headers.get(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER)
if (data == null || data.trim.isEmpty) data = headers.get(ServletHelper.CSEC_DISTRIBUTED_TRACING_HEADER.toLowerCase)
}
data
}

private def construct[F[_] : Sync, T](t: => T): F[T] = Sync[F].delay(t)
}

0 comments on commit 2e85a53

Please sign in to comment.