前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Embedded Server nano - NanoHTTPD.java学习

Embedded Server nano - NanoHTTPD.java学习

作者头像
Jerry Wang
发布2019-05-30 20:37:48
5080
发布2019-05-30 20:37:48
举报

Default threading strategy for NanoHTTPD. By default, the server spawns a new Thread for every incoming request. These are set to daemon status, and named according to the request number. The name is useful when profiling the application.

(1) Interface AsyncRunner

代码语言:javascript
复制
void closeAll();
void closed(ClientHandler clientHandler);
void exec(ClientHandler code);

(2) ClientHandler implements Runnable

代码语言:javascript
复制
private final InputStream inputStream;
private final Socket acceptSocket;


@Override
public void run() {
(1). get outputStream: outputStream = this.acceptSocket.getOutputStream();
(2). create temporary file: TempFileManager tempFileManager = NanoHTTPD.this.tempFileManagerFactory.create();
(3). Create HTTPSession via: session = new HTTPSession(tempFileManager, this.inputStream, outputStream, this.acceptSocket.getInetAddress());
(4). session.execute

(3) static class Cookie

(1) public static String getHTTPTime(int days):get dd MMM yyyy HH:mm:ss z via int days (2) constructor: Cookie(String name, String value, int numDays) numDays means expire days rudimentary: 基础的,初步的。

(4) CookieHandler:Provides rudimentary support for cookies. Doesn’t support ‘path’, ‘secure’ nor ‘httpOnly’. (1) HashMap<String, String> cookies = new HashMap<String, String>(); (2) ArrayList queue = new ArrayList(); (3) Set a cookie with an expiration date from a month ago, effectively deleting it on the client side. public void delete(String name) { set(name, “-delete-”, -30); }

(5) DefaultAsyncRunner implements AsyncRunner constructor:

代码语言:javascript
复制
int availableProcessors = Runtime.getRuntime().availableProcessors();
executor = new ThreadPoolExecutor(
5 * availableProcessors, // core size
15 * availableProcessors, // max size
2, TimeUnit.MINUTES, // 2 minutes timeout for thread to process request
new SynchronousQueue(),
new ThreadFactory(){
@Override
public Thread newThread(Runnable runnable) {
Thread t = Executors.defaultThreadFactory().newThread(runnable);
t.setPriority(Thread.MAX_PRIORITY);
t.setDaemon(true);
return t;
}
});

(1) private ThreadPoolExecutor executor; (2) private final List running = Collections.synchronizedList(new ArrayList()); (3) exec(ClientHandler clientHandler) a. this.running.add(clientHandler); b. executor.execute(clientHandler);

(6) DefaultTempFile implements TempFile

(1) private final File file; (2) private final OutputStream fstream; Constructor public DefaultTempFile(File tempdir) throws IOException { this.file = File.createTempFile(“NanoHTTPD-”, “”, tempdir); this.fstream = new FileOutputStream(this.file); }

(7) DefaultTempFileManager (1) private final File tmpdir; (2) private final List tempFiles; constructor this.tmpdir = new File(System.getProperty(“java.io.tmpdir”)); this.tempFiles = new ArrayList(); (1) createTempFile

(8) DefaultTempFileManagerFactory implements TempFileManagerFactory charset, boundary,

(9) SecureServerSocketFactory implements ServerSocketFactory (1) private SSLServerSocketFactory sslServerSocketFactory; (2) private String[] sslProtocols;

(10) HTTPSession implements IHTTPSession (1) params (2) headers (3) cookie (4) queryParameterString; (5) remoteIP (6) inputStream (7) outputStream (8) tempFileManager (9)

(11) Apache’s default header limit is 8KB. // Do NOT assume that a single read will get the entire header // at once!

(12) ServerRunnable implements Runnable

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档