Search packages, repositories and app categories

Package detailsv0.1.5

shelf_cors_headers

CORS headers middleware for Shelf.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
41.7K
Likes
41
Pub points
140

Install package

flutter pub add shelf_cors_headers

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

Shelf CORS Headers is a middleware package for the Shelf framework that simplifies adding Cross-Origin Resource Sharing (CORS) headers to HTTP responses. It enables server-side applications built with Shelf to securely handle cross-origin requests by configuring allowed origins, methods, headers, and credentials. The package integrates seamlessly into existing Shelf-based servers, providing a lightweight and configurable solution for managing CORS policies in web services across multiple platforms.

Use cases

  • Enabling cross-origin requests in Shelf-based APIs
  • Securing web services with controlled access policies
  • Supporting frontend apps hosted on different domains
  • Integrating CORS into Flutter web backends
  • Handling preflight requests automatically

Key features

  • Configurable allowed origins
  • Support for custom headers and methods
  • Automatic handling of OPTIONS preflight requests
  • Easy integration with Shelf routers
  • Flexible configuration for credentials and max age

Suitable for

  • Developers using Shelf for backend logic
  • Flutter developers building web APIs
  • Teams implementing secure RESTful services
  • Applications requiring fine-grained CORS control
  • Server-side Dart projects targeting multiple platforms

Considerations

  • Requires basic understanding of CORS standards
  • Not suitable for standalone client-side use
  • Configuration must be carefully managed in production
  • Does not include request validation beyond CORS
  • Limited to Shelf-based server environments

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

CORS headers middleware for Shelf

A Shelf middleware to add CORS headers to shelf responses.

Features

  • Handles preflight requests.
  • Allows override of default headers.
  • Allows user to provide their own origin checker function.

Usage

A simple usage example:

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

void main() async {
  var handler = const Pipeline()
      // Just add corsHeaders() middleware.
      .addMiddleware(corsHeaders())
      .addHandler(_requestHandler);

  await serve(handler, 'localhost', 8080);
}

Response _requestHandler(Request request) {
  return Response.ok('Request for "${request.url}"');
}
Override Headers
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

void main() async {
  // optionally override default headers
  final overrideHeaders = {
    ACCESS_CONTROL_ALLOW_ORIGIN: 'example.com',
    'Content-Type': 'application/json;charset=utf-8'
  };

  var handler = const Pipeline()
      // Just add corsHeaders() middleware and pass your header overrides.
      .addMiddleware(corsHeaders(headers: overrideHeaders))
      .addHandler(_requestHandler);

  await serve(handler, 'localhost', 8080);
}

Response _requestHandler(Request request) {
  return Response.ok('Request for "${request.url}"');
}
Custom origin checker

Your origin checker function should consume a string as a parameter(the value of ORIGIN header) and return a boolean value indicating if this middleware should process the request from that origin.

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

// define our origin checker
bool dontCheckCNNRequests(String origin) {
  if (origin.contains('cnn.com')) return false;
  return true;
}

void main() async {
  // optionally override default headers
  final overrideHeaders = {
    'Content-Type': 'application/json;charset=utf-8'
  };

  var handler = const Pipeline()
      // Just add corsHeaders() middleware and pass your origin checker
      .addMiddleware(corsHeaders(headers: overrideHeaders, originChecker: dontCheckCNNRequests))
      .addHandler(_requestHandler);

  await serve(handler, 'localhost', 8080);
}

Response _requestHandler(Request request) {
  return Response.ok('Request for "${request.url}"');
}

You can also use the inbuilt originOneOf which takes a list of origins to check:

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';

// define our origin checker
final listChecker = originOneOf(['https://cnn.com', 'https://dart.dev'])

void main() async {
  // optionally override default headers
  final overrideHeaders = {
    'Content-Type': 'application/json;charset=utf-8'
  };

  var handler = const Pipeline()
      // Just add corsHeaders() middleware and pass your origin checker
      .addMiddleware(corsHeaders(headers: overrideHeaders, originChecker: listChecker))
      .addHandler(_requestHandler);

  await serve(handler, 'localhost', 8080);
}

Response _requestHandler(Request request) {
  return Response.ok('Request for "${request.url}"');
}

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package