Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle options requests #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

koffeinfrei
Copy link
Contributor

Add the possibility to handle OPTIONS requests as well in the handler.

This allows to handle e.g. CORS requests, like the following:

class Handler
  def run(body, headers)
    if headers['REQUEST_METHOD'] == 'OPTIONS'
      return [
        [],
        {
          'Allow' => 'POST, GET, OPTIONS',
          'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS',
          'Access-Control-Allow-Origin' => '*',
          'Access-Control-Allow-Headers' => 'Accept'
        }
      ]
    end

    # ....
end

I also extracted the responding logic to a sinatra helper, as the code wasn't very dry.

@alexellis
Copy link
Member

Is there a linked issue for this?

@koffeinfrei
Copy link
Contributor Author

Is there a linked issue for this?

Yes: #11

I didn't link it initially as I didn't want to create bias for the discussion in the issue (as I created the MR first, which is not according to you contribution guidelines, as pointed out by you).

@alexellis
Copy link
Member

Writing the PR first already created a bias.

There is no CORS support in any of the function templates. Having this as a default would go against that, if it's to be added, I would suggest it's made strictly opt-in.

@koffeinfrei
Copy link
Contributor Author

There is no CORS support in any of the function templates.

Just to make sure we're on the same page: This PR doesn't add CORS support. It just adds a sinatra route for OPTIONS requests, so they are passed to the function as well. What to do with that is still up to the handler function. With this PR GET, POST, PUT, DELETE and OPTIONS requests are processed. Nothing more.

Having said that, do you think passing OPTIONS requests as well could be problematic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants