Module: EventedNet::HTTP::HttpEncoding
- Included in:
- Connection
- Defined in:
- lib/http/connection.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"
Instance Method Summary collapse
- #encode_cookies(cookies) ⇒ 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 a single k=v parameter.
- #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_cookies(cookies) ⇒ Object
100 101 102 |
# File 'lib/http/connection.rb', line 100 def () .inject('') { |result, (k, v)| result << encode_field('Cookie', encode_param(k, v)) } end |
#encode_field(k, v) ⇒ Object
Encode a field in an HTTP header
88 89 90 |
# File 'lib/http/connection.rb', line 88 def encode_field(k, v) FIELD_ENCODING % [k, v] end |
#encode_headers(head) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/http/connection.rb', line 92 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 |
#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.
69 70 71 |
# File 'lib/http/connection.rb', line 69 def encode_host remote_host + (remote_port.to_i != 80 ? ":#{remote_port}" : "") end |
#encode_param(k, v) ⇒ Object
URL encodes a single k=v parameter.
83 84 85 |
# File 'lib/http/connection.rb', line 83 def encode_param(k, v) escape(k) + "=" + escape(v) end |
#encode_query(path, query) ⇒ Object
77 78 79 80 |
# File 'lib/http/connection.rb', line 77 def encode_query(path, query) return path unless query path + "?" + query.map { |k, v| encode_param(k, v) }.join('&') end |
#encode_request(method, path, query) ⇒ Object
73 74 75 |
# File 'lib/http/connection.rb', line 73 def encode_request(method, path, query) HTTP_REQUEST_HEADER % [method.to_s.upcase, encode_query(path, query)] end |
#escape(s) ⇒ Object
Escapes a URI.
48 49 50 51 52 |
# File 'lib/http/connection.rb', line 48 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
62 63 64 |
# File 'lib/http/connection.rb', line 62 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.
55 56 57 58 59 |
# File 'lib/http/connection.rb', line 55 def unescape(s) s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') } end |