Search in sources :

Example 11 with BadMessageException

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

the class HttpChannelOverHTTP2 method onRequest.

public Runnable onRequest(HeadersFrame frame) {
    try {
        MetaData.Request request = (MetaData.Request) frame.getMetaData();
        HttpFields fields = request.getFields();
        // pseudo-header, so we need to synthesize a Host header.
        if (!fields.contains(HttpHeader.HOST)) {
            String authority = request.getURI().getAuthority();
            if (authority != null) {
                // Lower-case to be consistent with other HTTP/2 headers.
                fields.put("host", authority);
            }
        }
        _expect100Continue = fields.contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString());
        HttpFields response = getResponse().getHttpFields();
        if (getHttpConfiguration().getSendServerVersion())
            response.add(SERVER_VERSION);
        if (getHttpConfiguration().getSendXPoweredBy())
            response.add(POWERED_BY);
        onRequest(request);
        boolean endStream = frame.isEndStream();
        if (endStream) {
            onContentComplete();
            onRequestComplete();
        }
        _delayedUntilContent = getHttpConfiguration().isDelayDispatchUntilContent() && !endStream && !_expect100Continue;
        _handled = !_delayedUntilContent;
        if (LOG.isDebugEnabled()) {
            Stream stream = getStream();
            LOG.debug("HTTP2 Request #{}/{}, delayed={}:{}{} {} {}{}{}", stream.getId(), Integer.toHexString(stream.getSession().hashCode()), _delayedUntilContent, System.lineSeparator(), request.getMethod(), request.getURI(), request.getHttpVersion(), System.lineSeparator(), fields);
        }
        return _delayedUntilContent ? null : this;
    } catch (BadMessageException x) {
        onBadMessage(x.getCode(), x.getReason());
        return null;
    } catch (Throwable x) {
        onBadMessage(HttpStatus.INTERNAL_SERVER_ERROR_500, null);
        return null;
    }
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) BadMessageException(org.eclipse.jetty.http.BadMessageException) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream)

Example 12 with BadMessageException

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

the class HpackDecoderTest method testTooBigToIndex.

