@Test public void testConvertFromFieldsInvalidStatusCode() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Invalid response status: boom"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":status", "boom"), new BasicHeader("location", "http: new BasicHeader("custom123", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }
@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)) { 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; }
DefaultH2ResponseConverter implements H2MessageConverter<HttpResponse> { @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)) { 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; } }
DefaultH2ResponseConverter implements H2MessageConverter<HttpResponse> { @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)) { 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; }  }
DefaultH2ResponseConverter implements H2MessageConverter<HttpResponse> { @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)) { 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; }  @Override HttpResponse convert(final List<Header> headers); @Override List<Header> convert(final HttpResponse message); }
DefaultH2ResponseConverter implements H2MessageConverter<HttpResponse> { @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)) { 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; }  @Override HttpResponse convert(final List<Header> headers); @Override List<Header> convert(final HttpResponse message); final static DefaultH2ResponseConverter INSTANCE; }