Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/GRPC/Compression/Zlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ enum Zlib {
/// - Returns: The number of bytes written into `output`.
@discardableResult
func inflate(_ input: inout ByteBuffer, into output: inout ByteBuffer) throws -> Int {
if input.readableBytes == 0 {
// Zero length compressed messages are always empty messages. Skip the inflate step
// below and just return the number of bytes we wrote.
return 0
}

return try input.readWithUnsafeMutableReadableBytes { inputPointer -> (Int, Int) in
// Setup the input buffer.
self.stream.availableInputBytes = inputPointer.count
Expand Down
12 changes: 12 additions & 0 deletions Tests/GRPCTests/ZlibTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,16 @@ class ZlibTests: GRPCTestCase {
let ratio: DecompressionLimit = .ratio(2)
XCTAssertEqual(ratio.maximumDecompressedSize(compressedSize: 10), 20)
}

func testDecompressEmptyPayload() throws {
for limit in [DecompressionLimit.ratio(1), .absolute(1)] {
for format in [Zlib.CompressionFormat.deflate, .gzip] {
var compressed = self.allocator.buffer(capacity: 0)
let inflate = Zlib.Inflate(format: format, limit: limit)
var output = self.allocator.buffer(capacity: 0)
XCTAssertEqual(try inflate.inflate(&compressed, into: &output), 0)
XCTAssertEqual(output.readableBytes, 0)
}
}
}
}