|
| 1 | +import java.io.FileNotFoundException; |
| 2 | +import java.io.InputStreamReader; |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.io.File; |
| 10 | +import java.net.ServerSocket; |
| 11 | +import java.net.InetAddress; |
| 12 | +import java.net.Socket; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Date; |
| 15 | +import java.util.HashMap; |
| 16 | + |
| 17 | +public class HttpServer { |
| 18 | + private static class Processor implements Runnable { |
| 19 | + private Socket socket; |
| 20 | + private OutputStream output; |
| 21 | + private BufferedReader reader; |
| 22 | + private String root; |
| 23 | + |
| 24 | + private Processor(Socket socket, String root) throws IOException { |
| 25 | + this.root = root; |
| 26 | + this.socket = socket; |
| 27 | + this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| 28 | + } |
| 29 | + |
| 30 | + private byte[] byteStream(String fileRequest) { |
| 31 | + File file = new File(root + fileRequest); |
| 32 | + byte[] byteStream = new byte[(int)file.length()]; //??? |
| 33 | + FileInputStream fileInputStream = null; |
| 34 | + String error405 = "/405.html"; |
| 35 | + String error404 = "/404.html"; |
| 36 | + |
| 37 | + try { |
| 38 | + fileInputStream = new FileInputStream(file); |
| 39 | + fileInputStream.read(byteStream); |
| 40 | + |
| 41 | + if ( error405.equals(fileRequest) ) { |
| 42 | + System.out.println("405 /"); |
| 43 | + } else if ( error404.equals(fileRequest) ) { |
| 44 | + System.out.println("404 /"); |
| 45 | + } else { |
| 46 | + System.out.println("200 " + fileRequest); |
| 47 | + } |
| 48 | + } catch ( FileNotFoundException ex ) { |
| 49 | + if ( fileRequest.equals(error404) ) { |
| 50 | + System.out.println("File 404 not found."); |
| 51 | + } else { |
| 52 | + byteStream = byteStream(error404); |
| 53 | + } |
| 54 | + } catch ( IOException ex ) { |
| 55 | + System.err.println("Error Reading the file."); |
| 56 | + } finally { |
| 57 | + try { |
| 58 | + if ( fileInputStream != null ) { |
| 59 | + fileInputStream.close(); |
| 60 | + } |
| 61 | + } catch ( IOException ex ) {} |
| 62 | + } |
| 63 | + return byteStream; |
| 64 | + } |
| 65 | + |
| 66 | + private String getResponse(String mapValue, int length) { |
| 67 | + StringBuffer response = new StringBuffer(); |
| 68 | + |
| 69 | + if ( mapValue == null ) { |
| 70 | + mapValue = "application/octet-stream"; |
| 71 | + } |
| 72 | + |
| 73 | + response.append("HTTP/1.1 200 OK\r\n"); |
| 74 | + response.append("Connection: close\r\n"); |
| 75 | + response.append("Content-Length: " + length + "\r\n"); |
| 76 | + response.append("Content-Type: " + mapValue + "\r\n"); |
| 77 | + response.append("Date: " + new Date() + "\r\n\r\n"); |
| 78 | + return response.toString(); |
| 79 | + } |
| 80 | + |
| 81 | + private void writeResponse(String file) throws IOException { |
| 82 | + OutputStream output = socket.getOutputStream(); |
| 83 | + byte[] text = byteStream(file); |
| 84 | + Map<String, String> mimeMap = parseFile("mime.types"); |
| 85 | + String key = file.split("\\.")[1]; |
| 86 | + String mapValue = mimeMap.get(key); |
| 87 | + String response = getResponse(mapValue, text.length); |
| 88 | + byte[] result = new byte[text.length + response.getBytes().length]; |
| 89 | + System.arraycopy(response.getBytes(), 0, result, 0, response.getBytes().length); |
| 90 | + System.arraycopy(text, 0, result, response.getBytes().length, text.length); |
| 91 | + |
| 92 | + output.write(result); |
| 93 | + output.flush(); |
| 94 | + } |
| 95 | + |
| 96 | + public static Map<String, String> parseFile(String fileName) { |
| 97 | + Map<String, String> map = new HashMap<String, String>(); |
| 98 | + BufferedReader br = null; |
| 99 | + String line; |
| 100 | + |
| 101 | + try { |
| 102 | + br = new BufferedReader(new FileReader(fileName)); |
| 103 | + |
| 104 | + while ( (line = br.readLine()) != null ) { |
| 105 | + String[] parts = line.split("\\s", 2); |
| 106 | + int last = parts.length - 1; |
| 107 | + |
| 108 | + if ( parts[0].equals("")) {} |
| 109 | + else { |
| 110 | + parts[last] = parts[last].trim(); |
| 111 | + map.put(parts[0], parts[last]); |
| 112 | + } |
| 113 | + } |
| 114 | + } catch ( FileNotFoundException ex ) { |
| 115 | + ex.printStackTrace(); |
| 116 | + } catch ( IOException ex ) { |
| 117 | + ex.printStackTrace(); |
| 118 | + } |
| 119 | + return map; |
| 120 | + } |
| 121 | + |
| 122 | + public void run() { |
| 123 | + try { |
| 124 | + String line = reader.readLine(); |
| 125 | + String[] configArray = line.split(" "); |
| 126 | + |
| 127 | + if ( !configArray[0].equals("GET") ) { |
| 128 | + writeResponse("/405.html"); |
| 129 | + } else if ( configArray[1].equals("/") ) { |
| 130 | + writeResponse("/index.html"); |
| 131 | + } else { |
| 132 | + writeResponse(configArray[1]); |
| 133 | + } |
| 134 | + } catch ( IOException ex ) { |
| 135 | + } finally { |
| 136 | + try { |
| 137 | + socket.close(); |
| 138 | + } catch ( IOException ex ) { |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static void main(String[] args) throws IOException { |
| 145 | + Map<String, String> httpMap = Processor.parseFile("http.conf"); |
| 146 | + int port = Integer.parseInt(httpMap.get("port")); |
| 147 | + String address = httpMap.get("address"); |
| 148 | + String root = httpMap.get("root_dir"); |
| 149 | + int backlog = 42; |
| 150 | + ServerSocket server = new ServerSocket(port, backlog, InetAddress.getByName(address)); |
| 151 | + |
| 152 | + while (true) { |
| 153 | + Socket socket = server.accept(); |
| 154 | + new Thread(new Processor(socket, root)).start(); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments