jdk/test/com/sun/net/httpserver/FileServerHandler.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 import java.util.*;
       
    25 import java.util.concurrent.*;
       
    26 import java.io.*;
       
    27 import java.net.*;
       
    28 import java.security.*;
       
    29 import javax.net.ssl.*;
       
    30 import com.sun.net.httpserver.*;
       
    31 
       
    32 /**
       
    33  * Implements a basic static content HTTP server
       
    34  * which understands text/html, text/plain content types
       
    35  *
       
    36  * Must be given an abs pathname to the document root.
       
    37  * Directory listings together with text + html files
       
    38  * can be served.
       
    39  */
       
    40 public class FileServerHandler implements HttpHandler {
       
    41 
       
    42         public static void main (String[] args) throws Exception {
       
    43             if (args.length != 3) {
       
    44                 System.out.println ("usage: java FileServerHandler rootDir port logfilename");
       
    45                 System.exit(1);
       
    46             }
       
    47             String rootDir = args[0];
       
    48             int port = Integer.parseInt (args[1]);
       
    49             String logfile = args[2];
       
    50             HttpServer server = HttpServer.create (new InetSocketAddress (8000), 0);
       
    51             HttpHandler h = new FileServerHandler (rootDir);
       
    52 
       
    53             HttpContext c = server.createContext ("/", h);
       
    54             c.getFilters().add (new LogFilter (new File (logfile)));
       
    55             server.setExecutor (Executors.newCachedThreadPool());
       
    56             server.start ();
       
    57         }
       
    58 
       
    59         String docroot;
       
    60 
       
    61         FileServerHandler (String docroot) {
       
    62             this.docroot = docroot;
       
    63         }
       
    64 
       
    65         int invocation = 1;
       
    66         public void handle (HttpExchange t)
       
    67             throws IOException
       
    68         {
       
    69             InputStream is = t.getRequestBody();
       
    70             Headers map = t.getRequestHeaders();
       
    71             Headers rmap = t.getResponseHeaders();
       
    72             URI uri = t.getRequestURI();
       
    73             String path = uri.getPath();
       
    74 
       
    75             while (is.read () != -1) ;
       
    76             is.close();
       
    77             File f = new File (docroot, path);
       
    78             if (!f.exists()) {
       
    79                 notfound (t, path);
       
    80                 return;
       
    81             }
       
    82             String fixedrequest = map.getFirst ("XFixed");
       
    83 
       
    84             String method = t.getRequestMethod();
       
    85             if (method.equals ("HEAD")) {
       
    86                 rmap.set ("Content-Length", Long.toString (f.length()));
       
    87                 t.sendResponseHeaders (200, -1);
       
    88                 t.close();
       
    89             } else if (!method.equals("GET")) {
       
    90                 t.sendResponseHeaders (405, -1);
       
    91                 t.close();
       
    92                 return;
       
    93             }
       
    94 
       
    95             if (path.endsWith (".html") || path.endsWith (".htm")) {
       
    96                 rmap.set ("Content-Type", "text/html");
       
    97             } else {
       
    98                 rmap.set ("Content-Type", "text/plain");
       
    99             }
       
   100             if (f.isDirectory()) {
       
   101                 if (!path.endsWith ("/")) {
       
   102                     moved (t);
       
   103                     return;
       
   104                 }
       
   105                 rmap.set ("Content-Type", "text/html");
       
   106                 t.sendResponseHeaders (200, 0);
       
   107                 String[] list = f.list();
       
   108                 OutputStream os = t.getResponseBody();
       
   109                 PrintStream p = new PrintStream (os);
       
   110                 p.println ("<h2>Directory listing for: " + path+ "</h2>");
       
   111                 p.println ("<ul>");
       
   112                 for (int i=0; i<list.length; i++) {
       
   113                     p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
       
   114                 }
       
   115                 p.println ("</ul><p><hr>");
       
   116                 p.flush();
       
   117                 p.close();
       
   118             } else {
       
   119                 int clen;
       
   120                 if (fixedrequest != null) {
       
   121                     clen = (int) f.length();
       
   122                 } else {
       
   123                     clen = 0;
       
   124                 }
       
   125                 t.sendResponseHeaders (200, clen);
       
   126                 OutputStream os = t.getResponseBody();
       
   127                 FileInputStream fis = new FileInputStream (f);
       
   128                 int count = 0;
       
   129                 try {
       
   130                 byte[] buf = new byte [16 * 1024];
       
   131                 int len;
       
   132                 while ((len=fis.read (buf)) != -1) {
       
   133                     os.write (buf, 0, len);
       
   134                     count += len;
       
   135                 }
       
   136                 } catch (IOException e) {
       
   137                         e.printStackTrace();
       
   138                 }
       
   139                 fis.close();
       
   140                 os.close();
       
   141             }
       
   142         }
       
   143 
       
   144         void moved (HttpExchange t) throws IOException {
       
   145             Headers req = t.getRequestHeaders();
       
   146             Headers map = t.getResponseHeaders();
       
   147             URI uri = t.getRequestURI();
       
   148             String host = req.getFirst ("Host");
       
   149             String location = "http://"+host+uri.getPath() + "/";
       
   150             map.set ("Content-Type", "text/html");
       
   151             map.set ("Location", location);
       
   152             t.sendResponseHeaders (301, -1);
       
   153             t.close();
       
   154         }
       
   155 
       
   156         void notfound (HttpExchange t, String p) throws IOException {
       
   157             t.getResponseHeaders().set ("Content-Type", "text/html");
       
   158             t.sendResponseHeaders (404, 0);
       
   159             OutputStream os = t.getResponseBody();
       
   160             String s = "<h2>File not found</h2>";
       
   161             s = s + p + "<p>";
       
   162             os.write (s.getBytes());
       
   163             os.close();
       
   164             t.close();
       
   165         }
       
   166     }