Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/www/http/ChunkedInputStream.java
38923 views
/*1* Copyright (c) 1999, 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*/24package sun.net.www.http;2526import java.io.*;27import java.util.*;2829import sun.net.*;30import sun.net.www.*;3132/**33* A <code>ChunkedInputStream</code> provides a stream for reading a body of34* a http message that can be sent as a series of chunks, each with its own35* size indicator. Optionally the last chunk can be followed by trailers36* containing entity-header fields.37* <p>38* A <code>ChunkedInputStream</code> is also <code>Hurryable</code> so it39* can be hurried to the end of the stream if the bytes are available on40* the underlying stream.41*/42public43class ChunkedInputStream extends InputStream implements Hurryable {4445/**46* The underlying stream47*/48private InputStream in;4950/**51* The <code>HttpClient</code> that should be notified when the chunked stream has52* completed.53*/54private HttpClient hc;5556/**57* The <code>MessageHeader</code> that is populated with any optional trailer58* that appear after the last chunk.59*/60private MessageHeader responses;6162/**63* The size, in bytes, of the chunk that is currently being read.64* This size is only valid if the current position in the underlying65* input stream is inside a chunk (ie: state == STATE_READING_CHUNK).66*/67private int chunkSize;6869/**70* The number of bytes read from the underlying stream for the current71* chunk. This value is always in the range <code>0</code> through to72* <code>chunkSize</code>73*/74private int chunkRead;7576/**77* The internal buffer array where chunk data is available for the78* application to read.79*/80private byte chunkData[] = new byte[4096];8182/**83* The current position in the buffer. It contains the index84* of the next byte to read from <code>chunkData</code>85*/86private int chunkPos;8788/**89* The index one greater than the index of the last valid byte in the90* buffer. This value is always in the range <code>0</code> through91* <code>chunkData.length</code>.92*/93private int chunkCount;9495/**96* The internal buffer where bytes from the underlying stream can be97* read. It may contain bytes representing chunk-size, chunk-data, or98* trailer fields.99*/100private byte rawData[] = new byte[32];101102/**103* The current position in the buffer. It contains the index104* of the next byte to read from <code>rawData</code>105*/106private int rawPos;107108/**109* The index one greater than the index of the last valid byte in the110* buffer. This value is always in the range <code>0</code> through111* <code>rawData.length</code>.112*/113private int rawCount;114115/**116* Indicates if an error was encountered when processing the chunked117* stream.118*/119private boolean error;120121/**122* Indicates if the chunked stream has been closed using the123* <code>close</code> method.124*/125private boolean closed;126127/*128* Maximum chunk header size of 2KB + 2 bytes for CRLF129*/130private final static int MAX_CHUNK_HEADER_SIZE = 2050;131132/**133* State to indicate that next field should be :-134* chunk-size [ chunk-extension ] CRLF135*/136static final int STATE_AWAITING_CHUNK_HEADER = 1;137138/**139* State to indicate that we are currently reading the chunk-data.140*/141static final int STATE_READING_CHUNK = 2;142143/**144* Indicates that a chunk has been completely read and the next145* fields to be examine should be CRLF146*/147static final int STATE_AWAITING_CHUNK_EOL = 3;148149/**150* Indicates that all chunks have been read and the next field151* should be optional trailers or an indication that the chunked152* stream is complete.153*/154static final int STATE_AWAITING_TRAILERS = 4;155156/**157* State to indicate that the chunked stream is complete and158* no further bytes should be read from the underlying stream.159*/160static final int STATE_DONE = 5;161162/**163* Indicates the current state.164*/165private int state;166167168/**169* Check to make sure that this stream has not been closed.170*/171private void ensureOpen() throws IOException {172if (closed) {173throw new IOException("stream is closed");174}175}176177178/**179* Ensures there is <code>size</code> bytes available in180* <code>rawData</code>. This requires that we either181* shift the bytes in use to the begining of the buffer182* or allocate a large buffer with sufficient space available.183*/184private void ensureRawAvailable(int size) {185if (rawCount + size > rawData.length) {186int used = rawCount - rawPos;187if (used + size > rawData.length) {188byte tmp[] = new byte[used + size];189if (used > 0) {190System.arraycopy(rawData, rawPos, tmp, 0, used);191}192rawData = tmp;193} else {194if (used > 0) {195System.arraycopy(rawData, rawPos, rawData, 0, used);196}197}198rawCount = used;199rawPos = 0;200}201}202203204/**205* Close the underlying input stream by either returning it to the206* keep alive cache or closing the stream.207* <p>208* As a chunked stream is inheritly persistent (see HTTP 1.1 RFC) the209* underlying stream can be returned to the keep alive cache if the210* stream can be completely read without error.211*/212private void closeUnderlying() throws IOException {213if (in == null) {214return;215}216217if (!error && state == STATE_DONE) {218hc.finished();219} else {220if (!hurry()) {221hc.closeServer();222}223}224225in = null;226}227228/**229* Attempt to read the remainder of a chunk directly into the230* caller's buffer.231* <p>232* Return the number of bytes read.233*/234private int fastRead(byte[] b, int off, int len) throws IOException {235236// assert state == STATE_READING_CHUNKS;237238int remaining = chunkSize - chunkRead;239int cnt = (remaining < len) ? remaining : len;240if (cnt > 0) {241int nread;242try {243nread = in.read(b, off, cnt);244} catch (IOException e) {245error = true;246throw e;247}248if (nread > 0) {249chunkRead += nread;250if (chunkRead >= chunkSize) {251state = STATE_AWAITING_CHUNK_EOL;252}253return nread;254}255error = true;256throw new IOException("Premature EOF");257} else {258return 0;259}260}261262/**263* Process any outstanding bytes that have already been read into264* <code>rawData</code>.265* <p>266* The parsing of the chunked stream is performed as a state machine with267* <code>state</code> representing the current state of the processing.268* <p>269* Returns when either all the outstanding bytes in rawData have been270* processed or there is insufficient bytes available to continue271* processing. When the latter occurs <code>rawPos</code> will not have272* been updated and thus the processing can be restarted once further273* bytes have been read into <code>rawData</code>.274*/275private void processRaw() throws IOException {276int pos;277int i;278279while (state != STATE_DONE) {280281switch (state) {282283/**284* We are awaiting a line with a chunk header285*/286case STATE_AWAITING_CHUNK_HEADER:287/*288* Find \n to indicate end of chunk header. If not found when there is289* insufficient bytes in the raw buffer to parse a chunk header.290*/291pos = rawPos;292while (pos < rawCount) {293if (rawData[pos] == '\n') {294break;295}296pos++;297if ((pos - rawPos) >= MAX_CHUNK_HEADER_SIZE) {298error = true;299throw new IOException("Chunk header too long");300}301}302if (pos >= rawCount) {303return;304}305306/*307* Extract the chunk size from the header (ignoring extensions).308*/309String header = new String(rawData, rawPos, pos-rawPos+1, "US-ASCII");310for (i=0; i < header.length(); i++) {311if (Character.digit(header.charAt(i), 16) == -1)312break;313}314try {315chunkSize = Integer.parseInt(header.substring(0, i), 16);316} catch (NumberFormatException e) {317error = true;318throw new IOException("Bogus chunk size");319}320321/*322* Chunk has been parsed so move rawPos to first byte of chunk323* data.324*/325rawPos = pos + 1;326chunkRead = 0;327328/*329* A chunk size of 0 means EOF.330*/331if (chunkSize > 0) {332state = STATE_READING_CHUNK;333} else {334state = STATE_AWAITING_TRAILERS;335}336break;337338339/**340* We are awaiting raw entity data (some may have already been341* read). chunkSize is the size of the chunk; chunkRead is the342* total read from the underlying stream to date.343*/344case STATE_READING_CHUNK :345/* no data available yet */346if (rawPos >= rawCount) {347return;348}349350/*351* Compute the number of bytes of chunk data available in the352* raw buffer.353*/354int copyLen = Math.min( chunkSize-chunkRead, rawCount-rawPos );355356/*357* Expand or compact chunkData if needed.358*/359if (chunkData.length < chunkCount + copyLen) {360int cnt = chunkCount - chunkPos;361if (chunkData.length < cnt + copyLen) {362byte tmp[] = new byte[cnt + copyLen];363System.arraycopy(chunkData, chunkPos, tmp, 0, cnt);364chunkData = tmp;365} else {366System.arraycopy(chunkData, chunkPos, chunkData, 0, cnt);367}368chunkPos = 0;369chunkCount = cnt;370}371372/*373* Copy the chunk data into chunkData so that it's available374* to the read methods.375*/376System.arraycopy(rawData, rawPos, chunkData, chunkCount, copyLen);377rawPos += copyLen;378chunkCount += copyLen;379chunkRead += copyLen;380381/*382* If all the chunk has been copied into chunkData then the next383* token should be CRLF.384*/385if (chunkSize - chunkRead <= 0) {386state = STATE_AWAITING_CHUNK_EOL;387} else {388return;389}390break;391392393/**394* Awaiting CRLF after the chunk395*/396case STATE_AWAITING_CHUNK_EOL:397/* not available yet */398if (rawPos + 1 >= rawCount) {399return;400}401402if (rawData[rawPos] != '\r') {403error = true;404throw new IOException("missing CR");405}406if (rawData[rawPos+1] != '\n') {407error = true;408throw new IOException("missing LF");409}410rawPos += 2;411412/*413* Move onto the next chunk414*/415state = STATE_AWAITING_CHUNK_HEADER;416break;417418419/**420* Last chunk has been read so not we're waiting for optional421* trailers.422*/423case STATE_AWAITING_TRAILERS:424425/*426* Do we have an entire line in the raw buffer?427*/428pos = rawPos;429while (pos < rawCount) {430if (rawData[pos] == '\n') {431break;432}433pos++;434}435if (pos >= rawCount) {436return;437}438439if (pos == rawPos) {440error = true;441throw new IOException("LF should be proceeded by CR");442}443if (rawData[pos-1] != '\r') {444error = true;445throw new IOException("LF should be proceeded by CR");446}447448/*449* Stream done so close underlying stream.450*/451if (pos == (rawPos + 1)) {452453state = STATE_DONE;454closeUnderlying();455456return;457}458459/*460* Extract any tailers and append them to the message461* headers.462*/463String trailer = new String(rawData, rawPos, pos-rawPos, "US-ASCII");464i = trailer.indexOf(':');465if (i == -1) {466throw new IOException("Malformed tailer - format should be key:value");467}468String key = (trailer.substring(0, i)).trim();469String value = (trailer.substring(i+1, trailer.length())).trim();470471responses.add(key, value);472473/*474* Move onto the next trailer.475*/476rawPos = pos+1;477break;478479} /* switch */480}481}482483484/**485* Reads any available bytes from the underlying stream into486* <code>rawData</code> and returns the number of bytes of487* chunk data available in <code>chunkData</code> that the488* application can read.489*/490private int readAheadNonBlocking() throws IOException {491492/*493* If there's anything available on the underlying stream then we read494* it into the raw buffer and process it. Processing ensures that any495* available chunk data is made available in chunkData.496*/497int avail = in.available();498if (avail > 0) {499500/* ensure that there is space in rawData to read the available */501ensureRawAvailable(avail);502503int nread;504try {505nread = in.read(rawData, rawCount, avail);506} catch (IOException e) {507error = true;508throw e;509}510if (nread < 0) {511error = true; /* premature EOF ? */512return -1;513}514rawCount += nread;515516/*517* Process the raw bytes that have been read.518*/519processRaw();520}521522/*523* Return the number of chunked bytes available to read524*/525return chunkCount - chunkPos;526}527528/**529* Reads from the underlying stream until there is chunk data530* available in <code>chunkData</code> for the application to531* read.532*/533private int readAheadBlocking() throws IOException {534535do {536/*537* All of chunked response has been read to return EOF.538*/539if (state == STATE_DONE) {540return -1;541}542543/*544* We must read into the raw buffer so make sure there is space545* available. We use a size of 32 to avoid too much chunk data546* being read into the raw buffer.547*/548ensureRawAvailable(32);549int nread;550try {551nread = in.read(rawData, rawCount, rawData.length-rawCount);552} catch (IOException e) {553error = true;554throw e;555}556557/**558* If we hit EOF it means there's a problem as we should never559* attempt to read once the last chunk and trailers have been560* received.561*/562if (nread < 0) {563error = true;564throw new IOException("Premature EOF");565}566567/**568* Process the bytes from the underlying stream569*/570rawCount += nread;571processRaw();572573} while (chunkCount <= 0);574575/*576* Return the number of chunked bytes available to read577*/578return chunkCount - chunkPos;579}580581/**582* Read ahead in either blocking or non-blocking mode. This method583* is typically used when we run out of available bytes in584* <code>chunkData</code> or we need to determine how many bytes585* are available on the input stream.586*/587private int readAhead(boolean allowBlocking) throws IOException {588589/*590* Last chunk already received - return EOF591*/592if (state == STATE_DONE) {593return -1;594}595596/*597* Reset position/count if data in chunkData is exhausted.598*/599if (chunkPos >= chunkCount) {600chunkCount = 0;601chunkPos = 0;602}603604/*605* Read ahead blocking or non-blocking606*/607if (allowBlocking) {608return readAheadBlocking();609} else {610return readAheadNonBlocking();611}612}613614/**615* Creates a <code>ChunkedInputStream</code> and saves its arguments, for616* later use.617*618* @param in the underlying input stream.619* @param hc the HttpClient620* @param responses the MessageHeader that should be populated with optional621* trailers.622*/623public ChunkedInputStream(InputStream in, HttpClient hc, MessageHeader responses) throws IOException {624625/* save arguments */626this.in = in;627this.responses = responses;628this.hc = hc;629630/*631* Set our initial state to indicate that we are first starting to632* look for a chunk header.633*/634state = STATE_AWAITING_CHUNK_HEADER;635}636637/**638* See639* the general contract of the <code>read</code>640* method of <code>InputStream</code>.641*642* @return the next byte of data, or <code>-1</code> if the end of the643* stream is reached.644* @exception IOException if an I/O error occurs.645* @see java.io.FilterInputStream#in646*/647public synchronized int read() throws IOException {648ensureOpen();649if (chunkPos >= chunkCount) {650if (readAhead(true) <= 0) {651return -1;652}653}654return chunkData[chunkPos++] & 0xff;655}656657658/**659* Reads bytes from this stream into the specified byte array, starting at660* the given offset.661*662* @param b destination buffer.663* @param off offset at which to start storing bytes.664* @param len maximum number of bytes to read.665* @return the number of bytes read, or <code>-1</code> if the end of666* the stream has been reached.667* @exception IOException if an I/O error occurs.668*/669public synchronized int read(byte b[], int off, int len)670throws IOException671{672ensureOpen();673if ((off < 0) || (off > b.length) || (len < 0) ||674((off + len) > b.length) || ((off + len) < 0)) {675throw new IndexOutOfBoundsException();676} else if (len == 0) {677return 0;678}679680int avail = chunkCount - chunkPos;681if (avail <= 0) {682/*683* Optimization: if we're in the middle of the chunk read684* directly from the underlying stream into the caller's685* buffer686*/687if (state == STATE_READING_CHUNK) {688return fastRead( b, off, len );689}690691/*692* We're not in the middle of a chunk so we must read ahead693* until there is some chunk data available.694*/695avail = readAhead(true);696if (avail < 0) {697return -1; /* EOF */698}699}700int cnt = (avail < len) ? avail : len;701System.arraycopy(chunkData, chunkPos, b, off, cnt);702chunkPos += cnt;703704return cnt;705}706707/**708* Returns the number of bytes that can be read from this input709* stream without blocking.710*711* @return the number of bytes that can be read from this input712* stream without blocking.713* @exception IOException if an I/O error occurs.714* @see java.io.FilterInputStream#in715*/716public synchronized int available() throws IOException {717ensureOpen();718719int avail = chunkCount - chunkPos;720if(avail > 0) {721return avail;722}723724avail = readAhead(false);725726if (avail < 0) {727return 0;728} else {729return avail;730}731}732733/**734* Close the stream by either returning the connection to the735* keep alive cache or closing the underlying stream.736* <p>737* If the chunked response hasn't been completely read we738* try to "hurry" to the end of the response. If this is739* possible (without blocking) then the connection can be740* returned to the keep alive cache.741*742* @exception IOException if an I/O error occurs.743*/744public synchronized void close() throws IOException {745if (closed) {746return;747}748closeUnderlying();749closed = true;750}751752/**753* Hurry the input stream by reading everything from the underlying754* stream. If the last chunk (and optional trailers) can be read without755* blocking then the stream is considered hurried.756* <p>757* Note that if an error has occurred or we can't get to last chunk758* without blocking then this stream can't be hurried and should be759* closed.760*/761public synchronized boolean hurry() {762if (in == null || error) {763return false;764}765766try {767readAhead(false);768} catch (Exception e) {769return false;770}771772if (error) {773return false;774}775776return (state == STATE_DONE);777}778779}780781782