0N/A/*
3261N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4333920
0N/A * @run main ChunkedEncodingTest
0N/A * @summary ChunkedEncodingTest unit test
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.security.*;
663N/Aimport com.sun.net.httpserver.HttpServer;
663N/Aimport com.sun.net.httpserver.HttpHandler;
663N/Aimport com.sun.net.httpserver.HttpExchange;
663N/Aimport static java.lang.System.out;
0N/A
663N/Apublic class ChunkedEncodingTest{
663N/A private static MessageDigest serverDigest, clientDigest;
2747N/A private static volatile byte[] serverMac, clientMac;
663N/A
663N/A static void client(String u) throws Exception {
663N/A URL url = new URL(u);
663N/A out.println("client opening connection to: " + u);
663N/A URLConnection urlc = url.openConnection();
663N/A DigestInputStream dis =
663N/A new DigestInputStream(urlc.getInputStream(), clientDigest);
663N/A while (dis.read() != -1);
663N/A clientMac = dis.getMessageDigest().digest();
663N/A dis.close();
663N/A }
663N/A
663N/A public static void test() {
663N/A HttpServer server = null;
0N/A try {
663N/A serverDigest = MessageDigest.getInstance("MD5");
663N/A clientDigest = MessageDigest.getInstance("MD5");
663N/A server = startHttpServer();
663N/A
663N/A int port = server.getAddress().getPort();
663N/A out.println ("Server listening on port: " + port);
663N/A client("http://localhost:" + port + "/chunked/");
663N/A
663N/A if (!MessageDigest.isEqual(clientMac, serverMac)) {
663N/A throw new RuntimeException(
663N/A "Data received is NOT equal to the data sent");
0N/A }
663N/A } catch (Exception e) {
663N/A throw new RuntimeException(e);
663N/A } finally {
663N/A if (server != null)
663N/A server.stop(0);
0N/A }
0N/A }
0N/A
663N/A public static void main(String[] args) {
0N/A test();
0N/A }
0N/A
663N/A /**
663N/A * Http Server
663N/A */
663N/A static HttpServer startHttpServer() throws IOException {
663N/A HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
663N/A HttpHandler httpHandler = new SimpleHandler();
663N/A httpServer.createContext("/chunked/", httpHandler);
663N/A httpServer.start();
663N/A return httpServer;
663N/A }
663N/A
663N/A static class SimpleHandler implements HttpHandler {
663N/A static byte[] baMessage;
663N/A final static int CHUNK_SIZE = 8 * 1024;
663N/A final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE;
663N/A
663N/A static {
663N/A baMessage = new byte[MESSAGE_LENGTH];
663N/A for (int i=0; i<MESSAGE_LENGTH; i++)
663N/A baMessage[i] = (byte)i;
663N/A }
663N/A
663N/A @Override
663N/A public void handle(HttpExchange t) throws IOException {
663N/A InputStream is = t.getRequestBody();
663N/A while (is.read() != -1);
663N/A is.close();
663N/A
2747N/A t.sendResponseHeaders (200, 0);
663N/A OutputStream os = t.getResponseBody();
663N/A DigestOutputStream dos = new DigestOutputStream(os, serverDigest);
663N/A
663N/A int offset = 0;
663N/A for (int i=0; i<52; i++) {
663N/A dos.write(baMessage, offset, CHUNK_SIZE);
663N/A offset += CHUNK_SIZE;
663N/A }
663N/A serverMac = serverDigest.digest();
663N/A os.close();
663N/A t.close();
663N/A }
0N/A }
0N/A}