Path: blob/master/test/jdk/sun/net/www/protocol/http/HttpInputStream.java
41159 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 493759825* @library /test/lib26* @summary http://www.clipstream.com video does not play; read() problem27*/282930import java.io.IOException;31import java.io.InputStream;32import java.io.OutputStream;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.ServerSocket;36import java.net.Socket;37import java.net.URL;3839import jdk.test.lib.net.URIBuilder;4041public class HttpInputStream {4243private static final int CONTENT_LENGTH = 20;4445static class Server implements AutoCloseable, Runnable {4647final ServerSocket serverSocket;48static final byte[] requestEnd = new byte[]{'\r', '\n', '\r', '\n'};49static final int TIMEOUT = 10 * 1000;5051Server() throws IOException {52serverSocket = new ServerSocket();53serverSocket.bind(new InetSocketAddress(54InetAddress.getLoopbackAddress(), 0));55}5657void readOneRequest(InputStream is) throws IOException {58int requestEndCount = 0, r;59while ((r = is.read()) != -1) {60if (r == requestEnd[requestEndCount]) {61requestEndCount++;62if (requestEndCount == 4) {63break;64}65} else {66requestEndCount = 0;67}68}69}7071@Override72public void run() {73try (Socket s = serverSocket.accept()) {74s.setSoTimeout(TIMEOUT);75readOneRequest(s.getInputStream());76try (OutputStream os =77s.getOutputStream()) {78os.write("HTTP/1.1 200 OK".getBytes());79os.write(("Content-Length: " + CONTENT_LENGTH).getBytes());80os.write("\r\n\r\n".getBytes());81for (int i = 0; i < CONTENT_LENGTH; i++) {82os.write(0xff);83}84os.flush();85}86} catch (IOException e) {87e.printStackTrace();88}89}9091@Override92public void close() throws IOException {93if (!serverSocket.isClosed()) {94serverSocket.close();95}96}9798public int getPort() {99return serverSocket.getLocalPort();100}101}102103104private static int read(InputStream is) throws IOException {105int len = 0;106while (is.read() != -1) {107len++;108}109return len;110}111112public static void main(String args[]) throws IOException {113try (Server server = new Server()) {114(new Thread(server)).start();115URL url = URIBuilder.newBuilder()116.scheme("http")117.loopback()118.port(server.getPort())119.path("/anything")120.toURLUnchecked();121try (InputStream is = url.openConnection().getInputStream()) {122if (read(is) != CONTENT_LENGTH) {123throw new RuntimeException("HttpInputStream.read() failed with 0xff");124}125}126}127}128}129130131