package name.ixr.webserver; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * HTTP 处理单元 * @author IXR */ public class ProcessRunnable implements Runnable { private Socket socket = new Socket(); /** * @param socket * 要处理的连接 */ public ProcessRunnable(Socket socket) { this.socket = socket; } @Override public void run() { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); Pattern pattern = Pattern.compile("GET (.*?) HTTP/1.1"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); Matcher matcher = pattern.matcher(bufferedReader.readLine()); if (matcher.find()) { String path = matcher.group(1); path = URLDecoder.decode(path,Global.charset); File file = new File(Global.directory, path); if (file.exists()) { if (file.isFile()) { writeFile(file, outputStream); socket.close(); return; } else { for (String default_page : Global.default_page) { File defaultFile = new File(file, default_page); if (defaultFile.exists() && defaultFile.isFile()) { writeFile(defaultFile, outputStream); socket.close(); return; } } writeDirectory(file, outputStream, path); socket.close(); return; } } outputStream.write(getHead(404)); socket.close(); } } catch (Exception exception) { if (outputStream != null) { try { outputStream.write(getHead(500)); socket.close(); } catch (IOException e) { } } } } private void writeDirectory(File directory, OutputStream outputStream, String path) throws Exception { StringBuffer html = new StringBuffer(); if (path.split("/").length > 1) { html.append("<a href="/" mce_href="/""../">Parent Directory</a><br/>/r/n"); } File[] files = directory.listFiles(); if(files.length > 0){ for (File file : files) { String href = String.format("%s/%s", path, file.getName()); href = href.replace("//", "/"); href = URLEncoder.encode(href,Global.charset); String type = file.isDirectory() ? "Directory" : file.getName().substring(file.getName().indexOf(".") + 1); html.append(String.format("<a href="/" mce_href="/""%s/">[%s]/t%s</a><br/>/r/n", href, type, file.getName())); } }else{ html.append("Lost Fount"); } outputStream.write(getHead(200,Global.typeMap.get("htm"))); outputStream.write(html.toString().getBytes(Global.charset)); } private void writeFile(File file, OutputStream outputStream) throws Exception { byte[] bytes = new byte[Global.buffer_size]; InputStream fileStream = new FileInputStream(file); outputStream.write(getHead(200, getFileContentType(file))); for (int idx = fileStream.read(bytes); idx > -1; idx = fileStream.read(bytes)) { outputStream.write(bytes, 0, idx); } fileStream.close(); outputStream.flush(); } /** 获取文件后缀 */ private String getFileContentType(File file) { String fileType = file.getName(); int idx = fileType.indexOf("."); if (idx > -1) { fileType = fileType.substring(idx + 1); } return Global.typeMap.get(fileType); } /** 获取HTTP头 */ private byte[] getHead(int status, String... objs) { StringBuffer headBuffer = new StringBuffer(); if (status == 200) { headBuffer.append("HTTP/1.1 200 OK/r/n"); } else if (status == 404) { headBuffer.append("HTTP/1.1 404 Not Found/r/n"); } else if (status == 500) { headBuffer.append("HTTP/1.1 500 Appliction Exception/r/n"); } if (objs.length > 0 && objs[0] != null) { headBuffer.append(String.format("Content-Type: %s; charset=%s", objs[0], Global.charset)); } headBuffer.append("/r/n/r/n"); return headBuffer.toString().getBytes(); } }