1 /*
   2  * Copyright (c) 2006, 2014, 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 /*
  25  * @test
  26  * @bug 6369510
  27  * @run main/othervm B6369510
  28  * @summary HttpURLConnection sets Content-Type to application/x-www-form-urlencoded
  29  */
  30 
  31 import java.net.*;
  32 import java.util.*;
  33 import java.io.*;
  34 import com.sun.net.httpserver.*;
  35 import java.util.concurrent.Executors;
  36 import java.util.concurrent.ExecutorService;
  37 
  38 public class B6369510
  39 {
  40     com.sun.net.httpserver.HttpServer httpServer;
  41     ExecutorService executorService;
  42 
  43     public static void main(String[] args) throws Exception
  44     {
  45         new B6369510();
  46     }
  47 
  48     public B6369510()
  49     {
  50         try {
  51             startHttpServer();
  52             doClient();
  53         } catch (IOException ioe) {
  54             System.err.println(ioe);
  55         }
  56     }
  57 
  58     void doClient() {
  59         try {
  60             InetSocketAddress address = httpServer.getAddress();
  61             String urlString = "http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/";
  62             System.out.println("URL == " + urlString);
  63 
  64             // GET Request
  65             URL url = new URL("http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/");
  66             HttpURLConnection uc = (HttpURLConnection)url.openConnection();
  67             int resp = uc.getResponseCode();
  68             if (resp != 200)
  69                 throw new RuntimeException("Failed: Response code from GET is not 200 RSP == " + resp);
  70 
  71             System.out.println("Response code from GET = 200 OK");
  72 
  73             //POST Request
  74             uc = (HttpURLConnection)url.openConnection();
  75             uc.setDoOutput(true);
  76             uc.setRequestMethod("POST");
  77             OutputStream os = uc.getOutputStream();
  78             resp = uc.getResponseCode();
  79             if (resp != 200)
  80                 throw new RuntimeException("Failed: Response code form POST is not 200 RSP == " + resp);
  81 
  82             System.out.println("Response code from POST = 200 OK");
  83 
  84         } catch (IOException e) {
  85             e.printStackTrace();
  86             throw new RuntimeException("Failed with IOException");
  87         } finally {
  88             httpServer.stop(1);
  89             executorService.shutdown();
  90         }
  91     }
  92 
  93     /**
  94      * Http Server
  95      */
  96     public void startHttpServer() throws IOException {
  97         httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
  98 
  99         // create HttpServer context
 100         HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
 101 
 102         executorService = Executors.newCachedThreadPool();
 103         httpServer.setExecutor(executorService);
 104         httpServer.start();
 105     }
 106 
 107     class MyHandler implements HttpHandler {
 108         public void handle(HttpExchange t) throws IOException {
 109             InputStream is = t.getRequestBody();
 110             Headers reqHeaders = t.getRequestHeaders();
 111             Headers resHeaders = t.getResponseHeaders();
 112             while (is.read () != -1) ;
 113             is.close();
 114 
 115             List<String> ct = reqHeaders.get("content-type");
 116             String requestMethod = t.getRequestMethod();
 117 
 118             if (requestMethod.equalsIgnoreCase("GET") && ct != null &&
 119                 ct.get(0).equals("application/x-www-form-urlencoded"))
 120                 t.sendResponseHeaders(400, -1);
 121 
 122             else if (requestMethod.equalsIgnoreCase("POST") && ct != null &&
 123                      !ct.get(0).equals("application/x-www-form-urlencoded"))
 124                 t.sendResponseHeaders(400, -1);
 125 
 126             t.sendResponseHeaders(200, -1);
 127             t.close();
 128         }
 129     }
 130 }