use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class RedirectProtocolHandler method onComplete.
@Override
public void onComplete(Result result) {
Request request = result.getRequest();
Response response = result.getResponse();
if (result.isSucceeded())
redirector.redirect(request, response, null);
else
redirector.fail(request, response, result.getFailure());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpChannelOverHTTP method exchangeTerminated.
@Override
public void exchangeTerminated(HttpExchange exchange, Result result) {
super.exchangeTerminated(exchange, result);
Response response = result.getResponse();
HttpFields responseHeaders = response.getHeaders();
String closeReason = null;
if (result.isFailed())
closeReason = "failure";
else if (receiver.isShutdown())
closeReason = "server close";
else if (sender.isShutdown())
closeReason = "client close";
if (closeReason == null) {
if (response.getVersion().compareTo(HttpVersion.HTTP_1_1) < 0) {
// HTTP 1.0 must close the connection unless it has
// an explicit keep alive or it's a CONNECT method.
boolean keepAlive = responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString());
boolean connect = HttpMethod.CONNECT.is(exchange.getRequest().getMethod());
if (!keepAlive && !connect)
closeReason = "http/1.0";
} else {
// HTTP 1.1 closes only if it has an explicit close.
if (responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()))
closeReason = "http/1.1";
}
}
if (closeReason != null) {
if (LOG.isDebugEnabled())
LOG.debug("Closing, reason: {} - {}", closeReason, connection);
connection.close();
} else {
if (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)
connection.remove();
else
release();
}
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientAsyncContentTest method testSmallAsyncContent.
@Test
public void testSmallAsyncContent() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ServletOutputStream output = response.getOutputStream();
output.write(65);
output.flush();
output.write(66);
}
});
final AtomicInteger contentCount = new AtomicInteger();
final AtomicReference<Callback> callbackRef = new AtomicReference<>();
final AtomicReference<CountDownLatch> contentLatch = new AtomicReference<>(new CountDownLatch(1));
final CountDownLatch completeLatch = new CountDownLatch(1);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContentAsync(new Response.AsyncContentListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
contentCount.incrementAndGet();
callbackRef.set(callback);
contentLatch.get().countDown();
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
completeLatch.countDown();
}
});
Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
Callback callback = callbackRef.get();
// Wait a while to be sure that the parsing does not proceed.
TimeUnit.MILLISECONDS.sleep(1000);
Assert.assertEquals(1, contentCount.get());
// Succeed the content callback to proceed with parsing.
callbackRef.set(null);
contentLatch.set(new CountDownLatch(1));
callback.succeeded();
Assert.assertTrue(contentLatch.get().await(5, TimeUnit.SECONDS));
callback = callbackRef.get();
// Wait a while to be sure that the parsing does not proceed.
TimeUnit.MILLISECONDS.sleep(1000);
Assert.assertEquals(2, contentCount.get());
Assert.assertEquals(1, completeLatch.getCount());
// Succeed the content callback to proceed with parsing.
callbackRef.set(null);
contentLatch.set(new CountDownLatch(1));
callback.succeeded();
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
Assert.assertEquals(2, contentCount.get());
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientRedirectTest method testDontFollowRedirects.
@Test
public void testDontFollowRedirects() throws Exception {
start(new RedirectHandler());
Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).followRedirects(false).path("/303/localhost/done?close=true").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(303, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientRedirectTest method test_301.
@Test
public void test_301() throws Exception {
start(new RedirectHandler());
Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.HEAD).path("/301/localhost/done").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
Aggregations