httpx 0.24.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +0 -48
  3. data/README.md +4 -13
  4. data/doc/release_notes/0_24_4.md +3 -3
  5. data/doc/release_notes/1_0_0.md +60 -0
  6. data/lib/httpx/adapters/datadog.rb +28 -97
  7. data/lib/httpx/adapters/faraday.rb +9 -52
  8. data/lib/httpx/adapters/webmock.rb +2 -7
  9. data/lib/httpx/altsvc.rb +4 -22
  10. data/lib/httpx/base64.rb +27 -0
  11. data/lib/httpx/chainable.rb +0 -23
  12. data/lib/httpx/connection.rb +11 -32
  13. data/lib/httpx/domain_name.rb +5 -12
  14. data/lib/httpx/errors.rb +0 -2
  15. data/lib/httpx/extensions.rb +0 -124
  16. data/lib/httpx/io/ssl.rb +26 -59
  17. data/lib/httpx/io/tcp.rb +27 -68
  18. data/lib/httpx/io/udp.rb +13 -48
  19. data/lib/httpx/io/unix.rb +1 -8
  20. data/lib/httpx/loggable.rb +4 -19
  21. data/lib/httpx/options.rb +24 -84
  22. data/lib/httpx/plugins/{authentication → auth}/basic.rb +1 -5
  23. data/lib/httpx/plugins/{authentication → auth}/digest.rb +2 -5
  24. data/lib/httpx/plugins/{authentication → auth}/ntlm.rb +1 -3
  25. data/lib/httpx/plugins/{authentication → auth}/socks5.rb +0 -2
  26. data/lib/httpx/plugins/auth.rb +25 -0
  27. data/lib/httpx/plugins/aws_sigv4.rb +0 -1
  28. data/lib/httpx/plugins/{basic_authentication.rb → basic_auth.rb} +5 -6
  29. data/lib/httpx/plugins/brotli.rb +50 -0
  30. data/lib/httpx/plugins/circuit_breaker/circuit.rb +40 -16
  31. data/lib/httpx/plugins/circuit_breaker/circuit_store.rb +16 -5
  32. data/lib/httpx/plugins/circuit_breaker.rb +11 -4
  33. data/lib/httpx/plugins/cookies/set_cookie_parser.rb +0 -2
  34. data/lib/httpx/plugins/cookies.rb +1 -1
  35. data/lib/httpx/plugins/{digest_authentication.rb → digest_auth.rb} +5 -5
  36. data/lib/httpx/plugins/follow_redirects.rb +21 -24
  37. data/lib/httpx/plugins/grpc/grpc_encoding.rb +82 -0
  38. data/lib/httpx/plugins/grpc/message.rb +7 -39
  39. data/lib/httpx/plugins/grpc.rb +15 -21
  40. data/lib/httpx/plugins/h2c.rb +0 -1
  41. data/lib/httpx/plugins/{ntlm_authentication.rb → ntlm_auth.rb} +5 -5
  42. data/lib/httpx/plugins/oauth.rb +2 -2
  43. data/lib/httpx/plugins/proxy/http.rb +0 -2
  44. data/lib/httpx/plugins/proxy/socks4.rb +0 -4
  45. data/lib/httpx/plugins/proxy/socks5.rb +1 -5
  46. data/lib/httpx/plugins/proxy.rb +3 -32
  47. data/lib/httpx/plugins/retries.rb +3 -4
  48. data/lib/httpx/plugins/stream.rb +4 -6
  49. data/lib/httpx/punycode.rb +9 -291
  50. data/lib/httpx/request/body.rb +145 -0
  51. data/lib/httpx/request.rb +2 -119
  52. data/lib/httpx/resolver/https.rb +1 -1
  53. data/lib/httpx/resolver/native.rb +6 -14
  54. data/lib/httpx/resolver/resolver.rb +1 -1
  55. data/lib/httpx/resolver/system.rb +11 -9
  56. data/lib/httpx/response/body.rb +206 -0
  57. data/lib/httpx/response/buffer.rb +90 -0
  58. data/lib/httpx/response.rb +5 -208
  59. data/lib/httpx/selector.rb +0 -2
  60. data/lib/httpx/session.rb +0 -10
  61. data/lib/httpx/transcoder/body.rb +0 -1
  62. data/lib/httpx/transcoder/deflate.rb +37 -0
  63. data/lib/httpx/transcoder/form.rb +52 -32
  64. data/lib/httpx/transcoder/gzip.rb +74 -0
  65. data/lib/httpx/transcoder/json.rb +2 -4
  66. data/lib/httpx/transcoder/multipart/decoder.rb +139 -0
  67. data/lib/httpx/{plugins → transcoder}/multipart/encoder.rb +3 -3
  68. data/lib/httpx/{plugins → transcoder}/multipart/mime_type_detector.rb +1 -1
  69. data/lib/httpx/{plugins → transcoder}/multipart/part.rb +3 -2
  70. data/lib/httpx/transcoder/multipart.rb +17 -0
  71. data/lib/httpx/transcoder/utils/body_reader.rb +46 -0
  72. data/lib/httpx/transcoder/utils/deflater.rb +72 -0
  73. data/lib/httpx/transcoder/xml.rb +0 -2
  74. data/lib/httpx/transcoder.rb +2 -2
  75. data/lib/httpx/utils.rb +10 -20
  76. data/lib/httpx/version.rb +1 -1
  77. data/lib/httpx.rb +0 -8
  78. data/sig/chainable.rbs +5 -6
  79. data/sig/connection.rbs +0 -1
  80. data/sig/errors.rbs +0 -3
  81. data/sig/httpx.rbs +2 -1
  82. data/sig/io/unix.rbs +1 -1
  83. data/sig/options.rbs +12 -8
  84. data/sig/plugins/{authentication → auth}/basic.rbs +0 -2
  85. data/sig/plugins/auth.rbs +13 -0
  86. data/sig/plugins/{basic_authentication.rbs → basic_auth.rbs} +2 -2
  87. data/sig/plugins/brotli.rbs +22 -0
  88. data/sig/plugins/circuit_breaker.rbs +7 -3
  89. data/sig/plugins/compression.rbs +0 -2
  90. data/sig/plugins/{digest_authentication.rbs → digest_auth.rbs} +2 -2
  91. data/sig/plugins/follow_redirects.rbs +0 -1
  92. data/sig/plugins/grpc/call.rbs +19 -0
  93. data/sig/plugins/grpc/grpc_encoding.rbs +33 -0
  94. data/sig/plugins/grpc/message.rbs +17 -0
  95. data/sig/plugins/grpc.rbs +2 -32
  96. data/sig/plugins/{ntlm_authentication.rbs → ntlm_auth.rbs} +2 -2
  97. data/sig/plugins/oauth.rbs +1 -1
  98. data/sig/plugins/proxy/socks4.rbs +2 -3
  99. data/sig/plugins/proxy/socks5.rbs +0 -1
  100. data/sig/plugins/proxy/ssh.rbs +1 -1
  101. data/sig/plugins/response_cache.rbs +5 -2
  102. data/sig/request/body.rbs +42 -0
  103. data/sig/request.rbs +1 -27
  104. data/sig/resolver/resolver.rbs +1 -1
  105. data/sig/response/body.rbs +52 -0
  106. data/sig/response/buffer.rbs +24 -0
  107. data/sig/response.rbs +0 -39
  108. data/sig/transcoder/body.rbs +4 -3
  109. data/sig/transcoder/deflate.rbs +11 -0
  110. data/sig/transcoder/form.rbs +5 -3
  111. data/sig/transcoder/gzip.rbs +24 -0
  112. data/sig/transcoder/json.rbs +4 -2
  113. data/sig/{plugins → transcoder}/multipart.rbs +3 -10
  114. data/sig/transcoder/utils/body_reader.rbs +15 -0
  115. data/sig/transcoder/utils/deflater.rbs +29 -0
  116. data/sig/transcoder.rbs +18 -2
  117. metadata +50 -34
  118. data/lib/httpx/plugins/authentication.rb +0 -24
  119. data/lib/httpx/plugins/compression/brotli.rb +0 -54
  120. data/lib/httpx/plugins/compression/deflate.rb +0 -54
  121. data/lib/httpx/plugins/compression/gzip.rb +0 -90
  122. data/lib/httpx/plugins/compression.rb +0 -165
  123. data/lib/httpx/plugins/multipart/decoder.rb +0 -137
  124. data/lib/httpx/plugins/multipart.rb +0 -96
  125. data/sig/plugins/authentication.rbs +0 -13
  126. data/sig/plugins/compression/brotli.rbs +0 -21
  127. data/sig/plugins/compression/deflate.rbs +0 -17
  128. data/sig/plugins/compression/gzip.rbs +0 -29
  129. /data/sig/plugins/{authentication → auth}/digest.rbs +0 -0
  130. /data/sig/plugins/{authentication → auth}/ntlm.rbs +0 -0
  131. /data/sig/plugins/{authentication → auth}/socks5.rbs +0 -0
