Parent

Mechanize::HTTP::ContentDispositionParser

Parser Content-Disposition headers that loosely follows RFC 2183.

Beyond RFC 2183, this parser allows:

Public Class Methods

new() click to toggle source

Creates a new parser Content-Disposition headers

    # File lib/mechanize/http/content_disposition_parser.rb, line 38
38:   def initialize
39:     @scanner = nil
40:   end
parse(content_disposition) click to toggle source

Parses the disposition type and params in the content_disposition string. The “Content-Disposition:” must be removed.

    # File lib/mechanize/http/content_disposition_parser.rb, line 30
30:   def self.parse content_disposition
31:     @parser ||= self.new
32:     @parser.parse content_disposition
33:   end

Public Instance Methods

parse(content_disposition, header = false) click to toggle source

Parses the content_disposition header. If header is set to true the “Content-Disposition:” portion will be parsed

    # File lib/mechanize/http/content_disposition_parser.rb, line 46
46:   def parse content_disposition, header = false
47:     return nil if content_disposition.empty?
48: 
49:     @scanner = StringScanner.new content_disposition
50: 
51:     if header then
52:       return nil unless @scanner.scan(/Content-Disposition/)
53:       return nil unless @scanner.scan(/:/)
54:       spaces
55:     end
56: 
57:     type = rfc_2045_token
58:     @scanner.scan(/;+/)
59: 
60:     if @scanner.peek(1) == '=' then
61:       @scanner.pos = 0
62:       type = nil
63:     end
64: 
65:     disposition = Mechanize::HTTP::ContentDisposition.new type
66: 
67:     spaces
68: 
69:     return nil unless parameters = parse_parameters
70: 
71:     disposition.filename          = parameters.delete 'filename'
72:     disposition.creation_date     = parameters.delete 'creation-date'
73:     disposition.modification_date = parameters.delete 'modification-date'
74:     disposition.read_date         = parameters.delete 'read-date'
75:     disposition.size              = parameters.delete 'size'
76:     disposition.parameters        = parameters
77: 
78:     disposition
79:   end
parse_parameters() click to toggle source

Extracts disposition-parm and returns a Hash.

     # File lib/mechanize/http/content_disposition_parser.rb, line 84
 84:   def parse_parameters
 85:     parameters = {}
 86: 
 87:     while true do
 88:       return nil unless param = rfc_2045_token
 89:       param.downcase
 90:       return nil unless @scanner.scan(/=/)
 91: 
 92:       value = case param
 93:               when /^filename$/ then
 94:                 rfc_2045_value
 95:               when /^(creation|modification|read)-date$/ then
 96:                 Time.rfc822 rfc_2045_quoted_string
 97:               when /^size$/ then
 98:                 rfc_2045_value.to_i(10)
 99:               else
100:                 rfc_2045_value
101:               end
102: 
103:       return nil unless value
104: 
105:       parameters[param] = value
106: 
107:       spaces
108: 
109:       break if @scanner.eos? or not @scanner.scan(/;+/)
110: 
111:       spaces
112:     end
113: 
114:     parameters
115:   end
rfc_2045_quoted_string() click to toggle source
  quoted-string = <"> *(qtext/quoted-pair) <">
  qtext         = <any CHAR excepting <">, "\" & CR,
                   and including linear-white-space
  quoted-pair   = "\" CHAR

Parses an RFC 2045 quoted-string

     # File lib/mechanize/http/content_disposition_parser.rb, line 125
125:   def rfc_2045_quoted_string
126:     return nil unless @scanner.scan(/"/)
127: 
128:     text = ''
129: 
130:     while true do
131:       chunk = @scanner.scan(/[\0000-\0014\0016-\0041\0043-\1133\1135-\1177]+/) # not \r "
132: 
133:       if chunk then
134:         text << chunk
135: 
136:         if @scanner.peek(1) == '\' then
137:           @scanner.get_byte
138:           return nil if @scanner.eos?
139:           text << @scanner.get_byte
140:         elsif @scanner.scan(/\r\n[\t ]+/) then
141:           text << " "
142:         end
143:       else
144:         if '"' == @scanner.peek(1) then
145:           @scanner.get_byte
146:           break
147:         else
148:           return nil
149:         end
150:       end
151:     end
152: 
153:     text
154:   end
rfc_2045_token() click to toggle source
  token := 1*<any (US-ASCII) CHAR except SPACE, CTLs, or tspecials>

Parses an RFC 2045 token

     # File lib/mechanize/http/content_disposition_parser.rb, line 161
161:   def rfc_2045_token
162:     @scanner.scan(/[^\0000-\0037\1177()<>@,;:\\"\/\[\]?= ]+/)
163:   end
rfc_2045_value() click to toggle source
  value := token / quoted-string

Parses an RFC 2045 value

     # File lib/mechanize/http/content_disposition_parser.rb, line 170
170:   def rfc_2045_value
171:     if @scanner.peek(1) == '"' then
172:       rfc_2045_quoted_string
173:     else
174:       rfc_2045_token
175:     end
176:   end
spaces() click to toggle source
  1*SP

Parses spaces

     # File lib/mechanize/http/content_disposition_parser.rb, line 183
183:   def spaces
184:     @scanner.scan(/ +/)
185:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.