Module: EventMachine::AblyHttpRequest::HttpEncoding
- Included in:
- HttpClient, Middleware::DigestAuth, Middleware::OAuth
- Defined in:
- lib/em-http/http_encoding.rb
Constant Summary collapse
- HTTP_REQUEST_HEADER =
"%s %s HTTP/1.1\r\n"
- FIELD_ENCODING =
"%s: %s\r\n"
Instance Method Summary collapse
- #bytesize(string) ⇒ Object
-
#encode_auth(k, v) ⇒ Object
Encode basic auth in an HTTP header In: Array ([user, pass]) - for basic auth String - custom auth string (OAuth, etc).
- #encode_cookie(cookie) ⇒ Object
-
#encode_field(k, v) ⇒ Object
Encode a field in an HTTP header.
- #encode_headers(head) ⇒ Object
- #encode_host ⇒ Object
-
#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(uri, query) ⇒ Object
- #encode_request(method, uri, query, connopts) ⇒ Object
- #escape(s) ⇒ Object
- #form_encode_body(obj) ⇒ Object
-
#munge_header_keys(head) ⇒ Object
Map all header keys to a downcased string version.
- #unescape(s) ⇒ Object
Instance Method Details
#bytesize(string) ⇒ Object
28 29 30 |
# File 'lib/em-http/http_encoding.rb', line 28 def bytesize(string) string.bytesize end |
#encode_auth(k, v) ⇒ Object
Encode basic auth in an HTTP header In: Array ([user, pass]) - for basic auth
String - custom auth string (OAuth, etc)
121 122 123 124 125 126 127 |
# File 'lib/em-http/http_encoding.rb', line 121 def encode_auth(k,v) if v.is_a? Array FIELD_ENCODING % [k, ["Basic", Base64.strict_encode64(v.join(":")).split.join].join(" ")] else encode_field(k,v) end end |
#encode_cookie(cookie) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/em-http/http_encoding.rb', line 142 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
114 115 116 |
# File 'lib/em-http/http_encoding.rb', line 114 def encode_field(k, v) FIELD_ENCODING % [k, v] end |
#encode_headers(head) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/em-http/http_encoding.rb', line 129 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.to_s.capitalize }.join('-') result << case key when 'Authorization', 'Proxy-Authorization' encode_auth(key, value) else encode_field(key, value) end end end |
#encode_host ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/em-http/http_encoding.rb', line 42 def encode_host if @req.uri.port.nil? || @req.uri.port == 80 || @req.uri.port == 443 return @req.uri.host else @req.uri.host + ":#{@req.uri.port}" end 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
82 83 84 85 86 87 88 |
# File 'lib/em-http/http_encoding.rb', line 82 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(uri, query) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/em-http/http_encoding.rb', line 67 def encode_query(uri, query) encoded_query = if query.kind_of?(Hash) query.map { |k, v| encode_param(k, v) }.join('&') else query.to_s end if uri && !uri.query.to_s.empty? encoded_query = [encoded_query, uri.query].reject {|part| part.empty?}.join("&") end encoded_query.to_s.empty? ? uri.path : "#{uri.path}?#{encoded_query}" end |
#encode_request(method, uri, query, connopts) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/em-http/http_encoding.rb', line 50 def encode_request(method, uri, query, connopts) query = encode_query(uri, query) # Non CONNECT proxies require that you provide the full request # uri in request header, as opposed to a relative path. # Don't modify the header with CONNECT proxies. It's unneeded and will # cause 400 Bad Request errors with many standard setups. if connopts.proxy && !connopts.connect_proxy? query = uri.join(query) # Drop the userinfo, it's been converted to a header and won't be # accepted by the proxy query.userinfo = nil end HTTP_REQUEST_HEADER % [method.to_s.upcase, query] end |
#escape(s) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/em-http/http_encoding.rb', line 7 def escape(s) if defined?(EscapeUtils) EscapeUtils.escape_url(s.to_s) else s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/) { '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase } end end |
#form_encode_body(obj) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/em-http/http_encoding.rb', line 90 def form_encode_body(obj) pairs = [] recursive = Proc.new do |h, prefix| h.each do |k,v| key = prefix == '' ? escape(k) : "#{prefix}[#{escape(k)}]" if v.is_a? Array nh = Hash.new v.size.times { |t| nh[t] = v[t] } recursive.call(nh, key) elsif v.is_a? Hash recursive.call(v, key) else pairs << "#{key}=#{escape(v)}" end end end recursive.call(obj, '') return pairs.join('&') end |
#munge_header_keys(head) ⇒ Object
Map all header keys to a downcased string version
38 39 40 |
# File 'lib/em-http/http_encoding.rb', line 38 def munge_header_keys(head) head.inject({}) { |h, (k, v)| h[k.to_s.downcase] = v; h } end |
#unescape(s) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/em-http/http_encoding.rb', line 17 def unescape(s) if defined?(EscapeUtils) EscapeUtils.unescape_url(s.to_s) else s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) { [$1.delete('%')].pack('H*') } end end |