@@ -16,6 +16,7 @@ import Foundation
1616import NIO
1717import NIOConcurrencyHelpers
1818import NIOHTTP1
19+ import NIOHTTPCompression
1920import NIOSSL
2021
2122/// HTTPClient class provides API for request execution.
@@ -252,6 +253,13 @@ public class HTTPClient {
252253 case . some( let proxy) :
253254 return channel. pipeline. addProxyHandler ( for: request, decoder: decoder, encoder: encoder, tlsConfiguration: self . configuration. tlsConfiguration, proxy: proxy)
254255 }
256+ } . flatMap {
257+ switch self . configuration. decompression {
258+ case . disabled:
259+ return channel. eventLoop. makeSucceededFuture ( ( ) )
260+ case . enabled( let limit) :
261+ return channel. pipeline. addHandler ( NIOHTTPResponseDecompressor ( limit: limit) )
262+ }
255263 } . flatMap {
256264 if let timeout = self . resolve ( timeout: self . configuration. timeout. read, deadline: deadline) {
257265 return channel. pipeline. addHandler ( IdleStateHandler ( readTimeout: timeout) )
@@ -322,31 +330,37 @@ public class HTTPClient {
322330 public var timeout : Timeout
323331 /// Upstream proxy, defaults to no proxy.
324332 public var proxy : Proxy ?
333+ /// Enables automatic body decompression. Supported algorithms are gzip and deflate.
334+ public var decompression : Decompression
325335 /// Ignore TLS unclean shutdown error, defaults to `false`.
326336 public var ignoreUncleanSSLShutdown : Bool
327337
328- public init ( tlsConfiguration: TLSConfiguration ? = nil , followRedirects: Bool = false , timeout: Timeout = Timeout ( ) , proxy: Proxy ? = nil ) {
329- self . init ( tlsConfiguration: tlsConfiguration, followRedirects: followRedirects, timeout: timeout, proxy: proxy, ignoreUncleanSSLShutdown: false )
330- }
331-
332- public init ( tlsConfiguration: TLSConfiguration ? = nil , followRedirects: Bool = false , timeout: Timeout = Timeout ( ) , proxy: Proxy ? = nil , ignoreUncleanSSLShutdown: Bool = false ) {
338+ public init ( tlsConfiguration: TLSConfiguration ? = nil ,
339+ followRedirects: Bool = false ,
340+ timeout: Timeout = Timeout ( ) ,
341+ proxy: Proxy ? = nil ,
342+ ignoreUncleanSSLShutdown: Bool = false ,
343+ decompression: Decompression = . disabled) {
333344 self . tlsConfiguration = tlsConfiguration
334345 self . followRedirects = followRedirects
335346 self . timeout = timeout
336347 self . proxy = proxy
337348 self . ignoreUncleanSSLShutdown = ignoreUncleanSSLShutdown
349+ self . decompression = decompression
338350 }
339351
340- public init ( certificateVerification: CertificateVerification , followRedirects: Bool = false , timeout: Timeout = Timeout ( ) , proxy: Proxy ? = nil ) {
341- self . init ( certificateVerification: certificateVerification, followRedirects: followRedirects, timeout: timeout, proxy: proxy, ignoreUncleanSSLShutdown: false )
342- }
343-
344- public init ( certificateVerification: CertificateVerification , followRedirects: Bool = false , timeout: Timeout = Timeout ( ) , proxy: Proxy ? = nil , ignoreUncleanSSLShutdown: Bool = false ) {
352+ public init ( certificateVerification: CertificateVerification ,
353+ followRedirects: Bool = false ,
354+ timeout: Timeout = Timeout ( ) ,
355+ proxy: Proxy ? = nil ,
356+ ignoreUncleanSSLShutdown: Bool = false ,
357+ decompression: Decompression = . disabled) {
345358 self . tlsConfiguration = TLSConfiguration . forClient ( certificateVerification: certificateVerification)
346359 self . followRedirects = followRedirects
347360 self . timeout = timeout
348361 self . proxy = proxy
349362 self . ignoreUncleanSSLShutdown = ignoreUncleanSSLShutdown
363+ self . decompression = decompression
350364 }
351365 }
352366
@@ -403,6 +417,14 @@ public class HTTPClient {
403417 return EventLoopPreference ( . delegateAndChannel( on: eventLoop) )
404418 }
405419 }
420+
421+ /// Specifies decompression settings.
422+ public enum Decompression {
423+ /// Decompression is disabled.
424+ case disabled
425+ /// Decompression is enabled.
426+ case enabled( limit: NIOHTTPDecompression . DecompressionLimit )
427+ }
406428}
407429
408430extension HTTPClient . Configuration {
@@ -508,6 +530,6 @@ public struct HTTPClientError: Error, Equatable, CustomStringConvertible {
508530 public static let invalidProxyResponse = HTTPClientError ( code: . invalidProxyResponse)
509531 /// Request does not contain `Content-Length` header.
510532 public static let contentLengthMissing = HTTPClientError ( code: . contentLengthMissing)
511- /// Proxy Authentication Required
533+ /// Proxy Authentication Required.
512534 public static let proxyAuthenticationRequired = HTTPClientError ( code: . proxyAuthenticationRequired)
513535}
0 commit comments