@Test
public void testTooBigToIndex() {
    String encoded = "44FfEc02Df3990A190A0D4Ee5b3d2940Ec98Aa4a62D127D29e273a0aA20dEcAa190a503b262d8a2671D4A2672a927aA874988a2471D05510750c951139EdA2452a3a548cAa1aA90bE4B228342864A9E0D450A5474a92992a1aA513395448E3A0Aa17B96cFe3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f14E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F3E7Cf9f3e7cF9F353F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F54f";
    ByteBuffer buffer = ByteBuffer.wrap(TypeUtil.fromHexString(encoded));
    HpackDecoder decoder = new HpackDecoder(128, 8192);
    try {
        decoder.decode(buffer);
        Assert.fail();
    } catch (BadMessageException e) {
        assertThat(e.getCode(), equalTo(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE_431));
        assertThat(e.getReason(), Matchers.startsWith("Indexed field value too large"));
    }
}
Also used : BadMessageException(org.eclipse.jetty.http.BadMessageException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with BadMessageException

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

the class HpackDecoderTest method testUnknownIndex.

@Test
public void testUnknownIndex() {
    String encoded = "BE";
    ByteBuffer buffer = ByteBuffer.wrap(TypeUtil.fromHexString(encoded));
    HpackDecoder decoder = new HpackDecoder(128, 8192);
    try {
        decoder.decode(buffer);
        Assert.fail();
    } catch (BadMessageException e) {
        assertThat(e.getCode(), equalTo(HttpStatus.BAD_REQUEST_400));
        assertThat(e.getReason(), Matchers.startsWith("Unknown index"));
    }
}
Also used : BadMessageException(org.eclipse.jetty.http.BadMessageException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with BadMessageException

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

the class HttpChannel method onBadMessage.

public void onBadMessage(int status, String reason) {
    if (status < 400 || status > 599)
        status = HttpStatus.BAD_REQUEST_400;
    Action action;
    try {
        action = _state.handling();
    } catch (IllegalStateException e) {
        // The bad message cannot be handled in the current state, so throw
        // to hopefull somebody that can handle
        abort(e);
        throw new BadMessageException(status, reason);
    }
    try {
        if (action == Action.DISPATCH) {
            ByteBuffer content = null;
            HttpFields fields = new HttpFields();
            ErrorHandler handler = getServer().getBean(ErrorHandler.class);
            if (handler != null)
                content = handler.badMessageError(status, reason, fields);
            sendResponse(new MetaData.Response(HttpVersion.HTTP_1_1, status, reason, fields, BufferUtil.length(content)), content, true);
        }
    } catch (IOException e) {
        LOG.debug(e);
    } finally {
        // TODO: review whether it's the right state to check.
        if (_state.unhandle() == Action.COMPLETE)
            _state.onComplete();
        else
            // TODO: don't throw from finally blocks !
            throw new IllegalStateException();
        onCompleted();
    }
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Action(org.eclipse.jetty.server.HttpChannelState.Action) BadMessageException(org.eclipse.jetty.http.BadMessageException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 15 with BadMessageException

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

the class HttpChannelOverHttp method upgrade.

/**
     * <p>Attempts to perform a HTTP/1.1 upgrade.</p>
     * <p>The upgrade looks up a {@link ConnectionFactory.Upgrading} from the connector
     * matching the protocol specified in the {@code Upgrade} header.</p>
     * <p>The upgrade may succeed, be ignored (which can allow a later handler to implement)
     * or fail with a {@link BadMessageException}.</p>
     *
     * @return true if the upgrade was performed, false if it was ignored
     * @throws BadMessageException if the upgrade failed
     */
private boolean upgrade() throws BadMessageException {
    if (LOG.isDebugEnabled())
        LOG.debug("upgrade {} {}", this, _upgrade);
    if (_upgrade != PREAMBLE_UPGRADE_H2C && (_connection == null || !_connection.contains("upgrade")))
        throw new BadMessageException(HttpStatus.BAD_REQUEST_400);
    // Find the upgrade factory
    ConnectionFactory.Upgrading factory = null;
    for (ConnectionFactory f : getConnector().getConnectionFactories()) {
        if (f instanceof ConnectionFactory.Upgrading) {
            if (f.getProtocols().contains(_upgrade.getValue())) {
                factory = (ConnectionFactory.Upgrading) f;
                break;
            }
        }
    }
    if (factory == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No factory for {} in {}", _upgrade, getConnector());
        return false;
    }
    // Create new connection
    HttpFields response101 = new HttpFields();
    Connection upgrade_connection = factory.upgradeConnection(getConnector(), getEndPoint(), _metadata, response101);
    if (upgrade_connection == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("Upgrade ignored for {} by {}", _upgrade, factory);
        return false;
    }
    // Send 101 if needed
    try {
        if (_upgrade != PREAMBLE_UPGRADE_H2C)
            sendResponse(new MetaData.Response(HttpVersion.HTTP_1_1, HttpStatus.SWITCHING_PROTOCOLS_101, response101, 0), null, true);
    } catch (IOException e) {
        throw new BadMessageException(HttpStatus.INTERNAL_SERVER_ERROR_500, null, e);
    }
    if (LOG.isDebugEnabled())
        LOG.debug("Upgrade from {} to {}", getEndPoint().getConnection(), upgrade_connection);
    getRequest().setAttribute(HttpConnection.UPGRADE_CONNECTION_ATTRIBUTE, upgrade_connection);
    getResponse().setStatus(101);
    getHttpTransport().onCompleted();
    return true;
}
Also used : BadMessageException(org.eclipse.jetty.http.BadMessageException) HttpFields(org.eclipse.jetty.http.HttpFields) Connection(org.eclipse.jetty.io.Connection) IOException(java.io.IOException)

Aggregations

BadMessageException (org.eclipse.jetty.http.BadMessageException)15 ByteBuffer (java.nio.ByteBuffer)4 HttpFields (org.eclipse.jetty.http.HttpFields)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 MetaData (org.eclipse.jetty.http.MetaData)3 HttpField (org.eclipse.jetty.http.HttpField)2 HttpHeader (org.eclipse.jetty.http.HttpHeader)2 IStream (org.eclipse.jetty.http2.IStream)2 Stream (org.eclipse.jetty.http2.api.Stream)2 X509Certificate (java.security.cert.X509Certificate)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSLSession (javax.net.ssl.SSLSession)1 AsyncListener (javax.servlet.AsyncListener)1 ServletException (javax.servlet.ServletException)1 ServletInputStream (javax.servlet.ServletInputStream)1 UnavailableException (javax.servlet.UnavailableException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)1