Module: Excon::Utils

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

Constant Summary collapse

CONTROL =
(0x0..0x1f).map {|c| c.chr }.join + "\x7f"
DELIMS =
'<>#%"'
UNWISE =
'{}|\\^[]`'
NONASCII =
(0x80..0xff).map {|c| c.chr }.join
UNESCAPED =
/([#{ Regexp.escape(CONTROL + ' ' + DELIMS + UNWISE + NONASCII) }])/
ESCAPED =
/%([0-9a-fA-F]{2})/

Instance Method Summary collapse

Instance Method Details

#binary_encode(string) ⇒ Object


13
14
15
16
17
# File 'lib/excon/utils.rb', line 13

def binary_encode(string)
  if FORCE_ENC && string.encoding != Encoding::ASCII_8BIT
    string.force_encoding('BINARY')
  end
end

#connection_uri(datum = @data) ⇒ Object


19
20
21
22
23
24
25
26
27
28
# File 'lib/excon/utils.rb', line 19

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

#escape_uri(str) ⇒ Object

Escapes HTTP reserved and unwise characters in str


98
99
100
101
102
# File 'lib/excon/utils.rb', line 98

def escape_uri(str)
  str = str.dup
  binary_encode(str)
  str.gsub(UNESCAPED) { "%%%02X" % $1[0].ord }
end

#port_string(datum) ⇒ Object


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

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


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/excon/utils.rb', line 66

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

#redact(datum) ⇒ Object

Redact sensitive info from provided data


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/excon/utils.rb', line 31

def redact(datum)
  datum = datum.dup
  if datum.has_key?(:headers)
    if datum[:headers].has_key?('Authorization') || datum[:headers].has_key?('Proxy-Authorization')
      datum[:headers] = datum[:headers].dup
    end
    if datum[:headers].has_key?('Authorization')
      datum[:headers]['Authorization'] = REDACTED
    end
    if datum[:headers].has_key?('Proxy-Authorization')
      datum[:headers]['Proxy-Authorization'] = REDACTED
    end
  end
  if datum.has_key?(:password)
    datum[:password] = REDACTED
  end
  if datum.has_key?(:proxy) && datum[:proxy].has_key?(:password)
    datum[:proxy] = datum[:proxy].dup
    datum[:proxy][:password] = REDACTED
  end
  datum
end

#request_uri(datum) ⇒ Object


54
55
56
# File 'lib/excon/utils.rb', line 54

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.


89
90
91
92
93
94
95
# File 'lib/excon/utils.rb', line 89

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

#unescape_form(str) ⇒ Object

Unescape form encoded values in str


112
113
114
115
116
117
# File 'lib/excon/utils.rb', line 112

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

#unescape_uri(str) ⇒ Object

Unescapes HTTP reserved and unwise characters in str


105
106
107
108
109
# File 'lib/excon/utils.rb', line 105

def unescape_uri(str)
  str = str.dup
  binary_encode(str)
  str.gsub(ESCAPED) { $1.hex.chr }
end