use of org.apache.http.HeaderIterator in project robovm by robovm.
the class ResponseProcessCookies method process.
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
// Obtain cookie store
CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
if (cookieStore == null) {
this.log.info("Cookie store not available in HTTP context");
return;
}
// Obtain actual CookieSpec instance
CookieSpec cookieSpec = (CookieSpec) context.getAttribute(ClientContext.COOKIE_SPEC);
if (cookieSpec == null) {
this.log.info("CookieSpec not available in HTTP context");
return;
}
// Obtain actual CookieOrigin instance
CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(ClientContext.COOKIE_ORIGIN);
if (cookieOrigin == null) {
this.log.info("CookieOrigin not available in HTTP context");
return;
}
HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
// see if the cookie spec supports cookie versioning.
if (cookieSpec.getVersion() > 0) {
// process set-cookie2 headers.
// Cookie2 will replace equivalent Cookie instances
it = response.headerIterator(SM.SET_COOKIE2);
processCookies(it, cookieSpec, cookieOrigin, cookieStore);
}
}
use of org.apache.http.HeaderIterator in project robolectric by robolectric.
the class TestHttpResponseTest method shouldSupportHeaderIterator.
@Test
public void shouldSupportHeaderIterator() throws Exception {
HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com"), new BasicHeader("Location", "http://zombo.com"));
HeaderIterator it = resp.headerIterator();
assertThat(it.hasNext()).isTrue();
assertThat(it.nextHeader().getValue()).isEqualTo("http://bar.com");
assertThat(it.nextHeader().getValue()).isEqualTo("http://zombo.com");
assertThat(it.hasNext()).isFalse();
}
use of org.apache.http.HeaderIterator in project platform_external_apache-http by android.
the class DefaultConnectionReuseStrategy method keepAlive.
// see interface ConnectionReuseStrategy
public boolean keepAlive(final HttpResponse response, final HttpContext context) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null.");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null.");
}
HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
if (conn != null && !conn.isOpen())
return false;
// do NOT check for stale connection, that is an expensive operation
// Check for a self-terminating entity. If the end of the entity will
// be indicated by closing the connection, there is no keep-alive.
HttpEntity entity = response.getEntity();
ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
if (entity != null) {
if (entity.getContentLength() < 0) {
if (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0)) {
// encoded, the connection cannot be reused
return false;
}
}
}
// Check for the "Connection" header. If that is absent, check for
// the "Proxy-Connection" header. The latter is an unspecified and
// broken but unfortunately common extension of HTTP.
HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
if (!hit.hasNext())
hit = response.headerIterator("Proxy-Connection");
if (hit.hasNext()) {
try {
TokenIterator ti = createTokenIterator(hit);
boolean keepalive = false;
while (ti.hasNext()) {
final String token = ti.nextToken();
if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
return false;
} else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) {
// continue the loop, there may be a "close" afterwards
keepalive = true;
}
}
if (keepalive)
return true;
// neither "close" nor "keep-alive", use default policy
} catch (ParseException px) {
// we don't have logging in HttpCore, so the exception is lost
return false;
}
}
// default since HTTP/1.1 is persistent, before it was non-persistent
return !ver.lessEquals(HttpVersion.HTTP_1_0);
}
use of org.apache.http.HeaderIterator in project platform_external_apache-http by android.
the class HttpOptions method getAllowedMethods.
public Set<String> getAllowedMethods(final HttpResponse response) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
HeaderIterator it = response.headerIterator("Allow");
Set<String> methods = new HashSet<String>();
while (it.hasNext()) {
Header header = it.nextHeader();
HeaderElement[] elements = header.getElements();
for (HeaderElement element : elements) {
methods.add(element.getName());
}
}
return methods;
}
use of org.apache.http.HeaderIterator in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesDefaultForNonPersistentConnections.
@Test
public void usesDefaultForNonPersistentConnections() throws Exception {
configuration.setKeepAlive(Duration.seconds(1));
assertThat(builder.using(configuration).createClient(apacheBuilder, connectionManager, "test")).isNotNull();
final Field field = FieldUtils.getField(httpClientBuilderClass, "keepAliveStrategy", true);
final DefaultConnectionKeepAliveStrategy strategy = (DefaultConnectionKeepAliveStrategy) field.get(apacheBuilder);
final HttpContext context = mock(HttpContext.class);
final HttpResponse response = mock(HttpResponse.class);
final HeaderIterator iterator = new BasicListHeaderIterator(ImmutableList.of(new BasicHeader(HttpHeaders.CONNECTION, "timeout=50")), HttpHeaders.CONNECTION);
when(response.headerIterator(HTTP.CONN_KEEP_ALIVE)).thenReturn(iterator);
assertThat(strategy.getKeepAliveDuration(response, context)).isEqualTo(50000);
}
Aggregations