Module: FastHttp::HttpEncoding

Included in:
HttpClient
Defined in:
lib/fast_http/client.rb

Overview

A mixin that has most of the HTTP encoding methods you need to work with the protocol. It’s used by HttpClient, but you can use it as well.

Constant Summary collapse

"Cookie"
FIELD_ENCODING =
"%s: %s\r\n"

Instance Method Summary collapse

Instance Method Details

#encode_cookies(cookies) ⇒ Object

Converts a Hash of cookies to the appropriate simple cookie headers.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fast_http/client.rb', line 60

def encode_cookies(cookies)
  result = ""
  cookies.each do |k,v|
    if v.kind_of? Array
      v.each {|x| result << encode_field(COOKIE, encode_param(k,x)) }
    else
      result << encode_field(COOKIE, encode_param(k,v))
    end
  end
  return result
end

#encode_field(k, v) ⇒ Object

Encode HTTP header fields of “k: vrn”



73
74
75
# File 'lib/fast_http/client.rb', line 73

def encode_field(k,v)
  FIELD_ENCODING % [k,v]
end

#encode_headers(head) ⇒ Object

Encodes the headers given in the hash returning a string you can use.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fast_http/client.rb', line 79

def encode_headers(head)
  @headers_cache = {}
  @headers_cache[head] || 
  result = "" 
  head.each do |k,v|
    if v.kind_of? Array
      v.each {|x| result << encode_field(k,x) }
    else
      result << encode_field(k,v)
    end
  end
  return result
end

#encode_host(host, port) ⇒ Object

HTTP is kind of retarded that you have to specify a Host header, but if you include port 80 then further redirects will tack on the :80 which is annoying.



121
122
123
# File 'lib/fast_http/client.rb', line 121

def encode_host(host, port)
  host + (port.to_i != 80 ? ":#{port}" : "")
end

#encode_param(k, v) ⇒ Object

URL encodes a single k=v parameter.



94
95
96
# File 'lib/fast_http/client.rb', line 94

def encode_param(k,v)
  escape(k) + "=" + escape(v)
end

#encode_query(uri, query) ⇒ Object

Takes a query string and encodes it as a URL encoded set of key=value pairs with & separating them.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fast_http/client.rb', line 100

def encode_query(uri, query)
  params = []

  if query
    query.each do |k,v|
      if v.kind_of? Array
        v.each {|x| params << encode_param(k,x) } 
      else
        params << encode_param(k,v)
      end
    end

    uri += "?" + params.join('&')
  end

  return uri
end

#escape(s) ⇒ Object

Escapes a URI.



126
127
128
129
130
# File 'lib/fast_http/client.rb', line 126

def escape(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
    '%'+$1.unpack('H2'*$1.size).join('%').upcase
  }.tr(' ', '+') 
end

#query_parse(qs, d = '&;') ⇒ Object

Parses a query string by breaking it up at the ‘&’ and ‘;’ characters. You can also use this to parse cookies by changing the characters used in the second parameter (which defaults to ‘&;’.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fast_http/client.rb', line 144

def query_parse(qs, d = '&;')
  params = {}
  (qs||'').split(/[#{d}] */n).inject(params) { |h,p|
    k, v=unescape(p).split('=',2)
    if cur = params[k]
      if cur.class == Array
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  }

  return params
end

#unescape(s) ⇒ Object

Unescapes a URI escaped string.



134
135
136
137
138
# File 'lib/fast_http/client.rb', line 134

def unescape(s)
  s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
    [$1.delete('%')].pack('H*')
  } 
end