Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpServer.java
41159 views
1
/*
2
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.net.httpserver;
27
28
import com.sun.net.httpserver.spi.HttpServerProvider;
29
30
import java.io.IOException;
31
import java.net.BindException;
32
import java.net.InetSocketAddress;
33
import java.util.concurrent.Executor;
34
35
/**
36
* This class implements a simple HTTP server. A {@code HttpServer} is bound to an IP address
37
* and port number and listens for incoming TCP connections from clients on this address.
38
* The sub-class {@link HttpsServer} implements a server which handles HTTPS requests.
39
*
40
* <p>One or more {@link HttpHandler} objects must be associated with a server
41
* in order to process requests. Each such {@code HttpHandler} is registered with
42
* a root URI path which represents the location of the application or service
43
* on this server. The mapping of a handler to a {@code HttpServer} is
44
* encapsulated by a {@link HttpContext} object. HttpContexts are created by
45
* calling {@link #createContext(String,HttpHandler)}.
46
* Any request for which no handler can be found is rejected with a 404 response.
47
* Management of threads can be done external to this object by providing a
48
* {@link java.util.concurrent.Executor} object. If none is provided a default
49
* implementation is used.
50
*
51
* <p> <a id="mapping_description"></a> <b>Mapping request URIs to HttpContext paths</b>
52
*
53
* <p>When a HTTP request is received, the appropriate {@code HttpContext}
54
* (and handler) is located by finding the context whose path is the longest
55
* matching prefix of the request URI's path. Paths are matched literally,
56
* which means that the strings are compared case sensitively, and with no
57
* conversion to or from any encoded forms. For example, given a {@code HttpServer}
58
* with the following HttpContexts configured:
59
*
60
* <table class="striped"><caption style="display:none">description</caption>
61
* <thead>
62
* <tr>
63
* <th scope="col"><i>Context</i></th>
64
* <th scope="col"><i>Context path</i></th>
65
* </tr>
66
* </thead>
67
* <tbody>
68
* <tr><th scope="row">ctx1</th><td>"/"</td></tr>
69
* <tr><th scope="row">ctx2</th><td>"/apps/"</td></tr>
70
* <tr><th scope="row">ctx3</th><td>"/apps/foo/"</td></tr>
71
* </tbody>
72
* </table>
73
*
74
* <p>The following table shows some request URIs and which, if any context they would
75
* match with:
76
* <table class="striped"><caption style="display:none">description</caption>
77
* <thead>
78
* <tr>
79
* <th scope="col"><i>Request URI</i></th>
80
* <th scope="col"><i>Matches context</i></th>
81
* </tr>
82
* </thead>
83
* <tbody>
84
* <tr><th scope="row">"http://foo.com/apps/foo/bar"</th><td>ctx3</td></tr>
85
* <tr><th scope="row">"http://foo.com/apps/Foo/bar"</th><td>no match, wrong case</td></tr>
86
* <tr><th scope="row">"http://foo.com/apps/app1"</th><td>ctx2</td></tr>
87
* <tr><th scope="row">"http://foo.com/foo"</th><td>ctx1</td></tr>
88
* </tbody>
89
* </table>
90
*
91
* <p><b>Note about socket backlogs</b>
92
*
93
* <p>When binding to an address and port number, the application can also
94
* specify an integer <i>backlog</i> parameter. This represents the maximum
95
* number of incoming TCP connections which the system will queue internally.
96
* Connections are queued while they are waiting to be accepted by the
97
* {@code HttpServer}. When the limit is reached, further connections may be
98
* rejected (or possibly ignored) by the underlying TCP implementation. Setting
99
* the right backlog value is a compromise between efficient resource usage in
100
* the TCP layer (not setting it too high) and allowing adequate throughput of
101
* incoming requests (not setting it too low).
102
*
103
* @since 1.6
104
*/
105
106
public abstract class HttpServer {
107
108
/**
109
* Constructor for subclasses to call.
110
*/
111
protected HttpServer() {
112
}
113
114
/**
115
* Creates a {@code HttpServer} instance which is initially not bound to any
116
* local address/port. The {@code HttpServer} is acquired from the currently
117
* installed {@link HttpServerProvider}. The server must be bound using
118
* {@link #bind(InetSocketAddress,int)} before it can be used.
119
*
120
* @throws IOException if an I/O error occurs
121
* @return an instance of {@code HttpServer}
122
*/
123
public static HttpServer create() throws IOException {
124
return create (null, 0);
125
}
126
127
/**
128
* Create a {@code HttpServer} instance which will bind to the
129
* specified {@link java.net.InetSocketAddress} (IP address and port number).
130
*
131
* A maximum backlog can also be specified. This is the maximum number of
132
* queued incoming connections to allow on the listening socket.
133
* Queued TCP connections exceeding this limit may be rejected by the TCP
134
* implementation. The {@code HttpServer} is acquired from the currently
135
* installed {@link HttpServerProvider}
136
*
137
* @param addr the address to listen on, if {@code null} then
138
* {@link #bind(InetSocketAddress, int)} must be called to set
139
* the address
140
* @param backlog the socket backlog. If this value is less than or equal to zero,
141
* then a system default value is used
142
* @throws IOException if an I/O error occurs
143
* @throws BindException if the server cannot bind to the requested address,
144
* or if the server is already bound
145
* @return an instance of {@code HttpServer}
146
*/
147
148
public static HttpServer create(InetSocketAddress addr, int backlog) throws IOException {
149
HttpServerProvider provider = HttpServerProvider.provider();
150
return provider.createHttpServer (addr, backlog);
151
}
152
153
/**
154
* Binds a currently unbound {@code HttpServer} to the given address and
155
* port number. A maximum backlog can also be specified. This is the maximum
156
* number of queued incoming connections to allow on the listening socket.
157
* Queued TCP connections exceeding this limit may be rejected by the TCP
158
* implementation.
159
*
160
* @param addr the address to listen on
161
* @param backlog the socket backlog. If this value is less than or equal to
162
* zero, then a system default value is used
163
* @throws BindException if the server cannot bind to the requested address
164
* or if the server is already bound
165
* @throws NullPointerException if addr is {@code null}
166
*/
167
public abstract void bind(InetSocketAddress addr, int backlog) throws IOException;
168
169
/**
170
* Starts this server in a new background thread. The background thread
171
* inherits the priority, thread group and context class loader
172
* of the caller.
173
*/
174
public abstract void start();
175
176
/**
177
* Sets this server's {@link java.util.concurrent.Executor} object. An
178
* {@code Executor} must be established before {@link #start()} is called.
179
* All HTTP requests are handled in tasks given to the executor.
180
* If this method is not called (before {@link #start()}) or if it is called
181
* with a {@code null Executor}, then a default implementation is used,
182
* which uses the thread which was created by the {@link #start()} method.
183
*
184
* @param executor the {@code Executor} to set, or {@code null} for default
185
* implementation
186
* @throws IllegalStateException if the server is already started
187
*/
188
public abstract void setExecutor(Executor executor);
189
190
191
/**
192
* Returns this server's {@code Executor} object if one was specified with
193
* {@link #setExecutor(Executor)}, or {@code null} if none was specified.
194
*
195
* @return the {@code Executor} established for this server or {@code null} if not set.
196
*/
197
public abstract Executor getExecutor() ;
198
199
/**
200
* Stops this server by closing the listening socket and disallowing
201
* any new exchanges from being processed. The method will then block
202
* until all current exchange handlers have completed or else when
203
* approximately <i>delay</i> seconds have elapsed (whichever happens
204
* sooner). Then, all open TCP connections are closed, the background
205
* thread created by {@link #start()} exits, and the method returns.
206
* Once stopped, a {@code HttpServer} cannot be re-used.
207
*
208
* @param delay the maximum time in seconds to wait until exchanges have finished
209
* @throws IllegalArgumentException if delay is less than zero
210
*/
211
public abstract void stop(int delay);
212
213
/**
214
* Creates a {@code HttpContext}. A {@code HttpContext} represents a mapping
215
* from a URI path to a exchange handler on this {@code HttpServer}. Once
216
* created, all requests received by the server for the path will be handled
217
* by calling the given handler object. The context is identified by the
218
* path, and can later be removed from the server using this with the
219
* {@link #removeContext(String)} method.
220
*
221
* <p> The path specifies the root URI path for this context. The first
222
* character of path must be '/'.
223
*
224
* <p>The class overview describes how incoming request URIs are
225
* <a href="#mapping_description">mapped</a> to HttpContext instances.
226
*
227
* @apiNote The path should generally, but is not required to, end with '/'.
228
* If the path does not end with '/', eg such as with {@code "/foo"} then
229
* this would match requests with a path of {@code "/foobar"} or
230
* {@code "/foo/bar"}.
231
*
232
* @param path the root URI path to associate the context with
233
* @param handler the handler to invoke for incoming requests
234
* @throws IllegalArgumentException if path is invalid, or if a context
235
* already exists for this path
236
* @throws NullPointerException if either path, or handler are {@code null}
237
* @return an instance of {@code HttpContext}
238
*/
239
public abstract HttpContext createContext(String path, HttpHandler handler);
240
241
/**
242
* Creates a HttpContext without initially specifying a handler. The handler
243
* must later be specified using {@link HttpContext#setHandler(HttpHandler)}.
244
* A {@code HttpContext} represents a mapping from a URI path to an exchange
245
* handler on this {@code HttpServer}. Once created, and when the handler has
246
* been set, all requests received by the server for the path will be handled
247
* by calling the handler object. The context is identified by the path, and
248
* can later be removed from the server using this with the
249
* {@link #removeContext(String)} method.
250
*
251
* <p>The path specifies the root URI path for this context. The first character of path must be
252
* '/'.
253
*
254
* <p>The class overview describes how incoming request URIs are
255
* <a href="#mapping_description">mapped</a> to {@code HttpContext} instances.
256
*
257
* @apiNote The path should generally, but is not required to, end with '/'.
258
* If the path does not end with '/', eg such as with {@code "/foo"} then
259
* this would match requests with a path of {@code "/foobar"} or
260
* {@code "/foo/bar"}.
261
*
262
* @param path the root URI path to associate the context with
263
* @throws IllegalArgumentException if path is invalid, or if a context
264
* already exists for this path
265
* @throws NullPointerException if path is {@code null}
266
* @return an instance of {@code HttpContext}
267
*/
268
public abstract HttpContext createContext(String path);
269
270
/**
271
* Removes the context identified by the given path from the server.
272
* Removing a context does not affect exchanges currently being processed
273
* but prevents new ones from being accepted.
274
*
275
* @param path the path of the handler to remove
276
* @throws IllegalArgumentException if no handler corresponding to this
277
* path exists.
278
* @throws NullPointerException if path is {@code null}
279
*/
280
public abstract void removeContext(String path) throws IllegalArgumentException;
281
282
/**
283
* Removes the given context from the server.
284
* Removing a context does not affect exchanges currently being processed
285
* but prevents new ones from being accepted.
286
*
287
* @param context the context to remove
288
* @throws NullPointerException if context is {@code null}
289
*/
290
public abstract void removeContext(HttpContext context);
291
292
/**
293
* Returns the address this server is listening on
294
*
295
* @return the {@code InetSocketAddress} the server is listening on
296
*/
297
public abstract InetSocketAddress getAddress();
298
}
299
300