diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/RawHeaders.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/RawHeaders.java index e5abd2cfb..e1fdcf457 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/RawHeaders.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/RawHeaders.java @@ -123,23 +123,6 @@ public final class RawHeaders { this.httpMinorVersion = httpMinorVersion; } - public void computeResponseStatusLineFromSpdyHeaders() throws IOException { - String status = null; - String version = null; - for (int i = 0; i < namesAndValues.size(); i += 2) { - String name = namesAndValues.get(i); - if (":status".equals(name)) { - status = namesAndValues.get(i + 1); - } else if (":version".equals(name)) { - version = namesAndValues.get(i + 1); - } - } - if (status == null || version == null) { - throw new ProtocolException("Expected ':status' and ':version' headers not present"); - } - setStatusLine(version + " " + status); - } - /** * @param method like "GET", "POST", "HEAD", etc. * @param path like "/foo/bar.html" @@ -425,10 +408,13 @@ public final class RawHeaders { return result; } - public static RawHeaders fromNameValueBlock(List nameValueBlock) { + /** Returns headers for a name value block containing a SPDY response. */ + public static RawHeaders fromNameValueBlock(List nameValueBlock) throws IOException { if (nameValueBlock.size() % 2 != 0) { throw new IllegalArgumentException("Unexpected name value block: " + nameValueBlock); } + String status = null; + String version = null; RawHeaders result = new RawHeaders(); for (int i = 0; i < nameValueBlock.size(); i += 2) { String name = nameValueBlock.get(i); @@ -438,11 +424,21 @@ public final class RawHeaders { if (end == -1) { end = values.length(); } - result.namesAndValues.add(name); - result.namesAndValues.add(values.substring(start, end)); + String value = values.substring(start, end); + if (":status".equals(name)) { + status = value; + } else if (":version".equals(name)) { + version = value; + } else { + result.namesAndValues.add(name); + result.namesAndValues.add(value); + } start = end + 1; } } + if (status == null) throw new ProtocolException("Expected ':status' header not present"); + if (version == null) throw new ProtocolException("Expected ':version' header not present"); + result.setStatusLine(version + " " + status); return result; } } diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java index daa4e8010..a37a91c78 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java @@ -69,7 +69,6 @@ public final class SpdyTransport implements Transport { @Override public ResponseHeaders readResponseHeaders() throws IOException { List nameValueBlock = stream.getResponseHeaders(); RawHeaders rawHeaders = RawHeaders.fromNameValueBlock(nameValueBlock); - rawHeaders.computeResponseStatusLineFromSpdyHeaders(); httpEngine.receiveHeaders(rawHeaders); ResponseHeaders headers = new ResponseHeaders(httpEngine.uri, rawHeaders); diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/http/RawHeadersTest.java b/okhttp/src/test/java/com/squareup/okhttp/internal/http/RawHeadersTest.java index 474e5079e..7d8ecf3f3 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/http/RawHeadersTest.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/http/RawHeadersTest.java @@ -23,7 +23,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; public final class RawHeadersTest { - @Test public void parseNameValueBlock() { + @Test public void parseNameValueBlock() throws IOException { List nameValueBlock = Arrays.asList("cache-control", "no-cache, no-store", "set-cookie", "Cookie1\u0000Cookie2", ":status", "200 OK"); diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/spdy/HttpOverSpdyTest.java b/okhttp/src/test/java/com/squareup/okhttp/internal/spdy/HttpOverSpdyTest.java index 597008839..e17a120b7 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/spdy/HttpOverSpdyTest.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/spdy/HttpOverSpdyTest.java @@ -90,12 +90,14 @@ public final class HttpOverSpdyTest { } @Test public void get() throws Exception { - MockResponse response = new MockResponse().setBody("ABCDE"); + MockResponse response = new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet"); server.enqueue(response); server.play(); HttpURLConnection connection = client.open(server.getUrl("/foo")); assertContent("ABCDE", connection, Integer.MAX_VALUE); + assertEquals(200, connection.getResponseCode()); + assertEquals("Sweet", connection.getResponseMessage()); RecordedRequest request = server.takeRequest(); assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); @@ -211,6 +213,23 @@ public final class HttpOverSpdyTest { assertEquals(2, cache.getHitCount()); } + @Test public void conditionalCache() throws IOException { + client.setResponseCache(cache); + + server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A")); + server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)); + server.play(); + + assertContent("A", client.open(server.getUrl("/")), Integer.MAX_VALUE); + assertEquals(1, cache.getRequestCount()); + assertEquals(1, cache.getNetworkCount()); + assertEquals(0, cache.getHitCount()); + assertContent("A", client.open(server.getUrl("/")), Integer.MAX_VALUE); + assertEquals(2, cache.getRequestCount()); + assertEquals(2, cache.getNetworkCount()); + assertEquals(1, cache.getHitCount()); + } + @Test public void acceptAndTransmitCookies() throws Exception { CookieManager cookieManager = new CookieManager(); client.setCookieHandler(cookieManager);