Skip to content

Commit 7d84fb5

Browse files
tulinkryVladimir Kotal
authored and
Vladimir Kotal
committed
Using weak tag instead of just last modification date (#2435)
- This enables us to reflect all other factors which contribute to the resource freshness the last file timestamp messages on the project the timestamp of last index the opengrok version - We assemble all these to the weak tag, and the resource is reloaded only in any of these change
1 parent 71e6069 commit 7d84fb5

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/PageConfig.java

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Comparator;
4242
import java.util.EnumSet;
4343
import java.util.List;
44+
import java.util.Objects;
4445
import java.util.Set;
4546
import java.util.SortedSet;
4647
import java.util.TreeSet;
@@ -51,8 +52,9 @@
5152
import javax.servlet.ServletRequest;
5253
import javax.servlet.http.Cookie;
5354
import javax.servlet.http.HttpServletRequest;
54-
import org.suigeneris.jrcs.diff.Diff;
55-
import org.suigeneris.jrcs.diff.DifferentiationFailedException;
55+
import javax.servlet.http.HttpServletResponse;
56+
import javax.ws.rs.core.HttpHeaders;
57+
import org.opengrok.indexer.Info;
5658
import org.opengrok.indexer.analysis.AnalyzerGuru;
5759
import org.opengrok.indexer.analysis.ExpandTabsReader;
5860
import org.opengrok.indexer.analysis.FileAnalyzer.Genre;
@@ -70,6 +72,8 @@
7072
import org.opengrok.indexer.search.QueryBuilder;
7173
import org.opengrok.indexer.util.IOUtils;
7274
import org.opengrok.indexer.web.messages.MessagesContainer.AcceptedMessage;
75+
import org.suigeneris.jrcs.diff.Diff;
76+
import org.suigeneris.jrcs.diff.DifferentiationFailedException;
7377

7478
/**
7579
* A simple container to lazy initialize common vars wrt. a single request. It
@@ -1619,4 +1623,76 @@ public void checkSourceRootExistence() throws IOException {
16191623
throw new IOException(String.format("Source root path \"%s\" is not readable", sourceRootPathFile.getAbsolutePath()));
16201624
}
16211625
}
1626+
1627+
/**
1628+
* Get all project related messages. These include
1629+
* <ol>
1630+
* <li>Main messages</li>
1631+
* <li>Messages with tag = project name</li>
1632+
* <li>Messages with tag = project's groups names</li>
1633+
* </ol>
1634+
*
1635+
* @return the sorted set of messages according to the accept time
1636+
* @see org.opengrok.indexer.web.messages.MessagesContainer#MESSAGES_MAIN_PAGE_TAG
1637+
*/
1638+
private SortedSet<AcceptedMessage> getProjectMessages() {
1639+
SortedSet<AcceptedMessage> messages = getMessages();
1640+
1641+
if (getProject() != null) {
1642+
messages.addAll(getMessages(getProject().getName()));
1643+
getProject().getGroups().forEach(group -> {
1644+
messages.addAll(getMessages(group.getName()));
1645+
});
1646+
}
1647+
1648+
return messages;
1649+
}
1650+
1651+
/**
1652+
* Decide if this resource has been modified since the header value in the request.
1653+
* <p>
1654+
* The resource is modified since the weak ETag value in the request, the ETag is
1655+
* computed using:
1656+
* <ul>
1657+
* <li>the source file modification</li>
1658+
* <li>project messages</li>
1659+
* <li>last timestamp for index</li>
1660+
* <li>OpenGrok current deployed version</li>
1661+
* </ul>
1662+
* <p>
1663+
* <p>
1664+
* If the resource was modified, appropriate headers in the response are filled.
1665+
*
1666+
* @param request the http request containing the headers
1667+
* @param response the http response for setting the headers
1668+
* @return true if resource was not modified; false otherwise
1669+
* @see <a href="https://tools.ietf.org/html/rfc7232#section-2.3">HTTP ETag</a>
1670+
* @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">HTTP Caching</a>
1671+
*/
1672+
public boolean isNotModified(HttpServletRequest request, HttpServletResponse response) {
1673+
String currentEtag = String.format("W/\"%s\"",
1674+
Objects.hash(
1675+
// last modified time as UTC timestamp in millis
1676+
getLastModified(),
1677+
// all project related messages which changes the view
1678+
getProjectMessages(),
1679+
// last timestamp value
1680+
getEnv().getDateForLastIndexRun().getTime(),
1681+
// OpenGrok version has changed since the last time
1682+
Info.getVersion()
1683+
)
1684+
);
1685+
1686+
String headerEtag = request.getHeader(HttpHeaders.IF_NONE_MATCH);
1687+
1688+
if (headerEtag != null && headerEtag.equals(currentEtag)) {
1689+
// weak ETag has not changed, return 304 NOT MODIFIED
1690+
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
1691+
return true;
1692+
}
1693+
1694+
// return 200 OK
1695+
response.setHeader(HttpHeaders.ETAG, currentEtag);
1696+
return false;
1697+
}
16221698
}

opengrok-web/src/main/webapp/WEB-INF/web.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,8 @@
5656
<filter-mapping>
5757
<filter-name>ExpiresHalfHourFilter</filter-name>
5858
<url-pattern>/history/*</url-pattern>
59-
<url-pattern>/xref/*</url-pattern>
6059
<url-pattern>/raw/*</url-pattern>
6160
<url-pattern>/download/*</url-pattern>
62-
<url-pattern>/diff/*</url-pattern>
63-
<url-pattern>/more/*</url-pattern>
6461
<url-pattern>/rss/*</url-pattern>
6562
<url-pattern>/error</url-pattern>
6663
<url-pattern>/enoent</url-pattern>

opengrok-web/src/main/webapp/mast.jsp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ org.opengrok.indexer.web.Util"%><%
5454
return;
5555
}
5656
57-
// jel: hmmm - questionable for dynamic content
58-
long flast = cfg.getLastModified();
59-
if (request.getDateHeader("If-Modified-Since") >= flast) {
60-
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
57+
if (cfg.isNotModified(request, response)) {
58+
// the resource was not modified
59+
// the code 304 NOT MODIFIED has been inserted to the response
6160
return;
6261
}
63-
response.setDateHeader("Last-Modified", flast);
6462
6563
// Use UTF-8 if no encoding is specified in the request
6664
if (request.getCharacterEncoding() == null) {

0 commit comments

Comments
 (0)