0N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.net.httpserver;
0N/A
0N/Aimport java.io.*;
0N/Aimport javax.net.ssl.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.util.logging.Logger;
0N/Aimport com.sun.net.httpserver.*;
0N/Aimport com.sun.net.httpserver.spi.*;
0N/A
0N/A/**
0N/A * encapsulates all the connection specific state for a HTTP/S connection
0N/A * one of these is hung from the selector attachment and is used to locate
0N/A * everything from that.
0N/A */
0N/Aclass HttpConnection {
0N/A
0N/A HttpContextImpl context;
0N/A SSLEngine engine;
0N/A SSLContext sslContext;
0N/A SSLStreams sslStreams;
0N/A
0N/A /* high level streams returned to application */
0N/A InputStream i;
0N/A
0N/A /* low level stream that sits directly over channel */
0N/A InputStream raw;
0N/A OutputStream rawout;
0N/A
0N/A SocketChannel chan;
0N/A SelectionKey selectionKey;
0N/A String protocol;
0N/A long time;
3105N/A volatile long creationTime; // time this connection was created
3105N/A volatile long rspStartedTime; // time we started writing the response
0N/A int remaining;
0N/A boolean closed = false;
0N/A Logger logger;
0N/A
3105N/A public enum State {IDLE, REQUEST, RESPONSE};
3105N/A volatile State state;
3105N/A
0N/A public String toString() {
0N/A String s = null;
0N/A if (chan != null) {
0N/A s = chan.toString();
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A HttpConnection () {
0N/A }
0N/A
0N/A void setChannel (SocketChannel c) {
0N/A chan = c;
0N/A }
0N/A
0N/A void setContext (HttpContextImpl ctx) {
0N/A context = ctx;
0N/A }
0N/A
3105N/A State getState() {
3105N/A return state;
3105N/A }
3105N/A
3105N/A void setState (State s) {
3105N/A state = s;
3105N/A }
3105N/A
0N/A void setParameters (
0N/A InputStream in, OutputStream rawout, SocketChannel chan,
0N/A SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol,
0N/A HttpContextImpl context, InputStream raw
0N/A )
0N/A {
0N/A this.context = context;
0N/A this.i = in;
0N/A this.rawout = rawout;
0N/A this.raw = raw;
0N/A this.protocol = protocol;
0N/A this.engine = engine;
0N/A this.chan = chan;
0N/A this.sslContext = sslContext;
0N/A this.sslStreams = sslStreams;
0N/A this.logger = context.getLogger();
0N/A }
0N/A
0N/A SocketChannel getChannel () {
0N/A return chan;
0N/A }
0N/A
0N/A synchronized void close () {
0N/A if (closed) {
0N/A return;
0N/A }
0N/A closed = true;
0N/A if (logger != null && chan != null) {
0N/A logger.finest ("Closing connection: " + chan.toString());
0N/A }
0N/A
0N/A if (!chan.isOpen()) {
0N/A ServerImpl.dprint ("Channel already closed");
0N/A return;
0N/A }
0N/A try {
0N/A /* need to ensure temporary selectors are closed */
0N/A if (raw != null) {
0N/A raw.close();
0N/A }
0N/A } catch (IOException e) {
0N/A ServerImpl.dprint (e);
0N/A }
0N/A try {
0N/A if (rawout != null) {
0N/A rawout.close();
0N/A }
0N/A } catch (IOException e) {
0N/A ServerImpl.dprint (e);
0N/A }
0N/A try {
0N/A if (sslStreams != null) {
0N/A sslStreams.close();
0N/A }
0N/A } catch (IOException e) {
0N/A ServerImpl.dprint (e);
0N/A }
0N/A try {
0N/A chan.close();
0N/A } catch (IOException e) {
0N/A ServerImpl.dprint (e);
0N/A }
0N/A }
0N/A
0N/A /* remaining is the number of bytes left on the lowest level inputstream
0N/A * after the exchange is finished
0N/A */
0N/A void setRemaining (int r) {
0N/A remaining = r;
0N/A }
0N/A
0N/A int getRemaining () {
0N/A return remaining;
0N/A }
0N/A
0N/A SelectionKey getSelectionKey () {
0N/A return selectionKey;
0N/A }
0N/A
0N/A InputStream getInputStream () {
0N/A return i;
0N/A }
0N/A
0N/A OutputStream getRawOutputStream () {
0N/A return rawout;
0N/A }
0N/A
0N/A String getProtocol () {
0N/A return protocol;
0N/A }
0N/A
0N/A SSLEngine getSSLEngine () {
0N/A return engine;
0N/A }
0N/A
0N/A SSLContext getSSLContext () {
0N/A return sslContext;
0N/A }
0N/A
0N/A HttpContextImpl getHttpContext () {
0N/A return context;
0N/A }
0N/A}