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();
}
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;
}
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());
}
}
}
}
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");
}
}
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);
}
}
Aggregations