lib/ronin/network/http/http.rb in ronin-support-0.1.0 vs lib/ronin/network/http/http.rb in ronin-support-0.2.0.rc1

- old
+ new

@@ -35,10 +35,12 @@ # The Ronin HTTP proxy. # # @see Proxy.new # @see Proxy.parse # + # @api public + # def HTTP.proxy @proxy ||= if ENV['HTTP_PROXY'] Proxy.parse(ENV['HTTP_PROXY']) else Proxy.new @@ -56,70 +58,77 @@ # # @raise [ArgumentError] # The given proxy information was not a {Proxy}, `URI::HTTP`, # `Hash` or {String}. # + # @api public + # def HTTP.proxy=(new_proxy) @proxy = Proxy.create(new_proxy) end # # The default Ronin HTTP User-Agent string. # # @return [String, nil] # The default Ronin HTTP User-Agent. # + # @api public + # def HTTP.user_agent @user_agent ||= nil end # # Sets the default Ronin HTTP User-Agent string. # # @param [String] agent # The new User-Agent string to use. # + # @api public + # def HTTP.user_agent=(agent) @user_agent = agent end # # Expands the URL into options. # - # @param [URI::HTTP, String, nil] url + # @param [URI::HTTP, String] url # The URL to expand. # # @return [Hash{Symbol => Object}] # The options for the URL. # + # @api private + # def HTTP.expand_url(url) - new_options = { - :port => Net::HTTP.default_port, - :path => '/' - } + new_options = {} - if url - url = case url - when URI - url - when Hash - URI::HTTP.build(url) - else - URI(url.to_s) - end + url = case url + when URI + url + when Hash + URI::HTTP.build(url) + else + URI(url.to_s) + end - new_options[:ssl] = {} if url.scheme == 'https' + new_options[:ssl] = {} if url.scheme == 'https' - new_options[:host] = url.host - new_options[:port] = url.port + new_options[:host] = url.host + new_options[:port] = url.port - new_options[:user] = url.user if url.user - new_options[:password] = url.password if url.password + new_options[:user] = url.user if url.user + new_options[:password] = url.password if url.password - new_options[:path] = url.path unless url.path.empty? - new_options[:path] += "?#{url.query}" if url.query - end + new_options[:path] = unless url.path.empty? + url.path + else + '/' + end + new_options[:path] += "?#{url.query}" if url.query return new_options end # @@ -150,19 +159,25 @@ # The Proxy information. # # @return [Hash] # The expanded version of options. # + # @api private + # def HTTP.expand_options(options={}) new_options = options.dup + new_options[:port] ||= Net::HTTP.default_port + new_options[:path] ||= '/' + if new_options[:ssl] == true new_options[:ssl] = {} end - url = new_options.delete(:url) - new_options.merge!(HTTP.expand_url(url)) + if (url = new_options.delete(:url)) + new_options.merge!(HTTP.expand_url(url)) + end new_options[:proxy] = if new_options.has_key?(:proxy) HTTP::Proxy.create(new_options[:proxy]) else HTTP.proxy @@ -179,10 +194,12 @@ # The unformatted HTTP header name. # # @return [String] # The camel-case HTTP header name. # + # @api private + # def HTTP.header_name(name) words = name.to_s.split(/[\s+_-]/) words.each { |word| word.capitalize! } return words.join('-') @@ -196,10 +213,12 @@ # Ronin HTTP headers. # # @return [Hash] # The camel-cased HTTP headers created from the given options. # + # @api private + # def HTTP.headers(options={}) headers = {} if HTTP.user_agent headers['User-Agent'] = HTTP.user_agent @@ -250,9 +269,11 @@ # @raise [UnknownRequest] # The `:method` option did not match a known Net::HTTP request # class. # # @see HTTP.expand_options + # + # @api private # def HTTP.request(options={}) unless options[:method] raise(ArgumentError,"the :method option must be specified") end