Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.nio.charset.Charset;
- import java.io.*;
- import java.util.HashMap;
- import java.util.Date;
- import java.awt.Desktop;
- import java.net.URI;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class Main implements Runnable {
- BufferedReader req;
- OutputStream res;
- String path = null;
- HashMap<String,String> headers = new HashMap<String,String>();
- HashMap<String,String> parms = new HashMap<String,String>();
- Socket sock;
- boolean override_status = false;
- String mime = "text/html";
- Charset charset = Charset.defaultCharset();
- public static void main(String[] args) throws Exception {
- // Listen for a connection from a client
- ServerSocket serverSocket = new ServerSocket(1234);
- if(Desktop.isDesktopSupported())
- Desktop.getDesktop().browse(new URI("http://localhost:1234"));
- else
- System.out.println("Please direct your browser to http://localhost:1234.");
- ExecutorService executor = Executors.newFixedThreadPool(5);
- while(true) {
- Main client = new Main();
- client.sock = serverSocket.accept();
- client.req = new BufferedReader(new InputStreamReader(client.sock.getInputStream()));
- client.res = client.sock.getOutputStream();
- executor.execute(client);
- }
- }
- @Override
- public void run() {
- String dateString = (new Date()).toGMTString();
- String inputLine;
- try {
- while ((inputLine = req.readLine()) != null) {
- //System.out.println("The client said: " + inputLine);
- if(path==null) {
- int a = inputLine.indexOf(" ")+1;
- path = inputLine.substring(a,inputLine.indexOf(" ",a));
- }
- if(inputLine.length() <= 2)
- break;
- }
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- try {
- byte[] payload = process();
- if(!override_status)
- res.write("HTTP/1.1 200 OK\r\n".getBytes(charset));
- res.write(("Content-Length: " + Integer.toString(payload.length) + "\r\n").getBytes(charset));
- if(mime!=null) {
- res.write(("Content-Type: " + mime + "\r\n").getBytes(charset));
- } else {
- // initiate file download.
- res.write(("Content-Type: application/octet-stream\r\nContent-Disposition: attachment; filename=" + path.substring(path.indexOf("/")+1).replace("/","_")+"\r\n").getBytes(charset));
- }
- res.write(("Date: " + dateString + "\r\n").getBytes(charset));
- res.write(("Last-Modified: " + dateString + "\r\n").getBytes(charset));
- res.write("Connection: close\r\n".getBytes(charset));
- res.write("\r\n".getBytes(charset));
- // Send the payload
- res.write(payload);
- res.close();
- } catch(Exception ex) {
- try {
- ex.printStackTrace();
- res.write("HTTP/1.1 500 Internal Server Error\r\n".getBytes(charset));
- res.write(("Date: " + dateString + "\r\n").getBytes(charset));
- res.write("Connection: close\r\n".getBytes(charset));
- res.write("\r\n".getBytes(charset));
- res.close();
- } catch(Exception ex_) {
- ex_.printStackTrace();
- }
- }
- }
- public static String get_mime(String extension) {
- switch(extension) {
- case ".ico":
- return "image/x-icon";
- case ".png":
- return "image/png";
- case ".jpg":
- case ".jpeg":
- return "image/jpeg";
- case ".gif":
- return "image/gif";
- case ".js":
- return "text/javascript";
- case ".css":
- return "text/css";
- case ".html":
- return "text/html";
- default: return null;
- }
- }
- private byte[] read_file(String fn) throws FileNotFoundException, IOException {
- System.out.println("Reading file " + fn);
- File file = new File(fn);
- FileInputStream fis = new FileInputStream(file);
- byte[] data = new byte[(int) file.length()];
- fis.read(data);
- fis.close();
- return data;
- }
- protected byte[] process() throws IOException{
- try {
- switch(path) {
- case "/":
- // index.html
- return read_file("index.html");
- case "/dir_list":
- // return list of files.
- // directories will end with a trailing "/" character.
- return read_file("dir_list.txt");
- default:
- // this must be a filename.
- // return that or a 404.
- String extension = path.substring(path.lastIndexOf("."));
- mime = get_mime(extension);
- return read_file(path.substring(path.indexOf("/")+1));
- }
- } catch(FileNotFoundException e) {
- e.printStackTrace();
- override_status = true;
- res.write("HTTP/1.1 404 NOT FOUND\r\n".getBytes(charset));
- return new byte[]{};
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement