smart-http 1.1.0 发布,可编程的 Http 应用微内核

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 端的开发过程比较顺利,先推出首个可运行版供大家体验。在这个假期会抽些时间再作一些优化,争取早日运用到实际场景中。

更新内容

  1. 工程模块拆分,现分为:smart-http-common、smart-http-server、smart-http-client 三个模块。
  2. 优化:StaticResourceHandle处理的返回结果缺少content-length头信息。【I16KE7】
  3. 新特性:支持 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。并且非常欢迎大家为这个项目贡献你的想法和代码,开源不易,且行且珍惜。

猜你喜欢

转载自www.oschina.net/news/129744/smart-http-1-1-0-released