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