|
1 /* |
|
2 * Copyright (c) 2017, Oracle and/or its affiliates. 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 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. |
|
22 */ |
|
23 package jaxp.library; |
|
24 |
|
25 import com.sun.net.httpserver.Headers; |
|
26 import com.sun.net.httpserver.HttpContext; |
|
27 import com.sun.net.httpserver.HttpExchange; |
|
28 import com.sun.net.httpserver.HttpHandler; |
|
29 import com.sun.net.httpserver.HttpServer; |
|
30 import java.io.File; |
|
31 import java.io.FileInputStream; |
|
32 import java.io.IOException; |
|
33 import java.io.InputStream; |
|
34 import java.io.OutputStream; |
|
35 import java.net.InetSocketAddress; |
|
36 import java.net.URI; |
|
37 import java.util.concurrent.ExecutorService; |
|
38 import java.util.concurrent.Executors; |
|
39 |
|
40 /** |
|
41 * A simple HTTP Server |
|
42 */ |
|
43 public class SimpleHttpServer { |
|
44 HttpServer _httpserver; |
|
45 ExecutorService _executor; |
|
46 |
|
47 String _address; |
|
48 |
|
49 String _context, _docroot; |
|
50 int _port; |
|
51 |
|
52 public SimpleHttpServer(String context, String docroot) { |
|
53 //let the system pick up an ephemeral port in a bind operation |
|
54 this(0, context, docroot); |
|
55 } |
|
56 |
|
57 public SimpleHttpServer(int port, String context, String docroot) { |
|
58 _port = port; |
|
59 _context = context; |
|
60 _docroot = docroot; |
|
61 } |
|
62 |
|
63 public void start() { |
|
64 MyHttpHandler handler = new MyHttpHandler(_docroot); |
|
65 InetSocketAddress addr = new InetSocketAddress(_port); |
|
66 try { |
|
67 _httpserver = HttpServer.create(addr, 0); |
|
68 } catch (IOException ex) { |
|
69 throw new RuntimeException("cannot create httpserver", ex); |
|
70 } |
|
71 |
|
72 //TestHandler is mapped to /test |
|
73 HttpContext ctx = _httpserver.createContext(_context, handler); |
|
74 |
|
75 _executor = Executors.newCachedThreadPool(); |
|
76 _httpserver.setExecutor(_executor); |
|
77 _httpserver.start(); |
|
78 |
|
79 _address = "http://localhost:" + _httpserver.getAddress().getPort(); |
|
80 } |
|
81 |
|
82 public void stop() { |
|
83 _httpserver.stop(2); |
|
84 _executor.shutdown(); |
|
85 } |
|
86 |
|
87 public String getAddress() { |
|
88 return _address; |
|
89 } |
|
90 |
|
91 static class MyHttpHandler implements HttpHandler { |
|
92 |
|
93 String _docroot; |
|
94 |
|
95 public MyHttpHandler(String docroot) { |
|
96 _docroot = docroot; |
|
97 } |
|
98 |
|
99 public void handle(HttpExchange t) |
|
100 throws IOException { |
|
101 InputStream is = t.getRequestBody(); |
|
102 Headers map = t.getRequestHeaders(); |
|
103 Headers rmap = t.getResponseHeaders(); |
|
104 OutputStream os = t.getResponseBody(); |
|
105 URI uri = t.getRequestURI(); |
|
106 String path = uri.getPath(); |
|
107 |
|
108 |
|
109 while (is.read() != -1) ; |
|
110 is.close(); |
|
111 |
|
112 File f = new File(_docroot, path); |
|
113 if (!f.exists()) { |
|
114 notfound(t, path); |
|
115 return; |
|
116 } |
|
117 |
|
118 String method = t.getRequestMethod(); |
|
119 if (method.equals("HEAD")) { |
|
120 rmap.set("Content-Length", Long.toString(f.length())); |
|
121 t.sendResponseHeaders(200, -1); |
|
122 t.close(); |
|
123 } else if (!method.equals("GET")) { |
|
124 t.sendResponseHeaders(405, -1); |
|
125 t.close(); |
|
126 return; |
|
127 } |
|
128 |
|
129 if (path.endsWith(".html") || path.endsWith(".htm")) { |
|
130 rmap.set("Content-Type", "text/html"); |
|
131 } else { |
|
132 rmap.set("Content-Type", "text/plain"); |
|
133 } |
|
134 |
|
135 t.sendResponseHeaders (200, f.length()); |
|
136 |
|
137 FileInputStream fis = new FileInputStream(f); |
|
138 int count = 0; |
|
139 try { |
|
140 byte[] buf = new byte[16 * 1024]; |
|
141 int len; |
|
142 while ((len = fis.read(buf)) != -1) { |
|
143 os.write(buf, 0, len); |
|
144 count += len; |
|
145 } |
|
146 } catch (IOException e) { |
|
147 e.printStackTrace(); |
|
148 } |
|
149 fis.close(); |
|
150 os.close(); |
|
151 } |
|
152 |
|
153 void moved(HttpExchange t) throws IOException { |
|
154 Headers req = t.getRequestHeaders(); |
|
155 Headers map = t.getResponseHeaders(); |
|
156 URI uri = t.getRequestURI(); |
|
157 String host = req.getFirst("Host"); |
|
158 String location = "http://" + host + uri.getPath() + "/"; |
|
159 map.set("Content-Type", "text/html"); |
|
160 map.set("Location", location); |
|
161 t.sendResponseHeaders(301, -1); |
|
162 t.close(); |
|
163 } |
|
164 |
|
165 void notfound(HttpExchange t, String p) throws IOException { |
|
166 t.getResponseHeaders().set("Content-Type", "text/html"); |
|
167 t.sendResponseHeaders(404, 0); |
|
168 OutputStream os = t.getResponseBody(); |
|
169 String s = "<h2>File not found</h2>"; |
|
170 s = s + p + "<p>"; |
|
171 os.write(s.getBytes()); |
|
172 os.close(); |
|
173 t.close(); |
|
174 } |
|
175 } |
|
176 |
|
177 } |