提交 8bc39a03 编写于 作者: S snikandrova

8162876: [TEST_BUG] sun/net/www/protocol/http/HttpInputStream.java fails intermittently

Reviewed-by: chegar
上级 7785afa8
/* /*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,77 +23,95 @@ ...@@ -23,77 +23,95 @@
/* @test /* @test
* @bug 4937598 * @bug 4937598
* @summary http://www.clipstream.com vedio does not play; read() problem * @summary http://www.clipstream.com video does not play; read() problem
*/ */
import java.util.*;
import java.io.*;
import java.net.*;
import java.text.*;
public class HttpInputStream implements Runnable {
ServerSocket serverSock; import java.io.IOException;
import java.io.InputStream;
public void run() { import java.io.OutputStream;
try { import java.net.ServerSocket;
Socket s = serverSock.accept(); import java.net.Socket;
InputStream in = s.getInputStream(); import java.net.URL;
byte b[] = new byte[4096];
public class HttpInputStream {
// assume we read the entire http request
// (bad assumption but okay for test case) private static final int CONTENT_LENGTH = 20;
int nread = in.read(b);
static class Server implements AutoCloseable, Runnable {
OutputStream o = s.getOutputStream();
final ServerSocket serverSocket;
o.write( "HTTP/1.1 200 OK".getBytes() ); static final byte[] requestEnd = new byte[]{'\r', '\n', '\r', '\n'};
o.write( "Content-Length: 20".getBytes() ); static final int TIMEOUT = 10 * 1000;
o.write( (byte)'\r' );
o.write( (byte)'\n' ); Server() throws IOException {
o.write( (byte)'\r' ); serverSocket = new ServerSocket(0);
o.write( (byte)'\n' ); }
for (int i = 0; i < 20; i++) { void readOneRequest(InputStream is) throws IOException {
o.write((byte)0xff); int requestEndCount = 0, r;
} while ((r = is.read()) != -1) {
if (r == requestEnd[requestEndCount]) {
o.flush(); requestEndCount++;
o.close(); if (requestEndCount == 4) {
break;
} catch (Exception e) { } }
} } else {
requestEndCount = 0;
}
public HttpInputStream() throws Exception { }
}
serverSock = new ServerSocket(0);
int port = serverSock.getLocalPort(); @Override
public void run() {
Thread thr = new Thread(this); try (Socket s = serverSocket.accept()) {
thr.start(); s.setSoTimeout(TIMEOUT);
readOneRequest(s.getInputStream());
Date date = new Date(new Date().getTime()-1440000); // this time yesterday try (OutputStream os =
URL url; s.getOutputStream()) {
HttpURLConnection con; os.write("HTTP/1.1 200 OK".getBytes());
os.write(("Content-Length: " + CONTENT_LENGTH).getBytes());
url = new URL("http://localhost:" + String.valueOf(port) + os.write("\r\n\r\n".getBytes());
"/anything"); for (int i = 0; i < CONTENT_LENGTH; i++) {
con = (HttpURLConnection)url.openConnection(); os.write(0xff);
}
int ret = con.getResponseCode(); os.flush();
byte[] b = new byte[20]; }
InputStream is = con.getInputStream(); } catch (IOException e) {
int i = 0, count = 0; e.printStackTrace();
while ((i = is.read()) != -1) { }
System.out.println("i = "+i); }
count++;
} @Override
if (count != 20) { public void close() throws IOException {
throw new RuntimeException("HttpInputStream.read() failed with 0xff"); if (!serverSocket.isClosed()) {
} serverSocket.close();
} }
}
public static void main(String args[]) throws Exception {
new HttpInputStream(); 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();
URL url = new URL("http://localhost:" + server.getPort() + "/anything");
try (InputStream is = url.openConnection().getInputStream()) {
if (read(is) != CONTENT_LENGTH) {
throw new RuntimeException("HttpInputStream.read() failed with 0xff");
}
}
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册