Mercurial Hosting > luan
comparison src/luan/modules/url/WwwAuthenticate.java @ 1575:0160650180bc
better http authentication
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 27 Jan 2021 15:31:26 -0700 |
parents | 27efb1fcbcb5 |
children | dbf63bef4b86 |
comparison
equal
deleted
inserted
replaced
1574:f118ead273a1 | 1575:0160650180bc |
---|---|
1 package luan.modules.url; | 1 package luan.modules.url; |
2 | 2 |
3 import java.util.Map; | 3 import java.util.Map; |
4 import java.util.HashMap; | 4 import java.util.HashMap; |
5 import java.util.List; | |
6 import java.util.ArrayList; | |
5 import goodjava.parser.Parser; | 7 import goodjava.parser.Parser; |
6 import goodjava.parser.ParseException; | 8 import goodjava.parser.ParseException; |
9 import goodjava.logging.Logger; | |
10 import goodjava.logging.LoggerFactory; | |
7 | 11 |
8 | 12 |
9 public final class WwwAuthenticate { | 13 public final class WwwAuthenticate { |
14 private static final Logger logger = LoggerFactory.getLogger(WwwAuthenticate.class); | |
15 | |
16 public static List<WwwAuthenticate> parse(String header) throws ParseException { | |
17 Parser parser = new Parser(header); | |
18 List<WwwAuthenticate> auths = new ArrayList<WwwAuthenticate>(); | |
19 do { | |
20 auths.add(new WwwAuthenticate(parser,header)); | |
21 } while( !parser.endOfInput() ); | |
22 return auths; | |
23 } | |
24 | |
10 public final String type; | 25 public final String type; |
11 public final Map<String,String> options = new HashMap<String,String>(); | 26 public final Map<String,String> options = new HashMap<String,String>(); |
12 private final Parser parser; | 27 private final Parser parser; |
13 | 28 |
14 public WwwAuthenticate(String header) throws ParseException { | 29 private WwwAuthenticate(Parser parser,String header) throws ParseException { |
15 parser = new Parser(header); | 30 this.parser = parser; |
16 type = parseType(); | 31 type = parseType(); |
17 if( !matchSpace() ) | 32 if( !matchSpace() ) |
18 throw new ParseException(parser,"space expected"); | 33 throw new ParseException(parser,"space expected"); |
19 do { | 34 do { |
20 while( matchSpace() ); | 35 while( matchSpace() ); |
21 int start = parser.currentIndex(); | 36 if( !parseOption() ) |
22 while( parser.inCharRange('a','z') ); | 37 return; |
23 String name = parser.textFrom(start); | |
24 if( name.length() == 0 ) | |
25 throw new ParseException(parser,"option name not found"); | |
26 if( !parser.match('=') ) | |
27 throw new ParseException(parser,"'=' expected"); | |
28 if( !parser.match('"') ) | |
29 throw new ParseException(parser,"'\"' expected"); | |
30 start = parser.currentIndex(); | |
31 while( !parser.test('"') ) { | |
32 if( !parser.anyChar() ) | |
33 throw new ParseException(parser,"unexpected end of text"); | |
34 } | |
35 String value = parser.textFrom(start); | |
36 if( !parser.match('"') ) | |
37 throw new ParseException(parser,"'\"' expected"); | |
38 options.put(name,value); | |
39 while( matchSpace() ); | |
40 } while( parser.match(',') ); | 38 } while( parser.match(',') ); |
41 if( !parser.endOfInput() ) | 39 if( !parser.endOfInput() ) |
42 throw new ParseException(parser,"unexpected input"); | 40 throw new ParseException(parser,"unexpected input"); |
43 } | 41 } |
44 | 42 |
45 private String parseType() throws ParseException { | 43 private String parseType() throws ParseException { |
46 if( parser.match("Basic") ) | 44 int start = parser.currentIndex(); |
47 return "Basic"; | 45 if( !parser.inCharRange('A','Z') ) |
48 if( parser.match("Digest") ) | 46 throw new ParseException(parser,"invalid type"); |
49 return "Digest"; | 47 while( parser.inCharRange('a','z') ); |
50 throw new ParseException(parser,"invalid type"); | 48 return parser.textFrom(start); |
49 } | |
50 | |
51 private boolean parseOption() throws ParseException { | |
52 int start = parser.begin(); | |
53 while( parser.inCharRange('a','z') ); | |
54 String name = parser.textFrom(start); | |
55 if( name.length() == 0 ) | |
56 return parser.failure(); | |
57 if( !parser.match('=') ) | |
58 throw new ParseException(parser,"'=' expected"); | |
59 if( !parser.match('"') ) | |
60 throw new ParseException(parser,"'\"' expected"); | |
61 start = parser.currentIndex(); | |
62 while( !parser.test('"') ) { | |
63 if( !parser.anyChar() ) | |
64 throw new ParseException(parser,"unexpected end of text"); | |
65 } | |
66 String value = parser.textFrom(start); | |
67 if( !parser.match('"') ) | |
68 throw new ParseException(parser,"'\"' expected"); | |
69 options.put(name,value); | |
70 while( matchSpace() ); | |
71 return parser.success(); | |
51 } | 72 } |
52 | 73 |
53 private boolean matchSpace() { | 74 private boolean matchSpace() { |
54 return parser.anyOf(" \t\r\n"); | 75 return parser.anyOf(" \t\r\n"); |
55 } | 76 } |