-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleHTTPServer.java
72 lines (64 loc) · 2.57 KB
/
SimpleHTTPServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class SimpleHTTPServer {
private static final int PORT = 8000;
private static final String ROOT_DIR = ".";
private static final Map<String, String> MIME_TYPES = new HashMap<>();
static {
MIME_TYPES.put(".html", "text/html");
MIME_TYPES.put(".css", "text/css");
MIME_TYPES.put(".js", "application/javascript");
MIME_TYPES.put(".webp", "image/webp");
}
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
server.createContext("/", new FileHandler());
server.setExecutor(null);
server.start();
System.out.println("Server started on port " + PORT);
System.out.println("Open http://localhost:" + PORT + " in your browser");
}
static class FileHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
if (path.equals("/")) {
path = "/index.html";
}
File file = new File(ROOT_DIR + path);
if (!file.exists() || file.isDirectory()) {
String response = "404 Not Found";
exchange.sendResponseHeaders(404, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
}
return;
}
String contentType = getContentType(file.getName());
exchange.getResponseHeaders().set("Content-Type", contentType);
exchange.sendResponseHeaders(200, file.length());
try (OutputStream os = exchange.getResponseBody()) {
Files.copy(file.toPath(), os);
}
}
private String getContentType(String fileName) {
int dot = fileName.lastIndexOf('.');
if (dot > 0) {
String extension = fileName.substring(dot).toLowerCase();
String mimeType = MIME_TYPES.get(extension);
if (mimeType != null) {
return mimeType;
}
}
return "application/octet-stream";
}
}
}