lib/em-http/http_encoding.rb in em-http-request-0.3.0 vs lib/em-http/http_encoding.rb in em-http-request-1.0.0.beta.1

- old
+ new

@@ -1,18 +1,28 @@ module EventMachine module HttpEncoding HTTP_REQUEST_HEADER="%s %s HTTP/1.1\r\n" FIELD_ENCODING = "%s: %s\r\n" - # Escapes a URI. def escape(s) - EscapeUtils.escape_url(s.to_s) + if defined?(EscapeUtils) + EscapeUtils.escape_url(s.to_s) + else + s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) { + '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase + } + end end - # Unescapes a URI escaped string. def unescape(s) - EscapeUtils.unescape_url(s.to_s) + if defined?(EscapeUtils) + EscapeUtils.unescape_url(s.to_s) + else + s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) { + [$1.delete('%')].pack('H*') + } + end end if ''.respond_to?(:bytesize) def bytesize(string) string.bytesize @@ -26,27 +36,24 @@ # Map all header keys to a downcased string version def munge_header_keys(head) head.inject({}) { |h, (k, v)| h[k.to_s.downcase] = v; h } 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. def encode_host - if @uri.port == 80 || @uri.port == 443 - return @uri.host + if @req.uri.port == 80 || @req.uri.port == 443 + return @req.uri.host else - @uri.host + ":#{@uri.port}" + @req.uri.host + ":#{@req.uri.port}" end end def encode_request(method, uri, query, proxy) 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. - query = uri.join(query) if proxy && proxy[:type] != :socks && !proxy[:use_connect] + query = uri.join(query) if proxy && proxy[:type] != :socks HTTP_REQUEST_HEADER % [method.to_s.upcase, query] end def encode_query(uri, query) @@ -103,10 +110,10 @@ # Encode basic auth in an HTTP header # In: Array ([user, pass]) - for basic auth # String - custom auth string (OAuth, etc) def encode_auth(k,v) if v.is_a? Array - FIELD_ENCODING % [k, ["Basic", Base64.encode64(v.join(":")).chomp].join(" ")] + FIELD_ENCODING % [k, ["Basic", Base64.encode64(v.join(":")).split.join].join(" ")] else encode_field(k,v) end end