use of org.apache.hc.core5.http.MessageHeaders in project httpcomponents-core by apache.
the class DefaultH2RequestConverter method convert.
@Override
public HttpRequest convert(final List<Header> headers) throws HttpException {
String method = null;
String scheme = null;
String authority = null;
String path = null;
final List<Header> messageHeaders = new ArrayList<>();
for (int i = 0; i < headers.size(); i++) {
final Header header = headers.get(i);
final String name = header.getName();
final String value = header.getValue();
for (int n = 0; n < name.length(); n++) {
final char ch = name.charAt(n);
if (Character.isAlphabetic(ch) && !Character.isLowerCase(ch)) {
throw new ProtocolException("Header name '%s' is invalid (header name contains uppercase characters)", name);
}
}
if (name.startsWith(":")) {
if (!messageHeaders.isEmpty()) {
throw new ProtocolException("Invalid sequence of headers (pseudo-headers must precede message headers)");
}
switch(name) {
case H2PseudoRequestHeaders.METHOD:
if (method != null) {
throw new ProtocolException("Multiple '%s' request headers are illegal", name);
}
method = value;
break;
case H2PseudoRequestHeaders.SCHEME:
if (scheme != null) {
throw new ProtocolException("Multiple '%s' request headers are illegal", name);
}
scheme = value;
break;
case H2PseudoRequestHeaders.PATH:
if (path != null) {
throw new ProtocolException("Multiple '%s' request headers are illegal", name);
}
path = value;
break;
case H2PseudoRequestHeaders.AUTHORITY:
authority = value;
break;
default:
throw new ProtocolException("Unsupported request header '%s'", name);
}
} else {
if (name.equalsIgnoreCase(HttpHeaders.CONNECTION) || name.equalsIgnoreCase(HttpHeaders.KEEP_ALIVE) || name.equalsIgnoreCase(HttpHeaders.PROXY_CONNECTION) || name.equalsIgnoreCase(HttpHeaders.TRANSFER_ENCODING) || name.equalsIgnoreCase(HttpHeaders.HOST) || name.equalsIgnoreCase(HttpHeaders.UPGRADE)) {
throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
}
if (name.equalsIgnoreCase(HttpHeaders.TE) && !value.equalsIgnoreCase("trailers")) {
throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
}
messageHeaders.add(header);
}
}
if (method == null) {
throw new ProtocolException("Mandatory request header '%s' not found", H2PseudoRequestHeaders.METHOD);
}
if (Method.CONNECT.isSame(method)) {
if (authority == null) {
throw new ProtocolException("Header '%s' is mandatory for CONNECT request", H2PseudoRequestHeaders.AUTHORITY);
}
if (scheme != null) {
throw new ProtocolException("Header '%s' must not be set for CONNECT request", H2PseudoRequestHeaders.SCHEME);
}
if (path != null) {
throw new ProtocolException("Header '%s' must not be set for CONNECT request", H2PseudoRequestHeaders.PATH);
}
} else {
if (scheme == null) {
throw new ProtocolException("Mandatory request header '%s' not found", H2PseudoRequestHeaders.SCHEME);
}
if (path == null) {
throw new ProtocolException("Mandatory request header '%s' not found", H2PseudoRequestHeaders.PATH);
}
}
final HttpRequest httpRequest = new BasicHttpRequest(method, path);
httpRequest.setVersion(HttpVersion.HTTP_2);
httpRequest.setScheme(scheme);
try {
httpRequest.setAuthority(URIAuthority.create(authority));
} catch (final URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
httpRequest.setPath(path);
for (int i = 0; i < messageHeaders.size(); i++) {
httpRequest.addHeader(messageHeaders.get(i));
}
return httpRequest;
}
use of org.apache.hc.core5.http.MessageHeaders in project httpcomponents-core by apache.
the class DefaultH2ResponseConverter method convert.
@Override
public HttpResponse convert(final List<Header> headers) throws HttpException {
String statusText = null;
final List<Header> messageHeaders = new ArrayList<>();
for (int i = 0; i < headers.size(); i++) {
final Header header = headers.get(i);
final String name = header.getName();
final String value = header.getValue();
for (int n = 0; n < name.length(); n++) {
final char ch = name.charAt(n);
if (Character.isAlphabetic(ch) && !Character.isLowerCase(ch)) {
throw new ProtocolException("Header name '%s' is invalid (header name contains uppercase characters)", name);
}
}
if (name.startsWith(":")) {
if (!messageHeaders.isEmpty()) {
throw new ProtocolException("Invalid sequence of headers (pseudo-headers must precede message headers)");
}
if (name.equals(H2PseudoResponseHeaders.STATUS)) {
if (statusText != null) {
throw new ProtocolException("Multiple '%s' response headers are illegal", name);
}
statusText = value;
} else {
throw new ProtocolException("Unsupported response header '%s'", name);
}
} else {
if (name.equalsIgnoreCase(HttpHeaders.CONNECTION) || name.equalsIgnoreCase(HttpHeaders.KEEP_ALIVE) || name.equalsIgnoreCase(HttpHeaders.TRANSFER_ENCODING) || name.equalsIgnoreCase(HttpHeaders.UPGRADE)) {
throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
}
messageHeaders.add(header);
}
}
if (statusText == null) {
throw new ProtocolException("Mandatory response header '%s' not found", H2PseudoResponseHeaders.STATUS);
}
final int statusCode;
try {
statusCode = Integer.parseInt(statusText);
} catch (final NumberFormatException ex) {
throw new ProtocolException("Invalid response status: " + statusText);
}
final HttpResponse response = new BasicHttpResponse(statusCode, null);
response.setVersion(HttpVersion.HTTP_2);
for (int i = 0; i < messageHeaders.size(); i++) {
response.addHeader(messageHeaders.get(i));
}
return response;
}
use of org.apache.hc.core5.http.MessageHeaders in project httpcomponents-core by apache.
the class IncomingEntityDetailsTest method getContentLength.
@Test
public void getContentLength() {
final MessageHeaders messageHeaders = new HeaderGroup();
final HeaderGroup headerGroup = new HeaderGroup();
final Header header = new BasicHeader("name", "value");
headerGroup.addHeader(header);
final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
assertAll(() -> assertEquals(-1, incomingEntityDetails.getContentLength()), () -> assertTrue(incomingEntityDetails.isChunked()));
}
use of org.apache.hc.core5.http.MessageHeaders in project httpcomponents-core by apache.
the class IncomingEntityDetailsTest method getTrailerNames.
@Test
public void getTrailerNames() {
final HeaderGroup messageHeaders = new HeaderGroup();
final Header header = new BasicHeader(HttpHeaders.TRAILER, "a, b, c, c");
messageHeaders.setHeaders(header);
final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
final Set<String> incomingSet = incomingEntityDetails.getTrailerNames();
assertAll(() -> assertFalse(incomingSet.isEmpty()), () -> assertTrue(incomingSet.containsAll(Stream.of("a", "b", "c").collect(Collectors.toCollection(HashSet::new)))));
}
use of org.apache.hc.core5.http.MessageHeaders in project httpcomponents-core by apache.
the class IncomingEntityDetailsTest method getContentLengthEmpty.
@Test
public void getContentLengthEmpty() {
final MessageHeaders messageHeaders = new HeaderGroup();
final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
assertAll(() -> assertEquals(-1, incomingEntityDetails.getContentLength()), () -> assertNull(incomingEntityDetails.getContentType()), () -> assertNull(incomingEntityDetails.getContentEncoding()), () -> assertEquals(incomingEntityDetails.getTrailerNames().size(), 0));
}
Aggregations