Module: EventMachine::HttpEncoding
- Included in:
- HttpClient
- Defined in:
- lib/em-http/client.rb
Overview
Methods for building HTTP requests
Constant Summary collapse
- HTTP_REQUEST_HEADER =
"%s %s HTTP/1.1\r\n"- FIELD_ENCODING =
"%s: %s\r\n"- BASIC_AUTH_ENCODING =
"%s: Basic %s\r\n"
Instance Method Summary collapse
-
#encode_basic_auth(k, v) ⇒ Object
Encode basic auth in an HTTP header.
- #encode_body(body) ⇒ Object
- #encode_cookie(cookie) ⇒ Object
-
#encode_field(k, v) ⇒ Object
Encode a field in an HTTP header.
- #encode_headers(head) ⇒ Object
-
#encode_host ⇒ 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.
-
#encode_param(k, v) ⇒ Object
URL encodes query parameters: single k=v, or a URL encoded array, if v is an array of values.
- #encode_query(path, query) ⇒ Object
- #encode_request(method, path, query) ⇒ Object
-
#escape(s) ⇒ Object
Escapes a URI.
-
#munge_header_keys(head) ⇒ Object
Map all header keys to a downcased string version.
-
#unescape(s) ⇒ Object
Unescapes a URI escaped string.
Instance Method Details
#encode_basic_auth(k, v) ⇒ Object
Encode basic auth in an HTTP header
143 144 145 |
# File 'lib/em-http/client.rb', line 143 def encode_basic_auth(k,v) BASIC_AUTH_ENCODING % [k, Base64.encode64(v.join(":")).chomp] end |
#encode_body(body) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/em-http/client.rb', line 166 def encode_body body params = '' stack = [] body.each do |k, v| if v.is_a? Hash stack << [k, v] elsif v.is_a? Array stack << [k, Hash.from_array(v)] else params << encode_param(k, v) << '&' end end stack.each do |parent, hash| hash.each do |k, v| if v.is_a? Hash stack << ["#{parent}[#{k}]", v] else params << encode_param("#{parent}[#{k}]", v) << '&' end end end params.chop! # trailing & params end |
#encode_cookie(cookie) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/em-http/client.rb', line 159 def () if .is_a? Hash .inject('') { |result, (k, v)| result << encode_param(k, v) + ";" } else end end |
#encode_field(k, v) ⇒ Object
Encode a field in an HTTP header
138 139 140 |
# File 'lib/em-http/client.rb', line 138 def encode_field(k, v) FIELD_ENCODING % [k, v] end |
#encode_headers(head) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/em-http/client.rb', line 147 def encode_headers(head) head.inject('') do |result, (key, value)| # Munge keys from foo-bar-baz to Foo-Bar-Baz key = key.split('-').map { |k| k.capitalize }.join('-') unless key == "Authorization" result << encode_field(key, value) else result << encode_basic_auth(key, value) end end end |
#encode_host ⇒ 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.
106 107 108 |
# File 'lib/em-http/client.rb', line 106 def encode_host @uri.host + (@uri.port != @uri.default_port ? ":#{@uri.port}" : "") end |
#encode_param(k, v) ⇒ Object
URL encodes query parameters: single k=v, or a URL encoded array, if v is an array of values
129 130 131 132 133 134 135 |
# File 'lib/em-http/client.rb', line 129 def encode_param(k, v) if v.is_a?(Array) v.map { |e| escape(k) + "[]=" + escape(e) }.join("&") else escape(k) + "=" + escape(v) end end |
#encode_query(path, query) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/em-http/client.rb', line 114 def encode_query(path, query) encoded_query = if query.kind_of?(Hash) query.map { |k, v| encode_param(k, v) }.join('&') else query.to_s end if !@uri.query.to_s.empty? encoded_query = [encoded_query, @uri.query].reject {|part| part.empty?}.join("&") end return path if encoded_query.to_s.empty? "#{path}?#{encoded_query}" end |
#encode_request(method, path, query) ⇒ Object
110 111 112 |
# File 'lib/em-http/client.rb', line 110 def encode_request(method, path, query) HTTP_REQUEST_HEADER % [method.to_s.upcase, encode_query(path, query)] end |
#escape(s) ⇒ Object
Escapes a URI.
85 86 87 88 89 |
# File 'lib/em-http/client.rb', line 85 def escape(s) s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) { '%'+$1.unpack('H2'*$1.size).join('%').upcase }.tr(' ', '+') end |
#munge_header_keys(head) ⇒ Object
Map all header keys to a downcased string version
99 100 101 |
# File 'lib/em-http/client.rb', line 99 def munge_header_keys(head) head.inject({}) { |h, (k, v)| h[k.to_s.downcase] = v; h } end |
#unescape(s) ⇒ Object
Unescapes a URI escaped string.
92 93 94 95 96 |
# File 'lib/em-http/client.rb', line 92 def unescape(s) s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') } end |