@@ -0,0 +1,29 @@
1
+ module HTTPX
2
+ module Transcoder
3
+ class Deflater
4
+ extend Forwardable
5
+
6
+ @body: BodyReader
7
+ @buffer: Response::Buffer?
8
+ @body: _Encoder | StringIO
9
+ @closed: bool
10
+
11
+ attr_reader content_type: String
12
+
13
+ def initialize: (_Encoder body) -> void
14
+
15
+ def bytesize: () -> (Integer | Float)
16
+
17
+ def read: (?int? length, ?string outbuf) -> String?
18
+
19
+ def close: () -> void
20
+
21
+ def deflate: (String? chunk) -> String?
22
+
23
+ private
24
+
25
+ def buffer_deflate!: () -> void
26
+
27
+ end
28
+ end
29
+ end
data/sig/transcoder.rbs CHANGED
@@ -1,7 +1,7 @@
1
1
  module HTTPX
2
- type bodyIO = _Reader | _Each[[String, untyped]] | _ToS
2
+ type bodyIO = _Reader | _Each[String] | _ToS
3
3
 
4
- type body_encoder = Transcoder::_Encoder | _Each[String]
4
+ type body_encoder = (Transcoder::_Encoder & _ToS) | (Transcoder::_Encoder & _Reader) | (Transcoder::_Encoder & _Each[String])
5
5
 
