@@ -188,8 +188,10 @@ type
188188 reader* : HttpBodyReader
189189 error* : ref HttpError
190190 bodyFlag* : HttpClientBodyFlag
191- contentEncoding* : set [ContentEncodingFlags ]
192- transferEncoding* : set [TransferEncodingFlags ]
191+ contentEncoding* {.deprecated .}: set [ContentEncodingFlags ]
192+ transferEncoding* {.deprecated .}: set [TransferEncodingFlags ]
193+ contentEncodings* : seq [ContentEncodingFlags ]
194+ transferEncodings* : seq [TransferEncodingFlags ]
193195 contentLength* : uint64
194196 contentType* : Opt [ContentTypeData ]
195197 timestamp* : Moment
@@ -268,8 +270,9 @@ template isIdle(conn: HttpClientConnectionRef, timestamp: Moment,
268270
269271proc sessionWatcher (session: HttpSessionRef ) {.async : (raises: []).}
270272proc directProvider * (): HttpConnectionProvider
273+
271274proc new * (t: typedesc [HttpSessionRef ],
272- flags: HttpClientFlags = {NewConnectionAlways },
275+ flags: HttpClientFlags = {},
273276 maxRedirections = HttpMaxRedirections ,
274277 connectTimeout = HttpConnectTimeout ,
275278 headersTimeout = HttpHeadersTimeout ,
@@ -288,6 +291,31 @@ proc new*(t: typedesc[HttpSessionRef],
288291 # # ``idleTimeout`` - timeout to consider HTTP connection as idle
289292 # # ``idlePeriod`` - period of time to check HTTP connections for inactivity
290293 doAssert (maxRedirections >= 0 , " maxRedirections should not be negative" )
294+
295+ # TODO chronos v4.2 and earlier enabled persistent connections only
296+ # together with the pipelining flag (persistent connections are
297+ # necessary but not sufficient for pipelining).
298+ #
299+ # Since users of chronos (like presto and json-rpc) didn't include
300+ # pipelining in their defaults, this had the practical effect of disabling
301+ # persistent connections by default which is a good thing because the
302+ # current implementation of connection reuse in the http client is
303+ # practically unusable due to the lack of EOF monitoring (without EOF
304+ # monitoring, connections that get closed are not detected leading to
305+ # frequent request failures in most usage scenarios).
306+ #
307+ # Although connection reuse is useful even without pipelining, we'll
308+ # keep using the deprecated pipelining flag as a gatekeeper so that
309+ # practically, it remains disabled in most cases - this gatekeeper
310+ # will be removed once reuse is feature complete.
311+ {.push warning [Deprecated ]: off .}
312+ let flags =
313+ if HttpClientFlag .Http11Pipeline notin flags:
314+ flags + {HttpClientFlag .NewConnectionAlways }
315+ else :
316+ flags
317+ {.pop .}
318+
291319 let res = HttpSessionRef (
292320 flags: flags,
293321 maxRedirections: maxRedirections,
@@ -357,17 +385,20 @@ proc getHttpAddress*(
357385 path: url.path, query: url.query, anchor: url.anchor,
358386 username: url.username, password: url.password))
359387
388+ {.push warning [Deprecated ]: off .}
360389proc getHttpAddress * (
361390 uri: Uri ,
362391 flags: HttpClientFlags
363392 ): HttpAddressResult {.deprecated : " No DNS resolution in getHttpAddress, no flags needed" .} =
364393 getHttpAddress (uri)
394+ {.pop .}
365395
366396proc getHttpAddress * (
367397 url: string ,
368398 ): HttpAddressResult =
369399 getHttpAddress (parseUri (url))
370400
401+ {.push warning [Deprecated ]: off .}
371402proc getHttpAddress * (
372403 url: string ,
373404 flags: HttpClientFlags
@@ -387,6 +418,8 @@ proc getHttpAddress*(
387418 # # Create new HTTP address using URL string ``url`` and .
388419 getHttpAddress (parseUri (url))
389420
421+ {.pop .}
422+
390423proc getAddress * (session: HttpSessionRef , url: Uri ): HttpResult [HttpAddress ] {.deprecated : " use getHttpAddress" .} =
391424 let res = getHttpAddress (url).valueOr:
392425 return err ($ error)
@@ -721,7 +754,6 @@ proc reuseOrConnect(
721754 connection.flags.incl (HttpClientConnectionFlag .KeepAlive )
722755 connection.state = HttpClientConnectionState .Acquired
723756 return connection
724-
725757 let connection =
726758 try :
727759 await session.connect (ha).wait (session.connectTimeout)
@@ -906,22 +938,27 @@ proc prepareResponse(
906938 res
907939
908940 # Preprocessing "Content-Encoding" header.
909- let contentEncoding =
910- block :
911- let res = getContentEncoding (headers.getList (ContentEncodingHeader ))
912- if res.isErr ():
913- return err (" Invalid headers received, invalid `Content-Encoding`" )
941+ let
942+ contentEncodings = getContentEncodings (headers.getList (ContentEncodingHeader )).valueOr:
943+ return err (" Invalid `Content-Encoding` in headers" )
944+ contentEncoding = {
945+ if contentEncodings.len () > 0 :
946+ contentEncodings[^ 1 ]
914947 else :
915- res.get ()
948+ ContentEncodingFlags .Identity
949+ }
916950
917951 # Preprocessing "Transfer-Encoding" header.
918- let transferEncoding =
919- block :
920- let res = getTransferEncoding (headers.getList (TransferEncodingHeader ))
921- if res.isErr ():
922- return err (" Invalid headers received, invalid `Transfer-Encoding`" )
952+ let
953+ transferEncodings = getTransferEncodings (
954+ headers.getList (TransferEncodingHeader )).valueOr:
955+ return err (" Invalid `Transfer-Encoding` in headers" )
956+ transferEncoding = {
957+ if transferEncodings.len () > 0 :
958+ transferEncodings[^ 1 ]
923959 else :
924- res.get ()
960+ TransferEncodingFlags .Identity
961+ }
925962
926963 # Preprocessing "Content-Length" header.
927964 let (contentLength, bodyFlag) =
@@ -934,9 +971,9 @@ proc prepareResponse(
934971 # header
935972 let length = headers.getInt (ContentLengthHeader )
936973 (length, HttpClientBodyFlag .NoBody )
937- elif transferEncoding != { TransferEncodingFlags . Identity } :
974+ elif transferEncodings.len > 0 :
938975 # "Transfer-Encoding overrides the Content-Length"
939- if TransferEncodingFlags .Chunked in transferEncoding :
976+ if transferEncodings[ ^ 1 ] == TransferEncodingFlags .Chunked :
940977 (0 'u64 , HttpClientBodyFlag .Chunked )
941978 else :
942979 (0 'u64 , HttpClientBodyFlag .Custom )
@@ -986,6 +1023,7 @@ proc prepareResponse(
9861023 reason: resp.reason (data), version: resp.version, session: request.session,
9871024 connection: connection, headers: headers,
9881025 contentEncoding: contentEncoding, transferEncoding: transferEncoding,
1026+ contentEncodings: contentEncodings, transferEncodings: transferEncodings,
9891027 contentLength: contentLength, contentType: contentType, bodyFlag: bodyFlag
9901028 )
9911029
0 commit comments