Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/net/httpserver/HttpExchange.java
38922 views
/*1* Copyright (c) 2005, 2013, 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 com.sun.net.httpserver;2627import java.io.*;28import java.nio.*;29import java.nio.channels.*;30import java.net.*;31import javax.net.ssl.*;32import java.util.*;33import sun.net.www.MessageHeader;3435/**36* This class encapsulates a HTTP request received and a37* response to be generated in one exchange. It provides methods38* for examining the request from the client, and for building and39* sending the response.40* <p>41* The typical life-cycle of a HttpExchange is shown in the sequence42* below.43* <ol><li>{@link #getRequestMethod()} to determine the command44* <li>{@link #getRequestHeaders()} to examine the request headers (if needed)45* <li>{@link #getRequestBody()} returns a {@link java.io.InputStream} for reading the request body.46* After reading the request body, the stream is close.47* <li>{@link #getResponseHeaders()} to set any response headers, except content-length48* <li>{@link #sendResponseHeaders(int,long)} to send the response headers. Must be called before49* next step.50* <li>{@link #getResponseBody()} to get a {@link java.io.OutputStream} to send the response body.51* When the response body has been written, the stream must be closed to terminate the exchange.52* </ol>53* <b>Terminating exchanges</b>54* <br>55* Exchanges are terminated when both the request InputStream and response OutputStream are closed.56* Closing the OutputStream, implicitly closes the InputStream (if it is not already closed).57* However, it is recommended58* to consume all the data from the InputStream before closing it.59* The convenience method {@link #close()} does all of these tasks.60* Closing an exchange without consuming all of the request body is not an error61* but may make the underlying TCP connection unusable for following exchanges.62* The effect of failing to terminate an exchange is undefined, but will typically63* result in resources failing to be freed/reused.64* @since 1.665*/6667@jdk.Exported68public abstract class HttpExchange {6970protected HttpExchange () {71}7273/**74* Returns an immutable Map containing the HTTP headers that were75* included with this request. The keys in this Map will be the header76* names, while the values will be a List of Strings containing each value77* that was included (either for a header that was listed several times,78* or one that accepts a comma-delimited list of values on a single line).79* In either of these cases, the values for the header name will be80* presented in the order that they were included in the request.81* <p>82* The keys in Map are case-insensitive.83* @return a read-only Map which can be used to access request headers84*/85public abstract Headers getRequestHeaders () ;8687/**88* Returns a mutable Map into which the HTTP response headers can be stored89* and which will be transmitted as part of this response. The keys in the90* Map will be the header names, while the values must be a List of Strings91* containing each value that should be included multiple times92* (in the order that they should be included).93* <p>94* The keys in Map are case-insensitive.95* @return a writable Map which can be used to set response headers.96*/97public abstract Headers getResponseHeaders () ;9899/**100* Get the request URI101*102* @return the request URI103*/104public abstract URI getRequestURI () ;105106/**107* Get the request method108* @return the request method109*/110public abstract String getRequestMethod ();111112/**113* Get the HttpContext for this exchange114* @return the HttpContext115*/116public abstract HttpContext getHttpContext ();117118/**119* Ends this exchange by doing the following in sequence:<p><ol>120* <li>close the request InputStream, if not already closed<p></li>121* <li>close the response OutputStream, if not already closed. </li>122* </ol>123*/124public abstract void close () ;125126/**127* returns a stream from which the request body can be read.128* Multiple calls to this method will return the same stream.129* It is recommended that applications should consume (read) all of the130* data from this stream before closing it. If a stream is closed131* before all data has been read, then the close() call will132* read and discard remaining data (up to an implementation specific133* number of bytes).134* @return the stream from which the request body can be read.135*/136public abstract InputStream getRequestBody () ;137138/**139* returns a stream to which the response body must be140* written. {@link #sendResponseHeaders(int,long)}) must be called prior to calling141* this method. Multiple calls to this method (for the same exchange)142* will return the same stream. In order to correctly terminate143* each exchange, the output stream must be closed, even if no144* response body is being sent.145* <p>146* Closing this stream implicitly147* closes the InputStream returned from {@link #getRequestBody()}148* (if it is not already closed).149* <P>150* If the call to sendResponseHeaders() specified a fixed response151* body length, then the exact number of bytes specified in that152* call must be written to this stream. If too many bytes are written,153* then write() will throw an IOException. If too few bytes are written154* then the stream close() will throw an IOException. In both cases,155* the exchange is aborted and the underlying TCP connection closed.156* @return the stream to which the response body is written157*/158public abstract OutputStream getResponseBody () ;159160161/**162* Starts sending the response back to the client using the current set of response headers163* and the numeric response code as specified in this method. The response body length is also specified164* as follows. If the response length parameter is greater than zero, this specifies an exact165* number of bytes to send and the application must send that exact amount of data.166* If the response length parameter is <code>zero</code>, then chunked transfer encoding is167* used and an arbitrary amount of data may be sent. The application terminates the168* response body by closing the OutputStream. If response length has the value <code>-1</code>169* then no response body is being sent.170* <p>171* If the content-length response header has not already been set then172* this is set to the appropriate value depending on the response length parameter.173* <p>174* This method must be called prior to calling {@link #getResponseBody()}.175* @param rCode the response code to send176* @param responseLength if > 0, specifies a fixed response body length177* and that exact number of bytes must be written178* to the stream acquired from getResponseBody(), or else179* if equal to 0, then chunked encoding is used,180* and an arbitrary number of bytes may be written.181* if <= -1, then no response body length is specified and182* no response body may be written.183* @see HttpExchange#getResponseBody()184*/185public abstract void sendResponseHeaders (int rCode, long responseLength) throws IOException ;186187/**188* Returns the address of the remote entity invoking this request189* @return the InetSocketAddress of the caller190*/191public abstract InetSocketAddress getRemoteAddress ();192193/**194* Returns the response code, if it has already been set195* @return the response code, if available. <code>-1</code> if not available yet.196*/197public abstract int getResponseCode ();198199/**200* Returns the local address on which the request was received201* @return the InetSocketAddress of the local interface202*/203public abstract InetSocketAddress getLocalAddress ();204205/**206* Returns the protocol string from the request in the form207* <i>protocol/majorVersion.minorVersion</i>. For example,208* "HTTP/1.1"209* @return the protocol string from the request210*/211public abstract String getProtocol ();212213/**214* Filter modules may store arbitrary objects with HttpExchange215* instances as an out-of-band communication mechanism. Other Filters216* or the exchange handler may then access these objects.217* <p>218* Each Filter class will document the attributes which they make219* available.220* @param name the name of the attribute to retrieve221* @return the attribute object, or null if it does not exist222* @throws NullPointerException if name is <code>null</code>223*/224public abstract Object getAttribute (String name) ;225226/**227* Filter modules may store arbitrary objects with HttpExchange228* instances as an out-of-band communication mechanism. Other Filters229* or the exchange handler may then access these objects.230* <p>231* Each Filter class will document the attributes which they make232* available.233* @param name the name to associate with the attribute value234* @param value the object to store as the attribute value. <code>null</code>235* value is permitted.236* @throws NullPointerException if name is <code>null</code>237*/238public abstract void setAttribute (String name, Object value) ;239240/**241* Used by Filters to wrap either (or both) of this exchange's InputStream242* and OutputStream, with the given filtered streams so243* that subsequent calls to {@link #getRequestBody()} will244* return the given {@link java.io.InputStream}, and calls to245* {@link #getResponseBody()} will return the given246* {@link java.io.OutputStream}. The streams provided to this247* call must wrap the original streams, and may be (but are not248* required to be) sub-classes of {@link java.io.FilterInputStream}249* and {@link java.io.FilterOutputStream}.250* @param i the filtered input stream to set as this object's inputstream,251* or <code>null</code> if no change.252* @param o the filtered output stream to set as this object's outputstream,253* or <code>null</code> if no change.254*/255public abstract void setStreams (InputStream i, OutputStream o);256257258/**259* If an authenticator is set on the HttpContext that owns this exchange,260* then this method will return the {@link HttpPrincipal} that represents261* the authenticated user for this HttpExchange.262* @return the HttpPrincipal, or <code>null</code> if no authenticator is set.263*/264public abstract HttpPrincipal getPrincipal ();265}266267268