/* * Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.net.*; import java.io.*; import java.nio.*; import java.nio.channels.*; import sun.net.www.MessageHeader; import java.util.*; import javax.net.ssl.*; import javax.net.ssl.SSLEngineResult.*; import java.security.*; /** * This class implements a simple HTTPS server. It uses multiple threads to * handle connections in parallel, and will spin off a new thread to handle * each request. (this is easier to implement with SSLEngine) *
* It must be instantiated with a {@link HttpCallback} object to which * requests are given and must be handled. *
* Simple synchronization between the client(s) and server can be done
* using the {@link #waitForCondition(String)}, {@link #setCondition(String)} and
* {@link #rendezvous(String,int)} methods.
*
* NOTE NOTE NOTE NOTE NOTE NOTE NOTE
*
* If you make a change in here, please don't forget to make the
* corresponding change in the J2SE equivalent.
*
* NOTE NOTE NOTE NOTE NOTE NOTE NOTE
*/
public class HttpServer {
ServerSocketChannel schan;
int threads;
int cperthread;
HttpCallback cb;
Server[] servers;
// ssl related fields
static SSLContext sslCtx;
/**
* Create a
* A rendezvous built on a condition is also provided for synchronizing
* N threads.
*/
private static HashMap conditions = new HashMap();
/*
* Modifiable boolean object
*/
private static class BValue {
boolean v;
}
/*
* Modifiable int object
*/
private static class IValue {
int v;
IValue (int i) {
v =i;
}
}
private static BValue getCond (String condition) {
synchronized (conditions) {
BValue cond = (BValue) conditions.get (condition);
if (cond == null) {
cond = new BValue();
conditions.put (condition, cond);
}
return cond;
}
}
/**
* Set the condition to true. Any threads that are currently blocked
* waiting on the condition, will be unblocked and allowed to continue.
* Threads that subsequently call waitForCondition() will not block.
* If the named condition did not exist prior to the call, then it is created
* first.
*/
public static void setCondition (String condition) {
BValue cond = getCond (condition);
synchronized (cond) {
if (cond.v) {
return;
}
cond.v = true;
cond.notifyAll();
}
}
/**
* If the named condition does not exist, then it is created and initialized
* to false. If the condition exists or has just been created and its value
* is false, then the thread blocks until another thread sets the condition.
* If the condition exists and is already set to true, then this call returns
* immediately without blocking.
*/
public static void waitForCondition (String condition) {
BValue cond = getCond (condition);
synchronized (cond) {
if (!cond.v) {
try {
cond.wait();
} catch (InterruptedException e) {}
}
}
}
/* conditions must be locked when accessing this */
static HashMap rv = new HashMap();
/**
* Force N threads to rendezvous (ie. wait for each other) before proceeding.
* The first thread(s) to call are blocked until the last
* thread makes the call. Then all threads continue.
*
* All threads that call with the same condition name, must use the same value
* for N (or the results may be not be as expected).
*
* Obviously, if fewer than N threads make the rendezvous then the result
* will be a hang.
*/
public static void rendezvous (String condition, int N) {
BValue cond;
IValue iv;
String name = "RV_"+condition;
/* get the condition */
synchronized (conditions) {
cond = (BValue)conditions.get (name);
if (cond == null) {
/* we are first caller */
if (N < 2) {
throw new RuntimeException ("rendezvous must be called with N >= 2");
}
cond = new BValue ();
conditions.put (name, cond);
iv = new IValue (N-1);
rv.put (name, iv);
} else {
/* already initialised, just decrement the counter */
iv = (IValue) rv.get (name);
iv.v --;
}
}
if (iv.v > 0) {
waitForCondition (name);
} else {
setCondition (name);
synchronized (conditions) {
clearCondition (name);
rv.remove (name);
}
}
}
/**
* If the named condition exists and is set then remove it, so it can
* be re-initialized and used again. If the condition does not exist, or
* exists but is not set, then the call returns without doing anything.
* Note, some higher level synchronization
* may be needed between clear and the other operations.
*/
public static void clearCondition(String condition) {
BValue cond;
synchronized (conditions) {
cond = (BValue) conditions.get (condition);
if (cond == null) {
return;
}
synchronized (cond) {
if (cond.v) {
conditions.remove (condition);
}
}
}
}
}
HttpServer
instance with the specified callback object
* for handling requests. One thread is created to handle requests,
* and up to ten TCP connections will be handled simultaneously.
* @param cb the callback object which is invoked to handle each
* incoming request
*/
public HttpServer (HttpCallback cb) throws IOException {
this (cb, 1, 10, 0);
}
/**
* Create a
HttpServer
instance with the specified number of
* threads and maximum number of connections per thread. This functions
* the same as the 4 arg constructor, where the port argument is set to zero.
* @param cb the callback object which is invoked to handle each
* incoming request
* @param threads the number of threads to create to handle requests
* in parallel
* @param cperthread the number of simultaneous TCP connections to
* handle per thread
*/
public HttpServer (HttpCallback cb, int threads, int cperthread)
throws IOException {
this (cb, threads, cperthread, 0);
}
/**
* Create a
HttpServer
instance with the specified number
* of threads and maximum number of connections per thread and running on
* the specified port. The specified number of threads are created to
* handle incoming requests, and each thread is allowed
* to handle a number of simultaneous TCP connections.
* @param cb the callback object which is invoked to handle
* each incoming request
* @param threads the number of threads to create to handle
* requests in parallel
* @param cperthread the number of simultaneous TCP connections
* to handle per thread
* @param port the port number to bind the server to.
Zero
* means choose any free port.
*/
public HttpServer (HttpCallback cb, int threads, int cperthread, int port)
throws IOException {
schan = ServerSocketChannel.open ();
InetSocketAddress addr = new InetSocketAddress (port);
schan.socket().bind (addr);
this.threads = threads;
this.cb = cb;
this.cperthread = cperthread;
try {
// create and initialize a SSLContext
KeyStore ks = KeyStore.getInstance("JKS");
KeyStore ts = KeyStore.getInstance("JKS");
char[] passphrase = "passphrase".toCharArray();
ks.load(new FileInputStream(System.getProperty("javax.net.ssl.keyStore")), passphrase);
ts.load(new FileInputStream(System.getProperty("javax.net.ssl.trustStore")), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
servers = new Server [threads];
for (int i=0; i