Parent

Mechanize::HTTP::WWWAuthenticateParser

Parses the WWW-Authenticate HTTP header into separate challenges.

Public Class Methods

new() click to toggle source

Creates a new header parser for WWW-Authenticate headers

    # File lib/mechanize/http/www_authenticate_parser.rb, line 15
15:   def initialize
16:     @scanner = nil
17:   end

Public Instance Methods

auth_param() click to toggle source
  auth-param = token "=" ( token | quoted-string )

Parses an auth parameter

     # File lib/mechanize/http/www_authenticate_parser.rb, line 124
124:   def auth_param
125:     return nil unless name = token
126:     return nil unless @scanner.scan(/=/)
127: 
128:     value = if @scanner.peek(1) == '"' then
129:               quoted_string
130:             else
131:               token
132:             end
133: 
134:     return nil unless value
135: 
136:     return name, value
137:   end
auth_scheme() click to toggle source
  auth-scheme = token

Parses an auth scheme (a token)

Alias for: token
parse(www_authenticate) click to toggle source

Parsers the header. Returns an Array of challenges as strings

    # File lib/mechanize/http/www_authenticate_parser.rb, line 22
22:   def parse www_authenticate
23:     challenges = []
24:     @scanner = StringScanner.new www_authenticate
25: 
26:     while true do
27:       break if @scanner.eos?
28:       challenge = Mechanize::HTTP::AuthChallenge.new
29: 
30:       scheme = auth_scheme
31: 
32:       if scheme == 'Negotiate'
33:         scan_comma_spaces
34:       end
35: 
36:       next unless scheme
37:       challenge.scheme = scheme
38: 
39:       space = spaces
40: 
41:       if scheme == 'NTLM' then
42:         if space then
43:           challenge.params = @scanner.scan(/.*/)
44:         end
45: 
46:         challenges << challenge
47:         next
48:       else
49:         scheme.capitalize!
50:       end
51: 
52:       next unless space
53: 
54:       params = {}
55: 
56:       while true do
57:         pos = @scanner.pos
58:         name, value = auth_param
59: 
60:         name.downcase! if name =~ /^realm$/
61: 
62:         unless name then
63:           challenge.params = params
64:           challenges << challenge
65:           break if @scanner.eos?
66: 
67:           @scanner.pos = pos # rewind
68:           challenge = '' # a token should be next, new challenge
69:           break
70:         else
71:           params[name] = value
72:         end
73: 
74:         spaces
75: 
76:         return nil unless ',' == @scanner.peek(1) or @scanner.eos?
77: 
78:         @scanner.scan(/(, *)+/)
79:       end
80:     end
81: 
82:     challenges
83:   end
quoted_string() click to toggle source
  quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
  qdtext        = <any TEXT except <">>
  quoted-pair   = "\" CHAR

For TEXT, the rules of RFC 2047 are ignored.

     # File lib/mechanize/http/www_authenticate_parser.rb, line 146
146:   def quoted_string
147:     return nil unless @scanner.scan(/"/)
148: 
149:     text = ''
150: 
151:     while true do
152:       chunk = @scanner.scan(/[\r\n \t\0041\0043-\1176\2200-\3377]+/) # not "
153: 
154:       if chunk then
155:         text << chunk
156: 
157:         text << @scanner.get_byte if
158:           chunk.end_with? '\' and '"' == @scanner.peek(1)
159:       else
160:         if '"' == @scanner.peek(1) then
161:           @scanner.get_byte
162:           break
163:         else
164:           return nil
165:         end
166:       end
167:     end
168: 
169:     text
170:   end
scan_comma_spaces() click to toggle source

scans a comma followed by spaces needed for Negotiation, NTLM

     # File lib/mechanize/http/www_authenticate_parser.rb, line 99
 99:   def scan_comma_spaces
100:     @scanner.scan(/, +/)
101:   end
spaces() click to toggle source
  1*SP

Parses spaces

    # File lib/mechanize/http/www_authenticate_parser.rb, line 90
90:   def spaces
91:     @scanner.scan(/ +/)
92:   end
token() click to toggle source
  token = 1*<any CHAR except CTLs or separators>

Parses a token

     # File lib/mechanize/http/www_authenticate_parser.rb, line 108
108:   def token
109:     @scanner.scan(/[^\0000-\0037\1177()<>@,;:\\"\/\[\]?={} ]+/)
110:   end
Also aliased as: auth_scheme

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.