| Module | Rack::Utils |
| In: |
lib/rack/utils.rb
|
Rack::Utils contains a grab-bag of useful methods for writing web applications adopted from all kinds of Ruby libraries.
| HTTP_STATUS_CODES | = | { 100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported' | Every standard HTTP code mapped to the appropriate message. Stolen from Mongrel. | |
| STATUS_WITH_NO_ENTITY_BODY | = | Set.new((100..199).to_a << 204 << 304) | Responses with HTTP status codes that should not have an entity body |
# File lib/rack/utils.rb, line 96
96: def build_query(params)
97: params.map { |k, v|
98: if v.class == Array
99: build_query(v.map { |x| [k, x] })
100: else
101: escape(k) + "=" + escape(v)
102: end
103: }.join("&")
104: end
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).
# File lib/rack/utils.rb, line 12
12: def escape(s)
13: s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
14: '%'+$1.unpack('H2'*$1.size).join('%').upcase
15: }.tr(' ', '+')
16: end
Escape ampersands, brackets and quotes to their HTML/XML entities.
# File lib/rack/utils.rb, line 108
108: def escape_html(string)
109: string.to_s.gsub("&", "&").
110: gsub("<", "<").
111: gsub(">", ">").
112: gsub("'", "'").
113: gsub('"', """)
114: end
# File lib/rack/utils.rb, line 65
65: def normalize_params(params, name, v = nil)
66: name =~ %r([\[\]]*([^\[\]]+)\]*)
67: k = $1 || ''
68: after = $' || ''
69:
70: return if k.empty?
71:
72: if after == ""
73: params[k] = v
74: elsif after == "[]"
75: params[k] ||= []
76: raise TypeError unless params[k].is_a?(Array)
77: params[k] << v
78: elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
79: child_key = $1
80: params[k] ||= []
81: raise TypeError unless params[k].is_a?(Array)
82: if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
83: normalize_params(params[k].last, child_key, v)
84: else
85: params[k] << normalize_params({}, child_key, v)
86: end
87: else
88: params[k] ||= {}
89: params[k] = normalize_params(params[k], after, v)
90: end
91:
92: return params
93: end
# File lib/rack/utils.rb, line 53
53: def parse_nested_query(qs, d = '&;')
54: params = {}
55:
56: (qs || '').split(/[#{d}] */n).each do |p|
57: k, v = unescape(p).split('=', 2)
58: normalize_params(params, k, v)
59: end
60:
61: return params
62: end
Stolen from Mongrel, with some small modifications: 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 ’&;’).
# File lib/rack/utils.rb, line 32
32: def parse_query(qs, d = '&;')
33: params = {}
34:
35: (qs || '').split(/[#{d}] */n).each do |p|
36: k, v = unescape(p).split('=', 2)
37:
38: if cur = params[k]
39: if cur.class == Array
40: params[k] << v
41: else
42: params[k] = [cur, v]
43: end
44: else
45: params[k] = v
46: end
47: end
48:
49: return params
50: end
# File lib/rack/utils.rb, line 117
117: def select_best_encoding(available_encodings, accept_encoding)
118: # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
119:
120: expanded_accept_encoding =
121: accept_encoding.map { |m, q|
122: if m == "*"
123: (available_encodings - accept_encoding.map { |m2, _| m2 }).map { |m2| [m2, q] }
124: else
125: [[m, q]]
126: end
127: }.inject([]) { |mem, list|
128: mem + list
129: }
130:
131: encoding_candidates = expanded_accept_encoding.sort_by { |_, q| -q }.map { |m, _| m }
132:
133: unless encoding_candidates.include?("identity")
134: encoding_candidates.push("identity")
135: end
136:
137: expanded_accept_encoding.find_all { |m, q|
138: q == 0.0
139: }.each { |m, _|
140: encoding_candidates.delete(m)
141: }
142:
143: return (encoding_candidates & available_encodings)[0]
144: end