1 /* 2 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package sun.rmi.transport.proxy; 26 27 import java.io.*; 28 29 import sun.rmi.runtime.Log; 30 31 /** 32 * The HttpInputStream class assists the HttpSendSocket and HttpReceiveSocket 33 * classes by filtering out the header for the message as well as any 34 * data after its proper content length. 35 */ 36 class HttpInputStream extends FilterInputStream { 37 38 /** bytes remaining to be read from proper content of message */ 39 protected int bytesLeft; 40 41 /** bytes remaining to be read at time of last mark */ 42 protected int bytesLeftAtMark; 43 44 /** 45 * Create new filter on a given input stream. 46 * @param in the InputStream to filter from 47 */ 48 public HttpInputStream(InputStream in) throws IOException 49 { 50 super(in); 51 52 if (in.markSupported()) 53 in.mark(0); // prevent resetting back to old marks 54 55 // pull out header, looking for content length 56 57 DataInputStream dis = new DataInputStream(in); 58 String key = "Content-length:".toLowerCase(); 59 boolean contentLengthFound = false; 60 String line; 61 do { 62 line = dis.readLine(); 63 64 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) { 65 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE, 66 "received header line: \"" + line + "\""); 67 } 68 69 if (line == null) 70 throw new EOFException(); 71 72 if (line.toLowerCase().startsWith(key)) { 73 if (contentLengthFound) { 74 throw new IOException( 75 "Multiple Content-length entries found."); 76 } else { 77 bytesLeft = 78 Integer.parseInt(line.substring(key.length()).trim()); 79 contentLengthFound = true; 80 } 81 } 82 83 // The idea here is to go past the first blank line. 84 // Some DataInputStream.readLine() documentation specifies that 85 // it does include the line-terminating character(s) in the 86 // returned string, but it actually doesn't, so we'll cover 87 // all cases here... 88 } while ((line.length() != 0) && 89 (line.charAt(0) != '\r') && (line.charAt(0) != '\n')); 90 91 if (!contentLengthFound || bytesLeft < 0) { 92 // This really shouldn't happen, but if it does, shoud we fail?? 93 // For now, just give up and let a whole lot of bytes through... 94 bytesLeft = Integer.MAX_VALUE; 95 } 96 bytesLeftAtMark = bytesLeft; 97 98 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) { 99 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE, 100 "content length: " + bytesLeft); 101 } 102 } 103 104 /** 105 * Returns the number of bytes that can be read with blocking. 106 * Make sure that this does not exceed the number of bytes remaining 107 * in the proper content of the message. 108 */ 109 public int available() throws IOException 110 { 111 int bytesAvailable = in.available(); 112 if (bytesAvailable > bytesLeft) 113 bytesAvailable = bytesLeft; 114 115 return bytesAvailable; 116 } 117 118 /** 119 * Read a byte of data from the stream. Make sure that one is available 120 * from the proper content of the message, else -1 is returned to 121 * indicate to the user that the end of the stream has been reached. 122 */ 123 public int read() throws IOException 124 { 125 if (bytesLeft > 0) { 126 int data = in.read(); 127 if (data != -1) 128 -- bytesLeft; 129 130 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) { 131 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE, 132 "received byte: '" + 133 ((data & 0x7F) < ' ' ? " " : String.valueOf((char) data)) + 134 "' " + data); 135 } 136 137 return data; 138 } 139 else { 140 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE, 141 "read past content length"); 142 143 return -1; 144 } 145 } 146 147 public int read(byte b[], int off, int len) throws IOException 148 { 149 if (bytesLeft == 0 && len > 0) { 150 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE, 151 "read past content length"); 152 153 return -1; 154 } 155 if (len > bytesLeft) 156 len = bytesLeft; 157 int bytesRead = in.read(b, off, len); 158 bytesLeft -= bytesRead; 159 160 if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.VERBOSE)) { 161 RMIMasterSocketFactory.proxyLog.log(Log.VERBOSE, 162 "read " + bytesRead + " bytes, " + bytesLeft + " remaining"); 163 } 164 165 return bytesRead; 166 } 167 168 /** 169 * Mark the current position in the stream (for future calls to reset). 170 * Remember where we are within the proper content of the message, so 171 * that a reset method call can recreate our state properly. 172 * @param readlimit how many bytes can be read before mark becomes invalid 173 */ 174 public void mark(int readlimit) 175 { 176 in.mark(readlimit); 177 if (in.markSupported()) 178 bytesLeftAtMark = bytesLeft; 179 } 180 181 /** 182 * Repositions the stream to the last marked position. Make sure to 183 * adjust our position within the proper content accordingly. 184 */ 185 public void reset() throws IOException 186 { 187 in.reset(); 188 bytesLeft = bytesLeftAtMark; 189 } 190 191 /** 192 * Skips bytes of the stream. Make sure to adjust our 193 * position within the proper content accordingly. 194 * @param n number of bytes to be skipped 195 */ 196 public long skip(long n) throws IOException 197 { 198 if (n > bytesLeft) 199 n = bytesLeft; 200 long bytesSkipped = in.skip(n); 201 bytesLeft -= bytesSkipped; 202 return bytesSkipped; 203 } 204 }