# class GxG::Networking::HttpClient

### Public Class Methods

new() click to toggle source

    # File lib/gxg/net_clients.rb, line 5228
    def initialize()
      @cookie = nil
      self
    end

### Public Instance Methods

clear_cache() click to toggle source

    # File lib/gxg/net_clients.rb, line 5233
    def clear_cache()
      @cookie = nil
      true
    end

cookies() click to toggle source

    # File lib/gxg/net_clients.rb, line 5238
    def cookies()
      @cookie.clone
    end

delete(the_url=nil, options={}) click to toggle source

    # File lib/gxg/net_clients.rb, line 5635
    def delete(the_url=nil, options={})
      result = false
      begin
        unless options.is_a?(::Hash)
          options = {}
        end
        if the_url.scheme.downcase == "https"
          options[:use_ssl] = true
        end
        if the_url.is_any?(::URI::HTTP, ::URI::HTTPS)
          client_parameters = [(the_url.hostname), (the_url.port)]
          if options[:proxy].is_a?(::URI::Generic)
            client_parameters << options[:proxy].host
            client_parameters << options[:proxy].port
            if options[:proxy].user && options[:proxy].password
              client_parameters << options[:proxy].user
              client_parameters << options[:proxy].password
            else
              client_parameters << nil
              client_parameters << nil
            end
          else
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
          end
          if options.keys.size > 0
            client_parameters << options
          end
          server = Net::HTTP.start(*client_parameters)
          if options[:pem_file].is_a?(::String)
            pem_file = ::File.read(options[:pem_file])
            server.use_ssl = true
            server.cert = OpenSSL::X509::Certificate.new(pem_file)
            server.key = OpenSSL::PKey::RSA.new(pem_file, options[:pem_password])
            server.verify_mode = OpenSSL::SSL::VERIFY_PEER
          end
          if @cookie
            request = Net::HTTP::Delete.new(the_url.request_uri, {'Cookie' => @cookie})
          else
            request = Net::HTTP::Delete.new(the_url.request_uri)
          end
          if (the_url.user && the_url.password)
            # basic auth
            request.basic_auth(the_url.user, the_url.password)
          end
          response = server.request(request)
          response_code = response.code.to_i
          if options[:follow_redirects]
            if response_code >= 300 && response_code < 400
              # Process Redirect
              redirect_count = 1
              location = response['location']
              while location do
                #
                if @cookie
                  redirect_request = Net::HTTP::Delete.new(location, {'Cookie' => @cookie})
                else
                  redirect_request = Net::HTTP::Delete.new(location)
                end
                redirect_response = server.request(redirect_request)
                if redirect_response.code.to_i == 200
                  response = redirect_response
                  response_code = 200
                  break
                else
                  if redirect_response['location']
                    redirect_count += 1
                    if redirect_count > 9
                      raise Exception, "Circular redirect chain suspected (10 or more redirects)"
                    end
                    response_code = redirect_response.code.to_i
                    location = redirect_response['location']
                  else
                    break
                  end
                end
                #
              end
            end
          end
          if response.code.to_i == 200
            result = true
          else
            raise Exception, "Error occured: Code #{response.code}"
          end
        else
          raise Exception, "You MUST provide a valid HTTP URI"
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
      end
      result
    end

