1 /* 2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.*; 25 import java.nio.file.Files; 26 import java.nio.file.Path; 27 import java.nio.file.Paths; 28 29 import jdk.internal.net.http.common.HttpHeadersImpl; 30 31 public class EchoHandler implements Http2Handler { 32 static final Path CWD = Paths.get("."); 33 EchoHandler()34 public EchoHandler() {} 35 36 @Override handle(Http2TestExchange t)37 public void handle(Http2TestExchange t) 38 throws IOException { 39 try { 40 System.err.println("EchoHandler received request to " + t.getRequestURI()); 41 InputStream is = t.getRequestBody(); 42 HttpHeadersImpl map = t.getRequestHeaders(); 43 HttpHeadersImpl map1 = t.getResponseHeaders(); 44 map1.addHeader("X-Hello", "world"); 45 map1.addHeader("X-Bye", "universe"); 46 String fixedrequest = map.firstValue("XFixed").orElse(null); 47 File outfile = Files.createTempFile(CWD, "foo", "bar").toFile(); 48 //System.err.println ("QQQ = " + outfile.toString()); 49 FileOutputStream fos = new FileOutputStream(outfile); 50 int count = (int) is.transferTo(fos); 51 System.err.printf("EchoHandler read %d bytes\n", count); 52 is.close(); 53 fos.close(); 54 InputStream is1 = new FileInputStream(outfile); 55 OutputStream os = null; 56 // return the number of bytes received (no echo) 57 String summary = map.firstValue("XSummary").orElse(null); 58 if (fixedrequest != null && summary == null) { 59 t.sendResponseHeaders(200, count); 60 os = t.getResponseBody(); 61 int count1 = (int)is1.transferTo(os); 62 System.err.printf("EchoHandler wrote %d bytes\n", count1); 63 } else { 64 t.sendResponseHeaders(200, 0); 65 os = t.getResponseBody(); 66 int count1 = (int)is1.transferTo(os); 67 System.err.printf("EchoHandler wrote %d bytes\n", count1); 68 69 if (summary != null) { 70 String s = Integer.toString(count); 71 os.write(s.getBytes()); 72 } 73 } 74 outfile.delete(); 75 os.close(); 76 is1.close(); 77 } catch (Throwable e) { 78 e.printStackTrace(); 79 throw new IOException(e); 80 } 81 } 82 } 83