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