smart-http 1.1.0 釋出,可程式設計的 Http 應用微核心
阿新 • • 發佈:2021-02-10
新春現金紅包與免費Linux基金會培訓&認證,等你來領!>>>
smart-http 是一款比較簡易的 http 伺服器,其通訊核心採用了smart-socket最新版v1.5.5
。
smart-http 是一款可程式設計的 Http 應用微核心,使用者可根據自身需求進行Server或Client的應用開發。
你可以基於它開發 HTTP 代理伺服器、閘道器、靜態伺服器、http client 工具、效能壓測工具等。smart-http 依舊延續著作者一貫秉持的極簡、易用、高效能風格,只提供高效能的執行能力和易用的介面設計。把更多的可能性交給開發者,由那些富有創造力的 Java 開發者打造更優秀的 Http 作品。
從 v1.1.0 開始,smart-http 正式邁入Http Client 領域。憑藉著過去 2 年在 Server 端積累的經驗,Client 端的開發過程比較順利,先推出首個可執行版供大家體驗。在這個假期會抽些時間再作一些優化,爭取早日運用到實際場景中。
更新內容
- 工程模組拆分,現分為:smart-http-common、smart-http-server、smart-http-client 三個模組。
- 優化:StaticResourceHandle處理的返回結果缺少content-length頭資訊。【I16KE7】
- 新特性:支援 Http Client 開發。
- 預設協議 http/1.1,暫不支援 websocket client。
- 支援 get 請求提交。
- 支援 content-length、chunked 兩種響應解析方式。
- 支援自定義 Http Header
使用示例
1. Server 端
public class SimpleSmartHttp { public static void main(String[] args) { HttpBootstrap bootstrap = new HttpBootstrap(); // 普通http請求 bootstrap.pipeline().next(new HttpHandle() { @Override public void doHandle(HttpRequest request, HttpResponse response) throws IOException { response.write("hello world<br/>".getBytes()); } }); // websocket請求 bootstrap.wsPipeline().next(new WebSocketDefaultHandle() { @Override public void handleTextMessage(WebSocketRequest request, WebSocketResponse response, String data) { response.sendTextMessage("Hello World"); } }); bootstrap.setPort(8080).start(); } }
2. Client 端
public class HttpGetDemo {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient("www.baidu.com", 80);
httpClient.connect();
httpClient.get("/")
.onSuccess(response -> System.out.println(response.body()))
.onFailure(Throwable::printStackTrace)
.send();
}
}
最後
如果覺得這個專案還不錯,請給我們加個 Star。並且非常歡迎大家為這個專案貢獻你的想法和程式碼,開源不易,且行且珍惜。