Java Code Examples for io.netty.handler.codec.http.HttpHeaders#getInt()

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#getInt() . 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: Http2ClientChannelHandler.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    HttpHeaders headers = msg.headers();
    Integer streamId = headers.getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("HttpResponseHandler unexpected message received: {}, data is {}", msg.toString(),
                NettyHelper.toString(msg.content()));
        }
        return;
    }

    Entry<ChannelFuture, AbstractHttpClientHandler> entry = removePromise(streamId);
    if (entry == null) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Message received for unknown stream id {}, msg is {}, data is {}", streamId,
                msg.toString(), NettyHelper.toString(msg.content()));
        }
    } else {
        final AbstractHttpClientHandler callback = entry.getValue();
        callback.receiveHttpResponse(msg);
    }
}
 
Example 2
Source File: SpdyHttpEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private SpdyHeadersFrame createHeadersFrame(HttpResponse httpResponse) throws Exception {
    // Get the Stream-ID from the headers
    final HttpHeaders httpHeaders = httpResponse.headers();
    int streamId = httpHeaders.getInt(SpdyHttpHeaders.Names.STREAM_ID);
    httpHeaders.remove(SpdyHttpHeaders.Names.STREAM_ID);

    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpHeaders.remove(HttpHeaderNames.CONNECTION);
    httpHeaders.remove("Keep-Alive");
    httpHeaders.remove("Proxy-Connection");
    httpHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);

    SpdyHeadersFrame spdyHeadersFrame;
    if (SpdyCodecUtil.isServerId(streamId)) {
        spdyHeadersFrame = new DefaultSpdyHeadersFrame(streamId, validateHeaders);
    } else {
        spdyHeadersFrame = new DefaultSpdySynReplyFrame(streamId, validateHeaders);
    }
    SpdyHeaders frameHeaders = spdyHeadersFrame.headers();
    // Unfold the first line of the response into name/value pairs
    frameHeaders.set(SpdyHeaders.HttpNames.STATUS, httpResponse.status().codeAsText());
    frameHeaders.set(SpdyHeaders.HttpNames.VERSION, httpResponse.protocolVersion().text());

    // Transfer the remaining HTTP headers
    Iterator<Entry<CharSequence, CharSequence>> itr = httpHeaders.iteratorCharSequence();
    while (itr.hasNext()) {
        Map.Entry<CharSequence, CharSequence> entry = itr.next();
        final CharSequence headerName =
                headersToLowerCase ? AsciiString.of(entry.getKey()).toLowerCase() : entry.getKey();
        spdyHeadersFrame.headers().add(headerName, entry.getValue());
    }

    currentStreamId = streamId;
    spdyHeadersFrame.setLast(isLast(httpResponse));

    return spdyHeadersFrame;
}
 
Example 3
Source File: HttpToHttp2ConnectionHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void writeHeaders(ChannelHandlerContext ctx, Http2ConnectionEncoder encoder, int streamId,
                                 HttpHeaders headers, Http2Headers http2Headers, boolean endStream,
                                 SimpleChannelPromiseAggregator promiseAggregator) {
    int dependencyId = headers.getInt(
            HttpConversionUtil.ExtensionHeaderNames.STREAM_DEPENDENCY_ID.text(), 0);
    short weight = headers.getShort(
            HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT);
    encoder.writeHeaders(ctx, streamId, http2Headers, dependencyId, weight, false,
            0, endStream, promiseAggregator.newPromise());
}
 
Example 4
Source File: ResponseHeaderStrategy.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Duration apply(HttpClientResponse response) {
    HttpHeaders headers = response.responseHeaders();
    int remaining = headers.getInt("X-RateLimit-Remaining", -1);
    if (remaining == 0) {
        long resetAt = (long) (Double.parseDouble(headers.get("X-RateLimit-Reset-After")) * 1000);
        return Duration.ofMillis(resetAt);
    }
    return Duration.ZERO;
}
 
