package net.unternet.jc; import eve.sys.Vm; import java.net.*; import java.io.*; import eve.zipfile.ZipFile; public class HTTPServer { public static int PORT = 8000; public static int BACKLOG = 5; // how many connections we can listen for public static void serve() { ServerSocket httpServer = null; Socket connection = null; try { httpServer = new ServerSocket(PORT, BACKLOG); } catch (IOException fatal) { System.err.println("Cannot start server: " + fatal); Vm.exit(1); } while (true) { try { connection = httpServer.accept(); } catch (IOException nonfatal) { System.err.println("Cannot accept connection: " + nonfatal); } handle(connection); try {connection.close();} catch (IOException ignored) {} } } public static void handle(Socket connection) { DataInputStream input = null; String line = "", filename = null; try { input = new DataInputStream(connection.getInputStream()); } catch (IOException problem) { System.err.println("Cannot get input stream: " + problem); return; } while (true) { try { line = input.readLine(); if (line == null) throw new IOException("null received"); if (line.length() == 0) throw new IOException("end of headers"); } catch (IOException eof) { // could be something else, but treat as EOF System.err.println("EOF found: " + eof); line = null; break; // out of "while" loop } System.err.println("got line: " + line); if (line.substring(0, 4).equalsIgnoreCase("GET ")) { filename = line.substring(4, line.indexOf(' ', 4)); } } if (filename != null) sendfile(connection, filename); } public static String contentType(String filename) { String mimetype = "text/html"; // default if (filename.endsWith(".txt")) mimetype = "text/plain"; else if (filename.endsWith(".jpg")) mimetype = "image/jpeg"; else if (filename.endsWith(".png")) mimetype = "image/png"; else if (filename.endsWith(".gif")) mimetype = "image/gif"; return "Content-type: " + mimetype; } public static void sendfile(Socket connection, String filename) { DataOutputStream output = null; String fileContents = null; try { output = new DataOutputStream(connection.getOutputStream()); } catch (Exception nonfatal) { System.err.println("Could not get output stream: " + nonfatal); } if (output != null) try { fileContents = getZipFile(filename); if (fileContents == null) { output.writeBytes("HTTP/0.9 404 Not Found\r\n"); return; } else { output.writeBytes("HTTP/0.9 200 OK\r\n"); output.writeBytes(contentType(filename) + "\r\n\r\n"); output.writeBytes(fileContents); } } catch (Exception nonfatal) { System.err.println("Cannot write output: " + nonfatal); } } public static String getZipFile(String filename) { ZipFile zipfile = null; InputStream input = null; String contents = ""; byte[] buffer = new byte[4096]; int count = 0; if (filename.equals("/")) filename = "/index.html"; while (filename.startsWith("/")) filename = filename.substring(1); System.err.println("trying to open Zip file"); try { zipfile = new ZipFile(Vm.openResourceAsRandomStream(null, "net/unternet/jc/_filesystem.zip")); } catch (Exception fatal) { System.err.println("Cannot open _filesystem.zip: " + fatal); Vm.exit(1); } System.err.println("trying to fetch \"" + filename + "\" from Zip file"); try { input = zipfile.getInputStream(zipfile.getEntry(filename)); } catch (Exception fatal) { System.err.println("Could not get " + filename + " contents: " + fatal); return null; } System.err.println("trying to read \"" + filename + "\""); try { while ((count = input.read(buffer)) > 0) { System.err.println("read " + count + " bytes into buffer"); contents += new String(buffer, 0, count); System.err.println("contents now " + contents.length() + " bytes long"); } } catch (Exception nonfatal) { System.err.println("error during file read: " + nonfatal); } if (filename.endsWith(".html")) { System.err.println("returning contents: " + contents); } else { System.err.println("returning contents with length: " + contents.length()); } return contents; } public static void main(String[] args) { Vm.startEve(args); serve(); System.err.println("Server started"); } }