Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/FileServerHandler.java
38855 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.*;24import java.util.concurrent.*;25import java.io.*;26import java.net.*;27import java.security.*;28import javax.net.ssl.*;29import com.sun.net.httpserver.*;3031/**32* Implements a basic static content HTTP server33* which understands text/html, text/plain content types34*35* Must be given an abs pathname to the document root.36* Directory listings together with text + html files37* can be served.38*/39public class FileServerHandler implements HttpHandler {4041public static void main (String[] args) throws Exception {42if (args.length != 3) {43System.out.println ("usage: java FileServerHandler rootDir port logfilename");44System.exit(1);45}46String rootDir = args[0];47int port = Integer.parseInt (args[1]);48String logfile = args[2];49HttpServer server = HttpServer.create (new InetSocketAddress (8000), 0);50HttpHandler h = new FileServerHandler (rootDir);5152HttpContext c = server.createContext ("/", h);53c.getFilters().add (new LogFilter (new File (logfile)));54server.setExecutor (Executors.newCachedThreadPool());55server.start ();56}5758String docroot;5960FileServerHandler (String docroot) {61this.docroot = docroot;62}6364int invocation = 1;65public void handle (HttpExchange t)66throws IOException67{68InputStream is = t.getRequestBody();69Headers map = t.getRequestHeaders();70Headers rmap = t.getResponseHeaders();71URI uri = t.getRequestURI();72String path = uri.getPath();7374while (is.read () != -1) ;75is.close();76File f = new File (docroot, path);77if (!f.exists()) {78notfound (t, path);79return;80}81String fixedrequest = map.getFirst ("XFixed");8283String method = t.getRequestMethod();84if (method.equals ("HEAD")) {85rmap.set ("Content-Length", Long.toString (f.length()));86t.sendResponseHeaders (200, -1);87t.close();88} else if (!method.equals("GET")) {89t.sendResponseHeaders (405, -1);90t.close();91return;92}9394if (path.endsWith (".html") || path.endsWith (".htm")) {95rmap.set ("Content-Type", "text/html");96} else {97rmap.set ("Content-Type", "text/plain");98}99if (f.isDirectory()) {100if (!path.endsWith ("/")) {101moved (t);102return;103}104rmap.set ("Content-Type", "text/html");105t.sendResponseHeaders (200, 0);106String[] list = f.list();107OutputStream os = t.getResponseBody();108PrintStream p = new PrintStream (os);109p.println ("<h2>Directory listing for: " + path+ "</h2>");110p.println ("<ul>");111for (int i=0; i<list.length; i++) {112p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");113}114p.println ("</ul><p><hr>");115p.flush();116p.close();117} else {118int clen;119if (fixedrequest != null) {120clen = (int) f.length();121} else {122clen = 0;123}124t.sendResponseHeaders (200, clen);125OutputStream os = t.getResponseBody();126FileInputStream fis = new FileInputStream (f);127int count = 0;128try {129byte[] buf = new byte [16 * 1024];130int len;131while ((len=fis.read (buf)) != -1) {132os.write (buf, 0, len);133count += len;134}135} catch (IOException e) {136e.printStackTrace();137}138fis.close();139os.close();140}141}142143void moved (HttpExchange t) throws IOException {144Headers req = t.getRequestHeaders();145Headers map = t.getResponseHeaders();146URI uri = t.getRequestURI();147String host = req.getFirst ("Host");148String location = "http://"+host+uri.getPath() + "/";149map.set ("Content-Type", "text/html");150map.set ("Location", location);151t.sendResponseHeaders (301, -1);152t.close();153}154155void notfound (HttpExchange t, String p) throws IOException {156t.getResponseHeaders().set ("Content-Type", "text/html");157t.sendResponseHeaders (404, 0);158OutputStream os = t.getResponseBody();159String s = "<h2>File not found</h2>";160s = s + p + "<p>";161os.write (s.getBytes());162os.close();163t.close();164}165}166167168