Compare commits

..

No commits in common. "e4338979a6be90ff213072774e921d2267db14b3" and "b9ee892b2092b7dbeb56045d971ba6fb5df5cbba" have entirely different histories.

12 changed files with 28 additions and 130 deletions

View File

@ -38,8 +38,4 @@ Naming/AccessorMethodName:
Enabled: false
Performance/MethodObjectAsBlock:
Enabled: false
# TODO: remove after https://github.com/rubocop/rubocop/issues/11980 is fixed
Style/RedundantCurrentDirectoryInPath:
Enabled: false

View File

@ -24,7 +24,7 @@ require "httpx"
uri = "https://google.com"
responses = HTTPX.get(uri, uri)
responses = HTTPX.new(uri, uri)
# OR
HTTPX.wrap do |client|
@ -37,7 +37,7 @@ end
## Headers
```ruby
HTTPX.with(headers: {"user-agent" => "My Ruby Script"}).get("https://google.com")
HTTPX.headers("user-agent" => "My Ruby Script").get("https://google.com")
```
## HTTP Methods
@ -61,7 +61,7 @@ response = HTTPX.plugin(:basic_authentication).basic_authentication("username",
# Digest Auth
response = HTTPX.plugin(:digest_authentication).digest_authentication("username", "password").get("https://google.com")
# Bearer Token Auth
# Token Auth
response = HTTPX.plugin(:authentication).authentication("eyrandomtoken").get("https://google.com")
```
@ -74,11 +74,11 @@ require "httpx"
response = HTTPX.get("https://google.com/")
response.status # => 301
response.headers["location"] #=> "https://www.google.com/"
response.headers["cache-control"] #=> public, max-age=2592000
response.body.to_s #=> "<HTML><HEAD><meta http-equiv=\"content-type\" ....
response.body # => "<HTML><HEAD><meta http-equiv=\"content-type\" ....
response["cache-control"] # => public, max-age=2592000
```
## POST `application/x-www-form-urlencoded` request
## POST form request
```ruby
require "httpx"
@ -88,13 +88,16 @@ uri = URI.parse("http://example.com/search")
response = HTTPX.post(uri, form: {"q" => "My query", "per_page" => "50"})
```
## File `multipart/form-data` upload - input type="file" style
## File upload - input type="file" style
```ruby
require "httpx"
# uses http_form_data API: https://github.com/httprb/form_data
path = "/path/to/your/testfile.txt"
HTTPX.plugin(:multipart).post("http://something.com/uploads", form: {
name: Pathname.new("/path/to/your/testfile.txt")
name: HTTP::FormData::File.new(path)
})
```
@ -129,7 +132,8 @@ require "httpx"
HTTPX.plugin(:cookies).wrap do |client|
session_response = client.get("https://translate.google.com/")
response = client.get("https://translate.google.com/#auto|en|Pardon")
response_cookies = session_response.cookie_jar
response = client.cookies(response_cookies).get("https://translate.google.com/#auto|en|Pardon")
puts response
end
```
@ -140,7 +144,7 @@ end
require "httpx"
response = HTTPX.plugin(:compression).get("https://www.google.com")
puts response.headers["content-encoding"] #=> "gzip"
puts response.headers["content-encoding"] #=> "gzip"
```
@ -179,9 +183,7 @@ HTTPX.with(resolver_class: :https, resolver_options: {uri: "https://9.9.9.9/dns-
```ruby
require "httpx"
HTTPX.plugin(:follow_redirects)
.with(follow_insecure_redirects: false, max_redirects: 4)
.get("https://www.google.com")
HTTPX.plugin(:follow_redirects).with(follow_insecure_redirects: false, max_redirects: 4).get("https://www.google.com")
```
## Timeouts
@ -189,12 +191,12 @@ HTTPX.plugin(:follow_redirects)
```ruby
require "httpx"
# full E2E request/response timeout, 10 sec to connect to peer
HTTPX.with(timeout: {connect_timeout: 10, request_timeout: 3}).get("https://google.com")
HTTPX.with(timeout: {connect_timeout: 10, operation_timeout: 3}).get("https://google.com")
```
## Retries
```ruby
require "httpx"
HTTPX.plugin(:retries).max_retries(5).get("https://www.google.com")
@ -212,3 +214,4 @@ HTTPX.get("https://google.com") #=> udp://10.0.1.2:53...
HTTPX.with(debug_level: 1, debug: $stderr).get("https://google.com")
```

View File

@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
"changelog_uri" => "https://os85.gitlab.io/httpx/#release-notes",
"documentation_uri" => "https://os85.gitlab.io/httpx/rdoc/",
"source_code_uri" => "https://gitlab.com/os85/httpx",
"homepage_uri" => "https://honeyryderchuck.gitlab.io/httpx/",
"homepage_uri" => "https://os85.gitlab.io/httpx/",
"rubygems_mfa_required" => "true",
}

View File

@ -160,7 +160,7 @@ module TRACING_MODULE # rubocop:disable Naming/ClassAndModuleCamelCase
module RequestMethods
def __datadog_enable_trace!
return if @__datadog_enable_trace
return super if @__datadog_enable_trace
RequestTracer.new(self).call
@__datadog_enable_trace = true

View File

@ -63,7 +63,7 @@ module HTTPX
# Normalizes a _domain_ using the Punycode algorithm as necessary.
# The result will be a downcased, ASCII-only string.
def normalize(domain)
domain = domain.chomp(DOT).unicode_normalize(:nfc) unless domain.ascii_only?
domain = domain.ascii_only? ? domain : domain.chomp(DOT).unicode_normalize(:nfc)
Punycode.encode_hostname(domain).downcase
end
end

View File

@ -25,10 +25,6 @@ module HTTPX
klass.plugin(:"proxy/socks5")
end
def extra_options(options)
options.merge(supported_proxy_protocols: [])
end
if URI::Generic.methods.include?(:use_proxy?)
def use_proxy?(*args)
URI::Generic.use_proxy?(*args)
@ -122,12 +118,6 @@ module HTTPX
def option_proxy(value)
value.is_a?(Parameters) ? value : Hash[value]
end
def option_supported_proxy_protocols(value)
raise TypeError, ":supported_proxy_protocols must be an Array" unless value.is_a?(Array)
value.map(&:to_s)
end
end
module InstanceMethods
@ -152,12 +142,8 @@ module HTTPX
next_proxy = @_proxy_uris.first
raise Error, "Failed to connect to proxy" unless next_proxy
next_proxy = URI(next_proxy)
raise Error,
"#{next_proxy.scheme}: unsupported proxy protocol" unless options.supported_proxy_protocols.include?(next_proxy.scheme)
if proxy.key?(:no_proxy)
next_proxy = URI(next_proxy)
no_proxy = proxy[:no_proxy]
no_proxy = no_proxy.join(",") if no_proxy.is_a?(Array)
@ -267,11 +253,10 @@ module HTTPX
end
def send(request)
return super unless (
@options.proxy && @state != :idle && connecting?
)
return super unless @options.proxy
return super unless connecting?
(@proxy_pending ||= []) << request
@pending << request
end
def connecting?
@ -309,12 +294,6 @@ module HTTPX
when :idle
transition(:connecting)
when :connected
if @proxy_pending
while (req = @proxy_pendind.shift)
send(req)
end
end
transition(:open)
end
end

View File

@ -6,12 +6,6 @@ module HTTPX
module Plugins
module Proxy
module HTTP
class << self
def extra_options(options)
options.merge(supported_proxy_protocols: options.supported_proxy_protocols + %w[http])
end
end
module InstanceMethods
def with_proxy_basic_auth(opts)
with(proxy: opts.merge(scheme: "basic"))

View File

@ -16,12 +16,6 @@ module HTTPX
Error = Socks4Error
class << self
def extra_options(options)
options.merge(supported_proxy_protocols: options.supported_proxy_protocols + PROTOCOLS)
end
end
module ConnectionMethods
def interests
if @state == :connecting

View File

@ -18,14 +18,8 @@ module HTTPX
Error = Socks5Error
class << self
def load_dependencies(*)
require_relative "../authentication/socks5"
end
def extra_options(options)
options.merge(supported_proxy_protocols: options.supported_proxy_protocols + %w[socks5])
end
def self.load_dependencies(*)
require_relative "../authentication/socks5"
end
module ConnectionMethods

View File

@ -9,13 +9,10 @@ module HTTPX
# redefine the default options static var, which needs to
# refresh options_class
options = proxy_session.class.default_options.to_hash
options.freeze
original_verbosity = $VERBOSE
$VERBOSE = nil
const_set(:Options, proxy_session.class.default_options.options_class)
options[:options_class] = Class.new(options[:options_class])
options.freeze
Options.send(:const_set, :DEFAULT_OPTIONS, options)
Session.instance_variable_set(:@default_options, Options.new(options))
$VERBOSE = original_verbosity
end

View File

@ -1,52 +0,0 @@
# frozen_string_literal: true
require "webrick"
require "webrick/httpproxy"
require "test_helper"
require "support/http_helpers"
require "support/proxy_helper"
require "support/minitest_extensions"
class Bug_0_24_1_Test < Minitest::Test
include HTTPHelpers
include ProxyHelper
Plugin = Module.new do
@requests = []
class << self
attr_accessor :requests
end
self::ConnectionMethods = Module.new do
def send(req)
Plugin.requests << req
super
end
end
end
def test_proxy_plugin_silencing_conn_send_based_plugin
start_test_servlet(WEBrick::HTTPProxyServer) do |server|
def server.origin
sock = listeners.first
_, sock, ip, _ = sock.addr
"http://#{ip}:#{sock}"
end
proxy_uri = server.origin
http = HTTPX.plugin(Plugin).plugin(:proxy).plugin(ProxyResponseDetector).with_proxy(uri: proxy_uri)
uri = build_uri("/get")
response = http.get(uri)
verify_status(response, 200)
assert response.proxied?
assert Plugin.requests.size == 1
end
end
private
def scheme
"http://"
end
end

View File

@ -28,13 +28,6 @@ class ProxyTest < Minitest::Test
end
end
def test_proxy_unsupported_scheme
ex = assert_raises(HTTPX::HTTPProxyError) do
HTTPX.plugin(:proxy).with_proxy(uri: "https://proxy:123").get("http://smth.com")
end
assert ex.message == "https: unsupported proxy protocol"
end
private
def parameters(uri: "http://proxy", **args)