1 /* 2 * Copyright (c) 2005, 2017, 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.util.*; 25 import java.util.concurrent.*; 26 import java.util.logging.*; 27 import java.io.*; 28 import java.net.*; 29 import java.security.*; 30 import javax.net.ssl.*; 31 import com.sun.net.httpserver.*; 32 33 /** 34 * Implements a basic static EchoHandler for an HTTP server 35 */ 36 public class EchoHandler implements HttpHandler { 37 read(InputStream is)38 byte[] read(InputStream is) throws IOException { 39 byte[] buf = new byte[1024]; 40 byte[] result = new byte[0]; 41 42 while (true) { 43 int n = is.read(buf); 44 if (n > 0) { 45 byte[] b1 = new byte[result.length + n]; 46 System.arraycopy(result, 0, b1, 0, result.length); 47 System.arraycopy(buf, 0, b1, result.length, n); 48 result = b1; 49 } else if (n == -1) { 50 return result; 51 } 52 } 53 } 54 handle(HttpExchange t)55 public void handle (HttpExchange t) 56 throws IOException 57 { 58 InputStream is = t.getRequestBody(); 59 Headers map = t.getRequestHeaders(); 60 String fixedrequest = map.getFirst ("XFixed"); 61 62 // return the number of bytes received (no echo) 63 String summary = map.getFirst ("XSummary"); 64 if (fixedrequest != null && summary == null) { 65 byte[] in = read(is); 66 t.sendResponseHeaders(200, in.length); 67 OutputStream os = t.getResponseBody(); 68 os.write(in); 69 close(t, os); 70 close(t, is); 71 } else { 72 OutputStream os = t.getResponseBody(); 73 byte[] buf = new byte[64 * 1024]; 74 t.sendResponseHeaders(200, 0); 75 int n, count=0;; 76 77 while ((n = is.read(buf)) != -1) { 78 if (summary == null) { 79 os.write(buf, 0, n); 80 } 81 count += n; 82 } 83 if (summary != null) { 84 String s = Integer.toString(count); 85 os.write(s.getBytes()); 86 } 87 close(t, os); 88 close(t, is); 89 } 90 } 91 close(OutputStream os)92 protected void close(OutputStream os) throws IOException { 93 os.close(); 94 } close(InputStream is)95 protected void close(InputStream is) throws IOException { 96 is.close(); 97 } close(HttpExchange t, OutputStream os)98 protected void close(HttpExchange t, OutputStream os) throws IOException { 99 close(os); 100 } close(HttpExchange t, InputStream is)101 protected void close(HttpExchange t, InputStream is) throws IOException { 102 close(is); 103 } 104 } 105