/*
 * Minimal Object Storage Library, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*jshint sub: true */

function uriEscape(string) {
  var output = string
  // this was originally escape instead of encodeURIComponent but escape is deprecated.
  output = output.replace(/[^A-Za-z0-9_.~\-%]+/g, encodeURIComponent)

  // AWS percent-encodes some extra non-standard characters in a URI
  output = output.replace(/[*]/g, function(ch) {
    return '%' + ch.charCodeAt(0).toString(16).toUpperCase()
  })

  return output
}

function uriResourceEscape(string) {
  var output = string
  // this was originally escape instead of encodeURIComponent but escape is deprecated.
  output = output.replace(/[^A-Za-z0-9_.~\-%]+/g, encodeURIComponent)
  output = output.replace('%2F', '/')

  return output
}

function getRegion(host) {
  switch (host) {
    case 's3.amazonaws.com':
      return 'us-east-1'
    case 's3-ap-northeast-1.amazonaws.com':
      return 'ap-northeast-1'
    case 's3-ap-southeast-1.amazonaws.com':
      return 'ap-southeast-1'
    case 's3-ap-southeast-2.amazonaws.com':
      return 'ap-southeast-2'
    case 's3-eu-central-1.amazonaws.com':
      return 'eu-central-1'
    case 's3-eu-west-1.amazonaws.com':
      return 'eu-west-1'
    case 's3-sa-east-1.amazonaws.com':
      return 'sa-east-1'
    case 's3-external-1.amazonaws.com':
      return 'us-east-1'
    case 's3-us-west-1.amazonaws.com':
      return 'us-west-1'
    case 's3-us-west-2.amazonaws.com':
      return 'us-west-2'
    case 's3.cn-north-1.amazonaws.com.cn':
      return 'cn-north-1'
    case 's3-fips-us-gov-west-1.amazonaws.com':
      return 'us-gov-west-1'
    default:
      return 'milkyway'
  }
}

function validateBucketName(bucket) {
  // test for null
  if(bucket === null) {
    return false
  }

  // bucket length between 3 and 63
  if (bucket.length < 3 || bucket.length > 63) {
    return false
  }

  // lower case, numbers, hyphens
  // starts and ends with letters or numbers
  var re1 = /^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$/
  if(bucket.match(re1) === null) {
    return false
  }

  // no ip address style
  var re2 = /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
  if(bucket.match(re2) !== null) {
    return false
  }

  return true
}

module.exports = {
  uriEscape: uriEscape,
  getRegion: getRegion,
  uriResourceEscape: uriResourceEscape,
  validBucketName: validateBucketName
}

@@ -1360,33 +1360,27 @@
 %7B%0A  
-switch (host)
+var region =
  %7B%0A    
-case 
 's3.
@@ -1390,37 +1390,24 @@
 zonaws.com':
-%0A      return
  'us-east-1'
@@ -1402,34 +1402,30 @@
  'us-east-1'
+,
 %0A    
-case 
 's3-ap-north
@@ -1442,37 +1442,24 @@
 zonaws.com':
-%0A      return
  'ap-northea
@@ -1459,34 +1459,30 @@
 northeast-1'
+,
 %0A    
-case 
 's3-ap-south
@@ -1499,37 +1499,24 @@
 zonaws.com':
-%0A      return
  'ap-southea
@@ -1516,34 +1516,30 @@
 southeast-1'
+,
 %0A    
-case 
 's3-ap-south
@@ -1560,29 +1560,16 @@
 ws.com':
-%0A      return
  'ap-sou
@@ -1577,26 +1577,22 @@
 heast-2'
+,
 %0A    
-case 
 's3-eu-c
@@ -1611,37 +1611,24 @@
 zonaws.com':
-%0A      return
  'eu-central
@@ -1630,26 +1630,22 @@
 ntral-1'
+,
 %0A    
-case 
 's3-eu-w
@@ -1665,29 +1665,16 @@
 ws.com':
-%0A      return
  'eu-wes
@@ -1681,24 +1681,20 @@
 t-1'
+,
 %0A    
-case 
 's3-
-sa
+eu
 -eas
@@ -1716,33 +1716,20 @@
 om':
-%0A      return 'sa
+ 'eu
 -east-1'
 %0A   
@@ -1720,34 +1720,30 @@
  'eu-east-1'
+,
 %0A    
-case 
 's3-external
@@ -1756,37 +1756,24 @@
 zonaws.com':
-%0A      return
  'us-east-1'
@@ -1768,34 +1768,30 @@
  'us-east-1'
+,
 %0A    
-case 
 's3-us-west-
@@ -1803,37 +1803,24 @@
 zonaws.com':
-%0A      return
  'us-west-1'
@@ -1819,26 +1819,22 @@
 -west-1'
+,
 %0A    
-case 
 's3-us-w
@@ -1850,37 +1850,24 @@
 zonaws.com':
-%0A      return
  'us-west-2'
@@ -1866,26 +1866,22 @@
 -west-2'
+,
 %0A    
-case 
 's3.cn-n
@@ -1905,29 +1905,16 @@
 com.cn':
-%0A      return
  'cn-nor
@@ -1922,18 +1922,14 @@
 h-1'
+,
 %0A    
-case 
 's3-
@@ -1962,29 +1962,16 @@
 ws.com':
-%0A      return
  'us-gov
@@ -1985,21 +1985,82 @@
 '%0A  
-  default:%0A  
+%7D%0A%0A  if (region%5Bhost%5D !== undefined) %7B%0A    return region%5Bhost%5D%0A  %7D else %7B%0A
     

Simplify getRegion()