From db60d3d32bd8e93fa64ee1468827fe3db49ef337 Mon Sep 17 00:00:00 2001 From: Ian Partridge Date: Thu, 11 Apr 2019 23:48:14 +0100 Subject: [PATCH] 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. --- Sources/NIOHTTPClient/HTTPHandler.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/NIOHTTPClient/HTTPHandler.swift b/Sources/NIOHTTPClient/HTTPHandler.swift index 5522deb..a77f546 100644 --- a/Sources/NIOHTTPClient/HTTPHandler.swift +++ b/Sources/NIOHTTPClient/HTTPHandler.swift @@ -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: 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)) }