jdk/src/share/classes/sun/net/httpserver/ServerImpl.java
author michaelm
Tue, 10 Mar 2009 03:18:22 -0700
changeset 2612 d7fb0809c7e4
parent 2 90ce3da70b43
child 2624 1ae5a9028dd4
permissions -rw-r--r--
6630639: lightweight HttpServer leaks file descriptors on no-data connections Summary: not cleaning up no-data connections properly Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 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 java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Provides implementation for both HTTP and HTTPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class ServerImpl implements TimeSource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private boolean https;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Executor executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private HttpsConfigurator httpsConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private SSLContext sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private ContextList contexts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private InetSocketAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private ServerSocketChannel schan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private SelectionKey listenerKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Set<HttpConnection> idleConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Set<HttpConnection> allConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private List<Event> events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Object lolock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private volatile boolean finished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private volatile boolean terminating = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean started = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private volatile long time;  /* current time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private volatile long ticks; /* number of clock ticks since server started */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private HttpServer wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    final static int CLOCK_TICK = ServerConfig.getClockTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    final static long IDLE_INTERVAL = ServerConfig.getIdleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    final static int MAX_IDLE_CONNECTIONS = ServerConfig.getMaxIdleConnections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    ServerImpl (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        HttpServer wrapper, String protocol, InetSocketAddress addr, int backlog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    ) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.wrapper = wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.logger = Logger.getLogger ("com.sun.net.httpserver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        https = protocol.equalsIgnoreCase ("https");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.address = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        contexts = new ContextList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        schan = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            ServerSocket socket = schan.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            socket.bind (addr, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        selector = Selector.open ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        schan.configureBlocking (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        dispatcher = new Dispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        idleConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        allConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        timer = new Timer ("server-timer", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        timer.schedule (new ServerTimerTask(), CLOCK_TICK, CLOCK_TICK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        events = new LinkedList<Event>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        logger.config ("HttpServer created "+protocol+" "+ addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void bind (InetSocketAddress addr, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (bound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new BindException ("HttpServer already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new NullPointerException ("null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ServerSocket socket = schan.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        socket.bind (addr, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void start () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (!bound || started || finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new IllegalStateException ("server in wrong state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (executor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            executor = new DefaultExecutor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        started = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        final Dispatcher d = dispatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Thread t = AccessController.doPrivileged(new PrivilegedAction<Thread>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            public Thread run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                Thread t = new Thread (d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void setExecutor (Executor executor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (started) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalStateException ("server already started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.executor = executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static class DefaultExecutor implements Executor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public void execute (Runnable task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            task.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public Executor getExecutor () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void setHttpsConfigurator (HttpsConfigurator config) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (config == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new NullPointerException ("null HttpsConfigurator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (started) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new IllegalStateException ("server already started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.httpsConfig = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        sslContext = config.getSSLContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public HttpsConfigurator getHttpsConfigurator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return httpsConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public void stop (int delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (delay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new IllegalArgumentException ("negative delay parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        terminating = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try { schan.close(); } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        selector.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        long latest = System.currentTimeMillis() + delay * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        while (System.currentTimeMillis() < latest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            delay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        selector.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        synchronized (allConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            for (HttpConnection c : allConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        allConnections.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        idleConnections.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        timer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    Dispatcher dispatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public synchronized HttpContextImpl createContext (String path, HttpHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (handler == null || path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new NullPointerException ("null handler, or path parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        HttpContextImpl context = new HttpContextImpl (protocol, path, handler, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        contexts.add (context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        logger.config ("context created: " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public synchronized HttpContextImpl createContext (String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new NullPointerException ("null path parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        HttpContextImpl context = new HttpContextImpl (protocol, path, null, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        contexts.add (context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        logger.config ("context created: " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public synchronized void removeContext (String path) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new NullPointerException ("null path parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        contexts.remove (protocol, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        logger.config ("context removed: " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public synchronized void removeContext (HttpContext context) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!(context instanceof HttpContextImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IllegalArgumentException ("wrong HttpContext type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        contexts.remove ((HttpContextImpl)context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        logger.config ("context removed: " + context.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public InetSocketAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return (InetSocketAddress)schan.socket().getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    Selector getSelector () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    void addEvent (Event r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        synchronized (lolock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            events.add (r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            selector.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    int resultSize () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        synchronized (lolock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return events.size ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /* main server listener task */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    class Dispatcher implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        private void handleEvent (Event r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            ExchangeImpl t = r.exchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            HttpConnection c = t.getConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (r instanceof WriteFinishedEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    int exchanges = endExchange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    if (terminating && exchanges == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    SocketChannel chan = c.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    LeftOverInputStream is = t.getOriginalInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    if (!is.isEOF()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        t.close = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    if (t.close || idleConnections.size() >= MAX_IDLE_CONNECTIONS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        allConnections.remove (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        if (is.isDataBuffered()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                            /* don't re-enable the interestops, just handle it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            handle (c.getChannel(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                            /* re-enable interestops */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            SelectionKey key = c.getSelectionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                            if (key.isValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                key.interestOps (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                    key.interestOps()|SelectionKey.OP_READ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            c.time = getTime() + IDLE_INTERVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            idleConnections.add (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                logger.log (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    Level.FINER, "Dispatcher (1)", e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            while (!finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    /* process the events list first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    while (resultSize() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        Event r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        synchronized (lolock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            r = events.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            handleEvent (r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    selector.select(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    /* process the selected list now  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    Set<SelectionKey> selected = selector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    Iterator<SelectionKey> iter = selected.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        SelectionKey key = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        iter.remove ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        if (key.equals (listenerKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            if (terminating) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                            SocketChannel chan = schan.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            if (chan == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                continue; /* cancel something ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                            chan.configureBlocking (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            SelectionKey newkey = chan.register (selector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                            HttpConnection c = new HttpConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                            c.selectionKey = newkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                            c.setChannel (chan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                            newkey.attach (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            allConnections.add (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                if (key.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                    boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                    SocketChannel chan = (SocketChannel)key.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                    HttpConnection conn = (HttpConnection)key.attachment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                    // interestOps will be restored at end of read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                    key.interestOps (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                    handle (chan, conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                HttpConnection conn = (HttpConnection)key.attachment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                logger.log (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                    Level.FINER, "Dispatcher (2)", e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                conn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                } catch (CancelledKeyException  e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    logger.log (Level.FINER, "Dispatcher (3)", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    logger.log (Level.FINER, "Dispatcher (4)", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        public void handle (SocketChannel chan, HttpConnection conn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                Exchange t = new Exchange (chan, protocol, conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                executor.execute (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            } catch (HttpError e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                logger.log (Level.FINER, "Dispatcher (5)", e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                conn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                logger.log (Level.FINER, "Dispatcher (6)", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                conn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    static boolean debug = ServerConfig.debugEnabled ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    static synchronized void dprint (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            System.out.println (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    static synchronized void dprint (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    Logger getLogger () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /* per exchange task */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    class Exchange implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        HttpConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        HttpContextImpl context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        InputStream rawin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        OutputStream rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        ExchangeImpl tx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        HttpContextImpl ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        boolean rejected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        Exchange (SocketChannel chan, String protocol, HttpConnection conn) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            this.connection = conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            /* context will be null for new connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            context = connection.getHttpContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            boolean newconnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            SSLEngine engine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            String requestLine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            SSLStreams sslStreams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (context != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    this.rawin = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    this.rawout = connection.getRawOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    newconnection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    /* figure out what kind of connection this is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    newconnection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    if (https) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        if (sslContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                            logger.warning ("SSL connection received. No https contxt created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            throw new HttpError ("No SSL context established");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        sslStreams = new SSLStreams (ServerImpl.this, sslContext, chan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        rawin = sslStreams.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        rawout = sslStreams.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        engine = sslStreams.getSSLEngine();
2612
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
   444
                        connection.sslStreams = sslStreams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        rawin = new BufferedInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            new Request.ReadStream (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                ServerImpl.this, chan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        rawout = new Request.WriteStream (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                            ServerImpl.this, chan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
2612
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
   454
                    connection.raw = rawin;
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
   455
                    connection.rawout = rawout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                Request req = new Request (rawin, rawout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                requestLine = req.requestLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                if (requestLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    /* connection closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                int space = requestLine.indexOf (' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if (space == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            requestLine, "Bad request line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                String method = requestLine.substring (0, space);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                int start = space+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                space = requestLine.indexOf(' ', start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (space == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            requestLine, "Bad request line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                String uriStr = requestLine.substring (start, space);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                URI uri = new URI (uriStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                start = space+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                String version = requestLine.substring (start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                Headers headers = req.headers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                String s = headers.getFirst ("Transfer-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                int clen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (s !=null && s.equalsIgnoreCase ("chunked")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    clen = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    s = headers.getFirst ("Content-Length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        clen = Integer.parseInt (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                ctx = contexts.findContext (protocol, uri.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if (ctx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    reject (Code.HTTP_NOT_FOUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                            requestLine, "No context found for request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                connection.setContext (ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (ctx.getHandler() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    reject (Code.HTTP_INTERNAL_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            requestLine, "No handler for context");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                tx = new ExchangeImpl (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    method, uri, req, clen, connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                String chdr = headers.getFirst("Connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                Headers rheaders = tx.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if (chdr != null && chdr.equalsIgnoreCase ("close")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    tx.close = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (version.equalsIgnoreCase ("http/1.0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    tx.http10 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    if (chdr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        tx.close = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        rheaders.set ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    } else if (chdr.equalsIgnoreCase ("keep-alive")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        rheaders.set ("Connection", "keep-alive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        int idle=(int)ServerConfig.getIdleInterval()/1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        int max=(int)ServerConfig.getMaxIdleConnections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        String val = "timeout="+idle+", max="+max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        rheaders.set ("Keep-Alive", val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                if (newconnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    connection.setParameters (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        rawin, rawout, chan, engine, sslStreams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        sslContext, protocol, ctx, rawin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                /* check if client sent an Expect 100 Continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                 * In that case, need to send an interim response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                 * In future API may be modified to allow app to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                 * be involved in this process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                String exp = headers.getFirst("Expect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (exp != null && exp.equalsIgnoreCase ("100-continue")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    logReply (100, requestLine, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    sendReply (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        Code.HTTP_CONTINUE, false, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                /* uf is the list of filters seen/set by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                 * sf is the list of filters established internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                 * and which are not visible to the user. uc and sc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                 * are the corresponding Filter.Chains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                 * They are linked together by a LinkHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                 * so that they can both be invoked in one call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                List<Filter> sf = ctx.getSystemFilters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                List<Filter> uf = ctx.getFilters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                /* set up the two stream references */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                tx.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                tx.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                if (https) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    uc.doFilter (new HttpsExchangeImpl (tx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    uc.doFilter (new HttpExchangeImpl (tx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            } catch (IOException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                logger.log (Level.FINER, "ServerImpl.Exchange (1)", e1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            } catch (NumberFormatException e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        requestLine, "NumberFormatException thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        requestLine, "URISyntaxException thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            } catch (Exception e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                logger.log (Level.FINER, "ServerImpl.Exchange (2)", e4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        /* used to link to 2 or more Filter.Chains together */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        class LinkHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            Filter.Chain nextChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            LinkHandler (Filter.Chain nextChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                this.nextChain = nextChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            public void handle (HttpExchange exchange) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                nextChain.doFilter (exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        void reject (int code, String requestStr, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            rejected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            logReply (code, requestStr, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            sendReply (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                code, true, "<h1>"+code+Code.msg(code)+"</h1>"+message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        void sendReply (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            int code, boolean closeNow, String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                String s = "HTTP/1.1 " + code + Code.msg(code) + "\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                if (text != null && text.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    s = s + "Content-Length: "+text.length()+"\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    s = s + "Content-Type: text/html\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    s = s + "Content-Length: 0\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                if (closeNow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    s = s + "Connection: close\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                s = s + "\r\n" + text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                byte[] b = s.getBytes("ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                rawout.write (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                rawout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if (closeNow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                logger.log (Level.FINER, "ServerImpl.sendReply", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    void logReply (int code, String requestStr, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        String message = requestStr + " [" + code + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    Code.msg(code) + "] ("+text+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        logger.fine (message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    long getTicks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        return ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    void delay () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            Thread.sleep (200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    private int exchangeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    synchronized void startExchange () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        exchangeCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    synchronized int endExchange () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        exchangeCount --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        assert exchangeCount >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        return exchangeCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    HttpServer getWrapper () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * TimerTask run every CLOCK_TICK ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    class ServerTimerTask extends TimerTask {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            ticks ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            synchronized (idleConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                for (HttpConnection c : idleConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    if (c.time <= time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        toClose.add (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                for (HttpConnection c : toClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    idleConnections.remove (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    allConnections.remove (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
}