0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.net.httpserver;
0N/A
0N/Aimport java.net.*;
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.security.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport javax.net.ssl.*;
0N/Aimport com.sun.net.httpserver.spi.HttpServerProvider;
0N/A
0N/A/**
0N/A * This class implements a simple HTTP server. A HttpServer is bound to an IP address
0N/A * and port number and listens for incoming TCP connections from clients on this address.
0N/A * The sub-class {@link HttpsServer} implements a server which handles HTTPS requests.
0N/A * <p>
0N/A * One or more {@link HttpHandler} objects must be associated with a server
0N/A * in order to process requests. Each such HttpHandler is registered
0N/A * with a root URI path which represents the
0N/A * location of the application or service on this server. The mapping of a handler
0N/A * to a HttpServer is encapsulated by a {@link HttpContext} object. HttpContexts
0N/A * are created by calling {@link #createContext(String,HttpHandler)}.
0N/A * Any request for which no handler can be found is rejected with a 404 response.
0N/A * Management of threads can be done external to this object by providing a
0N/A * {@link java.util.concurrent.Executor} object. If none is provided a default
0N/A * implementation is used.
0N/A * <p>
0N/A * <a name="mapping_description"></a>
0N/A * <b>Mapping request URIs to HttpContext paths</b><p>
0N/A * When a HTTP request is received,
0N/A * the appropriate HttpContext (and handler) is located by finding the context
0N/A * whose path is the longest matching prefix of the request URI's path.
0N/A * Paths are matched literally, which means that the strings are compared
0N/A * case sensitively, and with no conversion to or from any encoded forms.
0N/A * For example. Given a HttpServer with the following HttpContexts configured.<p>
0N/A * <table >
0N/A * <tr><td><i>Context</i></td><td><i>Context path</i></td></tr>
0N/A * <tr><td>ctx1</td><td>"/"</td></tr>
0N/A * <tr><td>ctx2</td><td>"/apps/"</td></tr>
0N/A * <tr><td>ctx3</td><td>"/apps/foo/"</td></tr>
0N/A * </table>
0N/A * <p>
0N/A * the following table shows some request URIs and which, if any context they would
0N/A * match with.<p>
0N/A * <table>
0N/A * <tr><td><i>Request URI</i></td><td><i>Matches context</i></td></tr>
0N/A * <tr><td>"http://foo.com/apps/foo/bar"</td><td>ctx3</td></tr>
0N/A * <tr><td>"http://foo.com/apps/Foo/bar"</td><td>no match, wrong case</td></tr>
0N/A * <tr><td>"http://foo.com/apps/app1"</td><td>ctx2</td></tr>
0N/A * <tr><td>"http://foo.com/foo"</td><td>ctx1</td></tr>
0N/A * </table>
0N/A * <p>
0N/A * <b>Note about socket backlogs</b><p>
0N/A * When binding to an address and port number, the application can also specify an integer
0N/A * <i>backlog</i> parameter. This represents the maximum number of incoming TCP connections
0N/A * which the system will queue internally. Connections are queued while they are waiting to
0N/A * be accepted by the HttpServer. When the limit is reached, further connections may be
0N/A * rejected (or possibly ignored) by the underlying TCP implementation. Setting the right
0N/A * backlog value is a compromise between efficient resource usage in the TCP layer (not setting
0N/A * it too high) and allowing adequate throughput of incoming requests (not setting it too low).
0N/A * @since 1.6
0N/A */
0N/A
0N/Apublic abstract class HttpServer {
0N/A
0N/A /**
0N/A */
0N/A protected HttpServer () {
0N/A }
0N/A
0N/A /**
0N/A * creates a HttpServer instance which is initially not bound to any local address/port.
0N/A * The HttpServer is acquired from the currently installed {@link HttpServerProvider}
0N/A * The server must be bound using {@link #bind(InetSocketAddress,int)} before it can be used.
0N/A * @throws IOException
0N/A */
0N/A public static HttpServer create () throws IOException {
0N/A return create (null, 0);
0N/A }
0N/A
0N/A /**
0N/A * Create a <code>HttpServer</code> instance which will bind to the
0N/A * specified {@link java.net.InetSocketAddress} (IP address and port number)
0N/A *
0N/A * A maximum backlog can also be specified. This is the maximum number of
0N/A * queued incoming connections to allow on the listening socket.
0N/A * Queued TCP connections exceeding this limit may be rejected by the TCP implementation.
0N/A * The HttpServer is acquired from the currently installed {@link HttpServerProvider}
0N/A *
0N/A * @param addr the address to listen on, if <code>null</code> then bind() must be called
0N/A * to set the address
0N/A * @param backlog the socket backlog. If this value is less than or equal to zero,
0N/A * then a system default value is used.
0N/A * @throws BindException if the server cannot bind to the requested address,
0N/A * or if the server is already bound.
0N/A * @throws IOException
0N/A */
0N/A
0N/A public static HttpServer create (
0N/A InetSocketAddress addr, int backlog
0N/A ) throws IOException {
0N/A HttpServerProvider provider = HttpServerProvider.provider();
0N/A return provider.createHttpServer (addr, backlog);
0N/A }
0N/A
0N/A /**
0N/A * Binds a currently unbound HttpServer to the given address and port number.
0N/A * A maximum backlog can also be specified. This is the maximum number of
0N/A * queued incoming connections to allow on the listening socket.
0N/A * Queued TCP connections exceeding this limit may be rejected by the TCP implementation.
0N/A * @param addr the address to listen on
0N/A * @param backlog the socket backlog. If this value is less than or equal to zero,
0N/A * then a system default value is used.
0N/A * @throws BindException if the server cannot bind to the requested address or if the server
0N/A * is already bound.
0N/A * @throws NullPointerException if addr is <code>null</code>
0N/A */
0N/A public abstract void bind (InetSocketAddress addr, int backlog) throws IOException;
0N/A
0N/A /**
0N/A * Starts this server in a new background thread. The background thread
0N/A * inherits the priority, thread group and context class loader
0N/A * of the caller.
0N/A */
0N/A public abstract void start () ;
0N/A
0N/A /**
0N/A * sets this server's {@link java.util.concurrent.Executor} object. An
0N/A * Executor must be established before {@link #start()} is called.
0N/A * All HTTP requests are handled in tasks given to the executor.
0N/A * If this method is not called (before start()) or if it is
0N/A * called with a <code>null</code> Executor, then
0N/A * a default implementation is used, which uses the thread
0N/A * which was created by the {@link #start()} method.
0N/A * @param executor the Executor to set, or <code>null</code> for default
0N/A * implementation
0N/A * @throws IllegalStateException if the server is already started
0N/A */
0N/A public abstract void setExecutor (Executor executor);
0N/A
0N/A
0N/A /**
0N/A * returns this server's Executor object if one was specified with
0N/A * {@link #setExecutor(Executor)}, or <code>null</code> if none was
0N/A * specified.
0N/A * @return the Executor established for this server or <code>null</code> if not set.
0N/A */
0N/A public abstract Executor getExecutor () ;
0N/A
0N/A /**
0N/A * stops this server by closing the listening socket and disallowing
0N/A * any new exchanges from being processed. The method will then block
0N/A * until all current exchange handlers have completed or else when
0N/A * approximately <i>delay</i> seconds have elapsed (whichever happens
0N/A * sooner). Then, all open TCP connections are closed, the background
0N/A * thread created by start() exits, and the method returns.
0N/A * Once stopped, a HttpServer cannot be re-used. <p>
0N/A *
0N/A * @param delay the maximum time in seconds to wait until exchanges have finished.
0N/A * @throws IllegalArgumentException if delay is less than zero.
0N/A */
0N/A public abstract void stop (int delay);
0N/A
0N/A /**
0N/A * Creates a HttpContext. A HttpContext represents a mapping from a
0N/A * URI path to a exchange handler on this HttpServer. Once created, all requests
0N/A * received by the server for the path will be handled by calling
0N/A * the given handler object. The context is identified by the path, and
0N/A * can later be removed from the server using this with the {@link #removeContext(String)} method.
0N/A * <p>
0N/A * The path specifies the root URI path for this context. The first character of path must be
0N/A * '/'. <p>
0N/A * The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>
0N/A * to HttpContext instances.
0N/A * @param path the root URI path to associate the context with
0N/A * @param handler the handler to invoke for incoming requests.
0N/A * @throws IllegalArgumentException if path is invalid, or if a context
0N/A * already exists for this path
0N/A * @throws NullPointerException if either path, or handler are <code>null</code>
0N/A */
0N/A public abstract HttpContext createContext (String path, HttpHandler handler) ;
0N/A
0N/A /**
0N/A * Creates a HttpContext without initially specifying a handler. The handler must later be specified using
0N/A * {@link HttpContext#setHandler(HttpHandler)}. A HttpContext represents a mapping from a
0N/A * URI path to an exchange handler on this HttpServer. Once created, and when
0N/A * the handler has been set, all requests
0N/A * received by the server for the path will be handled by calling
0N/A * the handler object. The context is identified by the path, and
0N/A * can later be removed from the server using this with the {@link #removeContext(String)} method.
0N/A * <p>
0N/A * The path specifies the root URI path for this context. The first character of path must be
0N/A * '/'. <p>
0N/A * The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>
0N/A * to HttpContext instances.
0N/A * @param path the root URI path to associate the context with
0N/A * @throws IllegalArgumentException if path is invalid, or if a context
0N/A * already exists for this path
0N/A * @throws NullPointerException if path is <code>null</code>
0N/A */
0N/A public abstract HttpContext createContext (String path) ;
0N/A
0N/A /**
0N/A * Removes the context identified by the given path from the server.
0N/A * Removing a context does not affect exchanges currently being processed
0N/A * but prevents new ones from being accepted.
0N/A * @param path the path of the handler to remove
0N/A * @throws IllegalArgumentException if no handler corresponding to this
0N/A * path exists.
0N/A * @throws NullPointerException if path is <code>null</code>
0N/A */
0N/A public abstract void removeContext (String path) throws IllegalArgumentException ;
0N/A
0N/A /**
0N/A * Removes the given context from the server.
0N/A * Removing a context does not affect exchanges currently being processed
0N/A * but prevents new ones from being accepted.
0N/A * @param context the context to remove
0N/A * @throws NullPointerException if context is <code>null</code>
0N/A */
0N/A public abstract void removeContext (HttpContext context) ;
0N/A
0N/A /**
0N/A * returns the address this server is listening on
0N/A * @return the address/port number the server is listening on
0N/A */
0N/A public abstract InetSocketAddress getAddress() ;
0N/A}