rava2
home
documentation
examples
import java.io.File; import java.io.FileReader; import java.io.BufferedReader; import java.util.HashMap; public class Main { static HashMap mimeTypes; public static void main(String[] args) { int port = 8081; mimeTypes = new HashMap(); initMimeTypes(); System.out.println("Rava Multithreaded Web Server"); System.out.println("Serving current directory on port " + port); System.out.println("Each request runs in its own thread.\n"); ServerSocket server = new ServerSocket(port); while (true) { Socket client = server.accept(); if (client != null) { // Create a new thread for each client Thread handler = new Thread(() -> { handleRequest(client); }); handler.start(); } } } static void initMimeTypes() { mimeTypes.put(".html", "text/html"); mimeTypes.put(".css", "text/css"); mimeTypes.put(".js", "application/javascript"); mimeTypes.put(".json", "application/json"); mimeTypes.put(".txt", "text/plain"); mimeTypes.put(".java", "text/plain"); mimeTypes.put(".c", "text/x-csrc"); mimeTypes.put(".h", "text/x-chdr"); mimeTypes.put(".md", "text/markdown"); mimeTypes.put(".sh", "application/x-sh"); mimeTypes.put(".xml", "application/xml"); mimeTypes.put(".png", "image/png"); mimeTypes.put(".jpg", "image/jpeg"); mimeTypes.put(".gif", "image/gif"); mimeTypes.put(".ico", "image/x-icon"); mimeTypes.put(".o", "application/octet-stream"); mimeTypes.put("Makefile", "text/plain"); } static void handleRequest(Socket client) { try { var input = client.getInputStream(); var output = client.getOutputStream(); String request = input.readLine(); if (request == null || request.length() == 0) { client.close(); return; } String path = extractPath(request); if (path == null) path = "/"; // Note: System.out is synchronized in Rava System.out.println("[" + Thread.currentThread().getName() + "] GET " + path); consumeHeaders(input); if (path.equals("/")) { serveDirectory(output, new File("."), "/"); } else { File file = new File("." + path); if (file.exists()) { if (file.isDirectory()) { serveDirectory(output, file, path); } else { serveFile(output, file); } } else { serve404(output, path); } } } finally { client.close(); } } static String extractPath(String request) { int start = request.indexOf(" "); if (start < 0) return "/"; int end = request.indexOf(" ", start + 1); if (end < 0) return request.substring(start + 1); return request.substring(start + 1, end); } static void consumeHeaders(SocketInputStream input) { while (true) { String line = input.readLine(); if (line == null || line.length() == 0) break; } } static void serveFile(SocketOutputStream out, File file) { String content = readFile(file); String type = getContentType(file.getName()); StringBuilder resp = new StringBuilder(); resp.append("HTTP/1.1 200 OK\r\n"); resp.append("Content-Type: ").append(type).append("\r\n"); resp.append("Content-Length: ").append(content.length()).append("\r\n"); resp.append("Connection: close\r\n\r\n"); resp.append(content); out.write(resp.toString()); } static void serveDirectory(SocketOutputStream out, File dir, String urlPath) { StringBuilder html = new StringBuilder(); html.append("<html><body><h1>Index of ").append(urlPath).append("</h1><hr><ul>"); var files = dir.list(); if (files != null) { for (int i = 0; i < files.length; i++) { String name = files[i]; html.append("<li><a href=\"").append(name).append("\">\”").append(name).append("</a></li>"); } } html.append("</ul></body></html>"); String body = html.toString(); StringBuilder resp = new StringBuilder(); resp.append("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n"); resp.append("Content-Length: ").append(body.length()).append("\r\n"); resp.append("Connection: close\r\n\r\n").append(body); out.write(resp.toString()); } static void serve404(SocketOutputStream out, String path) { String body = "<html><body><h1>404 Not Found</h1><p>" + path + "</p></body></html>"; out.write("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: " + body.length() + "\r\n\r\n" + body); } static String readFile(File file) { try { StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(file.getPath())); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } br.close(); return sb.toString(); } catch (Exception e) { return ""; } } static String getContentType(String name) { int dot = name.lastIndexOf("."); if (dot > 0) { String ext = name.substring(dot); if (mimeTypes.containsKey(ext)) return (String)mimeTypes.get(ext); } return "application/octet-stream"; } }
run program
ready