2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
import java.util.*;
|
|
25 |
import java.util.concurrent.*;
|
36131
|
26 |
import java.util.logging.*;
|
2
|
27 |
import java.io.*;
|
|
28 |
import java.net.*;
|
|
29 |
import java.security.*;
|
|
30 |
import javax.net.ssl.*;
|
|
31 |
import com.sun.net.httpserver.*;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Implements a basic static content HTTP server
|
|
35 |
* which understands text/html, text/plain content types
|
|
36 |
*
|
|
37 |
* Must be given an abs pathname to the document root.
|
|
38 |
* Directory listings together with text + html files
|
|
39 |
* can be served.
|
36131
|
40 |
*
|
|
41 |
* File Server created on files sub-path
|
|
42 |
*
|
|
43 |
* Echo server created on echo sub-path
|
2
|
44 |
*/
|
|
45 |
public class FileServerHandler implements HttpHandler {
|
|
46 |
|
|
47 |
public static void main (String[] args) throws Exception {
|
|
48 |
if (args.length != 3) {
|
|
49 |
System.out.println ("usage: java FileServerHandler rootDir port logfilename");
|
|
50 |
System.exit(1);
|
|
51 |
}
|
36131
|
52 |
Logger logger = Logger.getLogger("com.sun.net.httpserver");
|
|
53 |
ConsoleHandler ch = new ConsoleHandler();
|
|
54 |
logger.setLevel(Level.ALL);
|
|
55 |
ch.setLevel(Level.ALL);
|
|
56 |
logger.addHandler(ch);
|
|
57 |
|
2
|
58 |
String rootDir = args[0];
|
|
59 |
int port = Integer.parseInt (args[1]);
|
|
60 |
String logfile = args[2];
|
36131
|
61 |
HttpServer server = HttpServer.create (new InetSocketAddress (port), 0);
|
2
|
62 |
HttpHandler h = new FileServerHandler (rootDir);
|
36131
|
63 |
HttpHandler h1 = new EchoHandler ();
|
2
|
64 |
|
36131
|
65 |
HttpContext c = server.createContext ("/files", h);
|
2
|
66 |
c.getFilters().add (new LogFilter (new File (logfile)));
|
36131
|
67 |
HttpContext c1 = server.createContext ("/echo", h1);
|
|
68 |
c.getFilters().add (new LogFilter (new File (logfile)));
|
|
69 |
c1.getFilters().add (new LogFilter (new File (logfile)));
|
2
|
70 |
server.setExecutor (Executors.newCachedThreadPool());
|
|
71 |
server.start ();
|
|
72 |
}
|
|
73 |
|
|
74 |
String docroot;
|
|
75 |
|
|
76 |
FileServerHandler (String docroot) {
|
|
77 |
this.docroot = docroot;
|
|
78 |
}
|
|
79 |
|
|
80 |
int invocation = 1;
|
|
81 |
public void handle (HttpExchange t)
|
|
82 |
throws IOException
|
|
83 |
{
|
|
84 |
InputStream is = t.getRequestBody();
|
|
85 |
Headers map = t.getRequestHeaders();
|
|
86 |
Headers rmap = t.getResponseHeaders();
|
|
87 |
URI uri = t.getRequestURI();
|
|
88 |
String path = uri.getPath();
|
|
89 |
|
36131
|
90 |
int x = 0;
|
|
91 |
while (is.read () != -1) x++;
|
2
|
92 |
is.close();
|
|
93 |
File f = new File (docroot, path);
|
|
94 |
if (!f.exists()) {
|
|
95 |
notfound (t, path);
|
|
96 |
return;
|
|
97 |
}
|
|
98 |
String fixedrequest = map.getFirst ("XFixed");
|
|
99 |
|
|
100 |
String method = t.getRequestMethod();
|
|
101 |
if (method.equals ("HEAD")) {
|
|
102 |
rmap.set ("Content-Length", Long.toString (f.length()));
|
|
103 |
t.sendResponseHeaders (200, -1);
|
|
104 |
t.close();
|
|
105 |
} else if (!method.equals("GET")) {
|
|
106 |
t.sendResponseHeaders (405, -1);
|
|
107 |
t.close();
|
|
108 |
return;
|
|
109 |
}
|
|
110 |
|
|
111 |
if (path.endsWith (".html") || path.endsWith (".htm")) {
|
|
112 |
rmap.set ("Content-Type", "text/html");
|
|
113 |
} else {
|
|
114 |
rmap.set ("Content-Type", "text/plain");
|
|
115 |
}
|
|
116 |
if (f.isDirectory()) {
|
|
117 |
if (!path.endsWith ("/")) {
|
|
118 |
moved (t);
|
|
119 |
return;
|
|
120 |
}
|
|
121 |
rmap.set ("Content-Type", "text/html");
|
|
122 |
t.sendResponseHeaders (200, 0);
|
|
123 |
String[] list = f.list();
|
|
124 |
OutputStream os = t.getResponseBody();
|
|
125 |
PrintStream p = new PrintStream (os);
|
|
126 |
p.println ("<h2>Directory listing for: " + path+ "</h2>");
|
|
127 |
p.println ("<ul>");
|
|
128 |
for (int i=0; i<list.length; i++) {
|
|
129 |
p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
|
|
130 |
}
|
|
131 |
p.println ("</ul><p><hr>");
|
|
132 |
p.flush();
|
|
133 |
p.close();
|
|
134 |
} else {
|
|
135 |
int clen;
|
|
136 |
if (fixedrequest != null) {
|
|
137 |
clen = (int) f.length();
|
|
138 |
} else {
|
|
139 |
clen = 0;
|
|
140 |
}
|
|
141 |
t.sendResponseHeaders (200, clen);
|
|
142 |
OutputStream os = t.getResponseBody();
|
|
143 |
FileInputStream fis = new FileInputStream (f);
|
|
144 |
int count = 0;
|
|
145 |
try {
|
|
146 |
byte[] buf = new byte [16 * 1024];
|
|
147 |
int len;
|
|
148 |
while ((len=fis.read (buf)) != -1) {
|
|
149 |
os.write (buf, 0, len);
|
|
150 |
count += len;
|
|
151 |
}
|
|
152 |
} catch (IOException e) {
|
|
153 |
e.printStackTrace();
|
|
154 |
}
|
|
155 |
fis.close();
|
|
156 |
os.close();
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
void moved (HttpExchange t) throws IOException {
|
|
161 |
Headers req = t.getRequestHeaders();
|
|
162 |
Headers map = t.getResponseHeaders();
|
|
163 |
URI uri = t.getRequestURI();
|
|
164 |
String host = req.getFirst ("Host");
|
|
165 |
String location = "http://"+host+uri.getPath() + "/";
|
|
166 |
map.set ("Content-Type", "text/html");
|
|
167 |
map.set ("Location", location);
|
|
168 |
t.sendResponseHeaders (301, -1);
|
|
169 |
t.close();
|
|
170 |
}
|
|
171 |
|
|
172 |
void notfound (HttpExchange t, String p) throws IOException {
|
|
173 |
t.getResponseHeaders().set ("Content-Type", "text/html");
|
|
174 |
t.sendResponseHeaders (404, 0);
|
|
175 |
OutputStream os = t.getResponseBody();
|
|
176 |
String s = "<h2>File not found</h2>";
|
|
177 |
s = s + p + "<p>";
|
|
178 |
os.write (s.getBytes());
|
|
179 |
os.close();
|
|
180 |
t.close();
|
|
181 |
}
|
|
182 |
}
|
36131
|
183 |
|
|
184 |
class EchoHandler implements HttpHandler {
|
|
185 |
|
|
186 |
byte[] read(InputStream is) throws IOException {
|
|
187 |
byte[] buf = new byte[1024];
|
|
188 |
byte[] result = new byte[0];
|
|
189 |
|
|
190 |
while (true) {
|
|
191 |
int n = is.read(buf);
|
|
192 |
if (n > 0) {
|
|
193 |
byte[] b1 = new byte[result.length + n];
|
|
194 |
System.arraycopy(result, 0, b1, 0, result.length);
|
|
195 |
System.arraycopy(buf, 0, b1, result.length, n);
|
|
196 |
result = b1;
|
|
197 |
} else if (n == -1) {
|
|
198 |
return result;
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
public void handle (HttpExchange t)
|
|
204 |
throws IOException
|
|
205 |
{
|
|
206 |
InputStream is = t.getRequestBody();
|
|
207 |
Headers map = t.getRequestHeaders();
|
|
208 |
String fixedrequest = map.getFirst ("XFixed");
|
|
209 |
|
|
210 |
// return the number of bytes received (no echo)
|
|
211 |
String summary = map.getFirst ("XSummary");
|
|
212 |
if (fixedrequest != null && summary == null) {
|
|
213 |
byte[] in = read(is);
|
|
214 |
t.sendResponseHeaders(200, in.length);
|
|
215 |
OutputStream os = t.getResponseBody();
|
|
216 |
os.write(in);
|
|
217 |
os.close();
|
|
218 |
is.close();
|
|
219 |
} else {
|
|
220 |
OutputStream os = t.getResponseBody();
|
|
221 |
byte[] buf = new byte[64 * 1024];
|
|
222 |
t.sendResponseHeaders(200, 0);
|
|
223 |
int n, count=0;;
|
|
224 |
|
|
225 |
while ((n = is.read(buf)) != -1) {
|
|
226 |
if (summary == null) {
|
|
227 |
os.write(buf, 0, n);
|
|
228 |
}
|
|
229 |
count += n;
|
|
230 |
}
|
|
231 |
if (summary != null) {
|
|
232 |
String s = Integer.toString(count);
|
|
233 |
os.write(s.getBytes());
|
|
234 |
}
|
|
235 |
os.close();
|
|
236 |
is.close();
|
|
237 |
}
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|