mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-07-27 00:00:45 -04:00
Compare commits
No commits in common. "330866f1024c7a53da1785cc10c41d4db4636231" and "b27f51c0f979cff3952cebcad924b781eb7d3fe4" have entirely different histories.
330866f102
...
b27f51c0f9
24
Gemfile
24
Gemfile
@ -16,7 +16,7 @@ group :test do
|
|||||||
gem "minitest"
|
gem "minitest"
|
||||||
gem "minitest-proveit"
|
gem "minitest-proveit"
|
||||||
gem "ruby-ntlm"
|
gem "ruby-ntlm"
|
||||||
gem "sentry-ruby" if RUBY_VERSION >= "2.4.0"
|
gem "sentry-ruby" if RUBY_VERSION >= "2.4"
|
||||||
gem "spy"
|
gem "spy"
|
||||||
if RUBY_VERSION < "2.3.0"
|
if RUBY_VERSION < "2.3.0"
|
||||||
gem "webmock", "< 3.15.0"
|
gem "webmock", "< 3.15.0"
|
||||||
@ -25,16 +25,16 @@ group :test do
|
|||||||
end
|
end
|
||||||
gem "websocket-driver"
|
gem "websocket-driver"
|
||||||
|
|
||||||
gem "net-ssh", "~> 4.2.0" if RUBY_VERSION < "2.2.0"
|
gem "net-ssh", "~> 4.2.0" if RUBY_VERSION < "2.2"
|
||||||
|
|
||||||
if RUBY_VERSION >= "2.3.0"
|
if RUBY_VERSION >= "2.3"
|
||||||
gem "ddtrace"
|
gem "ddtrace"
|
||||||
else
|
else
|
||||||
gem "ddtrace", "< 1.0.0"
|
gem "ddtrace", "< 1.0"
|
||||||
end
|
end
|
||||||
|
|
||||||
platform :mri do
|
platform :mri do
|
||||||
if RUBY_VERSION >= "2.3.0"
|
if RUBY_VERSION >= "2.3"
|
||||||
gem "google-protobuf", "< 3.19.2" if RUBY_VERSION < "2.5.0"
|
gem "google-protobuf", "< 3.19.2" if RUBY_VERSION < "2.5.0"
|
||||||
gem "grpc"
|
gem "grpc"
|
||||||
gem "logging"
|
gem "logging"
|
||||||
@ -42,12 +42,6 @@ group :test do
|
|||||||
gem "mimemagic", require: false
|
gem "mimemagic", require: false
|
||||||
gem "ruby-filemagic", require: false
|
gem "ruby-filemagic", require: false
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION >= "3.0.0"
|
|
||||||
gem "multi_json", require: false
|
|
||||||
gem "oj", require: false
|
|
||||||
gem "yajl-ruby", require: false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
platform :mri, :truffleruby do
|
platform :mri, :truffleruby do
|
||||||
@ -63,7 +57,7 @@ group :test do
|
|||||||
end
|
end
|
||||||
|
|
||||||
platform :mri_23 do
|
platform :mri_23 do
|
||||||
if RUBY_VERSION >= "2.3.0"
|
if RUBY_VERSION >= "2.3"
|
||||||
gem "openssl", "< 2.0.6" # force usage of openssl version we patch against
|
gem "openssl", "< 2.0.6" # force usage of openssl version we patch against
|
||||||
end
|
end
|
||||||
gem "msgpack", "<= 1.3.3"
|
gem "msgpack", "<= 1.3.3"
|
||||||
@ -89,7 +83,7 @@ group :test do
|
|||||||
end
|
end
|
||||||
|
|
||||||
group :coverage do
|
group :coverage do
|
||||||
if RUBY_VERSION < "2.2.0"
|
if RUBY_VERSION < "2.2"
|
||||||
gem "simplecov", "< 0.11.0"
|
gem "simplecov", "< 0.11.0"
|
||||||
elsif RUBY_VERSION < "2.3"
|
elsif RUBY_VERSION < "2.3"
|
||||||
gem "simplecov", "< 0.11.0"
|
gem "simplecov", "< 0.11.0"
|
||||||
@ -115,14 +109,14 @@ group :website do
|
|||||||
end if RUBY_VERSION > "2.4"
|
end if RUBY_VERSION > "2.4"
|
||||||
|
|
||||||
group :assorted do
|
group :assorted do
|
||||||
if RUBY_VERSION < "2.2.0"
|
if RUBY_VERSION < "2.2"
|
||||||
gem "pry", "~> 0.12.2"
|
gem "pry", "~> 0.12.2"
|
||||||
else
|
else
|
||||||
gem "pry"
|
gem "pry"
|
||||||
end
|
end
|
||||||
|
|
||||||
platform :mri do
|
platform :mri do
|
||||||
if RUBY_VERSION < "2.2.0"
|
if RUBY_VERSION < "2.2"
|
||||||
gem "pry-byebug", "~> 3.4.3"
|
gem "pry-byebug", "~> 3.4.3"
|
||||||
else
|
else
|
||||||
gem "debug" if RUBY_VERSION >= "3.1.0"
|
gem "debug" if RUBY_VERSION >= "3.1.0"
|
||||||
|
@ -61,7 +61,7 @@ module HTTPX::Plugins
|
|||||||
@state = :idle
|
@state = :idle
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(response, *)
|
def call(response, _)
|
||||||
response.body.each do |chunk|
|
response.body.each do |chunk|
|
||||||
@buffer << chunk
|
@buffer << chunk
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ module HTTPX
|
|||||||
@requests[request] = hostname
|
@requests[request] = hostname
|
||||||
resolver_connection.send(request)
|
resolver_connection.send(request)
|
||||||
@connections << connection
|
@connections << connection
|
||||||
rescue ResolveError, Resolv::DNS::EncodeError => e
|
rescue ResolveError, Resolv::DNS::EncodeError, JSON::JSONError => e
|
||||||
@queries.delete(hostname)
|
@queries.delete(hostname)
|
||||||
emit_resolve_error(connection, connection.origin.host, e)
|
emit_resolve_error(connection, connection.origin.host, e)
|
||||||
end
|
end
|
||||||
@ -129,7 +129,7 @@ module HTTPX
|
|||||||
def parse(request, response)
|
def parse(request, response)
|
||||||
begin
|
begin
|
||||||
answers = decode_response_body(response)
|
answers = decode_response_body(response)
|
||||||
rescue Resolv::DNS::DecodeError => e
|
rescue Resolv::DNS::DecodeError, JSON::JSONError => e
|
||||||
host, connection = @queries.first
|
host, connection = @queries.first
|
||||||
@queries.delete(host)
|
@queries.delete(host)
|
||||||
emit_resolve_error(connection, connection.origin.host, e)
|
emit_resolve_error(connection, connection.origin.host, e)
|
||||||
@ -203,6 +203,11 @@ module HTTPX
|
|||||||
|
|
||||||
def decode_response_body(response)
|
def decode_response_body(response)
|
||||||
case response.headers["content-type"]
|
case response.headers["content-type"]
|
||||||
|
when "application/dns-json",
|
||||||
|
"application/json",
|
||||||
|
%r{^application/x-javascript} # because google...
|
||||||
|
payload = JSON.parse(response.to_s)
|
||||||
|
payload["Answer"]
|
||||||
when "application/dns-udpwireformat",
|
when "application/dns-udpwireformat",
|
||||||
"application/dns-message"
|
"application/dns-message"
|
||||||
Resolver.decode_dns_answer(response.to_s)
|
Resolver.decode_dns_answer(response.to_s)
|
||||||
|
@ -76,8 +76,8 @@ module HTTPX
|
|||||||
raise err
|
raise err
|
||||||
end
|
end
|
||||||
|
|
||||||
def json(*args)
|
def json(options = nil)
|
||||||
decode("json", *args)
|
decode("json", options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def form
|
def form
|
||||||
@ -86,7 +86,7 @@ module HTTPX
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def decode(format, *args)
|
def decode(format, options = nil)
|
||||||
# TODO: check if content-type is a valid format, i.e. "application/json" for json parsing
|
# TODO: check if content-type is a valid format, i.e. "application/json" for json parsing
|
||||||
transcoder = Transcoder.registry(format)
|
transcoder = Transcoder.registry(format)
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ module HTTPX
|
|||||||
|
|
||||||
raise Error, "no decoder available for \"#{format}\"" unless decoder
|
raise Error, "no decoder available for \"#{format}\"" unless decoder
|
||||||
|
|
||||||
decoder.call(self, *args)
|
decoder.call(self, options)
|
||||||
rescue Registry::Error
|
rescue Registry::Error
|
||||||
raise Error, "no decoder available for \"#{format}\""
|
raise Error, "no decoder available for \"#{format}\""
|
||||||
end
|
end
|
||||||
|
@ -36,7 +36,7 @@ module HTTPX::Transcoder
|
|||||||
module Decoder
|
module Decoder
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def call(response, *)
|
def call(response, _)
|
||||||
URI.decode_www_form(response.to_s).each_with_object({}) do |(field, value), params|
|
URI.decode_www_form(response.to_s).each_with_object({}) do |(field, value), params|
|
||||||
HTTPX::Transcoder.normalize_query(params, field, value, PARAM_DEPTH_LIMIT)
|
HTTPX::Transcoder.normalize_query(params, field, value, PARAM_DEPTH_LIMIT)
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "forwardable"
|
require "forwardable"
|
||||||
|
require "json"
|
||||||
|
|
||||||
module HTTPX::Transcoder
|
module HTTPX::Transcoder
|
||||||
module JSON
|
module JSON
|
||||||
@ -18,7 +19,7 @@ module HTTPX::Transcoder
|
|||||||
def_delegator :@raw, :bytesize
|
def_delegator :@raw, :bytesize
|
||||||
|
|
||||||
def initialize(json)
|
def initialize(json)
|
||||||
@raw = JSON.json_dump(json)
|
@raw = ::JSON.dump(json)
|
||||||
@charset = @raw.encoding.name.downcase
|
@charset = @raw.encoding.name.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -36,25 +37,8 @@ module HTTPX::Transcoder
|
|||||||
|
|
||||||
raise HTTPX::Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
|
raise HTTPX::Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
|
||||||
|
|
||||||
method(:json_load)
|
::JSON.method(:parse)
|
||||||
end
|
end
|
||||||
|
|
||||||
# rubocop:disable Style/SingleLineMethods
|
|
||||||
if defined?(MultiJson)
|
|
||||||
def json_load(*args); MultiJson.load(*args); end
|
|
||||||
def json_dump(*args); MultiJson.dump(*args); end
|
|
||||||
elsif defined?(Oj)
|
|
||||||
def json_load(response, *args); Oj.load(response.to_s, *args); end
|
|
||||||
def json_dump(*args); Oj.dump(*args); end
|
|
||||||
elsif defined?(Yajl)
|
|
||||||
def json_load(response, *args); Yajl::Parser.new(*args).parse(response.to_s); end
|
|
||||||
def json_dump(*args); Yajl::Encoder.encode(*args); end
|
|
||||||
else
|
|
||||||
require "json"
|
|
||||||
def json_load(*args); ::JSON.parse(*args); end
|
|
||||||
def json_dump(*args); ::JSON.dump(*args); end
|
|
||||||
end
|
|
||||||
# rubocop:enable Style/SingleLineMethods
|
|
||||||
end
|
end
|
||||||
register "json", JSON
|
register "json", JSON
|
||||||
end
|
end
|
||||||
|
@ -61,7 +61,7 @@ module HTTPX
|
|||||||
@boundary: String
|
@boundary: String
|
||||||
@intermediate_boundary: String
|
@intermediate_boundary: String
|
||||||
|
|
||||||
def call: (Response response, *untyped) -> Hash[String, untyped]
|
def call: (Response response, untyped) -> Hash[String, untyped]
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ module HTTPX
|
|||||||
end
|
end
|
||||||
|
|
||||||
interface _Decoder
|
interface _Decoder
|
||||||
def call: (Response response, *untyped) -> untyped
|
def call: (Response response, untyped options) -> untyped
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,9 +5,6 @@ module HTTPX::Transcoder
|
|||||||
def self?.encode: (_ToJson json) -> Encoder
|
def self?.encode: (_ToJson json) -> Encoder
|
||||||
def self?.decode: (HTTPX::Response response) -> _Decoder
|
def self?.decode: (HTTPX::Response response) -> _Decoder
|
||||||
|
|
||||||
def self?.json_load: (string source, ?json_options) -> untyped
|
|
||||||
def self?.json_dump: (_ToJson obj, *untyped) -> String
|
|
||||||
|
|
||||||
class Encoder
|
class Encoder
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
include _Encoder
|
include _Encoder
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "multi_json"
|
|
||||||
require "test_helper"
|
|
||||||
|
|
||||||
class ResponseYajlTest < Minitest::Test
|
|
||||||
include HTTPX
|
|
||||||
|
|
||||||
def test_response_decoders
|
|
||||||
json_response = Response.new(request, 200, "2.0", { "content-type" => "application/json" })
|
|
||||||
json_response << %({"a": "b"})
|
|
||||||
assert json_response.json == { "a" => "b" }
|
|
||||||
assert json_response.json(symbolize_keys: true) == { :a => "b" }
|
|
||||||
json_response << "bogus"
|
|
||||||
assert_raises(MultiJson::ParseError) { json_response.json }
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def request(verb = :get, uri = "http://google.com")
|
|
||||||
Request.new(verb, uri)
|
|
||||||
end
|
|
||||||
|
|
||||||
def response(*args)
|
|
||||||
Response.new(*args)
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,27 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "oj"
|
|
||||||
require "test_helper"
|
|
||||||
|
|
||||||
class ResponseOjTest < Minitest::Test
|
|
||||||
include HTTPX
|
|
||||||
|
|
||||||
def test_response_decoders
|
|
||||||
json_response = Response.new(request, 200, "2.0", { "content-type" => "application/json" })
|
|
||||||
json_response << %({"a": "b"})
|
|
||||||
assert json_response.json == { "a" => "b" }
|
|
||||||
assert json_response.json(symbol_keys: true) == { :a => "b" }
|
|
||||||
json_response << "bogus"
|
|
||||||
assert_raises(Oj::ParseError) { json_response.json }
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def request(verb = :get, uri = "http://google.com")
|
|
||||||
Request.new(verb, uri)
|
|
||||||
end
|
|
||||||
|
|
||||||
def response(*args)
|
|
||||||
Response.new(*args)
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,27 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "yajl"
|
|
||||||
require "test_helper"
|
|
||||||
|
|
||||||
class ResponseYajlTest < Minitest::Test
|
|
||||||
include HTTPX
|
|
||||||
|
|
||||||
def test_response_decoders
|
|
||||||
json_response = Response.new(request, 200, "2.0", { "content-type" => "application/json" })
|
|
||||||
json_response << %({"a": "b"})
|
|
||||||
assert json_response.json == { "a" => "b" }
|
|
||||||
assert json_response.json(symbolize_keys: true) == { :a => "b" }
|
|
||||||
json_response << "bogus"
|
|
||||||
assert_raises(Yajl::ParseError) { json_response.json }
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def request(verb = :get, uri = "http://google.com")
|
|
||||||
Request.new(verb, uri)
|
|
||||||
end
|
|
||||||
|
|
||||||
def response(*args)
|
|
||||||
Response.new(*args)
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
x
Reference in New Issue
Block a user