Search in sources :

Example 1 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttpToHttp2Adapter method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpMessage) {
        FullHttpMessage message = (FullHttpMessage) msg;
        try {
            int streamId = getStreamId(message.headers());
            Http2Stream stream = connection.stream(streamId);
            if (stream == null) {
                stream = connection.remote().createStream(streamId, false);
            }
            message.headers().set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), HttpScheme.HTTP.name());
            Http2Headers messageHeaders = HttpConversionUtil.toHttp2Headers(message, true);
            boolean hasContent = message.content().isReadable();
            boolean hasTrailers = !message.trailingHeaders().isEmpty();
            listener.onHeadersRead(ctx, streamId, messageHeaders, 0, !(hasContent || hasTrailers));
            if (hasContent) {
                listener.onDataRead(ctx, streamId, message.content(), 0, !hasTrailers);
            }
            if (hasTrailers) {
                Http2Headers headers = HttpConversionUtil.toHttp2Headers(message.trailingHeaders(), true);
                listener.onHeadersRead(ctx, streamId, headers, 0, true);
            }
            stream.closeRemoteSide();
        } finally {
            message.release();
        }
    } else {
        super.channelRead(ctx, msg);
    }
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Example 2 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapter method onPushPromiseRead.

@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
    // A push promise should not be allowed to add headers to an existing stream
    Http2Stream promisedStream = connection.stream(promisedStreamId);
    if (headers.status() == null) {
        // A PUSH_PROMISE frame has no Http response status.
        // https://tools.ietf.org/html/rfc7540#section-8.2.1
        // Server push is semantically equivalent to a server responding to a
        // request; however, in this case, that request is also sent by the
        // server, as a PUSH_PROMISE frame.
        headers.status(OK.codeAsText());
    }
    FullHttpMessage msg = processHeadersBegin(ctx, promisedStream, headers, false, false, false);
    if (msg == null) {
        throw connectionError(PROTOCOL_ERROR, "Push Promise Frame received for pre-existing stream id %d", promisedStreamId);
    }
    msg.headers().setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_PROMISE_ID.text(), streamId);
    msg.headers().setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT);
    processHeadersEnd(ctx, promisedStream, msg, false);
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Example 3 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapter method onHeadersRead.

@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception {
    Http2Stream stream = connection.stream(streamId);
    FullHttpMessage msg = processHeadersBegin(ctx, stream, headers, endOfStream, true, true);
    if (msg != null) {
        processHeadersEnd(ctx, stream, msg, endOfStream);
    }
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage)

Example 4 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class InboundHttp2ToHttpAdapter method onDataRead.

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
    Http2Stream stream = connection.stream(streamId);
    FullHttpMessage msg = getMessage(stream);
    if (msg == null) {
        throw connectionError(PROTOCOL_ERROR, "Data Frame received for unknown stream id %d", streamId);
    }
    ByteBuf content = msg.content();
    final int dataReadableBytes = data.readableBytes();
    if (content.readableBytes() > maxContentLength - dataReadableBytes) {
        throw connectionError(INTERNAL_ERROR, "Content length exceeded max of %d for stream id %d", maxContentLength, streamId);
    }
    content.writeBytes(data, data.readerIndex(), dataReadableBytes);
    if (endOfStream) {
        fireChannelRead(ctx, msg, false, stream);
    }
    // All bytes have been processed.
    return dataReadableBytes + padding;
}
Also used : FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with FullHttpMessage

use of io.netty.handler.codec.http.FullHttpMessage in project netty by netty.

the class Http2StreamFrameToHttpObjectCodec method encode.

/**
 * Encode from an {@link HttpObject} to an {@link Http2StreamFrame}. This method will
 * be called for each written message that can be handled by this encoder.
 *
 * NOTE: 100-Continue responses that are NOT {@link FullHttpResponse} will be rejected.
 *
 * @param ctx           the {@link ChannelHandlerContext} which this handler belongs to
 * @param obj           the {@link HttpObject} message to encode
 * @param out           the {@link List} into which the encoded msg should be added
 *                      needs to do some kind of aggregation
 * @throws Exception    is thrown if an error occurs
 */
@Override
protected void encode(ChannelHandlerContext ctx, HttpObject obj, List<Object> out) throws Exception {
    // Http2HeadersFrame should not be marked as endStream=true
    if (obj instanceof HttpResponse) {
        final HttpResponse res = (HttpResponse) obj;
        if (res.status().equals(HttpResponseStatus.CONTINUE)) {
            if (res instanceof FullHttpResponse) {
                final Http2Headers headers = toHttp2Headers(ctx, res);
                out.add(new DefaultHttp2HeadersFrame(headers, false));
                return;
            } else {
                throw new EncoderException(HttpResponseStatus.CONTINUE + " must be a FullHttpResponse");
            }
        }
    }
    if (obj instanceof HttpMessage) {
        Http2Headers headers = toHttp2Headers(ctx, (HttpMessage) obj);
        boolean noMoreFrames = false;
        if (obj instanceof FullHttpMessage) {
            FullHttpMessage full = (FullHttpMessage) obj;
            noMoreFrames = !full.content().isReadable() && full.trailingHeaders().isEmpty();
        }
        out.add(new DefaultHttp2HeadersFrame(headers, noMoreFrames));
    }
    if (obj instanceof LastHttpContent) {
        LastHttpContent last = (LastHttpContent) obj;
        encodeLastContent(last, out);
    } else if (obj instanceof HttpContent) {
        HttpContent cont = (HttpContent) obj;
        out.add(new DefaultHttp2DataFrame(cont.content().retain(), false));
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpMessage(io.netty.handler.codec.http.HttpMessage) FullHttpMessage(io.netty.handler.codec.http.FullHttpMessage) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Aggregations

FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)12 ByteBuf (io.netty.buffer.ByteBuf)11 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 AsciiString (io.netty.util.AsciiString)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)10 Test (org.junit.jupiter.api.Test)10 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 HttpMessage (io.netty.handler.codec.http.HttpMessage)3 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)3 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 EncoderException (io.netty.handler.codec.EncoderException)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 EmptyHttpHeaders (io.netty.handler.codec.http.EmptyHttpHeaders)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1