Skip to content

Commit f505396

Browse files
author
Julia Boes
committed
8277459: Add jwebserver tool
Reviewed-by: michaelm, dfuchs, ihse
1 parent 84aa0a1 commit f505396

File tree

17 files changed

+1028
-70
lines changed

17 files changed

+1028
-70
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright (c) 2021, 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+
include LauncherCommon.gmk
27+
28+
$(eval $(call SetupBuildLauncher, jwebserver, \
29+
MAIN_CLASS := sun.net.httpserver.simpleserver.JWebServer, \
30+
))

src/jdk.httpserver/share/classes/com/sun/net/httpserver/SimpleFileServer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@
104104
* server.start();
105105
* }</pre>
106106
*
107-
* <h2>Main entry point</h2>
107+
* <h2>jwebserver Tool</h2>
108108
*
109-
* <p>A <a id="server-impl">simple HTTP file server implementation</a> is
110-
* provided via the
111-
* <a href="{@docRoot}/jdk.httpserver/module-summary.html#entry-point">main entry point</a>
112-
* of the {@code jdk.httpserver} module.
109+
* <p>A simple HTTP file server implementation is provided via the
110+
* {@code jwebserver} tool.
111+
*
112+
* @toolGuide jwebserver
113113
*
114114
* @since 18
115115
*/

src/jdk.httpserver/share/classes/com/sun/net/httpserver/package-info.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@
3131
Any HTTP functionality not provided by this API can be implemented by application code
3232
using the API.
3333
<p>
34+
* The main components are:
35+
* <ul>
36+
* <li>the {@link com.sun.net.httpserver.HttpExchange} class that describes a
37+
* request and response pair,</li>
38+
* <li>the {@link com.sun.net.httpserver.HttpHandler} interface to handle
39+
* incoming requests, plus the {@link com.sun.net.httpserver.HttpHandlers} class
40+
* that provides useful handler implementations,</li>
41+
* <li>the {@link com.sun.net.httpserver.HttpContext} class that maps a URI path
42+
* to a {@code HttpHandler},</li>
43+
* <li>the {@link com.sun.net.httpserver.HttpServer} class to listen for
44+
* connections and dispatch requests to handlers,</li>
45+
* <li>the {@link com.sun.net.httpserver.Filter} class that allows pre- and post-
46+
* processing of requests.</li></ul>
47+
* <p>
48+
* The {@link com.sun.net.httpserver.SimpleFileServer} class offers a simple
49+
* HTTP-only file server (intended for testing, development and debugging purposes
50+
* only). A default implementation is provided via the {@code jwebserver} tool.
51+
<p>
3452
Programmers must implement the {@link com.sun.net.httpserver.HttpHandler} interface. This interface
3553
provides a callback which is invoked to handle incoming requests from clients.
3654
A HTTP request and its response is known as an exchange. HTTP exchanges are
@@ -120,13 +138,6 @@ public void configure (HttpsParameters params) {
120138
}
121139
});
122140
</pre></blockquote>
123-
<p>
124-
The {@link com.sun.net.httpserver.SimpleFileServer} class offers a simple
125-
HTTP file server (intended for testing, development and debugging purposes
126-
only). A default implementation is provided via the
127-
<a href="{@docRoot}/jdk.httpserver/module-summary.html#entry-point">main entry point</a>
128-
of the {@code jdk.httpserver} module.
129-
130141
@since 1.6
131142
*/
132143
package com.sun.net.httpserver;

src/jdk.httpserver/share/classes/module-info.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,21 @@
2424
*/
2525

