HTTPCLIENT-1144: Caching client has a class for common headers that was not being used consistently in the code

Contributed by Joe Campbell <joseph.r.campbell at gmail.com>

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1209502 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-12-02 14:13:52 +00:00
parent c4ab5838f6
commit 5b89c1097f
11 changed files with 99 additions and 83 deletions

View File

@ -56,6 +56,9 @@ public class HeaderConstants {
public static final String AGE = "Age";
public static final String VARY = "Vary";
public static final String ALLOW = "Allow";
public static final String VIA = "Via";
public static final String PUBLIC = "public";
public static final String PRIVATE = "private";
public static final String CACHE_CONTROL = "Cache-Control";
public static final String CACHE_CONTROL_NO_STORE = "no-store";
@ -65,11 +68,14 @@ public class HeaderConstants {
public static final String CACHE_CONTROL_MIN_FRESH = "min-fresh";
public static final String CACHE_CONTROL_MUST_REVALIDATE = "must-revalidate";
public static final String CACHE_CONTROL_PROXY_REVALIDATE = "proxy-revalidate";
public static final String STALE_IF_ERROR = "stale-if-error";
public static final String STALE_WHILE_REVALIDATE = "stale-while-revalidate";
public static final String WARNING = "Warning";
public static final String RANGE = "Range";
public static final String CONTENT_RANGE = "Content-Range";
public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate";
public static final String AUTHORIZATION = "Authorization";
}

View File

@ -47,6 +47,7 @@ import org.apache.http.client.cache.Resource;
import org.apache.http.client.cache.ResourceFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HTTP;
class BasicHttpCache implements HttpCache {
@ -157,7 +158,7 @@ class BasicHttpCache implements HttpCache {
&& status != HttpStatus.SC_PARTIAL_CONTENT) {
return false;
}
Header hdr = resp.getFirstHeader("Content-Length");
Header hdr = resp.getFirstHeader(HTTP.CONTENT_LEN);
if (hdr == null) return false;
int contentLength;
try {
@ -170,7 +171,7 @@ class BasicHttpCache implements HttpCache {
HttpResponse generateIncompleteResponseError(HttpResponse response,
Resource resource) {
int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
int contentLength = Integer.parseInt(response.getFirstHeader(HTTP.CONTENT_LEN).getValue());
HttpResponse error =
new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_GATEWAY, "Bad Gateway");
error.setHeader("Content-Type","text/plain;charset=UTF-8");

View File

@ -43,6 +43,7 @@ import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.protocol.HTTP;
/**
* Given a particular HttpRequest, flush any cache entries that this request
@ -215,16 +216,16 @@ class CacheInvalidator {
private boolean responseAndEntryEtagsDiffer(HttpResponse response,
HttpCacheEntry entry) {
Header entryEtag = entry.getFirstHeader("ETag");
Header responseEtag = response.getFirstHeader("ETag");
Header entryEtag = entry.getFirstHeader(HeaderConstants.ETAG);
Header responseEtag = response.getFirstHeader(HeaderConstants.ETAG);
if (entryEtag == null || responseEtag == null) return false;
return (!entryEtag.getValue().equals(responseEtag.getValue()));
}
private boolean responseDateNewerThanEntryDate(HttpResponse response,
HttpCacheEntry entry) {
Header entryDateHeader = entry.getFirstHeader("Date");
Header responseDateHeader = response.getFirstHeader("Date");
Header entryDateHeader = entry.getFirstHeader(HTTP.DATE_HEADER);
Header responseDateHeader = response.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader == null || responseDateHeader == null) {
return false;
}

View File

@ -113,17 +113,17 @@ class CacheValidityPolicy {
}
public boolean mustRevalidate(final HttpCacheEntry entry) {
return hasCacheControlDirective(entry, "must-revalidate");
return hasCacheControlDirective(entry, HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE);
}
public boolean proxyRevalidate(final HttpCacheEntry entry) {
return hasCacheControlDirective(entry, "proxy-revalidate");
return hasCacheControlDirective(entry, HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE);
}
public boolean mayReturnStaleWhileRevalidating(final HttpCacheEntry entry, Date now) {
for (Header h : entry.getHeaders("Cache-Control")) {
for (Header h : entry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for(HeaderElement elt : h.getElements()) {
if ("stale-while-revalidate".equalsIgnoreCase(elt.getName())) {
if (HeaderConstants.STALE_WHILE_REVALIDATE.equalsIgnoreCase(elt.getName())) {
try {
int allowedStalenessLifetime = Integer.parseInt(elt.getValue());
if (getStalenessSecs(entry, now) <= allowedStalenessLifetime) {
@ -142,9 +142,9 @@ class CacheValidityPolicy {
public boolean mayReturnStaleIfError(HttpRequest request,
HttpCacheEntry entry, Date now) {
long stalenessSecs = getStalenessSecs(entry, now);
return mayReturnStaleIfError(request.getHeaders("Cache-Control"),
return mayReturnStaleIfError(request.getHeaders(HeaderConstants.CACHE_CONTROL),
stalenessSecs)
|| mayReturnStaleIfError(entry.getHeaders("Cache-Control"),
|| mayReturnStaleIfError(entry.getHeaders(HeaderConstants.CACHE_CONTROL),
stalenessSecs);
}
@ -152,7 +152,7 @@ class CacheValidityPolicy {
boolean result = false;
for(Header h : headers) {
for(HeaderElement elt : h.getElements()) {
if ("stale-if-error".equals(elt.getName())) {
if (HeaderConstants.STALE_IF_ERROR.equals(elt.getName())) {
try {
int staleIfErrorSecs = Integer.parseInt(elt.getValue());
if (stalenessSecs <= staleIfErrorSecs) {
@ -304,7 +304,7 @@ class CacheValidityPolicy {
public boolean hasCacheControlDirective(final HttpCacheEntry entry,
final String directive) {
for (Header h : entry.getHeaders("Cache-Control")) {
for (Header h : entry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for(HeaderElement elt : h.getElements()) {
if (directive.equalsIgnoreCase(elt.getName())) {
return true;

View File

@ -61,10 +61,10 @@ class CachedHttpResponseGenerator {
}
/**
* If I was able to use a {@link CacheEntry} to response to the {@link org.apache.http.HttpRequest} then
* If I was able to use a {@link CacheEntity} to response to the {@link org.apache.http.HttpRequest} then
* generate an {@link HttpResponse} based on the cache entry.
* @param entry
* {@link CacheEntry} to transform into an {@link HttpResponse}
* {@link CacheEntity} to transform into an {@link HttpResponse}
* @return {@link HttpResponse} that was constructed
*/
HttpResponse generateResponse(HttpCacheEntry entry) {
@ -92,8 +92,8 @@ class CachedHttpResponseGenerator {
}
/**
* Generate a 304 - Not Modified response from a {@link CacheEntry}. This should be
* used to respond to conditional requests, when the entry exists or has been revalidated.
* Generate a 304 - Not Modified response from a {@link CacheEntity}. This should be
* used to respond to conditional requests, when the entry exists or has been re-validated.
*
* @param entry
* @return
@ -107,15 +107,15 @@ class CachedHttpResponseGenerator {
// (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
// - Date, unless its omission is required by section 14.8.1
Header dateHeader = entry.getFirstHeader("Date");
Header dateHeader = entry.getFirstHeader(HTTP.DATE_HEADER);
if (dateHeader == null) {
dateHeader = new BasicHeader("Date", DateUtils.formatDate(new Date()));
dateHeader = new BasicHeader(HTTP.DATE_HEADER, DateUtils.formatDate(new Date()));
}
response.addHeader(dateHeader);
// - ETag and/or Content-Location, if the header would have been sent
// in a 200 response to the same request
Header etagHeader = entry.getFirstHeader("ETag");
Header etagHeader = entry.getFirstHeader(HeaderConstants.ETAG);
if (etagHeader != null) {
response.addHeader(etagHeader);
}
@ -128,17 +128,17 @@ class CachedHttpResponseGenerator {
// - Expires, Cache-Control, and/or Vary, if the field-value might
// differ from that sent in any previous response for the same
// variant
Header expiresHeader = entry.getFirstHeader("Expires");
Header expiresHeader = entry.getFirstHeader(HeaderConstants.EXPIRES);
if (expiresHeader != null) {
response.addHeader(expiresHeader);
}
Header cacheControlHeader = entry.getFirstHeader("Cache-Control");
Header cacheControlHeader = entry.getFirstHeader(HeaderConstants.CACHE_CONTROL);
if (cacheControlHeader != null) {
response.addHeader(cacheControlHeader);
}
Header varyHeader = entry.getFirstHeader("Vary");
Header varyHeader = entry.getFirstHeader(HeaderConstants.VARY);
if (varyHeader != null) {
response.addHeader(varyHeader);
}

View File

@ -91,9 +91,9 @@ class CachedResponseSuitabilityChecker {
private long getMaxStale(HttpRequest request) {
long maxstale = -1;
for(Header h : request.getHeaders("Cache-Control")) {
for(Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for(HeaderElement elt : h.getElements()) {
if ("max-stale".equals(elt.getName())) {
if (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
if ((elt.getValue() == null || "".equals(elt.getValue().trim()))
&& maxstale == -1) {
maxstale = Long.MAX_VALUE;
@ -246,9 +246,9 @@ class CachedResponseSuitabilityChecker {
}
private boolean hasUnsupportedConditionalHeaders(HttpRequest request) {
return (request.getFirstHeader("If-Range") != null
|| request.getFirstHeader("If-Match") != null
|| hasValidDateField(request, "If-Unmodified-Since"));
return (request.getFirstHeader(HeaderConstants.IF_RANGE) != null
|| request.getFirstHeader(HeaderConstants.IF_MATCH) != null
|| hasValidDateField(request, HeaderConstants.IF_UNMODIFIED_SINCE));
}
private boolean hasSupportedEtagValidator(HttpRequest request) {

View File

@ -64,6 +64,7 @@ import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.VersionInfo;
@ -122,9 +123,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<ProtocolVersion, String> viaHeaders = new HashMap<ProtocolVersion, String>(4);
private final HttpClient backend;
private final HttpCache responseCache;
private final CacheValidityPolicy validityPolicy;
@ -376,7 +377,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();
@ -538,7 +539,7 @@ public class CachingHttpClient implements HttpClient {
}
setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
if (validityPolicy.getStalenessSecs(entry, now) > 0L) {
cachedResponse.addHeader("Warning","110 localhost \"Response is stale\"");
cachedResponse.addHeader(HeaderConstants.WARNING,"110 localhost \"Response is stale\"");
}
return cachedResponse;
}
@ -574,7 +575,7 @@ public class CachingHttpClient implements HttpClient {
}
private boolean mayCallBackend(HttpRequest request) {
for (Header h: request.getHeaders("Cache-Control")) {
for (Header h: request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for (HeaderElement elt : h.getElements()) {
if ("only-if-cached".equals(elt.getName())) {
return false;
@ -585,9 +586,9 @@ public class CachingHttpClient implements HttpClient {
}
private boolean explicitFreshnessRequest(HttpRequest request, HttpCacheEntry entry, Date now) {
for(Header h : request.getHeaders("Cache-Control")) {
for(Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for(HeaderElement elt : h.getElements()) {
if ("max-stale".equals(elt.getName())) {
if (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
try {
int maxstale = Integer.parseInt(elt.getValue());
long age = validityPolicy.getCurrentAgeSecs(entry, now);
@ -596,8 +597,8 @@ public class CachingHttpClient implements HttpClient {
} catch (NumberFormatException nfe) {
return true;
}
} else if ("min-fresh".equals(elt.getName())
|| "max-age".equals(elt.getName())) {
} else if (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())
|| HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
return true;
}
}
@ -606,7 +607,7 @@ 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;
@ -688,8 +689,8 @@ public class CachingHttpClient implements HttpClient {
private boolean revalidationResponseIsTooOld(HttpResponse backendResponse,
HttpCacheEntry cacheEntry) {
final Header entryDateHeader = cacheEntry.getFirstHeader("Date");
final Header responseDateHeader = backendResponse.getFirstHeader("Date");
final Header entryDateHeader = cacheEntry.getFirstHeader(HTTP.DATE_HEADER);
final Header responseDateHeader = backendResponse.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader != null && responseDateHeader != null) {
try {
Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
@ -813,7 +814,7 @@ public class CachingHttpClient implements HttpClient {
responseDate = getCurrentDate();
}
backendResponse.addHeader("Via", generateViaHeader(backendResponse));
backendResponse.addHeader(HeaderConstants.VIA, generateViaHeader(backendResponse));
int statusCode = backendResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_NOT_MODIFIED || statusCode == HttpStatus.SC_OK) {
@ -891,15 +892,16 @@ public class CachingHttpClient implements HttpClient {
// nop
}
if (existing == null) return false;
Header entryDateHeader = existing.getFirstHeader("Date");
Header entryDateHeader = existing.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader == null) return false;
Header responseDateHeader = backendResponse.getFirstHeader("Date");
Header responseDateHeader = backendResponse.getFirstHeader(HTTP.DATE_HEADER);
if (responseDateHeader == null) return false;
try {
Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
Date responseDate = DateUtils.parseDate(responseDateHeader.getValue());
return responseDate.before(entryDate);
} catch (DateParseException e) {
// Empty on Purpose
}
return false;
}

View File

@ -53,7 +53,7 @@ class ConditionalRequestBuilder {
* the origin. Build the origin {@link HttpRequest} here and return it.
*
* @param request the original request from the caller
* @param cacheEntry the entry that needs to be revalidated
* @param cacheEntry the entry that needs to be re-validated
* @return the wrapped request
* @throws ProtocolException when I am unable to build a new origin request.
*/
@ -80,7 +80,7 @@ class ConditionalRequestBuilder {
}
}
if (mustRevalidate) {
wrapperRequest.addHeader("Cache-Control","max-age=0");
wrapperRequest.addHeader(HeaderConstants.CACHE_CONTROL, HeaderConstants.CACHE_CONTROL_MAX_AGE + "=0");
}
return wrapperRequest;
@ -133,8 +133,7 @@ class ConditionalRequestBuilder {
* @param entry existing cache entry we are trying to validate
* @return an unconditional validation request
*/
public HttpRequest buildUnconditionalRequest(HttpRequest request,
HttpCacheEntry entry) {
public HttpRequest buildUnconditionalRequest(HttpRequest request, HttpCacheEntry entry) {
RequestWrapper wrapped;
try {
wrapped = new RequestWrapper(request);
@ -143,13 +142,13 @@ class ConditionalRequestBuilder {
return request;
}
wrapped.resetHeaders();
wrapped.addHeader("Cache-Control","no-cache");
wrapped.addHeader("Pragma","no-cache");
wrapped.removeHeaders("If-Range");
wrapped.removeHeaders("If-Match");
wrapped.removeHeaders("If-None-Match");
wrapped.removeHeaders("If-Unmodified-Since");
wrapped.removeHeaders("If-Modified-Since");
wrapped.addHeader(HeaderConstants.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
wrapped.addHeader(HeaderConstants.PRAGMA,HeaderConstants.CACHE_CONTROL_NO_CACHE);
wrapped.removeHeaders(HeaderConstants.IF_RANGE);
wrapped.removeHeaders(HeaderConstants.IF_MATCH);
wrapped.removeHeaders(HeaderConstants.IF_NONE_MATCH);
wrapped.removeHeaders(HeaderConstants.IF_UNMODIFIED_SINCE);
wrapped.removeHeaders(HeaderConstants.IF_MODIFIED_SINCE);
return wrapped;
}

View File

@ -56,7 +56,7 @@ import org.apache.http.protocol.HTTP;
class RequestProtocolCompliance {
private static final List<String> disallowedWithNoCache =
Arrays.asList("min-fresh", "max-stale", "max-age");
Arrays.asList(HeaderConstants.CACHE_CONTROL_MIN_FRESH, HeaderConstants.CACHE_CONTROL_MAX_STALE, HeaderConstants.CACHE_CONTROL_MAX_AGE);
/**
* Test to see if the {@link HttpRequest} is HTTP1.1 compliant or not
@ -120,19 +120,19 @@ class RequestProtocolCompliance {
private void stripOtherFreshnessDirectivesWithNoCache(HttpRequest request) {
List<HeaderElement> outElts = new ArrayList<HeaderElement>();
boolean shouldStrip = false;
for(Header h : request.getHeaders("Cache-Control")) {
for(Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for(HeaderElement elt : h.getElements()) {
if (!disallowedWithNoCache.contains(elt.getName())) {
outElts.add(elt);
}
if ("no-cache".equals(elt.getName())) {
if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
shouldStrip = true;
}
}
}
if (!shouldStrip) return;
request.removeHeaders("Cache-Control");
request.setHeader("Cache-Control", buildHeaderFromElements(outElts));
request.removeHeaders(HeaderConstants.CACHE_CONTROL);
request.setHeader(HeaderConstants.CACHE_CONTROL, buildHeaderFromElements(outElts));
}
private String buildHeaderFromElements(List<HeaderElement> outElts) {
@ -376,9 +376,9 @@ class RequestProtocolCompliance {
}
private RequestProtocolError requestContainsNoCacheDirectiveWithFieldName(HttpRequest request) {
for(Header h : request.getHeaders("Cache-Control")) {
for(Header h : request.getHeaders(HeaderConstants.CACHE_CONTROL)) {
for(HeaderElement elt : h.getElements()) {
if ("no-cache".equalsIgnoreCase(elt.getName())
if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equalsIgnoreCase(elt.getName())
&& elt.getValue() != null) {
return RequestProtocolError.NO_CACHE_DIRECTIVE_WITH_FIELD_NAME;
}

View File

@ -154,7 +154,7 @@ class ResponseCachingPolicy {
for (HeaderElement elem : header.getElements()) {
if (HeaderConstants.CACHE_CONTROL_NO_STORE.equals(elem.getName())
|| HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elem.getName())
|| (sharedCache && "private".equals(elem.getName()))) {
|| (sharedCache && HeaderConstants.PRIVATE.equals(elem.getName()))) {
return true;
}
}
@ -179,8 +179,10 @@ class ResponseCachingPolicy {
protected boolean isExplicitlyCacheable(HttpResponse response) {
if (response.getFirstHeader(HeaderConstants.EXPIRES) != null)
return true;
String[] cacheableParams = { "max-age", "s-maxage",
"must-revalidate", "proxy-revalidate", "public"
String[] cacheableParams = { HeaderConstants.CACHE_CONTROL_MAX_AGE, "s-maxage",
HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE,
HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE,
HeaderConstants.PUBLIC
};
return hasCacheControlParameterFrom(response, cacheableParams);
}
@ -199,7 +201,7 @@ class ResponseCachingPolicy {
return false;
}
String[] uncacheableRequestDirectives = { "no-store" };
String[] uncacheableRequestDirectives = { HeaderConstants.CACHE_CONTROL_NO_STORE };
if (hasCacheControlParameterFrom(request,uncacheableRequestDirectives)) {
return false;
}
@ -215,10 +217,10 @@ class ResponseCachingPolicy {
}
if (sharedCache) {
Header[] authNHeaders = request.getHeaders("Authorization");
Header[] authNHeaders = request.getHeaders(HeaderConstants.AUTHORIZATION);
if (authNHeaders != null && authNHeaders.length > 0) {
String[] authCacheableParams = {
"s-maxage", "must-revalidate", "public"
"s-maxage", HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE, HeaderConstants.PUBLIC
};
return hasCacheControlParameterFrom(response, authCacheableParams);
}
@ -230,9 +232,9 @@ class ResponseCachingPolicy {
private boolean expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl(
HttpResponse response) {
if (response.getFirstHeader("Cache-Control") != null) return false;
Header expiresHdr = response.getFirstHeader("Expires");
Header dateHdr = response.getFirstHeader("Date");
if (response.getFirstHeader(HeaderConstants.CACHE_CONTROL) != null) return false;
Header expiresHdr = response.getFirstHeader(HeaderConstants.EXPIRES);
Header dateHdr = response.getFirstHeader(HTTP.DATE_HEADER);
if (expiresHdr == null || dateHdr == null) return false;
try {
Date expires = DateUtils.parseDate(expiresHdr.getValue());
@ -244,7 +246,7 @@ class ResponseCachingPolicy {
}
private boolean from1_0Origin(HttpResponse response) {
Header via = response.getFirstHeader("Via");
Header via = response.getFirstHeader(HeaderConstants.VIA);
if (via != null) {
for(HeaderElement elt : via.getElements()) {
String proto = elt.toString().split("\\s")[0];

View File

@ -67,7 +67,7 @@ class ResponseProtocolCompliance {
*
* @param request The {@link HttpRequest} that generated an origin hit and response
* @param response The {@link HttpResponse} from the origin server
* @throws IOException
* @throws IOException Bad things happened
*/
public void ensureProtocolCompliance(HttpRequest request, HttpResponse response)
throws IOException {
@ -102,26 +102,31 @@ class ResponseProtocolCompliance {
HttpResponse response) {
Date responseDate = null;
try {
responseDate = DateUtils.parseDate(response.getFirstHeader("Date").getValue());
responseDate = DateUtils.parseDate(response.getFirstHeader(HTTP.DATE_HEADER).getValue());
} catch (DateParseException e) {
//Empty On Purpose
}
if (responseDate == null) return;
Header[] warningHeaders = response.getHeaders("Warning");
Header[] warningHeaders = response.getHeaders(HeaderConstants.WARNING);
if (warningHeaders == null || warningHeaders.length == 0) return;
List<Header> newWarningHeaders = new ArrayList<Header>();
boolean modified = false;
for(Header h : warningHeaders) {
for(WarningValue wv : WarningValue.getWarningValues(h)) {
Date warnDate = wv.getWarnDate();
if (warnDate == null || warnDate.equals(responseDate)) {
newWarningHeaders.add(new BasicHeader("Warning",wv.toString()));
newWarningHeaders.add(new BasicHeader(HeaderConstants.WARNING,wv.toString()));
} else {
modified = true;
}
}
}
if (modified) {
response.removeHeaders("Warning");
response.removeHeaders(HeaderConstants.WARNING);
for(Header h : newWarningHeaders) {
response.addHeader(h);
}
@ -129,7 +134,7 @@ class ResponseProtocolCompliance {
}
private void identityIsNotUsedInContentEncoding(HttpResponse response) {
Header[] hdrs = response.getHeaders("Content-Encoding");
Header[] hdrs = response.getHeaders(HTTP.CONTENT_ENCODING);
if (hdrs == null || hdrs.length == 0) return;
List<Header> newHeaders = new ArrayList<Header>();
boolean modified = false;
@ -147,11 +152,11 @@ class ResponseProtocolCompliance {
}
String newHeaderValue = buf.toString();
if (!"".equals(newHeaderValue)) {
newHeaders.add(new BasicHeader("Content-Encoding", newHeaderValue));
newHeaders.add(new BasicHeader(HTTP.CONTENT_ENCODING, newHeaderValue));
}
}
if (!modified) return;
response.removeHeaders("Content-Encoding");
response.removeHeaders(HTTP.CONTENT_ENCODING);
for (Header h : newHeaders) {
response.addHeader(h);
}
@ -190,9 +195,9 @@ class ResponseProtocolCompliance {
}
private void ensure304DoesNotContainExtraEntityHeaders(HttpResponse response) {
String[] disallowedEntityHeaders = { "Allow", "Content-Encoding",
"Content-Language", "Content-Length", "Content-MD5",
"Content-Range", "Content-Type", "Last-Modified"
String[] disallowedEntityHeaders = { HeaderConstants.ALLOW, HTTP.CONTENT_ENCODING,
"Content-Language", HTTP.CONTENT_LEN, "Content-MD5",
"Content-Range", HTTP.CONTENT_TYPE, HeaderConstants.LAST_MODIFIED
};
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
for(String hdr : disallowedEntityHeaders) {