request | The raw request. |
boundary | The boundary string. |
content_length | The length of the content. |
ControllerExceptions::MultiPartParseError | Failed to parse request. |
Hash | The parsed request. |
:api: plugin
# File lib/merb-core/dispatch/request_parsers.rb, line 49 49: def self.multipart(request, boundary, content_length) 50: boundary = "--#{boundary}" 51: paramhsh = {} 52: buf = "" 53: input = request 54: input.binmode if defined? input.binmode 55: boundary_size = boundary.size + EOL.size 56: bufsize = 16384 57: content_length -= boundary_size 58: key_memo = [] 59: # status is boundary delimiter line 60: status = input.read(boundary_size) 61: return {} if status == nil || status.empty? 62: raise ControllerExceptions::MultiPartParseError, "bad content body:\n'#{status}' should == '#{boundary + EOL}'" unless status == boundary + EOL 63: 64: rx = /(?:#{EOL})?#{Regexp.quote(boundary)}(#{EOL}|--)/ 65: loop { 66: head = nil 67: body = '' 68: filename = content_type = name = nil 69: read_size = 0 70: until head && buf =~ rx 71: i = buf.index("\r\n\r\n") 72: if( i == nil && read_size == 0 && content_length == 0 ) 73: content_length = 1 74: break 75: end 76: if !head && i 77: head = buf.slice!(0, i+2) # First \r\n 78: buf.slice!(0, 2) # Second \r\n 79: 80: # String#[] with 2nd arg here is returning 81: # a group from match data 82: filename = head[FILENAME_REGEX, 1] 83: content_type = head[CONTENT_TYPE_REGEX, 1] 84: name = head[NAME_REGEX, 1] 85: 86: if filename && !filename.empty? 87: body = Tempfile.new(:Merb) 88: body.binmode if defined? body.binmode 89: end 90: next 91: end 92: 93: # Save the read body part. 94: if head && (boundary_size+4 < buf.size) 95: body << buf.slice!(0, buf.size - (boundary_size+4)) 96: end 97: 98: read_size = bufsize < content_length ? bufsize : content_length 99: if( read_size > 0 ) 100: c = input.read(read_size) 101: raise ControllerExceptions::MultiPartParseError, "bad content body" if c.nil? || c.empty? 102: buf << c 103: content_length -= c.size 104: end 105: end 106: 107: # Save the rest. 108: if i = buf.index(rx) 109: # correct value of i for some edge cases 110: if (i > 2) && (j = buf.index(rx, i-2)) && (j < i) 111: i = j 112: end 113: body << buf.slice!(0, i) 114: buf.slice!(0, boundary_size+2) 115: 116: content_length = 1 if $1 == "--" 117: end 118: 119: if filename && !filename.empty? 120: body.rewind 121: data = { 122: :filename => File.basename(filename), 123: :content_type => content_type, 124: :tempfile => body, 125: :size => File.size(body.path) 126: } 127: else 128: data = body 129: end 130: 131: unless key_memo.include?(name) && name !~ /\[\]/ 132: paramhsh = normalize_params(paramhsh,name,data) 133: end 134: 135: # Prevent from double processing files but process other params 136: key_memo << name if filename && !filename.empty? 137: 138: break if buf.empty? || content_length == 1 139: } 140: 141: paramhsh 142: end
value | The value for the query string. |
prefix<~to_s> | The prefix to add to the query string keys. |
String | The query string. |
If the value is a string, the prefix will be used as the key.
params_to_query_string(10, "page") # => "page=10" params_to_query_string({ :page => 10, :word => "ruby" }) # => "page=10&word=ruby" params_to_query_string({ :page => 10, :word => "ruby" }, "search") # => "search[page]=10&search[word]=ruby" params_to_query_string([ "ice-cream", "cake" ], "shopping_list") # => "shopping_list[]=ice-cream&shopping_list[]=cake"
:api: plugin
# File lib/merb-core/dispatch/request_parsers.rb, line 165 165: def self.params_to_query_string(value, prefix = nil) 166: case value 167: when Array 168: value.map { |v| 169: params_to_query_string(v, "#{prefix}[]") 170: } * "&" 171: when Hash, Dictionary 172: value.map { |k, v| 173: params_to_query_string(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) 174: } * "&" 175: else 176: "#{prefix}=#{escape(value)}" 177: end 178: end
query_string | The query string. |
delimiter | The query string divider. Defaults to “&”. |
preserve_order | Preserve order of args. Defaults to false. |
Mash | The parsed query string (Dictionary if preserve_order is set). |
Merb::Parse.query("bar=nik&post[body]=heya") # => { :bar => "nik", :post => { :body => "heya" } }
:api: plugin
# File lib/merb-core/dispatch/request_parsers.rb, line 17 17: def self.query(query_string, delimiter = '&;', preserve_order = false) 18: query = preserve_order ? Dictionary.new : {} 19: for pair in (query_string || '').split(/[#{delimiter}] */) 20: key, value = unescape(pair).split('=',2) 21: next if key.nil? 22: if key.include?('[') 23: normalize_params(query, key, value) 24: else 25: query[key] = value 26: end 27: end 28: preserve_order ? query : query.to_mash 29: end
s | String to URL unescape. |
encoding | Encoding which we force to return. Only for Ruby 1.9. If encoding is not passed it defaults to Encoding.default_internal. When this is nil (default) no encoding forcing is done. |
String | The unescaped string. |
:api: public
# File lib/merb-core/dispatch/request_parsers.rb, line 204 204: def self.unescape(s, encoding = nil) 205: s = s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/){ 206: [$1.delete('%')].pack('H*') 207: } 208: if RUBY_VERSION >= '1.9' 209: encoding ||= Encoding.default_internal 210: s.force_encoding(encoding) if encoding 211: end 212: s 213: end
Converts a query string snippet to a hash and adds it to existing parameters.
parms | Parameters to add the normalized parameters to. |
name | The key of the parameter to normalize. |
val | The value of the parameter. |
Hash | Normalized parameters |
:api: private
# File lib/merb-core/dispatch/request_parsers.rb, line 240 240: def self.normalize_params(parms, name, val=nil) 241: name =~ %([\[\]]*([^\[\]]+)\]*) 242: key = $1 || '' 243: after = $' || '' 244: 245: if after == "" 246: parms[key] = val 247: elsif after == "[]" 248: (parms[key] ||= []) << val 249: elsif after =~ %(^\[\]\[([^\[\]]+)\]$) 250: child_key = $1 251: parms[key] ||= [] 252: if parms[key].last.is_a?(Hash) && !parms[key].last.key?(child_key) 253: parms[key].last.update(child_key => val) 254: else 255: parms[key] << { child_key => val } 256: end 257: else 258: parms[key] ||= {} 259: parms[key] = normalize_params(parms[key], after, val) 260: end 261: parms 262: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.