Module: Excon::Utils

Extended by:
Utils
Included in:
Connection, Socket, Utils
Defined in:
lib/excon/utils.rb

Constant Summary collapse

ESCAPED =
/%([0-9a-fA-F]{2})/

Instance Method Summary collapse

Instance Method Details

#connection_uri(datum = @data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/excon/utils.rb', line 15

def connection_uri(datum = @data)
  unless datum
    raise ArgumentError, '`datum` must be given unless called on a Connection'
  end
  if datum[:scheme] == UNIX
    '' << datum[:scheme] << '://' << datum[:socket]
  else
    '' << datum[:scheme] << '://' << datum[:host] << port_string(datum)
  end
end

#port_string(datum) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/excon/utils.rb', line 30

def port_string(datum)
  if datum[:port].nil? || (datum[:omit_default_port] && ((datum[:scheme].casecmp('http') == 0 && datum[:port] == 80) || (datum[:scheme].casecmp('https') == 0 && datum[:port] == 443)))
    ''
  else
    ':' << datum[:port].to_s
  end
end

#query_string(datum) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/excon/utils.rb', line 38

def query_string(datum)
  str = ''
  case datum[:query]
  when String
    str << '?' << datum[:query]
  when Hash
    str << '?'
    datum[:query].sort_by {|k,_| k.to_s }.each do |key, values|
      if values.nil?
        str << key.to_s << '&'
      else
        [values].flatten.each do |value|
          str << key.to_s << '=' << CGI.escape(value.to_s) << '&'
        end
      end
    end
    str.chop! # remove trailing '&'
  end
  str
end

#request_uri(datum) ⇒ Object



26
27
28
# File 'lib/excon/utils.rb', line 26

def request_uri(datum)
  connection_uri(datum) << datum[:path] << query_string(datum)
end

#split_header_value(str) ⇒ Object

Splits a header value str according to HTTP specification.



60
61
62
63
64
65
66
# File 'lib/excon/utils.rb', line 60

def split_header_value(str)
  return [] if str.nil?
  str = str.strip
  str.force_encoding('BINARY') if FORCE_ENC
  str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]+)+)
                (?:,\s*|\Z)'xn).flatten
end

#unescape_form(str) ⇒ Object

Unescape form encoded values in str



77
78
79
80
81
82
83
# File 'lib/excon/utils.rb', line 77

def unescape_form(str)
  str = str.dup
  str.force_encoding('BINARY') if FORCE_ENC
  str.gsub!(/\+/, ' ')
  str.gsub!(ESCAPED) { $1.hex.chr }
  str
end

#unescape_uri(str) ⇒ Object

Unescapes HTTP reserved and unwise characters in str



69
70
71
72
73
74
# File 'lib/excon/utils.rb', line 69

def unescape_uri(str)
  str = str.dup
  str.force_encoding('BINARY') if FORCE_ENC
  str.gsub!(ESCAPED) { $1.hex.chr }
  str
end

#valid_connection_keys(params = {}) ⇒ Object



7
8
9
# File 'lib/excon/utils.rb', line 7

def valid_connection_keys(params = {})
  Excon::VALID_CONNECTION_KEYS
end

#valid_request_keys(params = {}) ⇒ Object



11
12
13
# File 'lib/excon/utils.rb', line 11

def valid_request_keys(params = {})
  Excon::VALID_REQUEST_KEYS
end