Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions opengrok-web/src/main/java/org/opengrok/web/PageConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1828,21 +1827,9 @@ private SortedSet<AcceptedMessage> getProjectMessages() {
* @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">HTTP Caching</a>
*/
public boolean isNotModified(HttpServletRequest request, HttpServletResponse response) {
String currentEtag = String.format("W/\"%s\"",
Objects.hash(
// last modified time as UTC timestamp in millis
getLastModified(),
// all project related messages which changes the view
getProjectMessages(),
// last timestamp value
getEnv().getDateForLastIndexRun() != null ? getEnv().getDateForLastIndexRun().getTime() : 0,
// OpenGrok version has changed since the last time
Info.getVersion()
)
);
String currentEtag = getEtag();

String headerEtag = request.getHeader(HttpHeaders.IF_NONE_MATCH);

if (headerEtag != null && headerEtag.equals(currentEtag)) {
// weak ETag has not changed, return 304 NOT MODIFIED
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
Expand All @@ -1854,13 +1841,19 @@ public boolean isNotModified(HttpServletRequest request, HttpServletResponse res
return false;
}

/**
* @param root root path
* @param path path
* @return path relative to root
*/
public static String getRelativePath(String root, String path) {
return Paths.get(root).relativize(Paths.get(path)).toString();
@VisibleForTesting @NotNull String getEtag() {
return String.format("W/\"%s\"",
Objects.hash(
// last modified time as UTC timestamp in millis
getLastModified(),
// all project related messages which changes the view
getProjectMessages(),
// last timestamp value
getEnv().getDateForLastIndexRun() != null ? getEnv().getDateForLastIndexRun().getTime() : 0,
// OpenGrok version
Info.getVersion()
)
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions opengrok-web/src/main/webapp/mast.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ org.opengrok.indexer.web.Util"%>
return;
}

String redir = cfg.canProcess();
if (redir == null || !redir.isEmpty()) {
if (redir == null) {
String redirectLocation = cfg.canProcess();
if (redirectLocation == null || !redirectLocation.isEmpty()) {
if (redirectLocation == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} else {
response.sendRedirect(redir);
response.sendRedirect(redirectLocation);
}
return;
}
Expand Down
42 changes: 42 additions & 0 deletions opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.stream.Stream;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -68,6 +70,11 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL;
import static org.opengrok.indexer.history.LatestRevisionUtil.getLatestRevision;

Expand Down Expand Up @@ -647,4 +654,39 @@ public String getPathInfo() {
}
};
}

@Test
void testIsNotModifiedEtag() {
HttpServletRequest req = new DummyHttpServletRequest() {
@Override
public String getHeader(String name) {
if (name.equals(HttpHeaders.IF_NONE_MATCH)) {
return "foo"; // will not match the hash computed in
}
return null;
}

@Override
public String getPathInfo() {
return "path";
}
};

PageConfig cfg = PageConfig.get(req);
HttpServletResponse resp = mock(HttpServletResponse.class);
assertFalse(cfg.isNotModified(req, resp));
verify(resp).setHeader(eq(HttpHeaders.ETAG), startsWith("W/"));
}

@Test
void testIsNotModifiedNotModified() {
DummyHttpServletRequest req = mock(DummyHttpServletRequest.class);
when(req.getPathInfo()).thenReturn("/");
PageConfig cfg = PageConfig.get(req);
final String etag = cfg.getEtag();
when(req.getHeader(HttpHeaders.IF_NONE_MATCH)).thenReturn(etag);
HttpServletResponse resp = mock(HttpServletResponse.class);
assertTrue(cfg.isNotModified(req, resp));
verify(resp).setStatus(eq(HttpServletResponse.SC_NOT_MODIFIED));
}
}
Loading