Skip to content

Commit

Permalink
Merge pull request #10321 from jrudolph/10107-proper-initialization
Browse files Browse the repository at this point in the history
AkkaHttpServer: prevent access to not yet initialized fields
  • Loading branch information
mergify[bot] committed Jun 4, 2020
2 parents b5e779a + 3950cce commit fd66e98
Showing 1 changed file with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,45 @@ class AkkaHttpServer(context: AkkaHttpServer.Context) extends Server {
.withParserSettings(parserSettings)
}

// Each request needs an id
private val requestIDs = new java.util.concurrent.atomic.AtomicLong(0)

/**
* Values that are cached based on the current application.
*/
private case class ReloadCacheValues(
resultUtils: ServerResultUtils,
modelConversion: AkkaModelConversion,
serverDebugInfo: Option[ServerDebugInfo]
)

/**
* A helper to cache values that are derived from the current application.
*/
private val reloadCache = new ReloadCache[ReloadCacheValues] {
protected override def reloadValue(tryApp: Try[Application]): ReloadCacheValues = {
val serverResultUtils = reloadServerResultUtils(tryApp)
val forwardedHeaderHandler = reloadForwardedHeaderHandler(tryApp)
val illegalResponseHeaderValue = ParserSettings.IllegalResponseHeaderValueProcessingMode(
illegalResponseHeaderValueProcessingMode
)
val modelConversion =
new AkkaModelConversion(serverResultUtils, forwardedHeaderHandler, illegalResponseHeaderValue)
ReloadCacheValues(
resultUtils = serverResultUtils,
modelConversion = modelConversion,
serverDebugInfo = reloadDebugInfo(tryApp, provider)
)
}
}

// ----------------------------------------------------------------------
// CAUTION
// NO fields (val) below this point that are accessed in handleRequest.
// They might not yet be initialized when handleRequest is run for the
// first request. In doubt use `lazy val`.
// ----------------------------------------------------------------------

/**
* Bind Akka HTTP to a port to listen for incoming connections. Calls [[createServerSettings()]] to configure the
* binding and [[handleRequest()]] as a handler for the binding.
Expand Down Expand Up @@ -252,38 +291,6 @@ class AkkaHttpServer(context: AkkaHttpServer.Context) extends Server {
}
}

// Each request needs an id
private val requestIDs = new java.util.concurrent.atomic.AtomicLong(0)

/**
* Values that are cached based on the current application.
*/
private case class ReloadCacheValues(
resultUtils: ServerResultUtils,
modelConversion: AkkaModelConversion,
serverDebugInfo: Option[ServerDebugInfo]
)

/**
* A helper to cache values that are derived from the current application.
*/
private val reloadCache = new ReloadCache[ReloadCacheValues] {
protected override def reloadValue(tryApp: Try[Application]): ReloadCacheValues = {
val serverResultUtils = reloadServerResultUtils(tryApp)
val forwardedHeaderHandler = reloadForwardedHeaderHandler(tryApp)
val illegalResponseHeaderValue = ParserSettings.IllegalResponseHeaderValueProcessingMode(
illegalResponseHeaderValueProcessingMode
)
val modelConversion =
new AkkaModelConversion(serverResultUtils, forwardedHeaderHandler, illegalResponseHeaderValue)
ReloadCacheValues(
resultUtils = serverResultUtils,
modelConversion = modelConversion,
serverDebugInfo = reloadDebugInfo(tryApp, provider)
)
}
}

private def resultUtils(tryApp: Try[Application]): ServerResultUtils =
reloadCache.cachedFrom(tryApp).resultUtils
private def modelConversion(tryApp: Try[Application]): AkkaModelConversion =
Expand Down

0 comments on commit fd66e98

Please sign in to comment.