Class: Riddl::Protocols::XMPP::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/riddl/protocols/xmpp/parser.rb

Constant Summary collapse

FORM_CONTENT_TYPES =
[
  #{{{
  'application/x-www-form-urlencoded'
  #}}}
].freeze
STD_ATTRIBUTES =
[
  #{{{
  'content-type',
  'content-disposition',
  'content-id',
  'content-transfer-type',
  'RIDDL-TYPE',
  #}}}
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string, input) ⇒ Parser

Returns a new instance of Parser.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruby/riddl/protocols/xmpp/parser.rb', line 95

def initialize(query_string,input)
  #{{{
  @params = Riddl::Parameter::Array.new

  parse_nested_query(query_string,:query)

  input.find('/message/xr:part').each do |p|
    content_type = p.attributes['content-type'] || nil
    media_type = content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
    if FORM_CONTENT_TYPES.include?(media_type)
      # sub is a fix for Safari Ajax postings that always append \0
      parse_nested_query(p.text.sub(/\0\z/, ''),:body)
    else  
      parse_part(p.children,p.attributes,content_type,p.attributes['content-disposition']||'',p.attributes['content-id']||'',p.attributes['RIDDL-TYPE']||'')
    end
  end
  #}}}
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



114
115
116
# File 'lib/ruby/riddl/protocols/xmpp/parser.rb', line 114

def params
  @params
end

Class Method Details

.unescape(s) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/ruby/riddl/protocols/xmpp/parser.rb', line 24

def self::unescape(s)
  #{{{
  return s if s.nil?  
  s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
    [$1.delete('%')].pack('H*')
  }
  #}}}
end

Instance Method Details

#parse_part(input, head, ctype, content_disposition, content_id, riddl_type) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby/riddl/protocols/xmpp/parser.rb', line 33

def parse_part(input,head,ctype,content_disposition,content_id,riddl_type)
 #{{{
  head = Hash[
    head.map do |h|
      STD_ATTRIBUTES.include?(h.qname.name) ? nil : [h.qname.name, h.value]
    end.compact
  ]
  ctype = nil if riddl_type == 'simple'
  filename = content_disposition[/ filename="?([^\";]*)"?/ni, 1]
  name = content_disposition[/ name="?([^\";]*)"?/ni, 1] || content_id

  if ctype || filename
    body = Parameter::Tempfile.new("RiddlMultipart")
    body.binmode if body.respond_to?(:binmode)
  else
    body = ''
  end

  input.each { |i| body << i.dump }
  body.rewind if body.respond_to?(:binmode)

  add_to_params(name,body,filename,ctype,head)
 #}}}
end