Module: Rack::TestApp::Util
- Defined in:
- lib/rack/test_app.rb
Constant Summary collapse
- COOKIE_KEYS =
{ 'path' => :path, 'domain' => :domain, 'expires' => :expires, 'max-age' => :max_age, 'httponly' => :httponly, 'secure' => :secure, }
Class Method Summary collapse
-
.build_query_string(query) ⇒ Object
:nodoc:.
- .guess_content_type(filename, default = 'application/octet-stream') ⇒ Object
- .parse_set_cookie(set_cookie_value) ⇒ Object
- .percent_decode(str) ⇒ Object
- .percent_encode(str) ⇒ Object
- .randstr_b64 ⇒ Object
Class Method Details
.build_query_string(query) ⇒ Object
:nodoc:
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rack/test_app.rb', line 40 def build_query_string(query) # :nodoc: #; [!098ac] returns nil when argument is nil. #; [!z9ds2] returns argument itself when it is a string. #; [!m5yyh] returns query string when Hash or Array passed. #; [!nksh3] raises ArgumentError when passed value except nil, string, hash or array. case query when nil ; return nil when String ; return query when Hash, Array return query.collect {|k, v| "#{percent_encode(k.to_s)}=#{percent_encode(v.to_s)}" }.join('&') else raise ArgumentError.new("Hash or Array expected but got #{query.inspect}.") end end |
.guess_content_type(filename, default = 'application/octet-stream') ⇒ Object
112 113 114 115 116 117 |
# File 'lib/rack/test_app.rb', line 112 def guess_content_type(filename, default='application/octet-stream') #; [!xw0js] returns content type guessed from filename. #; [!dku5c] returns 'application/octet-stream' when failed to guess content type. ext = ::File.extname(filename) return Rack::Mime.mime_type(ext, default) end |
.parse_set_cookie(set_cookie_value) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rack/test_app.rb', line 64 def () #; [!hvvu4] parses 'Set-Cookie' header value and returns hash object. keys = COOKIE_KEYS d = {} .split(/;\s*/).each do |string| #; [!h75uc] sets true when value is missing such as 'secure' or 'httponly' attribute. k, v = string.strip().split('=', 2) # if d.empty? d[:name] = k d[:value] = v elsif (sym = keys[k.downcase]) #; [!q1h29] sets true as value for Secure or HttpOnly attribute. #; [!50iko] raises error when attribute value specified for Secure or HttpOnly attirbute. if sym == :secure || sym == :httponly v.nil? or raise TypeError.new("#{k}=#{v}: unexpected attribute value.") v = true #; [!sucrm] raises error when attribute value is missing when neighter Secure nor HttpOnly. else ! v.nil? or raise TypeError.new("#{k}: attribute value expected but not specified.") #; [!f3rk7] converts string into integer for Max-Age attribute. #; [!wgzyz] raises error when Max-Age attribute value is not a positive integer. if sym == :max_age v =~ /\A\d+\z/ or raise TypeError.new("#{k}=#{v}: positive integer expected.") v = v.to_i end end d[sym] = v #; [!8xg63] raises ArgumentError when unknown attribute exists. else raise TypeError.new("#{k}=#{v}: unknown cookie attribute.") end end return d end |
.percent_decode(str) ⇒ Object
35 36 37 38 |
# File 'lib/rack/test_app.rb', line 35 def percent_decode(str) #; [!kl9sk] decodes percent encoded string. return URI.decode_www_form_component(str) end |
.percent_encode(str) ⇒ Object
30 31 32 33 |
# File 'lib/rack/test_app.rb', line 30 def percent_encode(str) #; [!a96jo] encodes string into percent encoding format. return URI.encode_www_form_component(str) end |
.randstr_b64 ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/rack/test_app.rb', line 103 def randstr_b64() #; [!yq0gv] returns random string, encoded with urlsafe base64. ## Don't use SecureRandom; entropy of /dev/random or /dev/urandom ## should be left for more secure-sensitive purpose. s = "#{rand()}#{rand()}#{rand()}#{Time.now.to_f}" binary = ::Digest::SHA1.digest(s) return [binary].pack('m').chomp("=\n").tr('+/', '-_') end |