httpx 0.24.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) 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/0_24_6.md +5 -0
  6. data/doc/release_notes/1_0_0.md +60 -0
  7. data/lib/httpx/adapters/datadog.rb +28 -97
  8. data/lib/httpx/adapters/faraday.rb +9 -52
  9. data/lib/httpx/adapters/webmock.rb +2 -7
  10. data/lib/httpx/altsvc.rb +4 -22
  11. data/lib/httpx/base64.rb +27 -0
  12. data/lib/httpx/chainable.rb +2 -25
  13. data/lib/httpx/connection.rb +11 -32
  14. data/lib/httpx/domain_name.rb +5 -12
  15. data/lib/httpx/errors.rb +0 -2
  16. data/lib/httpx/extensions.rb +0 -124
  17. data/lib/httpx/io/ssl.rb +26 -59
  18. data/lib/httpx/io/tcp.rb +27 -68
  19. data/lib/httpx/io/udp.rb +13 -48
  20. data/lib/httpx/io/unix.rb +1 -8
  21. data/lib/httpx/loggable.rb +4 -19
  22. data/lib/httpx/options.rb +24 -84
  23. data/lib/httpx/plugins/{authentication → auth}/basic.rb +1 -5
  24. data/lib/httpx/plugins/{authentication → auth}/digest.rb +2 -5
  25. data/lib/httpx/plugins/{authentication → auth}/ntlm.rb +1 -3
  26. data/lib/httpx/plugins/{authentication → auth}/socks5.rb +0 -2
  27. data/lib/httpx/plugins/auth.rb +25 -0
  28. data/lib/httpx/plugins/aws_sigv4.rb +0 -1
  29. data/lib/httpx/plugins/{basic_authentication.rb → basic_auth.rb} +5 -6
  30. data/lib/httpx/plugins/brotli.rb +50 -0
  31. data/lib/httpx/plugins/circuit_breaker/circuit.rb +40 -16
  32. data/lib/httpx/plugins/circuit_breaker/circuit_store.rb +16 -5
  33. data/lib/httpx/plugins/circuit_breaker.rb +11 -4
  34. data/lib/httpx/plugins/cookies/set_cookie_parser.rb +0 -2
  35. data/lib/httpx/plugins/cookies.rb +1 -1
  36. data/lib/httpx/plugins/{digest_authentication.rb → digest_auth.rb} +5 -5
  37. data/lib/httpx/plugins/follow_redirects.rb +21 -24
  38. data/lib/httpx/plugins/grpc/grpc_encoding.rb +82 -0
  39. data/lib/httpx/plugins/grpc/message.rb +7 -39
  40. data/lib/httpx/plugins/grpc.rb +15 -21
  41. data/lib/httpx/plugins/h2c.rb +0 -1
  42. data/lib/httpx/plugins/{ntlm_authentication.rb → ntlm_auth.rb} +5 -5
  43. data/lib/httpx/plugins/oauth.rb +2 -2
  44. data/lib/httpx/plugins/proxy/http.rb +0 -2
  45. data/lib/httpx/plugins/proxy/socks4.rb +0 -4
  46. data/lib/httpx/plugins/proxy/socks5.rb +1 -5
  47. data/lib/httpx/plugins/proxy.rb +3 -32
  48. data/lib/httpx/plugins/retries.rb +3 -4
  49. data/lib/httpx/plugins/stream.rb +4 -6
  50. data/lib/httpx/punycode.rb +9 -291
  51. data/lib/httpx/request/body.rb +145 -0
  52. data/lib/httpx/request.rb +2 -119
  53. data/lib/httpx/resolver/https.rb +1 -1
  54. data/lib/httpx/resolver/native.rb +6 -14
  55. data/lib/httpx/resolver/resolver.rb +1 -1
  56. data/lib/httpx/resolver/system.rb +11 -9
  57. data/lib/httpx/response/body.rb +206 -0
  58. data/lib/httpx/response/buffer.rb +90 -0
  59. data/lib/httpx/response.rb +5 -208
  60. data/lib/httpx/selector.rb +0 -2
  61. data/lib/httpx/session.rb +3 -10
  62. data/lib/httpx/transcoder/body.rb +0 -1
  63. data/lib/httpx/transcoder/deflate.rb +37 -0
  64. data/lib/httpx/transcoder/form.rb +52 -32
  65. data/lib/httpx/transcoder/gzip.rb +74 -0
  66. data/lib/httpx/transcoder/json.rb +2 -4
  67. data/lib/httpx/transcoder/multipart/decoder.rb +139 -0
  68. data/lib/httpx/{plugins → transcoder}/multipart/encoder.rb +3 -3
  69. data/lib/httpx/{plugins → transcoder}/multipart/mime_type_detector.rb +1 -1
  70. data/lib/httpx/{plugins → transcoder}/multipart/part.rb +3 -2
  71. data/lib/httpx/transcoder/multipart.rb +17 -0
  72. data/lib/httpx/transcoder/utils/body_reader.rb +46 -0
  73. data/lib/httpx/transcoder/utils/deflater.rb +72 -0
  74. data/lib/httpx/transcoder/xml.rb +0 -2
  75. data/lib/httpx/transcoder.rb +2 -2
  76. data/lib/httpx/utils.rb +10 -20
  77. data/lib/httpx/version.rb +1 -1
  78. data/lib/httpx.rb +0 -8
  79. data/sig/chainable.rbs +5 -6
  80. data/sig/connection.rbs +0 -1
  81. data/sig/errors.rbs +0 -3
  82. data/sig/httpx.rbs +2 -1
  83. data/sig/io/unix.rbs +1 -1
  84. data/sig/options.rbs +12 -8
  85. data/sig/plugins/{authentication → auth}/basic.rbs +0 -2
  86. data/sig/plugins/auth.rbs +13 -0
  87. data/sig/plugins/{basic_authentication.rbs → basic_auth.rbs} +2 -2
  88. data/sig/plugins/brotli.rbs +22 -0
  89. data/sig/plugins/circuit_breaker.rbs +7 -3
  90. data/sig/plugins/compression.rbs +0 -2
  91. data/sig/plugins/{digest_authentication.rbs → digest_auth.rbs} +2 -2
  92. data/sig/plugins/follow_redirects.rbs +0 -1
  93. data/sig/plugins/grpc/call.rbs +19 -0
  94. data/sig/plugins/grpc/grpc_encoding.rbs +33 -0
  95. data/sig/plugins/grpc/message.rbs +17 -0
  96. data/sig/plugins/grpc.rbs +2 -32
  97. data/sig/plugins/{ntlm_authentication.rbs → ntlm_auth.rbs} +2 -2
  98. data/sig/plugins/oauth.rbs +1 -1
  99. data/sig/plugins/proxy/socks4.rbs +2 -3
  100. data/sig/plugins/proxy/socks5.rbs +0 -1
  101. data/sig/plugins/proxy/ssh.rbs +1 -1
  102. data/sig/plugins/response_cache.rbs +5 -2
  103. data/sig/request/body.rbs +42 -0
  104. data/sig/request.rbs +1 -27
  105. data/sig/resolver/resolver.rbs +1 -1
  106. data/sig/response/body.rbs +52 -0
  107. data/sig/response/buffer.rbs +24 -0
  108. data/sig/response.rbs +0 -39
  109. data/sig/session.rbs +2 -0
  110. data/sig/transcoder/body.rbs +4 -3
  111. data/sig/transcoder/deflate.rbs +11 -0
  112. data/sig/transcoder/form.rbs +5 -3
  113. data/sig/transcoder/gzip.rbs +24 -0
  114. data/sig/transcoder/json.rbs +4 -2
  115. data/sig/{plugins → transcoder}/multipart.rbs +3 -10
  116. data/sig/transcoder/utils/body_reader.rbs +15 -0
  117. data/sig/transcoder/utils/deflater.rbs +29 -0
  118. data/sig/transcoder.rbs +18 -2
  119. metadata +52 -34
  120. data/lib/httpx/plugins/authentication.rb +0 -24
  121. data/lib/httpx/plugins/compression/brotli.rb +0 -54
  122. data/lib/httpx/plugins/compression/deflate.rb +0 -54
  123. data/lib/httpx/plugins/compression/gzip.rb +0 -90
  124. data/lib/httpx/plugins/compression.rb +0 -165
  125. data/lib/httpx/plugins/multipart/decoder.rb +0 -137
  126. data/lib/httpx/plugins/multipart.rb +0 -96
  127. data/sig/plugins/authentication.rbs +0 -13
  128. data/sig/plugins/compression/brotli.rbs +0 -21
  129. data/sig/plugins/compression/deflate.rbs +0 -17
  130. data/sig/plugins/compression/gzip.rbs +0 -29
  131. /data/sig/plugins/{authentication → auth}/digest.rbs +0 -0
  132. /data/sig/plugins/{authentication → auth}/ntlm.rbs +0 -0
  133. /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.5
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-17 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
@@ -105,6 +105,7 @@ extra_rdoc_files:
105
105
  - doc/release_notes/0_24_3.md
106
106
  - doc/release_notes/0_24_4.md
107
107
  - doc/release_notes/0_24_5.md
108
+ - doc/release_notes/0_24_6.md
108
109
  - doc/release_notes/0_2_0.md
109
110
  - doc/release_notes/0_2_1.md
110
111
  - doc/release_notes/0_3_0.md
@@ -126,6 +127,7 @@ extra_rdoc_files:
126
127
  - doc/release_notes/0_8_1.md
127
128
  - doc/release_notes/0_8_2.md
128
129
  - doc/release_notes/0_9_0.md
130
+ - doc/release_notes/1_0_0.md
129
131
  files:
130
132
  - LICENSE.txt
131
133
  - README.md
@@ -202,6 +204,7 @@ files:
202
204
  - doc/release_notes/0_24_3.md
203
205
  - doc/release_notes/0_24_4.md
204
206
  - doc/release_notes/0_24_5.md
207
+ - doc/release_notes/0_24_6.md
205
208
  - doc/release_notes/0_2_0.md
206
209
  - doc/release_notes/0_2_1.md
207
210
  - doc/release_notes/0_3_0.md
@@ -223,12 +226,14 @@ files:
223
226
  - doc/release_notes/0_8_1.md
224
227
  - doc/release_notes/0_8_2.md
