Skip to content

Commit

Permalink
Update Meta cdn redirect function with latest code (#5206)
Browse files Browse the repository at this point in the history
This incorporates :
https://github.com/pytorch/test-infra/blob/main/aws/websites/download.pytorch.org/pep503_whl_redirect.js
Also adds Country based redirect, currently set to 'CA' for testing
purposes
  • Loading branch information
atalman authored May 15, 2024
1 parent b925ae6 commit 2698cb3
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions aws/websites/download.pytorch.org/meta_cdn_whl_redirect.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,66 @@
function handler(event) {
var request = event.request;
const headers = request.headers;
var uri = request.uri;
var uri_parts = uri.split('/');
var last_uri_part = uri_parts[uri_parts.length -1];
var rocm_pattern = /^rocm[0-9]+\.[0-9]+$/
var CDN_TEST_PATH = '/whl/cdntest/';

// if we are requesting for whl files in cdntest path - perform the translation to meta CDN
// if we are requesting for whl files in cdntest path:
// - redirect to download.pytorch.org in case of CN
// - default perform the translation to meta CDN
if( uri.startsWith(CDN_TEST_PATH) && uri.endsWith('.whl') ) {
var uri_suffix = uri.slice(CDN_TEST_PATH.length).split('/').join('%2F');
var meta_cdn_path = 'https://scontent.xx.fbcdn.net/mci_ab/uap/?ab_b=m&ab_page=PyTorchBinary&ab_entry=tree%2Fwhl%2Ftest%2F'
var uri_suffix = uri.slice(CDN_TEST_PATH.length);
var redirect_value = '';

if (headers['cloudfront-viewer-country']) {
const countryCode = Symbol.for(headers['cloudfront-viewer-country'].value);
if (countryCode === Symbol.for('CA')) {
redirect_value = 'https://download.pytorch.org/whl/test/'+uri_suffix;
}
}
if(redirect_value == '') {
var meta_cdn_path = 'https://scontent.xx.fbcdn.net/mci_ab/uap/?ab_b=m&ab_page=PyTorchBinary&ab_entry=tree%2Fwhl%2Ftest%2F'
redirect_value = meta_cdn_path+uri_suffix.split('/').join('%2F');
}

const response = {
statusCode: 302,
statusDescription: 'Found',
headers:
{ "location": { "value": meta_cdn_path+uri_suffix } }
{ "location": { "value": redirect_value } }
}

return response;
}

if (uri.startsWith('/whl')) {
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
// Check whether the URI is folder like
// I.e. does not have dots in path or rocm\d.\d or 1.8 (for lts)
// For example /whl/cpu/torch
} else if (last_uri_part.indexOf('.') == -1
|| last_uri_part.match(rocm_pattern)
|| uri == "/whl/lts/1.8") {
request.uri += '/index.html';
}
}

// Similar behavior for libtorch
if (uri.startsWith('/libtorch')) {
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
// Check whether the URI is folder like
// I.e. does not have dots in path
// For example /libtorch/cpu
} else if (last_uri_part.indexOf('.') == -1) {
request.uri += '/index.html';
}
}

return request;
}

0 comments on commit 2698cb3

Please sign in to comment.