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