jdk/src/share/classes/sun/rmi/transport/proxy/HttpInputStream.java
author dmocek
Mon, 19 Nov 2012 15:38:56 -0800
changeset 16095 6aa0f442dccb
parent 12040 558b0e0d5910
child 16100 379f48d34516
permissions -rw-r--r--
8001242: Improve RMI HTTP conformance Reviewed-by: ahgross, mchung, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16095
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
     2
 * Copyright (c) 1996, 2012, 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
package sun.rmi.transport.proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The HttpInputStream class assists the HttpSendSocket and HttpReceiveSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * classes by filtering out the header for the message as well as any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * data after its proper content length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class HttpInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /** bytes remaining to be read from proper content of message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    protected int bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /** bytes remaining to be read at time of last mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected int bytesLeftAtMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Create new filter on a given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @param in the InputStream to filter from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public HttpInputStream(InputStream in) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (in.markSupported())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            in.mark(0); // prevent resetting back to old marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // pull out header, looking for content length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        DataInputStream dis = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        String key = "Content-length:".toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        boolean contentLengthFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            line = dis.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    "received header line: \"" + line + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (line == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (line.toLowerCase().startsWith(key)) {
16095
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    73
                if (contentLengthFound) {
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    74
                    throw new IOException(
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    75
                            "Multiple Content-length entries found.");
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    76
                } else {
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    77
                    bytesLeft =
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    78
                        Integer.parseInt(line.substring(key.length()).trim());
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    79
                    contentLengthFound = true;
6aa0f442dccb 8001242: Improve RMI HTTP conformance
dmocek
parents: 12040
diff changeset
    80
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // The idea here is to go past the first blank line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // Some DataInputStream.readLine() documentation specifies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // it does include the line-terminating character(s) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // returned string, but it actually doesn't, so we'll cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // all cases here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } while ((line.length() != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (!contentLengthFound || bytesLeft < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // This really shouldn't happen, but if it does, shoud we fail??
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // For now, just give up and let a whole lot of bytes through...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            bytesLeft = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        bytesLeftAtMark = bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                "content length: " + bytesLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns the number of bytes that can be read with blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Make sure that this does not exceed the number of bytes remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * in the proper content of the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int available() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int bytesAvailable = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (bytesAvailable > bytesLeft)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            bytesAvailable = bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return bytesAvailable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Read a byte of data from the stream.  Make sure that one is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * from the proper content of the message, else -1 is returned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * indicate to the user that the end of the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public int read() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (bytesLeft > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int data = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (data != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                -- bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                   "received byte: '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    ((data & 0x7F) < ' ' ? " " : String.valueOf((char) data)) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    "' " + data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                "read past content length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public int read(byte b[], int off, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (bytesLeft == 0 && len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                                "read past content length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (len > bytesLeft)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            len = bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int bytesRead = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        bytesLeft -= bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                "read " + bytesRead + " bytes, " + bytesLeft + " remaining");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Mark the current position in the stream (for future calls to reset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Remember where we are within the proper content of the message, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * that a reset method call can recreate our state properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param readlimit how many bytes can be read before mark becomes invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void mark(int readlimit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        in.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (in.markSupported())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            bytesLeftAtMark = bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Repositions the stream to the last marked position.  Make sure to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * adjust our position within the proper content accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void reset() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        bytesLeft = bytesLeftAtMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Skips bytes of the stream.  Make sure to adjust our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * position within the proper content accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param n number of bytes to be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public long skip(long n) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (n > bytesLeft)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            n = bytesLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        long bytesSkipped = in.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        bytesLeft -= bytesSkipped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return bytesSkipped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}