Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/HttpConnection.java
41159 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.httpserver;2627import java.io.*;28import javax.net.ssl.*;29import java.nio.channels.*;30import java.lang.System.Logger;31import java.lang.System.Logger.Level;32import com.sun.net.httpserver.*;33import com.sun.net.httpserver.spi.*;3435/**36* encapsulates all the connection specific state for a HTTP/S connection37* one of these is hung from the selector attachment and is used to locate38* everything from that.39*/40class HttpConnection {4142HttpContextImpl context;43SSLEngine engine;44SSLContext sslContext;45SSLStreams sslStreams;4647/* high level streams returned to application */48InputStream i;4950/* low level stream that sits directly over channel */51InputStream raw;52OutputStream rawout;5354SocketChannel chan;55SelectionKey selectionKey;56String protocol;57long time;58volatile long creationTime; // time this connection was created59volatile long rspStartedTime; // time we started writing the response60int remaining;61boolean closed = false;62Logger logger;6364public enum State {IDLE, REQUEST, RESPONSE};65volatile State state;6667public String toString() {68String s = null;69if (chan != null) {70s = chan.toString();71}72return s;73}7475HttpConnection () {76}7778void setChannel (SocketChannel c) {79chan = c;80}8182void setContext (HttpContextImpl ctx) {83context = ctx;84}8586State getState() {87return state;88}8990void setState (State s) {91state = s;92}9394void setParameters (95InputStream in, OutputStream rawout, SocketChannel chan,96SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol,97HttpContextImpl context, InputStream raw98)99{100this.context = context;101this.i = in;102this.rawout = rawout;103this.raw = raw;104this.protocol = protocol;105this.engine = engine;106this.chan = chan;107this.sslContext = sslContext;108this.sslStreams = sslStreams;109this.logger = context.getLogger();110}111112SocketChannel getChannel () {113return chan;114}115116synchronized void close () {117if (closed) {118return;119}120closed = true;121if (logger != null && chan != null) {122logger.log (Level.TRACE, "Closing connection: " + chan.toString());123}124125if (!chan.isOpen()) {126ServerImpl.dprint ("Channel already closed");127return;128}129try {130/* need to ensure temporary selectors are closed */131if (raw != null) {132raw.close();133}134} catch (IOException e) {135ServerImpl.dprint (e);136}137try {138if (rawout != null) {139rawout.close();140}141} catch (IOException e) {142ServerImpl.dprint (e);143}144try {145if (sslStreams != null) {146sslStreams.close();147}148} catch (IOException e) {149ServerImpl.dprint (e);150}151try {152chan.close();153} catch (IOException e) {154ServerImpl.dprint (e);155}156}157158/* remaining is the number of bytes left on the lowest level inputstream159* after the exchange is finished160*/161void setRemaining (int r) {162remaining = r;163}164165int getRemaining () {166return remaining;167}168169SelectionKey getSelectionKey () {170return selectionKey;171}172173InputStream getInputStream () {174return i;175}176177OutputStream getRawOutputStream () {178return rawout;179}180181String getProtocol () {182return protocol;183}184185SSLEngine getSSLEngine () {186return engine;187}188189SSLContext getSSLContext () {190return sslContext;191}192193HttpContextImpl getHttpContext () {194return context;195}196}197198199