src/jdk.httpserver/share/classes/sun/net/httpserver/ChunkedInputStream.java
author michaelm
Thu, 14 Nov 2019 09:06:10 +0000
branchunixdomainchannels
changeset 59074 7917f95f74c4
parent 47705 a6f8cacdef93
child 55782 0586f8664ba8
permissions -rw-r--r--
unixdomainchannels: minor apidoc update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47705
a6f8cacdef93 8190793: Httpserver does not detect truncated request body
michaelm
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2017, 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
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    44
    final static char CR = '\r';
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    45
    final static char LF = '\n';
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    46
    /*
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    47
     * Maximum chunk header size of 2KB + 2 bytes for CRLF
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    48
     */
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    49
    private final static int MAX_CHUNK_HEADER_SIZE = 2050;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int numeric (char[] arr, int nchars) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        assert arr.length >= nchars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        for (int i=0; i<nchars; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            char c = arr[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            int val=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (c>='0' && c <='9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                val = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            } else if (c>='a' && c<= 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                val = c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            } else if (c>='A' && c<= 'F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                val = c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                throw new IOException ("invalid chunk length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            len = len * 16 + val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* read the chunk header line and return the chunk length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * any chunk extensions are ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int readChunkHeader () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        boolean gotCR = false;
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    76
        int c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        char[] len_arr = new char [16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int len_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        boolean end_of_len = false;
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    80
        int read = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    82
        while ((c=in.read())!= -1) {
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    83
            char ch = (char) c;
16081
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    84
            read++;
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    85
            if ((len_size == len_arr.length -1) ||
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    86
                (read > MAX_CHUNK_HEADER_SIZE))
452341da6f0b 7186954: Improve connection performance
khazra
parents: 9035
diff changeset
    87
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new IOException ("invalid chunk header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (gotCR) {
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
    91
                if (ch == LF) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    int l = numeric (len_arr, len_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    gotCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                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
            } else {
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
   101
                if (ch == CR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    gotCR = true;
8541
75bedadddefa 6702400: ChunkedInputStream expecting -1 from int read, but int->char comparision is wrong
michaelm
parents: 7668
diff changeset
   103
                } else if (ch == ';') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    end_of_len = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                } 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
   106
                    len_arr[len_size++] = ch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throw new IOException ("end of stream reading chunk header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    protected int readImpl (byte[]b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (eof) {
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
        if (needToReadHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            remaining = readChunkHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (remaining == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                consumeCRLF();
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   122
                t.getServerImpl().requestCompleted (t.getConnection());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            needToReadHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            len = remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int n = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (n > -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            remaining -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (remaining == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            needToReadHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            consumeCRLF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
47705
a6f8cacdef93 8190793: Httpserver does not detect truncated request body
michaelm
parents: 47216
diff changeset
   138
        if (n < 0 && !eof)
a6f8cacdef93 8190793: Httpserver does not detect truncated request body
michaelm
parents: 47216
diff changeset
   139
            throw new IOException("connection closed before all data received");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private void consumeCRLF () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        c = (char)in.read(); /* CR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (c != CR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new IOException ("invalid chunk end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        c = (char)in.read(); /* LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (c != LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IOException ("invalid chunk end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * returns the number of bytes available to read in the current chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * which may be less than the real amount, but we'll live with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * limitation for the moment. It only affects potential efficiency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * rather than correctness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (eof || closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int n = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return n > remaining? remaining: n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /* called after the stream is closed to see if bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * have been read from the underlying channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * and buffered internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public boolean isDataBuffered () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        assert eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return in.available() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public boolean markSupported () {return false;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void mark (int l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throw new IOException ("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}