Methods for building HTTP requests
Encode a field in an HTTP header
# File lib/cool.io/http_client.rb, line 96 def encode_field(k, v) FIELD_ENCODING % [k, v] end
# File lib/cool.io/http_client.rb, line 100 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('-') result << encode_field(key, value) end end
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.
# File lib/cool.io/http_client.rb, line 77 def encode_host remote_host + (remote_port.to_i != 80 ? ":#{remote_port}" : "") end
URL encodes a single k=v parameter.
# File lib/cool.io/http_client.rb, line 91 def encode_param(k, v) escape(k) + "=" + escape(v) end
# File lib/cool.io/http_client.rb, line 85 def encode_query(path, query) return path unless query path + "?" + query.map { |k, v| encode_param(k, v) }.join('&') end
# File lib/cool.io/http_client.rb, line 81 def encode_request(method, path, query) HTTP_REQUEST_HEADER % [method.to_s.upcase, encode_query(path, query)] end
Escapes a URI.
# File lib/cool.io/http_client.rb, line 56 def escape(s) s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/) { '%'+$1.unpack('H2'*$1.size).join('%').upcase }.tr(' ', '+') end
Generated with the Darkfish Rdoc Generator 2.