Path: blob/master/test/jdk/com/sun/net/httpserver/FileServerHandler.java
41152 views
/*1* Copyright (c) 2005, 2017, 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.util.logging.*;26import java.io.*;27import java.net.*;28import java.security.*;29import javax.net.ssl.*;30import com.sun.net.httpserver.*;3132/**33* Implements a basic static content HTTP file server handler34* which understands text/html, text/plain content types35*36* Must be given an abs pathname to the document root.37* Directory listings together with text + html files38* can be served.39*40*/41public class FileServerHandler implements HttpHandler {4243String docroot;4445public FileServerHandler (String docroot) {46this.docroot = docroot;47}4849int invocation = 1;50public void handle (HttpExchange t)51throws IOException52{53InputStream is = t.getRequestBody();54Headers map = t.getRequestHeaders();55Headers rmap = t.getResponseHeaders();56URI uri = t.getRequestURI();57String path = uri.getPath();5859int x = 0;60while (is.read () != -1) x++;61is.close();62File f = new File (docroot, path);63if (!f.exists()) {64notfound (t, path);65return;66}67String fixedrequest = map.getFirst ("XFixed");6869String method = t.getRequestMethod();70if (method.equals ("HEAD")) {71rmap.set ("Content-Length", Long.toString (f.length()));72t.sendResponseHeaders (200, -1);73t.close();74} else if (!method.equals("GET")) {75t.sendResponseHeaders (405, -1);76t.close();77return;78}7980if (path.endsWith (".html") || path.endsWith (".htm")) {81rmap.set ("Content-Type", "text/html");82} else {83rmap.set ("Content-Type", "text/plain");84}85if (f.isDirectory()) {86if (!path.endsWith ("/")) {87moved (t);88return;89}90rmap.set ("Content-Type", "text/html");91t.sendResponseHeaders (200, 0);92String[] list = f.list();93OutputStream os = t.getResponseBody();94PrintStream p = new PrintStream (os);95p.println ("<h2>Directory listing for: " + path+ "</h2>");96p.println ("<ul>");97for (int i=0; i<list.length; i++) {98p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");99}100p.println ("</ul><p><hr>");101p.flush();102p.close();103} else {104int clen;105if (fixedrequest != null) {106clen = (int) f.length();107} else {108clen = 0;109}110t.sendResponseHeaders (200, clen);111OutputStream os = t.getResponseBody();112FileInputStream fis = new FileInputStream (f);113int count = 0;114try {115byte[] buf = new byte [16 * 1024];116int len;117while ((len=fis.read (buf)) != -1) {118os.write (buf, 0, len);119count += len;120}121} catch (IOException e) {122e.printStackTrace();123}124fis.close();125os.close();126}127}128129void moved (HttpExchange t) throws IOException {130Headers req = t.getRequestHeaders();131Headers map = t.getResponseHeaders();132URI uri = t.getRequestURI();133String host = req.getFirst ("Host");134String location = "http://"+host+uri.getPath() + "/";135map.set ("Content-Type", "text/html");136map.set ("Location", location);137t.sendResponseHeaders (301, -1);138t.close();139}140141void notfound (HttpExchange t, String p) throws IOException {142t.getResponseHeaders().set ("Content-Type", "text/html");143t.sendResponseHeaders (404, 0);144OutputStream os = t.getResponseBody();145String s = "<h2>File not found</h2>";146s = s + p + "<p>";147os.write (s.getBytes());148os.close();149t.close();150}151}152153154