225
228
  - doc/release_notes/0_9_0.md
229
+ - doc/release_notes/1_0_0.md
226
230
  - lib/httpx.rb
227
231
  - lib/httpx/adapters/datadog.rb
228
232
  - lib/httpx/adapters/faraday.rb
229
233
  - lib/httpx/adapters/sentry.rb
230
234
  - lib/httpx/adapters/webmock.rb
231
235
  - lib/httpx/altsvc.rb
236
+ - lib/httpx/base64.rb
232
237
  - lib/httpx/buffer.rb
233
238
  - lib/httpx/callbacks.rb
234
239
  - lib/httpx/chainable.rb
@@ -247,39 +252,32 @@ files:
247
252
  - lib/httpx/loggable.rb
248
253
  - lib/httpx/options.rb
249
254
  - lib/httpx/parser/http1.rb
250
- - lib/httpx/plugins/authentication.rb
251
- - lib/httpx/plugins/authentication/basic.rb
252
- - lib/httpx/plugins/authentication/digest.rb
253
- - lib/httpx/plugins/authentication/ntlm.rb
254
- - 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
255
260
  - lib/httpx/plugins/aws_sdk_authentication.rb
256
261
  - lib/httpx/plugins/aws_sigv4.rb
257
- - lib/httpx/plugins/basic_authentication.rb
262
+ - lib/httpx/plugins/basic_auth.rb
263
+ - lib/httpx/plugins/brotli.rb
258
264
  - lib/httpx/plugins/circuit_breaker.rb
259
265
  - lib/httpx/plugins/circuit_breaker/circuit.rb
260
266
  - lib/httpx/plugins/circuit_breaker/circuit_store.rb
261
- - lib/httpx/plugins/compression.rb
262
- - lib/httpx/plugins/compression/brotli.rb
263
- - lib/httpx/plugins/compression/deflate.rb
264
- - lib/httpx/plugins/compression/gzip.rb
265
267
  - lib/httpx/plugins/cookies.rb
266
268
  - lib/httpx/plugins/cookies/cookie.rb
267
269
  - lib/httpx/plugins/cookies/jar.rb
268
270
  - lib/httpx/plugins/cookies/set_cookie_parser.rb
269
- - lib/httpx/plugins/digest_authentication.rb
271
+ - lib/httpx/plugins/digest_auth.rb
270
272
  - lib/httpx/plugins/expect.rb
271
273
  - lib/httpx/plugins/follow_redirects.rb
272
274
  - lib/httpx/plugins/grpc.rb
273
275
  - lib/httpx/plugins/grpc/call.rb
276
+ - lib/httpx/plugins/grpc/grpc_encoding.rb
274
277
  - lib/httpx/plugins/grpc/message.rb
275
278
  - lib/httpx/plugins/h2c.rb
276
279
  - lib/httpx/plugins/internal_telemetry.rb
277
- - lib/httpx/plugins/multipart.rb
278
- - lib/httpx/plugins/multipart/decoder.rb
279
- - lib/httpx/plugins/multipart/encoder.rb
280
- - lib/httpx/plugins/multipart/mime_type_detector.rb
281
- - lib/httpx/plugins/multipart/part.rb
282
- - lib/httpx/plugins/ntlm_authentication.rb
280
+ - lib/httpx/plugins/ntlm_auth.rb
283
281
  - lib/httpx/plugins/oauth.rb
284
282
  - lib/httpx/plugins/persistent.rb
285
283
  - lib/httpx/plugins/proxy.rb
@@ -301,6 +299,7 @@ files:
301
299
  - lib/httpx/pool.rb
302
300
  - lib/httpx/punycode.rb
303
301
  - lib/httpx/request.rb
302
+ - lib/httpx/request/body.rb
304
303
  - lib/httpx/resolver.rb
305
304
  - lib/httpx/resolver/https.rb
306
305
  - lib/httpx/resolver/multi.rb
