Processes the exec directive to run CGI scripts or system commands.
(SSIMediator ssiMediator, String commandName, String[] paramNames, String[] paramValues,
PrintWriter writer)
| 50 | * Processes the exec directive to run CGI scripts or system commands. |
| 51 | */ |
| 52 | @Override |
| 53 | public long process(SSIMediator ssiMediator, String commandName, String[] paramNames, String[] paramValues, |
| 54 | PrintWriter writer) { |
| 55 | long lastModified = 0; |
| 56 | if (paramNames.length == 0 || paramValues.length == 0) { |
| 57 | return 0; |
| 58 | } |
| 59 | String configErrMsg = ssiMediator.getConfigErrMsg(); |
| 60 | String paramName = paramNames[0]; |
| 61 | String paramValue = paramValues[0]; |
| 62 | String substitutedValue = ssiMediator.substituteVariables(paramValue); |
| 63 | if (paramName.equalsIgnoreCase("cgi")) { |
| 64 | lastModified = ssiInclude.process(ssiMediator, "include", new String[] { "virtual" }, |
| 65 | new String[] { substitutedValue }, writer); |
| 66 | } else if (paramName.equalsIgnoreCase("cmd")) { |
| 67 | boolean foundProgram = false; |
| 68 | try { |
| 69 | Runtime rt = Runtime.getRuntime(); |
| 70 | ArrayList<String> tokens = new ArrayList<>(); |
| 71 | StringBuilder current = new StringBuilder(); |
| 72 | boolean inDoubleQuote = false; |
| 73 | boolean inSingleQuote = false; |
| 74 | for (int j = 0; j < substitutedValue.length(); j++) { |
| 75 | char c = substitutedValue.charAt(j); |
| 76 | if (c == '"' && !inSingleQuote) { |
| 77 | inDoubleQuote = !inDoubleQuote; |
| 78 | } else if (c == '\'' && !inDoubleQuote) { |
| 79 | inSingleQuote = !inSingleQuote; |
| 80 | } else if (Character.isWhitespace(c) && !inDoubleQuote && !inSingleQuote) { |
| 81 | if (current.length() > 0) { |
| 82 | tokens.add(current.toString()); |
| 83 | current.setLength(0); |
| 84 | } |
| 85 | } else { |
| 86 | current.append(c); |
| 87 | } |
| 88 | } |
| 89 | if (current.length() > 0) { |
| 90 | tokens.add(current.toString()); |
| 91 | } |
| 92 | String[] cmdArray = tokens.toArray(new String[0]); |
| 93 | if (cmdArray.length == 0) { |
| 94 | throw new IOException(sm.getString("ssiExec.noCommand")); |
| 95 | } |
| 96 | Process proc = rt.exec(cmdArray); |
| 97 | foundProgram = true; |
| 98 | char[] buf = new char[BUFFER_SIZE]; |
| 99 | try { |
| 100 | try (BufferedReader stdOutReader = new BufferedReader(new InputStreamReader(proc.getInputStream())); |
| 101 | BufferedReader stdErrReader = new BufferedReader( |
| 102 | new InputStreamReader(proc.getErrorStream()))) { |
| 103 | // We don't spawn a thread here, since this is costly. stderr would be usually written |
| 104 | // right away and the amount written would be small |
| 105 | IOTools.flow(stdOutReader, writer, buf); |
| 106 | IOTools.flow(stdErrReader, writer, buf); |
| 107 | } |
| 108 | proc.waitFor(); |
| 109 | } finally { |
nothing calls this directly
no test coverage detected