Example usage for io.netty.util AsciiString isEmpty

List of usage examples for io.netty.util AsciiString isEmpty

Introduction

In this page you can find the example usage for io.netty.util AsciiString isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Determine if this instance has 0 length.

Usage

From source file:com.linecorp.armeria.internal.ArmeriaHttpUtil.java

License:Apache License

/**
 * Translate and add HTTP/2 headers to HTTP/1.x headers.
 *
 * @param streamId The stream associated with {@code sourceHeaders}.
 * @param inputHeaders The HTTP/2 headers to convert.
 * @param outputHeaders The object which will contain the resulting HTTP/1.x headers..
 * @param httpVersion What HTTP/1.x version {@code outputHeaders} should be treated as
 *                    when doing the conversion.
 * @param isTrailer {@code true} if {@code outputHeaders} should be treated as trailing headers.
 *                  {@code false} otherwise.
 * @param isRequest {@code true} if the {@code outputHeaders} will be used in a request message.
 *                  {@code false} for response message.
 *
 * @throws Http2Exception If not all HTTP/2 headers can be translated to HTTP/1.x.
 *///from   w  ww. j a v a 2  s. co m
public static void toNettyHttp1(int streamId, HttpHeaders inputHeaders,
        io.netty.handler.codec.http.HttpHeaders outputHeaders, HttpVersion httpVersion, boolean isTrailer,
        boolean isRequest) throws Http2Exception {

    final CharSequenceMap translations = isRequest ? REQUEST_HEADER_TRANSLATIONS : RESPONSE_HEADER_TRANSLATIONS;
    StringJoiner cookieJoiner = null;
    try {
        for (Entry<AsciiString, String> entry : inputHeaders) {
            final AsciiString name = entry.getKey();
            final String value = entry.getValue();
            final AsciiString translatedName = translations.get(name);
            if (translatedName != null) {
                outputHeaders.add(translatedName, value);
                continue;
            }

            // https://tools.ietf.org/html/rfc7540#section-8.1.2.3
            if (name.isEmpty() || HTTP2_TO_HTTP_HEADER_BLACKLIST.contains(name)) {
                continue;
            }

            if (HttpHeaderNames.COOKIE.equals(name)) {
                // combine the cookie values into 1 header entry.
                // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
                if (cookieJoiner == null) {
                    cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
                }
                COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
            } else {
                outputHeaders.add(name, value);
            }
        }

        if (cookieJoiner != null && cookieJoiner.length() != 0) {
            outputHeaders.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
        }
    } catch (Throwable t) {
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }

    if (!isTrailer) {
        HttpUtil.setKeepAlive(outputHeaders, httpVersion, true);
    }
}

From source file:com.linecorp.armeria.internal.http.ArmeriaHttpUtil.java

License:Apache License

public static io.netty.handler.codec.http.HttpHeaders toNettyHttp1(HttpHeaders inputHeaders) {
    final io.netty.handler.codec.http.DefaultHttpHeaders outputHeaders = new io.netty.handler.codec.http.DefaultHttpHeaders();
    for (Entry<AsciiString, String> e : inputHeaders) {
        final AsciiString name = e.getKey();
        if (name.isEmpty() || HTTP2_TO_HTTP_HEADER_BLACKLIST.contains(name)) {
            continue;
        }//from  ww w.ja va  2s . c  om
        outputHeaders.add(name, e.getValue());
    }
    return outputHeaders;
}

From source file:com.linecorp.armeria.server.http.jetty.JettyService.java

License:Apache License

private static MetaData.Request toRequestMetadata(ServiceRequestContext ctx, AggregatedHttpMessage aReq) {
    // Construct the HttpURI
    final StringBuilder uriBuf = new StringBuilder();
    final HttpHeaders aHeaders = aReq.headers();

    uriBuf.append(ctx.sessionProtocol().isTls() ? "https" : "http");
    uriBuf.append("://");
    uriBuf.append(aHeaders.authority());
    uriBuf.append(aHeaders.path());// w  ww .  ja va  2s .  co  m

    final HttpURI uri = new HttpURI(uriBuf.toString());
    final String encoded = PATH_SPLITTER.splitToList(ctx.mappedPath()).stream()
            .map(UrlEscapers.urlPathSegmentEscaper()::escape).collect(Collectors.joining("/"));
    uri.setPath(encoded);

    // Convert HttpHeaders to HttpFields
    final HttpFields jHeaders = new HttpFields(aHeaders.size());
    aHeaders.forEach(e -> {
        final AsciiString key = e.getKey();
        if (!key.isEmpty() && key.byteAt(0) != ':') {
            jHeaders.add(key.toString(), e.getValue());
        }
    });

    return new MetaData.Request(aHeaders.method().name(), uri, HttpVersion.HTTP_1_1, jHeaders,
            aReq.content().length());
}

From source file:com.linecorp.armeria.server.http.tomcat.TomcatService.java

License:Apache License

private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;/*  w ww . ja va  2  s.  co m*/
    }

    for (Entry<AsciiString, String> e : headers) {
        final AsciiString k = e.getKey();
        final String v = e.getValue();

        if (k.isEmpty() || k.byteAt(0) == ':') {
            continue;
        }

        final MessageBytes cValue = cHeaders.addValue(k.array(), k.arrayOffset(), k.length());
        final byte[] valueBytes = v.getBytes(StandardCharsets.US_ASCII);
        cValue.setBytes(valueBytes, 0, valueBytes.length);
    }
}

From source file:com.linecorp.armeria.server.jetty.JettyService.java

License:Apache License

private static MetaData.Request toRequestMetadata(ServiceRequestContext ctx, AggregatedHttpMessage aReq) {
    // Construct the HttpURI
    final StringBuilder uriBuf = new StringBuilder();
    final HttpHeaders aHeaders = aReq.headers();

    uriBuf.append(ctx.sessionProtocol().isTls() ? "https" : "http");
    uriBuf.append("://");
    uriBuf.append(aHeaders.authority());
    uriBuf.append(aHeaders.path());/*from ww w.  j ava2s  .  c om*/

    final HttpURI uri = new HttpURI(uriBuf.toString());
    uri.setPath(ctx.mappedPath());

    // Convert HttpHeaders to HttpFields
    final HttpFields jHeaders = new HttpFields(aHeaders.size());
    aHeaders.forEach(e -> {
        final AsciiString key = e.getKey();
        if (!key.isEmpty() && key.byteAt(0) != ':') {
            jHeaders.add(key.toString(), e.getValue());
        }
    });

    return new MetaData.Request(aHeaders.method().name(), uri, HttpVersion.HTTP_1_1, jHeaders,
            aReq.content().length());
}