2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2019-05-27 13:29:11 +01:00
|
|
|
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* @test
|
|
|
|
* @bug 4937598
|
2019-05-27 13:29:11 +01:00
|
|
|
* @library /test/lib
|
2016-08-08 20:16:53 +03:00
|
|
|
* @summary http://www.clipstream.com video does not play; read() problem
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-08-08 20:16:53 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2019-05-27 13:29:11 +01:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
2016-08-08 20:16:53 +03:00
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.URL;
|
|
|
|
|
2019-05-27 13:29:11 +01:00
|
|
|
import jdk.test.lib.net.URIBuilder;
|
|
|
|
|
2016-08-08 20:16:53 +03:00
|
|
|
public class HttpInputStream {
|
|
|
|
|
|
|
|
private static final int CONTENT_LENGTH = 20;
|
|
|
|
|
|
|
|
static class Server implements AutoCloseable, Runnable {
|
|
|
|
|
|
|
|
final ServerSocket serverSocket;
|
|
|
|
static final byte[] requestEnd = new byte[]{'\r', '\n', '\r', '\n'};
|
|
|
|
static final int TIMEOUT = 10 * 1000;
|
|
|
|
|
|
|
|
Server() throws IOException {
|
2019-05-27 13:29:11 +01:00
|
|
|
serverSocket = new ServerSocket();
|
|
|
|
serverSocket.bind(new InetSocketAddress(
|
|
|
|
InetAddress.getLoopbackAddress(), 0));
|
2016-08-08 20:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void readOneRequest(InputStream is) throws IOException {
|
|
|
|
int requestEndCount = 0, r;
|
|
|
|
while ((r = is.read()) != -1) {
|
|
|
|
if (r == requestEnd[requestEndCount]) {
|
|
|
|
requestEndCount++;
|
|
|
|
if (requestEndCount == 4) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
requestEndCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try (Socket s = serverSocket.accept()) {
|
|
|
|
s.setSoTimeout(TIMEOUT);
|
|
|
|
readOneRequest(s.getInputStream());
|
|
|
|
try (OutputStream os =
|
|
|
|
s.getOutputStream()) {
|
|
|
|
os.write("HTTP/1.1 200 OK".getBytes());
|
|
|
|
os.write(("Content-Length: " + CONTENT_LENGTH).getBytes());
|
|
|
|
os.write("\r\n\r\n".getBytes());
|
|
|
|
for (int i = 0; i < CONTENT_LENGTH; i++) {
|
|
|
|
os.write(0xff);
|
|
|
|
}
|
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
if (!serverSocket.isClosed()) {
|
|
|
|
serverSocket.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPort() {
|
|
|
|
return serverSocket.getLocalPort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int read(InputStream is) throws IOException {
|
|
|
|
int len = 0;
|
|
|
|
while (is.read() != -1) {
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String args[]) throws IOException {
|
|
|
|
try (Server server = new Server()) {
|
|
|
|
(new Thread(server)).start();
|
2019-05-27 13:29:11 +01:00
|
|
|
URL url = URIBuilder.newBuilder()
|
|
|
|
.scheme("http")
|
|
|
|
.loopback()
|
|
|
|
.port(server.getPort())
|
|
|
|
.path("/anything")
|
|
|
|
.toURLUnchecked();
|
2016-08-08 20:16:53 +03:00
|
|
|
try (InputStream is = url.openConnection().getInputStream()) {
|
|
|
|
if (read(is) != CONTENT_LENGTH) {
|
|
|
|
throw new RuntimeException("HttpInputStream.read() failed with 0xff");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|