Skip to content

Commit

Permalink
Allow HTAB in header values.
Browse files Browse the repository at this point in the history
RFC 7230 section 3.2 allows HTAB ('\t', '\u0009') inside header
values as long as there is not more than one in a row:
  https://tools.ietf.org/html/rfc7230#section-3.2

Before this CL, OkHttp previously disallowed HTAB in header values.
This CL changes behavior to allow any number of consecutive HTABs
inside a header value; this is more permissive than the RFC, but
is consistent with how OkHttp currently treats space characters
(' ', '\u0020').
  • Loading branch information
15characterlimi committed Sep 7, 2016
1 parent 1a1b6ea commit 02b08fb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions okhttp-tests/src/test/java/okhttp3/HeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public final class HeadersTest {
.add("foo: bar")
.add(" foo: baz") // Name leading whitespace is trimmed.
.add("foo : bak") // Name trailing whitespace is trimmed.
.add("\tkey\t:\tvalue\t") // '\t' also counts as whitespace
.add("ping: pong ") // Value whitespace is trimmed.
.add("kit:kat") // Space after colon is not required.
.build();
assertEquals(Arrays.asList("bar", "baz", "bak"), headers.values("foo"));
assertEquals(Arrays.asList("value"), headers.values("key"));
assertEquals(Arrays.asList("pong"), headers.values("ping"));
assertEquals(Arrays.asList("kat"), headers.values("kit"));
}
Expand Down
11 changes: 10 additions & 1 deletion okhttp-tests/src/test/java/okhttp3/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,20 @@ public final class RequestTest {
}
}

@Test public void headerAllowsTabOnlyInValues() throws Exception {
Request.Builder builder = new Request.Builder();
builder.header("key", "sample\tvalue");
try {
builder.header("sample\tkey", "value");
fail();
} catch (IllegalArgumentException expected) {
}
}

@Test public void headerForbidsControlCharacters() throws Exception {
assertForbiddenHeader("\u0000");
assertForbiddenHeader("\r");
assertForbiddenHeader("\n");
assertForbiddenHeader("\t");
assertForbiddenHeader("\u001f");
assertForbiddenHeader("\u007f");
assertForbiddenHeader("\u0080");
Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private void checkNameAndValue(String name, String value) {
if (value == null) throw new NullPointerException("value == null");
for (int i = 0, length = value.length(); i < length; i++) {
char c = value.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
if ((c <= '\u001f' && c != '\u0009' /* htab */) || c >= '\u007f') {
throw new IllegalArgumentException(Util.format(
"Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value));
}
Expand Down

0 comments on commit 02b08fb

Please sign in to comment.