Search in sources :

Example 36 with HttpField

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

the class ClientGeneratorTest method testGenerateRequestHeaders.

@Test
public void testGenerateRequestHeaders() throws Exception {
    HttpFields fields = new HttpFields();
    // Short name, short value
    final String shortShortName = "REQUEST_METHOD";
    final String shortShortValue = "GET";
    fields.put(new HttpField(shortShortName, shortShortValue));
    // Short name, long value
    final String shortLongName = "REQUEST_URI";
    // Be sure it's longer than 127 chars to test the large value
    final String shortLongValue = "/api/0.6/map?bbox=-64.217736,-31.456810,-64.187736,-31.432322,filler=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    fields.put(new HttpField(shortLongName, shortLongValue));
    // Long name, short value
    // Be sure it's longer than 127 chars to test the large name
    final String longShortName = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
    final String longShortValue = "api.openstreetmap.org";
    fields.put(new HttpField(longShortName, longShortValue));
    // Long name, long value
    char[] chars = new char[ClientGenerator.MAX_PARAM_LENGTH];
    Arrays.fill(chars, 'z');
    final String longLongName = new String(chars);
    final String longLongValue = new String(chars);
    fields.put(new HttpField(longLongName, longLongValue));
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    ClientGenerator generator = new ClientGenerator(byteBufferPool);
    final int id = 13;
    Generator.Result result = generator.generateRequestHeaders(id, 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, 7, 11 };
    int value = 1;
    for (int prime : primes) value *= prime;
    final AtomicInteger params = new AtomicInteger(1);
    ServerParser parser = new ServerParser(new ServerParser.Listener.Adapter() {

        @Override
        public void onHeader(int request, HttpField field) {
            Assert.assertEquals(id, request);
            switch(field.getName()) {
                case shortShortName:
                    Assert.assertEquals(shortShortValue, field.getValue());
                    params.set(params.get() * primes[0]);
                    break;
                case shortLongName:
                    Assert.assertEquals(shortLongValue, field.getValue());
                    params.set(params.get() * primes[1]);
                    break;
                case longShortName:
                    Assert.assertEquals(longShortValue, field.getValue());
                    params.set(params.get() * primes[2]);
                    break;
                default:
                    Assert.assertEquals(longLongName, field.getName());
                    Assert.assertEquals(longLongValue, field.getValue());
                    params.set(params.get() * primes[3]);
                    break;
            }
        }

        @Override
        public void onHeaders(int request) {
            Assert.assertEquals(id, request);
            params.set(params.get() * primes[4]);
        }
    });
    for (ByteBuffer buffer : result.getByteBuffers()) {
        parser.parse(buffer);
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(value, params.get());
    // Parse again byte by byte
    params.set(1);
    for (ByteBuffer buffer : result.getByteBuffers()) {
        buffer.flip();
        while (buffer.hasRemaining()) parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
        Assert.assertFalse(buffer.hasRemaining());
    }
    Assert.assertEquals(value, params.get());
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) 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) ServerParser(org.eclipse.jetty.fcgi.parser.ServerParser) Test(org.junit.Test)

Example 37 with HttpField

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

the class Request method findServerPort.

/* ------------------------------------------------------------ */
private int findServerPort() {
    MetaData.Request metadata = _metaData;
    // Return host from header field
    HttpField host = metadata == null ? null : metadata.getFields().getField(HttpHeader.HOST);
    if (host != null) {
        // TODO is this needed now?
        HostPortHttpField authority = (host instanceof HostPortHttpField) ? ((HostPortHttpField) host) : new HostPortHttpField(host.getValue());
        metadata.getURI().setAuthority(authority.getHost(), authority.getPort());
        return authority.getPort();
    }
    // Return host from connection
    if (_channel != null)
        return getLocalPort();
    return -1;
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField)

Example 38 with HttpField

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

the class Response method reset.

