View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http2.impl;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HttpException;
36  import org.apache.hc.core5.http.HttpHeaders;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.HttpVersion;
39  import org.apache.hc.core5.http.ProtocolException;
40  import org.apache.hc.core5.http.message.BasicHeader;
41  import org.apache.hc.core5.http.message.BasicHttpResponse;
42  import org.apache.hc.core5.http2.H2MessageConverter;
43  import org.apache.hc.core5.http2.H2PseudoResponseHeaders;
44  import org.apache.hc.core5.util.TextUtils;
45  
46  /**
47   * HTTP/2 response converter.
48   *
49   * @since 5.0
50   */
51  public class DefaultH2ResponseConverter implements H2MessageConverter<HttpResponse> {
52  
53      public final static DefaultH2ResponseConverter INSTANCE = new DefaultH2ResponseConverter();
54  
55      @Override
56      public HttpResponse convert(final List<Header> headers) throws HttpException {
57          String statusText = null;
58          final List<Header> messageHeaders = new ArrayList<>();
59  
60          for (int i = 0; i < headers.size(); i++) {
61              final Header header = headers.get(i);
62              final String name = header.getName();
63              final String value = header.getValue();
64  
65              for (int n = 0; n < name.length(); n++) {
66                  final char ch = name.charAt(n);
67                  if (Character.isAlphabetic(ch) && !Character.isLowerCase(ch)) {
68                      throw new ProtocolException("Header name '%s' is invalid (header name contains uppercase characters)", name);
69                  }
70              }
71  
72              if (name.startsWith(":")) {
73                  if (!messageHeaders.isEmpty()) {
74                      throw new ProtocolException("Invalid sequence of headers (pseudo-headers must precede message headers)");
75                  }
76                  if (name.equals(H2PseudoResponseHeaders.STATUS)) {
77                      if (statusText != null) {
78                          throw new ProtocolException("Multiple '%s' response headers are illegal", name);
79                      }
80                      statusText = value;
81                  } else {
82                      throw new ProtocolException("Unsupported response header '%s'", name);
83                  }
84              } else {
85                  if (name.equalsIgnoreCase(HttpHeaders.CONNECTION) || name.equalsIgnoreCase(HttpHeaders.KEEP_ALIVE)
86                      || name.equalsIgnoreCase(HttpHeaders.TRANSFER_ENCODING) || name.equalsIgnoreCase(HttpHeaders.UPGRADE)) {
87                      throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
88                  }
89                  messageHeaders.add(header);
90              }
91  
92          }
93  
94          if (statusText == null) {
95              throw new ProtocolException("Mandatory response header '%s' not found", H2PseudoResponseHeaders.STATUS);
96          }
97          final int statusCode;
98          try {
99              statusCode = Integer.parseInt(statusText);
100         } catch (final NumberFormatException ex) {
101             throw new ProtocolException("Invalid response status: " + statusText);
102         }
103         final HttpResponse response = new BasicHttpResponse(statusCode, null);
104         response.setVersion(HttpVersion.HTTP_2);
105         for (int i = 0; i < messageHeaders.size(); i++) {
106             response.addHeader(messageHeaders.get(i));
107         }
108         return response;
109     }
110 
111     @Override
112     public List<Header> convert(final HttpResponse message) throws HttpException {
113         final int code = message.getCode();
114         if (code < 100 || code >= 600) {
115             throw new ProtocolException("Response status %s is invalid", code);
116         }
117         final List<Header> headers = new ArrayList<>();
118         headers.add(new BasicHeader(H2PseudoResponseHeaders.STATUS, Integer.toString(code), false));
119 
120         for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
121             final Header header = it.next();
122             final String name = header.getName();
123             final String value = header.getValue();
124             if (name.startsWith(":")) {
125                 throw new ProtocolException("Header name '%s' is invalid", name);
126             }
127             if (name.equalsIgnoreCase(HttpHeaders.CONNECTION) || name.equalsIgnoreCase(HttpHeaders.KEEP_ALIVE)
128                 || name.equalsIgnoreCase(HttpHeaders.TRANSFER_ENCODING) || name.equalsIgnoreCase(HttpHeaders.UPGRADE)) {
129                 throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
130             }
131             headers.add(new BasicHeader(TextUtils.toLowerCase(name), value));
132         }
133         return headers;
134     }
135 
136 }