< prev index next >

src/share/classes/java/net/HttpCookie.java

Print this page
rev 1562 : 6901170: HttpCookie parsing of version and max-age mis-handled
Summary: Accept single quotes in cookies and better exception handling in CookieManager
Reviewed-by: chegar


 975                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 976                     if (cookie.getPath() == null) cookie.setPath(attrValue);
 977                 }
 978             });
 979         assignors.put("port", new CookieAttributeAssignor(){
 980                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 981                     if (cookie.getPortlist() == null) cookie.setPortlist(attrValue);
 982                 }
 983             });
 984         assignors.put("secure", new CookieAttributeAssignor(){
 985                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 986                     cookie.setSecure(true);
 987                 }
 988             });
 989         assignors.put("version", new CookieAttributeAssignor(){
 990                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 991                     try {
 992                         int version = Integer.parseInt(attrValue);
 993                         cookie.setVersion(version);
 994                     } catch (NumberFormatException ignored) {
 995                         throw new IllegalArgumentException("Illegal cookie version attribute");
 996                     }
 997                 }
 998             });
 999         assignors.put("expires", new CookieAttributeAssignor(){ // Netscape only
1000                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
1001                     if (cookie.getMaxAge() == MAX_AGE_UNSPECIFIED) {
1002                         cookie.setMaxAge(cookie.expiryDate2DeltaSeconds(attrValue));
1003                     }
1004                 }
1005             });
1006     }
1007     private static void assignAttribute(HttpCookie cookie,
1008                                        String attrName,
1009                                        String attrValue)
1010     {
1011         // strip off the surrounding "-sign if there's any
1012         attrValue = stripOffSurroundingQuote(attrValue);
1013 
1014         CookieAttributeAssignor assignor = assignors.get(attrName.toLowerCase());
1015         if (assignor != null) {


1080 
1081         header = header.toLowerCase();
1082         if (header.indexOf("expires=") != -1) {
1083             // only netscape cookie using 'expires'
1084             version = 0;
1085         } else if (header.indexOf("version=") != -1) {
1086             // version is mandatory for rfc 2965/2109 cookie
1087             version = 1;
1088         } else if (header.indexOf("max-age") != -1) {
1089             // rfc 2965/2109 use 'max-age'
1090             version = 1;
1091         } else if (startsWithIgnoreCase(header, SET_COOKIE2)) {
1092             // only rfc 2965 cookie starts with 'set-cookie2'
1093             version = 1;
1094         }
1095 
1096         return version;
1097     }
1098 
1099     private static String stripOffSurroundingQuote(String str) {
1100         if (str != null && str.length() > 0 &&
1101             str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
1102             return str.substring(1, str.length() - 1);
1103         } else {
1104             return str;
1105         }





1106     }
1107 
1108     private static boolean equalsIgnoreCase(String s, String t) {
1109         if (s == t) return true;
1110         if ((s != null) && (t != null)) {
1111             return s.equalsIgnoreCase(t);
1112         }
1113         return false;
1114     }
1115 
1116     private static boolean equals(String s, String t) {
1117         if (s == t) return true;
1118         if ((s != null) && (t != null)) {
1119             return s.equals(t);
1120         }
1121         return false;
1122     }
1123 
1124     private static boolean startsWithIgnoreCase(String s, String start) {
1125         if (s == null || start == null) return false;




 975                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 976                     if (cookie.getPath() == null) cookie.setPath(attrValue);
 977                 }
 978             });
 979         assignors.put("port", new CookieAttributeAssignor(){
 980                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 981                     if (cookie.getPortlist() == null) cookie.setPortlist(attrValue);
 982                 }
 983             });
 984         assignors.put("secure", new CookieAttributeAssignor(){
 985                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 986                     cookie.setSecure(true);
 987                 }
 988             });
 989         assignors.put("version", new CookieAttributeAssignor(){
 990                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
 991                     try {
 992                         int version = Integer.parseInt(attrValue);
 993                         cookie.setVersion(version);
 994                     } catch (NumberFormatException ignored) {
 995                         // Just ignore bogus version, it will default to 0 or 1
 996                     }
 997                 }
 998             });
 999         assignors.put("expires", new CookieAttributeAssignor(){ // Netscape only
1000                 public void assign(HttpCookie cookie, String attrName, String attrValue) {
1001                     if (cookie.getMaxAge() == MAX_AGE_UNSPECIFIED) {
1002                         cookie.setMaxAge(cookie.expiryDate2DeltaSeconds(attrValue));
1003                     }
1004                 }
1005             });
1006     }
1007     private static void assignAttribute(HttpCookie cookie,
1008                                        String attrName,
1009                                        String attrValue)
1010     {
1011         // strip off the surrounding "-sign if there's any
1012         attrValue = stripOffSurroundingQuote(attrValue);
1013 
1014         CookieAttributeAssignor assignor = assignors.get(attrName.toLowerCase());
1015         if (assignor != null) {


1080 
1081         header = header.toLowerCase();
1082         if (header.indexOf("expires=") != -1) {
1083             // only netscape cookie using 'expires'
1084             version = 0;
1085         } else if (header.indexOf("version=") != -1) {
1086             // version is mandatory for rfc 2965/2109 cookie
1087             version = 1;
1088         } else if (header.indexOf("max-age") != -1) {
1089             // rfc 2965/2109 use 'max-age'
1090             version = 1;
1091         } else if (startsWithIgnoreCase(header, SET_COOKIE2)) {
1092             // only rfc 2965 cookie starts with 'set-cookie2'
1093             version = 1;
1094         }
1095 
1096         return version;
1097     }
1098 
1099     private static String stripOffSurroundingQuote(String str) {
1100         if (str != null && str.length() > 2 &&
1101             str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
1102             return str.substring(1, str.length() - 1);


1103         }
1104         if (str != null && str.length() > 2 &&
1105             str.charAt(0) == '\'' && str.charAt(str.length() - 1) == '\'') {
1106             return str.substring(1, str.length() - 1);
1107         }
1108         return str;
1109     }
1110 
1111     private static boolean equalsIgnoreCase(String s, String t) {
1112         if (s == t) return true;
1113         if ((s != null) && (t != null)) {
1114             return s.equalsIgnoreCase(t);
1115         }
1116         return false;
1117     }
1118 
1119     private static boolean equals(String s, String t) {
1120         if (s == t) return true;
1121         if ((s != null) && (t != null)) {
1122             return s.equals(t);
1123         }
1124         return false;
1125     }
1126 
1127     private static boolean startsWithIgnoreCase(String s, String start) {
1128         if (s == null || start == null) return false;


< prev index next >