Skip to content

Commit

Permalink
Support for static file parsing has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed Feb 24, 2024
1 parent 8f45867 commit e1f628c
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 11 deletions.
4 changes: 2 additions & 2 deletions jcommon/docean/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<dependency>
<groupId>run.mone</groupId>
<artifactId>easy</artifactId>
<!-- <version>1.4-jdk21-SNAPSHOT</version>-->
<version>1.5.0-jdk21</version>
<version>1.4-jdk21-SNAPSHOT</version>
<!-- <version>1.5.0-jdk21</version>-->
</dependency>
<dependency>
<groupId>cglib</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ private void setConfig(Ioc ioc) {
this.mvcConfig.setAllowCross(Boolean.valueOf(ioc.getBean(MvcConst.ALLOW_CROSS_DOMAIN, MvcConst.FALSE)));
this.mvcConfig.setDownload(Boolean.valueOf(ioc.getBean(MvcConst.MVC_DOWNLOAD, MvcConst.FALSE)));
this.mvcConfig.setUseCglib(Boolean.valueOf(ioc.getBean(MvcConst.CGLIB, MvcConst.TRUE)));

this.mvcConfig.setOpenStaticFile(Boolean.valueOf(ioc.getBean(MvcConst.OPEN_STATIC_FILE, MvcConst.FALSE)));
this.mvcConfig.setStaticFilePath(ioc.getBean(MvcConst.STATIC_FILE_PATH, MvcConst.EMPTY));

this.mvcConfig.setResponseOriginalValue(Boolean.valueOf(ioc.getBean(MvcConst.RESPONSE_ORIGINAL_VALUE, MvcConst.FALSE)));
this.mvcConfig.setPoolSize(Integer.valueOf(ioc.getBean(MvcConst.MVC_POOL_SIZE, String.valueOf(MvcConst.DEFAULT_MVC_POOL_SIZE))));
this.mvcConfig.setVirtualThread(Boolean.valueOf(ioc.getBean(MvcConst.VIRTUAL_THREAD, MvcConst.TRUE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public class MvcConfig implements Serializable {
*/
private boolean virtualThread;

private String staticFilePath;

private boolean openStaticFile;

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ public class HttpServerConfig {

private boolean userWs;


public static int HTTP_POOL_SIZE = 500;
public static int HTTP_POOL_QUEUE_SIZE = 1000;

public static int HTTP_POOL_QUEUE_SIZE = 1000;

public enum HttpVersion {
http1, http2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.google.gson.Gson;
import com.xiaomi.youpin.docean.Mvc;
import com.xiaomi.youpin.docean.bo.MvcConfig;
import com.xiaomi.youpin.docean.common.Cons;
import com.xiaomi.youpin.docean.common.Safe;
import com.xiaomi.youpin.docean.common.StringUtils;
import com.xiaomi.youpin.docean.config.HttpServerConfig;
import com.xiaomi.youpin.docean.mvc.common.MvcConst;
import com.xiaomi.youpin.docean.mvc.download.Download;
import com.xiaomi.youpin.docean.mvc.html.Html;
import com.xiaomi.youpin.docean.mvc.upload.MvcUpload;
import com.xiaomi.youpin.docean.mvc.util.ExceptionUtil;
import com.xiaomi.youpin.docean.mvc.util.MethodFinder;
Expand All @@ -31,6 +34,8 @@ public class MvcRunnable implements Runnable {

private MvcResponse response;

private MvcConfig config;

private ConcurrentHashMap<String, HttpRequestMethod> requestMethodMap;

private Mvc mvc;
Expand All @@ -43,6 +48,7 @@ public MvcRunnable(Mvc mvc, MvcContext context, MvcRequest request, MvcResponse
this.response = response;
this.requestMethodMap = requestMethodMap;
this.mvc = mvc;
this.config = this.mvc.getMvcConfig();
}


Expand All @@ -65,6 +71,7 @@ public MvcRunnable(Mvc mvc, HttpServerConfig config, ChannelHandlerContext ctx,
this.request.setBody(body);
this.response.setCtx(ctx);
this.mvc = mvc;
this.config = this.mvc.getMvcConfig();
this.requestMethodMap = requestMethodMap;
}

Expand Down Expand Up @@ -107,6 +114,17 @@ private void call() {
}
String path = request.getPath();


if (config.isOpenStaticFile() && Html.isHtmlFile(path)) {
String content = Html.view(config.getStaticFilePath() + path);
if (StringUtils.isEmpty(content)) {
sendNotFoundResponse();
return;
}
response.writeAndFlush(context, content);
return;
}

//Support file download (/download) and must enable downloads.
if (isDownload(path) && mvc.getMvcConfig().isDownload()) {
Download.download(context, request, response);
Expand All @@ -127,16 +145,20 @@ private void call() {
}

if (!path.equals(Cons.Service)) {
MvcResult<Object> result = new MvcResult<>();
result.setCode(HttpResponseStatus.NOT_FOUND.code());
result.setMessage(HttpResponseStatus.NOT_FOUND.reasonPhrase());
response.writeAndFlush(context, gson.toJson(result));
sendNotFoundResponse();
return;
}
//rate limited or exceeded quota
mvc.callService(context, request, response);
}

private void sendNotFoundResponse() {
MvcResult<Object> result = new MvcResult<>();
result.setCode(HttpResponseStatus.NOT_FOUND.code());
result.setMessage(HttpResponseStatus.NOT_FOUND.reasonPhrase());
response.writeAndFlush(context, gson.toJson(result));
}


private boolean isDownload(String path) {
return path.equals("/download");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ public abstract class MvcConst {

public static final String CGLIB = "$cglib";

public static final String STATIC_FILE_PATH = "$staticFilePath";

public static final String OPEN_STATIC_FILE = "$openStaticFile";

public static final String TRUE = "true";

public static final String EMPTY = "";

public static final String FALSE = "false";

public static final String ALLOW_CROSS_DOMAIN = "$allow-cross-domain";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.xiaomi.youpin.docean.mvc.html;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author [email protected]
* @date 2024/2/24 14:10
*/
@Slf4j
public class Html {

//读取指定html页面文件内容并返回
public static String view(String file) {
try {
Path path = Paths.get(file);
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
} catch (IOException e) {
log.error(e.getMessage(), e);
e.printStackTrace();
return "";
}
}

//判断一个文件是否是.html文件,并且返回boolean值(class)
public static boolean isHtmlFile(String filePath) {
return filePath != null && filePath.toLowerCase().endsWith(".html");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testHttpServer() throws InterruptedException {
}


Ioc.ins().putBean("$response-original-value", "true");
Ioc.ins().putBean("$response-original-value", "true").putBean("$openStaticFile","true").putBean("$staticFilePath","/tmp/");
// Ioc.ins().putBean("$ssl_domain", "zzy.com");
// Ioc.ins().putBean("$ssl_self_sign", "false");
// Ioc.ins().putBean("$ssl_certificate","/Users/zhangzhiyong/key/zzy.com/certificate.crt");
Expand All @@ -88,8 +88,8 @@ public void testHttpServer() throws InterruptedException {
Mvc.ins();
DoceanHttpServer server = new DoceanHttpServer(HttpServerConfig.builder()
.httpVersion(HttpServerConfig.HttpVersion.http1)
// .ssl(true)
.port(8999)
.ssl(false)
.port(8899)
.websocket(false)
.uploadDir("/tmp/v").upload(false)
.build());
Expand Down
4 changes: 4 additions & 0 deletions jcommon/docean/src/test/resources/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ demoVo=com.xiaomi.youpin.docean.test.demo.DemoVo
val=123


openStaticFile=true
staticFilePath=/tmp/



0 comments on commit e1f628c

Please sign in to comment.