Skip to content

Commit 6bec050

Browse files
committed
Fix missing messages return on finish() call
1 parent 893aeb5 commit 6bec050

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Task {
5555
}
5656
}
5757
} catch {
58-
try await parser.finish()
58+
// Calling finish might return messages that are considered complete now we know no more data is comming in. (For example a single HTTP request with no content length set.)
59+
// It can also fail if the HTTP message is invalid at this point and throw an error.
60+
let messages = try? await parser.finish()
5961
}
6062
}
6163
```

Sources/llhttp/HTTPMessagesParser.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,12 @@ public actor HTTPMessagesParser<MessageType: HTTPMessageType> {
207207
///
208208
/// Requests without Content-Length and other messages might require treating all incoming bytes as the part of the body, up to the last byte of the connection.
209209
///
210-
/// This method will trigger the callback if the request was terminated safely. Otherwise a error code would be returned.
211-
public func finish() async throws {
210+
/// This method will trigger the callback if the request was terminated safely. Otherwise an error would be thrown.
211+
public func finish() async throws -> [MessageType] {
212212
try llhttp.finish()
213+
214+
defer { messagesCollector = [] }
215+
return messagesCollector
213216
}
214217
}
215218
}

0 commit comments

Comments
 (0)