private boolean isGet(final HttpRequest request) { return request.getMethod().equals(HeaderConstants.GET_METHOD); }
static boolean requestIsGet(final HttpRequest req) { return req.getMethod().equals((HeaderConstants.GET_METHOD)); }
static boolean requestShouldNotBeCached(final HttpRequest req) { final String method = req.getMethod(); return notGetOrHeadRequest(method); }
private boolean responseShouldContainEntity(final HttpRequest request, final HttpCacheEntry cacheEntry) { return request.getMethod().equals(HeaderConstants.GET_METHOD) && cacheEntry.getResource() != null; }
boolean clientRequestsOurOptions(final HttpRequest request) { if (!HeaderConstants.OPTIONS_METHOD.equals(request.getMethod())) { return false; } if (!"*".equals(request.getRequestUri())) { return false; } final Header h = request.getFirstHeader(HeaderConstants.MAX_FORWARDS); if (!"0".equals(h != null ? h.getValue() : null)) { return false; } return true; }
private void decrementOPTIONSMaxForwardsIfGreaterThen0(final HttpRequest request) { if (!HeaderConstants.OPTIONS_METHOD.equals(request.getMethod())) { return; } final Header maxForwards = request.getFirstHeader(HeaderConstants.MAX_FORWARDS); if (maxForwards == null) { return; } request.removeHeaders(HeaderConstants.MAX_FORWARDS); final int currentMaxForwards = Integer.parseInt(maxForwards.getValue()); request.setHeader(HeaderConstants.MAX_FORWARDS, Integer.toString(currentMaxForwards - 1)); }
private void ensure200ForOPTIONSRequestWithNoBodyHasContentLengthZero(final HttpRequest request, final HttpResponse response) { if (!request.getMethod().equalsIgnoreCase(HeaderConstants.OPTIONS_METHOD)) { return; } if (response.getCode() != HttpStatus.SC_OK) { return; } if (response.getFirstHeader(HttpHeaders.CONTENT_LENGTH) == null) { response.addHeader(HttpHeaders.CONTENT_LENGTH, "0"); } }
@Override public void flushCacheEntriesFor(final HttpHost host, final HttpRequest request) { if (log.isDebugEnabled()) { log.debug("Flush cache entries: " + host + "; " + new RequestLine(request)); } if (!StandardMethods.isSafe(request.getMethod())) { final String cacheKey = cacheKeyGenerator.generateKey(host, request); try { storage.removeEntry(cacheKey); } catch (final ResourceIOException ex) { if (log.isWarnEnabled()) { log.warn("I/O error removing cache entry with key " + cacheKey); } } } }
@Override public void flushCacheEntriesInvalidatedByExchange(final HttpHost host, final HttpRequest request, final HttpResponse response) { if (log.isDebugEnabled()) { log.debug("Flush cache entries invalidated by exchange: " + host + "; " + new RequestLine(request) + " -> " + new StatusLine(response)); } if (!StandardMethods.isSafe(request.getMethod())) { cacheInvalidator.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyGenerator, storage); } }
private RequestProtocolError requestHasWeakETagAndRange(final HttpRequest request) { // TODO: Should these be looking at all the headers marked as Range? final String method = request.getMethod(); if (!(HeaderConstants.GET_METHOD.equals(method))) { return null; } final Header range = request.getFirstHeader(HeaderConstants.RANGE); if (range == null) { return null; } final Header ifRange = request.getFirstHeader(HeaderConstants.IF_RANGE); if (ifRange == null) { return null; } final String val = ifRange.getValue(); if (val.startsWith("W/")) { return RequestProtocolError.WEAK_ETAG_AND_RANGE_ERROR; } return null; }
private RequestProtocolError requestHasWeekETagForPUTOrDELETEIfMatch(final HttpRequest request) { // TODO: Should these be looking at all the headers marked as If-Match/If-None-Match? final String method = request.getMethod(); if (!(HeaderConstants.PUT_METHOD.equals(method) || HeaderConstants.DELETE_METHOD.equals(method))) { return null; } final Header ifMatch = request.getFirstHeader(HeaderConstants.IF_MATCH); if (ifMatch != null) { final String val = ifMatch.getValue(); if (val.startsWith("W/")) { return RequestProtocolError.WEAK_ETAG_ON_PUTDELETE_METHOD_ERROR; } } else { final Header ifNoneMatch = request.getFirstHeader(HeaderConstants.IF_NONE_MATCH); if (ifNoneMatch == null) { return null; } final String val2 = ifNoneMatch.getValue(); if (val2.startsWith("W/")) { return RequestProtocolError.WEAK_ETAG_ON_PUTDELETE_METHOD_ERROR; } } return null; }
final String method = request.getMethod();
final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException { final String method = request.getMethod(); if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method) &&
@Override public Cancellable flushCacheEntriesInvalidatedByExchange( final HttpHost host, final HttpRequest request, final HttpResponse response, final FutureCallback<Boolean> callback) { if (log.isDebugEnabled()) { log.debug("Flush cache entries invalidated by exchange: " + host + "; " + new RequestLine(request) + " -> " + new StatusLine(response)); } if (!StandardMethods.isSafe(request.getMethod())) { return cacheInvalidator.flushCacheEntriesInvalidatedByExchange(host, request, response, cacheKeyGenerator, storage, callback); } callback.completed(Boolean.TRUE); return Operations.nonCancellable(); }
final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException { final String method = request.getMethod(); if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method) &&
log.debug("Flush cache entries: " + host + "; " + new RequestLine(request)); if (!StandardMethods.isSafe(request.getMethod())) { final String cacheKey = cacheKeyGenerator.generateKey(host, request); return storage.removeEntry(cacheKey, new FutureCallback<Boolean>() {
@Test public void testBuildUnconditionalRequestUsesGETMethod() throws Exception { final HttpRequest result = impl.buildUnconditionalRequest(request); Assert.assertEquals("GET", result.getMethod()); }
@Test public void testBuildConditionalRequestWithETag() { final String theMethod = "GET"; final String theUri = "/theuri"; final String theETag = "this is my eTag"; final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri); basicRequest.addHeader("Accept-Encoding", "gzip"); final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest); final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(new Date())), new BasicHeader("Last-Modified", DateUtils.formatDate(new Date())), new BasicHeader("ETag", theETag) }; final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers); final HttpRequest newRequest = impl.buildConditionalRequest(requestWrapper, cacheEntry); Assert.assertNotSame(basicRequest, newRequest); Assert.assertEquals(theMethod, newRequest.getMethod()); Assert.assertEquals(theUri, newRequest.getRequestUri()); Assert.assertEquals(3, newRequest.getHeaders().length); Assert.assertEquals("Accept-Encoding", newRequest.getHeaders()[0].getName()); Assert.assertEquals("gzip", newRequest.getHeaders()[0].getValue()); Assert.assertEquals("If-None-Match", newRequest.getHeaders()[1].getName()); Assert.assertEquals(theETag, newRequest.getHeaders()[1].getValue()); }
@Test public void testBuildConditionalRequestWithLastModified() { final String theMethod = "GET"; final String theUri = "/theuri"; final String lastModified = "this is my last modified date"; final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri); basicRequest.addHeader("Accept-Encoding", "gzip"); final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest); final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(new Date())), new BasicHeader("Last-Modified", lastModified) }; final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers); final HttpRequest newRequest = impl.buildConditionalRequest(requestWrapper, cacheEntry); Assert.assertNotSame(basicRequest, newRequest); Assert.assertEquals(theMethod, newRequest.getMethod()); Assert.assertEquals(theUri, newRequest.getRequestUri()); Assert.assertEquals(2, newRequest.getHeaders().length); Assert.assertEquals("Accept-Encoding", newRequest.getHeaders()[0].getName()); Assert.assertEquals("gzip", newRequest.getHeaders()[0].getValue()); Assert.assertEquals("If-Modified-Since", newRequest.getHeaders()[1].getName()); Assert.assertEquals(lastModified, newRequest.getHeaders()[1].getValue()); }