src/jdk.httpserver/share/classes/com/sun/net/httpserver/package-info.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20742
diff changeset
     2
 * Copyright (c) 2005, 2013, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   Provides a simple high-level Http server API, which can be used to build
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   embedded HTTP servers. Both "http" and "https" are supported. The API provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
   a partial implementation of RFC <a href="http://www.ietf.org/rfc/rfc2616.txt">2616</a> (HTTP 1.1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
   and RFC <a href="http://www.ietf.org/rfc/rfc2818.txt">2818</a> (HTTP over TLS).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
   Any HTTP functionality not provided by this API can be implemented by application code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
   using the API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
   <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
   Programmers must implement the {@link com.sun.net.httpserver.HttpHandler} interface. This interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
   provides a callback which is invoked to handle incoming requests from clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
   A HTTP request and its response is known as an exchange. HTTP exchanges are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
   represented by the {@link com.sun.net.httpserver.HttpExchange} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
   The {@link com.sun.net.httpserver.HttpServer} class is used to listen for incoming TCP connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
   and it dispatches requests on these connections to handlers which have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
   registered with the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   A minimal Http server example is shown below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
       public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
           InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
           read(is); // .. read the request body
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
           String response = "This is the response";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
           t.sendResponseHeaders(200, response.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
           OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
           os.write(response.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
           os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
39654
c27372cfdb3f 8144692: HttpServer API: use of non-existant method in example in package Javadoc
vtewari
parents: 34894
diff changeset
    57
   HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   server.createContext("/applications/myapp", new MyHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
   server.setExecutor(null); // creates a default executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   server.start();
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    61
   </pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   <p>The example above creates a simple HttpServer which uses the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   application thread to invoke the handle() method for incoming http
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
   requests directed to port 8000, and to the path /applications/myapp/.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
   The {@link com.sun.net.httpserver.HttpExchange} class encapsulates everything an application needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   process incoming requests and to generate appropriate responses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
   <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
   Registering a handler with a HttpServer creates a {@link com.sun.net.httpserver.HttpContext} object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   {@link com.sun.net.httpserver.Filter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   objects can be added to the returned context. Filters are used to perform automatic pre- and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   post-processing of exchanges before they are passed to the exchange handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
   <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   For sensitive information, a {@link com.sun.net.httpserver.HttpsServer} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
   be used to process "https" requests secured by the SSL or TLS protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
   A HttpsServer must be provided with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
   {@link com.sun.net.httpserver.HttpsConfigurator} object, which contains an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   initialized {@link javax.net.ssl.SSLContext}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
   HttpsConfigurator can be used to configure the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
   cipher suites and other SSL operating parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
   A simple example SSLContext could be created as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
   <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
   char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
   KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
   ks.load(new FileInputStream("testkeys"), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
   KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
   kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
   TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
   tmf.init(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
   SSLContext ssl = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
   ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    95
   </pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
   <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
   In the example above, a keystore file called "testkeys", created with the keytool utility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   is used as a certificate store for client and server certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
   The following code shows how the SSLContext is then used in a HttpsConfigurator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
   and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
   <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        public void configure (HttpsParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // get the remote address if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        InetSocketAddress remote = params.getClientAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SSLContext c = getSSLContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // get the default parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        SSLParameters sslparams = c.getDefaultSSLParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (remote.equals (...) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // modify the default set for client x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        params.setSSLParameters(sslparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // statement above could throw IAE if any params invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // eg. if app has a UI and parameters supplied by a user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    });
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   122
   </pre></blockquote>
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
   @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
package com.sun.net.httpserver;