twingly-http 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8fd022cb30b4e6aea665f8e8bc569a7fa7023c78f1a0cc88b18c4f00793b23ac
4
- data.tar.gz: 498f19467946922913480c55637baea6871d6895ab2a51ed82d7e6cd6fd4b60c
3
+ metadata.gz: 62b69e5cac842eeefbaad91288707eb474458da8df5074521652fccd486edc89
4
+ data.tar.gz: 33c81395fbfd3f1588452fb93b66a067c40521654042f0eb6f04acd8a362b0c9
5
5
  SHA512:
6
- metadata.gz: 8658da71b5b0a3d327f81cf8f4c29014606ca3336b2ed3848b234498511fde9152953e841ca920818b2a1daae9024912ce5987b518b0be7204bdf61071a8b115
7
- data.tar.gz: a397277ad9301bae0b25589e0aeea82beaa11e57a8c228a6d3d8bcc276010b181721b184c230d29fbb41a592c74905a411be68d717ee04e5049a8cd4e6534097
6
+ metadata.gz: 3a95ad89c7bf1af645cb000bb739e8100048a268185e88d75ac307581f0832e85586f34fa1a4bc9f1f7291ca2147a2b9f61d032d562662a3f5a84aaca8750f3e
7
+ data.tar.gz: 4b79a8ea8a109fe5cbf0e57c0591e4a5382b10ba5bb5a05ada0ca96ed0197488fcf1e9bce47d0e1f61eb8d1ec016d5f1afec1338131a3b7b84388e85f35d2bf7
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module ResponseBodySizeLimit
5
+ class LimitExceededError < StandardError; end
6
+
7
+ class Middleware < Faraday::Middleware
8
+ def initialize(app, max_size_bytes:)
9
+ super(app)
10
+
11
+ @max_size_bytes = max_size_bytes
12
+ end
13
+
14
+ def call(env) # rubocop:disable Metrics/MethodLength
15
+ response_body_size = 0
16
+ accumulated_body = + ""
17
+
18
+ env.request.on_data = proc do |chunk, _|
19
+ response_body_size += chunk.bytesize
20
+ accumulated_body << chunk
21
+
22
+ if response_body_size > @max_size_bytes
23
+ raise LimitExceededError,
24
+ "Response body too large, exceeced the configured max size of #{@max_size_bytes} bytes."
25
+ end
26
+ end
27
+
28
+ @app.call(env).on_complete do |response_env|
29
+ response_env.body = accumulated_body
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Faraday::Request.register_middleware(
37
+ response_body_size_limit: Faraday::ResponseBodySizeLimit::Middleware
38
+ )
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "response_body_size_limit/middleware"
4
+
5
+ module Faraday
6
+ module ResponseBodySizeLimit
7
+ end
8
+ end
data/lib/twingly/http.rb CHANGED
@@ -7,6 +7,7 @@ require "faraday_middleware"
7
7
 
8
8
  require_relative "../faraday/logfmt_logger"
9
9
  require_relative "../faraday/url_size_limit"
10
+ require_relative "../faraday/response_body_size_limit"
10
11
  require_relative "heroku"
11
12
  require_relative "string_utilities"
12
13
 
@@ -15,6 +16,7 @@ module Twingly
15
16
  class ConnectionError < StandardError; end
16
17
  class UrlSizeLimitExceededError < StandardError; end
17
18
  class RedirectLimitReachedError < StandardError; end
19
+ class ResponseBodySizeLimitExceededError < StandardError; end
18
20
  class Client # rubocop:disable Metrics/ClassLength
