Class: Hobix::WebApp::QueryString

Inherits:
Object
  • Object
show all
Defined in:
lib/hobix/webapp/htmlform.rb

Instance Method Summary collapse

Instance Method Details

#decode_as_application_x_www_form_urlencodedObject

decode self as application/x-www-form-urlencoded and returns HTMLFormQuery object.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hobix/webapp/htmlform.rb', line 6

def decode_as_application_x_www_form_urlencoded
  # xxx: warning if invalid?
  pairs = []
  @escaped_query_string.scan(/([^&;=]*)=([^&;]*)/) {|key, val|
    key.gsub!(/\+/, ' ')
    key.gsub!(/%([0-9A-F][0-9A-F])/i) { [$1].pack("H*") }
    val.gsub!(/\+/, ' ')
    val.gsub!(/%([0-9A-F][0-9A-F])/i) { [$1].pack("H*") }
    pairs << [key.freeze, val.freeze]
  }
  HTMLFormQuery.new(pairs)
end

#decode_as_multipart_form_data(boundary) ⇒ Object

decode self as multipart/form-data and returns HTMLFormQuery object.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hobix/webapp/htmlform.rb', line 20

def decode_as_multipart_form_data( boundary )
  # xxx: warning if invalid?
  require 'tempfile'
  pairs = []
  boundary = "--" + boundary
  eol = "\015\012"
  str = @escaped_query_string.gsub( /(?:\r?\n|\A)#{ Regexp::quote( boundary ) }--#{ eol }.*/m, '' )
  str.split( /(?:\r?\n|\A)#{ Regexp::quote( boundary ) }#{ eol }/m ).each do |part|
      headers = {}
      header, value = part.split( "#{eol}#{eol}", 2 )
      next unless header and value
      field_name, field_data = nil, {}
      if header =~ /Content-Disposition: form-data;.*(?:\sname="([^"]+)")/m
          field_name = $1
      end
      if header =~ /Content-Disposition: form-data;.*(?:\sfilename="([^"]+)")/m
          body = Tempfile.new( "WebApp" )
          body.binmode if defined? body.binmode
          body.print value
          body.rewind
          field_data = {'filename' => $1, 'tempfile' => body}
          field_data['type'] = $1 if header =~ /Content-Type: (.+?)(?:#{ eol }|\Z)/m
      else
          field_data = value.gsub( /#{ eol }\Z/, '' )
      end
      pairs << [field_name, field_data]
  end
  HTMLFormQuery.new(pairs)
end