@@ -165,14 +165,6 @@ class _TCPSocket {
165
165
#endif
166
166
return String ( cString: & buffer)
167
167
}
168
-
169
- func split( _ str: String , _ count: Int ) -> [ String ] {
170
- return stride ( from: 0 , to: str. count, by: count) . map { i -> String in
171
- let startIndex = str. index ( str. startIndex, offsetBy: i)
172
- let endIndex = str. index ( startIndex, offsetBy: count, limitedBy: str. endIndex) ?? str. endIndex
173
- return String ( str [ startIndex..< endIndex] )
174
- }
175
- }
176
168
177
169
func writeRawData( _ data: Data ) throws {
178
170
#if os(Windows)
@@ -202,19 +194,23 @@ class _TCPSocket {
202
194
#endif
203
195
}
204
196
205
- func writeData( header: String , body : String , sendDelay: TimeInterval ? = nil , bodyChunks: Int ? = nil ) throws {
197
+ func writeData( header: String , bodyData : Data , sendDelay: TimeInterval ? = nil , bodyChunks: Int ? = nil ) throws {
206
198
_ = try _send ( Array ( header. utf8) )
207
199
208
200
if let sendDelay = sendDelay, let bodyChunks = bodyChunks {
209
- let count = max ( 1 , Int ( Double ( body. utf8. count) / Double( bodyChunks) ) )
210
- let texts = split ( body, count)
211
-
212
- for item in texts {
201
+ let count = max ( 1 , Int ( Double ( bodyData. count) / Double( bodyChunks) ) )
202
+ for startIndex in stride ( from: 0 , to: bodyData. count, by: count) {
213
203
Thread . sleep ( forTimeInterval: sendDelay)
214
- _ = try _send ( Array ( item. utf8) )
204
+ let endIndex = min ( startIndex + count, bodyData. count)
205
+ try bodyData. withUnsafeBytes { ( ptr: UnsafeRawBufferPointer ) -> Void in
206
+ let chunk = UnsafeRawBufferPointer ( rebasing: ptr [ startIndex..< endIndex] )
207
+ _ = try _send ( Array ( chunk. bindMemory ( to: UInt8 . self) ) )
208
+ }
215
209
}
216
210
} else {
217
- _ = try _send ( Array ( body. utf8) )
211
+ try bodyData. withUnsafeBytes { ( ptr: UnsafeRawBufferPointer ) -> Void in
212
+ _ = try _send ( Array ( ptr. bindMemory ( to: UInt8 . self) ) )
213
+ }
218
214
}
219
215
}
220
216
@@ -302,7 +298,7 @@ class _HTTPServer {
302
298
Thread . sleep ( forTimeInterval: delay)
303
299
}
304
300
do {
305
- try self . socket. writeData ( header: response. header, body : response. body , sendDelay: sendDelay, bodyChunks: bodyChunks)
301
+ try self . socket. writeData ( header: response. header, bodyData : response. bodyData , sendDelay: sendDelay, bodyChunks: bodyChunks)
306
302
} catch {
307
303
}
308
304
}
@@ -455,14 +451,18 @@ struct _HTTPResponse {
455
451
}
456
452
private let responseCode : Response
457
453
private let headers : String
458
- public let body : String
454
+ public let bodyData : Data
459
455
460
- public init ( response: Response , headers: String = _HTTPUtils. EMPTY, body : String ) {
456
+ public init ( response: Response , headers: String = _HTTPUtils. EMPTY, bodyData : Data ) {
461
457
self . responseCode = response
462
458
self . headers = headers
463
- self . body = body
459
+ self . bodyData = bodyData
464
460
}
465
-
461
+
462
+ public init ( response: Response , headers: String = _HTTPUtils. EMPTY, body: String ) {
463
+ self . init ( response: response, headers: headers, bodyData: body. data ( using: . utf8) !)
464
+ }
465
+
466
466
public var header : String {
467
467
let statusLine = _HTTPUtils. VERSION + _HTTPUtils. SPACE + " \( responseCode. rawValue) " + _HTTPUtils. SPACE + " \( responseCode) "
468
468
return statusLine + ( headers != _HTTPUtils. EMPTY ? _HTTPUtils. CRLF + headers : _HTTPUtils. EMPTY) + _HTTPUtils. CRLF2
@@ -639,6 +639,19 @@ public class TestURLSessionServer {
639
639
return httpResponse
640
640
641
641
}
642
+
643
+ if uri == " /gzipped-response " {
644
+ // This is "Hello World!" gzipped.
645
+ let helloWorld = Data ( [ 0x1f , 0x8b , 0x08 , 0x00 , 0x6d , 0xca , 0xb2 , 0x5c ,
646
+ 0x00 , 0x03 , 0xf3 , 0x48 , 0xcd , 0xc9 , 0xc9 , 0x57 ,
647
+ 0x08 , 0xcf , 0x2f , 0xca , 0x49 , 0x51 , 0x04 , 0x00 ,
648
+ 0xa3 , 0x1c , 0x29 , 0x1c , 0x0c , 0x00 , 0x00 , 0x00 ] )
649
+ return _HTTPResponse ( response: . OK,
650
+ headers: [ " Content-Length: \( helloWorld. count) " ,
651
+ " Content-Encoding: gzip " ] . joined ( separator: _HTTPUtils. CRLF) ,
652
+ bodyData: helloWorld)
653
+ }
654
+
642
655
return _HTTPResponse ( response: . OK, body: capitals [ String ( uri. dropFirst ( ) ) ] !)
643
656
}
644
657
@@ -738,7 +751,7 @@ class LoopbackServerTest : XCTestCase {
738
751
739
752
let timeout = DispatchTime ( uptimeNanoseconds: DispatchTime . now ( ) . uptimeNanoseconds + 2_000_000_000 )
740
753
741
- while serverPort == - 2 {
754
+ while serverPort == - 1 {
742
755
guard serverReady. wait ( timeout: timeout) == . success else {
743
756
fatalError ( " Timedout waiting for server to be ready " )
744
757
}
0 commit comments