jdk/src/share/classes/sun/net/httpserver/HttpConnection.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7271 17d3fc18872d
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * encapsulates all the connection specific state for a HTTP/S connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * one of these is hung from the selector attachment and is used to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * everything from that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class HttpConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    HttpContextImpl context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    SSLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    SSLContext sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    SSLStreams sslStreams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* high level streams returned to application */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    InputStream i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* low level stream that sits directly over channel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    InputStream raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    OutputStream rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    SelectionKey selectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    String protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    long time;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    58
    volatile long creationTime; // time this connection was created
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    59
    volatile long rspStartedTime; // time we started writing the response
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    64
    public enum State {IDLE, REQUEST, RESPONSE};
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    65
    volatile State state;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        String s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (chan != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            s = chan.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    HttpConnection () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    void setChannel (SocketChannel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        chan = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    void setContext (HttpContextImpl ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        context = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    86
    State getState() {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    87
        return state;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    88
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    89
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    90
    void setState (State s) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    91
        state = s;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    92
    }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    void setParameters (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        InputStream in, OutputStream rawout, SocketChannel chan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        HttpContextImpl context, InputStream raw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.i = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.rawout = rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.raw = raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.sslContext = sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.sslStreams = sslStreams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.logger = context.getLogger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    SocketChannel getChannel () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    synchronized void close () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (logger != null && chan != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            logger.finest ("Closing connection: " + chan.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (!chan.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ServerImpl.dprint ("Channel already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            /* need to ensure temporary selectors are closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (raw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                raw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (rawout != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                rawout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (sslStreams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                sslStreams.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            chan.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /* remaining is the number of bytes left on the lowest level inputstream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * after the exchange is finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    void setRemaining (int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        remaining = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    int getRemaining () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    SelectionKey getSelectionKey () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return selectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    InputStream getInputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    OutputStream getRawOutputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    String getProtocol () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    SSLEngine getSSLEngine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    SSLContext getSSLContext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    HttpContextImpl getHttpContext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}