<commit_before>var utils = {};

var awsRegions = [
  {
    "endpoint": "s3.amazonaws.com",
    "region": "us-east-1"
  },
  {
    "endpoint": "s3-external-1.amazonaws.com",
    "region": "us-east-1"
  },
  {
    "endpoint": "s3.eu-west-1.amazonaws.com",
    "region": "eu-west-1"
  },
  {
    "endpoint": "s3.ap-southeast-1.amazonaws.com",
    "region": "ap-southeast-1"
  },
  {
    "endpoint": "s3.ap-southeast-2.amazonaws.com",
    "region": "ap-southeast-2"
  },
  {
    "endpoint": "s3.eu-central-1.amazonaws.com",
    "region": "eu-central-1"
  },
  {
    "endpoint": "s3.eu.central-1.amazonaws.com",
    "region": "eu-central-1"
  },
  {
    "endpoint": "s3.ap-northeast-1.amazonaws.com",
    "region": "ap-northeast-1"
  },
  {
    "endpoint": "s3.us-east-1.amazonaws.com",
    "region": "us-east-1"
  },
  {
    "endpoint": "s3.sa-east-1.amazonaws.com",
    "region": "sa-east-1"
  },
  {
    "endpoint": "s3.us-west-1.amazonaws.com",
    "region": "us-west-1"
  },
  {
    "endpoint": "ec2.us-west-2.amazonaws.com",
    "region": "us-west-2"
  },
  {
    "endpoint": "s3-us-gov-west-1.amazonaws.com",
    "region": "us-gov-west-1"
  },
  {
    "endpoint": "s3-fips-us-gov-west-1.amazonaws.com",
    "region": "us-gov-west-1"
  }
];

/**
* Get hostname from the request
* @param {object} request - http request object
*/
utils.getBucketName = function(request) {
  return this.getBucketNameFromHost(request) || this.getBucketNameFromPath(request);
}

/**
* Get hostname from the request of a virtually hosted bucket
* @param {object} request - http request object
*/
utils.getBucketNameFromHost = function(request) {
  var host, hostname, endpoint, region, i, ii, hostArr;

  host = request.headers.host.split(':')[0];
  for(i = 0, ii = awsRegions.length; i < ii; i++) {
    endpoint = awsRegions[i].endpoint;
    websiteEndpoint = 's3-website-' + awsRegions[i].region + '.amazonaws.com';

    if(host !== endpoint && host.indexOf(endpoint) !== -1) {
      hostArr = host.split('.' + endpoint);
      hostname = hostArr[0];
      return hostname;
    }

    if(host !== endpoint && host.indexOf(websiteEndpoint) !== -1) {
      hostArr = host.split('.' + websiteEndpoint);
      hostname = hostArr[0];
      return hostname;
    }
  }

  return hostname;
}

/**
* Get hostname from the path of a request
* @param {object} request - http request object
*/
utils.getBucketNameFromPath = function(request) {
  var hostname, urlArr;

  urlArr = request.url.split('/');
  if(urlArr[1] !== undefined) hostname = urlArr[1];

  return hostname;
}

module.exports = utils;<commit_msg>bucket name validation<commit_after>var utils = {};

var awsRegions = [
  {
    "endpoint": "s3.amazonaws.com",
    "region": "us-east-1"
  },
  {
    "endpoint": "s3-external-1.amazonaws.com",
    "region": "us-east-1"
  },
  {
    "endpoint": "s3.eu-west-1.amazonaws.com",
    "region": "eu-west-1"
  },
  {
    "endpoint": "s3.ap-southeast-1.amazonaws.com",
    "region": "ap-southeast-1"
  },
  {
    "endpoint": "s3.ap-southeast-2.amazonaws.com",
    "region": "ap-southeast-2"
  },
  {
    "endpoint": "s3.eu-central-1.amazonaws.com",
    "region": "eu-central-1"
  },
  {
    "endpoint": "s3.eu.central-1.amazonaws.com",
    "region": "eu-central-1"
  },
  {
    "endpoint": "s3.ap-northeast-1.amazonaws.com",
    "region": "ap-northeast-1"
  },
  {
    "endpoint": "s3.us-east-1.amazonaws.com",
    "region": "us-east-1"
  },
  {
    "endpoint": "s3.sa-east-1.amazonaws.com",
    "region": "sa-east-1"
  },
  {
    "endpoint": "s3.us-west-1.amazonaws.com",
    "region": "us-west-1"
  },
  {
    "endpoint": "ec2.us-west-2.amazonaws.com",
    "region": "us-west-2"
  },
  {
    "endpoint": "s3-us-gov-west-1.amazonaws.com",
    "region": "us-gov-west-1"
  },
  {
    "endpoint": "s3-fips-us-gov-west-1.amazonaws.com",
    "region": "us-gov-west-1"
  }
];

/**
* Get hostname from the request
* @param {object} request - http request object
*/
utils.getBucketName = function(request) {
  return this.getBucketNameFromHost(request) || this.getBucketNameFromPath(request);
}

/**
* Get hostname from the request of a virtually hosted bucket
* @param {object} request - http request object
*/
utils.getBucketNameFromHost = function(request) {
  var host, hostname, endpoint, region, i, ii, hostArr;

  host = request.headers.host.split(':')[0];
  for(i = 0, ii = awsRegions.length; i < ii; i++) {
    endpoint = awsRegions[i].endpoint;
    websiteEndpoint = 's3-website-' + awsRegions[i].region + '.amazonaws.com';

    if(host !== endpoint && host.indexOf(endpoint) !== -1) {
      hostArr = host.split('.' + endpoint);
      hostname = hostArr[0];
      return hostname;
    }

    if(host !== endpoint && host.indexOf(websiteEndpoint) !== -1) {
      hostArr = host.split('.' + websiteEndpoint);
      hostname = hostArr[0];
      return hostname;
    }
  }

  return hostname;
}

/**
* Get hostname from the path of a request
* @param {object} request - http request object
*/
utils.getBucketNameFromPath = function(request) {
  var hostname, urlArr;

  urlArr = request.url.split('/');
  if(urlArr[1] !== undefined) {
    hostname = urlArr[1];
  }

  return hostname;
}

/**
* Validate bucket name per naming rules and restrictions
* @param {string} bucketname - name of the bucket to be created
*/
utils.isValidBucketName = function(bucketname) {
  var ipAddressRegex, dnsCompatibleRegex;
  // Must be at least 3 and no more than 63 characters long.
  if(bucketname.length < 3 || bucketname.length > 63) {
    return false;
  }

  // Must not an ip address
  ipAddressRegex = new RegExp(/(\d+\.){3}\d+/);
  if(bucketname.match(ipAddressRegex)) {
    return false;
  }

  // Must be dns compatible
  dnsCompatibleRegex = new RegExp(/^[a-z0-9]+([\.\-]{1}[a-z0-9]+)*$/);
  if(bucketname.match(dnsCompatibleRegex) === null) {
    return false;
  }

  return true;
}

module.exports = utils;
{'fraction_non_alphanumeric': 0.1891156462585034, 'fraction_numerical': 0.01904761904761905, 'mean_word_length': 2.9898236092265944, 'pattern_counts': {'":': 56, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6905723', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2217, 'n_words': 406}