6
6
  module Transcoder
7
7
  def self?.normalize_keys: [U] (_ToS key, _ToAry[untyped] | _ToHash[_ToS, untyped] | untyped value, ?(^(untyped value) -> bool | nil) cond) { (String, ?untyped) -> U } -> U
@@ -18,10 +18,26 @@ module HTTPX
18
18
 
19
19
  interface _Encoder
20
20
  def bytesize: () -> (Integer | Float)
21
+
22
+ def content_type: () -> String
21
23
  end
22
24
 
23
25
  interface _Decoder
24
26
  def call: (Response response, *untyped) -> untyped
25
27
  end
28
+
29
+ interface _Inflater
30
+ def initialize: (Integer | Float bytesize) -> void
31
+
32
+ def call: (String chunk) -> String
33
+ end
34
+
35
+ interface _Deflater
36
+ def initialize: (Response body) -> void
37
+
38
+ def deflate: () -> void
39
+
40
+ def call: (StringIO | File buffer, ?chunk_size: Integer) ?{ (String chunk) -> void } -> void
41
+ end
26
42
  end
27
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Cardoso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-28 00:00:00.000000000 Z
11
+ date: 2023-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http-2-next
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.4.1
19
+ version: 1.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.4.1
26
+ version: 1.0.0
27
27
  description: A client library for making HTTP requests from Ruby.
28
28
  email:
29
29
  - cardoso_tiago@hotmail.com
@@ -127,6 +127,7 @@ extra_rdoc_files:
127
127
  - doc/release_notes/0_8_1.md
128
128
  - doc/release_notes/0_8_2.md
129
129
  - doc/release_notes/0_9_0.md
