Search in sources :

Example 71 with HttpFields

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

the class PatternRuleTest method assertMatch.

private void assertMatch(boolean flag, String[] matchCase) throws IOException {
    _rule.setPattern(matchCase[0]);
    final String uri = matchCase[1];
    String result = _rule.matchAndApply(uri, new Request(null, null) {

        {
            setMetaData(new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_1_0, new HttpFields()));
        }
    }, null);
    assertEquals("pattern: " + matchCase[0] + " uri: " + matchCase[1], flag, result != null);
}
Also used : HttpFields(org.eclipse.jetty.http.HttpFields) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpURI(org.eclipse.jetty.http.HttpURI)

Example 72 with HttpFields

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

the class JDK9HTTP2Client method main.

public static void main(String[] args) throws Exception {
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    client.addBean(sslContextFactory);
    client.start();
    String host = "localhost";
    int port = 8443;
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new Session.Listener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(555, TimeUnit.SECONDS);
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            System.err.println(frame);
            if (frame.isEndStream())
                latch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            System.err.println(frame);
            callback.succeeded();
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    client.stop();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session)

Example 73 with HttpFields

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

the class HttpSenderOverFCGI method sendHeaders.

@Override
protected void sendHeaders(HttpExchange exchange, HttpContent content, Callback callback) {
    Request request = exchange.getRequest();
    // Copy the request headers to be able to convert them properly
    HttpFields headers = new HttpFields();
    for (HttpField field : request.getHeaders()) headers.put(field);
    HttpFields fcgiHeaders = new HttpFields();
    // FastCGI headers based on the URI
    URI uri = request.getURI();
    String path = uri == null ? request.getPath() : uri.getRawPath();
    fcgiHeaders.put(FCGI.Headers.DOCUMENT_URI, path);
    String query = uri == null ? null : uri.getRawQuery();
    fcgiHeaders.put(FCGI.Headers.QUERY_STRING, query == null ? "" : query);
    // FastCGI headers based on HTTP headers
    HttpField httpField = headers.remove(HttpHeader.AUTHORIZATION);
    if (httpField != null)
        fcgiHeaders.put(FCGI.Headers.AUTH_TYPE, httpField.getValue());
    httpField = headers.remove(HttpHeader.CONTENT_LENGTH);
    fcgiHeaders.put(FCGI.Headers.CONTENT_LENGTH, httpField == null ? "" : httpField.getValue());
    httpField = headers.remove(HttpHeader.CONTENT_TYPE);
    fcgiHeaders.put(FCGI.Headers.CONTENT_TYPE, httpField == null ? "" : httpField.getValue());
    // FastCGI headers that are not based on HTTP headers nor URI
    fcgiHeaders.put(FCGI.Headers.REQUEST_METHOD, request.getMethod());
    fcgiHeaders.put(FCGI.Headers.SERVER_PROTOCOL, request.getVersion().asString());
    fcgiHeaders.put(FCGI.Headers.GATEWAY_INTERFACE, "CGI/1.1");
    fcgiHeaders.put(FCGI.Headers.SERVER_SOFTWARE, "Jetty/" + Jetty.VERSION);
    // Translate remaining HTTP header into the HTTP_* format
    for (HttpField field : headers) {
        String name = field.getName();
        String fcgiName = "HTTP_" + name.replaceAll("-", "_").toUpperCase(Locale.ENGLISH);
        fcgiHeaders.add(fcgiName, field.getValue());
    }
    // Give a chance to the transport implementation to customize the FastCGI headers
    HttpClientTransportOverFCGI transport = (HttpClientTransportOverFCGI) getHttpChannel().getHttpDestination().getHttpClient().getTransport();
    transport.customize(request, fcgiHeaders);
    int id = getHttpChannel().getRequest();
    boolean hasContent = content.hasContent();
    Generator.Result headersResult = generator.generateRequestHeaders(id, fcgiHeaders, hasContent ? callback : Callback.NOOP);
    if (hasContent) {
        getHttpChannel().flush(headersResult);
    } else {
        Generator.Result noContentResult = generator.generateRequestContent(id, BufferUtil.EMPTY_BUFFER, true, callback);
        getHttpChannel().flush(headersResult, noContentResult);
    }
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) Request(org.eclipse.jetty.client.api.Request) URI(java.net.URI) ClientGenerator(org.eclipse.jetty.fcgi.generator.ClientGenerator) Generator(org.eclipse.jetty.fcgi.generator.Generator)