19
21
  DEFAULT_RETRYABLE_EXCEPTIONS = [
20
22
  Faraday::ConnectionFailed,
@@ -31,6 +33,7 @@ module Twingly
31
33
  DEFAULT_NUMBER_OF_RETRIES = 0
32
34
  DEFAULT_RETRY_INTERVAL = 1
33
35
  DEFAULT_MAX_URL_SIZE_BYTES = Float::INFINITY
36
+ DEFAULT_MAX_RESPONSE_BODY_SIZE_BYTES = Float::INFINITY
34
37
  DEFAULT_FOLLOW_REDIRECTS_LIMIT = 3
35
38
 
36
39
  attr_writer :http_timeout
@@ -39,6 +42,7 @@ module Twingly
39
42
  attr_writer :retry_interval
40
43
  attr_writer :on_retry_callback
41
44
  attr_writer :max_url_size_bytes
45
+ attr_writer :max_response_body_size_bytes
42
46
  attr_writer :request_id
43
47
  attr_writer :follow_redirects
44
48
 
@@ -81,19 +85,20 @@ module Twingly
81
85
  end
82
86
 
83
87
  def initialize_defaults
84
- @request_id = nil
85
- @http_timeout = DEFAULT_HTTP_TIMEOUT
86
- @http_open_timeout = DEFAULT_HTTP_OPEN_TIMEOUT
87
- @retryable_exceptions = DEFAULT_RETRYABLE_EXCEPTIONS
88
- @number_of_retries = DEFAULT_NUMBER_OF_RETRIES
89
- @retry_interval = DEFAULT_RETRY_INTERVAL
90
- @on_retry_callback = nil
91
- @follow_redirects = false
92
- @follow_redirects_limit = DEFAULT_FOLLOW_REDIRECTS_LIMIT
93
- @max_url_size_bytes = DEFAULT_MAX_URL_SIZE_BYTES
88
+ @request_id = nil
89
+ @http_timeout = DEFAULT_HTTP_TIMEOUT
90
+ @http_open_timeout = DEFAULT_HTTP_OPEN_TIMEOUT
91
+ @retryable_exceptions = DEFAULT_RETRYABLE_EXCEPTIONS
92
+ @number_of_retries = DEFAULT_NUMBER_OF_RETRIES
93
+ @retry_interval = DEFAULT_RETRY_INTERVAL
94
+ @on_retry_callback = nil
95
+ @follow_redirects = false
96
+ @follow_redirects_limit = DEFAULT_FOLLOW_REDIRECTS_LIMIT
97
+ @max_url_size_bytes = DEFAULT_MAX_URL_SIZE_BYTES
98
+ @max_response_body_size_bytes = DEFAULT_MAX_RESPONSE_BODY_SIZE_BYTES
94
99
  end
95
100
 
96
- def http_response_for(method, **args)
101
+ def http_response_for(method, **args) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
97
102
  response = send("http_#{method}_response", **args)
98
103
 
99
104
  Response.new(headers: response.headers.to_h,
@@ -106,6 +111,8 @@ module Twingly
106
111
  raise UrlSizeLimitExceededError, error.message
107
112
  rescue FaradayMiddleware::RedirectLimitReached => error
108
113
  raise RedirectLimitReachedError, error.message
114
+ rescue Faraday::ResponseBodySizeLimit::LimitExceededError => error
115
+ raise ResponseBodySizeLimitExceededError, error.message
109
116
  end
110
117
 
111
118
  def http_get_response(url:, params:, headers:)
@@ -193,6 +200,8 @@ module Twingly
193
200
  exceptions: @retryable_exceptions,
194
201
  methods: [], # empty [] forces Faraday to run retry_if
195
202
  retry_if: retry_if
203
+ faraday.request :response_body_size_limit,
204
+ max_size_bytes: @max_response_body_size_bytes
196
205
  faraday.response :logfmt_logger, @logger.dup,
197
206
  headers: true,
198
207
  bodies: true,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Twingly
4
4
  module HTTP
5
- VERSION = "0.6.0"
5
+ VERSION = "0.7.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twingly-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Twingly AB
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-06 00:00:00.000000000 Z
11
+ date: 2024-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -56,6 +56,8 @@ files:
56
56
  - lib/faraday/logfmt_logger.rb
57
57
  - lib/faraday/logfmt_logger/LICENSE.md
58
58
  - lib/faraday/logfmt_logger/middleware.rb
59
+ - lib/faraday/response_body_size_limit.rb
60
+ - lib/faraday/response_body_size_limit/middleware.rb
59
61
  - lib/faraday/url_size_limit.rb
60
62
  - lib/faraday/url_size_limit/middleware.rb
61
63
  - lib/twingly/heroku.rb
@@ -82,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
84
  - !ruby/object:Gem::Version
83
85
  version: '0'
84
86
  requirements: []
85
- rubygems_version: 3.4.10
87
+ rubygems_version: 3.5.4
86
88
  signing_key:
87
89
  specification_version: 4
88
90
  summary: Robust HTTP client