130
+ - doc/release_notes/1_0_0.md
130
131
  files:
131
132
  - LICENSE.txt
132
133
  - README.md
@@ -225,12 +226,14 @@ files:
225
226
  - doc/release_notes/0_8_1.md
226
227
  - doc/release_notes/0_8_2.md
227
228
  - doc/release_notes/0_9_0.md
229
+ - doc/release_notes/1_0_0.md
228
230
  - lib/httpx.rb
229
231
  - lib/httpx/adapters/datadog.rb
230
232
  - lib/httpx/adapters/faraday.rb
231
233
  - lib/httpx/adapters/sentry.rb
232
234
  - lib/httpx/adapters/webmock.rb
233
235
  - lib/httpx/altsvc.rb
236
+ - lib/httpx/base64.rb
234
237
  - lib/httpx/buffer.rb
235
238
  - lib/httpx/callbacks.rb
236
239
  - lib/httpx/chainable.rb
@@ -249,39 +252,32 @@ files:
249
252
  - lib/httpx/loggable.rb
250
253
  - lib/httpx/options.rb
251
254
  - lib/httpx/parser/http1.rb
252
- - lib/httpx/plugins/authentication.rb
253
- - lib/httpx/plugins/authentication/basic.rb
254
- - lib/httpx/plugins/authentication/digest.rb
255
- - lib/httpx/plugins/authentication/ntlm.rb
256
- - lib/httpx/plugins/authentication/socks5.rb
255
+ - lib/httpx/plugins/auth.rb
256
+ - lib/httpx/plugins/auth/basic.rb
257
+ - lib/httpx/plugins/auth/digest.rb
258
+ - lib/httpx/plugins/auth/ntlm.rb
259
+ - lib/httpx/plugins/auth/socks5.rb
257
260
  - lib/httpx/plugins/aws_sdk_authentication.rb
258
261
  - lib/httpx/plugins/aws_sigv4.rb
259
- - lib/httpx/plugins/basic_authentication.rb
262
+ - lib/httpx/plugins/basic_auth.rb
263
+ - lib/httpx/plugins/brotli.rb
260
264
  - lib/httpx/plugins/circuit_breaker.rb
261
265
  - lib/httpx/plugins/circuit_breaker/circuit.rb
262
266
  - lib/httpx/plugins/circuit_breaker/circuit_store.rb
263
- - lib/httpx/plugins/compression.rb
264
- - lib/httpx/plugins/compression/brotli.rb
265
- - lib/httpx/plugins/compression/deflate.rb
266
- - lib/httpx/plugins/compression/gzip.rb
267
267
  - lib/httpx/plugins/cookies.rb
268
268
  - lib/httpx/plugins/cookies/cookie.rb
269
269
  - lib/httpx/plugins/cookies/jar.rb
270
270
  - lib/httpx/plugins/cookies/set_cookie_parser.rb
271
- - lib/httpx/plugins/digest_authentication.rb
271
+ - lib/httpx/plugins/digest_auth.rb
272
272
  - lib/httpx/plugins/expect.rb
273
273
  - lib/httpx/plugins/follow_redirects.rb
274
274
  - lib/httpx/plugins/grpc.rb
275
275
  - lib/httpx/plugins/grpc/call.rb
276
+ - lib/httpx/plugins/grpc/grpc_encoding.rb
276
277
  - lib/httpx/plugins/grpc/message.rb
277
278
  - lib/httpx/plugins/h2c.rb
278
279
  - lib/httpx/plugins/internal_telemetry.rb
279
- - lib/httpx/plugins/multipart.rb
280
- - lib/httpx/plugins/multipart/decoder.rb
281
- - lib/httpx/plugins/multipart/encoder.rb
282
- - lib/httpx/plugins/multipart/mime_type_detector.rb
283
- - lib/httpx/plugins/multipart/part.rb
284
- - lib/httpx/plugins/ntlm_authentication.rb
280
+ - lib/httpx/plugins/ntlm_auth.rb
285
281
  - lib/httpx/plugins/oauth.rb
286
282
  - lib/httpx/plugins/persistent.rb
