/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*/
/**
* 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)
* <p>
* It must be instantiated with a {@link HttpCallback} object to which
* requests are given and must be handled.
* <p>
* 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 {
int threads;
int cperthread;
// ssl related fields
/**
* Create a <code>HttpServer<code> 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
*/
}
/**
* Create a <code>HttpServer<code> 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
*/
throws IOException {
}
/**
* Create a <code>HttpServer<code> 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. <code>Zero</code>
* means choose any free port.
*/
throws IOException {
this.cperthread = cperthread;
try {
// create and initialize a SSLContext
for (int i=0; i<threads; i++) {
}
}
}
/** Tell all threads in the server to exit within 5 seconds.
* This is an abortive termination. Just prior to the thread exiting
* all channels in that thread waiting to be closed are forceably closed.
*/
public void terminate () {
for (int i=0; i<threads; i++) {
}
}
/**
* return the local port number to which the server is bound.
* @return the local port number
*/
public int getLocalPort () {
}
int maxconn;
int nconn;
boolean shutdown;
nconn = 0;
clist = new ClosedChannelList ();
try {
schan.configureBlocking (false);
} catch (IOException e) {
}
}
/* Stop the thread as soon as possible */
public synchronized void terminate () {
shutdown = true;
}
public void run () {
try {
while (true) {
/* false notification */
continue;
}
sock.configureBlocking (true);
sslEng.setUseClientMode(false);
nconn ++;
/* deregister */
listenerKey.cancel ();
listenerKey = null;
}
} else {
if (key.isReadable()) {
boolean closed = false;
}
if (closed) {
}
nconn --;
}
}
}
}
synchronized (this) {
if (shutdown) {
return;
}
}
}
} catch (IOException e) {
// TODO finish
}
}
/* read all the data off the channel without looking at it
* return true if connection closed
*/
try {
consumeBuffer.clear ();
if (c == -1)
return true;
} catch (IOException e) {
return true;
}
return false;
}
}
boolean initialHSComplete;
/*
* All inbound data goes through this buffer.
*
* It might be nice to use a cache of ByteBuffers so we're
*/
/*
* Application buffers, also used for handshaking
*/
private int appBBSize;
initialHSComplete = false;
}
return sslEng;
}
return outNetBB;
}
return outAppBB;
}
public void run () {
try {
while (!initialHSComplete) {
switch (currentHSStatus) {
case NEED_UNWRAP:
/*
* Don't need to resize requestBB, since no app data should
* be generated here.
*/
case OK:
switch (currentHSStatus) {
case NOT_HANDSHAKING:
throw new IOException(
"Not handshaking during initial handshake");
case NEED_TASK:
}
break;
}
break;
case BUFFER_UNDERFLOW:
break needIO;
default: // BUFFER_OVERFLOW/CLOSED:
"during initial handshaking");
}
}
/*
* Just transitioned from read to write.
*/
break;
}
// Fall through and fill the write buffer.
case NEED_WRAP:
/*
* The flush above guarantees the out buffer to be empty
*/
case OK:
}
}
break;
default: // BUFFER_OVERFLOW/BUFFER_UNDERFLOW/CLOSED:
"during initial handshaking");
}
break;
case FINISHED:
initialHSComplete = true;
break;
default: // NOT_HANDSHAKING/NEED_TASK
throw new RuntimeException("Invalid Handshaking State" +
} // switch
}
// read the application data; using non-blocking mode
schan.configureBlocking(false);
throw new RuntimeException(ex);
}
}
/* return true if the connection is closed, false otherwise */
boolean res;
try {
/* invalid request line */
return false;
}
try {
} catch (URISyntaxException e) {
}
res = false;
} catch (IOException e) {
res = true;
}
return res;
}
remain -= c;
off += c;
}
return buf;
}
throw new IOException(
}
}
LinkedList l = new LinkedList ();
int total = 0;
}
int x = 0;
while (i.hasNext()) {
byte[] b = (byte[])i.next();
x += b.length;
}
return buf;
}
int c, len=0;
while (!done) {
if (c == '\n' && readCR) {
done = true;
} else {
if (c == '\r' && !readCR) {
readCR = true;
} else {
int x=0;
if (c >= 'a' && c <= 'f') {
x = c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
x = c - 'A' + 10;
} else if (c >= '0' && c <= '9') {
x = c - '0';
}
}
}
}
return len;
}
byte[] b = new byte [512];
int c, l = 0;
while (!done) {
if (c == '\n' && readCR) {
done = true;
} else {
if (c == '\r' && !readCR) {
readCR = true;
} else {
b[l++] = (byte)c;
}
}
}
return new String (b);
}
/** close the channel associated with the current key by:
* 1. shutdownOutput (send a FIN)
* 2. mark the key so that incoming data is to be consumed and discarded
* 3. After a period, close the socket
*/
}
s.setSoLinger (true, 0);
}
}
/**
* Implements blocking reading semantics on top of a non-blocking channel
*/
int available;
byte[] one;
boolean closed;
boolean marked;
boolean reset;
int readlimit;
public NioInputStream (SocketChannel chan, SSLEngine sslEng, ByteBuffer inNetBB, ByteBuffer inAppBB) throws IOException {
available = 0;
one = new byte[1];
}
}
}
int canreturn, willreturn;
if (closed)
return -1;
if (reset) { /* satisfy from markBuf */
if (canreturn == willreturn) {
reset = false;
}
} else { /* satisfy from channel */
if (canreturn == 0) {
block ();
}
available -= willreturn;
if (marked) { /* copy into markBuf */
try {
} catch (BufferOverflowException e) {
marked = false;
}
}
}
return willreturn;
}
if (closed)
throw new IOException ("Stream is closed");
if (reset)
if (available > 0)
return available;
}
if (available > 0)
else if (available == -1)
throw new IOException ("Stream is closed");
return available;
}
/**
* block() only called when available==0 and buf is empty
*/
//assert available == 0;
//assert n == 1;
available ();
}
if (closed)
return;
closed = true;
}
if (closed)
return;
marked = true;
reset = false;
}
if (closed )
return;
if (!marked)
throw new IOException ("Stream not marked");
marked = false;
reset = true;
}
}
boolean closed;
byte[] one;
public NioOutputStream (SocketChannel channel, SSLEngine sslEng, ByteBuffer outNetBB, ByteBuffer outAppBB) throws IOException {
closed = false;
one = new byte [1];
}
one[0] = (byte)b;
}
}
if (closed)
throw new IOException ("stream is closed");
int n;
}
newLen -= n;
if (newLen == 0)
return;
}
}
if (closed)
return;
closed = true;
}
}
/**
* Utilities for synchronization. A condition is
* identified by a string name, and is initialized
* upon first use (ie. setCondition() or waitForCondition()). Threads
* are blocked until some thread calls (or has called) setCondition() for the same
* condition.
* <P>
* A rendezvous built on a condition is also provided for synchronizing
* N threads.
*/
/*
* Modifiable boolean object
*/
private static class BValue {
boolean v;
}
/*
* Modifiable int object
*/
private static class IValue {
int v;
IValue (int i) {
v =i;
}
}
synchronized (conditions) {
}
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.
*/
synchronized (cond) {
if (cond.v) {
return;
}
cond.v = true;
}
}
/**
* 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.
*/
synchronized (cond) {
if (!cond.v) {
try {
} catch (InterruptedException e) {}
}
}
}
/* conditions must be locked when accessing this */
/**
* 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.
* <p>
* 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).
* <P>
* Obviously, if fewer than N threads make the rendezvous then the result
* will be a hang.
*/
/* get the condition */
synchronized (conditions) {
/* we are first caller */
if (N < 2) {
throw new RuntimeException ("rendezvous must be called with N >= 2");
}
} else {
/* already initialised, just decrement the counter */
iv.v --;
}
}
if (iv.v > 0) {
} else {
setCondition (name);
synchronized (conditions) {
}
}
}
/**
* 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.
*/
synchronized (conditions) {
return;
}
synchronized (cond) {
if (cond.v) {
}
}
}
}
}