text: // ----------------------------------------------------------------------------
  // Copyright (c) Microsoft Corporation. All rights reserved.
  // ----------------------------------------------------------------------------
  /**
  @module azure-mobile-apps/src/express/middleware/swagger-ui
  @description Creates middleware that renders swagger UI. Intended to be consumed
  by the {@link module:azure-mobile-apps/src/express/middleware/swagger swagger middleware}.
  */
  var express = require('express'),
      ua = require('ua-parser'),
      path = require('path');
  
  /**
  Create a new instance of the swagger-ui middleware
  @param {configuration} configuration The mobile app configuration
  */
  module.exports = function(configuration) {
      var swaggerPath = resolveSwaggerUi(),
          middleware = swaggerPath && express.static(swaggerPath);
  
      return function (req, res, next) {
          if(swaggerPath && configuration.swagger) {
              if(ua.parse(req.get('user-agent')).family === 'IE')
                  res.send('Internet Explorer is not supported for viewing swagger-ui.');
  
              else if(req.query.url !== swaggerUrl()) {
                  res.redirect('?url=' + swaggerUrl());
  
              } else {
                  res.set('Content-Security-Policy', "connect-src 'self' online.swagger.io");
                  middleware(req, res, next);
              }
          } else {
              res.status(404).send("To access the swagger UI, you must enable swagger support by adding swagger: true to your configuration and installing the swagger-ui npm module")
          }
  
          function swaggerUrl() {
              // this is a bit of a hack and assumes swagger metadata is exposed in the parent directory
              var swaggerPath = path.join(req.baseUrl, '..').replace(/\\/g, '/'),
                  // requests are always http from the Azure Web Apps front end to the worker - detect using headers that ARR attaches
                  protocol = (req.headers['x-forwarded-proto'] === 'https' || req.headers['x-arr-ssl']) ? 'https' : 'http';
              return protocol + '://' + req.get('host') + swaggerPath;
          }
      };
  
      function resolveSwaggerUi() {
          try {
              // enforce that the swagger-ui package is installed
              // when the full bundle is restored to the package, we'll revert to using that
              require.resolve('swagger-ui');
  
              // just return the embedded swagger ui for now
              return path.join(__dirname, '..', '..', 'swagger', 'ui');
          } catch(error) { }
      }
  };
  