get(the_url=nil, options={:follow_redirects => true}) click to toggle source

    # File lib/gxg/net_clients.rb, line 5242
    def get(the_url=nil, options={:follow_redirects => true})
      result = nil
      begin
        if the_url.is_any?(::URI::HTTP, ::URI::HTTPS)
          the_url = the_url.dup
          if options[:query].is_a?(::Hash)
            the_url.query = ::URI.encode_www_form(options[:query])
          end
          unless options[:follow_redirects] == false
            options.merge({:follow_redirects => true})
          end
          #
          if the_url.scheme.downcase == "https"
            options[:use_ssl] = true
          end
          # sets following values by its accessor.
          # The keys are ca_file, ca_path, cert, cert_store, ciphers,
          # close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout,
          # ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.
          # If you set :use_ssl as true, you can use https and default value of
          # verify_mode is set as OpenSSL::SSL::VERIFY_PEER.
          client_parameters = [(the_url.hostname), (the_url.port)]
          if options[:proxy].is_a?(::URI::Generic)
            client_parameters << options[:proxy].host
            client_parameters << options[:proxy].port
            if options[:proxy].user && options[:proxy].password
              client_parameters << options[:proxy].user
              client_parameters << options[:proxy].password
            else
              client_parameters << nil
              client_parameters << nil
            end
          else
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
          end
          if options.keys.size > 0
            client_parameters << options
          end
          client = Net::HTTP.start(*client_parameters)
          if options[:pem_file].is_a?(::String)
            pem_file = ::File.read(options[:pem_file])
            client.use_ssl = true
            client.cert = OpenSSL::X509::Certificate.new(pem_file)
            client.key = OpenSSL::PKey::RSA.new(pem_file, options[:pem_password])
            client.verify_mode = OpenSSL::SSL::VERIFY_PEER
          end
          if @cookie
            if options[:header].is_a?(::Hash)
              request = Net::HTTP::Get.new(the_url.request_uri, {'Cookie' => @cookie}.merge(options[:header]))
            else
              request = Net::HTTP::Get.new(the_url.request_uri, {'Cookie' => @cookie})
            end
          else
            if options[:header].is_a?(::Hash)
              request = Net::HTTP::Get.new(the_url.request_uri, options[:header])
            else
              request = Net::HTTP::Get.new(the_url.request_uri)
            end
          end
          if (the_url.user && the_url.password)
            # basic auth
            request.basic_auth(the_url.user, the_url.password)
          end
          response = client.request(request)
          @cookie = response.response['set-cookie']
          response_code = response.code.to_i
          if options[:follow_redirects]
            if response_code >= 300 && response_code < 400
              # Process Redirect
              redirect_count = 1
              location = response['location']
              while location do
                #
                if @cookie
                  redirect_request = Net::HTTP::Get.new(location, {'Cookie' => @cookie})
                else
                  redirect_request = Net::HTTP::Get.new(location)
                end
                redirect_response = client.request(redirect_request)
                if redirect_response.code.to_i == 200
                  response = redirect_response
                  response_code = 200
                  @cookie = response.response['set-cookie']
                  break
                else
                  if redirect_response['location']
                    @cookie = redirect_response.response['set-cookie']
                    redirect_count += 1
                    if redirect_count > 9
                      raise Exception, "Circular redirect chain suspected (10 or more redirects)"
                    end
                    response_code = redirect_response.code.to_i
                    location = redirect_response['location']
                  else
                    break
                  end
                end
                #
              end
            end
          end
          if options[:save_as].is_a?(::String)
            ::File.open(options[:save_as].to_s, 'w', 0644) do |the_io|
              response.read_body do |data_chunk|
                the_io.write(data_chunk)
              end
              # Return open file handle r/w to saved file.
              result = ::File.open(the_io.path, "w+")
            end
          end
          if options[:stream_to].is_a?(::GxG::Database::PersistedArray)
            the_accumulator = options[:stream_to]
            buffer = ::GxG::ByteArray.new
            response.read_body do |data_chunk|
              chunk_ranges = ::GxG.apportioned_ranges(data_chunk.bytesize,65536)
              chunk_ranges.each do |the_range|
                if (buffer.size + data_chunk[(the_range)].bytesize) <= 65536
                  buffer << data_chunk[(the_range)]
                  if buffer.size == 65536
                    the_accumulator << buffer.clone
                    the_accumulator[(the_accumulator.size - 1)].unload
                    buffer.clear
                  end
                else
                  the_size = buffer.size
                  buffer << data_chunk[(the_range)][(0..((65536 - the_size) - 1))]
                  if buffer.size == 65536
                    the_accumulator << buffer.clone
                    the_accumulator[(the_accumulator.size - 1)].unload
                    buffer.clear
                  end
                  buffer << data_chunk[(the_range)][((65536 - the_size)..-1)]
                end
              end
              #
            end
            if buffer.size > 0
              the_accumulator << buffer.clone
              the_accumulator[(the_accumulator.size - 1)].unload
            end
          end
          if response_code == 200 && options[:save_as] == nil && options[:stream_to] == nil
            # Process response
            if options[:raw_response]
              result = response
            else
              if options[:to_ruby] == true
                response_hash = {}
                response_hash = response.to_hash.process do |entry, the_key|
                  if the_key.is_a?(::Symbol)
                    entry
                  else
                    returns({:new_selector => (the_key.to_s.to_sym), :result => (entry)})
                  end
                end
                response_hash[:header] = response.header
                #
                body_text = (response.body.transcode({:replace => "."},::Encoding::UTF_8))
                if body_text.xml?()
                  response_hash[:body] = body_text.from_xml()
                else
                  if body_text.html?()
                    response_hash[:body] = body_text.from_html()
                  else
                    raise Exception, "Failed to retrieve either XML or HTML as data."
                  end
                end
                #
                result = response_hash
              else
                # Default output:
                result = response.body
              end
            end
          else
            unless options[:save_as]
              raise Exception, "Error occured: Code #{response.code}"
            end
          end
        else
          raise Exception, "You MUST provide a valid HTTP URI"
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
      end
      result
    end