287
283
  - lib/httpx/plugins/proxy.rb
@@ -303,6 +299,7 @@ files:
303
299
  - lib/httpx/pool.rb
304
300
  - lib/httpx/punycode.rb
305
301
  - lib/httpx/request.rb
302
+ - lib/httpx/request/body.rb
306
303
  - lib/httpx/resolver.rb
307
304
  - lib/httpx/resolver/https.rb
308
305
  - lib/httpx/resolver/multi.rb
@@ -310,6 +307,8 @@ files:
310
307
  - lib/httpx/resolver/resolver.rb
311
308
  - lib/httpx/resolver/system.rb
312
309
  - lib/httpx/response.rb
310
+ - lib/httpx/response/body.rb
311
+ - lib/httpx/response/buffer.rb
313
312
  - lib/httpx/selector.rb
314
313
  - lib/httpx/session.rb
315
314
  - lib/httpx/session2.rb
@@ -318,8 +317,17 @@ files:
318
317
  - lib/httpx/transcoder.rb
319
318
  - lib/httpx/transcoder/body.rb
320
319
  - lib/httpx/transcoder/chunker.rb
320
+ - lib/httpx/transcoder/deflate.rb
321
321
  - lib/httpx/transcoder/form.rb
322
+ - lib/httpx/transcoder/gzip.rb
322
323
  - lib/httpx/transcoder/json.rb
324
+ - lib/httpx/transcoder/multipart.rb
325
+ - lib/httpx/transcoder/multipart/decoder.rb
326
+ - lib/httpx/transcoder/multipart/encoder.rb
327
+ - lib/httpx/transcoder/multipart/mime_type_detector.rb
328
+ - lib/httpx/transcoder/multipart/part.rb
329
+ - lib/httpx/transcoder/utils/body_reader.rb
330
+ - lib/httpx/transcoder/utils/deflater.rb
323
331
  - lib/httpx/transcoder/xml.rb
324
332
  - lib/httpx/utils.rb
325
333
  - lib/httpx/version.rb
@@ -341,29 +349,29 @@ files:
341
349
  - sig/loggable.rbs
342
350
  - sig/options.rbs
343
351
  - sig/parser/http1.rbs
344
- - sig/plugins/authentication.rbs
345
- - sig/plugins/authentication/basic.rbs
346
- - sig/plugins/authentication/digest.rbs
347
- - sig/plugins/authentication/ntlm.rbs
348
- - sig/plugins/authentication/socks5.rbs
352
+ - sig/plugins/auth.rbs
353
+ - sig/plugins/auth/basic.rbs
354
+ - sig/plugins/auth/digest.rbs
355
+ - sig/plugins/auth/ntlm.rbs
356
+ - sig/plugins/auth/socks5.rbs
349
357
  - sig/plugins/aws_sdk_authentication.rbs
350
358
  - sig/plugins/aws_sigv4.rbs
351
- - sig/plugins/basic_authentication.rbs
359
+ - sig/plugins/basic_auth.rbs
360
+ - sig/plugins/brotli.rbs
352
361
  - sig/plugins/circuit_breaker.rbs
353
362
  - sig/plugins/compression.rbs
354
- - sig/plugins/compression/brotli.rbs
355
- - sig/plugins/compression/deflate.rbs
356
- - sig/plugins/compression/gzip.rbs
357
363
  - sig/plugins/cookies.rbs
358
364
  - sig/plugins/cookies/cookie.rbs
359
365
  - sig/plugins/cookies/jar.rbs
360
- - sig/plugins/digest_authentication.rbs
366
+ - sig/plugins/digest_auth.rbs
361
367
  - sig/plugins/expect.rbs
362
368
  - sig/plugins/follow_redirects.rbs
363
369
  - sig/plugins/grpc.rbs
370
+ - sig/plugins/grpc/call.rbs
371
+ - sig/plugins/grpc/grpc_encoding.rbs
372
+ - sig/plugins/grpc/message.rbs
364
373
  - sig/plugins/h2c.rbs
