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.io.*;
  25 import jdk.incubator.http.internal.common.HttpHeadersImpl;
  26 
  27 public class EchoHandler implements Http2Handler {
  28     public EchoHandler() {}
  29 
  30     @Override
  31     public void handle(Http2TestExchange t)
  32             throws IOException {
  33         try {
  34             System.err.println("EchoHandler received request to " + t.getRequestURI());
  35             InputStream is = t.getRequestBody();
  36             HttpHeadersImpl map = t.getRequestHeaders();
  37             HttpHeadersImpl map1 = t.getResponseHeaders();
  38             map1.addHeader("X-Hello", "world");
  39             map1.addHeader("X-Bye", "universe");
  40             String fixedrequest = map.firstValue("XFixed").orElse(null);
  41             File outfile = File.createTempFile("foo", "bar");
  42             //System.err.println ("QQQ = " + outfile.toString());
  43             FileOutputStream fos = new FileOutputStream(outfile);
  44             int count = (int) is.transferTo(fos);
  45             System.err.printf("EchoHandler read %d bytes\n", count);
  46             is.close();
  47             fos.close();
  48             InputStream is1 = new FileInputStream(outfile);
  49             OutputStream os = null;
  50             // return the number of bytes received (no echo)
  51             String summary = map.firstValue("XSummary").orElse(null);
  52             if (fixedrequest != null && summary == null) {
  53                 t.sendResponseHeaders(200, count);
  54                 os = t.getResponseBody();
  55                 int count1 = (int)is1.transferTo(os);
  56                 System.err.printf("EchoHandler wrote %d bytes\n", count1);
  57             } else {
  58                 t.sendResponseHeaders(200, 0);
  59                 os = t.getResponseBody();
  60                 int count1 = (int)is1.transferTo(os);
  61                 System.err.printf("EchoHandler wrote %d bytes\n", count1);
  62 
  63                 if (summary != null) {
  64                     String s = Integer.toString(count);
  65                     os.write(s.getBytes());
  66                 }
  67             }
  68             outfile.delete();
  69             os.close();
  70             is1.close();
  71         } catch (Throwable e) {
  72             e.printStackTrace();
  73             throw new IOException(e);
  74         }
  75     }
  76 }