Java Code Examples for io.netty.handler.codec.http2.Http2Headers#method()

The following examples show how to use io.netty.handler.codec.http2.Http2Headers#method() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadOnlyHttp2HeadersBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public int defaultClientHeaders() {
    Http2Headers headers = new DefaultHttp2Headers(false);
    for (int i = 0; i < headerCount; ++i) {
        headers.add(headerNames[i], headerValues[i]);
    }
    headers.method(HttpMethod.POST.asciiName());
    headers.scheme(HttpScheme.HTTPS.name());
    headers.path(path);
    headers.authority(authority);
    return iterate(headers);
}
 
Example 2
Source File: H2ToStH1ClientDuplexHandler.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof HttpRequestMetaData) {
        HttpRequestMetaData metaData = (HttpRequestMetaData) msg;
        HttpHeaders h1Headers = metaData.headers();
        CharSequence host = h1Headers.getAndRemove(HOST);
        Http2Headers h2Headers = h1HeadersToH2Headers(h1Headers);
        if (host == null) {
            host = metaData.effectiveHost();
            if (host != null) {
                h2Headers.authority(host);
            }
        } else {
            h2Headers.authority(host);
        }
        method = metaData.method();
        h2Headers.method(method.name());
        if (!CONNECT.equals(method)) {
            // The ":scheme" and ":path" pseudo-header fields MUST be omitted for CONNECT.
            // https://tools.ietf.org/html/rfc7540#section-8.3
            h2Headers.scheme(scheme.name());
            h2Headers.path(metaData.requestTarget());
        }
        ctx.write(new DefaultHttp2HeadersFrame(h2Headers, false), promise);
    } else if (msg instanceof Buffer) {
        writeBuffer(ctx, msg, promise);
    } else if (msg instanceof HttpHeaders) {
        writeTrailers(ctx, msg, promise);
    } else {
        ctx.write(msg, promise);
    }
}
 
Example 3
Source File: Http2ClientCodec.java    From xio with Apache License 2.0 5 votes vote down vote up
private Response wrapResponse(ChannelHandlerContext ctx, Http2Response msg) {
  log.debug("wrapResponse msg={}", msg);
  final Response response;
  Http2MessageSession session = Http2MessageSession.lazyCreateSession(ctx);
  int streamId =
      Http2ClientStreamMapper.http2ClientStreamMapper(ctx).inboundStreamId(msg.streamId, msg.eos);
  if (msg.payload instanceof Http2Headers) {
    Http2Headers headers = (Http2Headers) msg.payload;
    if (msg.eos && headers.method() == null && headers.status() == null) {
      response =
          session
              .currentResponse(msg.streamId)
              .map(
                  resp ->
                      session.onInboundResponse(
                          new SegmentedResponseData(
                              resp, new Http2SegmentedData(headers, streamId))))
              .orElse(null);
    } else {
      response = wrapHeaders(headers, streamId, msg.eos);
      session.onInboundResponse(response);
    }
  } else if (msg.payload instanceof Http2DataFrame) {
    Http2DataFrame frame = (Http2DataFrame) msg.payload;
    response =
        session
            .currentResponse(streamId)
            .map(
                resp ->
                    session.onInboundResponse(
                        new SegmentedResponseData(
                            resp, new Http2SegmentedData(frame.content(), msg.eos, streamId))))
            .orElse(null);
  } else {
    // TODO(CK): throw an exception?
    response = null;
  }

  return response;
}
 
Example 4
Source File: Http2RequestDecoder.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
                          boolean endOfStream) throws Http2Exception {
    keepAliveChannelRead();
    DecodedHttpRequest req = requests.get(streamId);
    if (req == null) {
        // Validate the method.
        final CharSequence method = headers.method();
        if (method == null) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST, DATA_MISSING_METHOD);
            return;
        }
        if (!HttpMethod.isSupported(method.toString())) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.METHOD_NOT_ALLOWED,
                               DATA_UNSUPPORTED_METHOD);
            return;
        }

        // Validate the 'content-length' header if exists.
        final boolean contentEmpty;
        if (headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
            final long contentLength = headers.getLong(HttpHeaderNames.CONTENT_LENGTH, -1L);
            if (contentLength < 0) {
                writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST,
                                   DATA_INVALID_CONTENT_LENGTH);
                return;
            }
            contentEmpty = contentLength == 0;
        } else {
            contentEmpty = true;
        }

        if (!handle100Continue(ctx, streamId, headers)) {
            writeErrorResponse(ctx, streamId, HttpResponseStatus.EXPECTATION_FAILED, null);
            return;
        }

        req = new DecodedHttpRequest(ctx.channel().eventLoop(), ++nextId, streamId,
                                     ArmeriaHttpUtil.toArmeriaRequestHeaders(ctx, headers, endOfStream,
                                                                             scheme, cfg),
                                     true, inboundTrafficController,
                                     // FIXME(trustin): Use a different maxRequestLength
                                     //                 for a different host.
                                     cfg.defaultVirtualHost().maxRequestLength());

        // Close the request early when it is sure that there will be
        // neither content nor trailers.
        if (contentEmpty && endOfStream) {
            req.close();
        }

        requests.put(streamId, req);
        ctx.fireChannelRead(req);
    } else {
        try {
            req.write(ArmeriaHttpUtil.toArmeria(headers, true, endOfStream));
        } catch (Throwable t) {
            req.close(t);
            throw connectionError(INTERNAL_ERROR, t, "failed to consume a HEADERS frame");
        }
    }

    if (endOfStream) {
        req.close();
    }
}