View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http2.server;
20  
21  import org.eclipse.jetty.http.BadMessageException;
22  import org.eclipse.jetty.http.HttpFields;
23  import org.eclipse.jetty.http.MetaData.Request;
24  import org.eclipse.jetty.io.Connection;
25  import org.eclipse.jetty.io.EndPoint;
26  import org.eclipse.jetty.server.ConnectionFactory;
27  import org.eclipse.jetty.server.Connector;
28  import org.eclipse.jetty.server.HttpConfiguration;
29  import org.eclipse.jetty.server.HttpConnectionFactory;
30  import org.eclipse.jetty.util.annotation.Name;
31  import org.eclipse.jetty.util.log.Log;
32  import org.eclipse.jetty.util.log.Logger;
33  
34  
35  /* ------------------------------------------------------------ */
36  /** HTTP2 Clear Text Connection factory.
37   * <p>This extension of HTTP2ServerConnection Factory sets the
38   * protocol name to "h2c" as used by the clear text upgrade mechanism
39   * for HTTP2 and marks all TLS ciphers as unacceptable.
40   * </p>
41   * <p>If used in combination with a {@link HttpConnectionFactory} as the
42   * default protocol, this factory can support the non-standard direct
43   * update mechanism, where a HTTP1 request of the form "PRI * HTTP/2.0"
44   * is used to trigger a switch to a HTTP2 connection.    This approach
45   * allows a single port to accept either HTTP/1 or HTTP/2 direct
46   * connections.
47   */
48  public class HTTP2CServerConnectionFactory extends HTTP2ServerConnectionFactory implements ConnectionFactory.Upgrading
49  {
50      private static final Logger LOG = Log.getLogger(HTTP2CServerConnectionFactory.class);
51  
52      public HTTP2CServerConnectionFactory(@Name("config") HttpConfiguration httpConfiguration)
53      {
54          super(httpConfiguration,"h2c","h2c-17","h2c-16","h2c-15","h2c-14");
55      }
56  
57      @Override
58      public boolean isAcceptable(String protocol, String tlsProtocol, String tlsCipher)
59      {
60          // Never use TLS with h2c
61          return false;
62      }
63  
64      @Override
65      public Connection upgradeConnection(Connector connector, EndPoint endPoint, Request request, HttpFields response101) throws BadMessageException
66      {
67          if (LOG.isDebugEnabled())
68              LOG.debug("{} upgraded {}{}", this, request.toString(), request.getFields());
69  
70          if (request.getContentLength() > 0)
71              return null;
72  
73          HTTP2ServerConnection connection = (HTTP2ServerConnection)newConnection(connector, endPoint);
74          if (connection.upgrade(request))
75              return connection;
76          return null;
77      }
78  }