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; }
@Test public void testConvertFromFieldsBasic() throws Exception { 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("custom123", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); final HttpRequest request = converter.convert(headers); Assert.assertNotNull(request); Assert.assertEquals("GET", request.getMethod()); Assert.assertEquals("http", request.getScheme()); Assert.assertEquals(new URIAuthority("www.example.com"), request.getAuthority()); Assert.assertEquals("/", request.getPath()); final Header[] allHeaders = request.getHeaders(); Assert.assertEquals(1, allHeaders.length); Assert.assertEquals("custom123", allHeaders[0].getName()); Assert.assertEquals("value", allHeaders[0].getValue()); }
@Test public void testConvertFromFieldsUpperCaseHeaderName() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header name ':Path' is invalid (header name contains uppercase characters)"); 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); }
@Test public void testConvertFromFieldsConnectionHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header 'connection: keep-alive' is illegal for HTTP/2 messages"); 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("connection", "keep-alive")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsPseudoHeaderSequence() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Invalid sequence of headers (pseudo-headers must precede message headers)"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader("custom", "value"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsMissingMethod() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Mandatory request header ':method' not found"); final List<Header> headers = Arrays.<Header>asList( 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); }
@Test public void testConvertFromFieldsMissingScheme() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Mandatory request header ':scheme' not found"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsMissingPath() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Mandatory request header ':path' not found"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@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); }
@Test public void testConvertFromFieldsMultipleMethod() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Multiple ':method' request headers are illegal"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), 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); }
@Test public void testConvertFromFieldsMultipleScheme() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Multiple ':scheme' request headers are illegal"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":scheme", "https"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsMultiplePath() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Multiple ':path' request headers are illegal"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "https"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader(":path", "/"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsConnect() throws Exception { final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "CONNECT"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsConnectMissingAuthority() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header ':authority' is mandatory for CONNECT request"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "CONNECT"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsConnectPresentScheme() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header ':scheme' must not be set for CONNECT request"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "CONNECT"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsConnectPresentPath() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header ':path' must not be set for CONNECT request"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":method", "CONNECT"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("custom", "value")); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(headers); }
@Test public void testConvertFromMessageBasic() throws Exception { final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/"); request.addHeader("custom123", "Value"); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); final List<Header> headers = converter.convert(request); Assert.assertNotNull(headers); Assert.assertEquals(5, headers.size()); final Header header1 = headers.get(0); Assert.assertEquals(":method", header1.getName()); Assert.assertEquals("GET", header1.getValue()); final Header header2 = headers.get(1); Assert.assertEquals(":scheme", header2.getName()); Assert.assertEquals("http", header2.getValue()); final Header header3 = headers.get(2); Assert.assertEquals(":authority", header3.getName()); Assert.assertEquals("host", header3.getValue()); final Header header4 = headers.get(3); Assert.assertEquals(":path", header4.getName()); Assert.assertEquals("/", header4.getValue()); final Header header5 = headers.get(4); Assert.assertEquals("custom123", header5.getName()); Assert.assertEquals("Value", header5.getValue()); }
@Test public void testConvertFromMessageMissingScheme() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Request scheme is not set"); final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/"); request.addHeader("Custom123", "Value"); request.setScheme(null); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(request); }
@Test public void testConvertFromMessageMissingPath() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Request path is not set"); final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/"); request.addHeader("Custom123", "Value"); request.setPath(null); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(request); }
@Test public void testConvertFromMessageConnect() throws Exception { final HttpRequest request = new BasicHttpRequest("CONNECT", new HttpHost("host:80"), null); request.addHeader("custom123", "Value"); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); final List<Header> headers = converter.convert(request); Assert.assertNotNull(headers); Assert.assertEquals(3, headers.size()); final Header header1 = headers.get(0); Assert.assertEquals(":method", header1.getName()); Assert.assertEquals("CONNECT", header1.getValue()); final Header header2 = headers.get(1); Assert.assertEquals(":authority", header2.getName()); Assert.assertEquals("host:80", header2.getValue()); final Header header3 = headers.get(2); Assert.assertEquals("custom123", header3.getName()); Assert.assertEquals("Value", header3.getValue()); }
@Test public void testConvertFromMessageConnectMissingAuthority() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("CONNECT request authority is not set"); final HttpRequest request = new BasicHttpRequest("CONNECT", null, null); request.addHeader("Custom123", "Value"); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(request); }
@Test public void testConvertFromMessageConnectWithPath() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("CONNECT request path must be null"); final HttpRequest request = new BasicHttpRequest("CONNECT", "/"); request.setAuthority(new URIAuthority("host")); request.addHeader("Custom123", "Value"); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(request); }
@Test public void testConvertFromMessageConnectionHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header 'Connection: Keep-Alive' is illegal for HTTP/2 messages"); final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/"); request.addHeader("Connection", "Keep-Alive"); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(request); }
@Test public void testConvertFromMessageInvalidHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header name ':custom' is invalid"); final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/"); request.addHeader(":custom", "stuff"); final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); converter.convert(request); }