public HttpRequest convert()

in httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java [59:148]


    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 {
                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);
            }
            validatePathPseudoHeader(method, scheme, 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;
    }