org.apache.hc.core5.http.HttpRequest.getFirstHeader()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(179)

本文整理了Java中org.apache.hc.core5.http.HttpRequest.getFirstHeader()方法的一些代码示例,展示了HttpRequest.getFirstHeader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getFirstHeader()方法的具体详情如下:
包路径:org.apache.hc.core5.http.HttpRequest
类名称:HttpRequest
方法名:getFirstHeader

HttpRequest.getFirstHeader介绍

暂无

代码示例

代码示例来源:origin: apache/httpcomponents-client

private boolean hasUnsupportedConditionalHeaders(final HttpRequest request) {
  return (request.getFirstHeader(HeaderConstants.IF_RANGE) != null
      || request.getFirstHeader(HeaderConstants.IF_MATCH) != null
      || hasValidDateField(request, HeaderConstants.IF_UNMODIFIED_SINCE));
}

代码示例来源:origin: apache/httpcomponents-client

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;
}

代码示例来源:origin: apache/httpcomponents-client

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;
}

代码示例来源:origin: apache/httpcomponents-client

private void ensurePartialContentIsNotSentToAClientThatDidNotRequestIt(final HttpRequest request,
    final HttpResponse response) throws IOException {
  if (request.getFirstHeader(HeaderConstants.RANGE) != null
      || response.getCode() != HttpStatus.SC_PARTIAL_CONTENT) {
    return;
  }
  throw new ClientProtocolException(UNEXPECTED_PARTIAL_CONTENT);
}

代码示例来源:origin: apache/httpcomponents-client

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));
}

代码示例来源:origin: apache/httpcomponents-client

private void requestDidNotExpect100ContinueButResponseIsOne(
    final HttpRequest originalRequest, final HttpResponse response) throws IOException {
  if (response.getCode() != HttpStatus.SC_CONTINUE) {
    return;
  }
  final Header header = originalRequest.getFirstHeader(HttpHeaders.EXPECT);
  if (header != null && header.getValue().equalsIgnoreCase(HeaderElements.CONTINUE)) {
    return;
  }
  throw new ClientProtocolException(UNEXPECTED_100_CONTINUE);
}

代码示例来源:origin: apache/httpcomponents-client

/**
 * For 304 Not modified responses, adds a "Last-Modified" header with the
 * value of the "If-Modified-Since" header passed in the request. This
 * header is required to be able to reuse match the cache entry for
 * subsequent requests but as defined in http specifications it is not
 * included in 304 responses by backend servers. This header will not be
 * included in the resulting response.
 */
