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 = 8080; mimeTypes = new HashMap(); initMimeTypes(); System.out.println("Rava Web Server"); System.out.println("Serving current directory on port " + port); System.out.println("Press Ctrl+C to stop\n"); ServerSocket server = new ServerSocket(port); while (true) { Socket client = server.accept(); handleRequest(client); } } 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"); // Scan current directory for any other extensions File dir = new File("."); String[] files = dir.list(); if (files != null) { for (int i = 0; i < files.length; i++) { String name = files[i]; int dot = name.lastIndexOf("."); if (dot > 0) { String ext = name.substring(dot); if (!mimeTypes.containsKey(ext)) { mimeTypes.put(ext, "application/octet-stream"); } } } } } static void handleRequest(Socket client) { if (client == null) return; var input = client.getInputStream(); var output = client.getOutputStream(); if (input == null || output == null) { client.close(); return; } String request = input.readLine(); if (request == null || request.length() == 0) { client.close(); return; } String path = extractPath(request); if (path == null) path = "/"; System.out.println("GET " + path); consumeHeaders(input); if (path.equals("/")) { File dir = new File("."); File index = new File("./index.html"); if (index.exists()) { serveFile(output, index); } else { serveDirectory(output, dir, "/"); } } else { File file = new File("." + path); if (file.exists()) { if (file.isDirectory()) { File index = new File(file.getPath() + "/index.html"); if (index.exists()) { serveFile(output, index); } else { serveDirectory(output, file, path); } } else { serveFile(output, file); } } else { serve404(output, path); } } client.close(); } static String extractPath(String request) { int start = request.indexOf(" "); if (start < 0) return "/"; int end = request.indexOf(" ", start + 1); String path; if (end < 0) { path = request.substring(start + 1); } else { path = request.substring(start + 1, end); } return path; } static void consumeHeaders(SocketInputStream input) { while (true) { String line = input.readLine(); if (line == null) break; if (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: "); resp.append(type); resp.append("\r\n"); resp.append("Content-Length: "); resp.append(content.length()); resp.append("\r\n"); resp.append("Connection: close\r\n"); resp.append("\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><head><title>Index of "); html.append(urlPath); html.append("</title>"); html.append("<style>"); html.append("body{font-family:monospace;margin:20px}"); html.append("a{text-decoration:none;color:#0066cc}"); html.append("a:hover{text-decoration:underline}"); html.append(".d{font-weight:bold}"); html.append("table{border-collapse:collapse}"); html.append("td{padding:4px 16px 4px 4px}"); html.append("</style></head><body>"); html.append("<h1>Index of "); html.append(urlPath); html.append("</h1><hr><table>"); if (!urlPath.equals("/")) { html.append("<tr><td class=\"d\"><a href=\"..\">..</a></td><td>-</td></tr>"); } var files = dir.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { var f = files[i]; String name = f.getName(); String href = urlPath.endsWith("/") ? urlPath + name : urlPath + "/" + name; html.append("<tr><td"); if (f.isDirectory()) { html.append(" class=\"d\"><a href=\""); html.append(href); html.append("/\">"); html.append(name); html.append("/</a></td><td>-</td>"); } else { html.append("><a href=\""); html.append(href); html.append("\">"); html.append(name); html.append("</a></td><td>"); html.append(f.length()); html.append(" B</td>"); } html.append("</tr>"); } } html.append("</table><hr><p><i>Rava Web Server</i></p></body></html>"); String body = html.toString(); StringBuilder resp = new StringBuilder(); resp.append("HTTP/1.1 200 OK\r\n"); resp.append("Content-Type: text/html\r\n"); resp.append("Content-Length: "); resp.append(body.length()); resp.append("\r\n"); resp.append("Connection: close\r\n"); resp.append("\r\n"); resp.append(body); out.write(resp.toString()); } static void serve404(SocketOutputStream out, String path) { String body = "<html><head><title>404 Not Found</title></head>"; body = body + "<body><h1>404 Not Found</h1><p>" + path + "</p></body></html>"; StringBuilder resp = new StringBuilder(); resp.append("HTTP/1.1 404 Not Found\r\n"); resp.append("Content-Type: text/html\r\n"); resp.append("Content-Length: "); resp.append(body.length()); resp.append("\r\n"); resp.append("Connection: close\r\n"); resp.append("\r\n"); resp.append(body); out.write(resp.toString()); } static String readFile(File file) { StringBuilder sb = new StringBuilder(); FileReader fr = new FileReader(file.getPath()); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); while (line != null) { sb.append(line); sb.append("\n"); line = br.readLine(); } br.close(); fr.close(); return sb.toString(); } 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); } } else { if (mimeTypes.containsKey(name)) { return (String)mimeTypes.get(name); } } return "application/octet-stream"; } }
run program
ready