@@ -308,6 +307,8 @@ files:
308
307
  - lib/httpx/resolver/resolver.rb
309
308
  - lib/httpx/resolver/system.rb
310
309
  - lib/httpx/response.rb
310
+ - lib/httpx/response/body.rb
311
+ - lib/httpx/response/buffer.rb
311
312
  - lib/httpx/selector.rb
312
313
  - lib/httpx/session.rb
313
314
  - lib/httpx/session2.rb
@@ -316,8 +317,17 @@ files:
316
317
  - lib/httpx/transcoder.rb
317
318
  - lib/httpx/transcoder/body.rb
318
319
  - lib/httpx/transcoder/chunker.rb
320
+ - lib/httpx/transcoder/deflate.rb
319
321
  - lib/httpx/transcoder/form.rb
322
+ - lib/httpx/transcoder/gzip.rb
320
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
321
331
  - lib/httpx/transcoder/xml.rb
322
332
  - lib/httpx/utils.rb
323
333
  - lib/httpx/version.rb
@@ -339,29 +349,29 @@ files:
339
349
  - sig/loggable.rbs
340
350
  - sig/options.rbs
341
351
  - sig/parser/http1.rbs
342
- - sig/plugins/authentication.rbs
343
- - sig/plugins/authentication/basic.rbs
344
- - sig/plugins/authentication/digest.rbs
345
- - sig/plugins/authentication/ntlm.rbs
346
- - 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
347
357
  - sig/plugins/aws_sdk_authentication.rbs
348
358
  - sig/plugins/aws_sigv4.rbs
349
- - sig/plugins/basic_authentication.rbs
359
+ - sig/plugins/basic_auth.rbs
360
+ - sig/plugins/brotli.rbs
350
361
  - sig/plugins/circuit_breaker.rbs
351
362
  - sig/plugins/compression.rbs
352
- - sig/plugins/compression/brotli.rbs
353
- - sig/plugins/compression/deflate.rbs
354
- - sig/plugins/compression/gzip.rbs
355
363
  - sig/plugins/cookies.rbs
356
364
  - sig/plugins/cookies/cookie.rbs
357
365
  - sig/plugins/cookies/jar.rbs
358
- - sig/plugins/digest_authentication.rbs
366
+ - sig/plugins/digest_auth.rbs
359
367
  - sig/plugins/expect.rbs
360
368
  - sig/plugins/follow_redirects.rbs
361
369
  - sig/plugins/grpc.rbs
370
+ - sig/plugins/grpc/call.rbs
371
+ - sig/plugins/grpc/grpc_encoding.rbs
372
+ - sig/plugins/grpc/message.rbs
362
373
  - sig/plugins/h2c.rbs
363
- - sig/plugins/multipart.rbs
364
- - sig/plugins/ntlm_authentication.rbs
374
+ - sig/plugins/ntlm_auth.rbs
365
375
  - sig/plugins/oauth.rbs
366
376
  - sig/plugins/persistent.rbs
367
377
  - sig/plugins/proxy.rbs
@@ -377,6 +387,7 @@ files:
377
387
  - sig/plugins/upgrade.rbs
378
388
  - sig/pool.rbs
379
389
  - sig/request.rbs
390
+ - sig/request/body.rbs
380
391
  - sig/resolver.rbs
381
392
  - sig/resolver/https.rbs
382
393
  - sig/resolver/multi.rbs
@@ -384,14 +395,21 @@ files:
384
395
  - sig/resolver/resolver.rbs
385
396
  - sig/resolver/system.rbs
386
397
  - sig/response.rbs
398
+ - sig/response/body.rbs
399
+ - sig/response/buffer.rbs
387
400
  - sig/selector.rbs
388
401
  - sig/session.rbs
389
402
  - sig/timers.rbs
390
403
  - sig/transcoder.rbs
391
404
  - sig/transcoder/body.rbs
392
405
  - sig/transcoder/chunker.rbs
406
+ - sig/transcoder/deflate.rbs
393
407
  - sig/transcoder/form.rbs
408
+ - sig/transcoder/gzip.rbs
394
409
  - sig/transcoder/json.rbs
410
+ - sig/transcoder/multipart.rbs
411
+ - sig/transcoder/utils/body_reader.rbs
412
+ - sig/transcoder/utils/deflater.rbs
395
413
  - sig/transcoder/xml.rbs
396
414
  - sig/utils.rbs
397
415
  homepage: https://gitlab.com/os85/httpx
@@ -412,7 +430,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
412
430
  requirements:
413
431
  - - ">="
414
432
  - !ruby/object:Gem::Version
415
- version: '0'
433
+ version: 2.7.0
416
434
  required_rubygems_version: !ruby/object:Gem::Requirement
417
435
  requirements:
418
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