-
Notifications
You must be signed in to change notification settings - Fork 1k
/
NettyHttp1Handler.kt
161 lines (138 loc) · 5.27 KB
/
NettyHttp1Handler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.server.netty.http1
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.netty.cio.*
import io.ktor.util.cio.*
import io.ktor.utils.io.*
import io.netty.channel.*
import io.netty.handler.codec.http.*
import io.netty.util.concurrent.*
import kotlinx.atomicfu.*
import kotlinx.coroutines.*
import java.io.*
import kotlin.coroutines.*
internal class NettyHttp1Handler(
private val enginePipeline: EnginePipeline,
private val environment: ApplicationEngineEnvironment,
private val callEventGroup: EventExecutorGroup,
private val engineContext: CoroutineContext,
private val userContext: CoroutineContext,
private val runningLimit: Int
) : ChannelInboundHandlerAdapter(), CoroutineScope {
private val handlerJob = CompletableDeferred<Nothing>()
override val coroutineContext: CoroutineContext get() = handlerJob
private var skipEmpty = false
private lateinit var responseWriter: NettyHttpResponsePipeline
private val state = NettyHttpHandlerState(runningLimit)
override fun channelActive(context: ChannelHandlerContext) {
responseWriter = NettyHttpResponsePipeline(
context,
state,
coroutineContext
)
context.channel().config().isAutoRead = false
context.channel().read()
context.pipeline().apply {
addLast(RequestBodyHandler(context))
addLast(callEventGroup, NettyApplicationCallHandler(userContext, enginePipeline))
}
context.fireChannelActive()
}
override fun channelRead(context: ChannelHandlerContext, message: Any) {
if (message is LastHttpContent) {
state.isCurrentRequestFullyRead.compareAndSet(expect = false, update = true)
}
when {
message is HttpRequest -> {
if (message !is LastHttpContent) {
state.isCurrentRequestFullyRead.compareAndSet(expect = true, update = false)
}
state.isChannelReadCompleted.compareAndSet(expect = true, update = false)
state.activeRequests.incrementAndGet()
handleRequest(context, message)
callReadIfNeeded(context)
}
message is LastHttpContent && !message.content().isReadable && skipEmpty -> {
skipEmpty = false
message.release()
callReadIfNeeded(context)
}
else -> {
context.fireChannelRead(message)
}
}
}
override fun channelInactive(context: ChannelHandlerContext) {
context.pipeline().remove(NettyApplicationCallHandler::class.java)
context.fireChannelInactive()
}
@Suppress("OverridingDeprecatedMember")
override fun exceptionCaught(context: ChannelHandlerContext, cause: Throwable) {
if (cause is IOException || cause is ChannelIOException) {
environment.application.log.debug("I/O operation failed", cause)
handlerJob.cancel()
} else {
handlerJob.completeExceptionally(cause)
}
context.close()
}
override fun channelReadComplete(context: ChannelHandlerContext?) {
state.isChannelReadCompleted.compareAndSet(expect = false, update = true)
responseWriter.flushIfNeeded()
super.channelReadComplete(context)
}
private fun handleRequest(context: ChannelHandlerContext, message: HttpRequest) {
val call = prepareCallFromRequest(context, message)
context.fireChannelRead(call)
responseWriter.processResponse(call)
}
/**
* Returns netty application call with [message] as a request
* and channel for request body
*/
private fun prepareCallFromRequest(
context: ChannelHandlerContext,
message: HttpRequest
): NettyHttp1ApplicationCall {
val requestBodyChannel = when {
message is LastHttpContent && !message.content().isReadable -> null
message.method() === HttpMethod.GET &&
!HttpUtil.isContentLengthSet(message) && !HttpUtil.isTransferEncodingChunked(message) -> {
skipEmpty = true
null
}
else -> prepareRequestContentChannel(context, message)
}
return NettyHttp1ApplicationCall(
environment.application,
context,
message,
requestBodyChannel,
engineContext,
userContext
)
}
private fun prepareRequestContentChannel(
context: ChannelHandlerContext,
message: HttpRequest
): ByteReadChannel {
val bodyHandler = context.pipeline().get(RequestBodyHandler::class.java)
val result = bodyHandler.newChannel()
if (message is HttpContent) {
bodyHandler.channelRead(context, message)
}
return result
}
private fun callReadIfNeeded(context: ChannelHandlerContext) {
if (state.activeRequests.value < runningLimit) {
context.read()
state.skippedRead.value = false
} else {
state.skippedRead.value = true
}
}
}