lib/ronin/network/http/http.rb in ronin-support-0.4.0.rc1 vs lib/ronin/network/http/http.rb in ronin-support-0.4.0.rc2
- old
+ new
@@ -1,7 +1,7 @@
#
-# Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
+# Copyright (c) 2006-2012 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of Ronin Support.
#
# Ronin Support is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
@@ -127,19 +127,19 @@
new_options[:ssl] = {} if url.scheme == 'https'
new_options[:host] = url.host
new_options[:port] = url.port
- new_options[:user] = url.user if url.user
+ new_options[:user] = url.user if url.user
new_options[:password] = url.password if url.password
new_options[:path] = unless url.path.empty?
url.path
else
'/'
end
- new_options[:path] += "?#{url.query}" if url.query
+ new_options[:path] += "?#{URI.escape(url.query)}" if url.query
return new_options
end
#
@@ -254,10 +254,16 @@
# The HTTP method to use for the request.
#
# @option options [String] :path ('/')
# The path to request.
#
+ # @option options [String] :query
+ # The query-string to append to the request path.
+ #
+ # @option options [String] :query_params
+ # The query-params to append to the request path.
+ #
# @option options [String] :body
# The body of the request.
#
# @option options [Hash, String] :form_data
# The form data that may be sent in the body of the request.
@@ -295,29 +301,42 @@
unless Net::HTTP.const_defined?(name)
raise(UnknownRequest,"unknown HTTP request type #{name.dump}")
end
headers = HTTP.headers(options[:headers])
- path = (options[:path] || '/').to_s
+ path = (options[:path] || '/').to_s
+ query = if options[:query]
+ URI.escape(options[:query])
+ elsif options[:query_params]
+ URI::QueryParams.dump(options[:query_params])
+ end
+ if query
+ # append the query-string onto the path
+ path += if path.include?('?')
+ "&#{query}"
+ else
+ "?#{query}"
+ end
+ end
+
request = Net::HTTP.const_get(name).new(path,headers)
if request.request_body_permitted?
if options[:form_data]
request.set_form_data(options[:form_data])
elsif options[:body]
request.body = options[:body]
end
end
- if (user = options.delete(:user))
- user = user.to_s
+ if options[:user]
+ user = options[:user].to_s
+ password = if options[:password]
+ options[:password].to_s
+ end
- if (password = options.delete(:password))
- password = password.to_s
- end
-
request.basic_auth(user,password)
end
return request
end
@@ -350,55 +369,55 @@
# Enables SSL for the HTTP connection.
#
# @option :ssl [Symbol] :verify
# Specifies the SSL certificate verification mode.
#
- # @yield [session]
+ # @yield [http]
# If a block is given, it will be passed the newly created HTTP
# session object.
#
- # @yieldparam [Net::HTTP] session
+ # @yieldparam [Net::HTTP] http
# The newly created HTTP session.
#
# @return [Net::HTTP]
# The HTTP session object.
#
# @api public
#
def http_connect(options={},&block)
options = HTTP.expand_options(options)
- host = options[:host].to_s
- port = options[:port]
+ host = options[:host].to_s
+ port = options[:port]
proxy = options[:proxy]
proxy_host = if (proxy && proxy[:host])
proxy[:host].to_s
end
- sess = Net::HTTP::Proxy(
+ http = Net::HTTP::Proxy(
proxy_host,
proxy[:port],
proxy[:user],
proxy[:password]
- ).new(host.to_s,port)
+ ).new(host,port)
if options[:ssl]
- sess.use_ssl = true
- sess.verify_mode = SSL::VERIFY[options[:ssl][:verify]]
+ http.use_ssl = true
+ http.verify_mode = SSL::VERIFY[options[:ssl][:verify]]
end
- sess.start()
+ http.start()
if block
if block.arity == 2
- block.call(sess,options)
+ block.call(http,options)
else
- block.call(sess)
+ block.call(http)
end
end
- return sess
+ return http
end
#
# Creates a new temporary HTTP session with the server.
#
@@ -427,34 +446,34 @@
# Enables SSL for the HTTP connection.
#
# @option :ssl [Symbol] :verify
# Specifies the SSL certificate verification mode.
#
- # @yield [session]
+ # @yield [http]
# If a block is given, it will be passed the newly created HTTP
# session object.
#
- # @yieldparam [Net::HTTP] session
+ # @yieldparam [Net::HTTP] http
# The newly created HTTP session.
#
# @return [nil]
#
# @see http_connect
#
# @api public
#
def http_session(options={},&block)
- http_connect(options) do |sess,expanded_options|
+ http_connect(options) do |http,expanded_options|
if block
if block.arity == 2
- block.call(sess,expanded_options)
+ block.call(http,expanded_options)
else
- block.call(sess)
+ block.call(http)
end
end
- sess.finish
+ http.finish
end
return nil
end
@@ -497,11 +516,11 @@
# @see http_session
#
# @api public
#
def http_request(options={},&block)
- resp = nil
+ response = nil
http_session(options) do |http,expanded_options|
req = HTTP.request(expanded_options)
if block
@@ -510,14 +529,14 @@
else
block.call(req)
end
end
- resp = http.request(req)
+ response = http.request(req)
end
- return resp
+ return response
end
#
# Returns the Status Code of the Response.
#
@@ -625,14 +644,14 @@
# @see http_request
#
# @api public
#
def http_copy(options={})
- resp = http_request(options.merge(:method => :copy))
+ response = http_request(options.merge(:method => :copy))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Delete request.
#
@@ -661,14 +680,14 @@
if original_headers
options[:header].merge!(original_headers)
end
- resp = http_request(options.merge(:method => :delete))
+ response = http_request(options.merge(:method => :delete))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Get request.
#
@@ -688,14 +707,14 @@
# @see http_request
#
# @api public
#
def http_get(options={},&block)
- resp = http_request(options.merge(:method => :get))
+ response = http_request(options.merge(:method => :get))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Get request and returns the Response Headers.
#
@@ -710,11 +729,17 @@
# @since 0.2.0
#
# @api public
#
def http_get_headers(options={})
- http_get(options).to_hash
+ headers = {}
+
+ http_get(options).each_header do |name,value|
+ headers[HTTP.header_name(name)] = value
+ end
+
+ return headers
end
#
# Performs an HTTP Get request and returns the Respond Body.
#
@@ -751,14 +776,14 @@
# @see http_request
#
# @api public
#
def http_head(options={},&block)
- resp = http_request(options.merge(:method => :head))
+ response = http_request(options.merge(:method => :head))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Lock request.
#
@@ -778,14 +803,14 @@
# @see http_request
#
# @api public
#
def http_lock(options={},&block)
- resp = http_request(options.merge(:method => :lock))
+ response = http_request(options.merge(:method => :lock))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Mkcol request.
#
@@ -805,14 +830,14 @@
# @see http_request
#
# @api public
#
def http_mkcol(options={},&block)
- resp = http_request(options.merge(:method => :mkcol))
+ response = http_request(options.merge(:method => :mkcol))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Move request.
#
@@ -832,14 +857,14 @@
# @see http_request
#
# @api public
#
def http_move(options={},&block)
- resp = http_request(options.merge(:method => :move))
+ response = http_request(options.merge(:method => :move))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Options request.
#
@@ -859,14 +884,14 @@
# @see http_request
#
# @api public
#
def http_options(options={},&block)
- resp = http_request(options.merge(:method => :options))
+ response = http_request(options.merge(:method => :options))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Post request.
#
@@ -889,14 +914,14 @@
# @see http_request
#
# @api public
#
def http_post(options={},&block)
- resp = http_request(options.merge(:method => :post))
+ response = http_request(options.merge(:method => :post))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Post request and returns the Response Headers.
#
@@ -914,11 +939,17 @@
# @since 0.2.0
#
# @api public
#
def http_post_headers(options={})
- http_post(options).to_hash
+ headers = {}
+
+ http_post(options).each_header do |name,value|
+ headers[HTTP.header_name(name)] = value
+ end
+
+ return headers
end
#
# Performs an HTTP Post request and returns the Response Body.
#
@@ -967,14 +998,14 @@
if original_headers
options[:header].merge!(original_headers)
end
- resp = http_request(options.merge(:method => :propfind))
+ response = http_request(options.merge(:method => :propfind))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Proppatch request.
#
@@ -994,14 +1025,14 @@
# @see http_request
#
# @api public
#
def http_prop_patch(options={},&block)
- resp = http_request(options.merge(:method => :proppatch))
+ response = http_request(options.merge(:method => :proppatch))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Trace request.
#
@@ -1021,14 +1052,14 @@
# @see http_request
#
# @api public
#
def http_trace(options={},&block)
- resp = http_request(options.merge(:method => :trace))
+ response = http_request(options.merge(:method => :trace))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
#
# Performs an HTTP Unlock request.
#
@@ -1048,13 +1079,13 @@
# @see http_request
#
# @api public
#
def http_unlock(options={},&block)
- resp = http_request(options.merge(:method => :unlock))
+ response = http_request(options.merge(:method => :unlock))
- yield resp if block_given?
- return resp
+ yield response if block_given?
+ return response
end
end
end
end