@Test public void testConvertFromFieldsUnknownPseudoHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Unsupported request header ':custom'"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader(":custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@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)"); } if (name.equals(H2PseudoRequestHeaders.METHOD)) { if (method != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } method = value; } else if (name.equals(H2PseudoRequestHeaders.SCHEME)) { if (scheme != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } scheme = value; } else if (name.equals(H2PseudoRequestHeaders.PATH)) { if (path != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } path = value; } else if (name.equals(H2PseudoRequestHeaders.AUTHORITY)) { authority = value; } else { throw new ProtocolException("Unsupported request header '%s'", name); } } else { if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) { 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; }
DefaultH2RequestConverter implements H2MessageConverter<HttpRequest> { @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)"); } if (name.equals(H2PseudoRequestHeaders.METHOD)) { if (method != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } method = value; } else if (name.equals(H2PseudoRequestHeaders.SCHEME)) { if (scheme != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } scheme = value; } else if (name.equals(H2PseudoRequestHeaders.PATH)) { if (path != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } path = value; } else if (name.equals(H2PseudoRequestHeaders.AUTHORITY)) { authority = value; } else { throw new ProtocolException("Unsupported request header '%s'", name); } } else { if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) { 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; } }
DefaultH2RequestConverter implements H2MessageConverter<HttpRequest> { @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)"); } if (name.equals(H2PseudoRequestHeaders.METHOD)) { if (method != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } method = value; } else if (name.equals(H2PseudoRequestHeaders.SCHEME)) { if (scheme != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } scheme = value; } else if (name.equals(H2PseudoRequestHeaders.PATH)) { if (path != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } path = value; } else if (name.equals(H2PseudoRequestHeaders.AUTHORITY)) { authority = value; } else { throw new ProtocolException("Unsupported request header '%s'", name); } } else { if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) { 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; }  }
DefaultH2RequestConverter implements H2MessageConverter<HttpRequest> { @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)"); } if (name.equals(H2PseudoRequestHeaders.METHOD)) { if (method != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } method = value; } else if (name.equals(H2PseudoRequestHeaders.SCHEME)) { if (scheme != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } scheme = value; } else if (name.equals(H2PseudoRequestHeaders.PATH)) { if (path != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } path = value; } else if (name.equals(H2PseudoRequestHeaders.AUTHORITY)) { authority = value; } else { throw new ProtocolException("Unsupported request header '%s'", name); } } else { if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) { 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; }  @Override HttpRequest convert(final List<Header> headers); @Override List<Header> convert(final HttpRequest message); }
DefaultH2RequestConverter implements H2MessageConverter<HttpRequest> { @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)"); } if (name.equals(H2PseudoRequestHeaders.METHOD)) { if (method != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } method = value; } else if (name.equals(H2PseudoRequestHeaders.SCHEME)) { if (scheme != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } scheme = value; } else if (name.equals(H2PseudoRequestHeaders.PATH)) { if (path != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } path = value; } else if (name.equals(H2PseudoRequestHeaders.AUTHORITY)) { authority = value; } else { throw new ProtocolException("Unsupported request header '%s'", name); } } else { if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) { 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; }  @Override HttpRequest convert(final List<Header> headers); @Override List<Header> convert(final HttpRequest message); final static DefaultH2RequestConverter INSTANCE; }