365
- - sig/plugins/multipart.rbs
366
- - sig/plugins/ntlm_authentication.rbs
374
+ - sig/plugins/ntlm_auth.rbs
367
375
  - sig/plugins/oauth.rbs
368
376
  - sig/plugins/persistent.rbs
369
377
  - sig/plugins/proxy.rbs
@@ -379,6 +387,7 @@ files:
379
387
  - sig/plugins/upgrade.rbs
380
388
  - sig/pool.rbs
381
389
  - sig/request.rbs
390
+ - sig/request/body.rbs
382
391
  - sig/resolver.rbs
383
392
  - sig/resolver/https.rbs
384
393
  - sig/resolver/multi.rbs
@@ -386,14 +395,21 @@ files:
386
395
  - sig/resolver/resolver.rbs
387
396
  - sig/resolver/system.rbs
388
397
  - sig/response.rbs
398
+ - sig/response/body.rbs
399
+ - sig/response/buffer.rbs
389
400
  - sig/selector.rbs
390
401
  - sig/session.rbs
391
402
  - sig/timers.rbs
392
403
  - sig/transcoder.rbs
393
404
  - sig/transcoder/body.rbs
394
405
  - sig/transcoder/chunker.rbs
406
+ - sig/transcoder/deflate.rbs
395
407
  - sig/transcoder/form.rbs
408
+ - sig/transcoder/gzip.rbs
396
409
  - sig/transcoder/json.rbs
410
+ - sig/transcoder/multipart.rbs
411
+ - sig/transcoder/utils/body_reader.rbs
412
+ - sig/transcoder/utils/deflater.rbs
397
413
  - sig/transcoder/xml.rbs
398
414
  - sig/utils.rbs
399
415
  homepage: https://gitlab.com/os85/httpx
@@ -414,7 +430,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
414
430
  requirements:
415
431
  - - ">="
416
432
  - !ruby/object:Gem::Version
417
- version: '0'
433
+ version: 2.7.0
418
434
  required_rubygems_version: !ruby/object:Gem::Requirement
419
435
  requirements:
