Search in sources :

Example 1 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HttpConnection method normalizeRequest.

protected void normalizeRequest(Request request) {
    HttpVersion version = request.getVersion();
    HttpFields headers = request.getHeaders();
    ContentProvider content = request.getContent();
    ProxyConfiguration.Proxy proxy = destination.getProxy();
    // Make sure the path is there
    String path = request.getPath();
    if (path.trim().length() == 0) {
        path = "/";
        request.path(path);
    }
    URI uri = request.getURI();
    if (proxy instanceof HttpProxy && !HttpClient.isSchemeSecure(request.getScheme()) && uri != null) {
        path = uri.toString();
        request.path(path);
    }
    // If we are HTTP 1.1, add the Host header
    if (version.getVersion() <= 11) {
        if (!headers.containsKey(HttpHeader.HOST.asString()))
            headers.put(getHttpDestination().getHostField());
    }
    // Add content headers
    if (content != null) {
        if (!headers.containsKey(HttpHeader.CONTENT_TYPE.asString())) {
            String contentType = null;
            if (content instanceof ContentProvider.Typed)
                contentType = ((ContentProvider.Typed) content).getContentType();
            if (contentType != null)
                headers.put(HttpHeader.CONTENT_TYPE, contentType);
            else
                headers.put(HttpHeader.CONTENT_TYPE, "application/octet-stream");
        }
        long contentLength = content.getLength();
        if (contentLength >= 0) {
            if (!headers.containsKey(HttpHeader.CONTENT_LENGTH.asString()))
                headers.put(HttpHeader.CONTENT_LENGTH, String.valueOf(contentLength));
        }
    }
    // Cookies
    CookieStore cookieStore = getHttpClient().getCookieStore();
    if (cookieStore != null) {
        StringBuilder cookies = null;
        if (uri != null)
            cookies = convertCookies(cookieStore.get(uri), null);
        cookies = convertCookies(request.getCookies(), cookies);
        if (cookies != null)
            request.header(HttpHeader.COOKIE.asString(), cookies.toString());
    }
    // Authentication
    applyAuthentication(request, proxy != null ? proxy.getURI() : null);
    applyAuthentication(request, uri);
}
Also used : CookieStore(java.net.CookieStore) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) HttpFields(org.eclipse.jetty.http.HttpFields) HttpVersion(org.eclipse.jetty.http.HttpVersion) URI(java.net.URI)

Example 2 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestResponseNoContent.

@Test
public void testRequestResponseNoContent() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            latch.countDown();
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        final AtomicReference<HeadersFrame> frameRef = new AtomicReference<>();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                latch.countDown();
            }

            @Override
            public void onHeaders(HeadersFrame frame) {
                frameRef.set(frame);
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        HeadersFrame response = frameRef.get();
        Assert.assertNotNull(response);
        MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
        Assert.assertEquals(200, responseMetaData.getStatus());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Socket(java.net.Socket) Test(org.junit.Test)

Example 3 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithContinuationFrames.

private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable<ByteBufferPool.Lease> frames) throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            if (priorityFrame != null) {
                PriorityFrame priority = frame.getPriority();
                Assert.assertNotNull(priority);
                Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
                Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
                Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
                Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
            }
            serverLatch.countDown();
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return null;
        }
    });
    generator = new Generator(byteBufferPool, 4096, 4);
    ByteBufferPool.Lease lease = frames.call();
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        output.flush();
        Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
        final CountDownLatch clientLatch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                if (frame.isEndStream())
                    clientLatch.countDown();
            }
        }, 4096, 8192);
        boolean closed = parseResponse(client, parser);
        Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
        Assert.assertFalse(closed);
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator)

Example 4 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithPriorityWithContinuationFrames.

@Test
public void testRequestWithPriorityWithContinuationFrames() throws Exception {
    PriorityFrame priority = new PriorityFrame(1, 13, 200, true);
    testRequestWithContinuationFrames(priority, () -> {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = newRequest("GET", new HttpFields());
        generator.control(lease, new HeadersFrame(1, metaData, priority, true));
        return lease;
    });
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Test(org.junit.Test)

Example 5 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithContinuationFramesWithEmptyLastContinuationFrame.

@Test
public void testRequestWithContinuationFramesWithEmptyLastContinuationFrame() throws Exception {
    testRequestWithContinuationFrames(null, () -> {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = newRequest("GET", new HttpFields());
        generator.control(lease, new HeadersFrame(1, metaData, null, true));
        // Take the last CONTINUATION frame and reset the flag.
        List<ByteBuffer> buffers = lease.getByteBuffers();
        ByteBuffer continuationFrameHeader = buffers.get(buffers.size() - 2);
        continuationFrameHeader.put(4, (byte) 0);
        // Add a last, empty, CONTINUATION frame.
        ByteBuffer last = ByteBuffer.wrap(new byte[] { // Length
        0, // Length
        0, // Length
        0, (byte) FrameType.CONTINUATION.getType(), (byte) Flags.END_HEADERS, // Stream ID
        0, // Stream ID
        0, // Stream ID
        0, // Stream ID
        1 });
        lease.append(last, false);
        return lease;
    });
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)185 Test (org.junit.Test)143 MetaData (org.eclipse.jetty.http.MetaData)118 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)106 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)94 Session (org.eclipse.jetty.http2.api.Session)90 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)80 FuturePromise (org.eclipse.jetty.util.FuturePromise)70 HttpServletResponse (javax.servlet.http.HttpServletResponse)62 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)48 IOException (java.io.IOException)43 ServletException (javax.servlet.ServletException)40 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32