public static TimeSource getTimeSource() { return HttpUtil.getTimeSource(); }
public static TimeSource getTimeSource() { return HttpUtil.getTimeSource(); }
public static TimeSource getTimeSource() { return HttpUtil.getTimeSource(); }
/** * Verify response time, and if response time is off from current time by more then * speficied time change response time to be current time. * The function resolve cases that remote server time is wrong, which can cause * resources to expire prematurly or served after they should be expired. * The allowd drift time is configured by responseDateDriftLimit. * @param response the response to fix * @return new response with fix date or original reesponse */ public static HttpResponse maybeFixDriftTime(HttpResponse response) { Collection<String> dates = response.getHeaders("Date"); if (!dates.isEmpty()) { Date d = DateUtil.parseRfc1123Date(dates.iterator().next()); if (d != null) { long timestamp = d.getTime(); long currentTime = HttpUtil.getTimeSource().currentTimeMillis(); if (Math.abs(currentTime - timestamp) > responseDateDriftLimit) { // Do not trust the date from response if it is too old (server time out of sync) HttpResponseBuilder builder = new HttpResponseBuilder(response); builder.setHeader("Date", DateUtil.formatRfc1123Date(currentTime)); response = builder.create(); } } } return response; } }
@Test public void testShouldRefetchForStrictNoCacheResponsePastShouldRefetch() throws Exception { assertEquals(true, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 20000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }
@Test public void testShouldRefetchForStrictNoCacheResponsePastShouldNotRefetch() throws Exception { assertEquals(false, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 5000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }
@Test public void testShouldRefetchForStrictNoCacheResponsePastShouldNotRefetch() throws Exception { assertEquals(false, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 5000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }
@Test public void testShouldRefetchForStrictNoCacheResponsePastShouldRefetch() throws Exception { assertEquals(true, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 20000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }