Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PUTing documents with If-None-Match when non-existent #104

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 9 additions & 3 deletions lib/controllers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class Storage extends Controller {

async get (head = false) {
const version = this.getVersion();
const matchVersion = !!this.request.headers['if-match'];

if (await this.checkToken('r')) {
let numBytesWritten = 0;
try {
var { item, versionMatch } = await this.server._store.get(this._username, this._path, version, head);
var { item, versionMatch } = await this.server._store.get(this._username, this._path, version, matchVersion, head);
} catch (e) {
getLogger().error(`Your storage backend does not behave correctly => ${e.message}`);
this.response.writeHead(500, this._headers);
Expand All @@ -59,7 +61,7 @@ class Storage extends Controller {
}

this.setVersion(item && item.ETag);
if (versionMatch) {
if (!matchVersion && versionMatch) {
delete this._headers['Content-Type'];
this.response.writeHead(304, this._headers);
this.response.end();
Expand All @@ -86,10 +88,14 @@ class Storage extends Controller {
return false;
}
const version = this.getVersion();
const matchVersion = !!this.request.headers['if-match'];

let status, error, created, modified, conflict, isDir;
if (await this.checkToken('w')) {
try {
({ created, modified, conflict, isDir } = await this.server._store.put(this._username, this._path, type, value, version));
({ created, modified, conflict, isDir } = await this.server._store.put(
this._username, this._path, type, value, version, matchVersion
));
status = conflict
? 412
: isDir
Expand Down
10 changes: 8 additions & 2 deletions lib/stores/file_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ class FileTree {
}
}

async put (username, pathname, type, value, version) {
async put (username, pathname, type, value, version, matchVersion) {
const datapath = this.dataPath(username, pathname);
const metapath = this._metaPath(username, pathname);
const basename = decodeURI(path.basename(pathname));
await this._lock(username);
const metadata = await this.readMeta(username, pathname);
let created = false;

if (version) {
if (version && matchVersion) {
if (version === '*'
// check document existence when version '*' specified
? metadata.items && metadata.items[basename]
Expand All @@ -217,6 +217,12 @@ class FileTree {
await this._unlock(username);
return { conflict: true, created };
}
} else if (version && !matchVersion) {
if (metadata.items && metadata.items[basename] &&
version.replace(/"/g, '') === metadata.items[basename].ETag) {
await this._unlock(username);
return { conflict: true, created };
}
}

if (metadata.items[`${basename}/`]) {
Expand Down
48 changes: 48 additions & 0 deletions spec/store_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,54 @@ sharedExamplesFor('Stores', (store) => {
expect(!conflict).to.be.true;
});

it("doesn't set the value of an existing item, when If-Match [ETag] doesn't match", async () => {
// client 1
const client1put1 = await store.put('boris', '/if-match/no-match', 'text/markdown', Buffer.from('Original paragraph'), null);
expect(client1put1.conflict).to.be.false;
expect(client1put1.created).to.be.true;

// both clients
const { item: original } = await store.get('boris', '/if-match/no-match', null);
expect(original.ETag).to.exist;

// client 2
const client2put = await store.put('boris', '/if-match/no-match', 'text/markdown', Buffer.from('Changed paragraph'), original.ETag);
expect(client2put.conflict).to.be.false;
expect(client2put.created).to.be.false;
const { item: change1 } = await store.get('boris', '/if-match/no-match', null);
expect(change1.value.toString('utf8')).to.be.deep.equal('Changed paragraph');
expect(change1.ETag).to.not.equal(original.ETag);

// client 1
const client1put2 = await store.put('boris', '/if-match/no-match', 'text/markdown', Buffer.from('Another change'), original.ETag);
expect(client1put2.conflict).to.be.true;
expect(client1put2.created).to.be.false;

const { item: final } = await store.get('boris', '/if-match/no-match', null);
expect(final.value.toString('utf8')).to.be.deep.equal('Changed paragraph');
expect(final.ETag).to.equal(change1.ETag);
});

it("doesn't set the value of an existing item, when If-None-Match * is used", async () => {
const response1 = await store.put('boris', '/if-none-match/star', 'text/markdown', Buffer.from('# Some Title'), null);
expect(response1.conflict).to.be.false;
expect(response1.created).to.be.true;
const response2 = await store.put('boris', '/if-none-match/star', 'text/markdown', Buffer.from('# Another Title'), '*');
expect(response2.conflict).to.be.true;
expect(response2.created).to.be.false;
const { item } = await store.get('boris', '/if-none-match/star', null);
expect(item.value).to.be.deep.equal(Buffer.from('# Some Title'));
});

it("doesn't set the value of an existing item, when If-None-Match [ETag] matches", async () => {
await store.put('boris', '/if-none-match/etag', 'text/markdown', Buffer.from('* Original Item'), null);
const { item: original } = await store.get('boris', '/if-none-match/etag', null);
expect(original.ETag).to.exist;
await store.put('boris', '/if-none-match/etag', 'text/markdown', Buffer.from('* Changed Item'), original.ETag);
const { item: final } = await store.get('boris', '/if-none-match/etag', null);
expect(final.value.toString('utf8')).to.be.deep.equal('* Original Item');
});

describe('for a nested document', () => {
it('created the parent directory', async () => {
await store.put('boris', '/photos/foo/bar/qux', 'image/poster', Buffer.from('vertibo'), null);
Expand Down