jdk/src/share/classes/sun/net/httpserver/ServerImpl.java
author michaelm
Thu, 10 Nov 2011 15:30:45 +0000
changeset 11014 456742d63643
parent 10596 39b3a979e600
child 17463 9392f1567896
child 18212 22f8c33b0690
permissions -rw-r--r--
7110484: HttpServer.stop() not closing selector Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10130
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5463
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5463
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5463
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5463
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5463
diff changeset
    23
 * questions.
2
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.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.net.httpserver.*;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    37
import sun.net.httpserver.HttpConnection.State;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Provides implementation for both HTTP and HTTPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class ServerImpl implements TimeSource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private boolean https;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Executor executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private HttpsConfigurator httpsConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private SSLContext sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ContextList contexts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private InetSocketAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private ServerSocketChannel schan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private SelectionKey listenerKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Set<HttpConnection> idleConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Set<HttpConnection> allConnections;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    56
    /* following two are used to keep track of the times
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    57
     * when a connection/request is first received
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    58
     * and when we start to send the response
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    59
     */
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    60
    private Set<HttpConnection> reqConnections;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    61
    private Set<HttpConnection> rspConnections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private List<Event> events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Object lolock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private volatile boolean finished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private volatile boolean terminating = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean started = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private volatile long time;  /* current time */
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    69
    private volatile long subticks = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private volatile long ticks; /* number of clock ticks since server started */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private HttpServer wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    final static int CLOCK_TICK = ServerConfig.getClockTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    final static long IDLE_INTERVAL = ServerConfig.getIdleInterval();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    final static int MAX_IDLE_CONNECTIONS = ServerConfig.getMaxIdleConnections();
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    76
    final static long TIMER_MILLIS = ServerConfig.getTimerMillis ();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    77
    final static long MAX_REQ_TIME=getTimeMillis(ServerConfig.getMaxReqTime());
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    78
    final static long MAX_RSP_TIME=getTimeMillis(ServerConfig.getMaxRspTime());
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    79
    final static boolean timer1Enabled = MAX_REQ_TIME != -1 || MAX_RSP_TIME != -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    81
    private Timer timer, timer1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    ServerImpl (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        HttpServer wrapper, String protocol, InetSocketAddress addr, int backlog
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    ) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.wrapper = wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.logger = Logger.getLogger ("com.sun.net.httpserver");
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    91
        ServerConfig.checkLegacyProperties (logger);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        https = protocol.equalsIgnoreCase ("https");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.address = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        contexts = new ContextList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        schan = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ServerSocket socket = schan.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            socket.bind (addr, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        selector = Selector.open ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        schan.configureBlocking (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        dispatcher = new Dispatcher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        idleConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        allConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   107
        reqConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   108
        rspConnections = Collections.synchronizedSet (new HashSet<HttpConnection>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        timer = new Timer ("server-timer", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        timer.schedule (new ServerTimerTask(), CLOCK_TICK, CLOCK_TICK);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   112
        if (timer1Enabled) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   113
            timer1 = new Timer ("server-timer1", true);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   114
            timer1.schedule (new ServerTimerTask1(),TIMER_MILLIS,TIMER_MILLIS);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   115
            logger.config ("HttpServer timer1 enabled period in ms:  "+TIMER_MILLIS);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   116
            logger.config ("MAX_REQ_TIME:  "+MAX_REQ_TIME);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   117
            logger.config ("MAX_RSP_TIME:  "+MAX_RSP_TIME);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   118
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        events = new LinkedList<Event>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        logger.config ("HttpServer created "+protocol+" "+ addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void bind (InetSocketAddress addr, int backlog) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (bound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new BindException ("HttpServer already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new NullPointerException ("null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        ServerSocket socket = schan.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        socket.bind (addr, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void start () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (!bound || started || finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new IllegalStateException ("server in wrong state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (executor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            executor = new DefaultExecutor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
905
8445a646acfb 6536211: flaw in ServerImpl
michaelm
parents: 2
diff changeset
   142
        Thread t = new Thread (dispatcher);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        started = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        t.start();
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 void setExecutor (Executor executor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (started) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new IllegalStateException ("server already started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.executor = executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static class DefaultExecutor implements Executor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public void execute (Runnable task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            task.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
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 Executor getExecutor () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void setHttpsConfigurator (HttpsConfigurator config) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (config == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new NullPointerException ("null HttpsConfigurator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (started) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new IllegalStateException ("server already started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.httpsConfig = config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        sslContext = config.getSSLContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public HttpsConfigurator getHttpsConfigurator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return httpsConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void stop (int delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (delay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalArgumentException ("negative delay parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        terminating = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        try { schan.close(); } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        selector.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        long latest = System.currentTimeMillis() + delay * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        while (System.currentTimeMillis() < latest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            delay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        selector.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        synchronized (allConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (HttpConnection c : allConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        allConnections.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        idleConnections.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        timer.cancel();
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   203
        if (timer1Enabled) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   204
            timer1.cancel();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   205
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    Dispatcher dispatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public synchronized HttpContextImpl createContext (String path, HttpHandler handler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (handler == null || path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new NullPointerException ("null handler, or path parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        HttpContextImpl context = new HttpContextImpl (protocol, path, handler, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        contexts.add (context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        logger.config ("context created: " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public synchronized HttpContextImpl createContext (String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new NullPointerException ("null path parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        HttpContextImpl context = new HttpContextImpl (protocol, path, null, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        contexts.add (context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        logger.config ("context created: " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return context;
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 synchronized void removeContext (String path) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (path == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new NullPointerException ("null path parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        contexts.remove (protocol, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        logger.config ("context removed: " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public synchronized void removeContext (HttpContext context) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!(context instanceof HttpContextImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new IllegalArgumentException ("wrong HttpContext type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        contexts.remove ((HttpContextImpl)context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        logger.config ("context removed: " + context.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public InetSocketAddress getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return (InetSocketAddress)schan.socket().getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    Selector getSelector () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    void addEvent (Event r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        synchronized (lolock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            events.add (r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            selector.wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /* main server listener task */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    class Dispatcher implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        private void handleEvent (Event r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            ExchangeImpl t = r.exchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            HttpConnection c = t.getConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (r instanceof WriteFinishedEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    int exchanges = endExchange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    if (terminating && exchanges == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        finished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   275
                    responseCompleted (c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    LeftOverInputStream is = t.getOriginalInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    if (!is.isEOF()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        t.close = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (t.close || idleConnections.size() >= MAX_IDLE_CONNECTIONS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        allConnections.remove (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        if (is.isDataBuffered()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                            /* don't re-enable the interestops, just handle it */
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   286
                            requestStarted (c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            handle (c.getChannel(), c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        } else {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   289
                            connsToRegister.add (c);
2
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
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                logger.log (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    Level.FINER, "Dispatcher (1)", e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   301
        final LinkedList<HttpConnection> connsToRegister =
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   302
                new LinkedList<HttpConnection>();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   303
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   304
        void reRegister (HttpConnection c) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   305
            /* re-register with selector */
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   306
            try {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   307
                SocketChannel chan = c.getChannel();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   308
                chan.configureBlocking (false);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   309
                SelectionKey key = chan.register (selector, SelectionKey.OP_READ);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   310
                key.attach (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   311
                c.selectionKey = key;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   312
                c.time = getTime() + IDLE_INTERVAL;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   313
                idleConnections.add (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   314
            } catch (IOException e) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   315
                dprint(e);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   316
                logger.log(Level.FINER, "Dispatcher(8)", e);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   317
                c.close();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   318
            }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   319
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   320
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            while (!finished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                try {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   324
                    ListIterator<HttpConnection> li =
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   325
                        connsToRegister.listIterator();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   326
                    for (HttpConnection c : connsToRegister) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   327
                        reRegister(c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   328
                    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   329
                    connsToRegister.clear();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   331
                    List<Event> list = null;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   332
                    selector.select(1000);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   333
                    synchronized (lolock) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   334
                        if (events.size() > 0) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   335
                            list = events;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   336
                            events = new LinkedList<Event>();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   337
                        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   338
                    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   339
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   340
                    if (list != null) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   341
                        for (Event r: list) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            handleEvent (r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    /* process the selected list now  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    Set<SelectionKey> selected = selector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    Iterator<SelectionKey> iter = selected.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        SelectionKey key = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        iter.remove ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        if (key.equals (listenerKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            if (terminating) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                            SocketChannel chan = schan.accept();
10130
254e206a89d8 7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents: 7668
diff changeset
   358
254e206a89d8 7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents: 7668
diff changeset
   359
                            // Set TCP_NODELAY, if appropriate
254e206a89d8 7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents: 7668
diff changeset
   360
                            if (ServerConfig.noDelay()) {
254e206a89d8 7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents: 7668
diff changeset
   361
                                chan.socket().setTcpNoDelay(true);
254e206a89d8 7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents: 7668
diff changeset
   362
                            }
254e206a89d8 7068416: Lightweight HTTP Server should support TCP_NODELAY
chegar
parents: 7668
diff changeset
   363
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            if (chan == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                continue; /* cancel something ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            chan.configureBlocking (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                            SelectionKey newkey = chan.register (selector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            HttpConnection c = new HttpConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            c.selectionKey = newkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            c.setChannel (chan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            newkey.attach (c);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   373
                            requestStarted (c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            allConnections.add (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                if (key.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                    boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                    SocketChannel chan = (SocketChannel)key.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                    HttpConnection conn = (HttpConnection)key.attachment();
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   381
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   382
                                    key.cancel();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   383
                                    chan.configureBlocking (true);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   384
                                    if (idleConnections.remove(conn)) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   385
                                        // was an idle connection so add it
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   386
                                        // to reqConnections set.
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   387
                                        requestStarted (conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   388
                                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                    handle (chan, conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   393
                            } catch (CancelledKeyException e) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   394
                                handleException(key, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            } catch (IOException e) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   396
                                handleException(key, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   400
                    // call the selector just to process the cancelled keys
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   401
                    selector.selectNow();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   402
                } catch (IOException e) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   403
                    logger.log (Level.FINER, "Dispatcher (4)", e);
907
11f377f9319d 6728076: Test case for 6536211 is failing on all platforms
michaelm
parents: 905
diff changeset
   404
                } catch (Exception e) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   405
                    logger.log (Level.FINER, "Dispatcher (7)", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
11014
456742d63643 7110484: HttpServer.stop() not closing selector
michaelm
parents: 10596
diff changeset
   408
            try {selector.close(); } catch (Exception e) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   411
        private void handleException (SelectionKey key, Exception e) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   412
            HttpConnection conn = (HttpConnection)key.attachment();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   413
            if (e != null) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   414
                logger.log (Level.FINER, "Dispatcher (2)", e);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   415
            }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   416
            closeConnection(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   417
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   418
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        public void handle (SocketChannel chan, HttpConnection conn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                Exchange t = new Exchange (chan, protocol, conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                executor.execute (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            } catch (HttpError e1) {
907
11f377f9319d 6728076: Test case for 6536211 is failing on all platforms
michaelm
parents: 905
diff changeset
   426
                logger.log (Level.FINER, "Dispatcher (4)", e1);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   427
                closeConnection(conn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            } catch (IOException e) {
907
11f377f9319d 6728076: Test case for 6536211 is failing on all platforms
michaelm
parents: 905
diff changeset
   429
                logger.log (Level.FINER, "Dispatcher (5)", e);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   430
                closeConnection(conn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    static boolean debug = ServerConfig.debugEnabled ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    static synchronized void dprint (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            System.out.println (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    static synchronized void dprint (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    Logger getLogger () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   454
    private void closeConnection(HttpConnection conn) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   455
        conn.close();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   456
        allConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   457
        switch (conn.getState()) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   458
        case REQUEST:
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   459
            reqConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   460
            break;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   461
        case RESPONSE:
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   462
            rspConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   463
            break;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   464
        case IDLE:
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   465
            idleConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   466
            break;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   467
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   468
        assert !reqConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   469
        assert !rspConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   470
        assert !idleConnections.remove(conn);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   471
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   472
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   473
        /* per exchange task */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    class Exchange implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        HttpConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        HttpContextImpl context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        InputStream rawin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        OutputStream rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        ExchangeImpl tx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        HttpContextImpl ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        boolean rejected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        Exchange (SocketChannel chan, String protocol, HttpConnection conn) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            this.connection = conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            /* context will be null for new connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            context = connection.getHttpContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            boolean newconnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            SSLEngine engine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            String requestLine = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            SSLStreams sslStreams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (context != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    this.rawin = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    this.rawout = connection.getRawOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    newconnection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    /* figure out what kind of connection this is */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    newconnection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    if (https) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        if (sslContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            logger.warning ("SSL connection received. No https contxt created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                            throw new HttpError ("No SSL context established");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        sslStreams = new SSLStreams (ServerImpl.this, sslContext, chan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        rawin = sslStreams.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        rawout = sslStreams.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        engine = sslStreams.getSSLEngine();
2612
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
   516
                        connection.sslStreams = sslStreams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        rawin = new BufferedInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                            new Request.ReadStream (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                ServerImpl.this, chan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        rawout = new Request.WriteStream (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            ServerImpl.this, chan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    }
2612
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
   526
                    connection.raw = rawin;
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
   527
                    connection.rawout = rawout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                Request req = new Request (rawin, rawout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                requestLine = req.requestLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                if (requestLine == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    /* connection closed */
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   533
                    closeConnection(connection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                int space = requestLine.indexOf (' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (space == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                            requestLine, "Bad request line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                String method = requestLine.substring (0, space);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                int start = space+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                space = requestLine.indexOf(' ', start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (space == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                            requestLine, "Bad request line");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                String uriStr = requestLine.substring (start, space);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                URI uri = new URI (uriStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                start = space+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                String version = requestLine.substring (start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                Headers headers = req.headers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                String s = headers.getFirst ("Transfer-encoding");
1511
65ddd8f149f3 6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents: 1247
diff changeset
   556
                long clen = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                if (s !=null && s.equalsIgnoreCase ("chunked")) {
1511
65ddd8f149f3 6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents: 1247
diff changeset
   558
                    clen = -1L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    s = headers.getFirst ("Content-Length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    if (s != null) {
1511
65ddd8f149f3 6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents: 1247
diff changeset
   562
                        clen = Long.parseLong(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   564
                    if (clen == 0) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   565
                        requestCompleted (connection);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   566
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                ctx = contexts.findContext (protocol, uri.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                if (ctx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    reject (Code.HTTP_NOT_FOUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                            requestLine, "No context found for request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                connection.setContext (ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                if (ctx.getHandler() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    reject (Code.HTTP_INTERNAL_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            requestLine, "No handler for context");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                tx = new ExchangeImpl (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    method, uri, req, clen, connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                String chdr = headers.getFirst("Connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                Headers rheaders = tx.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                if (chdr != null && chdr.equalsIgnoreCase ("close")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    tx.close = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                if (version.equalsIgnoreCase ("http/1.0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    tx.http10 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    if (chdr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        tx.close = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        rheaders.set ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    } else if (chdr.equalsIgnoreCase ("keep-alive")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        rheaders.set ("Connection", "keep-alive");
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10130
diff changeset
   596
                        int idle=(int)(ServerConfig.getIdleInterval()/1000);
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10130
diff changeset
   597
                        int max=ServerConfig.getMaxIdleConnections();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        String val = "timeout="+idle+", max="+max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        rheaders.set ("Keep-Alive", val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                if (newconnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    connection.setParameters (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        rawin, rawout, chan, engine, sslStreams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        sslContext, protocol, ctx, rawin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                /* check if client sent an Expect 100 Continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                 * In that case, need to send an interim response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                 * In future API may be modified to allow app to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                 * be involved in this process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                String exp = headers.getFirst("Expect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                if (exp != null && exp.equalsIgnoreCase ("100-continue")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    logReply (100, requestLine, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    sendReply (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        Code.HTTP_CONTINUE, false, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                /* uf is the list of filters seen/set by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                 * sf is the list of filters established internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                 * and which are not visible to the user. uc and sc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                 * are the corresponding Filter.Chains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                 * They are linked together by a LinkHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                 * so that they can both be invoked in one call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                List<Filter> sf = ctx.getSystemFilters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                List<Filter> uf = ctx.getFilters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                /* set up the two stream references */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                tx.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                tx.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (https) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    uc.doFilter (new HttpsExchangeImpl (tx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    uc.doFilter (new HttpExchangeImpl (tx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            } catch (IOException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                logger.log (Level.FINER, "ServerImpl.Exchange (1)", e1);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   645
                closeConnection(connection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            } catch (NumberFormatException e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        requestLine, "NumberFormatException thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                reject (Code.HTTP_BAD_REQUEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        requestLine, "URISyntaxException thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            } catch (Exception e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                logger.log (Level.FINER, "ServerImpl.Exchange (2)", e4);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   654
                closeConnection(connection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        /* used to link to 2 or more Filter.Chains together */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        class LinkHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            Filter.Chain nextChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            LinkHandler (Filter.Chain nextChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                this.nextChain = nextChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            public void handle (HttpExchange exchange) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                nextChain.doFilter (exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        void reject (int code, String requestStr, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            rejected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            logReply (code, requestStr, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            sendReply (
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   676
                code, false, "<h1>"+code+Code.msg(code)+"</h1>"+message
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            );
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   678
            closeConnection(connection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        void sendReply (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            int code, boolean closeNow, String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            try {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   685
                StringBuilder builder = new StringBuilder (512);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   686
                builder.append ("HTTP/1.1 ")
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   687
                    .append (code).append (Code.msg(code)).append ("\r\n");
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   688
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                if (text != null && text.length() != 0) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   690
                    builder.append ("Content-Length: ")
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   691
                        .append (text.length()).append ("\r\n")
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   692
                        .append ("Content-Type: text/html\r\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                } else {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   694
                    builder.append ("Content-Length: 0\r\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if (closeNow) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   698
                    builder.append ("Connection: close\r\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   700
                builder.append ("\r\n").append (text);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   701
                String s = builder.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                byte[] b = s.getBytes("ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                rawout.write (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                rawout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if (closeNow) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   706
                    closeConnection(connection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                logger.log (Level.FINER, "ServerImpl.sendReply", e);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   710
                closeConnection(connection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    void logReply (int code, String requestStr, String text) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   717
        if (!logger.isLoggable(Level.FINE)) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   718
            return;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   719
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   723
        String r;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   724
        if (requestStr.length() > 80) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   725
           r = requestStr.substring (0, 80) + "<TRUNCATED>";
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   726
        } else {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   727
           r = requestStr;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   728
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   729
        String message = r + " [" + code + " " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    Code.msg(code) + "] ("+text+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        logger.fine (message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    long getTicks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    void delay () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        Thread.yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            Thread.sleep (200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    private int exchangeCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    synchronized void startExchange () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        exchangeCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    synchronized int endExchange () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        exchangeCount --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        assert exchangeCount >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        return exchangeCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    HttpServer getWrapper () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        return wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   765
    void requestStarted (HttpConnection c) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   766
        c.creationTime = getTime();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   767
        c.setState (State.REQUEST);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   768
        reqConnections.add (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   769
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   770
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   771
    // called after a request has been completely read
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   772
    // by the server. This stops the timer which would
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   773
    // close the connection if the request doesn't arrive
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   774
    // quickly enough. It then starts the timer
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   775
    // that ensures the client reads the response in a timely
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   776
    // fashion.
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   777
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   778
    void requestCompleted (HttpConnection c) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   779
        assert c.getState() == State.REQUEST;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   780
        reqConnections.remove (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   781
        c.rspStartedTime = getTime();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   782
        rspConnections.add (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   783
        c.setState (State.RESPONSE);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   784
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   785
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   786
    // called after response has been sent
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   787
    void responseCompleted (HttpConnection c) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   788
        assert c.getState() == State.RESPONSE;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   789
        rspConnections.remove (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   790
        c.setState (State.IDLE);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   791
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   792
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * TimerTask run every CLOCK_TICK ms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    class ServerTimerTask extends TimerTask {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            ticks ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            synchronized (idleConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                for (HttpConnection c : idleConnections) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    if (c.time <= time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        toClose.add (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                for (HttpConnection c : toClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    idleConnections.remove (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    allConnections.remove (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   815
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   816
    class ServerTimerTask1 extends TimerTask {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   817
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   818
        // runs every TIMER_MILLIS
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   819
        public void run () {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   820
            LinkedList<HttpConnection> toClose = new LinkedList<HttpConnection>();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   821
            time = System.currentTimeMillis();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   822
            synchronized (reqConnections) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   823
                if (MAX_REQ_TIME != -1) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   824
                    for (HttpConnection c : reqConnections) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   825
                        if (c.creationTime + TIMER_MILLIS + MAX_REQ_TIME <= time) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   826
                            toClose.add (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   827
                        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   828
                    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   829
                    for (HttpConnection c : toClose) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   830
                        logger.log (Level.FINE, "closing: no request: " + c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   831
                        reqConnections.remove (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   832
                        allConnections.remove (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   833
                        c.close();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   834
                    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   835
                }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   836
            }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   837
            toClose = new LinkedList<HttpConnection>();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   838
            synchronized (rspConnections) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   839
                if (MAX_RSP_TIME != -1) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   840
                    for (HttpConnection c : rspConnections) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   841
                        if (c.rspStartedTime + TIMER_MILLIS +MAX_RSP_TIME <= time) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   842
                            toClose.add (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   843
                        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   844
                    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   845
                    for (HttpConnection c : toClose) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   846
                        logger.log (Level.FINE, "closing: no response: " + c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   847
                        rspConnections.remove (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   848
                        allConnections.remove (c);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   849
                        c.close();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   850
                    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   851
                }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   852
            }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   853
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   854
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   855
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   856
    void logStackTrace (String s) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   857
        logger.finest (s);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   858
        StringBuilder b = new StringBuilder ();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   859
        StackTraceElement[] e = Thread.currentThread().getStackTrace();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   860
        for (int i=0; i<e.length; i++) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   861
            b.append (e[i].toString()).append("\n");
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   862
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   863
        logger.finest (b.toString());
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   864
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   865
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   866
    static long getTimeMillis(long secs) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   867
        if (secs == -1) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   868
            return -1;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   869
        } else {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   870
            return secs * 1000;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   871
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   872
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
}