def gen_cors_headers(opts)
      headers = []

      allow_credentials = opts[:access_control_allow_credentials]
      if allow_credentials
        allow_credentials = allow_credentials.to_s
        raise ArgumentError unless allow_credentials == 'true' ||
                                   allow_credentials == 'false'
        headers.push([ 'Access-Control-Allow-Credentials', allow_credentials ])
      end

      allow_headers = opts[:access_control_allow_headers]
      if allow_headers
        raise ArgumentError unless allow_headers =~ CORS_HEADERS_REGEX
        allow_headers = allow_headers.split(', ').map(&:downcase).sort.join(', ')
        headers.push([ 'Access-Control-Allow-Headers', allow_headers ])
      end

      allow_methods = opts[:access_control_allow_methods]
      if allow_methods
        raise ArgumentError unless allow_methods.kind_of? String

        unknown_methods = allow_methods.split(', ').reject do |str|
                            CORS_METHODS.include? str
                          end
        raise ArgumentError unless unknown_methods.size == 0

        headers.push([ 'Access-Control-Allow-Methods', allow_methods ])
      end

      allow_origin = opts[:access_control_allow_origin]
      if allow_origin
        raise ArgumentError unless allow_origin.kind_of? String
        raise ArgumentError unless allow_origin == '*' ||
                                   allow_origin == 'null' ||
                                   allow_origin =~ CORS_ORIGIN_REGEX
        headers.push([ 'Access-Control-Allow-Origin', allow_origin ])
      end

      expose_headers = opts[:access_control_expose_headers]
      if expose_headers
        raise ArgumentError unless expose_headers =~ CORS_HEADERS_REGEX
        expose_headers = expose_headers.split(', ').map(&:downcase).sort.join(', ')
        headers.push([ 'Access-Control-Expose-Headers', expose_headers ])
      end

      max_age = opts[:access_control_max_age]
      if max_age
        raise ArgumentError unless max_age.kind_of?(Integer) && max_age >= 0
        headers.push([ 'Access-Control-Max-Age', max_age.to_s ])
      end

      headers
    end
def gen_cors_headers(opts)
      headers = []

      allow_credentials = opts[:access_control_allow_credentials]
      if allow_credentials
        allow_credentials = allow_credentials.to_s
        raise ArgumentError unless allow_credentials == 'true' ||
                                   allow_credentials == 'false'
        headers.push([ 'Access-Control-Allow-Credentials', allow_credentials ])
      end

      allow_headers = opts[:access_control_allow_headers]
      if allow_headers
        raise ArgumentError unless allow_headers =~ CORS_HEADERS_REGEX
        allow_headers = allow_headers.split(', ').map(&:downcase).sort.join(', ')
        headers.push([ 'Access-Control-Allow-Headers', allow_headers ])
      end

      allow_methods = opts[:access_control_allow_methods]
      if allow_methods
        raise ArgumentError unless allow_methods.kind_of? String

        unknown_methods = allow_methods.split(', ').reject do |str|
                            CORS_METHODS.include? str
                          end
        raise ArgumentError unless unknown_methods.size == 0

        headers.push([ 'Access-Control-Allow-Methods', allow_methods ])
      end

      allow_origin = opts[:access_control_allow_origin]
      if allow_origin
        raise ArgumentError unless allow_origin.kind_of? String
        raise ArgumentError unless allow_origin == '*' ||
                                   allow_origin == 'null' ||
                                   allow_origin =~ CORS_ORIGIN_REGEX
        headers.push([ 'Access-Control-Allow-Origin', allow_origin ])
      end

      expose_headers = opts[:access_control_expose_headers]
      if expose_headers
        raise ArgumentError unless expose_headers =~ CORS_HEADERS_REGEX
        expose_headers = expose_headers.split(', ').map(&:downcase).sort.join(', ')
        headers.push([ 'Access-Control-Expose-Headers', expose_headers ])
      end

      max_age = opts[:access_control_max_age]
      if max_age
        raise ArgumentError unless max_age.kind_of?(Integer) && max_age >= 0
        headers.push([ 'Access-Control-Max-Age', max_age.to_s ])
      end

      headers
    end
Do some sanity checks and create CORS-related headers

 For more details, see http://www.w3.org/TR/cors/ and
 https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Access-Control-Expose-Headers