Skip to content

Commit

Permalink
Add option to serve resources from subpath only with WebDAV Servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Nov 13, 2024
1 parent 2c61ba6 commit 35dc268
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
23 changes: 21 additions & 2 deletions java/org/apache/catalina/servlets/WebdavServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@
/**
* Servlet which adds support for <a href="https://tools.ietf.org/html/rfc4918">WebDAV</a>
* <a href="https://tools.ietf.org/html/rfc4918#section-18">level 3</a>. All the basic HTTP requests are handled by the
* DefaultServlet. The WebDAVServlet must not be used as the default servlet (ie mapped to '/') as it will not work in
* DefaultServlet. The WebdavServlet must not be used as the default servlet (ie mapped to '/') as it will not work in
* this configuration.
* <p>
* Mapping a subpath (e.g. <code>/webdav/*</code> to this servlet has the effect of re-mounting the entire web
* application under that sub-path, with WebDAV access to all the resources. The <code>WEB-INF</code> and
* application under that sub-path, with WebDAV access to all the resources. To restore the DefaultServlet
* behavior set <code>serveSubpathOnly</code> to <code>true</code>. The <code>WEB-INF</code> and
* <code>META-INF</code> directories are protected in this re-mounted resource tree.
* <p>
* To enable WebDAV for a context add the following to web.xml:
Expand Down Expand Up @@ -246,6 +247,12 @@ public class WebdavServlet extends DefaultServlet implements PeriodicEventListen
*/
private boolean strictIfProcessing = true;

/**
* Serve resources from the mounted subpath only, restoring the behavior of
* {@code DefaultServlet}.
*/
private boolean serveSubpathOnly = false;


/**
* Property store used for storage of dead properties.
Expand Down Expand Up @@ -284,6 +291,10 @@ public void init() throws ServletException {
strictIfProcessing = Boolean.parseBoolean(getServletConfig().getInitParameter("strictIfProcessing"));
}

if (getServletConfig().getInitParameter("serveSubpathOnly") != null) {
serveSubpathOnly = Boolean.parseBoolean(getServletConfig().getInitParameter("serveSubpathOnly"));
}

String propertyStore = getServletConfig().getInitParameter("propertyStore");
if (propertyStore != null) {
try {
Expand Down Expand Up @@ -685,6 +696,10 @@ protected boolean checkIfHeaders(HttpServletRequest request, HttpServletResponse
*/
@Override
protected String getRelativePath(HttpServletRequest request, boolean allowEmptyPath) {
if (serveSubpathOnly) {
return super.getRelativePath(request, allowEmptyPath);
}

String pathInfo;

if (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null) {
Expand All @@ -711,6 +726,10 @@ protected String getRelativePath(HttpServletRequest request, boolean allowEmptyP

@Override
protected String getPathPrefix(final HttpServletRequest request) {
if (serveSubpathOnly) {
return super.getPathPrefix(request);
}

// Repeat the servlet path (e.g. /webdav/) in the listing path
String contextPath = request.getContextPath();
if (request.getServletPath() != null) {
Expand Down
8 changes: 8 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
issues do not "pop up" wrt. others).
-->
<section name="Tomcat 9.0.98 (remm)" rtext="in development">
<subsection name="Catalina">
<changelog>
<add>
Add option to serve resources from subpath only with WebDAV Servlet like
with DefaultServlet. (michaelo)
</add>
</changelog>
</subsection>
<subsection name="Coyote">
<changelog>
<fix>
Expand Down

0 comments on commit 35dc268

Please sign in to comment.