Path: blob/master/test/jdk/java/net/httpclient/EchoHandler.java
41149 views
/*1* Copyright (c) 2015, 2018, 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*/2223import com.sun.net.httpserver.*;24import java.net.*;25import java.net.http.*;26import java.io.*;27import java.util.concurrent.*;28import javax.net.ssl.*;29import java.nio.file.*;30import java.util.HashSet;31import java.util.LinkedList;32import java.util.List;33import java.util.Random;34import jdk.test.lib.net.SimpleSSLContext;35import static java.net.http.HttpRequest.*;36import static java.net.http.HttpResponse.*;37import java.util.logging.ConsoleHandler;38import java.util.logging.Level;39import java.util.logging.Logger;4041public class EchoHandler implements HttpHandler {42static final Path CWD = Paths.get(".");43public EchoHandler() {}4445@Override46public void handle(HttpExchange t)47throws IOException {48try {49System.err.println("EchoHandler received request to " + t.getRequestURI());50InputStream is = t.getRequestBody();51Headers map = t.getRequestHeaders();52Headers map1 = t.getResponseHeaders();53map1.add("X-Hello", "world");54map1.add("X-Bye", "universe");55String fixedrequest = map.getFirst("XFixed");56File outfile = Files.createTempFile(CWD, "foo", "bar").toFile();57FileOutputStream fos = new FileOutputStream(outfile);58int count = (int) is.transferTo(fos);59is.close();60fos.close();61InputStream is1 = new FileInputStream(outfile);62OutputStream os = null;63// return the number of bytes received (no echo)64String summary = map.getFirst("XSummary");65if (fixedrequest != null && summary == null) {66t.sendResponseHeaders(200, count);67os = t.getResponseBody();68is1.transferTo(os);69} else {70t.sendResponseHeaders(200, 0);71os = t.getResponseBody();72is1.transferTo(os);7374if (summary != null) {75String s = Integer.toString(count);76os.write(s.getBytes());77}78}79outfile.delete();80os.close();81is1.close();82} catch (Throwable e) {83e.printStackTrace();84throw new IOException(e);85}86}87}888990