Skip to content

Commit

Permalink
webdav: fix link header
Browse files Browse the repository at this point in the history
Motivation:

A recent path introduced support for adding the `Link` HTTP response
header, according to RFC 6249.  Unfortunately, the code added the Link
header for all requests, not just the intended GET and HEAD requests.

Additionally, due to peculiarities of how Milton generates PROPFIND
results, the `Link` header is added multiple times: once for each
subdirectory.

This results in the HTTP response headers taking up too much space and
dCache failing the request, returning a 500 status code.

Modification:

Add guard to ensure the `Link` header is only added for GET and HEAD
requests, and only if there isn't already a `Link` header.

Result:

A regression is fixed where PROPFIND requests would fail if the
directory contains too many subdirectories.  The cut-off point depends
on the length of the URL the PROPFIND request targets.

Target: master
Request: 9.2
Requires-notes: yes
Requires-book: no
Closes: #7496
Patch: https://rb.dcache.org/r/14201/
Acked-by: Dmitry Litvintsev
  • Loading branch information
paulmillar authored and mksahakyan committed Jan 19, 2024
1 parent 3125694 commit f15e622
Showing 1 changed file with 7 additions and 1 deletion.
Expand Up @@ -43,9 +43,11 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import org.dcache.vehicles.FileAttributes;
Expand Down Expand Up @@ -83,6 +85,8 @@ private interface EntityWriter {

private static final MediaType DEFAULT_ENTITY_TYPE = MediaType.HTML_UTF_8;
private static final MediaType METALINK_ENTITY_TYPE = MediaType.create("application", "metalink4+xml");
private static final Set<Request.Method> LINK_HEADER_REQUESTS =
EnumSet.of(Request.Method.HEAD, Request.Method.GET);

private final Map<MediaType,EntityWriter> supportedMediaTypes = Map.of(
DEFAULT_ENTITY_TYPE, this::htmlEntity,
Expand Down Expand Up @@ -227,7 +231,9 @@ public String getContentType(String accepts) {

// We must set the "Link" HTTP response header here, as we want it to appear for both HEAD
// and GET requests, and (not unreasonably) Milton doesn't call sendContent for HEAD requests.
if (type.equals(DEFAULT_ENTITY_TYPE)) {
if (type.equals(DEFAULT_ENTITY_TYPE)
&& LINK_HEADER_REQUESTS.contains(request.getMethod())
&& HttpManager.response().getNonStandardHeader("Link") == null) {
/* There is a slight subtly here. A GET request that targets a directory with a
* non-trailing-slash URL (e.g., "https://example.org/my-directory") triggers Milton to
* issue a redirection to the corresponding trailing-slash URL
Expand Down

0 comments on commit f15e622

Please sign in to comment.