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.

Instance Method Summary collapse

Instance Method Details

#encode_cookies(cookies) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rfuzz/client.rb', line 45

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”



58
59
60
# File 'lib/rfuzz/client.rb', line 58

def encode_field(k,v)
  "#{k}: #{v}\r\n"
end

#encode_headers(head) ⇒ Object

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



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

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.



104
105
106
# File 'lib/rfuzz/client.rb', line 104

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

#encode_param(k, v) ⇒ Object

URL encodes a single k=v parameter.



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

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rfuzz/client.rb', line 83

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

Performs URI escaping so that you can construct proper query strings faster. Use this rather than the cgi.rb version since it’s faster. (Stolen from Camping).



111
112
113
114
115
# File 'lib/rfuzz/client.rb', line 111

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 ‘&;’.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rfuzz/client.rb', line 129

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. (Stolen from Camping).



119
120
121
122
123
# File 'lib/rfuzz/client.rb', line 119

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