Skip to content

Commit ae29289

Browse files
committed
wip conformance to Swift 6
1 parent b4f0164 commit ae29289

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift

+17-12
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ private enum LocalLambda {
9797
public typealias InboundIn = HTTPServerRequestPart
9898
public typealias OutboundOut = HTTPServerResponsePart
9999

100-
private var pending = CircularBuffer<(head: HTTPRequestHead, body: ByteBuffer?)>()
100+
private var requestHead: HTTPRequestHead?
101+
private var requestBody: ByteBuffer?
101102

102103
private static var invocations = CircularBuffer<Invocation>()
103104
private static var invocationState = InvocationState.waitingForLambdaRequest
@@ -110,23 +111,27 @@ private enum LocalLambda {
110111
self.invocationEndpoint = invocationEndpoint
111112
}
112113

114+
func handlerAdded(context: ChannelHandlerContext) {
115+
self.requestBody = context.channel.allocator.buffer(capacity: 0)
116+
}
117+
113118
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
114119
let requestPart = unwrapInboundIn(data)
115120

116121
switch requestPart {
117122
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)
127131
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
130135
}
131136
}
132137

0 commit comments

Comments
 (0)