Search in sources :

Example 46 with AsciiString

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

the class ByteBufUtilTest method testWriteUsAsciiString.

@Test
public void testWriteUsAsciiString() {
    AsciiString usAscii = new AsciiString("NettyRocks");
    ByteBuf buf = Unpooled.buffer(16);
    buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII));
    ByteBuf buf2 = Unpooled.buffer(16);
    ByteBufUtil.writeAscii(buf2, usAscii);
    assertEquals(buf, buf2);
    buf.release();
    buf2.release();
}
Also used : AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 47 with AsciiString

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

the class ReadOnlyHttp2Headers method contains.

@Override
public boolean contains(CharSequence name, CharSequence value) {
    final int nameHash = AsciiString.hashCode(name);
    final int valueHash = AsciiString.hashCode(value);
    final int pseudoHeadersEnd = pseudoHeaders.length - 1;
    for (int i = 0; i < pseudoHeadersEnd; i += 2) {
        AsciiString roName = pseudoHeaders[i];
        AsciiString roValue = pseudoHeaders[i + 1];
        if (roName.hashCode() == nameHash && roValue.hashCode() == valueHash && roName.contentEqualsIgnoreCase(name) && roValue.contentEqualsIgnoreCase(value)) {
            return true;
        }
    }
    final int otherHeadersEnd = otherHeaders.length - 1;
    for (int i = 0; i < otherHeadersEnd; i += 2) {
        AsciiString roName = otherHeaders[i];
        AsciiString roValue = otherHeaders[i + 1];
        if (roName.hashCode() == nameHash && roValue.hashCode() == valueHash && roName.contentEqualsIgnoreCase(name) && roValue.contentEqualsIgnoreCase(value)) {
            return true;
        }
    }
    return false;
}
Also used : AsciiString(io.netty.util.AsciiString)

Example 48 with AsciiString

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

the class HttpConversionUtil method toHttp2Headers.

public static void toHttp2Headers(HttpHeaders inHeaders, Http2Headers out) {
    Iterator<Entry<CharSequence, CharSequence>> iter = inHeaders.iteratorCharSequence();
    while (iter.hasNext()) {
        Entry<CharSequence, CharSequence> entry = iter.next();
        final AsciiString aName = AsciiString.of(entry.getKey()).toLowerCase();
        if (!HTTP_TO_HTTP2_HEADER_BLACKLIST.contains(aName)) {
            // https://tools.ietf.org/html/rfc7540#section-8.1.2.2 makes a special exception for TE
            if (aName.contentEqualsIgnoreCase(HttpHeaderNames.TE) && !AsciiString.contentEqualsIgnoreCase(entry.getValue(), HttpHeaderValues.TRAILERS)) {
                throw new IllegalArgumentException("Invalid value for " + HttpHeaderNames.TE + ": " + entry.getValue());
            }
            if (aName.contentEqualsIgnoreCase(HttpHeaderNames.COOKIE)) {
                AsciiString value = AsciiString.of(entry.getValue());
                // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
                try {
                    int index = value.forEachByte(FIND_SEMI_COLON);
                    if (index != -1) {
                        int start = 0;
                        do {
                            out.add(HttpHeaderNames.COOKIE, value.subSequence(start, index, false));
                            // skip 2 characters "; " (see https://tools.ietf.org/html/rfc6265#section-4.2.1)
                            start = index + 2;
                        } while (start < value.length() && (index = value.forEachByte(start, value.length() - start, FIND_SEMI_COLON)) != -1);
                        if (start >= value.length()) {
                            throw new IllegalArgumentException("cookie value is of unexpected format: " + value);
                        }
                        out.add(HttpHeaderNames.COOKIE, value.subSequence(start, value.length(), false));
                    } else {
                        out.add(HttpHeaderNames.COOKIE, value);
                    }
                } catch (Exception e) {
                    // because of the ByteProcessor interface.
                    throw new IllegalStateException(e);
                }
            } else {
                out.add(aName, entry.getValue());
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) AsciiString(io.netty.util.AsciiString)

Example 49 with AsciiString

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

the class HttpConversionUtil method setHttp2Scheme.

private static void setHttp2Scheme(HttpHeaders in, URI uri, Http2Headers out) {
    String value = uri.getScheme();
    if (value != null) {
        out.scheme(new AsciiString(value));
        return;
    }
    // Consume the Scheme extension header if present
    CharSequence cValue = in.get(ExtensionHeaderNames.SCHEME.text());
    if (cValue != null) {
        out.scheme(AsciiString.of(cValue));
        return;
    }
    if (uri.getPort() == HTTPS.port()) {
        out.scheme(HTTPS.name());
    } else if (uri.getPort() == HTTP.port()) {
        out.scheme(HTTP.name());
    } else {
        throw new IllegalArgumentException(":scheme must be specified. " + "see https://tools.ietf.org/html/rfc7540#section-8.1.2.3");
    }
}
Also used : AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString)

Example 50 with AsciiString

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

the class HpackHuffmanEncoder method encode.

/**
     * Compresses the input string literal using the Huffman coding.
     *
     * @param out the output stream for the compressed data
     * @param data the string literal to be Huffman encoded
     */
public void encode(ByteBuf out, CharSequence data) {
    ObjectUtil.checkNotNull(out, "out");
    if (data instanceof AsciiString) {
        AsciiString string = (AsciiString) data;
        try {
            encodeProcessor.out = out;
            string.forEachByte(encodeProcessor);
        } catch (Exception e) {
            PlatformDependent.throwException(e);
        } finally {
            encodeProcessor.end();
        }
    } else {
        encodeSlowPath(out, data);
    }
}
Also used : 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