Skip to content

Commit

Permalink
Merge pull request #1031 from finagle/vk/unsafe-headers
Browse files Browse the repository at this point in the history
Use HeaderMap.setUnsafe when possible
  • Loading branch information
vkostyukov authored Nov 9, 2018
2 parents 5604f14 + b10c763 commit 6f5006f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
7 changes: 0 additions & 7 deletions core/src/main/scala/io/finch/Output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,6 @@ object Output {
def toResponse[CT](implicit
tr: ToResponse.Aux[A, CT],
tre: ToResponse.Aux[Exception, CT]
): Response = {
convertToResponse(tr.apply, tre.apply)
}

private[finch] def convertToResponse(
tr: (A, Charset) => Response,
tre: (Exception, Charset) => Response
): Response = {
val rep = o match {
case p: Output.Payload[A] => tr(p.value, p.charset.getOrElse(StandardCharsets.UTF_8))
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/io/finch/ToResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ trait LowPriorityToResponseInstances {
val writable = rep.writer

as.foreachF(chunk => writable.write(writer(chunk, cs))).ensure(writable.close())
rep.contentType = w.value
rep.headerMap.setUnsafe("Content-Type", w.value)

rep
}
Expand Down Expand Up @@ -80,7 +80,7 @@ trait HighPriorityToResponseInstances extends LowPriorityToResponseInstances {

if (!buf.isEmpty) {
rep.content = buf
rep.contentType = w.value
rep.headerMap.setUnsafe("Content-Type", w.value)
}

rep
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/scala/io/finch/ToService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ object ToService {
rep.version = version

if (options.includeDateHeader) {
rep.date = currentTime()
rep.headerMap.setUnsafe("Date", currentTime())
}

if (options.includeServerHeader) {
rep.server = "Finch"
rep.headerMap.setUnsafe("Server", "Finch")
}

rep
Expand Down Expand Up @@ -120,26 +120,26 @@ object ToService {
val accept =
if (opts.negotiateContentType) req.accept.map(a => Accept.fromString(a)) else Nil

val promise = Promise[Response]
val rep = new Promise[Response]

(effect match {
case concurrent: ConcurrentEffect[F] =>
(concurrent.runCancelable(out) _).andThen(io => io.map(cancelToken =>
promise.setInterruptHandler {
rep.setInterruptHandler {
case _ => concurrent.toIO(cancelToken).unsafeRunAsyncAndForget()
}
))
case e => e.runAsync(out) _
}) {
case Left(t) => IO(promise.setException(t))
case Left(t) => IO(rep.setException(t))
case Right(v) => IO {
promise.setValue(
conformHttp(v.convertToResponse(ntrA(accept).apply, ntrE(accept).apply), req.version, opts)
rep.setValue(
conformHttp(v.toResponse(ntrA(accept), ntrE(accept)), req.version, opts)
)
}
}.unsafeRunSync()

promise
rep

case EndpointResult.NotMatched.MethodNotAllowed(allowed) =>
tsT(es.tail, opts, ctx.copy(wouldAllow = ctx.wouldAllow ++ allowed))(req)
Expand Down

0 comments on commit 6f5006f

Please sign in to comment.