Skip to content

Commit db60d3d

Browse files
authored
fix: length calculation of .string HTTPBody (AsyncHttpClient#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.
1 parent 4acd312 commit db60d3d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Sources/NIOHTTPClient/HTTPHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public enum HTTPBody: Equatable {
5252
case .data(let data):
5353
return data.count
5454
case .string(let string):
55-
return string.count
55+
return string.utf8.count
5656
}
5757
}
5858
}
@@ -324,7 +324,7 @@ class HTTPTaskHandler<T: HTTPResponseDelegate>: ChannelInboundHandler, ChannelOu
324324
buffer.writeBytes(data)
325325
part = HTTPClientRequestPart.body(.byteBuffer(buffer))
326326
case .string(let string):
327-
var buffer = context.channel.allocator.buffer(capacity: string.count)
327+
var buffer = context.channel.allocator.buffer(capacity: string.utf8.count)
328328
buffer.writeString(string)
329329
part = HTTPClientRequestPart.body(.byteBuffer(buffer))
330330
}

0 commit comments

Comments
 (0)