crossOriginResourcePolicy function

Middleware crossOriginResourcePolicy({
  1. CrossOriginResourcePolicyOptions policy = CrossOriginResourcePolicyOptions.sameOrigin,
})

This middleware sets the Cross-Origin-Resource-Policy header. Read about it in the spec.

Usage:

import 'package:shelf_helmet/shelf_helmet.dart'

// Sets Cross-Origin-Resource-Policy: same-origin
.addMiddleware(crossOriginResourcePolicy());

// Sets "Cross-Origin-Resource-Policy: cross-origin"
.addMiddleware(crossOriginResourcePolicy(
  policy: CrossOriginResourcePolicyOptions.crossOrigin
));

// Sets "Cross-Origin-Resource-Policy: same-site"
.addMiddleware(crossOriginResourcePolicy(
  policy: CrossOriginResourcePolicyOptions.sameSite
));

Implementation

Middleware crossOriginResourcePolicy({
  CrossOriginResourcePolicyOptions policy =
      CrossOriginResourcePolicyOptions.sameOrigin,
}) {
  return (innerHandler) {
    return (request) async {
      final response = await innerHandler(request);
      return response.change(
        headers: {
          'cross-origin-resource-policy': policy.option,
          ...response.headersAll,
        },
      );
    };
  };
}