Permalink
Browse files

Don't send request body unless post request.

Fixes #75. Turns body content into get parameters.
  • Loading branch information...
1 parent aee1350 commit ba9642554266b0a5d6c151ba1fb468e29f741199 @i0rek i0rek committed May 21, 2012
Showing with 20 additions and 13 deletions.
  1. +2 −2 lib/typhoeus/hydra.rb
  2. +18 −11 lib/typhoeus/request.rb
View
@@ -161,9 +161,9 @@ def get_easy_object(request)
easy.url = request.url
easy.method = request.method
- easy.params = request.params if request.method == :post && !request.params.nil?
+ easy.params = request.params if request.method == :post && request.params.present?
easy.headers = request.headers if request.headers
- easy.request_body = request.body if request.body
+ easy.request_body = request.body if request.method == :post && request.body.present?
easy.timeout = request.timeout if request.timeout
easy.connect_timeout = request.connect_timeout if request.connect_timeout
easy.interface = request.interface if request.interface
View
@@ -38,7 +38,6 @@ class Request
:proxy_type
]
- attr_reader :url
attr_accessor *ACCESSOR_OPTIONS
# Initialize a new Request
@@ -72,6 +71,7 @@ class Request
# ** +:auth_method
#
def initialize(url, options = {})
+ @url = url
@method = options[:method] || :get
@params = options[:params]
@body = options[:body]
@@ -107,14 +107,6 @@ def initialize(url, options = {})
@password = options[:password]
@auth_method = options[:auth_method]
- if @method == :post
- @url = url
- else
- @url = @params ? "#{url}?#{params_string}" : url
- end
-
- @parsed_uri = URI.parse(@url)
-
@on_complete = nil
@after_complete = nil
@handled_response = nil
@@ -123,13 +115,27 @@ def initialize(url, options = {})
LOCALHOST_ALIASES = %w[ localhost 127.0.0.1 0.0.0.0 ]
def localhost?
- LOCALHOST_ALIASES.include?(@parsed_uri.host)
+ LOCALHOST_ALIASES.include?(parsed_uri.host)
end
def user_agent
headers['User-Agent']
end
+ def url
+ if @method == :post
+ @url
+ else
+ url = "#{@url}?#{params_string}"
+ url += "&#{URI.escape(@body)}" if @body
+ url.gsub("?&", "?").gsub(/\?$/, '')
+ end
+ end
+
+ def parsed_uri
+ @parsed_uri ||= URI.parse(@url)
+ end
+
def host
slash_location = @url.index('/', 8)
if slash_location
@@ -141,10 +147,11 @@ def host
end
def host_domain
- @parsed_uri.host
+ parsed_uri.host
end
def params_string
+ return nil unless params
traversal = Typhoeus::Utils.traverse_params_hash(params)
Typhoeus::Utils.traversal_to_param_string(traversal)
end

0 comments on commit ba96425

Please sign in to comment.