jdk/src/share/classes/sun/net/httpserver/HttpConnection.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    int remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    Logger logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (chan != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            s = chan.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    HttpConnection () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void setChannel (SocketChannel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        chan = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    void setContext (HttpContextImpl ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        context = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    void setParameters (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        InputStream in, OutputStream rawout, SocketChannel chan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        HttpContextImpl context, InputStream raw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.i = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.rawout = rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.raw = raw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.protocol = protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.sslContext = sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.sslStreams = sslStreams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.logger = context.getLogger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    SocketChannel getChannel () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    synchronized void close () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (logger != null && chan != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            logger.finest ("Closing connection: " + chan.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!chan.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            ServerImpl.dprint ("Channel already closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            /* need to ensure temporary selectors are closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (raw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                raw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (rawout != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                rawout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (sslStreams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                sslStreams.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
            chan.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            ServerImpl.dprint (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /* remaining is the number of bytes left on the lowest level inputstream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * after the exchange is finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    void setRemaining (int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        remaining = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    int getRemaining () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    SelectionKey getSelectionKey () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return selectionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    InputStream getInputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    OutputStream getRawOutputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    String getProtocol () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    SSLEngine getSSLEngine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    SSLContext getSSLContext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    HttpContextImpl getHttpContext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}