420
436
  - - ">="
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HTTPX
4
- module Plugins
5
- #
6
- # This plugin adds a shim +authentication+ method to the session, which will fill
7
- # the HTTP Authorization header.
8
- #
9
- # https://gitlab.com/os85/httpx/wikis/Authentication#authentication
10
- #
11
- module Authentication
12
- module InstanceMethods
13
- def authentication(token)
14
- with(headers: { "authorization" => token })
15
- end
16
-
17
- def bearer_auth(token)
18
- authentication("Bearer #{token}")
19
- end
20
- end
21
- end
22
- register_plugin :authentication, Authentication
23
- end
24
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HTTPX
4
- module Plugins
5
- module Compression
6
- module Brotli
7
- class << self
8
- def load_dependencies(klass)
9
- require "brotli"
10
- klass.plugin(:compression)
11
- end
12
-
13
- def extra_options(options)
14
- options.merge(encodings: options.encodings.merge("br" => self))
15
- end
16
- end
17
-
18
- module Deflater
19
- module_function
20
-
21
- def deflate(raw, buffer = "".b, chunk_size: 16_384)
22
- while (chunk = raw.read(chunk_size))
23
- compressed = ::Brotli.deflate(chunk)
24
- buffer << compressed
25
- yield compressed if block_given?
26
- end
27
- buffer
28
- end
29
- end
30
-
31
- class Inflater
32
- def initialize(bytesize)
33
- @bytesize = bytesize
34
- end
35
-
36
- def inflate(chunk)
37
- ::Brotli.inflate(chunk)
38
- end
39
- end
40
-
41
- module_function
42
-
43
- def deflater
44
- Deflater
45
- end
46
-
47
- def inflater(bytesize)
48
- Inflater.new(bytesize)
49
- end
50
- end
51
- end
52
- register_plugin :"compression/brotli", Compression::Brotli
53
- end
54
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HTTPX
4
- module Plugins
5
- module Compression
6
- module Deflate
7
- class << self
8
- def load_dependencies(_klass)
9
- require "stringio"
10
- require "zlib"
11
- end
12
-
13
- def configure(klass)
14
- klass.plugin(:"compression/gzip")
15
- end
16
-
17
- def extra_options(options)
18
- options.merge(encodings: options.encodings.merge("deflate" => self))
19
- end
20
- end
21
-
22
- module Deflater
23
- module_function
24
-
25
- def deflate(raw, buffer = "".b, chunk_size: 16_384)
26
- deflater = Zlib::Deflate.new
27
- while (chunk = raw.read(chunk_size))
28
- compressed = deflater.deflate(chunk)
29
- buffer << compressed
30
- yield compressed if block_given?
31
- end
32
- last = deflater.finish
33
- buffer << last
34
- yield last if block_given?
35
- buffer
36
- ensure
37
- deflater.close if deflater
38
- end
39
- end
40
-
41
- module_function
42
-
43
- def deflater
44
- Deflater
45
- end
46
-
47
- def inflater(bytesize)
48
- GZIP::Inflater.new(bytesize)
49
- end
50
- end
51
- end
52
- register_plugin :"compression/deflate", Compression::Deflate
53
- end
54
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "forwardable"
4
-
5
- module HTTPX
6
- module Plugins
7
- module Compression
8
- module GZIP
9
- class << self
10
- def load_dependencies(*)
11
- require "zlib"
12
- end
13
-
14
- def extra_options(options)
15
- options.merge(encodings: options.encodings.merge("gzip" => self))
16
- end
17
- end
18
-
19
- class Deflater
20
- def initialize
21
- @compressed_chunk = "".b
22
- end
23
-
24
- def deflate(raw, buffer = "".b, chunk_size: 16_384)
25
- gzip = Zlib::GzipWriter.new(self)
26
-
27
- begin
28
- while (chunk = raw.read(chunk_size))
29
- gzip.write(chunk)
30
- gzip.flush
31
- compressed = compressed_chunk
32
- buffer << compressed
33
- yield compressed if block_given?
34
- end
35
- ensure
36
- gzip.close
37
- end
38
-
39
- return unless (compressed = compressed_chunk)
40
-
41
- buffer << compressed
42
- yield compressed if block_given?
43
- buffer
44
- end
45
-
46
- private
47
-
48
- def write(chunk)
49
- @compressed_chunk << chunk
50
- end
51
-
52
- def compressed_chunk
53
- @compressed_chunk.dup
54
- ensure
55
- @compressed_chunk.clear
56
- end
57
- end
58
-
59
- class Inflater
60
- def initialize(bytesize)
61
- @inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
62
- @bytesize = bytesize
63
- @buffer = nil
64
- end
65
-
66
- def inflate(chunk)
67
- buffer = @inflater.inflate(chunk)
68
- @bytesize -= chunk.bytesize
69
- if @bytesize <= 0
70
- buffer << @inflater.finish
71
- @inflater.close
72
- end
73
- buffer
74
- end
75
- end
76
-
77
- module_function
78
-
79
- def deflater
80
- Deflater.new
81
- end
82
-
83
- def inflater(bytesize)
84
- Inflater.new(bytesize)
85
- end
86
- end
87
- end
88
- register_plugin :"compression/gzip", Compression::GZIP
89
- end
90
- end
@@ -1,165 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HTTPX
4
- module Plugins
5
- #
6
- # This plugin adds compression support. Namely it:
7
- #
8
- # * Compresses the request body when passed a supported "Content-Encoding" mime-type;
9
- # * Decompresses the response body from a supported "Content-Encoding" mime-type;
10
- #
11
- # It supports both *gzip* and *deflate*.
12
- #
13
- # https://gitlab.com/os85/httpx/wikis/Compression
14
- #
15
- module Compression
16
- class << self
17
- def configure(klass)
18
- klass.plugin(:"compression/gzip")
19
- klass.plugin(:"compression/deflate")
20
- end
21
-
22
- def extra_options(options)
23
- options.merge(encodings: {})
24
- end
25
- end
26
-
27
- module OptionsMethods
28
- def option_compression_threshold_size(value)
29
- bytes = Integer(value)
30
- raise TypeError, ":compression_threshold_size must be positive" unless bytes.positive?
31
-
32
- bytes
33
- end
34
-
35
- def option_encodings(value)
36
- raise TypeError, ":encodings must be an Hash" unless value.is_a?(Hash)
37
-
38
- value
39
- end
40
- end
41
-
42
- module RequestMethods
43
- def initialize(*)
44
- super
45
- # forego compression in the Range cases
46
- if @headers.key?("range")
47
- @headers.delete("accept-encoding")
48
- else
49
- @headers["accept-encoding"] ||= @options.encodings.keys
50
- end
51
- end
52
- end
53
-
54
- module RequestBodyMethods
55
- def initialize(*, options)
56
- super
57
- return if @body.nil?
58
-
59
- threshold = options.compression_threshold_size
60
- return if threshold && !unbounded_body? && @body.bytesize < threshold
61
-
62
- @headers.get("content-encoding").each do |encoding|
63
- next if encoding == "identity"
64
-
65
- next unless options.encodings.key?(encoding)
66
-
67
- @body = Encoder.new(@body, options.encodings[encoding].deflater)
68
- end
69
- @headers["content-length"] = @body.bytesize unless unbounded_body?
70
- end
71
- end
72
-
73
- module ResponseBodyMethods
74
- using ArrayExtensions::FilterMap
75
-
76
- attr_reader :encodings
77
-
78
- def initialize(*)
79
- @encodings = []
80
-
81
- super
82
-
83
- return unless @headers.key?("content-encoding")
84
-
85
- # remove encodings that we are able to decode
86
- @headers["content-encoding"] = @headers.get("content-encoding") - @encodings
87
-
88
- compressed_length = if @headers.key?("content-length")
89
- @headers["content-length"].to_i
90
- else
91
- Float::INFINITY
92
- end
93
-
94
- @_inflaters = @headers.get("content-encoding").filter_map do |encoding|
95
- next if encoding == "identity"
96
-
97
- next unless @options.encodings.key?(encoding)
98
-
99
- inflater = @options.encodings[encoding].inflater(compressed_length)
100
- # do not uncompress if there is no decoder available. In fact, we can't reliably
101
- # continue decompressing beyond that, so ignore.
102
- break unless inflater
103
-
104
- @encodings << encoding
105
- inflater
106
- end
107
-
108
- # this can happen if the only declared encoding is "identity"
109
- remove_instance_variable(:@_inflaters) if @_inflaters.empty?
110
- end
111
-
112
- def write(chunk)
113
- return super unless defined?(@_inflaters) && !chunk.empty?
114
-
115
- chunk = decompress(chunk)
116
- super(chunk)
117
- end
118
-
119
- private
120
-
121
- def decompress(buffer)
122
- @_inflaters.reverse_each do |inflater|
123
- buffer = inflater.inflate(buffer)
124
- end
125
- buffer
126
- end
127
- end
128
-
129
- class Encoder
130
- attr_reader :content_type
131
-
132
- def initialize(body, deflater)
133
- @content_type = body.content_type
134
- @body = body.respond_to?(:read) ? body : StringIO.new(body.to_s)
135
- @buffer = StringIO.new("".b, File::RDWR)
136
- @deflater = deflater
137
- end
138
-
139
- def each(&blk)
140
- return enum_for(__method__) unless blk
141
-
142
- return deflate(&blk) if @buffer.size.zero? # rubocop:disable Style/ZeroLengthPredicate
143
-
144
- @buffer.rewind
145
- @buffer.each(&blk)
146
- end
147
-
148
- def bytesize
149
- deflate
150
- @buffer.size
151
- end
152
-
153
- private
154
-
155
- def deflate(&blk)
156
- return unless @buffer.size.zero? # rubocop:disable Style/ZeroLengthPredicate
157
-
158
- @body.rewind
159
- @deflater.deflate(@body, @buffer, chunk_size: 16_384, &blk)
160
- end
161
- end
162
- end
163
- register_plugin :compression, Compression
164
- end
165
- end