Skip to content

Instantly share code, notes, and snippets.

@nicusX
Last active February 19, 2018 16:12

Revisions

  1. nicusX revised this gist Feb 19, 2018. 1 changed file with 0 additions and 58 deletions.
    58 changes: 0 additions & 58 deletions originRequest.js
    Original file line number Diff line number Diff line change
    @@ -1,58 +0,0 @@
    'use strict';

    const sourceCoookie = 'X-Source';
    const sourceMain = 'main';
    const sourceExperiment = 'experiment';
    const experimentBucketName = 'my-experiment.s3.amazonaws.com';
    const experimentBucketRegion = 'eu-west-1';

    // Origin Request handler
    exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    const source = decideSource(headers);

    // If Source is Experiment, change Origin and Host header
    if ( source === sourceExperiment ) {
    console.log('Setting Origin to experiment bucket');
    // Specify Origin
    request.origin = {
    s3: {
    authMethod: 'origin-access-identity',
    domainName: experimentBucketName,
    path: '',
    region: experimentBucketRegion
    }
    };

    // Also set Host header to prevent “The request signature we calculated does not match the signature you provided” error
    headers['host'] = [{key: 'host', value: experimentBucketName }];
    }
    // No need to change anything if Source was Main or undefined

    callback(null, request);
    };


    // Decide source based on source cookie.
    const decideSource = function(headers) {
    const sourceMainCookie = `${sourceCoookie}=${sourceMain}`;
    const sourceExperimenCookie = `${sourceCoookie}=${sourceExperiment}`;

    // Remember a single cookie header entry may contains multiple cookies
    if (headers.cookie) {
    // ...ugly but simple enough for now
    for (let i = 0; i < headers.cookie.length; i++) {
    if (headers.cookie[i].value.indexOf(sourceExperimenCookie) >= 0) {
    console.log('Experiment Source cookie found');
    return sourceExperiment;
    }
    if (headers.cookie[i].value.indexOf(sourceMainCookie) >= 0) {
    console.log('Main Source cookie found');
    return sourceMain;
    }
    }
    }
    console.log('No Source cookie found (Origin undecided)');
    }
  2. nicusX revised this gist Feb 19, 2018. 1 changed file with 58 additions and 0 deletions.
    58 changes: 58 additions & 0 deletions originRequest.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    'use strict';

    const sourceCoookie = 'X-Source';
    const sourceMain = 'main';
    const sourceExperiment = 'experiment';
    const experimentBucketName = 'my-experiment.s3.amazonaws.com';
    const experimentBucketRegion = 'eu-west-1';

    // Origin Request handler
    exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    const source = decideSource(headers);

    // If Source is Experiment, change Origin and Host header
    if ( source === sourceExperiment ) {
    console.log('Setting Origin to experiment bucket');
    // Specify Origin
    request.origin = {
    s3: {
    authMethod: 'origin-access-identity',
    domainName: experimentBucketName,
    path: '',
    region: experimentBucketRegion
    }
    };

    // Also set Host header to prevent “The request signature we calculated does not match the signature you provided” error
    headers['host'] = [{key: 'host', value: experimentBucketName }];
    }
    // No need to change anything if Source was Main or undefined

    callback(null, request);
    };


    // Decide source based on source cookie.
    const decideSource = function(headers) {
    const sourceMainCookie = `${sourceCoookie}=${sourceMain}`;
    const sourceExperimenCookie = `${sourceCoookie}=${sourceExperiment}`;

    // Remember a single cookie header entry may contains multiple cookies
    if (headers.cookie) {
    // ...ugly but simple enough for now
    for (let i = 0; i < headers.cookie.length; i++) {
    if (headers.cookie[i].value.indexOf(sourceExperimenCookie) >= 0) {
    console.log('Experiment Source cookie found');
    return sourceExperiment;
    }
    if (headers.cookie[i].value.indexOf(sourceMainCookie) >= 0) {
    console.log('Main Source cookie found');
    return sourceMain;
    }
    }
    }
    console.log('No Source cookie found (Origin undecided)');
    }
  3. nicusX revised this gist Feb 19, 2018. 1 changed file with 23 additions and 44 deletions.
    67 changes: 23 additions & 44 deletions viewerRequest.js
    Original file line number Diff line number Diff line change
    @@ -3,56 +3,35 @@
    const sourceCoookie = 'X-Source';
    const sourceMain = 'main';
    const sourceExperiment = 'experiment';
    const experimentBucketName = 'my-experiment.s3.amazonaws.com';
    const experimentBucketRegion = 'eu-west-1';
    const experimentTraffic = 0.5;

    // Viewer request handler
    exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    const source = decideSource(headers);

    // If Source is Experiment, change Origin and Host header
    if ( source === sourceExperiment ) {
    console.log('Setting Origin to experiment bucket');
    // Specify Origin
    request.origin = {
    s3: {
    authMethod: 'origin-access-identity',
    domainName: experimentBucketName,
    path: '',
    region: experimentBucketRegion
    }
    };

    // Also set Host header to prevent “The request signature we calculated does not match the signature you provided” error
    headers['host'] = [{key: 'host', value: experimentBucketName }];
    // Look for source cookie
    if ( headers.cookie ) {
    for (let i = 0; i < headers.cookie.length; i++) {
    if (headers.cookie[i].value.indexOf(sourceCoookie) >= 0) {
    console.log('Source cookie found. Forwarding request as-is');
    // Forward request as-is
    callback(null, request);
    return;
    }
    }
    }
    // No need to change anything if Source was Main or undefined

    callback(null, request);
    };

    console.log('Source cookie has not been found. Throwing dice...');
    const source = ( Math.random() < experimentTraffic ) ? sourceExperiment : sourceMain;
    console.log(`Source: ${source}`)

    // Decide source based on source cookie.
    const decideSource = function(headers) {
    const sourceMainCookie = `${sourceCoookie}=${sourceMain}`;
    const sourceExperimenCookie = `${sourceCoookie}=${sourceExperiment}`;

    // Remember a single cookie header entry may contains multiple cookies
    if (headers.cookie) {
    // Add Source cookie
    const cookie = `${sourceCoookie}=${source}`
    console.log(`Adding cookie header: ${cookie}`);
    headers.cookie = headers.cookie || [];
    headers.cookie.push({ key:'Cookie', value: cookie });

    // ...ugly but simple enough for now
    for (let i = 0; i < headers.cookie.length; i++) {
    if (headers.cookie[i].value.indexOf(sourceExperimenCookie) >= 0) {
    console.log('Experiment Source cookie found');
    return sourceExperiment;
    }
    if (headers.cookie[i].value.indexOf(sourceMainCookie) >= 0) {
    console.log('Main Source cookie found');
    return sourceMain;
    }
    }
    }
    console.log('No Source cookie found (Origin undecided)');
    }
    // Forwarding request
    callback(null, request);
    };
  4. nicusX renamed this gist Feb 19, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. nicusX created this gist Feb 19, 2018.
    58 changes: 58 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    'use strict';

    const sourceCoookie = 'X-Source';
    const sourceMain = 'main';
    const sourceExperiment = 'experiment';
    const experimentBucketName = 'my-experiment.s3.amazonaws.com';
    const experimentBucketRegion = 'eu-west-1';

    exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    const source = decideSource(headers);

    // If Source is Experiment, change Origin and Host header
    if ( source === sourceExperiment ) {
    console.log('Setting Origin to experiment bucket');
    // Specify Origin
    request.origin = {
    s3: {
    authMethod: 'origin-access-identity',
    domainName: experimentBucketName,
    path: '',
    region: experimentBucketRegion
    }
    };

    // Also set Host header to prevent “The request signature we calculated does not match the signature you provided” error
    headers['host'] = [{key: 'host', value: experimentBucketName }];
    }
    // No need to change anything if Source was Main or undefined

    callback(null, request);
    };


    // Decide source based on source cookie.
    const decideSource = function(headers) {
    const sourceMainCookie = `${sourceCoookie}=${sourceMain}`;
    const sourceExperimenCookie = `${sourceCoookie}=${sourceExperiment}`;

    // Remember a single cookie header entry may contains multiple cookies
    if (headers.cookie) {

    // ...ugly but simple enough for now
    for (let i = 0; i < headers.cookie.length; i++) {
    if (headers.cookie[i].value.indexOf(sourceExperimenCookie) >= 0) {
    console.log('Experiment Source cookie found');
    return sourceExperiment;
    }
    if (headers.cookie[i].value.indexOf(sourceMainCookie) >= 0) {
    console.log('Main Source cookie found');
    return sourceMain;
    }
    }
    }
    console.log('No Source cookie found (Origin undecided)');
    }