httsoiree 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +7 -0
- data/Gemfile +14 -0
- data/Guardfile +16 -0
- data/History +303 -0
- data/MIT-LICENSE +20 -0
- data/README.md +77 -0
- data/Rakefile +12 -0
- data/bin/httparty +117 -0
- data/cucumber.yml +1 -0
- data/examples/aaws.rb +32 -0
- data/examples/basic.rb +28 -0
- data/examples/crack.rb +19 -0
- data/examples/custom_parsers.rb +67 -0
- data/examples/delicious.rb +37 -0
- data/examples/google.rb +16 -0
- data/examples/headers_and_user_agents.rb +6 -0
- data/examples/logging.rb +38 -0
- data/examples/nokogiri_html_parser.rb +22 -0
- data/examples/rubyurl.rb +14 -0
- data/examples/stackexchange.rb +24 -0
- data/examples/tripit_sign_in.rb +33 -0
- data/examples/twitter.rb +31 -0
- data/examples/whoismyrep.rb +10 -0
- data/features/basic_authentication.feature +20 -0
- data/features/command_line.feature +7 -0
- data/features/deals_with_http_error_codes.feature +26 -0
- data/features/digest_authentication.feature +20 -0
- data/features/handles_compressed_responses.feature +27 -0
- data/features/handles_multiple_formats.feature +57 -0
- data/features/steps/env.rb +22 -0
- data/features/steps/httparty_response_steps.rb +52 -0
- data/features/steps/httparty_steps.rb +43 -0
- data/features/steps/mongrel_helper.rb +94 -0
- data/features/steps/remote_service_steps.rb +74 -0
- data/features/supports_read_timeout_option.feature +13 -0
- data/features/supports_redirection.feature +22 -0
- data/features/supports_timeout_option.feature +13 -0
- data/httparty.gemspec +25 -0
- data/lib/httparty/connection_adapter.rb +188 -0
- data/lib/httparty/cookie_hash.rb +22 -0
- data/lib/httparty/core_extensions.rb +32 -0
- data/lib/httparty/exceptions.rb +29 -0
- data/lib/httparty/hash_conversions.rb +51 -0
- data/lib/httparty/logger/apache_logger.rb +22 -0
- data/lib/httparty/logger/curl_logger.rb +48 -0
- data/lib/httparty/logger/logger.rb +18 -0
- data/lib/httparty/module_inheritable_attributes.rb +56 -0
- data/lib/httparty/net_digest_auth.rb +84 -0
- data/lib/httparty/parser.rb +141 -0
- data/lib/httparty/request.rb +339 -0
- data/lib/httparty/response/headers.rb +31 -0
- data/lib/httparty/response.rb +72 -0
- data/lib/httparty/version.rb +3 -0
- data/lib/httparty.rb +618 -0
- data/lib/httsoiree.rb +3 -0
- data/script/release +42 -0
- data/spec/fixtures/delicious.xml +23 -0
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/google.html +3 -0
- data/spec/fixtures/ssl/generate.sh +29 -0
- data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
- data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
- data/spec/fixtures/ssl/generated/ca.crt +16 -0
- data/spec/fixtures/ssl/generated/ca.key +15 -0
- data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
- data/spec/fixtures/ssl/generated/server.crt +13 -0
- data/spec/fixtures/ssl/generated/server.key +15 -0
- data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
- data/spec/fixtures/twitter.csv +2 -0
- data/spec/fixtures/twitter.json +1 -0
- data/spec/fixtures/twitter.xml +403 -0
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
- data/spec/httparty/connection_adapter_spec.rb +370 -0
- data/spec/httparty/cookie_hash_spec.rb +83 -0
- data/spec/httparty/exception_spec.rb +23 -0
- data/spec/httparty/logger/apache_logger_spec.rb +41 -0
- data/spec/httparty/logger/curl_logger_spec.rb +18 -0
- data/spec/httparty/logger/logger_spec.rb +22 -0
- data/spec/httparty/net_digest_auth_spec.rb +152 -0
- data/spec/httparty/parser_spec.rb +165 -0
- data/spec/httparty/request_spec.rb +774 -0
- data/spec/httparty/response_spec.rb +221 -0
- data/spec/httparty/ssl_spec.rb +74 -0
- data/spec/httparty_spec.rb +783 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +37 -0
- data/spec/support/ssl_test_helper.rb +47 -0
- data/spec/support/ssl_test_server.rb +80 -0
- data/spec/support/stub_response.rb +43 -0
- data/website/css/common.css +47 -0
- data/website/index.html +73 -0
- metadata +215 -0
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'base64'
|
2
|
+
class BasicMongrelHandler < Mongrel::HttpHandler
|
3
|
+
attr_accessor :content_type, :custom_headers, :response_body, :response_code, :preprocessor, :username, :password
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@content_type = "text/html"
|
7
|
+
@response_body = ""
|
8
|
+
@response_code = 200
|
9
|
+
@custom_headers = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def process(request, response)
|
13
|
+
instance_eval &preprocessor if preprocessor
|
14
|
+
reply_with(response, response_code, response_body)
|
15
|
+
end
|
16
|
+
|
17
|
+
def reply_with(response, code, response_body)
|
18
|
+
response.start(code) do |head, body|
|
19
|
+
head["Content-Type"] = content_type
|
20
|
+
custom_headers.each { |k,v| head[k] = v }
|
21
|
+
body.write(response_body)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class DeflateHandler < BasicMongrelHandler
|
27
|
+
def process(request, response)
|
28
|
+
response.start do |head, body|
|
29
|
+
head['Content-Encoding'] = 'deflate'
|
30
|
+
body.write Zlib::Deflate.deflate(response_body)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class GzipHandler < BasicMongrelHandler
|
36
|
+
def process(request, response)
|
37
|
+
response.start do |head, body|
|
38
|
+
head['Content-Encoding'] = 'gzip'
|
39
|
+
body.write gzip(response_body)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def gzip(string)
|
46
|
+
sio = StringIO.new('', 'r+')
|
47
|
+
gz = Zlib::GzipWriter.new sio
|
48
|
+
gz.write string
|
49
|
+
gz.finish
|
50
|
+
sio.rewind
|
51
|
+
sio.read
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module BasicAuthentication
|
56
|
+
def self.extended(base)
|
57
|
+
base.custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"'
|
58
|
+
end
|
59
|
+
|
60
|
+
def process(request, response)
|
61
|
+
if authorized?(request)
|
62
|
+
super
|
63
|
+
else
|
64
|
+
reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def authorized?(request)
|
69
|
+
request.params["HTTP_AUTHORIZATION"] == "Basic " + Base64.encode64("#{@username}:#{@password}").strip
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
module DigestAuthentication
|
74
|
+
def self.extended(base)
|
75
|
+
base.custom_headers["WWW-Authenticate"] = 'Digest realm="testrealm@host.com",qop="auth,auth-int",nonce="nonce",opaque="opaque"'
|
76
|
+
end
|
77
|
+
|
78
|
+
def process(request, response)
|
79
|
+
if authorized?(request)
|
80
|
+
super
|
81
|
+
else
|
82
|
+
reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def authorized?(request)
|
87
|
+
request.params["HTTP_AUTHORIZATION"] =~ /Digest.*uri=/
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def new_mongrel_redirector(target_url, relative_path = false)
|
92
|
+
target_url = "http://#{@host_and_port}#{target_url}" unless relative_path
|
93
|
+
Mongrel::RedirectHandler.new(target_url)
|
94
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
Given /a remote service that returns '(.*)'/ do |response_body|
|
2
|
+
@handler = BasicMongrelHandler.new
|
3
|
+
Given "the response from the service has a body of '#{response_body}'"
|
4
|
+
end
|
5
|
+
|
6
|
+
Given /^a remote service that returns:$/ do |response_body|
|
7
|
+
@handler = BasicMongrelHandler.new
|
8
|
+
@handler.response_body = response_body
|
9
|
+
end
|
10
|
+
|
11
|
+
Given /a remote service that returns a (\d+) status code/ do |code|
|
12
|
+
@handler = BasicMongrelHandler.new
|
13
|
+
@handler.response_code = code
|
14
|
+
end
|
15
|
+
|
16
|
+
Given /that service is accessed at the path '(.*)'/ do |path|
|
17
|
+
@server.register(path, @handler)
|
18
|
+
end
|
19
|
+
|
20
|
+
Given /^that service takes (\d+) seconds to generate a response$/ do |time|
|
21
|
+
@server_response_time = time.to_i
|
22
|
+
@handler.preprocessor = Proc.new { sleep time.to_i }
|
23
|
+
end
|
24
|
+
|
25
|
+
Given /^a remote deflate service$/ do
|
26
|
+
@handler = DeflateHandler.new
|
27
|
+
end
|
28
|
+
|
29
|
+
Given /^a remote gzip service$/ do
|
30
|
+
@handler = GzipHandler.new
|
31
|
+
end
|
32
|
+
|
33
|
+
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
|
34
|
+
@handler.content_type = content_type
|
35
|
+
end
|
36
|
+
|
37
|
+
Given /the response from the service has a body of '(.*)'/ do |response_body|
|
38
|
+
@handler.response_body = response_body
|
39
|
+
end
|
40
|
+
|
41
|
+
Given /the url '(.*)' redirects to '(.*)'/ do |redirection_url, target_url|
|
42
|
+
@server.register redirection_url, new_mongrel_redirector(target_url)
|
43
|
+
end
|
44
|
+
|
45
|
+
Given /that service is protected by Basic Authentication/ do
|
46
|
+
@handler.extend BasicAuthentication
|
47
|
+
end
|
48
|
+
|
49
|
+
Given /that service is protected by Digest Authentication/ do
|
50
|
+
@handler.extend DigestAuthentication
|
51
|
+
end
|
52
|
+
|
53
|
+
Given /that service requires the username '(.*)' with the password '(.*)'/ do |username, password|
|
54
|
+
@handler.username = username
|
55
|
+
@handler.password = password
|
56
|
+
end
|
57
|
+
|
58
|
+
Given /a restricted page at '(.*)'/ do |url|
|
59
|
+
Given "a remote service that returns 'A response I will never see'"
|
60
|
+
And "that service is accessed at the path '#{url}'"
|
61
|
+
And "that service is protected by Basic Authentication"
|
62
|
+
And "that service requires the username 'something' with the password 'secret'"
|
63
|
+
end
|
64
|
+
|
65
|
+
# This joins the server thread, and halts cucumber, so you can actually hit the
|
66
|
+
# server with a browser. Runs until you kill it with Ctrl-c
|
67
|
+
Given /I want to hit this in a browser/ do
|
68
|
+
@server.acceptor.join
|
69
|
+
end
|
70
|
+
|
71
|
+
Then /I wait for the server to recover/ do
|
72
|
+
timeout = @request_options[:timeout] || 0
|
73
|
+
sleep @server_response_time - timeout
|
74
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Feature: Supports the read timeout option
|
2
|
+
In order to handle inappropriately slow response times
|
3
|
+
As a developer
|
4
|
+
I want my request to raise an exception after my specified read_timeout as elapsed
|
5
|
+
|
6
|
+
Scenario: A long running response
|
7
|
+
Given a remote service that returns '<h1>Some HTML</h1>'
|
8
|
+
And that service is accessed at the path '/long_running_service.html'
|
9
|
+
And that service takes 2 seconds to generate a response
|
10
|
+
When I set my HTTParty read_timeout option to 1
|
11
|
+
And I call HTTParty#get with '/long_running_service.html'
|
12
|
+
Then it should raise a Timeout::Error exception
|
13
|
+
And I wait for the server to recover
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Feature: Supports Redirection
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to work with services that may redirect me
|
5
|
+
And I want it to follow a reasonable number of redirects
|
6
|
+
Because sometimes web services do that
|
7
|
+
|
8
|
+
Scenario: A service that redirects once
|
9
|
+
Given a remote service that returns 'Service Response'
|
10
|
+
And that service is accessed at the path '/landing_service.html'
|
11
|
+
And the url '/redirector.html' redirects to '/landing_service.html'
|
12
|
+
When I call HTTParty#get with '/redirector.html'
|
13
|
+
Then the return value should match 'Service Response'
|
14
|
+
|
15
|
+
# TODO: Look in to why this actually fails...
|
16
|
+
Scenario: A service that redirects to a relative URL
|
17
|
+
|
18
|
+
Scenario: A service that redirects infinitely
|
19
|
+
Given the url '/first.html' redirects to '/second.html'
|
20
|
+
And the url '/second.html' redirects to '/first.html'
|
21
|
+
When I call HTTParty#get with '/first.html'
|
22
|
+
Then it should raise an HTTParty::RedirectionTooDeep exception
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Feature: Supports the timeout option
|
2
|
+
In order to handle inappropriately slow response times
|
3
|
+
As a developer
|
4
|
+
I want my request to raise an exception after my specified timeout as elapsed
|
5
|
+
|
6
|
+
Scenario: A long running response
|
7
|
+
Given a remote service that returns '<h1>Some HTML</h1>'
|
8
|
+
And that service is accessed at the path '/long_running_service.html'
|
9
|
+
And that service takes 2 seconds to generate a response
|
10
|
+
When I set my HTTParty timeout option to 1
|
11
|
+
And I call HTTParty#get with '/long_running_service.html'
|
12
|
+
Then it should raise a Timeout::Error exception
|
13
|
+
And I wait for the server to recover
|
data/httparty.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "httparty/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "httparty"
|
7
|
+
s.version = HTTParty::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.licenses = ['MIT']
|
10
|
+
s.authors = ["John Nunemaker", "Sandro Turriate"]
|
11
|
+
s.email = ["nunemaker@gmail.com"]
|
12
|
+
s.homepage = "http://jnunemaker.github.com/httparty"
|
13
|
+
s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
14
|
+
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
15
|
+
|
16
|
+
s.required_ruby_version = '>= 1.9.3'
|
17
|
+
|
18
|
+
s.add_dependency 'json', "~> 1.8"
|
19
|
+
s.add_dependency 'multi_xml', ">= 0.5.2"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
module HTTParty
|
2
|
+
# Default connection adapter that returns a new Net::HTTP each time
|
3
|
+
#
|
4
|
+
# == Custom Connection Factories
|
5
|
+
#
|
6
|
+
# If you like to implement your own connection adapter, subclassing
|
7
|
+
# HTTPParty::ConnectionAdapter will make it easier. Just override
|
8
|
+
# the #connection method. The uri and options attributes will have
|
9
|
+
# all the info you need to construct your http connection. Whatever
|
10
|
+
# you return from your connection method needs to adhere to the
|
11
|
+
# Net::HTTP interface as this is what HTTParty expects.
|
12
|
+
#
|
13
|
+
# @example log the uri and options
|
14
|
+
# class LoggingConnectionAdapter < HTTParty::ConnectionAdapter
|
15
|
+
# def connection
|
16
|
+
# puts uri
|
17
|
+
# puts options
|
18
|
+
# Net::HTTP.new(uri)
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @example count number of http calls
|
23
|
+
# class CountingConnectionAdapter < HTTParty::ConnectionAdapter
|
24
|
+
# @@count = 0
|
25
|
+
#
|
26
|
+
# self.count
|
27
|
+
# @@count
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# def connection
|
31
|
+
# self.count += 1
|
32
|
+
# super
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# === Configuration
|
37
|
+
# There is lots of configuration data available for your connection adapter
|
38
|
+
# in the #options attribute. It is up to you to interpret them within your
|
39
|
+
# connection adapter. Take a look at the implementation of
|
40
|
+
# HTTParty::ConnectionAdapter#connection for examples of how they are used.
|
41
|
+
# Some things that are probably interesting are as follows:
|
42
|
+
# * :+timeout+: timeout in seconds
|
43
|
+
# * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set
|
44
|
+
# * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set
|
45
|
+
# * :+debug_output+: see HTTParty::ClassMethods.debug_output.
|
46
|
+
# * :+pem+: contains pem data. see HTTParty::ClassMethods.pem.
|
47
|
+
# * :+verify+: verify the server’s certificate against the ca certificate.
|
48
|
+
# * :+verify_peer+: set to false to turn off server verification but still send client certificate
|
49
|
+
# * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
|
50
|
+
# * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
|
51
|
+
# * :+connection_adapter_options+: contains the hash you passed to HTTParty.connection_adapter when you configured your connection adapter
|
52
|
+
class ConnectionAdapter
|
53
|
+
|
54
|
+
# Private: Regex used to strip brackets from IPv6 URIs.
|
55
|
+
StripIpv6BracketsRegex = /\A\[(.*)\]\z/
|
56
|
+
|
57
|
+
# Public
|
58
|
+
def self.call(uri, options)
|
59
|
+
new(uri, options).connection
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_reader :uri, :options
|
63
|
+
|
64
|
+
def initialize(uri, options={})
|
65
|
+
raise ArgumentError, "uri must be a URI, not a #{uri.class}" unless uri.kind_of? URI
|
66
|
+
|
67
|
+
@uri = uri
|
68
|
+
@options = options
|
69
|
+
end
|
70
|
+
|
71
|
+
def connection
|
72
|
+
host = clean_host(uri.host)
|
73
|
+
if options[:http_proxyaddr]
|
74
|
+
http = Net::HTTP.new(host, uri.port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass])
|
75
|
+
else
|
76
|
+
http = Net::HTTP.new(host, uri.port)
|
77
|
+
end
|
78
|
+
|
79
|
+
http.use_ssl = ssl_implied?(uri)
|
80
|
+
|
81
|
+
attach_ssl_certificates(http, options)
|
82
|
+
|
83
|
+
if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
|
84
|
+
http.open_timeout = options[:timeout]
|
85
|
+
http.read_timeout = options[:timeout]
|
86
|
+
end
|
87
|
+
|
88
|
+
if options[:read_timeout] && (options[:read_timeout].is_a?(Integer) || options[:read_timeout].is_a?(Float))
|
89
|
+
http.read_timeout = options[:read_timeout]
|
90
|
+
end
|
91
|
+
|
92
|
+
if options[:open_timeout] && (options[:open_timeout].is_a?(Integer) || options[:open_timeout].is_a?(Float))
|
93
|
+
http.open_timeout = options[:open_timeout]
|
94
|
+
end
|
95
|
+
|
96
|
+
if options[:debug_output]
|
97
|
+
http.set_debug_output(options[:debug_output])
|
98
|
+
end
|
99
|
+
|
100
|
+
if options[:ciphers]
|
101
|
+
http.ciphers = options[:ciphers]
|
102
|
+
end
|
103
|
+
|
104
|
+
# Bind to a specific local address or port
|
105
|
+
#
|
106
|
+
# @see https://bugs.ruby-lang.org/issues/6617
|
107
|
+
if options[:local_host]
|
108
|
+
if RUBY_VERSION >= "2.0.0"
|
109
|
+
http.local_host = options[:local_host]
|
110
|
+
else
|
111
|
+
Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
if options[:local_port]
|
116
|
+
if RUBY_VERSION >= "2.0.0"
|
117
|
+
http.local_port = options[:local_port]
|
118
|
+
else
|
119
|
+
Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
return http
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def clean_host(host)
|
129
|
+
strip_ipv6_brackets(host)
|
130
|
+
end
|
131
|
+
|
132
|
+
def strip_ipv6_brackets(host)
|
133
|
+
StripIpv6BracketsRegex =~ host ? $1 : host
|
134
|
+
end
|
135
|
+
|
136
|
+
def ssl_implied?(uri)
|
137
|
+
uri.port == 443 || uri.instance_of?(URI::HTTPS)
|
138
|
+
end
|
139
|
+
|
140
|
+
def attach_ssl_certificates(http, options)
|
141
|
+
if http.use_ssl?
|
142
|
+
if options.fetch(:verify, true)
|
143
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
144
|
+
if options[:cert_store]
|
145
|
+
http.cert_store = options[:cert_store]
|
146
|
+
else
|
147
|
+
# Use the default cert store by default, i.e. system ca certs
|
148
|
+
http.cert_store = OpenSSL::X509::Store.new
|
149
|
+
http.cert_store.set_default_paths
|
150
|
+
end
|
151
|
+
else
|
152
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
153
|
+
end
|
154
|
+
|
155
|
+
# Client certificate authentication
|
156
|
+
if options[:pem]
|
157
|
+
http.cert = OpenSSL::X509::Certificate.new(options[:pem])
|
158
|
+
http.key = OpenSSL::PKey::RSA.new(options[:pem], options[:pem_password])
|
159
|
+
http.verify_mode = options[:verify_peer] == false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
160
|
+
end
|
161
|
+
|
162
|
+
# PKCS12 client certificate authentication
|
163
|
+
if options[:p12]
|
164
|
+
p12 = OpenSSL::PKCS12.new(options[:p12], options[:p12_password])
|
165
|
+
http.cert = p12.certificate
|
166
|
+
http.key = p12.key
|
167
|
+
http.verify_mode = options[:verify_peer] == false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
168
|
+
end
|
169
|
+
|
170
|
+
# SSL certificate authority file and/or directory
|
171
|
+
if options[:ssl_ca_file]
|
172
|
+
http.ca_file = options[:ssl_ca_file]
|
173
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
174
|
+
end
|
175
|
+
|
176
|
+
if options[:ssl_ca_path]
|
177
|
+
http.ca_path = options[:ssl_ca_path]
|
178
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
179
|
+
end
|
180
|
+
|
181
|
+
# This is only Ruby 1.9+
|
182
|
+
if options[:ssl_version] && http.respond_to?(:ssl_version=)
|
183
|
+
http.ssl_version = options[:ssl_version]
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class HTTParty::CookieHash < Hash #:nodoc:
|
2
|
+
|
3
|
+
CLIENT_COOKIES = %w{path expires domain path secure httponly}
|
4
|
+
|
5
|
+
def add_cookies(value)
|
6
|
+
case value
|
7
|
+
when Hash
|
8
|
+
merge!(value)
|
9
|
+
when String
|
10
|
+
value.split('; ').each do |cookie|
|
11
|
+
array = cookie.split('=',2)
|
12
|
+
self[array[0].to_sym] = array[1]
|
13
|
+
end
|
14
|
+
else
|
15
|
+
raise "add_cookies only takes a Hash or a String"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_cookie_string
|
20
|
+
delete_if { |k, v| CLIENT_COOKIES.include?(k.to_s.downcase) }.collect { |k, v| "#{k}=#{v}" }.join("; ")
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module HTTParty
|
2
|
+
if defined?(::BasicObject)
|
3
|
+
BasicObject = ::BasicObject #:nodoc:
|
4
|
+
else
|
5
|
+
class BasicObject #:nodoc:
|
6
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
unless defined?(Net::HTTP::Patch)
|
11
|
+
class Net::HTTP
|
12
|
+
def patch(path, data, initheader = nil, dest = nil, &block) #:nodoc:
|
13
|
+
res = nil
|
14
|
+
request(Patch.new(path, initheader), data) {|r|
|
15
|
+
r.read_body dest, &block
|
16
|
+
res = r
|
17
|
+
}
|
18
|
+
unless @newimpl
|
19
|
+
res.value
|
20
|
+
return res, res.body
|
21
|
+
end
|
22
|
+
res
|
23
|
+
end
|
24
|
+
|
25
|
+
class Patch < Net::HTTPRequest
|
26
|
+
METHOD = 'PATCH'
|
27
|
+
REQUEST_HAS_BODY = true
|
28
|
+
RESPONSE_HAS_BODY = true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module HTTParty
|
2
|
+
# @abstact Exceptions raised by HTTParty inherit from Error
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
# Exception raised when you attempt to set a non-existant format
|
6
|
+
class UnsupportedFormat < Error; end
|
7
|
+
|
8
|
+
# Exception raised when using a URI scheme other than HTTP or HTTPS
|
9
|
+
class UnsupportedURIScheme < Error; end
|
10
|
+
|
11
|
+
# @abstract Exceptions which inherit from ResponseError contain the Net::HTTP
|
12
|
+
# response object accessible via the {#response} method.
|
13
|
+
class ResponseError < Error
|
14
|
+
# Returns the response of the last request
|
15
|
+
# @return [Net::HTTPResponse] A subclass of Net::HTTPResponse, e.g.
|
16
|
+
# Net::HTTPOK
|
17
|
+
attr_reader :response
|
18
|
+
|
19
|
+
# Instantiate an instance of ResponseError with a Net::HTTPResponse object
|
20
|
+
# @param [Net::HTTPResponse]
|
21
|
+
def initialize(response)
|
22
|
+
@response = response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Exception that is raised when request has redirected too many times.
|
27
|
+
# Calling {#response} returns the Net:HTTP response object.
|
28
|
+
class RedirectionTooDeep < ResponseError; end
|
29
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module HTTParty
|
2
|
+
module HashConversions
|
3
|
+
# @return <String> This hash as a query string
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# { name: "Bob",
|
7
|
+
# address: {
|
8
|
+
# street: '111 Ruby Ave.',
|
9
|
+
# city: 'Ruby Central',
|
10
|
+
# phones: ['111-111-1111', '222-222-2222']
|
11
|
+
# }
|
12
|
+
# }.to_params
|
13
|
+
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
|
14
|
+
def self.to_params(hash)
|
15
|
+
params = hash.to_hash.map { |k,v| normalize_param(k,v) }.join
|
16
|
+
params.chop! # trailing &
|
17
|
+
params
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param key<Object> The key for the param.
|
21
|
+
# @param value<Object> The value for the param.
|
22
|
+
#
|
23
|
+
# @return <String> This key value pair as a param
|
24
|
+
#
|
25
|
+
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
26
|
+
def self.normalize_param(key, value)
|
27
|
+
param = ''
|
28
|
+
stack = []
|
29
|
+
|
30
|
+
if value.respond_to?(:to_ary)
|
31
|
+
param << value.to_ary.map { |element| normalize_param("#{key}[]", element) }.join
|
32
|
+
elsif value.respond_to?(:to_hash)
|
33
|
+
stack << [key,value.to_hash]
|
34
|
+
else
|
35
|
+
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
|
36
|
+
end
|
37
|
+
|
38
|
+
stack.each do |parent, hash|
|
39
|
+
hash.each do |k, v|
|
40
|
+
if v.respond_to?(:to_hash)
|
41
|
+
stack << ["#{parent}[#{k}]", v.to_hash]
|
42
|
+
else
|
43
|
+
param << normalize_param("#{parent}[#{k}]", v)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
param
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module HTTParty
|
2
|
+
module Logger
|
3
|
+
class ApacheLogger #:nodoc:
|
4
|
+
TAG_NAME = HTTParty.name
|
5
|
+
|
6
|
+
attr_accessor :level, :logger, :current_time
|
7
|
+
|
8
|
+
def initialize(logger, level)
|
9
|
+
@logger = logger
|
10
|
+
@level = level.to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
def format(request, response)
|
14
|
+
current_time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
|
15
|
+
http_method = request.http_method.name.split("::").last.upcase
|
16
|
+
path = request.path.to_s
|
17
|
+
content_length = response.respond_to?(:headers) ? response.headers['Content-Length'] : response['Content-Length']
|
18
|
+
@logger.send @level, "[#{TAG_NAME}] [#{current_time}] #{response.code} \"#{http_method} #{path}\" #{content_length || "-"} "
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module HTTParty
|
2
|
+
module Logger
|
3
|
+
class CurlLogger #:nodoc:
|
4
|
+
TAG_NAME = HTTParty.name
|
5
|
+
OUT = ">"
|
6
|
+
IN = "<"
|
7
|
+
|
8
|
+
attr_accessor :level, :logger, :current_time
|
9
|
+
|
10
|
+
def initialize(logger, level)
|
11
|
+
@logger = logger
|
12
|
+
@level = level.to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
def format(request, response)
|
16
|
+
messages = []
|
17
|
+
time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
|
18
|
+
http_method = request.http_method.name.split("::").last.upcase
|
19
|
+
path = request.path.to_s
|
20
|
+
|
21
|
+
messages << print(time, OUT, "#{http_method} #{path}")
|
22
|
+
|
23
|
+
if request.options[:headers] && request.options[:headers].size > 0
|
24
|
+
request.options[:headers].each do |k, v|
|
25
|
+
messages << print(time, OUT, "#{k}: #{v}")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
messages << print(time, OUT, request.raw_body)
|
30
|
+
messages << print(time, OUT, "")
|
31
|
+
messages << print(time, IN, "HTTP/#{response.http_version} #{response.code}")
|
32
|
+
|
33
|
+
headers = response.respond_to?(:headers) ? response.headers : response
|
34
|
+
response.each_header do |response_header|
|
35
|
+
messages << print(time, IN, "#{response_header.capitalize}: #{headers[response_header]}")
|
36
|
+
end
|
37
|
+
|
38
|
+
messages << print(time, IN, "\n#{response.body}")
|
39
|
+
|
40
|
+
@logger.send @level, messages.join("\n")
|
41
|
+
end
|
42
|
+
|
43
|
+
def print(time, direction, line)
|
44
|
+
"[#{TAG_NAME}] [#{time}] #{direction} #{line}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'httparty/logger/apache_logger'
|
2
|
+
require 'httparty/logger/curl_logger'
|
3
|
+
|
4
|
+
module HTTParty
|
5
|
+
module Logger
|
6
|
+
def self.build(logger, level, formatter)
|
7
|
+
level ||= :info
|
8
|
+
format ||= :apache
|
9
|
+
|
10
|
+
case formatter
|
11
|
+
when :curl
|
12
|
+
Logger::CurlLogger.new(logger, level)
|
13
|
+
else
|
14
|
+
Logger::ApacheLogger.new(logger, level)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|