Example usage for io.netty.handler.codec.http2 Http2Error PROTOCOL_ERROR

List of usage examples for io.netty.handler.codec.http2 Http2Error PROTOCOL_ERROR

Introduction

In this page you can find the example usage for io.netty.handler.codec.http2 Http2Error PROTOCOL_ERROR.

Prototype

Http2Error PROTOCOL_ERROR

To view the source code for io.netty.handler.codec.http2 Http2Error PROTOCOL_ERROR.

Click Source Link

Usage

From source file:com.linkedin.r2.transport.http.client.Http2FrameListener.java

License:Apache License

@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
        boolean endOfStream) throws Http2Exception {
    LOG.debug("Received HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes",
            new Object[] { streamId, endOfStream, headers.size(), padding });
    // Ignores response for the upgrade request
    if (streamId == Http2CodecUtil.HTTP_UPGRADE_STREAM_ID) {
        return;/* ww  w  .j  av  a  2  s. c o m*/
    }

    final StreamResponseBuilder builder = new StreamResponseBuilder();
    // Process HTTP/2 pseudo headers
    if (headers.status() != null) {
        builder.setStatus(Integer.parseInt(headers.status().toString()));
    }
    if (headers.authority() != null) {
        builder.addHeaderValue(HttpHeaderNames.HOST.toString(), headers.authority().toString());
    }

    // Process other HTTP headers
    for (Map.Entry<CharSequence, CharSequence> header : headers) {
        if (Http2Headers.PseudoHeaderName.isPseudoHeader(header.getKey())) {
            // Do no set HTTP/2 pseudo headers to response
            continue;
        }

        final String key = header.getKey().toString();
        final String value = header.getValue().toString();
        if (key.equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
            builder.addCookie(value);
        } else {
            builder.unsafeAddHeaderValue(key, value);
        }
    }

    // Gets async pool handle from stream properties
    Http2Connection.PropertyKey handleKey = ctx.channel()
            .attr(Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY).get();
    TimeoutAsyncPoolHandle<?> handle = _connection.stream(streamId).removeProperty(handleKey);
    if (handle == null) {
        _lifecycleManager.onError(ctx, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR,
                "No channel pool handle is associated with this stream", streamId));
        return;
    }

    final StreamResponse response;
    if (endOfStream) {
        response = builder.build(EntityStreams.emptyStream());
        ctx.fireChannelRead(handle);
    } else {
        // Associate an entity stream writer to the HTTP/2 stream
        final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, streamId, _maxContentLength,
                handle);
        if (_connection.stream(streamId).setProperty(_writerKey, writer) != null) {
            _lifecycleManager.onError(ctx, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR,
                    "Another writer has already been associated with current stream ID", streamId));
            return;
        }

        // Prepares StreamResponse for the channel pipeline
        EntityStream entityStream = EntityStreams.newEntityStream(writer);
        response = builder.build(entityStream);
    }

    // Gets callback from stream properties
    Http2Connection.PropertyKey callbackKey = ctx.channel()
            .attr(Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY).get();
    TransportCallback<?> callback = _connection.stream(streamId).removeProperty(callbackKey);
    if (callback != null) {
        ctx.fireChannelRead(new ResponseWithCallback<Response, TransportCallback<?>>(response, callback));
    }
}

From source file:io.vertx.core.http.impl.Http2ServerConnection.java

License:Open Source License

@Override
public synchronized void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
        int padding, boolean endOfStream) {
    VertxHttp2Stream stream = streams.get(streamId);
    if (stream == null) {
        if (isMalformedRequest(headers)) {
            handler.writeReset(streamId, Http2Error.PROTOCOL_ERROR.code());
            return;
        }/* w w  w  .java2  s.  c o m*/
        String contentEncoding = options.isCompressionSupported() ? HttpUtils.determineContentEncoding(headers)
                : null;
        Http2Stream s = handler.connection().stream(streamId);
        boolean writable = handler.encoder().flowController().isWritable(s);
        Http2ServerRequestImpl req = new Http2ServerRequestImpl(this, s, metrics, serverOrigin, headers,
                contentEncoding, writable);
        stream = req;
        CharSequence value = headers.get(HttpHeaderNames.EXPECT);
        if (options.isHandle100ContinueAutomatically()
                && ((value != null && HttpHeaderValues.CONTINUE.equals(value))
                        || headers.contains(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE))) {
            req.response().writeContinue();
        }
        streams.put(streamId, req);
        context.executeFromIO(() -> {
            Http2ServerResponseImpl resp = req.response();
            resp.beginRequest();
            requestHandler.handle(req);
            boolean hasPush = resp.endRequest();
            if (hasPush) {
                ctx.flush();
            }
        });
    } else {
        // Http server request trailer - not implemented yet (in api)
    }
    if (endOfStream) {
        context.executeFromIO(stream::onEnd);
    }
}