add http server engine

This commit is contained in:
Looly
2024-11-18 01:53:18 +08:00
parent 99a2f5f4a2
commit e3a60ad93f
39 changed files with 1925 additions and 910 deletions

View File

@@ -16,17 +16,16 @@
package org.dromara.hutool.http.server;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.server.filter.DefaultExceptionFilter;
import java.io.IOException;
import org.dromara.hutool.http.server.engine.sun.filter.DefaultExceptionFilter;
public class ExceptionServerTest {
public static void main(final String[] args) {
HttpUtil.createServer(8888)
.addFilter(new DefaultExceptionFilter())
.addAction("/", (req, res) -> {
throw new IOException("Test Exception");
throw new IORuntimeException("Test Exception");
})
.start();
}

View File

@@ -20,6 +20,7 @@ import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.multi.ListValueMap;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.server.engine.sun.SimpleServer;
/**
* http://localhost:8888/?name=hutool
@@ -33,7 +34,7 @@ public class Issue3343Test {
Console.log(" > from : " + req.getClientIP());
// 过滤器中获取请求参数
Console.log(" > params : " + req.getParams());
chain.doFilter(req.getHttpExchange());
chain.doFilter(req.getExchange());
});
server.addAction("/", Issue3343Test::index);
@@ -41,9 +42,9 @@ public class Issue3343Test {
server.start();
}
private static void index(HttpServerRequest request, HttpServerResponse response) {
private static void index(final ServerRequest request, final ServerResponse response) {
// 具体逻辑中再次获取请求参数
ListValueMap<String, String> params = request.getParams();
final ListValueMap<String, String> params = request.getParams();
Console.log("index params: " + params);
response.getWriter().write("GOT: " + params);
}

View File

@@ -19,18 +19,19 @@ package org.dromara.hutool.http.server;
import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.meta.ContentType;
import org.dromara.hutool.http.server.engine.sun.SimpleServer;
public class Issue3723Test {
public static void main(final String[] args) {
final SimpleServer server = HttpUtil.createServer(8888);
server.addFilter((req, res, chain) -> {
final String requestId = IdUtil.fastSimpleUUID();
req.getHttpExchange().setAttribute("requestId", requestId);
req.getExchange().setAttribute("requestId", requestId);
res.addHeader("X-Request-Id", requestId);
res.write("new Content");
chain.doFilter(req.getHttpExchange());
chain.doFilter(req.getExchange());
});
server.addAction("/", (req, res)-> res.write("Hello Hutool Server", ContentType.JSON.getValue()));
server.start();

View File

@@ -19,8 +19,10 @@ package org.dromara.hutool.http.server;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.multipart.MultipartFormData;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.multipart.MultipartFormData;
import org.dromara.hutool.http.server.engine.sun.SimpleServer;
import org.dromara.hutool.http.server.engine.sun.SunServerRequest;
import java.util.concurrent.TimeUnit;
@@ -35,7 +37,7 @@ public class IssueI6Q30XTest {
public static void main(String[] args) {
final SimpleServer server = HttpUtil.createServer(8888);
server.addAction("/file", (request, response) -> {
Console.log(request.getHeaders().entrySet());
Console.log(((SunServerRequest)request).getHeaders().entrySet());
final StopWatch stopWatch = DateUtil.createStopWatch();
stopWatch.start();

View File

@@ -19,25 +19,26 @@ package org.dromara.hutool.http.server;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.meta.HeaderName;
import org.dromara.hutool.http.meta.HttpStatus;
public class RedirectServerTest {
public static void main(final String[] args) {
HttpUtil.createServer(8888).addAction("/redirect1", (request, response) -> {
response.addHeader(HeaderName.LOCATION.getValue(),"http://localhost:8888/redirect2");
response.addHeader(HeaderName.SET_COOKIE.getValue(),"redirect1=1; path=/; HttpOnly");
response.send(301);
response.setStatus(301);
}).addAction("/redirect2", (request, response) -> {
response.addHeader(HeaderName.LOCATION.getValue(),"http://localhost:8888/redirect3");
response.addHeader(HeaderName.SET_COOKIE.getValue(), "redirect2=2; path=/; HttpOnly");
response.send(301);
response.setStatus(301);
}).addAction("/redirect3", (request, response) -> {
response.addHeader(HeaderName.LOCATION.getValue(),"http://localhost:8888/redirect4");
response.addHeader(HeaderName.SET_COOKIE.getValue(),"redirect3=3; path=/; HttpOnly");
response.send(301);
response.setStatus(301);
}).addAction("/redirect4", (request, response) -> {
final String cookie = request.getHeader(HeaderName.COOKIE);
Console.log(cookie);
response.sendOk();
response.setStatus(HttpStatus.HTTP_OK);
}).start();
}
}

View File

@@ -19,10 +19,11 @@ package org.dromara.hutool.http.server;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.multipart.UploadFile;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.meta.ContentType;
import org.dromara.hutool.http.meta.HeaderName;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.multipart.UploadFile;
import org.dromara.hutool.http.server.engine.sun.SunServerRequest;
import org.dromara.hutool.json.JSONUtil;
import java.net.HttpCookie;
@@ -33,13 +34,13 @@ public class SimpleServerTest {
HttpUtil.createServer(8888)
.addFilter(((req, res, chain) -> {
Console.log("Filter: " + req.getPath());
chain.doFilter(req.getHttpExchange());
chain.doFilter(req.getExchange());
}))
// 设置默认根目录classpath/html
.setRoot(FileUtil.file("html"))
// get数据测试返回请求的PATH
.addAction("/get", (request, response) ->
response.write(request.getURI().toString(), ContentType.TEXT_PLAIN.toString())
response.write(((SunServerRequest)request).getURI().toString(), ContentType.TEXT_PLAIN.toString())
)
// 返回JSON数据测试
.addAction("/restTest", (request, response) -> {

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.server.ServerConfig;
import org.dromara.hutool.http.server.engine.undertow.UndertowEngine;
public class UndertowTest {
public static void main(String[] args) {
final UndertowEngine undertowEngine = new UndertowEngine();
undertowEngine.init(ServerConfig.of());
undertowEngine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Undertow response test");
});
undertowEngine.start();
}
}