Module: EventMachine::HttpEncoding

### Home Classes Methods

### In Files

- lib/em-http/http_encoding.rb

### Methods

- #bytesize
- #encode_auth
- #encode_cookie
- #encode_field
- #encode_headers
- #encode_host
- #encode_param
- #encode_query
- #encode_request
- #escape
- #form_encode_body
- #munge_header_keys
- #unescape

### Class/Module Index

Quicksearch

- EventMachine
- EventMachine::HTTPMethods
- EventMachine::HttpClient
- EventMachine::HttpClient::CookieJar
- EventMachine::HttpConnection
- EventMachine::HttpDecoders
- EventMachine::HttpDecoders::Base
- EventMachine::HttpDecoders::DecoderError
- EventMachine::HttpDecoders::Deflate
- EventMachine::HttpDecoders::GZip
- EventMachine::HttpDecoders::GZipHeader
- EventMachine::HttpEncoding
- EventMachine::HttpRequest
- EventMachine::HttpResponseHeader
- EventMachine::HttpStatus
- EventMachine::HttpStubConnection
- EventMachine::Middleware
- EventMachine::Middleware::DigestAuth
- EventMachine::Middleware::JSONResponse
- EventMachine::Middleware::OAuth
- EventMachine::Middleware::OAuth2
- EventMachine::MultiRequest
- HttpClientOptions
- HttpConnectionOptions
- String

No matching classes.

# EventMachine::HttpEncoding

### Constants

FIELD_ENCODING
HTTP_REQUEST_HEADER

### Public Instance Methods

bytesize(string) click to toggle source

    # File lib/em-http/http_encoding.rb, line 27
    def bytesize(string)
      string.bytesize
    end

encode_auth(k,v) click to toggle source

Encode basic auth in an HTTP header In: Array ([user, pass]) - for basic auth

    String - custom auth string (OAuth, etc)

    # File lib/em-http/http_encoding.rb, line 113
    def encode_auth(k,v)
      if v.is_a? Array
        FIELD_ENCODING % [k, ["Basic", Base64.encode64(v.join(":")).split.join].join(" ")]
      else
        encode_field(k,v)
      end
    end

encode_cookie(cookie) click to toggle source

    # File lib/em-http/http_encoding.rb, line 134
    def encode_cookie(cookie)
      if cookie.is_a? Hash
        cookie.inject('') { |result, (k, v)| result <<  encode_param(k, v) + ";" }
      else
        cookie
      end
    end

encode_field(k, v) click to toggle source

Encode a field in an HTTP header

    # File lib/em-http/http_encoding.rb, line 106
    def encode_field(k, v)
      FIELD_ENCODING % [k, v]
    end

encode_headers(head) click to toggle source

    # File lib/em-http/http_encoding.rb, line 121
    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() click to toggle source

    # File lib/em-http/http_encoding.rb, line 41
    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) click to toggle source

URL encodes query parameters: single k=v, or a URL encoded array, if v is an array of values

    # File lib/em-http/http_encoding.rb, line 74
    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) click to toggle source

    # File lib/em-http/http_encoding.rb, line 59
    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, proxy) click to toggle source

    # File lib/em-http/http_encoding.rb, line 49
    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

      HTTP_REQUEST_HEADER % [method.to_s.upcase, query]
    end

escape(s) click to toggle source

    # File lib/em-http/http_encoding.rb, line 6
    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) click to toggle source

    # File lib/em-http/http_encoding.rb, line 82
    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) click to toggle source

Map all header keys to a downcased string version

    # File lib/em-http/http_encoding.rb, line 37
    def munge_header_keys(head)
      head.inject({}) { |h, (k, v)| h[k.to_s.downcase] = v; h }
    end

unescape(s) click to toggle source

    # File lib/em-http/http_encoding.rb, line 16
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.