test/jdk/com/sun/net/httpserver/FileServerHandler.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45535 jdk/test/com/sun/net/httpserver/FileServerHandler.java@4b19310ae4ee
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45531
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
     2
 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.concurrent.*;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    26
import java.util.logging.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    34
 * Implements a basic static content HTTP file server handler
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * which understands text/html, text/plain content types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Must be given an abs pathname to the document root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Directory listings together with text + html files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * can be served.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    40
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class FileServerHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    44
    String docroot;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    45
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    46
    public FileServerHandler (String docroot) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    47
        this.docroot = docroot;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    48
    }
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    49
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    50
    int invocation = 1;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    51
    public void handle (HttpExchange t)
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    52
        throws IOException
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    53
    {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    54
        InputStream is = t.getRequestBody();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    55
        Headers map = t.getRequestHeaders();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    56
        Headers rmap = t.getResponseHeaders();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    57
        URI uri = t.getRequestURI();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    58
        String path = uri.getPath();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    59
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    60
        int x = 0;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    61
        while (is.read () != -1) x++;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    62
        is.close();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    63
        File f = new File (docroot, path);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    64
        if (!f.exists()) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    65
            notfound (t, path);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    66
            return;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    67
        }
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    68
        String fixedrequest = map.getFirst ("XFixed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    70
        String method = t.getRequestMethod();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    71
        if (method.equals ("HEAD")) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    72
            rmap.set ("Content-Length", Long.toString (f.length()));
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    73
            t.sendResponseHeaders (200, -1);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    74
            t.close();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    75
        } else if (!method.equals("GET")) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    76
            t.sendResponseHeaders (405, -1);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    77
            t.close();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    78
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    81
        if (path.endsWith (".html") || path.endsWith (".htm")) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    82
            rmap.set ("Content-Type", "text/html");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    83
        } else {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    84
            rmap.set ("Content-Type", "text/plain");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    85
        }
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    86
        if (f.isDirectory()) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    87
            if (!path.endsWith ("/")) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    88
                moved (t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    91
            rmap.set ("Content-Type", "text/html");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    92
            t.sendResponseHeaders (200, 0);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    93
            String[] list = f.list();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    94
            OutputStream os = t.getResponseBody();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    95
            PrintStream p = new PrintStream (os);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    96
            p.println ("<h2>Directory listing for: " + path+ "</h2>");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    97
            p.println ("<ul>");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    98
            for (int i=0; i<list.length; i++) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
    99
                p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   101
            p.println ("</ul><p><hr>");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   102
            p.flush();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   103
            p.close();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   104
        } else {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   105
            int clen;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   106
            if (fixedrequest != null) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   107
                clen = (int) f.length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else {
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   109
                clen = 0;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   110
            }
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   111
            t.sendResponseHeaders (200, clen);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   112
            OutputStream os = t.getResponseBody();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   113
            FileInputStream fis = new FileInputStream (f);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   114
            int count = 0;
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   115
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                byte[] buf = new byte [16 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                while ((len=fis.read (buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    os.write (buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   122
            } catch (IOException e) {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   123
                e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   125
            fis.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   129
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   130
    void moved (HttpExchange t) throws IOException {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   131
        Headers req = t.getRequestHeaders();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   132
        Headers map = t.getResponseHeaders();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   133
        URI uri = t.getRequestURI();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   134
        String host = req.getFirst ("Host");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   135
        String location = "http://"+host+uri.getPath() + "/";
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   136
        map.set ("Content-Type", "text/html");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   137
        map.set ("Location", location);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   138
        t.sendResponseHeaders (301, -1);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   139
        t.close();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   140
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   141
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   142
    void notfound (HttpExchange t, String p) throws IOException {
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   143
        t.getResponseHeaders().set ("Content-Type", "text/html");
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   144
        t.sendResponseHeaders (404, 0);
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   145
        OutputStream os = t.getResponseBody();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   146
        String s = "<h2>File not found</h2>";
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   147
        s = s + p + "<p>";
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   148
        os.write (s.getBytes());
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   149
        os.close();
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   150
        t.close();
45531
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   151
    }
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 45531
diff changeset
   152
}