Skip to content

Commit c692e8c

Browse files
Henrik BaardJohan Redestig
authored andcommitted
Make HTTP Header class handle multiple cache-control fields.
The HTTP specification states the following about the fields: Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded. Change-Id: I1a6fe5cc8f541f8e80d559641d270d09eac9d85c
1 parent 2594066 commit c692e8c

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

core/java/android/net/http/Headers.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,14 @@ public void parseHeader(CharArrayBuffer buffer) {
262262
break;
263263
case HASH_CACHE_CONTROL:
264264
if (name.equals(CACHE_CONTROL)) {
265-
mHeaders[IDX_CACHE_CONTROL] = val;
265+
// In case where we receive more than one header, create a ',' separated list.
266+
// This should be ok, according to RFC 2616 chapter 4.2
267+
if (mHeaders[IDX_CACHE_CONTROL] != null &&
268+
mHeaders[IDX_CACHE_CONTROL].length() > 0) {
269+
mHeaders[IDX_CACHE_CONTROL] += (',' + val);
270+
} else {
271+
mHeaders[IDX_CACHE_CONTROL] = val;
272+
}
266273
}
267274
break;
268275
case HASH_LAST_MODIFIED:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package android.core;
17+
18+
import android.test.AndroidTestCase;
19+
import org.apache.http.util.CharArrayBuffer;
20+
21+
import android.net.http.Headers;
22+
23+
public class HttpHeaderTest extends AndroidTestCase {
24+
25+
static final String LAST_MODIFIED = "Last-Modified: Fri, 18 Jun 2010 09:56:47 GMT";
26+
static final String CACHE_CONTROL_MAX_AGE = "Cache-Control:max-age=15";
27+
static final String CACHE_CONTROL_PRIVATE = "Cache-Control: private";
28+
29+
/**
30+
* Tests that cache control header supports multiple instances of the header,
31+
* according to HTTP specification.
32+
*
33+
* The HTTP specification states the following about the fields:
34+
* Multiple message-header fields with the same field-name MAY be present
35+
* in a message if and only if the entire field-value for that header field
36+
* is defined as a comma-separated list [i.e., #(values)]. It MUST be
37+
* possible to combine the multiple header fields into one "field-name:
38+
* field-value" pair, without changing the semantics of the message, by
39+
* appending each subsequent field-value to the first, each separated by a
40+
* comma. The order in which header fields with the same field-name are
41+
* received is therefore significant to the interpretation of the combined
42+
* field value, and thus a proxy MUST NOT change the order of these field
43+
* values when a message is forwarded.
44+
*/
45+
public void testCacheControl() throws Exception {
46+
Headers h = new Headers();
47+
CharArrayBuffer buffer = new CharArrayBuffer(64);
48+
49+
buffer.append(CACHE_CONTROL_MAX_AGE);
50+
h.parseHeader(buffer);
51+
52+
buffer.clear();
53+
buffer.append(LAST_MODIFIED);
54+
h.parseHeader(buffer);
55+
assertEquals("max-age=15", h.getCacheControl());
56+
57+
buffer.clear();
58+
buffer.append(CACHE_CONTROL_PRIVATE);
59+
h.parseHeader(buffer);
60+
assertEquals("max-age=15,private", h.getCacheControl());
61+
}
62+
}

0 commit comments

Comments
 (0)