Exemplo n.º 1
0
  /**
   * Parses a GET request. Just breaks up the original request to find the correct file parts
   *
   * @param headers
   * @param out
   * @param in
   * @throws IOException
   */
  private void parseGET(List<String> headers, PrintStream out, InputStream in) throws IOException {
    int i = headers.get(0).indexOf("GET");
    int j = headers.get(0).indexOf('/', i);
    String fileRequested;
    if (j > i + 3) {
      // strip off everything up until the first slash
      fileRequested = headers.get(0).substring(j + 1);

      // strip off anything after the filename (if there
      // is anything after the filename it is separated
      // by a space)
      j = fileRequested.indexOf(' ');
      if (j >= 0) {
        fileRequested = fileRequested.substring(0, j);
      }

      // strip off the base app dir
      if (!fileRequested.contains(APP_DIR.substring(1))) {
        System.out.println("GET did not contain propper dir beginning");
        System.out.println("Disallowed request");
        out.println("HTTP/1.0 403 Forbidden\r\n\r\n");
        out.println(
            "<html><body>You do not have permission to access that file.  "
                + "Are you trying to snoop arround?</html></body>");
        out.flush();
        return;
      }
      fileRequested = fileRequested.substring(APP_DIR.length() - 1);

      // if the request was just "/" we'll give them
      // a listing of the base directory
      if (fileRequested.equals("")) {
        fileRequested = ".";
      }

      handleFileRequestGET(fileRequested, headers, out, in);
    }
  }