1 module hunt.http.routing.impl.AcceptHeaderMatcher; 2 3 import hunt.http.routing.impl.AbstractPreciseMatcher; 4 import hunt.http.routing.Matcher; 5 import hunt.http.routing.Router; 6 7 import hunt.text; 8 import hunt.Exceptions; 9 import hunt.collection; 10 11 import hunt.util.MimeTypeUtils; 12 import hunt.util.AcceptMimeType; 13 14 alias MatchType = Matcher.MatchType; 15 alias MatchResult = Matcher.MatchResult; 16 17 import std.algorithm; 18 import std.container.array; 19 import std.string; 20 21 import hunt.logging; 22 23 /** 24 * 25 */ 26 class AcceptHeaderMatcher : AbstractPreciseMatcher { 27 28 override MatchType getMatchType() { 29 return MatchType.ACCEPT; 30 } 31 32 private bool checkKey(AcceptMimeType type, string key) { 33 string[] t = StringUtils.split(key, "/"); 34 string p = t[0].strip(); 35 string c = t[1].strip(); 36 switch (type.getMatchType()) { 37 case AcceptMimeMatchType.EXACT: 38 return p.equals(type.getParentType()) && c.equals(type.getChildType()); 39 case AcceptMimeMatchType.CHILD: 40 return c.equals(type.getChildType()); 41 case AcceptMimeMatchType.PARENT: 42 return p.equals(type.getParentType()); 43 case AcceptMimeMatchType.ALL: 44 return true; 45 default: 46 return false; 47 } 48 } 49 50 override MatchResult match(string value) { 51 if (_map is null) { 52 return null; 53 } 54 55 Array!AcceptMimeType acceptMIMETypes = MimeTypeUtils.parseAcceptMIMETypes(value); 56 Set!Router set = new HashSet!Router(); 57 58 foreach (AcceptMimeType type ; acceptMIMETypes) { 59 foreach(string key, Set!(Router) v; _map) { 60 if(checkKey(type, key)) { 61 set.addAll(v); 62 } 63 } 64 65 if(!set.isEmpty()) 66 return new MatchResult(set, Collections.emptyMap!(Router, Map!(string, string))(), getMatchType()); 67 } 68 69 return null; 70 } 71 }