src/jdk.httpserver/share/classes/sun/net/httpserver/HttpConnection.java
author ssadetsky
Mon, 16 Oct 2017 08:06:56 -0700
changeset 47387 4d711a58bb3b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8180821: [macosx]com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java: The menu item should have been GC-ed Reviewed-by: prr, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7271
diff changeset
     2
 * Copyright (c) 2005, 2010, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
41592
855537e5ad9c 8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents: 25859
diff changeset
    31
import java.lang.System.Logger;
855537e5ad9c 8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents: 25859
diff changeset
    32
import java.lang.System.Logger.Level;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * encapsulates all the connection specific state for a HTTP/S connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * one of these is hung from the selector attachment and is used to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * everything from that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class HttpConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    HttpContextImpl context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    SSLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    SSLContext sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    SSLStreams sslStreams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* high level streams returned to application */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    InputStream i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* low level stream that sits directly over channel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    InputStream raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    OutputStream rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    SelectionKey selectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    long time;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    59
    volatile long creationTime; // time this connection was created
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    60
    volatile long rspStartedTime; // time we started writing the response
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    65
    public enum State {IDLE, REQUEST, RESPONSE};
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    66
    volatile State state;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (chan != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            s = chan.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    HttpConnection () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    void setChannel (SocketChannel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        chan = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    void setContext (HttpContextImpl ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        context = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    87
    State getState() {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    88
        return state;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    89
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    90
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    91
    void setState (State s) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    92
        state = s;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    93
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    void setParameters (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        InputStream in, OutputStream rawout, SocketChannel chan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        HttpContextImpl context, InputStream raw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.i = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.rawout = rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.raw = raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.sslContext = sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.sslStreams = sslStreams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.logger = context.getLogger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    SocketChannel getChannel () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    synchronized void close () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (logger != null && chan != null) {
41592
855537e5ad9c 8157965: update httpserver logging to use java.lang.System.Logger
dfuchs
parents: 25859
diff changeset
   123
            logger.log (Level.TRACE, "Closing connection: " + chan.toString());
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
        if (!chan.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ServerImpl.dprint ("Channel already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            /* need to ensure temporary selectors are closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (raw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                raw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (rawout != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                rawout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (sslStreams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                sslStreams.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            chan.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /* remaining is the number of bytes left on the lowest level inputstream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * after the exchange is finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    void setRemaining (int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        remaining = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    int getRemaining () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    SelectionKey getSelectionKey () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return selectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    InputStream getInputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    OutputStream getRawOutputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    String getProtocol () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    SSLEngine getSSLEngine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    SSLContext getSSLContext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    HttpContextImpl getHttpContext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}