From d19daa3dcd26bc74f7b5256ae77cbd9be091bec4 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Date: Fri, 4 Nov 2011 19:07:52 +0000 Subject: [PATCH] HTTPCLIENT-1137: The values for the Via header are created by httpclient-cache for each cached and backend request. Committing patch by alinachegalati at yahoo dot com. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1197713 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/client/cache/CachingHttpClient.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java index 543f5cea0..8627ce514 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java @@ -29,6 +29,7 @@ package org.apache.http.impl.client.cache; import java.io.IOException; import java.net.URI; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; @@ -119,7 +120,9 @@ public class CachingHttpClient implements HttpClient { private final AtomicLong cacheHits = new AtomicLong(); private final AtomicLong cacheMisses = new AtomicLong(); private final AtomicLong cacheUpdates = new AtomicLong(); - + + private final Map viaHeaders = new HashMap(4); + private final HttpClient backend; private final HttpCache responseCache; private final CacheValidityPolicy validityPolicy; @@ -371,7 +374,7 @@ public class CachingHttpClient implements HttpClient { setResponseStatus(context, CacheResponseStatus.CACHE_MISS); String via = generateViaHeader(request); - + if (clientRequestsOurOptions(request)) { setResponseStatus(context, CacheResponseStatus.CACHE_MODULE_RESPONSE); return new OptionsHttp11Response(); @@ -601,16 +604,25 @@ public class CachingHttpClient implements HttpClient { } private String generateViaHeader(HttpMessage msg) { + + final ProtocolVersion pv = msg.getProtocolVersion(); + String existingEntry = viaHeaders.get(pv); + if (existingEntry != null) return existingEntry; + final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client", getClass().getClassLoader()); final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE; - final ProtocolVersion pv = msg.getProtocolVersion(); + + String value; if ("http".equalsIgnoreCase(pv.getProtocol())) { - return String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", - pv.getMajor(), pv.getMinor(), release); + value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getMajor(), pv.getMinor(), + release); } else { - return String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", - pv.getProtocol(), pv.getMajor(), pv.getMinor(), release); + value = String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getProtocol(), pv.getMajor(), + pv.getMinor(), release); } + viaHeaders.put(pv, value); + + return value; } private void setResponseStatus(final HttpContext context, final CacheResponseStatus value) {