/**
 * CORS middleware
 *
 * @param {Object} [settings]
 * @return {Function}
 * @api public
 */
module.exports = function(settings) {
  
  var defaults = {
    origin: '*',
    methods: 'GET,HEAD,PUT,POST,DELETE'
  };

  settings = settings || defaults;

  return function* cors(next) {
    
    yield next;
    
    var options = settings;
    
    if (typeof options === 'function') {
      options = options(this.request);
    }
  
    if (typeof options.origin === 'function') {
      options.origin = options.origin(this.request);
    }
  
    /**
     * Access Control Allow Origin
     */
    if (options.origin === false) {
      return;
    } else if (options.origin === true) {
      options.origin = this.header.origin;
    } else if (!options.origin) {
      options.origin = '*';
    }
    this.set('Access-Control-Allow-Origin', options.origin);
    
    /**
     * Access Control Allow Methods
     */
    if (options.methods.join) {
      options.methods = options.methods.join(',');
    }
    this.set('Access-Control-Allow-Methods', options.methods);
    
    /**
     * Access Control Allow Credentials
     */
    if (options.credentials === true) {
      this.set('Access-Control-Allow-Credentials', 'true');
    }
    
    /**
     * Access Control Allow Headers
     */
    if (!options.headers) {
      options.headers = this.header['access-control-request-headers'];
    } else if (options.headers.join) {
      options.headers = options.headers.join(',');
    }
    if (options.headers && options.headers.length) {
      this.set('Access-Control-Allow-Headers', options.headers);
    }
    
    /**
     * Access Control Allow Max Age
     */
    options.maxAge = options.maxAge && options.maxAge.toString();
    if (options.maxAge && options.maxAge.length) {
      this.set('Access-Control-Allow-Max-Age', options.maxAge);
    }
    
    /**
     * Returns
     */
    if (this.method === 'OPTIONS') {
      this.status = 204;
    }
    
  }
  
};

@@ -128,19 +128,17 @@
 ings) %7B%0A
-  
 %0A
+
   var de
@@ -279,21 +279,17 @@
 next) %7B%0A
-    
 %0A
+
     yiel
@@ -296,25 +296,21 @@
 d next;%0A
+%0A
     
-%0A    
 var opti
@@ -314,16 +314,188 @@
 ptions =
+ %7B%0A      origin:      settings.origin,%0A      methods:     settings.methods,%0A      credentials: settings.credentials,%0A      headers:     settings.headers,%0A      maxAge:     
  setting
@@ -495,22 +495,31 @@
 settings
-;
+.maxAge
 %0A    
+%7D;%0A
 %0A    if 
@@ -593,27 +593,25 @@
 est);%0A    %7D%0A
-  
 %0A
+
     if (type
@@ -705,19 +705,17 @@
 ;%0A    %7D%0A
-  
 %0A
+
     /**%0A
@@ -890,16 +890,23 @@
 r.origin
+ %7C%7C '*'
 ;%0A    %7D 
@@ -1028,21 +1028,17 @@
 rigin);%0A
-    
 %0A
+
     /**%0A
@@ -1233,21 +1233,17 @@
 thods);%0A
-    
 %0A
+
     /**%0A
@@ -1388,37 +1388,33 @@
  'true');%0A    %7D%0A
-    
 %0A
+
     /**%0A     * A
@@ -1760,37 +1760,33 @@
 headers);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * A
@@ -2008,21 +2008,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     /**%0A
@@ -2112,19 +2112,13 @@
   %7D%0A
-    
 %0A
+
   %7D%0A
-  
 %0A%7D;%0A

Fix origin mirroring persisting from initial request