Пример #1
0
 /**
  * Deals with all directory requests the same. Printing a list of files to the output.
  *
  * @param out the stream to prin to
  * @param f the file to look in
  * @throws IOException
  */
 private void handleDirectoryRequest(PrintStream out, File f) throws IOException {
   // create a dir listing as a text file
   String[] listing = SimpleHttpServer2.generateDirListing(f);
   out.print("HTTP/1.0 200 OK\r\n");
   out.print("Content-Type: text/plain\r\n");
   long contentLength = SimpleHttpServer2.getContentLengthFromStringArray(listing);
   out.print("Content-Length: " + contentLength + "\r\n\r\n");
   out.flush();
   for (String s : listing) {
     out.println(s);
   }
   out.flush();
   return;
 }
Пример #2
0
 /**
  * Checks to see if a directory requested is fair game.
  *
  * @param out stream to print errors to
  * @param f the file to check
  * @return true if all was good, false otherwise.
  */
 private boolean checkBelowAndHandle(PrintStream out, File f) {
   // make sure the file is really in the content directory
   try {
     if (!SimpleHttpServer2.checkIsBelow(new File(CONTENT_BASE_DIR_NAME), f)) {
       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 false;
     }
   } catch (IOException e) {
     return false;
   }
   return true;
 }
Пример #3
0
  /**
   * Processes one request for a file or directory.
   *
   * @param request the pathname for the requested file
   * @param headers
   * @param out output stream for the client connection
   * @throws IOException
   */
  private void handleFileRequestGET(
      String request, List<String> headers, PrintStream out, InputStream in) {
    System.out.println("File requested: " + request);

    try {
      File f = new File(CONTENT_BASE_DIR_NAME + request);

      if (!checkBelowAndHandle(out, f)) return;

      if (f.isDirectory()) {
        handleDirectoryRequest(out, f);
      } else {
        // catch FileNotFoundException if file doesn't exist
        FileInputStream fis = new FileInputStream(f);

        out.print("HTTP/1.0 200 OK\r\n");
        out.print("Content-Type: " + SimpleHttpServer2.guessMimeType(f) + "\r\n");
        out.print("Content-Length: " + f.length() + "\r\n\r\n");
        out.flush();

        // copy contents of file to output stream
        // loop uses array versions of read/write methods to
        // copy multiple bytes with each call
        byte[] data = new byte[64 * 1024];
        int bytesRead;
        while ((bytesRead = fis.read(data)) != -1) {
          out.write(data, 0, bytesRead);
        }
        out.flush();
        out.close();
        in.close();
        return;
      }
    } catch (FileNotFoundException e) {
      System.out.println("File not found: " + request);
      out.println("HTTP/1.0 404 Not Found\r\n\r\n");
      out.println("<html><body>404 File Not Found, not my problem.  Sorry.</body></html>");
      out.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }