| 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 54
54: def build_query(params)
55: params.map { |k, v|
56: if v.class == Array
57: build_query(v.map { |x| [k, x] })
58: else
59: escape(k) + "=" + escape(v)
60: end
61: }.join("&")
62: 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 66
66: def escape_html(string)
67: string.to_s.gsub("&", "&").
68: gsub("<", "<").
69: gsub(">", ">").
70: gsub("'", "'").
71: gsub('"', """)
72: 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 33
33: def parse_query(qs, d = '&;')
34: params = {}
35:
36: (qs || '').split(/[#{d}] */n).each do |p|
37: k, v = unescape(p).split('=', 2)
38:
39: if cur = params[k]
40: if cur.class == Array
41: params[k] << v
42: else
43: params[k] = [cur, v]
44: end
45: else
46: params[k] = v
47: end
48: end
49:
50: return params
51: end
# File lib/rack/utils.rb, line 75
75: def select_best_encoding(available_encodings, accept_encoding)
76: # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
77:
78: expanded_accept_encoding =
79: accept_encoding.map { |m, q|
80: if m == "*"
81: (available_encodings - accept_encoding.map { |m2, _| m2 }).map { |m2| [m2, q] }
82: else
83: [[m, q]]
84: end
85: }.inject([]) { |mem, list|
86: mem + list
87: }
88:
89: encoding_candidates = expanded_accept_encoding.sort_by { |_, q| -q }.map { |m, _| m }
90:
91: unless encoding_candidates.include?("identity")
92: encoding_candidates.push("identity")
93: end
94:
95: expanded_accept_encoding.find_all { |m, q|
96: q == 0.0
97: }.each { |m, _|
98: encoding_candidates.delete(m)
99: }
100:
101: return (encoding_candidates & available_encodings)[0]
102: end