1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.ssi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet to process SSI requests within a webpage. Mapped to a path from within web.xml.
*/
public class SSIServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/** Debug level for this servlet. */
protected int debug = 0;
/** Should the output be buffered. */
protected boolean buffered = false;
/** Expiration time in seconds for the doc. */
protected Long expires = null;
/** virtual path can be webapp-relative */
protected boolean isVirtualWebappRelative = false;
/** Input encoding. If not specified, uses platform default */
protected String inputEncoding = null;
/** Output encoding. If not specified, uses platform default */
protected String outputEncoding = "UTF-8";
/** Allow exec (normally blocked for security) */
protected boolean allowExec = false;
// ----------------- Public methods.
@Override
public void init() throws ServletException {
if (getServletConfig().getInitParameter("debug") != null) {
debug = Integer.parseInt(getServletConfig().getInitParameter("debug"));
}
isVirtualWebappRelative = Boolean.parseBoolean(getServletConfig().getInitParameter("isVirtualWebappRelative"));
if (getServletConfig().getInitParameter("expires") != null) {
expires = Long.valueOf(getServletConfig().getInitParameter("expires"));
}
buffered = Boolean.parseBoolean(getServletConfig().getInitParameter("buffered"));
inputEncoding = getServletConfig().getInitParameter("inputEncoding");
if (getServletConfig().getInitParameter("outputEncoding") != null) {
outputEncoding = getServletConfig().getInitParameter("outputEncoding");
}
allowExec = Boolean.parseBoolean(getServletConfig().getInitParameter("allowExec"));
if (debug > 0) {
log("SSIServlet.init() SSI invoker started with 'debug'=" + debug);
}
}
/**
* Process and forward the GET request to our <code>requestHandler()</code>.
*
* @param req a value of type 'HttpServletRequest'
* @param res a value of type 'HttpServletResponse'
*
* @exception IOException if an error occurs
* @exception ServletException if an error occurs
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
if (debug > 0) {
log("SSIServlet.doGet()");
}
requestHandler(req, res);
}
/**
* Process and forward the POST request to our <code>requestHandler()</code>.
*
* @param req a value of type 'HttpServletRequest'
* @param res a value of type 'HttpServletResponse'
*
* @exception IOException if an error occurs
* @exception ServletException if an error occurs
*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
if (debug > 0) {
log("SSIServlet.doPost()");
}
requestHandler(req, res);
}
/**
* Process our request and locate right SSI command.
*
* @param req a value of type 'HttpServletRequest'
* @param res a value of type 'HttpServletResponse'
*
* @throws IOException an IO error occurred
*/
protected void requestHandler(HttpServletRequest req, HttpServletResponse res) throws IOException {
ServletContext servletContext = getServletContext();
String path = SSIServletRequestUtil.getRelativePath(req);
if (debug > 0) {
log("SSIServlet.requestHandler()\n" + "Serving " + (buffered ? "buffered " : "unbuffered ") + "resource '" +
path + "'");
}
// Exclude any resource in the /WEB-INF and /META-INF subdirectories
// (the "toUpperCase()" avoids problems on Windows systems)
if (path == null || path.toUpperCase(Locale.ENGLISH).startsWith("/WEB-INF") ||
path.toUpperCase(Locale.ENGLISH).startsWith("/META-INF")) {
res.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
URL resource = servletContext.getResource(path);
if (resource == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String resourceMimeType = servletContext.getMimeType(path);
if (resourceMimeType == null) {
resourceMimeType = "text/html";
}
res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
if (expires != null) {
res.setDateHeader("Expires", (new java.util.Date()).getTime() + expires.longValue() * 1000);
}
processSSI(req, res, resource);
}
protected void processSSI(HttpServletRequest req, HttpServletResponse res, URL resource) throws IOException {
SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(getServletContext(), req, res,
isVirtualWebappRelative, debug, inputEncoding);
SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec);
PrintWriter printWriter;
StringWriter stringWriter = null;
if (buffered) {
stringWriter = new StringWriter();
printWriter = new PrintWriter(stringWriter);
} else {
printWriter = res.getWriter();
}
URLConnection resourceInfo = resource.openConnection();
InputStream resourceInputStream = resourceInfo.getInputStream();
String encoding = resourceInfo.getContentEncoding();
if (encoding == null) {
encoding = inputEncoding;
}
InputStreamReader isr;
if (encoding == null) {
isr = new InputStreamReader(resourceInputStream);
} else {
isr = new InputStreamReader(resourceInputStream, encoding);
}
try (BufferedReader bufferedReader = new BufferedReader(isr)) {
long lastModified = ssiProcessor.process(bufferedReader, resourceInfo.getLastModified(), printWriter);
if (lastModified > 0) {
res.setDateHeader("last-modified", lastModified);
}
if (stringWriter != null) {
printWriter.flush();
String text = stringWriter.toString();
res.getWriter().write(text);
}
}
}
}
|