@@ -243,22 +243,36 @@ internal struct LambdaHTTPServer {
243
243
requestHead = head
244
244
245
245
case . body( let body) :
246
- requestBody. setOrWriteImmutableBuffer ( body)
246
+ precondition ( requestHead != nil , " Received .body without .head " )
247
+
248
+ // if this is a request from a Streaming Lambda Handler,
249
+ // stream the response instead of buffering it
250
+ if self . isStreamingResponse ( requestHead) {
251
+ // we are receiving a chunked body,
252
+ // we can stream the response and not accumulate the chunks
253
+ print ( String ( buffer: body) )
254
+ } else {
255
+ requestBody. setOrWriteImmutableBuffer ( body)
256
+ }
247
257
248
258
case . end:
249
259
precondition ( requestHead != nil , " Received .end without .head " )
250
- // process the request
251
- let response = try await self . processRequest (
252
- head: requestHead,
253
- body: requestBody,
254
- logger: logger
255
- )
256
- // send the responses
257
- try await self . sendResponse (
258
- response: response,
259
- outbound: outbound,
260
- logger: logger
261
- )
260
+
261
+ // process the buffered response for non streaming requests
262
+ if !self . isStreamingResponse ( requestHead) {
263
+ // process the complete request
264
+ let response = try await self . processCompleteRequest (
265
+ head: requestHead,
266
+ body: requestBody,
267
+ logger: logger
268
+ )
269
+ // send the responses
270
+ try await self . sendCompleteResponse (
271
+ response: response,
272
+ outbound: outbound,
273
+ logger: logger
274
+ )
275
+ }
262
276
263
277
requestHead = nil
264
278
requestBody = nil
@@ -273,6 +287,15 @@ internal struct LambdaHTTPServer {
273
287
}
274
288
}
275
289
290
+ /// This function checks if the request is a streaming response request
291
+ /// verb = POST, uri = :requestID/response, HTTP Header contains "Transfer-Encoding: chunked"
292
+ private func isStreamingResponse( _ requestHead: HTTPRequestHead ) -> Bool {
293
+ requestHead. method == . POST &&
294
+ requestHead. uri. hasSuffix ( Consts . postResponseURLSuffix) &&
295
+ requestHead. headers. contains ( name: " Transfer-Encoding " ) &&
296
+ requestHead. headers [ " Transfer-Encoding " ] . contains ( " chunked " )
297
+ }
298
+
276
299
/// This function process the URI request sent by the client and by the Lambda function
277
300
///
278
301
/// It enqueues the client invocation and iterate over the invocation queue when the Lambda function sends /next request
@@ -283,7 +306,7 @@ internal struct LambdaHTTPServer {
283
306
/// - body: the HTTP request body
284
307
/// - Throws:
285
308
/// - Returns: the response to send back to the client or the Lambda function
286
- private func processRequest (
309
+ private func processCompleteRequest (
287
310
head: HTTPRequestHead ,
288
311
body: ByteBuffer ? ,
289
312
logger: Logger
@@ -406,7 +429,7 @@ internal struct LambdaHTTPServer {
406
429
}
407
430
}
408
431
409
- private func sendResponse (
432
+ private func sendCompleteResponse (
410
433
response: LocalServerResponse ,
411
434
outbound: NIOAsyncChannelOutboundWriter < HTTPServerResponsePart > ,
412
435
logger: Logger
0 commit comments