Module: RFuzz::HttpEncoding

Included in:
HttpClient
Defined in:
lib/rfuzz/client.rb

Overview

A mixin that has most of the HTTP encoding methods you need to work with the protocol. It’s used by HttpClient, but you can use it as well.

Constant Summary collapse

"Cookie"
FIELD_ENCODING =
"%s: %s\r\n"

Instance Method Summary collapse

Instance Method Details

#encode_cookies(cookies) ⇒ Object

Converts a Hash of cookies to the appropriate simple cookie headers.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rfuzz/client.rb', line 63

def encode_cookies(cookies)
  result = ""
  cookies.each do |k,v|
    if v.kind_of? Array
      v.each {|x| result << encode_field(COOKIE, encode_param(k,x)) }
    else
      result << encode_field(COOKIE, encode_param(k,v))
    end
  end
  return result
end

#encode_field(k, v) ⇒ Object

Encode HTTP header fields of “k: vrn”



76
77
78
# File 'lib/rfuzz/client.rb', line 76

def encode_field(k,v)
  FIELD_ENCODING % [k,v]
end

#encode_headers(head) ⇒ Object

Encodes the headers given in the hash returning a string you can use.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rfuzz/client.rb', line 82

def encode_headers(head)
  result = "" 
  head.each do |k,v|
    if v.kind_of? Array
      v.each {|x| result << encode_field(k,x) }
    else
      result << encode_field(k,v)
    end
  end
  return result
end

#encode_host(host, port) ⇒ 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.



122
123
124
# File 'lib/rfuzz/client.rb', line 122

def encode_host(host, port)
  host + (port.to_i != 80 ? ":#{port}" : "")
end

#encode_param(k, v) ⇒ Object

URL encodes a single k=v parameter.



95
96
97
# File 'lib/rfuzz/client.rb', line 95

def encode_param(k,v)
  escape(k) + "=" + escape(v)
end

#encode_query(uri, query) ⇒ Object

Takes a query string and encodes it as a URL encoded set of key=value pairs with & separating them.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rfuzz/client.rb', line 101

def encode_query(uri, query)
  params = []

  if query
    query.each do |k,v|
      if v.kind_of? Array
        v.each {|x| params << encode_param(k,x) } 
      else
        params << encode_param(k,v)
      end
    end

    uri += "?" + params.join('&')
  end

  return uri
end

#escape(s) ⇒ Object

Escapes a URI.



127
128
129
130
131
# File 'lib/rfuzz/client.rb', line 127

def escape(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
    '%'+$1.unpack('H2'*$1.size).join('%').upcase
  }.tr(' ', '+') 
end

#query_parse(qs, d = '&;') ⇒ Object

Parses a query string by breaking it up at the ‘&’ and ‘;’ characters. You can also use this to parse cookies by changing the characters used in the second parameter (which defaults to ‘&;’.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rfuzz/client.rb', line 145

def query_parse(qs, d = '&;')
  params = {}
  (qs||'').split(/[#{d}] */n).inject(params) { |h,p|
    k, v=unescape(p).split('=',2)
    if cur = params[k]
      if cur.class == Array
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  }

  return params
end

#unescape(s) ⇒ Object

Unescapes a URI escaped string.



135
136
137
138
139
# File 'lib/rfuzz/client.rb', line 135

def unescape(s)
  s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
    [$1.delete('%')].pack('H*')
  } 
end