package Web;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;import java.util.Iterator;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;/*** 服务器 负责分配静态文件服务器和应用服务** @author Ma*/public class HttpServer {public static void init(int port) {try {//初始化数据库连接DB.connection();Selector selector = Selector.open();//创建selectorServerSocketChannel serverChannel = ServerSocketChannel.open();serverChannel.socket().bind(new InetSocketAddress(port));serverChannel.configureBlocking(false);serverChannel.register(selector, SelectionKey.OP_ACCEPT);//注册SocketChannel channel;try {while (true) {selector.select();Iterator<SelectionKey> iter = selector.selectedKeys().iterator();while (iter.hasNext()) {SelectionKey key = iter.next();iter.remove();Object attach = key.attachment();//如果有正在读取静态文件的标记就返回if (attach != null && attach.equals(0)) {continue;}//开始处理if (key.isAcceptable()) { // 接收请求ServerSocketChannel server = (ServerSocketChannel) key.channel();channel = server.accept();//设置非阻塞模式channel.configureBlocking(false);channel.register(selector, SelectionKey.OP_READ);} else if (key.isReadable()) { // 读信息channel = (SocketChannel) key.channel();ByteBuffer clientBuffer = ByteBuffer.allocate(1024);int count = channel.read(clientBuffer);String receive = "";while (count > 0) {clientBuffer.flip();CharBuffer charBuffer = Charset.forName("UTF-8").decode(clientBuffer);String temp = charBuffer.toString();receive = receive + temp;clientBuffer.clear();count = channel.read(clientBuffer);channel.register(selector, SelectionKey.OP_WRITE);}key.attach(receive);clientBuffer.clear();} else if (key.isWritable()) { // 写事件//开始处理事件//开始时间long startTime = System.currentTimeMillis();channel = (SocketChannel) key.channel();boolean accessStaticFile = false;//是不是访问的静态文件// 写事件Request request = new Request((String) key.attachment());if (request.Path.equals("/favicon.ico") || request.Path.equals("robots.txt") || request.Path.toLowerCase().indexOf("/static") == 0) {//##处理//#说明是访问静态文件accessStaticFile = true;StaticFileServer staticFile = new StaticFileServer(request);if (staticFile.exists()) {staticFile.read(channel);key.attach(0);//这个标记是正在读取静态文件continue;//读取下一个循环} else {//如果不存在//当作非静态文件处理404 省事accessStaticFile = false;Response.setStatus(404);}}//访问的不是静态文件,或者静态文件不存在if (!accessStaticFile) {//###处理请求//获取客户端IPrequest.setRemoteIP(channel.socket().getLocalAddress().getHostName());//开始分配到HandlerAllocation.init(request);//Handler结束if (Response.Content == null) {Response.setContent("未知错误造成,无返回实体!");Response.setStatus(500);}byte[] contentBytes = Response.Content.getBytes("UTF-8");//先设置实体长度Response.setContentLength(contentBytes.length);//然后获取headerbyte[] headerBytes = Response.getHeader().getBytes("UTF-8");ByteBuffer writerBuffer = ByteBuffer.allocate(headerBytes.length + contentBytes.length);writerBuffer.clear();writerBuffer.put(headerBytes);writerBuffer.put(contentBytes);writerBuffer.flip();while (writerBuffer.hasRemaining()) {channel.write(writerBuffer);}writerBuffer.clear();channel.close();//输出debug信息if (Options.DEBUG || Options.ParseLine) {System.out.println(Response.Status + " " + request.Header.get("Host") + request.Path + " " + (System.currentTimeMillis() - startTime) + "ms");}//完成响应,清理相应信息Response.finish();}}}}} finally {serverChannel.close();}} catch (IOException ex) {Logger.getLogger(HttpServer.class.getName()).log(Level.SEVERE, null, ex);}}/*** 设置URL** @param path* @param handler*/public static void setPATH(String path, Object handler) {Allocation.PATH.put(path, handler);}/*** 获取所有的Path** @return*/public static String[] getAllPath() {String[] pathArray = new String[Allocation.PATH.size()];Set set = Allocation.PATH.entrySet();Iterator iterator = set.iterator();int i = 0;while (iterator.hasNext()) {java.util.Map.Entry item = (java.util.Map.Entry) iterator.next();pathArray[i] = (String) item.getKey();i++;}return pathArray;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型