map = new HashMap<>();
+ // are we doing a GET or just a HEAD
+ boolean doingGet;
+ // beginning of file name
+ int index;
+ if (buf[0] == (byte) 'G' &&
+ buf[1] == (byte) 'E' &&
+ buf[2] == (byte) 'T' &&
+ buf[3] == (byte) ' ') {
+ doingGet = true;
+ index = 4;
+ } else if (buf[0] == (byte) 'H' &&
+ buf[1] == (byte) 'E' &&
+ buf[2] == (byte) 'A' &&
+ buf[3] == (byte) 'D' &&
+ buf[4] == (byte) ' ') {
+ doingGet = false;
+ index = 5;
+ } else {
+ // we don't support this method
+ ps.print("HTTP/1.0 " + HTTP_BAD_METHOD + " unsupported method type: ");
+ ps.write(buf, 0, 5);
+ ps.write(EOL);
+ ps.flush();
+ s.close();
+ return;
+ }
- static {
- fillMap();
- }
- static void setSuffix(String k, String v) {
- map.put(k, v);
- }
-
- static void fillMap() {
- setSuffix("", "content/unknown");
-
- setSuffix(".uu", "application/octet-stream");
- setSuffix(".exe", "application/octet-stream");
- setSuffix(".ps", "application/postscript");
- setSuffix(".zip", "application/zip");
- setSuffix(".sh", "application/x-shar");
- setSuffix(".tar", "application/x-tar");
- setSuffix(".snd", "audio/basic");
- setSuffix(".au", "audio/basic");
- setSuffix(".wav", "audio/x-wav");
-
- setSuffix(".gif", "image/gif");
- setSuffix(".jpg", "image/jpeg");
- setSuffix(".jpeg", "image/jpeg");
-
- setSuffix(".htm", "text/html");
- setSuffix(".html", "text/html");
- setSuffix(".css", "text/css");
- setSuffix(".java", "text/javascript");
-
- setSuffix(".txt", "text/plain");
- setSuffix(".java", "text/plain");
-
- setSuffix(".c", "text/plain");
- setSuffix(".cc", "text/plain");
- setSuffix(".c++", "text/plain");
- setSuffix(".h", "text/plain");
- setSuffix(".pl", "text/plain");
- }
-
- /*
- void listDirectory(File dir, PrintStream ps) {
- ps.println("Directory listing\n");
- ps.println("Parent Directory
\n");
- String[] list = dir.list();
- for (int i = 0; list != null && i < list.length; i++) {
- File f = new File(dir, list[i]);
- if (f.isDirectory()) {
- ps.println(""+list[i]+"/
");
- } else {
- ps.println(""+list[i]+"
" + (new Date()) + "");
+ }
+
+ String filename = new String(buf, index, i - index);
+ // get the zip entry, remove the front slash
+ ZipEntry entry = entries.get(filename.substring(1));
+ boolean ok = printHeaders(entry, ps);
+ if (entry != null) {
+ InputStream stream = zip.getInputStream(entry);
+ if (doingGet && ok) {
+ sendFile(stream, ps);
+ }
+ } else {
+ send404(ps);
+ }
+ } finally {
+ s.close();
}
- */
+ }
+
+
+ boolean printHeaders(ZipEntry entry, PrintStream ps) throws IOException {
+ boolean ret;
+ int rCode;
+ if (entry == null) {
+ rCode = HTTP_NOT_FOUND;
+ ps.print("HTTP/1.0 " + HTTP_NOT_FOUND + " Not Found");
+ ps.write(EOL);
+ ret = false;
+ } else {
+ rCode = HTTP_OK;
+ ps.print("HTTP/1.0 " + HTTP_OK + " OK");
+ ps.write(EOL);
+ ret = true;
+ }
+ if (entry != null) {
+ Messages.log("From " + s.getInetAddress().getHostAddress() + ": GET " + entry.getName() + " --> " + rCode);
+ }
+ ps.print("Server: Processing Documentation Server");
+ ps.write(EOL);
+ ps.print("Date: " + new Date());
+ ps.write(EOL);
+ if (ret) {
+ if (!entry.isDirectory()) {
+ ps.print("Content-length: " + entry.getSize());
+ ps.write(EOL);
+ ps.print("Last Modified: " + new Date(entry.getTime()));
+ ps.write(EOL);
+ String name = entry.getName();
+ int ind = name.lastIndexOf('.');
+ String ct = null;
+ if (ind > 0) {
+ ct = map.get(name.substring(ind));
+ }
+ if (ct == null) {
+ //System.err.println("unknown content type " + name.substring(ind));
+ ct = "application/x-unknown-content-type";
+ }
+ ps.print("Content-type: " + ct);
+ } else {
+ ps.print("Content-type: text/html");
+ }
+ ps.write(EOL);
+ }
+ ps.write(EOL); // adding another newline here [fry]
+ return ret;
+ }
+
+
+ void send404(PrintStream ps) throws IOException {
+ ps.write(EOL);
+ ps.write(EOL);
+ ps.print("404 Not Found
" +
+ "The requested resource was not found.");
+ ps.write(EOL);
+ ps.write(EOL);
+ }
+
+
+ void sendFile(InputStream is, PrintStream ps) throws IOException {
+ try (is) {
+ int n;
+ while ((n = is.read(buf)) > 0) {
+ ps.write(buf, 0, n);
+ }
+ }
+ }
+
+
+ /** mapping of file extensions to content-types */
+ static Map map = new ConcurrentHashMap<>();
+
+ static {
+ map.put("", "content/unknown");
+
+ map.put(".uu", "application/octet-stream");
+ map.put(".exe", "application/octet-stream");
+ map.put(".ps", "application/postscript");
+ map.put(".zip", "application/zip");
+ map.put(".sh", "application/x-shar");
+ map.put(".tar", "application/x-tar");
+ map.put(".snd", "audio/basic");
+ map.put(".au", "audio/basic");
+ map.put(".wav", "audio/x-wav");
+
+ map.put(".gif", "image/gif");
+ map.put(".jpg", "image/jpeg");
+ map.put(".jpeg", "image/jpeg");
+
+ map.put(".htm", "text/html");
+ map.put(".html", "text/html");
+ map.put(".css", "text/css");
+ map.put(".js", "text/javascript");
+
+ map.put(".txt", "text/plain");
+ map.put(".java", "text/plain");
+
+ map.put(".c", "text/plain");
+ map.put(".cc", "text/plain");
+ map.put(".c++", "text/plain");
+ map.put(".h", "text/plain");
+ map.put(".pl", "text/plain");
+ }
}
-interface HttpConstants {
- /* 2XX: generally "OK" */
- int HTTP_OK = 200;
-// int HTTP_CREATED = 201;
-// int HTTP_ACCEPTED = 202;
-// int HTTP_NOT_AUTHORITATIVE = 203;
-// int HTTP_NO_CONTENT = 204;
-// int HTTP_RESET = 205;
-// int HTTP_PARTIAL = 206;
-
- /* 3XX: relocation/redirect */
-// int HTTP_MULT_CHOICE = 300;
-// int HTTP_MOVED_PERM = 301;
-// int HTTP_MOVED_TEMP = 302;
-// int HTTP_SEE_OTHER = 303;
-// int HTTP_NOT_MODIFIED = 304;
-// int HTTP_USE_PROXY = 305;
-
- /* 4XX: client error */
-// int HTTP_BAD_REQUEST = 400;
-// int HTTP_UNAUTHORIZED = 401;
-// int HTTP_PAYMENT_REQUIRED = 402;
-// int HTTP_FORBIDDEN = 403;
- int HTTP_NOT_FOUND = 404;
- int HTTP_BAD_METHOD = 405;
-// int HTTP_NOT_ACCEPTABLE = 406;
-// int HTTP_PROXY_AUTH = 407;
-// int HTTP_CLIENT_TIMEOUT = 408;
-// int HTTP_CONFLICT = 409;
-// int HTTP_GONE = 410;
-// int HTTP_LENGTH_REQUIRED = 411;
-// int HTTP_PRECON_FAILED = 412;
-// int HTTP_ENTITY_TOO_LARGE = 413;
-// int HTTP_REQ_TOO_LONG = 414;
-// int HTTP_UNSUPPORTED_TYPE = 415;
-
- /* 5XX: server error */
-// int HTTP_SERVER_ERROR = 500;
-// int HTTP_INTERNAL_ERROR = 501;
-// int HTTP_BAD_GATEWAY = 502;
-// int HTTP_UNAVAILABLE = 503;
-// int HTTP_GATEWAY_TIMEOUT = 504;
-// int HTTP_VERSION = 505;
-}