diff --git a/src/META-INF/application-template.xml b/src/META-INF/application-template.xml index 280f0801c..7bd2e46e1 100644 --- a/src/META-INF/application-template.xml +++ b/src/META-INF/application-template.xml @@ -229,6 +229,8 @@ + diff --git a/src/org/redkale/net/http/HttpResponse.java b/src/org/redkale/net/http/HttpResponse.java index fde8f1f97..1ce16ce04 100644 --- a/src/org/redkale/net/http/HttpResponse.java +++ b/src/org/redkale/net/http/HttpResponse.java @@ -101,7 +101,7 @@ public class HttpResponse extends Response { private int status = 200; - private String contentType = "text/plain; charset=utf-8"; + private String contentType = ""; private long contentLength = -1; @@ -112,6 +112,10 @@ public class HttpResponse extends Response { private BiFunction bufferHandler; //------------------------------------------------ + private final String plainContentType; + + private final String jsonContentType; + private final DefaultAnyValue header = new DefaultAnyValue(); private final String[][] defaultAddHeaders; @@ -132,9 +136,13 @@ public class HttpResponse extends Response { return new ObjectPool<>(creatCounter, cycleCounter, max, creator, (x) -> ((HttpResponse) x).prepare(), (x) -> ((HttpResponse) x).recycle()); } - public HttpResponse(HttpContext context, HttpRequest request, String[][] defaultAddHeaders, String[][] defaultSetHeaders, + public HttpResponse(HttpContext context, HttpRequest request, + String plainContentType, String jsonContentType, + String[][] defaultAddHeaders, String[][] defaultSetHeaders, HttpCookie defcookie, boolean autoOptions, List< HttpRender> renders) { super(context, request); + this.plainContentType = plainContentType == null || plainContentType.isEmpty() ? "text/plain; charset=utf-8" : plainContentType; + this.jsonContentType = jsonContentType == null || jsonContentType.isEmpty() ? "application/json; charset=utf-8" : jsonContentType; this.defaultAddHeaders = defaultAddHeaders; this.defaultSetHeaders = defaultSetHeaders; this.defcookie = defcookie; @@ -142,6 +150,7 @@ public class HttpResponse extends Response { this.renders = renders; this.hasRender = renders != null && !renders.isEmpty(); this.onlyoneHttpRender = renders != null && renders.size() == 1 ? renders.get(0) : null; + this.contentType = this.plainContentType; } @Override @@ -270,7 +279,7 @@ public class HttpResponse extends Response { * @param obj 输出对象 */ public void finishJson(final Object obj) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = obj; finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), obj)); } @@ -282,7 +291,7 @@ public class HttpResponse extends Response { * @param objs 输出对象 */ public void finishMapJson(final Object... objs) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = objs; finish(request.getJsonConvert().convertMapTo(getBodyBufferSupplier(), objs)); } @@ -294,7 +303,7 @@ public class HttpResponse extends Response { * @param obj 输出对象 */ public void finishJson(final JsonConvert convert, final Object obj) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = obj; finish(convert.convertTo(getBodyBufferSupplier(), obj)); } @@ -307,7 +316,7 @@ public class HttpResponse extends Response { * @param objs 输出对象 */ public void finishMapJson(final JsonConvert convert, final Object... objs) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = objs; finish(convert.convertMapTo(getBodyBufferSupplier(), objs)); } @@ -319,7 +328,7 @@ public class HttpResponse extends Response { * @param obj 输出对象 */ public void finishJson(final Type type, final Object obj) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; this.output = obj; finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), type, obj)); } @@ -332,7 +341,7 @@ public class HttpResponse extends Response { * @param obj 输出对象 */ public void finishJson(final JsonConvert convert, final Type type, final Object obj) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = obj; finish(convert.convertTo(getBodyBufferSupplier(), type, obj)); } @@ -343,7 +352,7 @@ public class HttpResponse extends Response { * @param objs 输出对象 */ public void finishJson(final Object... objs) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = objs; finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), objs)); } @@ -354,7 +363,7 @@ public class HttpResponse extends Response { * @param ret RetResult输出对象 */ public void finishJson(final org.redkale.service.RetResult ret) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = ret; if (ret != null && !ret.isSuccess()) { this.header.addValue("retcode", String.valueOf(ret.getRetcode())); @@ -370,7 +379,7 @@ public class HttpResponse extends Response { * @param ret RetResult输出对象 */ public void finishJson(final JsonConvert convert, final org.redkale.service.RetResult ret) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; if (this.recycleListener != null) this.output = ret; if (ret != null && !ret.isSuccess()) { this.header.addValue("retcode", String.valueOf(ret.getRetcode())); @@ -494,9 +503,9 @@ public class HttpResponse extends Response { } } if (convert instanceof JsonConvert) { - this.contentType = "application/json; charset=utf-8"; + this.contentType = this.jsonContentType; } else if (convert instanceof TextConvert) { - this.contentType = "text/plain; charset=utf-8"; + this.contentType = this.plainContentType; } if (this.recycleListener != null) this.output = obj; if (obj instanceof org.redkale.service.RetResult) { @@ -837,7 +846,7 @@ public class HttpResponse extends Response { ByteBuffer buffer = this.pollWriteReadBuffer(); buffer.put(("HTTP/1.1 " + this.status + " " + (this.status == 200 ? "OK" : httpCodes.get(this.status)) + "\r\n").getBytes()); - buffer.put(("Content-Type: " + (this.contentType == null ? "text/plain; charset=utf-8" : this.contentType) + "\r\n").getBytes()); + buffer.put(("Content-Type: " + (this.contentType == null ? this.plainContentType : this.contentType) + "\r\n").getBytes()); if (this.contentLength >= 0) { buffer.put(("Content-Length: " + this.contentLength + "\r\n").getBytes()); diff --git a/src/org/redkale/net/http/HttpServer.java b/src/org/redkale/net/http/HttpServer.java index 3ec9ae159..321ec304a 100644 --- a/src/org/redkale/net/http/HttpServer.java +++ b/src/org/redkale/net/http/HttpServer.java @@ -295,7 +295,8 @@ public class HttpServer extends Server defaultAddHeaders = new ArrayList<>(); final List defaultSetHeaders = new ArrayList<>(); boolean autoOptions = false; - + String plainContentType = null; + String jsonContentType = null; HttpCookie defaultCookie = null; String remoteAddrHeader = null; if (config != null) { @@ -314,6 +315,11 @@ public class HttpServer extends Server 0) { for (AnyValue addHeader : addHeaders) { @@ -363,6 +369,8 @@ public class HttpServer extends Server responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null); HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, this.sslContext, rcapacity, bufferPool, responsePool, this.maxbody, this.charset, this.address, this.resourceFactory, this.prepare, this.readTimeoutSecond, this.writeTimeoutSecond); - responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), addHeaders, setHeaders, defCookie, options, ((HttpPrepareServlet) prepare).renders)); + responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), + plainType, jsonType, addHeaders, setHeaders, defCookie, options, ((HttpPrepareServlet) prepare).renders)); return httpcontext; }