2626
/**
27-
* Defines the JDK-specific HTTP server API.
28-
* <p>
29-
* A basic high-level API for building embedded servers. Both HTTP and
30-
* HTTPS are supported.
31-
* <p>
32-
* The main components are:
33-
* <ul>
34-
* <li>the {@link com.sun.net.httpserver.HttpExchange} class that describes a
35-
* request and response pair,</li>
36-
* <li>the {@link com.sun.net.httpserver.HttpHandler} interface to handle
37-
* incoming requests, plus the {@link com.sun.net.httpserver.HttpHandlers} class
38-
* that provides useful handler implementations,</li>
39-
* <li>the {@link com.sun.net.httpserver.HttpContext} class that maps a URI path
40-
* to a {@code HttpHandler},</li>
41-
* <li>the {@link com.sun.net.httpserver.HttpServer} class to listen for
42-
* connections and dispatch requests to handlers,</li>
43-
* <li>the {@link com.sun.net.httpserver.Filter} class that allows pre- and post-
44-
* processing of requests.</li></ul>
45-
* <p>
46-
* The {@link com.sun.net.httpserver.SimpleFileServer} class offers a simple
47-
* HTTP file server (intended for testing, development and debugging purposes
48-
* only). A default implementation is provided via the <a id="entry-point"></a>
49-
* main entry point of the {@code jdk.httpserver} module, which can be used on
50-
* the command line as such:
51-
* <pre>{@code
52-
* Usage: java -m jdk.httpserver [-b bind address] [-p port] [-d directory]
53-
* [-o none|info|verbose] [-h to show options]
54-
* Options:
55-
* -b, --bind-address - Address to bind to. Default: 127.0.0.1 or ::1 (loopback).
56-
* For all interfaces use "-b 0.0.0.0" or "-b ::".
57-
* -d, --directory - Directory to serve. Default: current directory.
58-
* -o, --output - Output format. none|info|verbose. Default: info.
59-
* -p, --port - Port to listen on. Default: 8000.
60-
* -h, -?, --help - Print this help message.
61-
* }</pre>
27+
* Defines the JDK-specific HTTP server API, and provides the jwebserver tool
28+
* for running a minimal HTTP server.
29+
*
30+
* <p>The {@link com.sun.net.httpserver} package defines a high-level API for
31+
* building servers that support HTTP and HTTPS. The SimpleFileServer class
32+
* implements a simple HTTP-only file server intended for testing, development
33+
* and debugging purposes. A default implementation is provided via the
34+
* {@code jwebserver} tool and the main entry point of the module, which can
35+
* also be invoked with {@code java -m jdk.httpserver}.
36+
*
37+
* <p>The {@link com.sun.net.httpserver.spi} package specifies a Service Provider
38+
* Interface (SPI) for locating HTTP server implementations based on the
39+
* {@code com.sun.net.httpserver} API.
40+
*
41+
* @toolGuide jwebserver
6242
*
6343
* @uses com.sun.net.httpserver.spi.HttpServerProvider
6444
*
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2021, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package sun.net.httpserver.simpleserver;
25+
26+
import java.io.PrintWriter;
27+
import static java.nio.charset.StandardCharsets.UTF_8;
28+
29+
/**
30+
* Programmatic entry point to start the jwebserver tool.
31+
*
32+
* <p><b> This is NOT part of any supported API.
33+
* If you write code that depends on this, you do so at your own risk.
34+
* This code and its internal interface are subject to change or deletion
35+
* without notice.</b>
36+
*/
37+
public class JWebServer {
38+
39+
/**
40+
* This constructor should never be called.
41+
*/
42+
private JWebServer() { throw new AssertionError(); }
43+
44+
/**
45+
* The main entry point.
46+
*
47+
* <p> The command line arguments are parsed and the server is started. If
48+
* started successfully, the server will run on a new non-daemon thread,
49+
* and this method will return. Otherwise, if the server is not started
50+
* successfully, e.g. an error is encountered while parsing the arguments
51+
* or an I/O error occurs, the server is not started and this method invokes
52+
* System::exit with an appropriate exit code.
53+
*
54+
* @param args the command-line options
55+
* @throws NullPointerException if {@code args} is {@code null}, or if there
56+
* are any {@code null} values in the {@code args} array
57+
*/
58+
public static void main(String... args) {
59+
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true, UTF_8), "jwebserver", args);
60+
if (ec != 0) {
61+
System.exit(ec);
62+
} // otherwise, the server has either been started successfully and
63+
// runs in another non-daemon thread, or -h or -version have been
64+
// passed and the main thread has exited normally.
65+
}
66+
}

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/Main.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import static java.nio.charset.StandardCharsets.UTF_8;
2828

