1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
60
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 }