Class: Rbkb::Http::MultipartFormParams
- Inherits:
-
Parameters
- Object
- Array
- Parameters
- Rbkb::Http::MultipartFormParams
- Defined in:
- lib/rbkb/http/parameters.rb
Overview
The MultipartFormParams class is for Parameters in POST data when using the multipart/form-data content type. This is often used for file uploads.
Instance Attribute Summary collapse
-
#boundary ⇒ Object
Returns the value of attribute boundary.
-
#part_headers ⇒ Object
Returns the value of attribute part_headers.
Instance Method Summary collapse
- #capture(str) ⇒ Object
-
#initialize(*args) ⇒ MultipartFormParams
constructor
You must specify a boundary somehow when instantiating a new MultipartFormParams object.
- #to_raw ⇒ Object
Methods inherited from Parameters
#delete_param, #get_all, #get_all_values_for, #get_param, #get_value_for, parse, #set_all_for, #set_param
Methods included from CommonInterface
Constructor Details
#initialize(*args) ⇒ MultipartFormParams
You must specify a boundary somehow when instantiating a new MultipartFormParams object. The
161 162 163 164 165 166 167 |
# File 'lib/rbkb/http/parameters.rb', line 161 def initialize(*args) _common_init(*args) do |this| yield this if block_given? this.boundary ||= ( this.opts[:boundary] || rand(0xffffffffffffffff).to_s(16).rjust(48,'-') ) end end |
Instance Attribute Details
#boundary ⇒ Object
Returns the value of attribute boundary.
157 158 159 |
# File 'lib/rbkb/http/parameters.rb', line 157 def boundary @boundary end |
#part_headers ⇒ Object
Returns the value of attribute part_headers.
157 158 159 |
# File 'lib/rbkb/http/parameters.rb', line 157 def part_headers @part_headers end |
Instance Method Details
#capture(str) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rbkb/http/parameters.rb', line 190 def capture(str) raise "arg 0 must be a string" unless String === str @part_headers = [] self.replace([]) s = StringScanner.new(str) bound = /\-\-#{Regexp.escape(@boundary)}\r?\n/ unless start=s.scan_until(bound) and start.index(@boundary)==2 raise "unexpected start data #{start.inspect}" end while chunk = s.scan_until(bound) part = chunk[0,chunk.index(bound)].chomp phdr, body = part.split(/^\r?\n/, 2) head=Headers.parse(phdr) x, parms = head.get_parameterized_value('Content-Disposition') if parms and name=parms.get_value_for("name") @part_headers << head self << [name, body] else raise "invalid chunk at #{s.pos} bytes" end end unless str[s.pos..-1] =~ /^\-\-#{Regexp.escape(@boundary)}--(?:\r?\n|$)/ raise "expected boundary terminator at #{s.pos}" end return self end |
#to_raw ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rbkb/http/parameters.rb', line 169 def to_raw ret = "" self.each_with_index do |p,i| name, value = p ret << "--#{boundary.to_s}\n" hdrs = @part_headers[i] if cd = hdrs.get_parameterized_value("Content-Disposition") v, parms = cd parms.set_value_for("name", name) if name hdrs.set_parameterized_value("Content-Disposition", v, parms) else hdrs.set_value_for("Content-Disposition", "form-data; name=#{name}") end ret << hdrs.to_raw ret << "#{value}\n" end ret << "#{boundary}--" end |