Skip to content
Permalink
Browse files
8234825: Better Headings for HTTP Servers
Reviewed-by: chegar, dfuchs, igerasim
  • Loading branch information
Michael-Mc-Mahon committed Dec 16, 2019
1 parent 2309ac5 commit 8e5af26326a806ec8d33c14e7651795ea7250223
Showing 1 changed file with 32 additions and 2 deletions.
@@ -82,11 +82,14 @@ private String normalize (String key) {
char[] b = key.toCharArray();
if (b[0] >= 'a' && b[0] <= 'z') {
b[0] = (char)(b[0] - ('a' - 'A'));
}
} else if (b[0] == '\r' || b[0] == '\n')
throw new IllegalArgumentException("illegal character in key");

for (int i=1; i<len; i++) {
if (b[i] >= 'A' && b[i] <= 'Z') {
b[i] = (char) (b[i] + ('a' - 'A'));
}
} else if (b[i] == '\r' || b[i] == '\n')
throw new IllegalArgumentException("illegal character in key");
}
return new String(b);
}
@@ -128,6 +131,8 @@ public String getFirst (String key) {
}

public List<String> put(String key, List<String> value) {
for (String v : value)
checkValue(v);
return map.put (normalize(key), value);
}

@@ -139,6 +144,7 @@ public List<String> put(String key, List<String> value) {
* @param value the header value to add to the header
*/
public void add (String key, String value) {
checkValue(value);
String k = normalize(key);
List<String> l = map.get(k);
if (l == null) {
@@ -148,6 +154,30 @@ public void add (String key, String value) {
l.add (value);
}

private static void checkValue(String value) {
int len = value.length();
for (int i=0; i<len; i++) {
char c = value.charAt(i);
if (c == '\r') {
// is allowed if it is followed by \n and a whitespace char
if (i >= len - 2) {
throw new IllegalArgumentException("Illegal CR found in header");
}
char c1 = value.charAt(i+1);
char c2 = value.charAt(i+2);
if (c1 != '\n') {
throw new IllegalArgumentException("Illegal char found after CR in header");
}
if (c2 != ' ' && c2 != '\t') {
throw new IllegalArgumentException("No whitespace found after CRLF in header");
}
i+=2;
} else if (c == '\n') {
throw new IllegalArgumentException("Illegal LF found in header");
}
}
}

/**
* sets the given value as the sole header value
* for the given key. If the mapping does not

0 comments on commit 8e5af26

Please sign in to comment.