Skip to content

Commit 0b8d360

Browse files
authored
Merge pull request #283 from lets-blade/dev
Dev
2 parents 34e3c5b + 27fb00a commit 0b8d360

13 files changed

+150
-124
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.bladejava</groupId>
77
<artifactId>blade-mvc</artifactId>
8-
<version>2.0.11.BETA</version>
8+
<version>2.0.12-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

1111
<name>blade</name>

src/main/java/com/blade/Blade.java

+14-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.blade.ioc.SimpleIoc;
2323
import com.blade.kit.Assert;
2424
import com.blade.kit.BladeKit;
25+
import com.blade.kit.JsonKit;
2526
import com.blade.kit.StringKit;
2627
import com.blade.kit.reload.FileChangeDetector;
2728
import com.blade.loader.BladeLoader;
@@ -809,15 +810,6 @@ public Blade disableSession() {
809810
return this;
810811
}
811812

812-
public Blade disableCost() {
813-
this.environment.set(ENV_KEY_HTTP_REQUEST_COST, false);
814-
return this;
815-
}
816-
817-
public boolean allowCost() {
818-
return this.environment.getBoolean(ENV_KEY_HTTP_REQUEST_COST, true);
819-
}
820-
821813
public Blade watchEnvChange(boolean watchEnvChange) {
822814
this.environment.set(ENV_KEY_APP_WATCH_ENV, watchEnvChange);
823815
return this;
@@ -1040,6 +1032,7 @@ public WebSocketHandler webSocketHandler() {
10401032
private void loadConfig(String[] args) {
10411033
String bootConf = environment().get(ENV_KEY_BOOT_CONF, PROP_NAME);
10421034
Environment bootEnv = Environment.of(bootConf);
1035+
String envName = "default";
10431036

10441037
if (null == bootEnv || bootEnv.isEmpty()) {
10451038
bootEnv = Environment.of(PROP_NAME0);
@@ -1051,9 +1044,8 @@ private void loadConfig(String[] args) {
10511044
entrySet.forEach(entry -> environment.set(entry.getKey(), entry.getValue()));
10521045
}
10531046

1054-
String envName = "default";
1055-
10561047
Map<String, String> argsMap = BladeKit.parseArgs(args);
1048+
log.info("command line args: {}", JsonKit.toString(argsMap));
10571049

10581050
if (StringKit.isNotEmpty(argsMap.get(ENV_KEY_APP_ENV))) {
10591051
envName = argsMap.get(ENV_KEY_APP_ENV);
@@ -1065,10 +1057,16 @@ private void loadConfig(String[] args) {
10651057
// compatible with older versions
10661058
evnFileName = "app-" + envName + ".properties";
10671059
customEnv = Environment.of(evnFileName);
1060+
10681061
if (customEnv != null && !customEnv.isEmpty()) {
1069-
customEnv.props().forEach((key, value) -> this.environment.set(key.toString(), value));
1062+
Iterator<Map.Entry<Object, Object>> iterator = customEnv.props().entrySet().iterator();
1063+
while (iterator.hasNext()) {
1064+
Map.Entry<Object, Object> next = iterator.next();
1065+
this.environment.set(next.getKey().toString(), next.getValue());
1066+
}
10701067
}
10711068
}
1069+
argsMap.remove(ENV_KEY_APP_ENV);
10721070
}
10731071

10741072
log.info("current environment is: {}", envName);
@@ -1080,12 +1078,10 @@ private void loadConfig(String[] args) {
10801078
return;
10811079
}
10821080

1083-
if (StringKit.isNotEmpty(argsMap.get(ENV_KEY_SERVER_ADDRESS))) {
1084-
this.environment.set(ENV_KEY_SERVER_ADDRESS, argsMap.get(ENV_KEY_SERVER_ADDRESS));
1085-
}
1086-
1087-
if (StringKit.isNotEmpty(argsMap.get(ENV_KEY_SERVER_PORT))) {
1088-
this.environment.set(ENV_KEY_SERVER_PORT, argsMap.get(ENV_KEY_SERVER_PORT));
1081+
Iterator<Map.Entry<String, String>> iterator = argsMap.entrySet().iterator();
1082+
while (iterator.hasNext()) {
1083+
Map.Entry<String, String> next = iterator.next();
1084+
this.environment.set(next.getKey(), next.getValue());
10891085
}
10901086

10911087
}

src/main/java/com/blade/kit/BladeKit.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,16 @@ public static void log304(Logger log, String method, String uri) {
254254
log.warn("{} {} {} {}", msg304, pad, method, uri);
255255
}
256256

257-
public static long log200(Logger log, Instant start, String method, String uri) {
257+
public static void log200(Logger log, String method, String uri) {
258+
if (!log.isInfoEnabled()) {
259+
return;
260+
}
261+
String pad = StringKit.padLeft("", 6);
262+
String msg200 = Ansi.BgGreen.and(Ansi.Black).format(" 200 ");
263+
log.info("{} {} {} {}", msg200, pad, method, uri);
264+
}
265+
266+
public static long log200AndCost(Logger log, Instant start, String method, String uri) {
258267
long cost = getCostMS(start);
259268
if (!log.isInfoEnabled()) {
260269
return cost;

src/main/java/com/blade/mvc/Const.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface Const {
3131
int DEFAULT_SERVER_PORT = 9000;
3232
String DEFAULT_SERVER_ADDRESS = "0.0.0.0";
3333
String LOCAL_IP_ADDRESS = "127.0.0.1";
34-
String VERSION = "2.0.11.BETA";
34+
String VERSION = "2.0.11.RELEASE";
3535
String WEB_JARS = "/webjars/";
3636
String CLASSPATH = BladeKit.getCurrentClassPath();
3737
String CONTENT_TYPE_HTML = "text/html; charset=UTF-8";
@@ -69,6 +69,7 @@ public interface Const {
6969
String ENV_KEY_TEMPLATE_PATH = "mvc.template.path";
7070
String ENV_KEY_SERVER_ADDRESS = "server.address";
7171
String ENV_KEY_SERVER_PORT = "server.port";
72+
String ENV_KEY_PERFORMANCE = "server.performance";
7273
String ENV_KEY_SSL = "server.ssl.enable";
7374
String ENV_KEY_SSL_CERT = "server.ssl.cert-path";
7475
String ENE_KEY_SSL_PRIVATE_KEY = "server.ssl.private-key-path";

src/main/java/com/blade/mvc/WebContext.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static void clean() {
163163
}
164164

165165
public Environment environment() {
166-
return blade().environment();
166+
return blade.environment();
167167
}
168168

169169
/**
@@ -191,18 +191,10 @@ public ChannelHandlerContext getChannelHandlerContext() {
191191
return channelHandlerContext;
192192
}
193193

194-
public LocalContext getLocalContext() {
195-
return localContext;
196-
}
197-
198194
public Route getRoute() {
199195
return route;
200196
}
201197

202-
public void setLocalContext(LocalContext localContext) {
203-
this.localContext = localContext;
204-
}
205-
206198
public void setRoute(Route route) {
207199
this.route = route;
208200
}

src/main/java/com/blade/mvc/http/HttpRequest.java

+49-40
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
@NoArgsConstructor
5757
public class HttpRequest implements Request {
5858

59-
private static final HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); // Disk if size exceed
59+
private static final HttpDataFactory factory =
60+
new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); // Disk if size exceed
6061

6162
private static final ByteBuf EMPTY_BUF = Unpooled.copiedBuffer("", CharsetUtil.UTF_8);
6263

@@ -73,13 +74,15 @@ public class HttpRequest implements Request {
7374
private String url;
7475
private String protocol;
7576
private String method;
77+
private String cookieString;
7678
private boolean keepAlive;
7779
private Session session;
7880

7981
private boolean isRequestPart;
8082
private boolean isChunked;
8183
private boolean isMultipart;
8284
private boolean isEnd;
85+
private boolean initCookie;
8386

8487
private Map<String, String> headers = null;
8588
private Map<String, Object> attributes = null;
@@ -147,10 +150,10 @@ public Map<String, String> pathParams() {
147150

148151
@Override
149152
public String queryString() {
150-
if (null != this.url && this.url.contains("?")) {
151-
return this.url.substring(this.url.indexOf("?") + 1);
153+
if (null == url || !url.contains("?")) {
154+
return "";
152155
}
153-
return "";
156+
return url.substring(url.indexOf("?") + 1);
154157
}
155158

156159
@Override
@@ -180,10 +183,14 @@ public HttpMethod httpMethod() {
180183

181184
@Override
182185
public boolean useGZIP() {
183-
boolean useGZIP = WebContext.blade().environment().getBoolean(Const.ENV_KEY_GZIP_ENABLE, false);
186+
187+
boolean useGZIP = WebContext.blade().environment()
188+
.getBoolean(Const.ENV_KEY_GZIP_ENABLE, false);
189+
184190
if (!useGZIP) {
185191
return false;
186192
}
193+
187194
String acceptEncoding = this.header(HttpConst.ACCEPT_ENCODING);
188195
if (StringKit.isEmpty(acceptEncoding)) {
189196
return false;
@@ -209,6 +216,10 @@ public boolean isIE() {
209216

210217
@Override
211218
public Map<String, Cookie> cookies() {
219+
if (!initCookie && StringKit.isNotEmpty(cookieString)) {
220+
initCookie = true;
221+
ServerCookieDecoder.LAX.decode(cookieString).forEach(this::parseCookie);
222+
}
212223
return this.cookies;
213224
}
214225

@@ -336,80 +347,76 @@ public static HttpRequest build(String remoteAddress, HttpObject msg) {
336347
request.uri = cleanUri;
337348
}
338349

339-
SessionManager sessionManager = WebContext.blade().sessionManager();
340-
if (null != sessionManager) {
341-
request.session = SESSION_HANDLER.createSession(request);
350+
if (!HttpServerHandler.PERFORMANCE) {
351+
SessionManager sessionManager = WebContext.blade().sessionManager();
352+
if (null != sessionManager) {
353+
request.session = SESSION_HANDLER.createSession(request);
354+
}
342355
}
343356

344357
HttpServerHandler.setLocalContext(new LocalContext(msg, request, decoder));
345358
return request;
346359
}
347360

348-
private static HttpPostRequestDecoder initRequest(HttpRequest request, io.netty.handler.codec.http.HttpRequest nettyRequest) {
361+
private static HttpPostRequestDecoder initRequest(
362+
HttpRequest request,
363+
io.netty.handler.codec.http.HttpRequest nettyRequest) {
364+
349365
// headers
350366
var httpHeaders = nettyRequest.headers();
351367
if (httpHeaders.isEmpty()) {
352368
request.headers = new HashMap<>();
353369
} else {
354370
request.headers = new HashMap<>(httpHeaders.size());
355371

356-
httpHeaders.forEach(entry -> request.headers.put(entry.getKey(), entry.getValue()));
357-
358-
// Iterator<Map.Entry<String, String>> entryIterator = httpHeaders.iteratorAsString();
359-
// while (entryIterator.hasNext()) {
360-
// var entry = entryIterator.next();
361-
// request.headers.put(entry.getKey(), entry.getValue());
362-
// }
372+
Iterator<Map.Entry<String, String>> entryIterator = httpHeaders.iteratorAsString();
373+
while (entryIterator.hasNext()) {
374+
Map.Entry<String, String> next = entryIterator.next();
375+
request.headers.put(next.getKey(), next.getValue());
376+
}
363377
}
364378

365379
// request query parameters
366-
var parameters = new QueryStringDecoder(request.url(), CharsetUtil.UTF_8).parameters();
367-
if (null != parameters) {
368-
request.parameters = new HashMap<>();
369-
request.parameters.putAll(parameters);
380+
if (request.url().contains("?")) {
381+
var parameters = new QueryStringDecoder(request.url(), CharsetUtil.UTF_8)
382+
.parameters();
383+
384+
if (null != parameters) {
385+
request.parameters.putAll(parameters);
386+
}
370387
}
371388

372-
request.initCookie();
389+
// cookies
390+
request.cookieString = request.header(HttpConst.COOKIE_STRING);
373391

374392
if ("GET".equals(request.method())) {
375393
return null;
376394
}
377395

378396
try {
379-
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(factory, nettyRequest);
397+
HttpPostRequestDecoder decoder =
398+
new HttpPostRequestDecoder(factory, nettyRequest);
399+
380400
request.isMultipart = decoder.isMultipart();
381401
return decoder;
382402
} catch (Exception e) {
383403
throw new HttpParseException("build decoder fail", e);
384404
}
385405
}
386406

387-
private void initCookie() {
388-
// cookies
389-
var cookie = this.header(HttpConst.COOKIE_STRING);
390-
cookie = cookie.length() > 0 ? cookie : this.header(HttpConst.COOKIE_STRING.toLowerCase());
391-
if (null != cookie && cookie.length() > 0) {
392-
ServerCookieDecoder.LAX.decode(cookie).forEach(this::parseCookie);
393-
}
394-
}
395-
396407
/**
397-
* Example of reading request by chunk and getting values from chunk to chunk
408+
* Reading request by chunk and getting values from chunk
398409
*/
399-
private boolean readHttpDataChunkByChunk(HttpPostRequestDecoder decoder) {
410+
private void readHttpDataChunkByChunk(HttpPostRequestDecoder decoder) {
400411
try {
401-
boolean read = false;
402412
while (decoder.hasNext()) {
403-
read = true;
404413
InterfaceHttpData data = decoder.next();
405414
if (data != null) {
406415
parseData(data);
407416
}
408417
}
409-
return read;
410418
} catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
411419
// ignore
412-
return true;
413420
} catch (Exception e) {
414421
throw new HttpParseException(e);
415422
}
@@ -453,7 +460,6 @@ private void parseAttribute(Attribute attribute) throws IOException {
453460
* Parse FileUpload to {@link FileItem}.
454461
*
455462
* @param fileUpload netty http file upload
456-
* @throws IOException
457463
*/
458464
private void parseFileUpload(FileUpload fileUpload) throws IOException {
459465
if (!fileUpload.isCompleted()) {
@@ -466,8 +472,11 @@ private void parseFileUpload(FileUpload fileUpload) throws IOException {
466472
// Upload the file is moved to the specified temporary file,
467473
// because FileUpload will be release after completion of the analysis.
468474
// tmpFile will be deleted automatically if they are used.
469-
Path tmpFile = Files.createTempFile(Paths.get(fileUpload.getFile().getParent()), "blade_", "_upload");
470-
Files.move(Paths.get(fileUpload.getFile().getPath()), tmpFile, StandardCopyOption.REPLACE_EXISTING);
475+
Path tmpFile = Files.createTempFile(
476+
Paths.get(fileUpload.getFile().getParent()), "blade_", "_upload");
477+
478+
Path fileUploadPath = Paths.get(fileUpload.getFile().getPath());
479+
Files.move(fileUploadPath, tmpFile, StandardCopyOption.REPLACE_EXISTING);
471480

472481
fileItem.setFile(tmpFile.toFile());
473482
fileItem.setPath(tmpFile.toFile().getPath());

src/main/java/com/blade/server/netty/HttpConst.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public interface HttpConst {
1717
String COOKIE_STRING = "Cookie";
1818
String METHOD_GET = "GET";
1919
String METHOD_POST = "POST";
20-
String IE_UA = "MSIE";
2120
String DEFAULT_SESSION_KEY = "SESSION";
2221
String SLASH = "/";
2322
char CHAR_SLASH = '/';
@@ -29,7 +28,6 @@ public interface HttpConst {
2928
CharSequence CONTENT_ENCODING = AsciiString.cached("Content-Encoding");
3029
CharSequence DATE = AsciiString.cached("Date");
3130
CharSequence LOCATION = AsciiString.cached("Location");
32-
CharSequence X_POWER_BY = AsciiString.cached("X-Powered-By");
3331
CharSequence EXPIRES = AsciiString.cached("Expires");
3432
CharSequence CACHE_CONTROL = AsciiString.cached("Cache-Control");
3533
CharSequence LAST_MODIFIED = AsciiString.cached("Last-Modified");
@@ -39,6 +37,6 @@ public interface HttpConst {
3937

4038
String CONTENT_TYPE_HTML = "text/html; charset=UTF-8";
4139

42-
CharSequence VERSION = AsciiString.cached("blade-" + Const.VERSION);
40+
String VERSION = "blade-" + Const.VERSION;
4341

4442
}

0 commit comments

Comments
 (0)