Example 74 with HttpFields

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

the class ClientParserTest method testParseSmallResponseContent.

@Test
public void testParseSmallResponseContent() throws Exception {
    final int id = 13;
    HttpFields fields = new HttpFields();
    ByteBuffer content = ByteBuffer.wrap(new byte[1024]);
    final int contentLength = content.remaining();
    final int code = 200;
    final String contentTypeName = "Content-Length";
    final String contentTypeValue = String.valueOf(contentLength);
    fields.put(contentTypeName, contentTypeValue);
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ServerGenerator generator = new ServerGenerator(byteBufferPool);
    Generator.Result result1 = generator.generateResponseHeaders(id, code, "OK", fields, null);
    Generator.Result result2 = generator.generateResponseContent(id, content, true, false, null);
    final AtomicInteger verifier = new AtomicInteger();
    ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() {

        @Override
        public boolean onContent(int request, FCGI.StreamType stream, ByteBuffer buffer) {
            Assert.assertEquals(id, request);
            Assert.assertEquals(contentLength, buffer.remaining());
            verifier.addAndGet(2);
            return false;
        }

        @Override
        public void onEnd(int request) {
            Assert.assertEquals(id, request);
            verifier.addAndGet(3);
        }
    });
    for (ByteBuffer buffer : result1.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    for (ByteBuffer buffer : result2.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(5, verifier.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpFields(org.eclipse.jetty.http.HttpFields) Generator(org.eclipse.jetty.fcgi.generator.Generator) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) FCGI(org.eclipse.jetty.fcgi.FCGI) Test(org.junit.Test)

Example 75 with HttpFields

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

the class ClientParserTest method testParseResponseHeaders.

@Test
public void testParseResponseHeaders() throws Exception {
    final int id = 13;
    HttpFields fields = new HttpFields();
    final int statusCode = 200;
    final String statusMessage = "OK";
    final String contentTypeName = "Content-Type";
    final String contentTypeValue = "text/html;charset=utf-8";
    fields.put(contentTypeName, contentTypeValue);
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ServerGenerator generator = new ServerGenerator(byteBufferPool);
    Generator.Result result = generator.generateResponseHeaders(id, statusCode, statusMessage, fields, null);
    // Use the fundamental theorem of arithmetic to test the results.
    // This way we know onHeader() has been called the right number of
    // times with the right arguments, and so onHeaders().
    final int[] primes = new int[] { 2, 3, 5 };
    int value = 1;
    for (int prime : primes) value *= prime;
    final AtomicInteger params = new AtomicInteger(1);
    ClientParser parser = new ClientParser(new ClientParser.Listener.Adapter() {

        @Override
        public void onBegin(int request, int code, String reason) {
            Assert.assertEquals(statusCode, code);
            Assert.assertEquals(statusMessage, reason);
            params.set(params.get() * primes[0]);
        }

        @Override
        public void onHeader(int request, HttpField field) {
            Assert.assertEquals(id, request);
            switch(field.getName()) {
                case contentTypeName:
                    Assert.assertEquals(contentTypeValue, field.getValue());
                    params.set(params.get() * primes[1]);
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onHeaders(int request) {
            Assert.assertEquals(id, request);
            params.set(params.get() * primes[2]);
        }
    });
    for (ByteBuffer buffer : result.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(value, params.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) Generator(org.eclipse.jetty.fcgi.generator.Generator) ServerGenerator(org.eclipse.jetty.fcgi.generator.ServerGenerator) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)172 Test (org.junit.Test)142 MetaData (org.eclipse.jetty.http.MetaData)117 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)105 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)93 Session (org.eclipse.jetty.http2.api.Session)89 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)79 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)59 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)46 IOException (java.io.IOException)42 ServletException (javax.servlet.ServletException)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32