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