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"

Instance Method Summary collapse

Instance Method Details

#encode_cookies(cookies) ⇒ Object

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



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

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”



60
61
62
# File 'lib/rfuzz/client.rb', line 60

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.



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

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.



106
107
108
# File 'lib/rfuzz/client.rb', line 106

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

#encode_param(k, v) ⇒ Object

URL encodes a single k=v parameter.



79
80
81
# File 'lib/rfuzz/client.rb', line 79

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.



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

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).



113
114
115
116
117
# File 'lib/rfuzz/client.rb', line 113

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



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

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).



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

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