1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.httpd;
  28 
  29 import java.io.BufferedWriter;
  30 import java.io.InputStreamReader;
  31 import java.io.IOException;
  32 import java.io.LineNumberReader;
  33 import java.io.OutputStreamWriter;
  34 import java.io.PrintWriter;
  35 import java.net.Socket;
  36 import java.nio.charset.StandardCharsets;
  37 import java.util.Hashtable;
  38 
  39 import com.sun.javatest.util.I18NResourceBundle;
  40 import com.sun.javatest.util.StringArray;
  41 
  42 /**
  43  * HTTP service handler for requests sent to JT Harness
  44  */
  45 
  46 // this code is based upon that found in the sun.net package
  47 class RequestHandler implements Runnable {
  48     /**
  49      * Construct a handler to take the HTTP request on the given socket.
  50      * Based on the requested URL, the correct JThttpProvider class will be
  51      * notified.  For special URLs, this class will delegate responsibility to
  52      * them.
  53      */
  54     public RequestHandler(Socket soc) {
  55         this.soc = soc;
  56     }
  57 
  58     public void run() {
  59         // read the request
  60         String request = null;
  61 
  62         try {
  63             if (debug) {
  64                 StringBuffer buf = new StringBuffer();
  65                 buf.append("Handling request from ");
  66                 buf.append(soc.getInetAddress().getHostName());
  67                 buf.append(" (");
  68                 buf.append(soc.getInetAddress().getHostAddress());
  69                 buf.append(")");
  70                 System.out.println(buf.toString());
  71                 buf.setLength(0);
  72             }
  73 
  74             out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soc.getOutputStream(), StandardCharsets.UTF_8)));
  75             in = new LineNumberReader(new InputStreamReader(soc.getInputStream(), StandardCharsets.UTF_8));
  76 
  77 
  78             request = in.readLine();
  79 
  80             if (debug) {
  81                 System.out.println("-------------");
  82                 System.out.println("RH-Full request:");
  83                 System.out.println(request);
  84             }
  85 
  86             // if we are debugging, print the rest of the message
  87             while (debug && in.ready()) {
  88                 String line = in.readLine();
  89                 System.out.println(line);
  90             }
  91 
  92             if (debug) System.out.println("-------------");
  93         }
  94         catch (IOException e) {
  95             e.printStackTrace();
  96         }
  97 
  98         // decode
  99         String[] args = StringArray.split(request);
 100         if (debug) {
 101             System.out.print("Decode: " + request);
 102             System.out.println(" -> " + args.length + " params.");
 103         }
 104 
 105         if (args == null || args.length < 3) {
 106             if (debug) System.err.println("RH-HTTP request too short.");
 107             // ignore the request
 108             return;
 109         }
 110 
 111         if (args[0].equalsIgnoreCase(GET)) {
 112             if (debug) System.out.println("RH-Processing HTTP GET request.");
 113             doGet(args);
 114         }
 115         else if (args[0].equalsIgnoreCase(POST)) {
 116             if (debug) System.out.println("RH-Processing HTTP POST request.");
 117             doPost(args);
 118         }
 119         else {
 120             if (debug) System.err.println("RH-Unsupported request method: " + args[0]);
 121             error(out, BAD_METHOD, args[0] + " is an unsupported request method. " + BAD_METHOD);
 122             out.close();
 123             return;
 124         }
 125     }
 126 
 127     protected void doGet(String[] args) {
 128         if (debug) System.out.println("RH-Get processing URL: \"" + args[1] + "\"");
 129 
 130         httpURL url = new httpURL(args[1]);
 131 
 132         /*
 133         JThttpProvider handler = ProviderRegistry.getHandler(args[1]);
 134         */
 135         JThttpProvider handler = RootRegistry.getInstance().getHandler(url);
 136         if (handler != null)
 137             handler.serviceRequest(url, out);
 138         else {
 139             // need to produce an error page!
 140             if (debug) System.out.println("No handler found for: " + args[1]);
 141         }
 142     }
 143 
 144     protected void doPost(String[] args) {
 145     }
 146 
 147     /**
 148      * Produce and error message.  404?
 149      */
 150     protected void notFound(String[] args) {
 151     }
 152 
 153     private void error(PrintWriter out, String code, String msg) {
 154         out.print("HTTP/1.1 ");
 155         out.println(code);
 156         out.println(HTTP_CONTENT_HTML);
 157         out.print("<html><head><title>");
 158         out.print(code);
 159         out.println("</title></head><body>");
 160         out.print(i18n.getString("handler.err.txt"));
 161         out.println("<br>");
 162         out.println(code);
 163         out.println("</body></html>");
 164     }
 165 
 166     private static Hashtable urlMap;
 167 
 168     private Socket soc;
 169     private PrintWriter out;
 170     private LineNumberReader in;
 171 
 172     private static final String GET = "GET";
 173     private static final String POST = "POST";
 174     private static final String BAD_METHOD = "405 Method Not Allowed";
 175     private static final String HTTP_CONTENT_TYPE = "Content-Type: ";
 176     private static final String HTTP_CONTENT_HTML = HTTP_CONTENT_TYPE + "text/html";
 177     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(RequestHandler.class);
 178     protected static boolean debug = Boolean.getBoolean("debug." + RequestHandler.class.getName());
 179 }