Merge pull request #47 from srinandan/master

support for cloud foundry
diff --git a/cloud-foundry-route-service/index.js b/cloud-foundry-route-service/index.js
new file mode 100644
index 0000000..991f172
--- /dev/null
+++ b/cloud-foundry-route-service/index.js
@@ -0,0 +1,64 @@
+'use strict'
+/*
+ * Copyright 2016 Apigee Corporation
+ *
+ * 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.
+ */
+
+
+/**
+ * Changes target host/path according to Cloud Foundry "magic header".
+ * 
+ * @module
+ */
+
+function retarget (req, res, next) {
+  const cfurl = req.headers['x-cf-forwarded-url']
+  if (! cfurl) {
+    next()
+    return
+  }
+
+  let h = cfurl.indexOf('://')
+  if (h >= 0) {
+    h += 3
+  }
+  else {
+    h = 0
+  }
+
+  let p = cfurl.indexOf('/', h)
+  if (p < 0) {
+    p = cfurl.length
+  }
+
+  const cfHostname = cfurl.slice(h, p)
+  const cfPath = cfurl.slice(p) || '/'
+
+  // console.log('x-cf-forwarded-url', cfurl, h, p)
+  // console.log(req.targetHostname, '->', cfHostname)
+  // console.log(req.targetPath, '->', cfPath)
+
+  if (cfHostname) {
+    req.targetHostname = cfHostname
+  }
+  req.targetPath = cfPath
+
+  next()
+}
+
+module.exports.init = function (config, logger, stats) {
+  return {
+    onrequest: retarget
+  }
+}
diff --git a/cloud-foundry-route-service/package.json b/cloud-foundry-route-service/package.json
new file mode 100644
index 0000000..58a30b0
--- /dev/null
+++ b/cloud-foundry-route-service/package.json
@@ -0,0 +1,11 @@
+{
+  "name": "cloud-foundry-route-service",
+  "version": "1.0.0",
+  "description": "Handles 'x-cf-forwarded-url' header for Cloud Foundry route-service",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "Apache-2.0"
+}
diff --git a/index.js b/index.js
index 7ac17a8..c7eba3d 100644
--- a/index.js
+++ b/index.js
@@ -13,5 +13,6 @@
   accesscontrol:require('./accesscontrol'),
   eurekaclient:require('./eurekaclient'),
   json2xml: require('./json2xml'),
-  healthcheck: require('./healthcheck')
+  healthcheck: require('./healthcheck'),
+  'cloud-foundry-route-service': require('./cloud-foundry-route-service')
 }