• Stars
    star
    177
  • Rank 215,985 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross Origin Request Sharing extension for Sinatra

About

A simple Sinatra extension to enable Cross Domain Resource Sharing (CORS) To see more on cross domain resource sharing, see https://developer.mozilla.org/En/HTTP_access_control

Installation with Bundler

gem "sinatra-cross_origin", "~> 0.3.1"

Examples

To enable cross origin requests for all routes:

require 'sinatra'
require 'sinatra/cross_origin'

configure do
  enable :cross_origin
end

To only enable cross origin requests for certain routes:

get '/cross_origin' do
  cross_origin
  "This is available to cross-origin javascripts"
end

If you're using a modular application, remember to register this extension:

register Sinatra::CrossOrigin

Global Configuration

You can set global options via the normal Sinatra set method:

set :allow_origin, :any
set :allow_methods, [:get, :post, :options]
set :allow_credentials, true
set :max_age, "1728000"
set :expose_headers, ['Content-Type']

You can change configuration options on the fly within routes with:

get '/custom' do
  cross_origin :allow_origin => 'http://example.com',
    :allow_methods => [:get],
    :allow_credentials => false,
    :max_age => "60"

  "My custom cross origin response"
end

Responding to OPTIONS

Many browsers send an OPTIONS request to a server before performing a CORS request (this is part of the specification for CORS ). These sorts of requests are called preflight requests. Without a valid response to an OPTIONS request, a browser may refuse to make a CORS request (and complain that the CORS request violates the same-origin policy).

Currently, this gem does not properly respond to OPTIONS requests. See this issue. You may have to add code like this in order to make your app properly respond to OPTIONS requests:

options "*" do
  response.headers["Allow"] = "HEAD,GET,PUT,POST,DELETE,OPTIONS"
 
  response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept"
 
  200
end

License

Copyright (c) 2007, 2008, 2009 Brit Gardner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

MultistrokeGestureRecognizer-iOS

A library for defining and detecting multi-stroke gestures created through touch-input on iOS devices
Objective-C
273
star
2

New-Relic-Menus

Objective-C
13
star
3

use-animated-sprite

Animated sprite hook for react-three-fiber
TypeScript
12
star
4

sammy-google-analytics

A Sammy.js plugin for tracking route changes in Google Analytics
JavaScript
12
star
5

macvim-mirror

Mirror of the MacVim repo.or.cz repo updated hourly
C
7
star
6

node-simple-lobby

A simple game lobby for a blog post on http://britg.com
7
star
7

scriptstack

Organize and share javascripts
JavaScript
7
star
8

DunGen

Simple dungeon floor generator in C#
C#
4
star
9

Sublime-Packages

My sublime tmBundles and snippets
Python
3
star
10

collectasaurus

Bookmarklet that shows Collecta results for a given page on the web.
JavaScript
3
star
11

Petite-Papillon

Objective-C
2
star
12

Try-Force

Unity3D Playmaker experiment in a top down action game
C#
2
star
13

Pyroclasm

Unity3D endless runner
C#
2
star
14

britg-vimgit

My vim and git stuff for easy replication
Vim Script
2
star
15

GTUG_ga_demo

Dallas GTUG Google Analytics Demo
1
star
16

iphone-web-studio

A simple pixel-accurate mock up of a retina iPhone (3.5" and 4") screen for testing UI elements via html/css.
1
star
17

twitter-like-game

Play a game through a twitter-like interface
Ruby
1
star
18

playforgecraft.com

ForgeCraft Website
JavaScript
1
star
19

fdbk

Super simple, page-based feedback app
Ruby
1
star
20

photowall-api

Client libraries for the photowall.com API
JavaScript
1
star
21

britg-wp-theme

The wordpress theme used on http://britg.com
PHP
1
star
22

cv

My CV
1
star