@@ -97,7 +97,8 @@ private enum LocalLambda {
97
97
public typealias InboundIn = HTTPServerRequestPart
98
98
public typealias OutboundOut = HTTPServerResponsePart
99
99
100
- private var pending = CircularBuffer < ( head: HTTPRequestHead , body: ByteBuffer ? ) > ( )
100
+ private var requestHead : HTTPRequestHead ?
101
+ private var requestBody : ByteBuffer ?
101
102
102
103
private static var invocations = CircularBuffer < Invocation > ( )
103
104
private static var invocationState = InvocationState . waitingForLambdaRequest
@@ -110,23 +111,27 @@ private enum LocalLambda {
110
111
self . invocationEndpoint = invocationEndpoint
111
112
}
112
113
114
+ func handlerAdded( context: ChannelHandlerContext ) {
115
+ self . requestBody = context. channel. allocator. buffer ( capacity: 0 )
116
+ }
117
+
113
118
func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
114
119
let requestPart = unwrapInboundIn ( data)
115
120
116
121
switch requestPart {
117
122
case . head( let head) :
118
- self . pending. append ( ( head: head, body: nil ) )
119
- case . body( var buffer) :
120
- var request = self . pending. removeFirst ( )
121
- if request. body == nil {
122
- request. body = buffer
123
- } else {
124
- request. body!. writeBuffer ( & buffer)
125
- }
126
- self . pending. prepend ( request)
123
+ precondition ( self . requestHead == nil , " received two HTTP heads " )
124
+ precondition ( self . requestBody != nil , " body buffer is not initialized " )
125
+ self . requestHead = head
126
+ self . requestBody!. clear ( )
127
+ case . body( buffer: var buf) :
128
+ precondition ( self . requestHead != nil , " received HTTP body before head " )
129
+ precondition ( self . requestBody != nil , " body buffer is not initialized " )
130
+ self . requestBody!. writeBuffer ( & buf)
127
131
case . end:
128
- let request = self . pending. removeFirst ( )
129
- self . processRequest ( context: context, request: request)
132
+ precondition ( self . requestHead != nil , " received HTTP end before head " )
133
+ self . processRequest ( context: context, request: ( head: self . requestHead!, body: self . requestBody) )
134
+ self . requestHead = nil
130
135
}
131
136
}
132
137
0 commit comments