Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/httpserver/HttpConnection.java
38918 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.util.logging.Logger;31import com.sun.net.httpserver.*;32import com.sun.net.httpserver.spi.*;3334/**35* encapsulates all the connection specific state for a HTTP/S connection36* one of these is hung from the selector attachment and is used to locate37* everything from that.38*/39class HttpConnection {4041HttpContextImpl context;42SSLEngine engine;43SSLContext sslContext;44SSLStreams sslStreams;4546/* high level streams returned to application */47InputStream i;4849/* low level stream that sits directly over channel */50InputStream raw;51OutputStream rawout;5253SocketChannel chan;54SelectionKey selectionKey;55String protocol;56long time;57volatile long creationTime; // time this connection was created58volatile long rspStartedTime; // time we started writing the response59int remaining;60boolean closed = false;61Logger logger;6263public enum State {IDLE, REQUEST, RESPONSE};64volatile State state;6566public String toString() {67String s = null;68if (chan != null) {69s = chan.toString();70}71return s;72}7374HttpConnection () {75}7677void setChannel (SocketChannel c) {78chan = c;79}8081void setContext (HttpContextImpl ctx) {82context = ctx;83}8485State getState() {86return state;87}8889void setState (State s) {90state = s;91}9293void setParameters (94InputStream in, OutputStream rawout, SocketChannel chan,95SSLEngine engine, SSLStreams sslStreams, SSLContext sslContext, String protocol,96HttpContextImpl context, InputStream raw97)98{99this.context = context;100this.i = in;101this.rawout = rawout;102this.raw = raw;103this.protocol = protocol;104this.engine = engine;105this.chan = chan;106this.sslContext = sslContext;107this.sslStreams = sslStreams;108this.logger = context.getLogger();109}110111SocketChannel getChannel () {112return chan;113}114115synchronized void close () {116if (closed) {117return;118}119closed = true;120if (logger != null && chan != null) {121logger.finest ("Closing connection: " + chan.toString());122}123124if (!chan.isOpen()) {125ServerImpl.dprint ("Channel already closed");126return;127}128try {129/* need to ensure temporary selectors are closed */130if (raw != null) {131raw.close();132}133} catch (IOException e) {134ServerImpl.dprint (e);135}136try {137if (rawout != null) {138rawout.close();139}140} catch (IOException e) {141ServerImpl.dprint (e);142}143try {144if (sslStreams != null) {145sslStreams.close();146}147} catch (IOException e) {148ServerImpl.dprint (e);149}150try {151chan.close();152} catch (IOException e) {153ServerImpl.dprint (e);154}155}156157/* remaining is the number of bytes left on the lowest level inputstream158* after the exchange is finished159*/160void setRemaining (int r) {161remaining = r;162}163164int getRemaining () {165return remaining;166}167168SelectionKey getSelectionKey () {169return selectionKey;170}171172InputStream getInputStream () {173return i;174}175176OutputStream getRawOutputStream () {177return rawout;178}179180String getProtocol () {181return protocol;182}183184SSLEngine getSSLEngine () {185return engine;186}187188SSLContext getSSLContext () {189return sslContext;190}191192HttpContextImpl getHttpContext () {193return context;194}195}196197198