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  package org.apache.hc.core5.http2.impl.nio;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.util.List;
32  
33  import org.apache.hc.core5.annotation.Internal;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HttpException;
36  import org.apache.hc.core5.http.RequestHeaderFieldsTooLargeException;
37  import org.apache.hc.core5.http.config.CharCodingConfig;
38  import org.apache.hc.core5.http.impl.BasicHttpConnectionMetrics;
39  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
40  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
41  import org.apache.hc.core5.http.nio.HandlerFactory;
42  import org.apache.hc.core5.http.nio.command.ExecutableCommand;
43  import org.apache.hc.core5.http.protocol.HttpCoreContext;
44  import org.apache.hc.core5.http.protocol.HttpProcessor;
45  import org.apache.hc.core5.http2.H2ConnectionException;
46  import org.apache.hc.core5.http2.H2Error;
47  import org.apache.hc.core5.http2.config.H2Config;
48  import org.apache.hc.core5.http2.frame.DefaultFrameFactory;
49  import org.apache.hc.core5.http2.frame.FrameFactory;
50  import org.apache.hc.core5.http2.frame.StreamIdGenerator;
51  import org.apache.hc.core5.http2.hpack.HeaderListConstraintException;
52  import org.apache.hc.core5.reactor.ProtocolIOSession;
53  import org.apache.hc.core5.util.Args;
54  
55  /**
56   * I/O event handler for events fired by {@link ProtocolIOSession} that implements
57   * server side HTTP/2 messaging protocol with full support for
58   * multiplexed message transmission.
59   *
60   * @since 5.0
61   */
62  @Internal
63  public class ServerH2StreamMultiplexer extends AbstractH2StreamMultiplexer {
64  
65      private final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory;
66  
67      public ServerH2StreamMultiplexer(
68              final ProtocolIOSession ioSession,
69              final FrameFactory frameFactory,
70              final HttpProcessor httpProcessor,
71              final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
72              final CharCodingConfig charCodingConfig,
73              final H2Config h2Config,
74              final H2StreamListener streamListener) {
75          super(ioSession, frameFactory, StreamIdGenerator.EVEN, httpProcessor, charCodingConfig, h2Config, streamListener);
76          this.exchangeHandlerFactory = Args.notNull(exchangeHandlerFactory, "Handler factory");
77      }
78  
79      public ServerH2StreamMultiplexer(
80              final ProtocolIOSession ioSession,
81              final HttpProcessor httpProcessor,
82              final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory,
83              final CharCodingConfig charCodingConfig,
84              final H2Config h2Config) {
85          this(ioSession, DefaultFrameFactory.INSTANCE, httpProcessor, exchangeHandlerFactory, charCodingConfig, h2Config, null);
86      }
87  
88      @Override
89      void acceptHeaderFrame() throws H2ConnectionException {
90      }
91  
92      @Override
93      void acceptPushRequest() throws H2ConnectionException {
94      }
95  
96      @Override
97      void acceptPushFrame() throws H2ConnectionException {
98          throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Push not supported");
99      }
100 
101     @Override
102     H2StreamHandler createRemotelyInitiatedStream(
103             final H2StreamChannel channel,
104             final HttpProcessor httpProcessor,
105             final BasicHttpConnectionMetrics connMetrics,
106             final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
107         final HttpCoreContext context = HttpCoreContext.create();
108         context.setAttribute(HttpCoreContext.SSL_SESSION, getSSLSession());
109         context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, getEndpointDetails());
110         return new ServerH2StreamHandler(channel, httpProcessor, connMetrics, exchangeHandlerFactory, context);
111     }
112 
113     @Override
114     H2StreamHandler createLocallyInitiatedStream(
115             final ExecutableCommand command,
116             final H2StreamChannel channel,
117             final HttpProcessor httpProcessor,
118             final BasicHttpConnectionMetrics connMetrics) throws IOException {
119         throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Illegal attempt to execute a request");
120     }
121 
122     @Override
123     List<Header> decodeHeaders(final ByteBuffer payload) throws HttpException {
124         try {
125             return super.decodeHeaders(payload);
126         } catch (final HeaderListConstraintException ex) {
127             throw new RequestHeaderFieldsTooLargeException(ex.getMessage(), ex);
128         }
129     }
130 
131     @Override
132     public String toString() {
133         final StringBuilder buf = new StringBuilder();
134         buf.append("[");
135         appendState(buf);
136         buf.append("]");
137         return buf.toString();
138     }
139 
140 }