progs/ap3.js
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 26 Oct 2016 01:03:33 +0100
changeset 484 ddcc4ef4f82c
parent 166 bba0504abcf0
child 531 35ffb7a7eafa
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
166
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
function mk_hash(s) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
    return crypto.createHash('sha1').update(s).digest('hex')
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
}
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
function mk_cookie(c) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
    return c.toString() + "-" + mk_hash(c.toString())
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
}
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
function gt_cookie(s) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
    var splits = s.split("-", 2);
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    var counter = parseInt(splits[0])
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    if (mk_hash(counter.toString()) == splits[1]) {
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
        return counter
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    } else { return 0 }
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
}
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
app.get('/', function(req, res){
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
    var counter = gt_cookie(req.cookies.counter) || 0;
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    res.cookie('counter', mk_cookie(counter + 1));
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    ...
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
});
bba0504abcf0 added js snippets
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22