SimpleWebServer继承于NanoHTTPD
启动的时候,首先初始化index文件
public static final List<String> INDEX_FILE_NAMES = new ArrayList<String>() {
{
add("index.html");
add("index.htm");
}
};
初始化 mimeType和LICENCE
public static Map<String, String> mimeTypes() {
if (MIME_TYPES == null) {
MIME_TYPES = new HashMap<String, String>();
loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/default-mimetypes.properties");
loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/mimetypes.properties");
if (MIME_TYPES.isEmpty()) {
LOG.log(Level.WARNING, "no mime types found in the classpath! please provide mimetypes.properties");
}
}
return MIME_TYPES;
}
看一下这个文件default-mimetypes.properties
然后进入main函数
/**
* Starts as a standalone file server and waits for Enter.
*/
public static void main(String[] args) {
// Defaults
int port = 8080;//端口号
String host = null; // bind to all interfaces by default
List<File> rootDirs = new ArrayList<File>();
boolean quiet = false;
String cors = null;
Map<String, String> options = new HashMap<String, String>();
// Parse command-line, with short and long versions of the options.
for (int i = 0; i < args.length; ++i) {//解析命令行参数
if ("-h".equalsIgnoreCase(args[i]) || "--host".equalsIgnoreCase(args[i])) {//主机
host = args[i + 1];
} else if ("-p".equalsIgnoreCase(args[i]) || "--port".equalsIgnoreCase(args[i])) {//端口
port = Integer.parseInt(args[i + 1]);
} else if ("-q".equalsIgnoreCase(args[i]) || "--quiet".equalsIgnoreCase(args[i])) {//安静模式
quiet = true;
} else if ("-d".equalsIgnoreCase(args[i]) || "--dir".equalsIgnoreCase(args[i])) {//根目录
rootDirs.add(new File(args[i + 1]).getAbsoluteFile());
} else if (args[i].startsWith("--cors")) {
cors = "*";
int equalIdx = args[i].indexOf('=');
if (equalIdx > 0) {
cors = args[i].substring(equalIdx + 1);
}
} else if ("--licence".equalsIgnoreCase(args[i])) {//许可证
System.out.println(SimpleWebServer.LICENCE + "\n");
} else if (args[i].startsWith("-X:")) {
int dot = args[i].indexOf('=');
if (dot > 0) {
String name = args[i].substring(0, dot);
String value = args[i].substring(dot + 1, args[i].length());
options.put(name, value);
}
}
}
if (rootDirs.isEmpty()) {//如果为空,添加当前目录
rootDirs.add(new File(".").getAbsoluteFile());
}
options.put("host", host);
options.put("port", "" + port);
options.put("quiet", String.valueOf(quiet));
StringBuilder sb = new StringBuilder();
for (File dir : rootDirs) {
if (sb.length() > 0) {
sb.append(":");
}
try {
sb.append(dir.getCanonicalPath());
} catch (IOException ignored) {
}
}
options.put("home", sb.toString());
ServiceLoader<WebServerPluginInfo> serviceLoader = ServiceLoader.load(WebServerPluginInfo.class);
for (WebServerPluginInfo info : serviceLoader) {
String[] mimeTypes = info.getMimeTypes();
for (String mime : mimeTypes) {
String[] indexFiles = info.getIndexFilesForMimeType(mime);
if (!quiet) {
System.out.print("# Found plugin for Mime type: \"" + mime + "\"");
if (indexFiles != null) {
System.out.print(" (serving index files: ");
for (String indexFile : indexFiles) {
System.out.print(indexFile + " ");
}
}
System.out.println(").");
}
registerPluginForMimeType(indexFiles, mime, info.getWebServerPlugin(mime), options);
}
}
ServerRunner.executeInstance(new SimpleWebServer(host, port, rootDirs, quiet, cors));
}
最后调用ServerRunner.executeInstance启动SimpleWebServer,这里构造了一个SimpleWebServer
public SimpleWebServer(String host, int port, List<File> wwwroots, boolean quiet, String cors) {
super(host, port);
this.quiet = quiet;
this.cors = cors;
this.rootDirs = new ArrayList<File>(wwwroots);
init();
}
quite表示安静模式,是否打印客户端请求的相关信息,cors用于跨域访问
继承NanoHTTPD
/**
* Constructs an HTTP server on given hostname and port.
*/
public NanoHTTPD(String hostname, int port) {
this.hostname = hostname;
this.myPort = port;
setTempFileManagerFactory(new DefaultTempFileManagerFactory());//设置临时文件管理类
setAsyncRunner(new DefaultAsyncRunner());
}
这里设置默认的线程策略为DefaultAsyncRunner
再看ServerRunner的executeInstance
public static void executeInstance(NanoHTTPD server) {
try {
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
System.exit(-1);
}
System.out.println("Server started, Hit Enter to stop.\n");
try {
System.in.read();//等待读取命令行输入
} catch (Throwable ignored) {
}
server.stop();//断开服务器
System.out.println("Server stopped.\n");
}
这里直接调用server的start函数启动server
/**
* Start the server.
* 启动Server
* @param timeout
* timeout to use for socket connections.
* @param daemon
* start the thread daemon or not.
* @throws IOException
* if the socket is in use.
*/
public void start(final int timeout, boolean daemon) throws IOException {//启动server端
this.myServerSocket = this.getServerSocketFactory().create();
this.myServerSocket.setReuseAddress(true);//表示在连接还在timewait状态时,就可复用其端口,这个对于大量连接且经常有timewait时适用
ServerRunnable serverRunnable = createServerRunnable(timeout);//创建Runnable 服务端处理主要线程操作
this.myThread = new Thread(serverRunnable);//创建线程
this.myThread.setDaemon(daemon);
this.myThread.setName("NanoHttpd Main Listener");
this.myThread.start();//启动线程
while (!serverRunnable.hasBinded() && serverRunnable.getBindException() == null) {
try {
Thread.sleep(10L);
} catch (Throwable e) {
// on android this may not be allowed, that's why we
// catch throwable the wait should be very short because we are
// just waiting for the bind of the socket
}
}
if (serverRunnable.getBindException() != null) {
throw serverRunnable.getBindException();
}
}
这里首先创建一个ServerSocket,然后会创建一个ServerRunnable,启动线程执行他的run
public void run() {
try {
httpd.getMyServerSocket().bind(httpd.hostname != null ? new InetSocketAddress(httpd.hostname, httpd
.myPort) : new InetSocketAddress(httpd.myPort));//绑定端口地址
hasBinded = true;
} catch (IOException e) {
this.bindException = e;
return;
}
do {
try {
final Socket finalAccept = httpd.getMyServerSocket().accept();//接受请求
if (this.timeout > 0) {
finalAccept.setSoTimeout(this.timeout);//连接状态timeout秒没有收到数据的话强制断开客户端
}
final InputStream inputStream = finalAccept.getInputStream();
httpd.asyncRunner.exec(httpd.createClientHandler(finalAccept, inputStream));
//首先以socket和inputStream参数创建一个ClientHandler,然后调用NanoHTTPD的asyncRunner执行这个handler
} catch (IOException e) {
NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e);
}
} while (!httpd.getMyServerSocket().isClosed());
}
这里绑定ServerSocket,然后接受请求,等待客户端的连接。下一节我们分析一下请求的处理过程