Skip to content

Commit

Permalink
prepare to v1.0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
xnbox committed Sep 23, 2021
1 parent c8c7043 commit 9dd049c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 45 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Your 100% static dynamic backend</h1>

<a title="License MIT" href="https://github.com/xnbox/DeepfakeHTTP/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue?style=flat-square"></a>
<a title="Release 1.0.14" href="https://github.com/xnbox/DeepfakeHTTP/releases"><img src="https://img.shields.io/badge/release-1.0.14-4DC71F?style=flat-square"></a>
<a title="Release 1.0.15" href="https://github.com/xnbox/DeepfakeHTTP/releases"><img src="https://img.shields.io/badge/release-1.0.15-4DC71F?style=flat-square"></a>
<a title="Powered by Tommy" href="https://github.com/xnbox/tommy"><img src="https://img.shields.io/badge/powered_by-Tommy-blueviolet?style=flat-square"></a>

<p align="center">
Expand Down Expand Up @@ -75,13 +75,17 @@ OPTIONS:
--collect <file> collect live request/response to file
--data <file> specify json/yaml data file to populate templates
--format <json|yaml> output format for --print-* commands, default: json
--status <number> status code for non-matching requests, default: 400
FLAGS:
--no-log disable request/response console logging
--no-etag disable ETag optimization
--no-cors disable CORS headers
--no-etag disable 'ETag' header
--no-powered-by disable 'X-Powered-By' header
--no-watch disable watch files for changes
--no-color disable ANSI color output for --print-* commands
--no-pretty disable prettyprint for --print-* commands
--strict-json enable strict JSON comparison
--redirect redirect HTTP to HTTPS
COMMANDS:
Expand Down
2 changes: 1 addition & 1 deletion build-DeepfakeHTTP.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ E-Mail: xnbox.team@outlook.com