post(the_url=nil, form_data=nil, options={}) click to toggle source

    # File lib/gxg/net_clients.rb, line 5534
    def post(the_url=nil, form_data=nil, options={})
      result = false
      begin
        unless options.is_a?(::Hash)
          options = {}
        end
        if the_url.scheme.downcase == "https"
          options[:use_ssl] = true
        end
        if the_url.is_any?(::URI::HTTP, ::URI::HTTPS)
          unless form_data.is_a?(::Hash)
            raise Exception, "You MUST provide valid form data (in the form of a Hash)"
          end
          client_parameters = [(the_url.hostname), (the_url.port)]
          if options[:proxy].is_a?(::URI::Generic)
            client_parameters << options[:proxy].host
            client_parameters << options[:proxy].port
            if options[:proxy].user && options[:proxy].password
              client_parameters << options[:proxy].user
              client_parameters << options[:proxy].password
            else
              client_parameters << nil
              client_parameters << nil
            end
          else
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
          end
          if options.keys.size > 0
            client_parameters << options
          end
          server = Net::HTTP.start(*client_parameters)
          if options[:pem_file].is_a?(::String)
            pem_file = ::File.read(options[:pem_file])
            server.use_ssl = true
            server.cert = OpenSSL::X509::Certificate.new(pem_file)
            server.key = OpenSSL::PKey::RSA.new(pem_file, options[:pem_password])
            server.verify_mode = OpenSSL::SSL::VERIFY_PEER
          end
          if @cookie
            request = Net::HTTP::Post.new(the_url.request_uri, {'Cookie' => @cookie})
          else
            request = Net::HTTP::Post.new(the_url.request_uri)
          end
          if (the_url.user && the_url.password)
            # basic auth
            request.basic_auth(the_url.user, the_url.password)
          end
          request.set_form_data(form_data)
          response = server.request(request)
          response_code = response.code.to_i
          if options[:follow_redirects]
            if response_code >= 300 && response_code < 400
              # Process Redirect
              redirect_count = 1
              location = response['location']
              while location do
                #
                if @cookie
                  redirect_request = Net::HTTP::Post.new(location, {'Cookie' => @cookie})
                else
                  redirect_request = Net::HTTP::Post.new(location)
                end
                redirect_request.set_form_data(form_data)
                redirect_response = server.request(redirect_request)
                if redirect_response.code.to_i == 200
                  response = redirect_response
                  response_code = 200
                  break
                else
                  if redirect_response['location']
                    redirect_count += 1
                    if redirect_count > 9
                      raise Exception, "Circular redirect chain suspected (10 or more redirects)"
                    end
                    response_code = redirect_response.code.to_i
                    location = redirect_response['location']
                  else
                    break
                  end
                end
                #
              end
            end
          end
          if response.code.to_i == 200
            result = true
          else
            raise Exception, "Error occured: Code #{response.code}"
          end
        else
          raise Exception, "You MUST provide a valid HTTP URI"
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:url => the_url, :form_data => form_data, :options => options}})
      end
      result
    end

