Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/net/httpserver/HttpServer.java
38922 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.net.httpserver;2627import java.net.*;28import java.io.*;29import java.nio.*;30import java.security.*;31import java.nio.channels.*;32import java.util.*;33import java.util.concurrent.*;34import javax.net.ssl.*;35import com.sun.net.httpserver.spi.HttpServerProvider;3637/**38* This class implements a simple HTTP server. A HttpServer is bound to an IP address39* and port number and listens for incoming TCP connections from clients on this address.40* The sub-class {@link HttpsServer} implements a server which handles HTTPS requests.41* <p>42* One or more {@link HttpHandler} objects must be associated with a server43* in order to process requests. Each such HttpHandler is registered44* with a root URI path which represents the45* location of the application or service on this server. The mapping of a handler46* to a HttpServer is encapsulated by a {@link HttpContext} object. HttpContexts47* are created by calling {@link #createContext(String,HttpHandler)}.48* Any request for which no handler can be found is rejected with a 404 response.49* Management of threads can be done external to this object by providing a50* {@link java.util.concurrent.Executor} object. If none is provided a default51* implementation is used.52* <p>53* <a name="mapping_description"></a>54* <b>Mapping request URIs to HttpContext paths</b><p>55* When a HTTP request is received,56* the appropriate HttpContext (and handler) is located by finding the context57* whose path is the longest matching prefix of the request URI's path.58* Paths are matched literally, which means that the strings are compared59* case sensitively, and with no conversion to or from any encoded forms.60* For example. Given a HttpServer with the following HttpContexts configured.<p>61* <table >62* <tr><td><i>Context</i></td><td><i>Context path</i></td></tr>63* <tr><td>ctx1</td><td>"/"</td></tr>64* <tr><td>ctx2</td><td>"/apps/"</td></tr>65* <tr><td>ctx3</td><td>"/apps/foo/"</td></tr>66* </table>67* <p>68* the following table shows some request URIs and which, if any context they would69* match with.<p>70* <table>71* <tr><td><i>Request URI</i></td><td><i>Matches context</i></td></tr>72* <tr><td>"http://foo.com/apps/foo/bar"</td><td>ctx3</td></tr>73* <tr><td>"http://foo.com/apps/Foo/bar"</td><td>no match, wrong case</td></tr>74* <tr><td>"http://foo.com/apps/app1"</td><td>ctx2</td></tr>75* <tr><td>"http://foo.com/foo"</td><td>ctx1</td></tr>76* </table>77* <p>78* <b>Note about socket backlogs</b><p>79* When binding to an address and port number, the application can also specify an integer80* <i>backlog</i> parameter. This represents the maximum number of incoming TCP connections81* which the system will queue internally. Connections are queued while they are waiting to82* be accepted by the HttpServer. When the limit is reached, further connections may be83* rejected (or possibly ignored) by the underlying TCP implementation. Setting the right84* backlog value is a compromise between efficient resource usage in the TCP layer (not setting85* it too high) and allowing adequate throughput of incoming requests (not setting it too low).86* @since 1.687*/8889@jdk.Exported90public abstract class HttpServer {9192/**93*/94protected HttpServer () {95}9697/**98* creates a HttpServer instance which is initially not bound to any local address/port.99* The HttpServer is acquired from the currently installed {@link HttpServerProvider}100* The server must be bound using {@link #bind(InetSocketAddress,int)} before it can be used.101* @throws IOException102*/103public static HttpServer create () throws IOException {104return create (null, 0);105}106107/**108* Create a <code>HttpServer</code> instance which will bind to the109* specified {@link java.net.InetSocketAddress} (IP address and port number)110*111* A maximum backlog can also be specified. This is the maximum number of112* queued incoming connections to allow on the listening socket.113* Queued TCP connections exceeding this limit may be rejected by the TCP implementation.114* The HttpServer is acquired from the currently installed {@link HttpServerProvider}115*116* @param addr the address to listen on, if <code>null</code> then bind() must be called117* to set the address118* @param backlog the socket backlog. If this value is less than or equal to zero,119* then a system default value is used.120* @throws BindException if the server cannot bind to the requested address,121* or if the server is already bound.122* @throws IOException123*/124125public static HttpServer create (126InetSocketAddress addr, int backlog127) throws IOException {128HttpServerProvider provider = HttpServerProvider.provider();129return provider.createHttpServer (addr, backlog);130}131132/**133* Binds a currently unbound HttpServer to the given address and port number.134* A maximum backlog can also be specified. This is the maximum number of135* queued incoming connections to allow on the listening socket.136* Queued TCP connections exceeding this limit may be rejected by the TCP implementation.137* @param addr the address to listen on138* @param backlog the socket backlog. If this value is less than or equal to zero,139* then a system default value is used.140* @throws BindException if the server cannot bind to the requested address or if the server141* is already bound.142* @throws NullPointerException if addr is <code>null</code>143*/144public abstract void bind (InetSocketAddress addr, int backlog) throws IOException;145146/**147* Starts this server in a new background thread. The background thread148* inherits the priority, thread group and context class loader149* of the caller.150*/151public abstract void start () ;152153/**154* sets this server's {@link java.util.concurrent.Executor} object. An155* Executor must be established before {@link #start()} is called.156* All HTTP requests are handled in tasks given to the executor.157* If this method is not called (before start()) or if it is158* called with a <code>null</code> Executor, then159* a default implementation is used, which uses the thread160* which was created by the {@link #start()} method.161* @param executor the Executor to set, or <code>null</code> for default162* implementation163* @throws IllegalStateException if the server is already started164*/165public abstract void setExecutor (Executor executor);166167168/**169* returns this server's Executor object if one was specified with170* {@link #setExecutor(Executor)}, or <code>null</code> if none was171* specified.172* @return the Executor established for this server or <code>null</code> if not set.173*/174public abstract Executor getExecutor () ;175176/**177* stops this server by closing the listening socket and disallowing178* any new exchanges from being processed. The method will then block179* until all current exchange handlers have completed or else when180* approximately <i>delay</i> seconds have elapsed (whichever happens181* sooner). Then, all open TCP connections are closed, the background182* thread created by start() exits, and the method returns.183* Once stopped, a HttpServer cannot be re-used. <p>184*185* @param delay the maximum time in seconds to wait until exchanges have finished.186* @throws IllegalArgumentException if delay is less than zero.187*/188public abstract void stop (int delay);189190/**191* Creates a HttpContext. A HttpContext represents a mapping from a192* URI path to a exchange handler on this HttpServer. Once created, all requests193* received by the server for the path will be handled by calling194* the given handler object. The context is identified by the path, and195* can later be removed from the server using this with the {@link #removeContext(String)} method.196* <p>197* The path specifies the root URI path for this context. The first character of path must be198* '/'. <p>199* The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>200* to HttpContext instances.201* @param path the root URI path to associate the context with202* @param handler the handler to invoke for incoming requests.203* @throws IllegalArgumentException if path is invalid, or if a context204* already exists for this path205* @throws NullPointerException if either path, or handler are <code>null</code>206*/207public abstract HttpContext createContext (String path, HttpHandler handler) ;208209/**210* Creates a HttpContext without initially specifying a handler. The handler must later be specified using211* {@link HttpContext#setHandler(HttpHandler)}. A HttpContext represents a mapping from a212* URI path to an exchange handler on this HttpServer. Once created, and when213* the handler has been set, all requests214* received by the server for the path will be handled by calling215* the handler object. The context is identified by the path, and216* can later be removed from the server using this with the {@link #removeContext(String)} method.217* <p>218* The path specifies the root URI path for this context. The first character of path must be219* '/'. <p>220* The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>221* to HttpContext instances.222* @param path the root URI path to associate the context with223* @throws IllegalArgumentException if path is invalid, or if a context224* already exists for this path225* @throws NullPointerException if path is <code>null</code>226*/227public abstract HttpContext createContext (String path) ;228229/**230* Removes the context identified by the given path from the server.231* Removing a context does not affect exchanges currently being processed232* but prevents new ones from being accepted.233* @param path the path of the handler to remove234* @throws IllegalArgumentException if no handler corresponding to this235* path exists.236* @throws NullPointerException if path is <code>null</code>237*/238public abstract void removeContext (String path) throws IllegalArgumentException ;239240/**241* Removes the given context from the server.242* Removing a context does not affect exchanges currently being processed243* but prevents new ones from being accepted.244* @param context the context to remove245* @throws NullPointerException if context is <code>null</code>246*/247public abstract void removeContext (HttpContext context) ;248249/**250* returns the address this server is listening on251* @return the address/port number the server is listening on252*/253public abstract InetSocketAddress getAddress() ;254}255256257