Search in sources :

Example 16 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class HttpConversionUtil method toHttp2Path.

/**
     * Generate a HTTP/2 {code :path} from a URI in accordance with
     * <a href="https://tools.ietf.org/html/rfc7230#section-5.3">rfc7230, 5.3</a>.
     */
private static AsciiString toHttp2Path(URI uri) {
    StringBuilder pathBuilder = new StringBuilder(length(uri.getRawPath()) + length(uri.getRawQuery()) + length(uri.getRawFragment()) + 2);
    if (!isNullOrEmpty(uri.getRawPath())) {
        pathBuilder.append(uri.getRawPath());
    }
    if (!isNullOrEmpty(uri.getRawQuery())) {
        pathBuilder.append('?');
        pathBuilder.append(uri.getRawQuery());
    }
    if (!isNullOrEmpty(uri.getRawFragment())) {
        pathBuilder.append('#');
        pathBuilder.append(uri.getRawFragment());
    }
    String path = pathBuilder.toString();
    return path.isEmpty() ? EMPTY_REQUEST_PATH : new AsciiString(path);
}
Also used : AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString)

Example 17 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class HttpConversionUtil method toHttp2Headers.

/**
     * Converts the given HTTP/1.x headers into HTTP/2 headers.
     * The following headers are only used if they can not be found in from the {@code HOST} header or the
     * {@code Request-Line} as defined by <a href="https://tools.ietf.org/html/rfc7230">rfc7230</a>
     * <ul>
     * <li>{@link ExtensionHeaderNames#SCHEME}</li>
     * </ul>
     * {@link ExtensionHeaderNames#PATH} is ignored and instead extracted from the {@code Request-Line}.
     */
public static Http2Headers toHttp2Headers(HttpMessage in, boolean validateHeaders) {
    HttpHeaders inHeaders = in.headers();
    final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size());
    if (in instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) in;
        URI requestTargetUri = URI.create(request.uri());
        out.path(toHttp2Path(requestTargetUri));
        out.method(request.method().asciiName());
        setHttp2Scheme(inHeaders, requestTargetUri, out);
        if (!isOriginForm(requestTargetUri) && !isAsteriskForm(requestTargetUri)) {
            // Attempt to take from HOST header before taking from the request-line
            String host = inHeaders.getAsString(HttpHeaderNames.HOST);
            setHttp2Authority((host == null || host.isEmpty()) ? requestTargetUri.getAuthority() : host, out);
        }
    } else if (in instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) in;
        out.status(new AsciiString(Integer.toString(response.status().code())));
    }
    // Add the HTTP headers which have not been consumed above
    toHttp2Headers(inHeaders, out);
    return out;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) AsciiString(io.netty.util.AsciiString) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) AsciiString(io.netty.util.AsciiString) URI(java.net.URI)

Example 18 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class ReadOnlyHttp2Headers method validateHeaders.

private static void validateHeaders(AsciiString[] pseudoHeaders, AsciiString... otherHeaders) {
    // We are only validating values... so start at 1 and go until end.
    for (int i = 1; i < pseudoHeaders.length; i += 2) {
        // pseudoHeaders names are only set internally so they are assumed to be valid.
        if (pseudoHeaders[i] == null) {
            throw new IllegalArgumentException("pseudoHeaders value at index " + i + " is null");
        }
    }
    boolean seenNonPseudoHeader = false;
    final int otherHeadersEnd = otherHeaders.length - 1;
    for (int i = 0; i < otherHeadersEnd; i += 2) {
        AsciiString name = otherHeaders[i];
        HTTP2_NAME_VALIDATOR.validateName(name);
        if (!seenNonPseudoHeader && !name.isEmpty() && name.byteAt(0) != PSEUDO_HEADER_TOKEN) {
            seenNonPseudoHeader = true;
        } else if (seenNonPseudoHeader && !name.isEmpty() && name.byteAt(0) == PSEUDO_HEADER_TOKEN) {
            throw new IllegalArgumentException("otherHeaders name at index " + i + " is a pseudo header that appears after non-pseudo headers.");
        }
        if (otherHeaders[i + 1] == null) {
            throw new IllegalArgumentException("otherHeaders value at index " + (i + 1) + " is null");
        }
    }
}
Also used : AsciiString(io.netty.util.AsciiString)

Example 19 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class ReadOnlyHttp2Headers method get0.

private AsciiString get0(CharSequence name) {
    final int nameHash = AsciiString.hashCode(name);
    final int pseudoHeadersEnd = pseudoHeaders.length - 1;
    for (int i = 0; i < pseudoHeadersEnd; i += 2) {
        AsciiString roName = pseudoHeaders[i];
        if (roName.hashCode() == nameHash && roName.contentEqualsIgnoreCase(name)) {
            return pseudoHeaders[i + 1];
        }
    }
    final int otherHeadersEnd = otherHeaders.length - 1;
    for (int i = 0; i < otherHeadersEnd; i += 2) {
        AsciiString roName = otherHeaders[i];
        if (roName.hashCode() == nameHash && roName.contentEqualsIgnoreCase(name)) {
            return otherHeaders[i + 1];
        }
    }
    return null;
}
Also used : AsciiString(io.netty.util.AsciiString)

Example 20 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class ReadOnlyHttp2Headers method getAll.

@Override
public List<CharSequence> getAll(CharSequence name) {
    final int nameHash = AsciiString.hashCode(name);
    List<CharSequence> values = new ArrayList<CharSequence>();
    final int pseudoHeadersEnd = pseudoHeaders.length - 1;
    for (int i = 0; i < pseudoHeadersEnd; i += 2) {
        AsciiString roName = pseudoHeaders[i];
        if (roName.hashCode() == nameHash && roName.contentEqualsIgnoreCase(name)) {
            values.add(pseudoHeaders[i + 1]);
        }
    }
    final int otherHeadersEnd = otherHeaders.length - 1;
    for (int i = 0; i < otherHeadersEnd; i += 2) {
        AsciiString roName = otherHeaders[i];
        if (roName.hashCode() == nameHash && roName.contentEqualsIgnoreCase(name)) {
            values.add(otherHeaders[i + 1]);
        }
    }
    return values;
}
Also used : ArrayList(java.util.ArrayList) AsciiString(io.netty.util.AsciiString)

Aggregations

AsciiString (io.netty.util.AsciiString)73 Test (org.junit.Test)43 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)27 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)25 ByteBuf (io.netty.buffer.ByteBuf)18 ChannelPromise (io.netty.channel.ChannelPromise)15 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)12 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)12 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 Http2Headers (io.netty.handler.codec.http2.Http2Headers)9 Metadata (io.grpc.Metadata)8 ChannelFuture (io.netty.channel.ChannelFuture)4 Status (io.grpc.Status)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Setup (org.openjdk.jmh.annotations.Setup)3 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2