Advertisement
Tyler_Elric

Untitled

Mar 19th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. import java.net.*;
  2. import java.nio.charset.Charset;
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.Date;
  6. import java.awt.Desktop;
  7. import java.net.URI;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10.  
  11. public class Main implements Runnable {
  12.  
  13.     BufferedReader req;
  14.     OutputStream res;
  15.     String path = null;
  16.     HashMap<String,String> headers = new HashMap<String,String>();
  17.     HashMap<String,String> parms = new HashMap<String,String>();
  18.     Socket sock;
  19.     boolean override_status = false;
  20.     String mime = "text/html";
  21.     Charset charset = Charset.defaultCharset();
  22.  
  23.     public static void main(String[] args) throws Exception {
  24.  
  25.         // Listen for a connection from a client
  26.         ServerSocket serverSocket = new ServerSocket(1234);
  27.         if(Desktop.isDesktopSupported())
  28.             Desktop.getDesktop().browse(new URI("http://localhost:1234"));
  29.         else
  30.             System.out.println("Please direct your browser to http://localhost:1234.");
  31.         ExecutorService executor = Executors.newFixedThreadPool(5);
  32.         while(true) {
  33.             Main client = new Main();
  34.             client.sock = serverSocket.accept();
  35.             client.req = new BufferedReader(new InputStreamReader(client.sock.getInputStream()));
  36.             client.res = client.sock.getOutputStream();
  37.             executor.execute(client);
  38.         }
  39.     }
  40.  
  41.     @Override
  42.     public void run() {
  43.  
  44.         String dateString = (new Date()).toGMTString();
  45.         String inputLine;
  46.         try {
  47.             while ((inputLine = req.readLine()) != null) {
  48.             //System.out.println("The client said: " + inputLine);
  49.             if(path==null) {
  50.                 int a = inputLine.indexOf(" ")+1;
  51.                 path = inputLine.substring(a,inputLine.indexOf(" ",a));
  52.             }
  53.             if(inputLine.length() <= 2)
  54.                 break;
  55.             }
  56.         } catch(Exception ex) {
  57.             ex.printStackTrace();
  58.         }
  59.  
  60.         try {
  61.             byte[] payload = process();
  62.  
  63.             if(!override_status)
  64.                 res.write("HTTP/1.1 200 OK\r\n".getBytes(charset));
  65.             res.write(("Content-Length: " + Integer.toString(payload.length) + "\r\n").getBytes(charset));
  66.             if(mime!=null) {
  67.                 res.write(("Content-Type: " + mime + "\r\n").getBytes(charset));
  68.             } else {
  69.                 // initiate file download.
  70.                 res.write(("Content-Type: application/octet-stream\r\nContent-Disposition: attachment; filename=" + path.substring(path.indexOf("/")+1).replace("/","_")+"\r\n").getBytes(charset));
  71.             }
  72.             res.write(("Date: "  + dateString + "\r\n").getBytes(charset));
  73.             res.write(("Last-Modified: " + dateString + "\r\n").getBytes(charset));
  74.             res.write("Connection: close\r\n".getBytes(charset));
  75.             res.write("\r\n".getBytes(charset));
  76.  
  77.             // Send the payload
  78.             res.write(payload);
  79.  
  80.             res.close();
  81.         } catch(Exception ex) {
  82.             try {
  83.                 ex.printStackTrace();
  84.                 res.write("HTTP/1.1 500 Internal Server Error\r\n".getBytes(charset));
  85.                 res.write(("Date: " + dateString + "\r\n").getBytes(charset));
  86.                 res.write("Connection: close\r\n".getBytes(charset));
  87.                 res.write("\r\n".getBytes(charset));
  88.  
  89.                 res.close();
  90.             } catch(Exception ex_) {
  91.                 ex_.printStackTrace();
  92.             }
  93.         }
  94.     }
  95.  
  96.     public static String get_mime(String extension) {
  97.         switch(extension) {
  98.             case ".ico":
  99.                 return "image/x-icon";
  100.             case ".png":
  101.                 return "image/png";
  102.             case ".jpg":
  103.             case ".jpeg":
  104.                 return "image/jpeg";
  105.             case ".gif":
  106.                 return "image/gif";
  107.             case ".js":
  108.                 return "text/javascript";
  109.             case ".css":
  110.                 return "text/css";
  111.             case ".html":
  112.                 return "text/html";
  113.             default: return null;
  114.         }
  115.     }
  116.  
  117.     private byte[] read_file(String fn) throws FileNotFoundException, IOException {
  118.         System.out.println("Reading file " + fn);
  119.         File file = new File(fn);
  120.         FileInputStream fis = new FileInputStream(file);
  121.         byte[] data = new byte[(int) file.length()];
  122.         fis.read(data);
  123.         fis.close();
  124.         return data;
  125.     }
  126.  
  127.     protected byte[] process() throws IOException{
  128.         try {
  129.             switch(path) {
  130.                 case "/":
  131.                     // index.html
  132.                     return read_file("index.html");
  133.                 case "/dir_list":
  134.                     // return list of files.
  135.                     // directories will end with a trailing "/" character.
  136.                     return read_file("dir_list.txt");
  137.                 default:
  138.                     // this must be a filename.
  139.                     // return that or a 404.
  140.                     String extension = path.substring(path.lastIndexOf("."));
  141.                     mime = get_mime(extension);
  142.                     return read_file(path.substring(path.indexOf("/")+1));
  143.             }
  144.         } catch(FileNotFoundException e) {
  145.             e.printStackTrace();
  146.             override_status = true;
  147.             res.write("HTTP/1.1 404 NOT FOUND\r\n".getBytes(charset));
  148.             return new byte[]{};
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement