X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=kona%2Fsrc%2Forg%2Ffeistymeow%2Fnetworking%2FBasicWebServer.java;h=b85896f6600322126f6b2230771490913c9ab1bc;hb=aa4f5c433c69d1769649b4b08860ad1127aed7bf;hp=7e374298ecc717af030d92c179b6c1c186de9b55;hpb=67d3d9a5e40685d3898f34a3651944c8a0c3e2ca;p=feisty_meow.git diff --git a/kona/src/org/feistymeow/networking/BasicWebServer.java b/kona/src/org/feistymeow/networking/BasicWebServer.java index 7e374298..b85896f6 100644 --- a/kona/src/org/feistymeow/networking/BasicWebServer.java +++ b/kona/src/org/feistymeow/networking/BasicWebServer.java @@ -7,8 +7,6 @@ import java.net.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.feistymeow.dragdrop.ListTransferable; - /** * Provides a lightweight way for RNS structures to be accessible over http. * @@ -20,7 +18,8 @@ import org.feistymeow.dragdrop.ListTransferable; * http://cs.fit.edu/~mmahoney/cse3103/java/Webserver.java */ -public class BasicWebServer { +public class BasicWebServer +{ static private Log logger = LogFactory.getLog(BasicWebServer.class); private int port; @@ -28,11 +27,13 @@ public class BasicWebServer { servingThread socketThread; private ServerSocket realSocket; - BasicWebServer(int portIn) { + BasicWebServer(int portIn) + { port = portIn; } - public void shutDown() { + public void shutDown() + { leaving = true; if (realSocket != null) { try { @@ -45,18 +46,21 @@ public class BasicWebServer { } } - public class servingThread implements Runnable { + public class servingThread implements Runnable + { private Thread thread; private ServerSocket serverSocket; - servingThread(ServerSocket socket) { + servingThread(ServerSocket socket) + { serverSocket = socket; thread = new Thread(this); thread.start(); } @Override - public void run() { + public void run() + { while (!leaving) { try { logger.debug("about to accept on server socket."); @@ -66,16 +70,15 @@ public class BasicWebServer { new ClientHandler(s); // Handle the client in a separate // thread } catch (Throwable cause) { - logger.error( - "exception raised while handling accepted socket", - cause); + logger.error("exception raised while handling accepted socket", cause); } } } } // enums for outcomes? really need better reporting. - public int startServing() { + public int startServing() + { if (socketThread != null) return 1; // already running outcome. try { @@ -89,42 +92,42 @@ public class BasicWebServer { return 0; } - public String predictMimeType(String filename) { + public String predictMimeType(String filename) + { // kludge to try one type: return "text/plain;charset=utf-8"; /* * - * if (filename.endsWith(".html") || filename.endsWith(".htm")) return - * "text/html"; if (filename.endsWith(".jpg") || - * filename.endsWith(".jpeg")) return "image/jpeg"; if - * (filename.endsWith(".gif")) return "image/gif"; if - * (filename.endsWith(".class")) return "application/octet-stream"; - * return "text/plain"; + * if (filename.endsWith(".html") || filename.endsWith(".htm")) return "text/html"; if + * (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) return "image/jpeg"; if + * (filename.endsWith(".gif")) return "image/gif"; if (filename.endsWith(".class")) return + * "application/octet-stream"; return "text/plain"; */ } // A ClientHandler reads an HTTP request and responds - class ClientHandler extends Thread { + class ClientHandler extends Thread + { private Socket socket; // The accepted socket from the Webserver // Start the thread in the constructor - public ClientHandler(Socket s) { + public ClientHandler(Socket s) + { socket = s; start(); } // Read the HTTP request, respond, and close the connection - public void run() { + public void run() + { try { logger.debug("into client run(): listening for gets."); // Open connections to the socket - BufferedReader in = new BufferedReader(new InputStreamReader( - socket.getInputStream())); - PrintStream out = new PrintStream(new BufferedOutputStream( - socket.getOutputStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + PrintStream out = new PrintStream(new BufferedOutputStream(socket.getOutputStream())); // Read filename from first input line "GET /filename.html ..." // or if not in this format, treat as a file not found. @@ -142,12 +145,9 @@ public class BasicWebServer { // get the command first. String command = st.nextToken(); // Parse the filename from the command. - if (st.hasMoreElements() && command.equalsIgnoreCase("GET") - && st.hasMoreElements()) { + if (st.hasMoreElements() && command.equalsIgnoreCase("GET") && st.hasMoreElements()) { filename = st.nextToken(); - } else if (st.hasMoreElements() - && command.equalsIgnoreCase("HEAD") - && st.hasMoreElements()) { + } else if (st.hasMoreElements() && command.equalsIgnoreCase("HEAD") && st.hasMoreElements()) { filename = st.nextToken(); transferFile = false; // don't need to do that, just the // header. @@ -175,23 +175,18 @@ public class BasicWebServer { // Check for illegal characters to prevent access to // superdirectories - if (filename.indexOf("..") >= 0 - || filename.indexOf(':') >= 0 - || filename.indexOf('|') >= 0) + if (filename.indexOf("..") >= 0 || filename.indexOf(':') >= 0 || filename.indexOf('|') >= 0) throw new FileNotFoundException(); logger.info("got past filename checks for: " + filename); /* - * this doesn't actually check that trailing slash is - * missing! // If a directory is requested and the trailing - * / is missing, // send the client an HTTP request to - * append it. (This is // necessary for relative links to - * work correctly in the client). if ((new - * GeniiPath(filename)).isDirectory()) { - * out.print("HTTP/1.0 301 Moved Permanently\r\n" + - * "Location: /" + filename + "/\r\n\r\n"); out.close(); - * return; } + * this doesn't actually check that trailing slash is missing! // If a directory + * is requested and the trailing / is missing, // send the client an HTTP + * request to append it. (This is // necessary for relative links to work + * correctly in the client). if ((new GeniiPath(filename)).isDirectory()) { + * out.print("HTTP/1.0 301 Moved Permanently\r\n" + "Location: /" + filename + + * "/\r\n\r\n"); out.close(); return; } */ // trying to get around worrying about mime types by saying @@ -201,30 +196,26 @@ public class BasicWebServer { File source = new File(filename); if (!source.exists()) { - logger.error("source does not exist for serving: " - + filename); + logger.error("source does not exist for serving: " + filename); // do something! // hmmm: below could be abstracted to more general // denial method. - out.println("HTTP/1.1 404 Not Found\r\n" - + "Content-type: text/html\r\n\r\n" - + "" + filename - + " not found\n"); + out.println("HTTP/1.1 404 Not Found\r\n" + "Content-type: text/html\r\n\r\n" + + "" + filename + " not found\n"); out.close(); } - out.print("HTTP/1.1 200 OK\r\n" + "Content-type: " - + mimeType + "\r\n" + "Connection: close" + "\r\n" - // // + "\r\nContent-Length: " + source.size? + - // "\r\n" - + "\r\n"); + out.print("HTTP/1.1 200 OK\r\n" + "Content-type: " + mimeType + "\r\n" + "Connection: close" + "\r\n" + // // + "\r\nContent-Length: " + source.size? + + // "\r\n" + + "\r\n"); if (!transferFile) { logger.debug("closing stream for finished HEAD request."); out.close(); return; } logger.debug("moving to handle GET request."); - InputStream f = source.openInputStream(); + FileInputStream f = new FileInputStream(filename); logger.debug("opened stream on source"); // Send file contents to client, then close the connection. byte[] a = new byte[4096]; @@ -232,18 +223,16 @@ public class BasicWebServer { while ((n = f.read(a)) > 0) out.write(a, 0, n); logger.debug("wrote file back for request, closing stream."); + f.close(); out.close(); } catch (FileNotFoundException x) { logger.error("failed to find requested file: " + filename); - out.println("HTTP/1.1 404 Not Found\r\n" - + "Content-type: text/html\r\n\r\n" - + "" + filename - + " not found\n"); + out.println("HTTP/1.1 404 Not Found\r\n" + "Content-type: text/html\r\n\r\n" + "" + + filename + " not found\n"); out.close(); } } catch (IOException x) { - logger.error("exception blew out in outer area of web server", - x); + logger.error("exception blew out in outer area of web server", x); // / System.out.println(x); } }