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.nio;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  
33  import org.apache.hc.core5.annotation.Internal;
34  import org.apache.hc.core5.concurrent.FutureCallback;
35  import org.apache.hc.core5.http.ConnectionClosedException;
36  import org.apache.hc.core5.http.HttpVersion;
37  import org.apache.hc.core5.http.ProtocolVersion;
38  import org.apache.hc.core5.http.impl.nio.BufferedData;
39  import org.apache.hc.core5.reactor.IOSession;
40  import org.apache.hc.core5.reactor.ProtocolIOSession;
41  import org.apache.hc.core5.util.Args;
42  
43  /**
44   * I/O event handler for events fired by {@link ProtocolIOSession} that implements
45   * server side of the HTTP/2 protocol negotiation handshake.
46   *
47   * @since 5.1
48   */
49  @Internal
50  public class H2OnlyServerHttpProtocolNegotiator extends ProtocolNegotiatorBase {
51  
52      final static byte[] PREFACE = ClientHttpProtocolNegotiator.PREFACE;
53  
54      private final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory;
55      private final BufferedData inBuf;
56  
57      public H2OnlyServerHttpProtocolNegotiator(
58              final ProtocolIOSession ioSession,
59              final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory) {
60          this(ioSession, http2StreamHandlerFactory, null);
61      }
62  
63      public H2OnlyServerHttpProtocolNegotiator(
64              final ProtocolIOSession ioSession,
65              final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory,
66              final FutureCallback<ProtocolIOSession> resultCallback) {
67          super(ioSession, resultCallback);
68          this.http2StreamHandlerFactory = Args.notNull(http2StreamHandlerFactory, "HTTP/2 stream handler factory");
69          this.inBuf = BufferedData.allocate(1024);
70      }
71  
72      @Override
73      public void connected(final IOSession session) throws IOException {
74      }
75  
76      @Override
77      public void inputReady(final IOSession session, final ByteBuffer src) throws IOException {
78          if (src != null) {
79              inBuf.put(src);
80          }
81          boolean endOfStream = false;
82          if (inBuf.length() < PREFACE.length) {
83              final int bytesRead = inBuf.readFrom(session);
84              if (bytesRead == -1) {
85                  endOfStream = true;
86              }
87          }
88          final ByteBuffer data = inBuf.data();
89          if (data.remaining() >= PREFACE.length) {
90              for (int i = 0; i < PREFACE.length; i++) {
91                  if (data.get() != PREFACE[i]) {
92                      throw new ProtocolNegotiationException("Unexpected HTTP/2 preface");
93                  }
94              }
95              startProtocol(HttpVersion.HTTP_2, new ServerH2IOEventHandler(http2StreamHandlerFactory.create(ioSession)), data.hasRemaining() ? data : null);
96          } else {
97              if (endOfStream) {
98                  throw new ConnectionClosedException();
99              }
100         }
101     }
102 
103     @Override
104     public void outputReady(final IOSession session) throws IOException {
105     }
106 
107     @Override
108     public ProtocolVersion getProtocolVersion() {
109         final ProtocolVersion protocolVersion = super.getProtocolVersion();
110         return protocolVersion != null ? protocolVersion : HttpVersion.HTTP_2;
111     }
112 
113     @Override
114     public String toString() {
115         return getClass().getName();
116     }
117 
118 }