Example 5
Source File: SpdyHttpEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private SpdySynStreamFrame createSynStreamFrame(HttpRequest httpRequest) throws Exception {
    // Get the Stream-ID, Associated-To-Stream-ID, Priority, and scheme from the headers
    final HttpHeaders httpHeaders = httpRequest.headers();
    int streamId = httpHeaders.getInt(SpdyHttpHeaders.Names.STREAM_ID);
    int associatedToStreamId = httpHeaders.getInt(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID, 0);
    byte priority = (byte) httpHeaders.getInt(SpdyHttpHeaders.Names.PRIORITY, 0);
    CharSequence scheme = httpHeaders.get(SpdyHttpHeaders.Names.SCHEME);
    httpHeaders.remove(SpdyHttpHeaders.Names.STREAM_ID);
    httpHeaders.remove(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID);
    httpHeaders.remove(SpdyHttpHeaders.Names.PRIORITY);
    httpHeaders.remove(SpdyHttpHeaders.Names.SCHEME);

    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpHeaders.remove(HttpHeaderNames.CONNECTION);
    httpHeaders.remove("Keep-Alive");
    httpHeaders.remove("Proxy-Connection");
    httpHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);

    SpdySynStreamFrame spdySynStreamFrame =
            new DefaultSpdySynStreamFrame(streamId, associatedToStreamId, priority, validateHeaders);

    // Unfold the first line of the message into name/value pairs
    SpdyHeaders frameHeaders = spdySynStreamFrame.headers();
    frameHeaders.set(SpdyHeaders.HttpNames.METHOD, httpRequest.method().name());
    frameHeaders.set(SpdyHeaders.HttpNames.PATH, httpRequest.uri());
    frameHeaders.set(SpdyHeaders.HttpNames.VERSION, httpRequest.protocolVersion().text());

    // Replace the HTTP host header with the SPDY host header
    CharSequence host = httpHeaders.get(HttpHeaderNames.HOST);
    httpHeaders.remove(HttpHeaderNames.HOST);
    frameHeaders.set(SpdyHeaders.HttpNames.HOST, host);

    // Set the SPDY scheme header
    if (scheme == null) {
        scheme = "https";
    }
    frameHeaders.set(SpdyHeaders.HttpNames.SCHEME, scheme);

    // Transfer the remaining HTTP headers
    Iterator<Entry<CharSequence, CharSequence>> itr = httpHeaders.iteratorCharSequence();
    while (itr.hasNext()) {
        Map.Entry<CharSequence, CharSequence> entry = itr.next();
        final CharSequence headerName =
                headersToLowerCase ? AsciiString.of(entry.getKey()).toLowerCase() : entry.getKey();
        frameHeaders.add(headerName, entry.getValue());
    }
    currentStreamId = spdySynStreamFrame.streamId();
    if (associatedToStreamId == 0) {
        spdySynStreamFrame.setLast(isLast(httpRequest));
    } else {
        spdySynStreamFrame.setUnidirectional(true);
    }

    return spdySynStreamFrame;
}
 
Example 6
Source File: InboundHttpToHttp2Adapter.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static int getStreamId(Http2Connection connection, HttpHeaders httpHeaders) {
    return httpHeaders.getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
                              connection.remote().incrementAndGetNextStreamId());
}
 
Example 7
Source File: HttpToHttp2ConnectionHandler.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
/**
 * Get the next stream id either from the {@link HttpHeaders} object or HTTP/2 codec
 *
 * @param httpHeaders The HTTP/1.x headers object to look for the stream id
 * @return The stream id to use with this {@link HttpHeaders} object
 * @throws Exception If the {@code httpHeaders} object specifies an invalid stream id
 */
private int getStreamId(HttpHeaders httpHeaders) throws Exception {
    return httpHeaders.getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(),
                              connection().local().incrementAndGetNextStreamId());
}