class HTTP::Request
def connect_using_proxy(socket)
def connect_using_proxy(socket) Request::Writer.new(socket, nil, proxy_connect_headers, proxy_connect_header).connect_through_proxy end
def default_host_header_value
-
(String)
- Default host (with port if needed) header value.
def default_host_header_value PORTS[@scheme] != port ? "#{host}:#{port}" : host end
def headline
def headline request_uri = if using_proxy? && !uri.https? uri.omit(:fragment) else uri.omit(:scheme, :authority, :fragment) end "#{verb.to_s.upcase} #{request_uri} HTTP/#{version}" end
def include_proxy_authorization_header
def include_proxy_authorization_header headers[Headers::PROXY_AUTHORIZATION] = proxy_authorization_header end
def include_proxy_headers
def include_proxy_headers headers.merge!(proxy[:proxy_headers]) if proxy.key?(:proxy_headers) include_proxy_authorization_header if using_authenticated_proxy? end
def initialize(opts)
(**opts)
-
:body
(String, Enumerable, IO, nil
) -- -
:proxy
(Hash
) -- -
:headers
(Hash
) -- -
:uri
(HTTP::URI, #to_s
) -- -
:verb
(#to_s
) -- HTTP request method -
:version
(String
) --
def initialize(opts) @verb = opts.fetch(:verb).to_s.downcase.to_sym @uri = normalize_uri(opts.fetch(:uri)) @scheme = @uri.scheme.to_s.downcase.to_sym if @uri.scheme raise(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb) raise(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme) @proxy = opts[:proxy] || {} @body = request_body(opts[:body], opts) @version = opts[:version] || "1.1" @headers = HTTP::Headers.coerce(opts[:headers] || {}) @headers[Headers::HOST] ||= default_host_header_value @headers[Headers::USER_AGENT] ||= USER_AGENT end
def normalize_uri(uri)
-
(HTTP::URI)
- URI with all componentes but query being normalized.
def normalize_uri(uri) uri = HTTP::URI.parse uri HTTP::URI.new( :scheme => uri.normalized_scheme, :authority => uri.normalized_authority, :path => uri.normalized_path, :query => uri.query, :fragment => uri.normalized_fragment ) end
def port
-
(Fixnum)
-
def port @uri.port || @uri.default_port end
def proxy_authorization_header
def proxy_authorization_header digest = Base64.strict_encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}") "Basic #{digest}" end
def proxy_connect_header
def proxy_connect_header "CONNECT #{host}:#{port} HTTP/#{version}" end
def proxy_connect_headers
def proxy_connect_headers connect_headers = HTTP::Headers.coerce( Headers::HOST => headers[Headers::HOST], Headers::USER_AGENT => headers[Headers::USER_AGENT] ) connect_headers[Headers::PROXY_AUTHORIZATION] = proxy_authorization_header if using_authenticated_proxy? connect_headers.merge!(proxy[:proxy_headers]) if proxy.key?(:proxy_headers) connect_headers end
def redirect(uri, verb = @verb)
def redirect(uri, verb = @verb) headers = self.headers.dup headers.delete(Headers::HOST) self.class.new( :verb => verb, :uri => @uri.join(uri), :headers => headers, :proxy => proxy, :body => body, :version => version ) end
def request_body(body, opts)
def request_body(body, opts) body = Request::Body.new(body) unless body.is_a?(Request::Body) body = opts[:auto_deflate].deflated_body(body) if opts[:auto_deflate] body end
def socket_host
def socket_host using_proxy? ? proxy[:proxy_address] : host end
def socket_port
def socket_port using_proxy? ? proxy[:proxy_port] : port end
def stream(socket)
def stream(socket) include_proxy_headers if using_proxy? && !@uri.https? Request::Writer.new(socket, body, headers, headline).stream end
def using_authenticated_proxy?
def using_authenticated_proxy? proxy && proxy.keys.size >= 4 end
def using_proxy?
def using_proxy? proxy && proxy.keys.size >= 2 end