fix: length calculation of .string HTTPBody (#11)

Currently, if an `HTTPBody` is set to .string, the length is calculated as
string.count. In Swift, this is the number of extended grapheme clusters, not
the UTF8 byte count. Instead, we should use the count of the UTF8View, as the
bytes will be transmitted in this encoding.
This commit is contained in:
Ian Partridge
2019-04-11 23:48:14 +01:00
committed by GitHub
parent 4acd312a5a
commit db60d3d32b
+2 -2
View File
@@ -52,7 +52,7 @@ public enum HTTPBody: Equatable {
case .data(let data):
return data.count
case .string(let string):
return string.count
return string.utf8.count
}
}
}
@@ -324,7 +324,7 @@ class HTTPTaskHandler<T: HTTPResponseDelegate>: ChannelInboundHandler, ChannelOu
buffer.writeBytes(data)
part = HTTPClientRequestPart.body(.byteBuffer(buffer))
case .string(let string):
var buffer = context.channel.allocator.buffer(capacity: string.count)
var buffer = context.channel.allocator.buffer(capacity: string.utf8.count)
buffer.writeString(string)
part = HTTPClientRequestPart.body(.byteBuffer(buffer))
}