<target name="main">
<property name="dollar" value="$" />
<property name="build.version" value="1.0.14" />
<property name="build.version" value="1.0.15" />
<property name="tmpAppWar" value="${java.io.tmpdir}/app.war" />
<property name="tmpMetaInfDir" value="${java.io.tmpdir}/DeepfakeHTTP/META-INF" />
<property name="tmpManifest" value="${tmpMetaInfDir}/MANIFEST.MF" />
Expand Down
48 changes: 24 additions & 24 deletions src/org/deepfake_http/common/servlet/DeepfakeHttpServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ of this software and associated documentation files (the "Software"), to deal
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
* TODO
* - support block comments in dumps
* --no-cors
*/
public class DeepfakeHttpServlet extends HttpServlet {
/**
* serialVersionUID
Expand All @@ -120,7 +115,7 @@ public class DeepfakeHttpServlet extends HttpServlet {
public static final String INTERNAL_HTTP_HEADER_X_OPENAPI_DESCRIPTION = "X-OpenAPI-Description";// request non-standard
public static final String INTERNAL_HTTP_HEADER_X_OPENAPI_TAGS = "X-OpenAPI-Tags"; // request non-standard

private static final String X_POWERED_BY_VALUE = "DeepfakeHTTP";
private static final String X_POWERED_BY_VALUE = "DeepfakeHTTP " + System.getProperty("build.version") + " (" + System.getProperty("build.timestamp") + ")";

private byte[] openApiJsonBs;
private byte[] openApiYamlBs;
Expand All @@ -130,8 +125,10 @@ public class DeepfakeHttpServlet extends HttpServlet {
private boolean noEtag;
private boolean noLog;
private boolean noCors;
private boolean noPoweredBy;
private boolean noColor;
private boolean strictJson;
private int badRequestStatus;

private String collectFile;
private String openApiPath;
Expand Down Expand Up @@ -185,16 +182,18 @@ public void init(ServletConfig servletConfig) throws ServletException {
String[] args = (String[]) ctx.lookup("java:comp/env/tommy/args");

Map<String, Object> paramMap = ParseCommandLineUtils.parseCommandLineArgs(logger, args, dumps);
noWatch = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_WATCH);
noEtag = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_ETAG);
noLog = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_LOG);
noCors = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_CORS);
noColor = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_COLOR);
strictJson = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_STRICT_JSON);
collectFile = (String) paramMap.get(ParseCommandLineUtils.ARGS_COLLECT);
openApiPath = (String) paramMap.get(ParseCommandLineUtils.ARGS_OPENAPI_PATH);
openApiTitle = (String) paramMap.get(ParseCommandLineUtils.ARGS_OPENAPI_TITLE);
String dataFile = (String) paramMap.get(ParseCommandLineUtils.ARGS_DATA);
noWatch = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_WATCH);
noEtag = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_ETAG);
noLog = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_LOG);
noCors = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_CORS);
noPoweredBy = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_POWERED_BY);
noColor = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_NO_COLOR);
strictJson = (boolean) paramMap.get(ParseCommandLineUtils.ARGS_STRICT_JSON);
collectFile = (String) paramMap.get(ParseCommandLineUtils.ARGS_COLLECT);
openApiPath = (String) paramMap.get(ParseCommandLineUtils.ARGS_OPENAPI_PATH);
openApiTitle = (String) paramMap.get(ParseCommandLineUtils.ARGS_OPENAPI_TITLE);
dataFile = (String) paramMap.get(ParseCommandLineUtils.ARGS_DATA);
badRequestStatus = (int) paramMap.get(ParseCommandLineUtils.ARGS_STATUS);

if (openApiTitle == null)
openApiTitle = "";
Expand Down Expand Up @@ -539,7 +538,7 @@ else if (INTERNAL_HTTP_HEADER_X_SERVER_RESPONSE_DELAY.toLowerCase(Locale.ENGLISH
boolean simpleBadRequest400;
if (reqResp == null) {
reqResp = new ReqResp();
reqResp.response.firstLine = ParseDumpUtils.HTTP_1_1 + ' ' + HttpServletResponse.SC_BAD_REQUEST + ' ' + "Bad request";
reqResp.response.firstLine = ParseDumpUtils.HTTP_1_1 + ' ' + badRequestStatus + ' ' + "Bad request";
simpleBadRequest400 = true;
} else
simpleBadRequest400 = false;
Expand Down Expand Up @@ -579,8 +578,9 @@ else if (INTERNAL_HTTP_HEADER_X_SERVER_RESPONSE_DELAY.toLowerCase(Locale.ENGLISH
responseHeaders.put(header.name, header.value);
}

if (!responseHeaders.containsKey(HTTP_HEADER_X_POWERED_BY))
responseHeaders.put(HTTP_HEADER_X_POWERED_BY, X_POWERED_BY_VALUE);
if (!noPoweredBy)
if (!responseHeaders.containsKey(HTTP_HEADER_X_POWERED_BY))
responseHeaders.put(HTTP_HEADER_X_POWERED_BY, X_POWERED_BY_VALUE);

if (requestDelay != 0)
Thread.sleep(requestDelay);
Expand Down Expand Up @@ -634,7 +634,7 @@ else if ("application/x-sh".equals(bodyType)) {
responseHeaders.put(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(bs.length));

if (!noEtag) {
if (status != HttpServletResponse.SC_BAD_REQUEST) {
if (status != badRequestStatus) {
String etag = "\"" + Integer.toHexString(Murmur3.hash32(bs)) + "\""; // Murmur3 32-bit variant

String etagFromClient = request.getHeader(HTTP_HEADER_IF_NONE_MATCH);
Expand Down Expand Up @@ -745,7 +745,7 @@ private void logReqRespToConsole(HttpServletRequest request, String providedFirs
String headersColor;
String contentColor;

if (status == HttpServletResponse.SC_BAD_REQUEST) {
if (status == badRequestStatus) {
firstLineColor = IAnsi.CYAN_BOLD_BRIGHT;
headersColor = IAnsi.CYAN;
contentColor = IAnsi.CYAN_BRIGHT;
Expand Down Expand Up @@ -791,7 +791,7 @@ private void logReqRespToConsole(HttpServletRequest request, String providedFirs
if (color)
baos.write(IAnsi.RESET.getBytes(StandardCharsets.UTF_8));

if (status == HttpServletResponse.SC_BAD_REQUEST) {
if (status == badRequestStatus) {
firstLineColor = IAnsi.RED_BOLD_BRIGHT;
headersColor = IAnsi.PURPLE;
contentColor = IAnsi.PURPLE_BRIGHT;
Expand Down Expand Up @@ -1026,10 +1026,10 @@ private Map<String, Object> createHttpObject(String requestMethod, String reques
* @return
* @throws Exception
*/
private static ReqResp getBadRequest400(List<ReqResp> reqResps) throws Exception {
private ReqResp getBadRequest400(List<ReqResp> reqResps) throws Exception {
for (ReqResp reqResp : reqResps) {
FirstLineResp firstLineResp = new FirstLineResp(reqResp.response.firstLine);
if (firstLineResp.getStatus() == HttpServletResponse.SC_BAD_REQUEST)
if (firstLineResp.getStatus() == badRequestStatus)
return reqResp;
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/org/deepfake_http/common/utils/OpenApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ else if (lowerCaseHeaderName.equals(DeepfakeHttpServlet.INTERNAL_HTTP_HEADER_X_O
mapMethodProps.put("tags", openApiMethodTags);

Object objBody;
if (requestContentType != null) {
if (requestContentType != null && !requestBody.isEmpty()) {
Map<String, Object> mapOpenApiRequestBody = new LinkedHashMap<>();
mapMethodProps.put("requestBody", mapOpenApiRequestBody);

Expand Down
42 changes: 26 additions & 16 deletions src/org/deepfake_http/common/utils/ParseCommandLineUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.logging.Level;
import java.util.logging.Logger;

import jakarta.servlet.http.HttpServletResponse;

public class ParseCommandLineUtils {
/**
* https://no-color.org
Expand Down Expand Up @@ -86,20 +88,21 @@ public class ParseCommandLineUtils {
//@formatter:on

/* command line args */
public static final String ARGS_NO_WATCH = "--no-watch"; // disable watch dump files for changes
public static final String ARGS_NO_ETAG = "--no-etag"; // disable ETag optimization
public static final String ARGS_NO_LOG = "--no-log"; // disable request/response console logging
public static final String ARGS_NO_CORS = "--no-cors"; // disable CORS headers
public static final String ARGS_STRICT_JSON = "--strict-json"; // enable strict JSON comparison
public static final String ARGS_COLLECT = "--collect"; // collect live request/response dumps to file
public static final String ARGS_OPENAPI_PATH = "--openapi-path"; // serve OpenAPI client at specified context path
public static final String ARGS_OPENAPI_TITLE = "--openapi-title"; // provide custom OpenAPI spec title
public static final String ARGS_DATA = "--data"; // specify json/yaml data file to populate templates

public static final String ARGS_NO_WATCH = "--no-watch"; // disable watch dump files for changes
public static final String ARGS_NO_ETAG = "--no-etag"; // disable ETag optimization
public static final String ARGS_NO_LOG = "--no-log"; // disable request/response console logging
public static final String ARGS_NO_CORS = "--no-cors"; // disable CORS headers
public static final String ARGS_NO_POWERED_BY = "--no-powered-by"; // disable 'X-Powered-By' header
public static final String ARGS_STRICT_JSON = "--strict-json"; // enable strict JSON comparison
public static final String ARGS_COLLECT = "--collect"; // collect live request/response dumps to file
public static final String ARGS_OPENAPI_PATH = "--openapi-path"; // serve OpenAPI client at specified context path
public static final String ARGS_OPENAPI_TITLE = "--openapi-title"; // provide custom OpenAPI spec title
public static final String ARGS_DATA = "--data"; // specify json/yaml data file to populate templates
public static final String ARGS_PRINT_INFO = "--print-info";
public static final String ARGS_PRINT_REQUESTS = "--print-requests";
public static final String ARGS_PRINT_OPENAPI = "--print-openapi";
public static final String ARGS_FORMAT = "--format";
public static final String ARGS_STATUS = "--status"; // status code for non-matching requests, default: 400
public static final String ARGS_NO_PRETTY = "--no-pretty";
public static final String ARGS_NO_COLOR = "--no-color";

Expand All @@ -126,20 +129,22 @@ public static Map<String, Object> parseCommandLineArgs(Logger logger, //
paramMap.put(ARGS_NO_ETAG, false);
paramMap.put(ARGS_NO_LOG, false);
paramMap.put(ARGS_NO_CORS, false);
paramMap.put(ARGS_NO_POWERED_BY, false);
paramMap.put(ARGS_STRICT_JSON, false);
paramMap.put(ARGS_NO_COLOR, false);
paramMap.put(ARGS_COLLECT, null);
paramMap.put(ARGS_OPENAPI_PATH, null);
paramMap.put(ARGS_OPENAPI_TITLE, "");
paramMap.put(ARGS_DATA, null);
paramMap.put(ARGS_FORMAT, "json");
paramMap.put(ARGS_STATUS, HttpServletResponse.SC_BAD_REQUEST); // 400

for (int i = 0; i < args.length; i++) {
/* skip original Tommy options */

// Tommy options Start
if (args[i].equals(ARGS_HELP_OPTION))
paramMap.put(ARGS_HELP_OPTION, true);
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_APP_OPTION))
i++;
else if (args[i].equals(ARGS_PORT_OPTION))
Expand All @@ -162,22 +167,24 @@ else if (args[i].equals(ARGS_NO_LOG))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_NO_CORS))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_NO_POWERED_BY))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_STRICT_JSON))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_NO_COLOR))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_COLLECT)) {
if (i < args.length - 1)
paramMap.put(ARGS_COLLECT, args[++i]);
paramMap.put(args[i], args[++i]);
} else if (args[i].equals(ARGS_OPENAPI_PATH)) {
if (i < args.length - 1)
paramMap.put(ARGS_OPENAPI_PATH, args[++i]);
paramMap.put(args[i], args[++i]);
} else if (args[i].equals(ARGS_OPENAPI_TITLE)) {
if (i < args.length - 1)
paramMap.put(ARGS_OPENAPI_TITLE, args[++i]);
paramMap.put(args[i], args[++i]);
} else if (args[i].equals(ARGS_DATA)) {
if (i < args.length - 1)
paramMap.put(ARGS_DATA, args[++i]);
paramMap.put(args[i], args[++i]);
} else if (args[i].equals(ARGS_PRINT_INFO))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_PRINT_REQUESTS))
Expand All @@ -188,7 +195,10 @@ else if (args[i].equals(ARGS_NO_PRETTY))
paramMap.put(args[i], true);
else if (args[i].equals(ARGS_FORMAT)) {
if (i < args.length - 1)
paramMap.put(ARGS_FORMAT, args[++i].toLowerCase(Locale.ENGLISH));
paramMap.put(args[i], args[++i].toLowerCase(Locale.ENGLISH));
} else if (args[i].equals(ARGS_STATUS)) {
if (i < args.length - 1)
paramMap.put(args[i], Integer.parseInt(args[++i]));
} else {
String fileName = args[i];
if (fileName.startsWith("--"))
Expand Down
4 changes: 3 additions & 1 deletion src/org/tommy/main/CustomMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ public static void main(String[] args) {
sb.append(" --collect <file> collect live request/response to file \n");
sb.append(" --data <file> specify json/yaml data file to populate templates \n");
sb.append(" --format <json|yaml> output format for --print-* commands, default: json\n");
sb.append(" --status <number> status code for non-matching requests, default: 400\n");
sb.append(" \n");
sb.append(" FLAGS: \n");
sb.append(" --no-log disable request/response console logging \n");
sb.append(" --no-etag disable ETag optimization \n");
sb.append(" --no-cors disable CORS headers \n");
sb.append(" --no-etag disable 'ETag' header \n");
sb.append(" --no-powered-by disable 'X-Powered-By' header \n");
sb.append(" --no-watch disable watch files for changes \n");
sb.append(" --no-color disable ANSI color output for --print-* commands \n");
sb.append(" --no-pretty disable prettyprint for --print-* commands \n");
Expand Down

0 comments on commit 9dd049c

Please sign in to comment.