0N/A/*
3909N/A * Copyright (c) 2005, 2011, 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 java.net.*;
0N/Aimport com.sun.net.httpserver.*;
0N/Aimport com.sun.net.httpserver.spi.*;
0N/A
0N/Aclass ChunkedInputStream extends LeftOverInputStream {
0N/A ChunkedInputStream (ExchangeImpl t, InputStream src) {
0N/A super (t, src);
0N/A }
0N/A
0N/A private int remaining;
0N/A
0N/A /* true when a chunk header needs to be read */
0N/A
0N/A private boolean needToReadHeader = true;
0N/A
5683N/A final static char CR = '\r';
5683N/A final static char LF = '\n';
5683N/A /*
5683N/A * Maximum chunk header size of 2KB + 2 bytes for CRLF
5683N/A */
5683N/A private final static int MAX_CHUNK_HEADER_SIZE = 2050;
0N/A
0N/A private int numeric (char[] arr, int nchars) throws IOException {
0N/A assert arr.length >= nchars;
0N/A int len = 0;
0N/A for (int i=0; i<nchars; i++) {
0N/A char c = arr[i];
0N/A int val=0;
0N/A if (c>='0' && c <='9') {
0N/A val = c - '0';
0N/A } else if (c>='a' && c<= 'f') {
0N/A val = c - 'a' + 10;
0N/A } else if (c>='A' && c<= 'F') {
0N/A val = c - 'A' + 10;
0N/A } else {
0N/A throw new IOException ("invalid chunk length");
0N/A }
0N/A len = len * 16 + val;
0N/A }
0N/A return len;
0N/A }
0N/A
0N/A /* read the chunk header line and return the chunk length
0N/A * any chunk extensions are ignored
0N/A */
0N/A private int readChunkHeader () throws IOException {
0N/A boolean gotCR = false;
3644N/A int c;
0N/A char[] len_arr = new char [16];
0N/A int len_size = 0;
0N/A boolean end_of_len = false;
5683N/A int read = 0;
0N/A
3644N/A while ((c=in.read())!= -1) {
3644N/A char ch = (char) c;
5683N/A read++;
5683N/A if ((len_size == len_arr.length -1) ||
5683N/A (read > MAX_CHUNK_HEADER_SIZE))
5683N/A {
0N/A throw new IOException ("invalid chunk header");
0N/A }
0N/A if (gotCR) {
3644N/A if (ch == LF) {
0N/A int l = numeric (len_arr, len_size);
0N/A return l;
0N/A } else {
0N/A gotCR = false;
0N/A }
0N/A if (!end_of_len) {
3644N/A len_arr[len_size++] = ch;
0N/A }
0N/A } else {
3644N/A if (ch == CR) {
0N/A gotCR = true;
3644N/A } else if (ch == ';') {
0N/A end_of_len = true;
0N/A } else if (!end_of_len) {
3644N/A len_arr[len_size++] = ch;
0N/A }
0N/A }
0N/A }
0N/A throw new IOException ("end of stream reading chunk header");
0N/A }
0N/A
0N/A protected int readImpl (byte[]b, int off, int len) throws IOException {
0N/A if (eof) {
0N/A return -1;
0N/A }
0N/A if (needToReadHeader) {
0N/A remaining = readChunkHeader();
0N/A if (remaining == 0) {
0N/A eof = true;
0N/A consumeCRLF();
3105N/A t.getServerImpl().requestCompleted (t.getConnection());
0N/A return -1;
0N/A }
0N/A needToReadHeader = false;
0N/A }
0N/A if (len > remaining) {
0N/A len = remaining;
0N/A }
0N/A int n = in.read(b, off, len);
0N/A if (n > -1) {
0N/A remaining -= n;
0N/A }
0N/A if (remaining == 0) {
0N/A needToReadHeader = true;
0N/A consumeCRLF();
0N/A }
0N/A return n;
0N/A }
0N/A
0N/A private void consumeCRLF () throws IOException {
0N/A char c;
0N/A c = (char)in.read(); /* CR */
0N/A if (c != CR) {
0N/A throw new IOException ("invalid chunk end");
0N/A }
0N/A c = (char)in.read(); /* LF */
0N/A if (c != LF) {
0N/A throw new IOException ("invalid chunk end");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * returns the number of bytes available to read in the current chunk
0N/A * which may be less than the real amount, but we'll live with that
0N/A * limitation for the moment. It only affects potential efficiency
0N/A * rather than correctness.
0N/A */
0N/A public int available () throws IOException {
0N/A if (eof || closed) {
0N/A return 0;
0N/A }
0N/A int n = in.available();
0N/A return n > remaining? remaining: n;
0N/A }
0N/A
0N/A /* called after the stream is closed to see if bytes
0N/A * have been read from the underlying channel
0N/A * and buffered internally
0N/A */
0N/A public boolean isDataBuffered () throws IOException {
0N/A assert eof;
0N/A return in.available() > 0;
0N/A }
0N/A
0N/A public boolean markSupported () {return false;}
0N/A
0N/A public void mark (int l) {
0N/A }
0N/A
0N/A public void reset () throws IOException {
0N/A throw new IOException ("mark/reset not supported");
0N/A }
0N/A}