public void reset(boolean preserveCookies) {
    resetForForward();
    _status = 200;
    _reason = null;
    _contentLength = -1;
    List<HttpField> cookies = preserveCookies ? _fields.stream().filter(f -> f.getHeader() == HttpHeader.SET_COOKIE).collect(Collectors.toList()) : null;
    _fields.clear();
    String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
    if (connection != null) {
        for (String value : StringUtil.csvSplit(null, connection, 0, connection.length())) {
            HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
            if (cb != null) {
                switch(cb) {
                    case CLOSE:
                        _fields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.toString());
                        break;
                    case KEEP_ALIVE:
                        if (HttpVersion.HTTP_1_0.is(_channel.getRequest().getProtocol()))
                            _fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.toString());
                        break;
                    case TE:
                        _fields.put(HttpHeader.CONNECTION, HttpHeaderValue.TE.toString());
                        break;
                    default:
                }
            }
        }
    }
    if (preserveCookies)
        cookies.forEach(f -> _fields.add(f));
    else {
        Request request = getHttpChannel().getRequest();
        HttpSession session = request.getSession(false);
        if (session != null && session.isNew()) {
            SessionHandler sh = request.getSessionHandler();
            if (sh != null) {
                HttpCookie c = sh.getSessionCookie(session, request.getContextPath(), request.isSecure());
                if (c != null)
                    addCookie(c);
            }
        }
    }
}
Also used : HttpScheme(org.eclipse.jetty.http.HttpScheme) HttpVersion(org.eclipse.jetty.http.HttpVersion) CookieCompliance(org.eclipse.jetty.http.CookieCompliance) StringUtil(org.eclipse.jetty.util.StringUtil) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpURI(org.eclipse.jetty.http.HttpURI) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) Locale(java.util.Locale) MetaData(org.eclipse.jetty.http.MetaData) IllegalSelectorException(java.nio.channels.IllegalSelectorException) HttpStatus(org.eclipse.jetty.http.HttpStatus) Cookie(javax.servlet.http.Cookie) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) HttpFields(org.eclipse.jetty.http.HttpFields) EnumSet(java.util.EnumSet) PrintWriter(java.io.PrintWriter) HttpSession(javax.servlet.http.HttpSession) HttpContent(org.eclipse.jetty.http.HttpContent) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) QuotedStringTokenizer(org.eclipse.jetty.util.QuotedStringTokenizer) Collection(java.util.Collection) RequestDispatcher(javax.servlet.RequestDispatcher) HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Collectors(java.util.stream.Collectors) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) List(java.util.List) Syntax(org.eclipse.jetty.http.Syntax) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) Log(org.eclipse.jetty.util.log.Log) HttpGenerator(org.eclipse.jetty.http.HttpGenerator) URIUtil(org.eclipse.jetty.util.URIUtil) Logger(org.eclipse.jetty.util.log.Logger) DateGenerator(org.eclipse.jetty.http.DateGenerator) Collections(java.util.Collections) MimeTypes(org.eclipse.jetty.http.MimeTypes) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpSession(javax.servlet.http.HttpSession) HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue)

Example 39 with HttpField

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

the class HttpClient method copyRequest.

protected Request copyRequest(HttpRequest oldRequest, URI newURI) {
    Request newRequest = newHttpRequest(oldRequest.getConversation(), newURI);
    newRequest.method(oldRequest.getMethod()).version(oldRequest.getVersion()).content(oldRequest.getContent()).idleTimeout(oldRequest.getIdleTimeout(), TimeUnit.MILLISECONDS).timeout(oldRequest.getTimeout(), TimeUnit.MILLISECONDS).followRedirects(oldRequest.isFollowRedirects());
    for (HttpField field : oldRequest.getHeaders()) {
        HttpHeader header = field.getHeader();
        // We have a new URI, so skip the host header if present.
        if (HttpHeader.HOST == header)
            continue;
        // Remove expectation headers.
        if (HttpHeader.EXPECT == header)
            continue;
        // Remove cookies.
        if (HttpHeader.COOKIE == header)
            continue;
        // Remove authorization headers.
        if (HttpHeader.AUTHORIZATION == header || HttpHeader.PROXY_AUTHORIZATION == header)
            continue;
        String name = field.getName();
        String value = field.getValue();
        if (!newRequest.getHeaders().contains(name, value))
            newRequest.header(name, value);
    }
    return newRequest;
}
Also used : HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpField(org.eclipse.jetty.http.HttpField) Request(org.eclipse.jetty.client.api.Request)

Example 40 with HttpField

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

the class ResponseNotifier method forwardFailure.

public void forwardFailure(List<Response.ResponseListener> listeners, Response response, Throwable failure) {
    notifyBegin(listeners, response);
    for (Iterator<HttpField> iterator = response.getHeaders().iterator(); iterator.hasNext(); ) {
        HttpField field = iterator.next();
        if (!notifyHeader(listeners, response, field))
            iterator.remove();
    }
    notifyHeaders(listeners, response);
    if (response instanceof ContentResponse)
        notifyContent(listeners, response, ByteBuffer.wrap(((ContentResponse) response).getContent()), Callback.NOOP);
    notifyFailure(listeners, response, failure);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpField(org.eclipse.jetty.http.HttpField)

Aggregations

HttpField (org.eclipse.jetty.http.HttpField)57 Test (org.junit.Test)29 HttpFields (org.eclipse.jetty.http.HttpFields)19 ByteBuffer (java.nio.ByteBuffer)17 MetaData (org.eclipse.jetty.http.MetaData)16 HostPortHttpField (org.eclipse.jetty.http.HostPortHttpField)12 ArrayList (java.util.ArrayList)8 PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)7 Entry (org.eclipse.jetty.http2.hpack.HpackContext.Entry)7 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)7 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)7 HttpHeader (org.eclipse.jetty.http.HttpHeader)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HpackEncoder (org.eclipse.jetty.http2.hpack.HpackEncoder)4 Parser (org.eclipse.jetty.http2.parser.Parser)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3