void storeRequestIfModifiedSinceFor304Response(final HttpRequest request, final HttpResponse backendResponse) {
  if (backendResponse.getCode() == HttpStatus.SC_NOT_MODIFIED) {
    final Header h = request.getFirstHeader("If-Modified-Since");
    if (h != null) {
      backendResponse.addHeader("Last-Modified", h.getValue());
    }
  }
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestDoesNotUseIfUnmodifiedSince()
  throws Exception {
  request.addHeader("If-Unmodified-Since", DateUtils.formatDate(new Date()));
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertNull(result.getFirstHeader("If-Unmodified-Since"));
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestDoesNotUseIfModifiedSince()
  throws Exception {
  request.addHeader("If-Modified-Since", DateUtils.formatDate(new Date()));
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertNull(result.getFirstHeader("If-Modified-Since"));
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestDoesNotUseIfMatch()
  throws Exception {
  request.addHeader("If-Match","\"etag\"");
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertNull(result.getFirstHeader("If-Match"));
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestDoesNotUseIfNoneMatch()
  throws Exception {
  request.addHeader("If-None-Match","\"etag\"");
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertNull(result.getFirstHeader("If-None-Match"));
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestDoesNotUseIfRange()
  throws Exception {
  request.addHeader("If-Range","\"etag\"");
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertNull(result.getFirstHeader("If-Range"));
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testConditionalRequestForEntryWithLastModifiedAndEtagIncludesBothAsValidators()
    throws Exception {
  final Date now = new Date();
  final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
  final Date twentySecondsAgo = new Date(now.getTime() - 20 * 1000L);
  final String lmDate = DateUtils.formatDate(twentySecondsAgo);
  final String etag = "\"etag\"";
  final Header[] headers = {
    new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
    new BasicHeader("Last-Modified", lmDate),
    new BasicHeader("ETag", etag)
  };
  final HttpRequest basicRequest = new BasicHttpRequest("GET", "/");
  final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);
  final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
  final HttpRequest result = impl.buildConditionalRequest(requestWrapper, cacheEntry);
  Assert.assertEquals(lmDate,
      result.getFirstHeader("If-Modified-Since").getValue());
  Assert.assertEquals(etag,
      result.getFirstHeader("If-None-Match").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestCarriesOtherRequestHeaders()
  throws Exception {
  request.addHeader("User-Agent","MyBrowser/1.0");
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertEquals("MyBrowser/1.0",
      result.getFirstHeader("User-Agent").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void doesNotStripMinFreshFromRequestWithoutNoCache()
  throws Exception {
  final HttpRequest req = new BasicHttpRequest("GET", "/");
  req.setHeader("Cache-Control", "min-fresh=10");
  final HttpRequest wrapper = RequestCopier.INSTANCE.copy(req);
  impl.makeRequestCompliant(wrapper);
  assertEquals("min-fresh=10",
      wrapper.getFirstHeader("Cache-Control").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void correctlyStripsMinFreshFromMiddleIfNoCache()
  throws Exception {
  final HttpRequest req = new BasicHttpRequest("GET", "/");
  req.setHeader("Cache-Control", "no-cache,min-fresh=10,no-store");
  final HttpRequest wrapper = RequestCopier.INSTANCE.copy(req);
  impl.makeRequestCompliant(wrapper);
  assertEquals("no-cache,no-store",
      wrapper.getFirstHeader("Cache-Control").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void stripsMaxAgeFromRequestIfNoCachePresent()
  throws Exception {
  final HttpRequest req = new BasicHttpRequest("GET", "/");
  req.setHeader("Cache-Control", "no-cache, max-age=10");
  final HttpRequest wrapper = RequestCopier.INSTANCE.copy(req);
  impl.makeRequestCompliant(wrapper);
  assertEquals("no-cache",
      wrapper.getFirstHeader("Cache-Control").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void stripsMinFreshFromRequestIfNoCachePresent()
  throws Exception {
  final HttpRequest req = new BasicHttpRequest("GET", "/");
  req.setHeader("Cache-Control", "no-cache, min-fresh=10");
  final HttpRequest wrapper = RequestCopier.INSTANCE.copy(req);
  impl.makeRequestCompliant(wrapper);
  assertEquals("no-cache",
      wrapper.getFirstHeader("Cache-Control").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void stripsMaxFreshFromRequestIfNoCachePresent()
  throws Exception {
  final HttpRequest req = new BasicHttpRequest("GET", "/");
  req.setHeader("Cache-Control", "no-cache, max-stale=10");
  final HttpRequest wrapper = RequestCopier.INSTANCE.copy(req);
  impl.makeRequestCompliant(wrapper);
  assertEquals("no-cache",
      wrapper.getFirstHeader("Cache-Control").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testRecordsClientProtocolInViaHeaderIfRequestNotServableFromCache() throws Exception {
  impl = createCachingExecChain(new BasicHttpCache(), CacheConfig.DEFAULT);
  final ClassicHttpRequest originalRequest = new BasicClassicHttpRequest("GET", "/");
  originalRequest.setVersion(HttpVersion.HTTP_1_0);
  final ClassicHttpRequest req = originalRequest;
  req.setHeader("Cache-Control", "no-cache");
  final ClassicHttpResponse resp = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT, "No Content");
  final Capture<ClassicHttpRequest> cap = EasyMock.newCapture();
  backendCaptureRequestAndReturn(cap, resp);
  replayMocks();
  execute(req);
  verifyMocks();
  final HttpRequest captured = cap.getValue();
  final String via = captured.getFirstHeader("Via").getValue();
  final String proto = via.split("\\s+")[0];
  Assert.assertTrue("http/1.0".equalsIgnoreCase(proto) || "1.0".equalsIgnoreCase(proto));
}

相关文章