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