jdk/src/share/classes/sun/net/httpserver/ChunkedInputStream.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8541 75bedadddefa
child 16081 452341da6f0b
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8541
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
class ChunkedInputStream extends LeftOverInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    ChunkedInputStream (ExchangeImpl t, InputStream src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        super (t, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /* true when a chunk header needs to be read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private boolean needToReadHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static char CR = '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static char LF = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int numeric (char[] arr, int nchars) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        assert arr.length >= nchars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i=0; i<nchars; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            char c = arr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            int val=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            if (c>='0' && c <='9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                val = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            } else if (c>='a' && c<= 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                val = c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            } else if (c>='A' && c<= 'F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                val = c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                throw new IOException ("invalid chunk length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            len = len * 16 + val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* read the chunk header line and return the chunk length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * any chunk extensions are ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int readChunkHeader () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        boolean gotCR = false;
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    72
        int c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        char[] len_arr = new char [16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int len_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        boolean end_of_len = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    77
        while ((c=in.read())!= -1) {
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    78
            char ch = (char) c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (len_size == len_arr.length -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw new IOException ("invalid chunk header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (gotCR) {
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    83
                if (ch == LF) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    int l = numeric (len_arr, len_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    gotCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (!end_of_len) {
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    90
                    len_arr[len_size++] = ch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else {
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    93
                if (ch == CR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    gotCR = true;
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    95
                } else if (ch == ';') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    end_of_len = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                } else if (!end_of_len) {
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    98
                    len_arr[len_size++] = ch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        throw new IOException ("end of stream reading chunk header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected int readImpl (byte[]b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (needToReadHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            remaining = readChunkHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (remaining == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                consumeCRLF();
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   114
                t.getServerImpl().requestCompleted (t.getConnection());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            needToReadHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            len = remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int n = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (n > -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            remaining -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (remaining == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            needToReadHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            consumeCRLF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private void consumeCRLF () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        c = (char)in.read(); /* CR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (c != CR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new IOException ("invalid chunk end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        c = (char)in.read(); /* LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (c != LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new IOException ("invalid chunk end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * returns the number of bytes available to read in the current chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * which may be less than the real amount, but we'll live with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * limitation for the moment. It only affects potential efficiency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * rather than correctness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (eof || closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int n = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return n > remaining? remaining: n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /* called after the stream is closed to see if bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * have been read from the underlying channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * and buffered internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public boolean isDataBuffered () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        assert eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return in.available() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean markSupported () {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void mark (int l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        throw new IOException ("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}