mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
fixed all remaining rubocop fails
This commit is contained in:
parent
2b1ab8b6b6
commit
861d69c409
@ -12,6 +12,7 @@ AllCops:
|
||||
- 'examples/**/*'
|
||||
- '.bundle/**/*'
|
||||
- 'vendor/**/*'
|
||||
- 'lib/httpx/extensions.rb'
|
||||
|
||||
Metrics/LineLength:
|
||||
Max: 120
|
||||
|
@ -38,3 +38,9 @@ Style/HashSyntax:
|
||||
Style/NumericPredicate:
|
||||
Exclude:
|
||||
- lib/palanca/extensions/*.rb
|
||||
|
||||
Style/Documentation:
|
||||
Enabled: false
|
||||
|
||||
Naming/AccessorMethodName:
|
||||
Enabled: false
|
||||
|
3
Gemfile
3
Gemfile
@ -19,9 +19,6 @@ gem "certificate_authority", git: "https://github.com/cchandler/certificate_auth
|
||||
branch: "master",
|
||||
require: false
|
||||
|
||||
gem "simplecov"
|
||||
|
||||
gem "minitest", require: false
|
||||
gem "minitest-proveit", require: false
|
||||
gem "oga", require: false
|
||||
gem "rubocop", require: false
|
||||
|
@ -21,8 +21,6 @@ Gem::Specification.new do |gem|
|
||||
gem.require_paths = ["lib"]
|
||||
gem.version = HTTPX::VERSION
|
||||
|
||||
gem.required_ruby_version = ">= 2.1"
|
||||
|
||||
gem.add_runtime_dependency "http-2", ">= 0.8.4"
|
||||
gem.add_runtime_dependency "http-form_data", ">= 2.0.0", "< 3"
|
||||
gem.add_runtime_dependency "http_parser.rb", ">= 0.6.0"
|
||||
|
@ -18,6 +18,8 @@ require "httpx/response"
|
||||
require "httpx/chainable"
|
||||
require "httpx/client"
|
||||
|
||||
# Top-Level Namespace
|
||||
#
|
||||
module HTTPX
|
||||
# All plugins should be stored under this module/namespace. Can register and load
|
||||
# plugins.
|
||||
|
@ -79,7 +79,6 @@ module HTTPX
|
||||
case @state
|
||||
when :idle
|
||||
transition(:open)
|
||||
when :open
|
||||
end
|
||||
@io.to_io
|
||||
end
|
||||
@ -162,7 +161,7 @@ module HTTPX
|
||||
|
||||
def transition(nextstate)
|
||||
case nextstate
|
||||
when :idle
|
||||
# when :idle
|
||||
|
||||
when :open
|
||||
return if @state == :closed
|
||||
@ -171,7 +170,7 @@ module HTTPX
|
||||
send_pending
|
||||
when :closed
|
||||
return if @state == :idle
|
||||
if pr = @parser
|
||||
if (pr = @parser)
|
||||
pr.close
|
||||
@parser = nil
|
||||
end
|
||||
|
@ -38,7 +38,7 @@ module HTTPX
|
||||
dispatch if @has_response
|
||||
end
|
||||
|
||||
def send(request, retries: @retries, **)
|
||||
def send(request, **)
|
||||
if @requests.size >= @max_concurrent_requests
|
||||
@pending << request
|
||||
return
|
||||
@ -116,17 +116,16 @@ module HTTPX
|
||||
end
|
||||
close
|
||||
send(@pending.shift) unless @pending.empty?
|
||||
if response.headers["connection"] == "close"
|
||||
unless @requests.empty?
|
||||
@requests.map { |r| r.transition(:idle) }
|
||||
# server doesn't handle pipelining, and probably
|
||||
# doesn't support keep-alive. Fallback to send only
|
||||
# 1 keep alive request.
|
||||
@max_concurrent_requests = 1
|
||||
end
|
||||
log(2) { "connection: close" }
|
||||
emit(:close)
|
||||
return unless response.headers["connection"] == "close"
|
||||
unless @requests.empty?
|
||||
@requests.map { |r| r.transition(:idle) }
|
||||
# server doesn't handle pipelining, and probably
|
||||
# doesn't support keep-alive. Fallback to send only
|
||||
# 1 keep alive request.
|
||||
@max_concurrent_requests = 1
|
||||
end
|
||||
log(2) { "connection: close" }
|
||||
emit(:close)
|
||||
end
|
||||
|
||||
private
|
||||
@ -174,7 +173,7 @@ module HTTPX
|
||||
|
||||
def join_body(request)
|
||||
return if request.empty?
|
||||
while chunk = request.drain_body
|
||||
while (chunk = request.drain_body)
|
||||
log { "<- DATA: #{chunk.bytesize} bytes..." }
|
||||
log(2) { "<- #{chunk.inspect}" }
|
||||
@buffer << chunk
|
||||
|
@ -32,12 +32,12 @@ module HTTPX
|
||||
@connection << data
|
||||
end
|
||||
|
||||
def send(request, retries: @retries, **)
|
||||
def send(request, **)
|
||||
if @connection.active_stream_count >= @max_concurrent_requests
|
||||
@pending << request
|
||||
return
|
||||
end
|
||||
unless stream = @streams[request]
|
||||
unless (stream = @streams[request])
|
||||
stream = @connection.new_stream
|
||||
handle_stream(stream, request)
|
||||
@streams[request] = stream
|
||||
@ -164,7 +164,8 @@ module HTTPX
|
||||
end
|
||||
|
||||
def on_settings(*)
|
||||
@max_concurrent_requests = [@max_concurrent_requests, @connection.remote_settings[:settings_max_concurrent_streams]].min
|
||||
@max_concurrent_requests = [@max_concurrent_requests,
|
||||
@connection.remote_settings[:settings_max_concurrent_streams]].min
|
||||
end
|
||||
|
||||
def on_close(*)
|
||||
@ -208,6 +209,10 @@ module HTTPX
|
||||
emit(:promise, self, stream)
|
||||
end
|
||||
|
||||
def respond_to_missing?(meth, *args)
|
||||
@connection.respond_to?(meth, *args) || super
|
||||
end
|
||||
|
||||
def method_missing(meth, *args, &blk)
|
||||
if @connection.respond_to?(meth)
|
||||
@connection.__send__(meth, *args, &blk)
|
||||
|
@ -9,14 +9,13 @@ module HTTPX
|
||||
@options = self.class.default_options.merge(options)
|
||||
@connection = Connection.new(@options)
|
||||
@responses = {}
|
||||
if block_given?
|
||||
begin
|
||||
@keep_open = true
|
||||
yield self
|
||||
ensure
|
||||
@keep_open = false
|
||||
close
|
||||
end
|
||||
return unless block_given?
|
||||
begin
|
||||
@keep_open = true
|
||||
yield self
|
||||
ensure
|
||||
@keep_open = false
|
||||
close
|
||||
end
|
||||
end
|
||||
|
||||
@ -73,13 +72,13 @@ module HTTPX
|
||||
case args.size
|
||||
when 1
|
||||
reqs = args.first
|
||||
requests = reqs.map do |verb, uri|
|
||||
reqs.map do |verb, uri|
|
||||
__build_req(verb, uri, options)
|
||||
end
|
||||
when 2, 3
|
||||
verb, *uris = args
|
||||
if uris.respond_to?(:each)
|
||||
requests = uris.map do |uri|
|
||||
uris.map do |uri|
|
||||
__build_req(verb, uri, options)
|
||||
end
|
||||
else
|
||||
@ -101,7 +100,7 @@ module HTTPX
|
||||
loop do
|
||||
begin
|
||||
request = requests.first
|
||||
@connection.next_tick until response = fetch_response(request)
|
||||
@connection.next_tick until (response = fetch_response(request))
|
||||
|
||||
responses << response
|
||||
requests.shift
|
||||
@ -146,16 +145,17 @@ module HTTPX
|
||||
options_klass.__send__(:include, pl::OptionsMethods) if defined?(pl::OptionsMethods)
|
||||
@default_options = options_klass.new
|
||||
end
|
||||
default_options.request_class.__send__(:include, pl::RequestMethods) if defined?(pl::RequestMethods)
|
||||
default_options.request_class.extend(pl::RequestClassMethods) if defined?(pl::RequestClassMethods)
|
||||
default_options.response_class.__send__(:include, pl::ResponseMethods) if defined?(pl::ResponseMethods)
|
||||
default_options.response_class.extend(pl::ResponseClassMethods) if defined?(pl::ResponseClassMethods)
|
||||
default_options.headers_class.__send__(:include, pl::HeadersMethods) if defined?(pl::HeadersMethods)
|
||||
default_options.headers_class.extend(pl::HeadersClassMethods) if defined?(pl::HeadersClassMethods)
|
||||
default_options.request_body_class.__send__(:include, pl::RequestBodyMethods) if defined?(pl::RequestBodyMethods)
|
||||
default_options.request_body_class.extend(pl::RequestBodyClassMethods) if defined?(pl::RequestBodyClassMethods)
|
||||
default_options.response_body_class.__send__(:include, pl::ResponseBodyMethods) if defined?(pl::ResponseBodyMethods)
|
||||
default_options.response_body_class.extend(pl::ResponseBodyClassMethods) if defined?(pl::ResponseBodyClassMethods)
|
||||
opts = default_options
|
||||
opts.request_class.__send__(:include, pl::RequestMethods) if defined?(pl::RequestMethods)
|
||||
opts.request_class.extend(pl::RequestClassMethods) if defined?(pl::RequestClassMethods)
|
||||
opts.response_class.__send__(:include, pl::ResponseMethods) if defined?(pl::ResponseMethods)
|
||||
opts.response_class.extend(pl::ResponseClassMethods) if defined?(pl::ResponseClassMethods)
|
||||
opts.headers_class.__send__(:include, pl::HeadersMethods) if defined?(pl::HeadersMethods)
|
||||
opts.headers_class.extend(pl::HeadersClassMethods) if defined?(pl::HeadersClassMethods)
|
||||
opts.request_body_class.__send__(:include, pl::RequestBodyMethods) if defined?(pl::RequestBodyMethods)
|
||||
opts.request_body_class.extend(pl::RequestBodyClassMethods) if defined?(pl::RequestBodyClassMethods)
|
||||
opts.response_body_class.__send__(:include, pl::ResponseBodyMethods) if defined?(pl::ResponseBodyMethods)
|
||||
opts.response_body_class.extend(pl::ResponseBodyClassMethods) if defined?(pl::ResponseBodyClassMethods)
|
||||
pl.configure(self, *args, &block) if pl.respond_to?(:configure)
|
||||
end
|
||||
self
|
||||
|
@ -18,7 +18,7 @@ module HTTPX
|
||||
|
||||
def next_tick(timeout: @timeout.timeout)
|
||||
@selector.select(timeout) do |monitor|
|
||||
if channel = monitor.value
|
||||
if (channel = monitor.value)
|
||||
consume(channel)
|
||||
end
|
||||
end
|
||||
@ -31,7 +31,7 @@ module HTTPX
|
||||
@selector.deregister(channel)
|
||||
end
|
||||
else
|
||||
while ch = @channels.shift
|
||||
while (ch = @channels.shift)
|
||||
ch.close(true)
|
||||
@selector.deregister(ch)
|
||||
end
|
||||
|
@ -126,7 +126,7 @@ module HTTPX
|
||||
|
||||
def transition(nextstate)
|
||||
case nextstate
|
||||
when :idle
|
||||
# when :idle
|
||||
when :connected
|
||||
return unless @state == :idle
|
||||
when :closed
|
||||
@ -138,8 +138,11 @@ module HTTPX
|
||||
end
|
||||
|
||||
class SSL < TCP
|
||||
TLS_OPTIONS = OpenSSL::SSL::SSLContext.instance_methods.include?(:alpn_protocols) ?
|
||||
{ alpn_protocols: %w[h2 http/1.1] } : {}
|
||||
TLS_OPTIONS = if OpenSSL::SSL::SSLContext.instance_methods.include?(:alpn_protocols)
|
||||
{ alpn_protocols: %w[h2 http/1.1] }
|
||||
else
|
||||
{}
|
||||
end
|
||||
|
||||
def initialize(_, _, options)
|
||||
@ctx = OpenSSL::SSL::SSLContext.new
|
||||
|
@ -17,7 +17,7 @@ module HTTPX
|
||||
module_function
|
||||
|
||||
def deflate(raw, buffer, chunk_size:)
|
||||
while chunk = raw.read(chunk_size)
|
||||
while (chunk = raw.read(chunk_size))
|
||||
compressed = ::Brotli.deflate(chunk)
|
||||
buffer << compressed
|
||||
yield compressed if block_given?
|
||||
|
@ -21,7 +21,7 @@ module HTTPX
|
||||
Zlib::MAX_WBITS,
|
||||
Zlib::MAX_MEM_LEVEL,
|
||||
Zlib::HUFFMAN_ONLY)
|
||||
while chunk = raw.read(chunk_size)
|
||||
while (chunk = raw.read(chunk_size))
|
||||
compressed = deflater.deflate(chunk)
|
||||
buffer << compressed
|
||||
yield compressed if block_given?
|
||||
|
@ -18,7 +18,7 @@ module HTTPX
|
||||
def deflate(raw, buffer, chunk_size:)
|
||||
gzip = Zlib::GzipWriter.new(self)
|
||||
|
||||
while chunk = raw.read(chunk_size)
|
||||
while (chunk = raw.read(chunk_size))
|
||||
gzip.write(chunk)
|
||||
gzip.flush
|
||||
compressed = compressed_chunk
|
||||
|
@ -54,7 +54,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
module UpgradeExtensions
|
||||
def upgrade(request, _response, retries: @retries, **)
|
||||
def upgrade(request, _response, **)
|
||||
@connection.send_connection_preface
|
||||
# skip checks, it is assumed that this is the first
|
||||
# request in the connection
|
||||
|
@ -60,7 +60,7 @@ module HTTPX
|
||||
throw(:called)
|
||||
else
|
||||
pending = @parser.pending
|
||||
while req = pending.shift
|
||||
while (req = pending.shift)
|
||||
@on_response.call(req, response)
|
||||
end
|
||||
end
|
||||
|
@ -22,7 +22,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
def on_packet(packet)
|
||||
version, status, port, ip = packet.unpack("CCnN")
|
||||
_version, status, _port, _ip = packet.unpack("CCnN")
|
||||
if status == GRANTED
|
||||
req, _ = @pending.first
|
||||
request_uri = req.uri
|
||||
|
@ -76,7 +76,7 @@ module HTTPX
|
||||
def query
|
||||
return @query if defined?(@query)
|
||||
query = []
|
||||
if q = @options.params
|
||||
if (q = @options.params)
|
||||
query << URI.encode_www_form(q)
|
||||
end
|
||||
query << @uri.query if @uri.query
|
||||
@ -204,8 +204,6 @@ module HTTPX
|
||||
@response && @response.status == 100
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class ProcIO
|
||||
def initialize(block)
|
||||
@block = block
|
||||
|
@ -84,7 +84,7 @@ module HTTPX
|
||||
begin
|
||||
unless @state == :idle
|
||||
rewind
|
||||
while chunk = @buffer.read(@window_size)
|
||||
while (chunk = @buffer.read(@window_size))
|
||||
yield(chunk)
|
||||
end
|
||||
end
|
||||
|
@ -48,8 +48,6 @@ class ClientTest < Minitest::Test
|
||||
assert body.foo == "response-body-foo", "response body method is unexpected"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
TestPlugin = Module.new do
|
||||
self::ClassMethods = Module.new do
|
||||
def foo
|
||||
|
@ -8,21 +8,21 @@ module ProxyHelper
|
||||
private
|
||||
|
||||
def socks4_proxy
|
||||
ip, port, version, _ = socks_proxies_list.select do |_, _, version, https|
|
||||
ip, port, _, _ = socks_proxies_list.select do |_, _, version, https|
|
||||
version == "Socks4" && https
|
||||
end.sample
|
||||
"socks4://#{ip}:#{port}"
|
||||
end
|
||||
|
||||
def socks4a_proxy
|
||||
ip, port, version, _ = socks_proxies_list.select do |_, _, version, https|
|
||||
ip, port, _, _ = socks_proxies_list.select do |_, _, version, https|
|
||||
version == "Socks4" && https
|
||||
end.sample
|
||||
"socks4a://#{ip}:#{port}"
|
||||
end
|
||||
|
||||
def socks5_proxy
|
||||
ip, port, version, _ = socks_proxies_list.select do |_, _, version, https|
|
||||
ip, port, _, _ = socks_proxies_list.select do |_, _, version, https|
|
||||
version == "Socks5" && https
|
||||
end.sample
|
||||
"socks5://#{ip}:#{port}"
|
||||
|
@ -35,7 +35,7 @@ module Requests
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
compressed_data = body["data"]
|
||||
assert body["data"].bytesize < 8012, "body hasn't been compressed"
|
||||
assert compressed_data.bytesize < 8012, "body hasn't been compressed"
|
||||
end
|
||||
|
||||
def test_plugin_compression_deflate
|
||||
@ -56,7 +56,7 @@ module Requests
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
compressed_data = body["data"]
|
||||
assert body["data"].bytesize < 8012, "body hasn't been compressed"
|
||||
assert compressed_data.bytesize < 8012, "body hasn't been compressed"
|
||||
end
|
||||
|
||||
def test_plugin_compression_brotli
|
||||
@ -76,7 +76,7 @@ module Requests
|
||||
body = json_body(response)
|
||||
verify_header(body["headers"], "Content-Type", "application/octet-stream")
|
||||
compressed_data = body["data"]
|
||||
assert body["data"].bytesize < 8012, "body hasn't been compressed"
|
||||
assert compressed_data.bytesize < 8012, "body hasn't been compressed"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -21,15 +21,15 @@ module Requests
|
||||
def test_plugin_cookies_set
|
||||
client = HTTPX.plugin(:cookies)
|
||||
session_cookies = { "a" => "b", "c" => "d" }
|
||||
session_uri = set_cookies_uri(session_cookies)
|
||||
session_response = client.get(set_cookies_uri(session_cookies))
|
||||
session_uri = cookies_set_uri(session_cookies)
|
||||
session_response = client.get(cookies_set_uri(session_cookies))
|
||||
assert session_response.status == 302, "response should redirect"
|
||||
|
||||
assert !session_response.cookies.nil?, "there should be cookies in the response"
|
||||
response_cookies = session_response.cookie_jar
|
||||
assert !response_cookies.empty?
|
||||
response_cookies.cookies(session_uri).each do |cookie|
|
||||
assert session_cookies.one? { |k, v| k == cookie.name && v == cookie.value }
|
||||
assert(session_cookies.one? { |k, v| k == cookie.name && v == cookie.value })
|
||||
end
|
||||
|
||||
response = client.cookies(response_cookies).get(cookies_uri)
|
||||
@ -45,7 +45,7 @@ module Requests
|
||||
build_uri("/cookies")
|
||||
end
|
||||
|
||||
def set_cookies_uri(cookies)
|
||||
def cookies_set_uri(cookies)
|
||||
build_uri("/cookies/set?" + URI.encode_www_form(cookies))
|
||||
end
|
||||
end
|
||||
|
@ -12,8 +12,6 @@ if ENV.key?("PARALLEL")
|
||||
end
|
||||
end
|
||||
|
||||
$HTTPX_DEBUG = !!ENV["HTTPX_DEBUG"]
|
||||
|
||||
require "httpx"
|
||||
|
||||
Dir[File.join(".", "test", "support", "**", "*.rb")].each { |f| require f }
|
||||
|
Loading…
x
Reference in New Issue
Block a user