2929
/**
30-
* Programmatic entry point to start the simpleserver tool.
30+
* Programmatic entry point to start "java -m jdk.httpserver".
3131
*
3232
* <p><b> This is NOT part of any supported API.
3333
* If you write code that depends on this, you do so at your own risk.
@@ -56,10 +56,11 @@ public class Main {
5656
* are any {@code null} values in the {@code args} array
5757
*/
5858
public static void main(String... args) {
59-
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true, UTF_8), args);
60-
if (ec != 0)
59+
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true, UTF_8), "java", args);
60+
if (ec != 0) {
6161
System.exit(ec);
62-
// otherwise the server has been started successfully and runs in
63-
// another non-daemon thread.
62+
} // otherwise, the server has either been started successfully and
63+
// runs in another non-daemon thread, or -h or -version have been
64+
// passed and the main thread has exited normally.
6465
}
6566
}

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/SimpleFileServerImpl.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ final class SimpleFileServerImpl {
7171
*
7272
* @param writer the writer to which output should be written
7373
* @param args the command line options
74+
* @param launcher the launcher the server is started from
7475
* @throws NullPointerException if any of the arguments are {@code null},
7576
* or if there are any {@code null} values in the {@code args} array
7677
* @return startup status code
7778
*/
78-
static int start(PrintWriter writer, String[] args) {
79+
static int start(PrintWriter writer, String launcher, String[] args) {
7980
Objects.requireNonNull(args);
8081
for (var arg : args) {
8182
Objects.requireNonNull(arg);
@@ -96,7 +97,11 @@ static int start(PrintWriter writer, String[] args) {
9697
option = options.next();
9798
switch (option) {
9899
case "-h", "-?", "--help" -> {
99-
out.showHelp();
100+
out.showHelp(launcher);
101+
return Startup.OK.statusCode;
102+
}
103+
case "-version", "--version" -> {
104+
out.showVersion(launcher);
100105
return Startup.OK.statusCode;
101106
}
102107
case "-b", "--bind-address" -> {
@@ -115,7 +120,7 @@ static int start(PrintWriter writer, String[] args) {
115120
}
116121
} catch (AssertionError ae) {
117122
out.reportError(ResourceBundleHelper.getMessage("err.unknown.option", option));
118-
out.showUsage();
123+
out.showUsage(launcher);
119124
return Startup.CMDERR.statusCode;
120125
} catch (NoSuchElementException nsee) {
121126
out.reportError(ResourceBundleHelper.getMessage("err.missing.arg", option));
@@ -169,12 +174,16 @@ void printStartMessage(Path root, HttpServer server)
169174
}
170175
}
171176

172-
void showUsage() {
173-
writer.println(ResourceBundleHelper.getMessage("usage"));
177+
void showUsage(String launcher) {
178+
writer.println(ResourceBundleHelper.getMessage("usage." + launcher));
179+
}
180+
181+
void showVersion(String launcher) {
182+
writer.println(ResourceBundleHelper.getMessage("version", launcher, System.getProperty("java.version")));
174183
}
175184

176-
void showHelp() {
177-
writer.println(ResourceBundleHelper.getMessage("usage"));
185+
void showHelp(String launcher) {
186+
writer.println(ResourceBundleHelper.getMessage("usage." + launcher));
178187
writer.println(ResourceBundleHelper.getMessage("options", LOOPBACK_ADDR.getHostAddress()));
179188
}
180189

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/resources/simpleserver.properties

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@
2323
# questions.
2424
#
2525

26-
usage=\
26+
usage.java=\
2727
Usage: java -m jdk.httpserver [-b bind address] [-p port] [-d directory]\n\
28-
\ [-o none|info|verbose] [-h to show options]
28+
\ [-o none|info|verbose] [-h to show options]\n\
29+
\ [-version to show version information]
30+
31+
usage.jwebserver=\
32+
Usage: jwebserver [-b bind address] [-p port] [-d directory]\n\
33+
\ [-o none|info|verbose] [-h to show options]\n\
34+
\ [-version to show version information]
35+
36+
version=\
37+
{0} {1}
2938

3039
options=\
3140
Options:\n\
@@ -34,7 +43,8 @@ Options:\n\
3443
-d, --directory - Directory to serve. Default: current directory.\n\
3544
-o, --output - Output format. none|info|verbose. Default: info.\n\
3645
-p, --port - Port to listen on. Default: 8000.\n\
37-
-h, -?, --help - Print this help message.\n\
46+
-h, -?, --help - Prints this help message and exits.\n\
47+
-version, --version - Prints version information and exits.\n\
3848
To stop the server, press Ctrl + C.
3949

4050
opt.bindaddress=\

0 commit comments

Comments
 (0)