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  import java.util.Locale;
34  
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpResponse;
39  import org.apache.hc.core5.http.HttpVersion;
40  import org.apache.hc.core5.http.ProtocolException;
41  import org.apache.hc.core5.http.message.BasicHeader;
42  import org.apache.hc.core5.http.message.BasicHttpResponse;
43  import org.apache.hc.core5.http2.H2MessageConverter;
44  import org.apache.hc.core5.http2.H2PseudoResponseHeaders;
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 DefaultH2ResponseConverteronseConverter.html#DefaultH2ResponseConverter">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)) {
86                      throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue());
87                  }
88                  messageHeaders.add(header);
89              }
90  
91          }
92  
93          if (statusText == null) {
94              throw new ProtocolException("Mandatory response header '%s' not found", H2PseudoResponseHeaders.STATUS);
95          }
96          final int statusCode;
97          try {
98              statusCode = Integer.parseInt(statusText);
99          } catch (final NumberFormatException ex) {
100             throw new ProtocolException("Invalid response status: " + statusText);
101         }
102         final HttpResponse response = new BasicHttpResponse(statusCode, null);
103         response.setVersion(HttpVersion.HTTP_2);
104         for (int i = 0; i < messageHeaders.size(); i++) {
105             response.addHeader(messageHeaders.get(i));
106         }
107         return response;
108     }
109 
110     @Override
111     public List<Header> convert(final HttpResponse message) throws HttpException {
112         final int code = message.getCode();
113         if (code < 100 || code >= 600) {
114             throw new ProtocolException("Response status %s is invalid", code);
115         }
116         final List<Header> headers = new ArrayList<>();
117         headers.add(new BasicHeader(H2PseudoResponseHeaders.STATUS, Integer.toString(code), false));
118 
119         for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
120             final Header header = it.next();
121             final String name = header.getName();
122             final String value = header.getValue();
123             if (name.startsWith(":")) {
124                 throw new ProtocolException("Header name '%s' is invalid", name);
125             }
126             if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) {
127                 throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", name, value);
128             }
129             headers.add(new BasicHeader(name.toLowerCase(Locale.ROOT), value));
130         }
131         return headers;
132     }
133 
134 }