Added support for automagic OPTIONS
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent a532568 · Browse files · 7 files·+74 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+74 added−18 removed
- tests:+13 added−4 removed
- other:+61 added−14 removed
Provenance
- Commit
5e1b103- Tree
62265aa- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a532568
7 files changed+74 added−18 removed
modified:docs/quickstart.rst+7 added-1 removed
267 unmodified lines268 If `GET` is present, `HEAD` will be added automatically for you. You269 don't have to deal with that. It will also make sure that `HEAD` requests270 are handled like the `HTTP RFC`_ (the document describing the HTTP271 protocol) demands, so you can completely ignore that part of the HTTP272-specification.272+specification. Likewise as of Flask 0.6, `OPTIONS` is implemented for you273+as well automatically.274 275 You have no idea what an HTTP method is? Worry not, here quick276 introduction in HTTP methods and why they matter:277 32 unmodified lines310 311 `DELETE`312 Remove the information that the given location.313 314+`OPTIONS`315+ Provides a quick way for a requesting client to figure out which316+ methods are supported by this URL. Starting with Flask 0.6, this317+ is implemented for you automatically.318+319 Now the interesting part is that in HTML4 and XHTML1, the only methods a320 form might submit to the server are `GET` and `POST`. But with JavaScript321 and future HTML standards you can use other methods as well. Furthermore322 HTTP became quite popular lately and there are more things than browsers414 unmodified linesmodified:flask/app.py+31 added-6 removed
462 unmodified lines463 464 .. versionchanged:: 0.2465 `view_func` parameter added.466 467+ .. versionchanged:: 0.6468+ `OPTIONS` is added automatically as method.469+470 :param rule: the URL rule as string471 :param endpoint: the endpoint for the registered URL rule. Flask472 itself assumes the name of the view function as473 endpoint474 :param view_func: the function to call when serving a request to the475 provided endpoint476 :param options: the options to be forwarded to the underlying474- :class:`~werkzeug.routing.Rule` object477+ :class:`~werkzeug.routing.Rule` object. A change478+ to Werkzeug is handling of method options. methods479+ is a list of methods this rule should be limited480+ to (`GET`, `POST` etc.). By default a rule481+ just listens for `GET` (and implicitly `HEAD`).482+ Starting with Flask 0.6, `OPTIONS` is implicitly483+ added and handled by the standard request handling.484 """485 if endpoint is None:486 assert view_func is not None, 'expected view func if endpoint ' \487 'is not provided.'488 endpoint = view_func.__name__489 options['endpoint'] = endpoint481- options.setdefault('methods', ('GET',))482- self.url_map.add(Rule(rule, **options))490+ methods = options.pop('methods', ('GET',))491+ provide_automatic_options = False492+ if 'OPTIONS' not in methods:493+ methods = tuple(methods) + ('OPTIONS',)494+ provide_automatic_options = True495+ rule = Rule(rule, methods=methods, **options)496+ rule.provide_automatic_options = provide_automatic_options497+ self.url_map.add(rule)498 if view_func is not None:499 self.view_functions[endpoint] = view_func500 501 def route(self, rule, **options):51 unmodified lines553 as well:554 555 :param rule: the URL rule as string556 :param methods: a list of methods this rule should be limited542- to (``GET``, ``POST`` etc.). By default a rule543- just listens for ``GET`` (and implicitly ``HEAD``).557+ to (`GET`, `POST` etc.). By default a rule558+ just listens for `GET` (and implicitly `HEAD`).559+ Starting with Flask 0.6, `OPTIONS` is implicitly560+ added and handled by the standard request handling.561 :param subdomain: specifies the rule for the subdomain in case562 subdomain matching is in use.563 :param strict_slashes: can be used to disable the strict slashes564 setting for this rule. See above.101 unmodified lines666 req = _request_ctx_stack.top.request667 try:668 if req.routing_exception is not None:669 raise req.routing_exception670+ rule = req.url_rule671+ # if we provide automatic options for this URL and the672+ # request came with the OPTIONS method, reply automatically 673+ if rule.provide_automatic_options and req.method == 'OPTIONS':674+ rv = self.response_class()675+ rv.allow.update(rule.methods)676+ return rv677+ # otherwise dispatch to the handler for that endpoint653- return self.view_functions[req.endpoint](**req.view_args)678+ return self.view_functions[rule.endpoint](**req.view_args)679 except HTTPException, e:680 return self.handle_http_exception(e)681 682 def make_response(self, rv):162 unmodified linesmodified:flask/ctx.py+3 added-2 removed
36 unmodified lines37 self.g = _RequestGlobals()38 self.flashes = None39 40 try:41- self.request.endpoint, self.request.view_args = \42- self.url_adapter.match()41+ url_rule, self.request.view_args = \42+ self.url_adapter.match(return_rule=True)43+ self.request.url_rule = url_rule44 except HTTPException, e:45 self.request.routing_exception = e46 47 def push(self):20 unmodified linesmodified:flask/helpers.py+1 added
13 unmodified lines14 import posixpath15 import mimetypes16 from time import time17 from zlib import adler3218+from functools import wraps19 20 # try to load the best simplejson implementation available. If JSON21 # is not installed, we add a failing class.22 json_available = True382 unmodified linesmodified:flask/wrappers.py+16 added-5 removed
22 unmodified lines23 the request object used you can subclass this and set24 :attr:`~flask.Flask.request_class` to your subclass.25 """26 27- #: the endpoint that matched the request. This in combination with28- #: :attr:`view_args` can be used to reconstruct the same or a29- #: modified URL. If an exception happened when matching, this will30- #: be `None`.31- endpoint = None27+ #: the internal URL rule that matched the request. This can be28+ #: useful to inspect which methods are allowed for the URL from29+ #: a before/after handler (``request.url_rule.methods``) etc.30+ #:31+ #: .. versionadded:: 0.632+ url_rule = None33 34 #: a dict of view arguments that matched the request. If an exception35 #: happened when matching, this will be `None`.36 view_args = None4 unmodified lines41 #: something similar.42 routing_exception = None43 44 @property45+ def endpoint(self):46+ """The endpoint that matched the request. This in combination with47+ :attr:`view_args` can be used to reconstruct the same or a48+ modified URL. If an exception happened when matching, this will49+ be `None`.50+ """51+ if self.url_rule is not None:52+ return self.url_rule.endpoint53+54+ @property55 def module(self):56 """The name of the current module"""57 if self.endpoint and '.' in self.endpoint:58 return self.endpoint.rsplit('.', 1)[0]22 unmodified linesmodified:tests/flask_tests.py+13 added-4 removed
109 unmodified lines110 111 112 class BasicFunctionalityTestCase(unittest.TestCase):113 114+ def test_options_work(self):115+ app = flask.Flask(__name__)116+ @app.route('/', methods=['GET', 'POST'])117+ def index():118+ return 'Hello World'119+ rv = app.test_client().open('/', method='OPTIONS')120+ assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST']121+ assert rv.data == ''122+123 def test_request_dispatching(self):124 app = flask.Flask(__name__)125 @app.route('/')126 def index():5 unmodified lines132 c = app.test_client()133 assert c.get('/').data == 'GET'134 rv = c.post('/')135 assert rv.status_code == 405127- assert sorted(rv.allow) == ['GET', 'HEAD']136+ assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS']137 rv = c.head('/')138 assert rv.status_code == 200139 assert not rv.data # head truncates140 assert c.post('/more').data == 'POST'141 assert c.get('/more').data == 'GET'142 rv = c.delete('/more')143 assert rv.status_code == 405135- assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']144+ assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST']145 146 def test_url_mapping(self):147 app = flask.Flask(__name__)148 def index():7 unmodified lines156 c = app.test_client()157 assert c.get('/').data == 'GET'158 rv = c.post('/')159 assert rv.status_code == 405151- assert sorted(rv.allow) == ['GET', 'HEAD']160+ assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS']161 rv = c.head('/')162 assert rv.status_code == 200163 assert not rv.data # head truncates164 assert c.post('/more').data == 'POST'165 assert c.get('/more').data == 'GET'166 rv = c.delete('/more')167 assert rv.status_code == 405159- assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']168+ assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST']169 170 def test_session(self):171 app = flask.Flask(__name__)172 app.secret_key = 'testkey'821 unmodified linesmodified:CHANGES+3 added
8 unmodified lines9 Release date to be announced, codename to be decided.10 11 - after request functions are now called in reverse order of12 registration.13+- OPTIONS is now automatically implemented by Flask unless the14+ application explictly adds 'OPTIONS' as method to the URL rule.15+ In this case no automatic OPTIONS handling kicks in.16 17 Version 0.5.118 -------------19 98 unmodified lines