Search in sources :

Example 1 with HttpRequest

use of org.eclipse.jetty.client.HttpRequest in project jetty.project by eclipse.

the class PushedResourcesTest method testPushedResourceCancelled.

@Test
public void testPushedResourceCancelled() throws Exception {
    String pushPath = "/secondary";
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpURI pushURI = new HttpURI("http://localhost:" + connector.getLocalPort() + pushPath);
            MetaData.Request pushRequest = new MetaData.Request(HttpMethod.GET.asString(), pushURI, HttpVersion.HTTP_2, new HttpFields());
            stream.push(new PushPromiseFrame(stream.getId(), 0, pushRequest), new Promise.Adapter<Stream>() {

                @Override
                public void succeeded(Stream pushStream) {
                    // Just send the normal response and wait for the reset.
                    MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                }
            }, new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    latch.countDown();
                }
            });
            return null;
        }
    });
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpURI(org.eclipse.jetty.http.HttpURI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 2 with HttpRequest

use of org.eclipse.jetty.client.HttpRequest in project jetty.project by eclipse.

the class HttpChannelOverHTTP method exchangeTerminating.

@Override
public Result exchangeTerminating(HttpExchange exchange, Result result) {
    if (result.isFailed())
        return result;
    HttpResponse response = exchange.getResponse();
    if ((response.getVersion() == HttpVersion.HTTP_1_1) && (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)) {
        String connection = response.getHeaders().get(HttpHeader.CONNECTION);
        if ((connection == null) || !connection.toLowerCase(Locale.US).contains("upgrade")) {
            return new Result(result, new HttpResponseException("101 Switching Protocols without Connection: Upgrade not supported", response));
        }
        // Upgrade Response
        HttpRequest request = exchange.getRequest();
        if (request instanceof HttpConnectionUpgrader) {
            HttpConnectionUpgrader listener = (HttpConnectionUpgrader) request;
            try {
                listener.upgrade(response, getHttpConnection());
            } catch (Throwable x) {
                return new Result(result, x);
            }
        }
    }
    return result;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpResponse(org.eclipse.jetty.client.HttpResponse) HttpResponseException(org.eclipse.jetty.client.HttpResponseException) Result(org.eclipse.jetty.client.api.Result)

Example 3 with HttpRequest

use of org.eclipse.jetty.client.HttpRequest in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method newExchange.

protected HttpExchange newExchange() {
    HttpRequest request = (HttpRequest) client.newRequest("http://localhost");
    FutureResponseListener listener = new FutureResponseListener(request);
    HttpExchange exchange = new HttpExchange(destination, request, Collections.<Response.ResponseListener>singletonList(listener));
    boolean associated = connection.getHttpChannel().associate(exchange);
    Assert.assertTrue(associated);
    exchange.requestComplete(null);
    exchange.terminateRequest();
    return exchange;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 4 with HttpRequest

use of org.eclipse.jetty.client.HttpRequest in project jetty.project by eclipse.

the class HttpReceiverOverHTTP2 method onPush.

@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return null;
    HttpRequest request = exchange.getRequest();
    MetaData.Request metaData = (MetaData.Request) frame.getMetaData();
    HttpRequest pushRequest = (HttpRequest) getHttpDestination().getHttpClient().newRequest(metaData.getURIString());
    BiFunction<Request, Request, Response.CompleteListener> pushListener = request.getPushListener();
    if (pushListener != null) {
        Response.CompleteListener listener = pushListener.apply(request, pushRequest);
        if (listener != null) {
            HttpChannelOverHTTP2 pushChannel = getHttpChannel().getHttpConnection().newHttpChannel(true);
            List<Response.ResponseListener> listeners = Collections.singletonList(listener);
            HttpExchange pushExchange = new HttpExchange(getHttpDestination(), pushRequest, listeners);
            pushChannel.associate(pushExchange);
            pushChannel.setStream(stream);
            // TODO: idle timeout ?
            pushExchange.requestComplete(null);
            pushExchange.terminateRequest();
            return pushChannel.getStreamListener();
        }
    }
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
    return null;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpExchange(org.eclipse.jetty.client.HttpExchange) Request(org.eclipse.jetty.client.api.Request) HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame)

Example 5 with HttpRequest

use of org.eclipse.jetty.client.HttpRequest in project incubator-skywalking by apache.

the class SyncHttpRequestSendInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    HttpRequest request = (HttpRequest) objInst;
    ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan span = ContextManager.createExitSpan(request.getURI().getPath(), contextCarrier, request.getHost() + ":" + request.getPort());
    span.setComponent(ComponentsDefine.JETTY_CLIENT);
    Tags.HTTP.METHOD.set(span, getHttpMethod(request));
    Tags.URL.set(span, request.getURI().toString());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    HttpFields field = request.getHeaders();
    while (next.hasNext()) {
        next = next.next();
        field.add(next.getHeadKey(), next.getHeadValue());
    }
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) HttpFields(org.eclipse.jetty.http.HttpFields) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

HttpRequest (org.eclipse.jetty.client.HttpRequest)13 IOException (java.io.IOException)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 Response (org.eclipse.jetty.client.api.Response)3 Result (org.eclipse.jetty.client.api.Result)3 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)3 HttpFields (org.eclipse.jetty.http.HttpFields)3 Request (org.eclipse.jetty.server.Request)3 Test (org.junit.Test)3 BodyGenerator (io.airlift.http.client.BodyGenerator)2 FileBodyGenerator (io.airlift.http.client.FileBodyGenerator)2 StaticBodyGenerator (io.airlift.http.client.StaticBodyGenerator)2 Path (java.nio.file.Path)2 Random (java.util.Random)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 ServletException (javax.servlet.ServletException)2