65 private static ExecutorService getDefaultExecutor() { |
65 private static ExecutorService getDefaultExecutor() { |
66 return Executors.newCachedThreadPool(defaultThreadFac); |
66 return Executors.newCachedThreadPool(defaultThreadFac); |
67 } |
67 } |
68 |
68 |
69 public Http2TestServer(String serverName, boolean secure, int port) throws Exception { |
69 public Http2TestServer(String serverName, boolean secure, int port) throws Exception { |
70 this(serverName, secure, port, getDefaultExecutor(), null); |
70 this(serverName, secure, port, getDefaultExecutor(), 50, null); |
71 } |
71 } |
72 |
72 |
73 public Http2TestServer(boolean secure, int port) throws Exception { |
73 public Http2TestServer(boolean secure, int port) throws Exception { |
74 this(null, secure, port, getDefaultExecutor(), null); |
74 this(null, secure, port, getDefaultExecutor(), 50, null); |
75 } |
75 } |
76 |
76 |
77 public InetSocketAddress getAddress() { |
77 public InetSocketAddress getAddress() { |
78 return (InetSocketAddress)server.getLocalSocketAddress(); |
78 return (InetSocketAddress)server.getLocalSocketAddress(); |
79 } |
79 } |
83 + getAddress().getPort(); |
83 + getAddress().getPort(); |
84 } |
84 } |
85 |
85 |
86 public Http2TestServer(boolean secure, |
86 public Http2TestServer(boolean secure, |
87 SSLContext context) throws Exception { |
87 SSLContext context) throws Exception { |
88 this(null, secure, 0, null, context); |
88 this(null, secure, 0, null, 50, context); |
89 } |
89 } |
90 |
90 |
91 public Http2TestServer(String serverName, boolean secure, |
91 public Http2TestServer(String serverName, boolean secure, |
92 SSLContext context) throws Exception { |
92 SSLContext context) throws Exception { |
93 this(serverName, secure, 0, null, context); |
93 this(serverName, secure, 0, null, 50, context); |
94 } |
94 } |
95 |
95 |
96 public Http2TestServer(boolean secure, |
96 public Http2TestServer(boolean secure, |
97 int port, |
97 int port, |
98 ExecutorService exec, |
98 ExecutorService exec, |
99 SSLContext context) throws Exception { |
99 SSLContext context) throws Exception { |
100 this(null, secure, port, exec, context); |
100 this(null, secure, port, exec, 50, context); |
|
101 } |
|
102 |
|
103 public Http2TestServer(String serverName, |
|
104 boolean secure, |
|
105 int port, |
|
106 ExecutorService exec, |
|
107 SSLContext context) |
|
108 throws Exception |
|
109 { |
|
110 this(serverName, secure, port, exec, 50, context); |
101 } |
111 } |
102 |
112 |
103 /** |
113 /** |
104 * Create a Http2Server listening on the given port. Currently needs |
114 * Create a Http2Server listening on the given port. Currently needs |
105 * to know in advance whether incoming connections are plain TCP "h2c" |
115 * to know in advance whether incoming connections are plain TCP "h2c" |
107 * |
117 * |
108 * @param serverName SNI servername |
118 * @param serverName SNI servername |
109 * @param secure https or http |
119 * @param secure https or http |
110 * @param port listen port |
120 * @param port listen port |
111 * @param exec executor service (cached thread pool is used if null) |
121 * @param exec executor service (cached thread pool is used if null) |
|
122 * @param backlog the server socket backlog |
112 * @param context the SSLContext used when secure is true |
123 * @param context the SSLContext used when secure is true |
113 */ |
124 */ |
114 public Http2TestServer(String serverName, |
125 public Http2TestServer(String serverName, |
115 boolean secure, |
126 boolean secure, |
116 int port, |
127 int port, |
117 ExecutorService exec, |
128 ExecutorService exec, |
|
129 int backlog, |
118 SSLContext context) |
130 SSLContext context) |
119 throws Exception |
131 throws Exception |
120 { |
132 { |
121 this.serverName = serverName; |
133 this.serverName = serverName; |
122 if (secure) { |
134 if (secure) { |
123 server = initSecure(port); |
135 server = initSecure(port, backlog); |
124 } else { |
136 } else { |
125 server = initPlaintext(port); |
137 server = initPlaintext(port, backlog); |
126 } |
138 } |
127 this.secure = secure; |
139 this.secure = secure; |
128 this.exec = exec == null ? getDefaultExecutor() : exec; |
140 this.exec = exec == null ? getDefaultExecutor() : exec; |
129 this.handlers = Collections.synchronizedMap(new HashMap<>()); |
141 this.handlers = Collections.synchronizedMap(new HashMap<>()); |
130 this.sslContext = context; |
142 this.sslContext = context; |
169 throw new RuntimeException("No handler found for path " + path); |
181 throw new RuntimeException("No handler found for path " + path); |
170 System.err.println("Using handler for: " + bestMatch.get()); |
182 System.err.println("Using handler for: " + bestMatch.get()); |
171 return handler; |
183 return handler; |
172 } |
184 } |
173 |
185 |
174 final ServerSocket initPlaintext(int port) throws Exception { |
186 final ServerSocket initPlaintext(int port, int backlog) throws Exception { |
175 ServerSocket ss = new ServerSocket(); |
187 ServerSocket ss = new ServerSocket(); |
176 ss.setReuseAddress(false); |
188 ss.setReuseAddress(false); |
177 ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
189 ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog); |
178 return ss; |
190 return ss; |
179 } |
191 } |
180 |
192 |
181 public synchronized void stop() { |
193 public synchronized void stop() { |
182 // TODO: clean shutdown GoAway |
194 // TODO: clean shutdown GoAway |
190 } catch (IOException e) {} |
202 } catch (IOException e) {} |
191 exec.shutdownNow(); |
203 exec.shutdownNow(); |
192 } |
204 } |
193 |
205 |
194 |
206 |
195 final ServerSocket initSecure(int port) throws Exception { |
207 final ServerSocket initSecure(int port, int backlog) throws Exception { |
196 ServerSocketFactory fac; |
208 ServerSocketFactory fac; |
197 if (sslContext != null) { |
209 if (sslContext != null) { |
198 fac = sslContext.getServerSocketFactory(); |
210 fac = sslContext.getServerSocketFactory(); |
199 } else { |
211 } else { |
200 fac = SSLServerSocketFactory.getDefault(); |
212 fac = SSLServerSocketFactory.getDefault(); |
201 } |
213 } |
202 SSLServerSocket se = (SSLServerSocket) fac.createServerSocket(); |
214 SSLServerSocket se = (SSLServerSocket) fac.createServerSocket(); |
203 se.setReuseAddress(false); |
215 se.setReuseAddress(false); |
204 se.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
216 se.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog); |
205 SSLParameters sslp = se.getSSLParameters(); |
217 SSLParameters sslp = se.getSSLParameters(); |
206 sslp.setApplicationProtocols(new String[]{"h2"}); |
218 sslp.setApplicationProtocols(new String[]{"h2"}); |
207 sslp.setEndpointIdentificationAlgorithm("HTTPS"); |
219 sslp.setEndpointIdentificationAlgorithm("HTTPS"); |
208 se.setSSLParameters(sslp); |
220 se.setSSLParameters(sslp); |
209 se.setEnabledCipherSuites(se.getSupportedCipherSuites()); |
221 se.setEnabledCipherSuites(se.getSupportedCipherSuites()); |