put(the_url=nil, data=nil, options={}) click to toggle source

    # File lib/gxg/net_clients.rb, line 5433
    def put(the_url=nil, data=nil, options={})
      #
      result = false
      begin
        unless options.is_a?(::Hash)
          options = {}
        end
        if the_url.scheme.downcase == "https"
          options[:use_ssl] = true
        end
        if the_url.is_any?(::URI::HTTP, ::URI::HTTPS)
          unless data.is_a?(::String)
            raise Exception, "You MUST provide valid PUT data (in the form of a String: 'parameter = value')"
          end
          client_parameters = [(the_url.hostname), (the_url.port)]
          if options[:proxy].is_a?(::URI::Generic)
            client_parameters << options[:proxy].host
            client_parameters << options[:proxy].port
            if options[:proxy].user && options[:proxy].password
              client_parameters << options[:proxy].user
              client_parameters << options[:proxy].password
            else
              client_parameters << nil
              client_parameters << nil
            end
          else
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
            client_parameters << nil
          end
          if options.keys.size > 0
            client_parameters << options
          end
          server = Net::HTTP.start(*client_parameters)
          if options[:pem_file].is_a?(::String)
            pem_file = ::File.read(options[:pem_file])
            server.use_ssl = true
            server.cert = OpenSSL::X509::Certificate.new(pem_file)
            server.key = OpenSSL::PKey::RSA.new(pem_file, options[:pem_password])
            server.verify_mode = OpenSSL::SSL::VERIFY_PEER
          end
          #
          if @cookie
            request = Net::HTTP::Put.new(the_url.request_uri,data, {'Cookie' => @cookie})
          else
            request = Net::HTTP::Put.new(the_url.request_uri,data)
          end
          if (the_url.user && the_url.password)
            # basic auth
            request.basic_auth(the_url.user, the_url.password)
          end
          response = server.request(request)
          response_code = response.code.to_i
          if options[:follow_redirects]
            if response_code >= 300 && response_code < 400
              # Process Redirect
              redirect_count = 1
              location = response['location']
              while location do
                #
                if @cookie
                  redirect_request = Net::HTTP::Put.new(location, {'Cookie' => @cookie})
                else
                  redirect_request = Net::HTTP::Put.new(location)
                end
                redirect_response = server.request(redirect_request)
                if redirect_response.code.to_i == 200
                  response = redirect_response
                  response_code = 200
                  break
                else
                  if redirect_response['location']
                    redirect_count += 1
                    if redirect_count > 9
                      raise Exception, "Circular redirect chain suspected (10 or more redirects)"
                    end
                    response_code = redirect_response.code.to_i
                    location = redirect_response['location']
                  else
                    break
                  end
                end
                #
              end
            end
          end
          if response.code.to_i == 200
            result = true
          else
            raise Exception, "Error occured: Code #{response.code}"
          end
        else
          raise Exception, "You MUST provide a valid HTTP URI"
        end
      rescue Exception => the_error
        log_error({:error => the_error, :parameters => {:url => the_url, :form_data => data, :options => options}})
      end
      result
    end