Fix resource collection modification times

The resource collection modification times weren't updated when an
individual resource was updated. By tracking back the path of a resource
and updating the modification times, all nodes in the path will get the
correct modification time.
This commit is contained in:
Markus Mäkelä 2017-08-04 22:09:37 +03:00
parent 7e18e49eec
commit 1308e7a304
2 changed files with 44 additions and 11 deletions

View File

@ -42,6 +42,20 @@ using std::stringstream;
using mxs::SpinLock;
using mxs::SpinLockGuard;
static bool drop_path_part(std::string& path)
{
size_t pos = path.find_last_of('/');
bool rval = false;
if (pos != std::string::npos)
{
path.erase(pos);
rval = true;
}
return rval && path.length();
}
/**
* Class that keeps track of resource modification times
*/
@ -54,21 +68,27 @@ public:
{
}
void modify(const string& path)
void modify(const std::string& orig_path)
{
map<string, uint64_t>::iterator it = m_etag.find(path);
std::string path = orig_path;
if (it != m_etag.end())
do
{
it->second++;
}
else
{
// First modification
m_etag[path] = 1;
}
map<std::string, uint64_t>::iterator it = m_etag.find(path);
m_last_modified[path] = time(NULL);
if (it != m_etag.end())
{
it->second++;
}
else
{
// First modification
m_etag[path] = 1;
}
m_last_modified[path] = time(NULL);
}
while (drop_path_part(path));
}
time_t last_modified(const string& path) const

View File

@ -18,6 +18,12 @@ describe("HTTP Headers", function() {
.then(function(resp) {
resp.headers.etag.should.be.equal("\"1\"")
})
.then(function() {
return request.get(base_url + "/servers", {resolveWithFullResponse: true})
})
.then(function(resp) {
resp.headers.etag.should.be.equal("\"1\"")
})
});
it("Last-Modified changes after modification", function(done) {
@ -46,6 +52,13 @@ describe("HTTP Headers", function() {
.then(function() {
return request.get(base_url + "/servers/server1", {resolveWithFullResponse: true})
})
.then(function(resp) {
resp.headers["last-modified"].should.not.be.null
resp.headers["last-modified"].should.not.be.equal(date)
})
.then(function() {
return request.get(base_url + "/servers", {resolveWithFullResponse: true})
})
.then(function(resp) {
resp.headers["last-modified"].should.not.be.null
resp.headers["last-modified"].should.not.be.equal(date)