diff options
| author | Fulgen301 <tokmajigeorge@gmail.com> | 2018-10-16 18:57:01 +0200 |
|---|---|---|
| committer | Fulgen301 <tokmajigeorge@gmail.com> | 2018-10-16 18:57:01 +0200 |
| commit | 8213c244eaca9a65a51ccb8422b3adb45485ef43 (patch) | |
| tree | d8f22d6e91aa869e8ab51b5d5bf6de5de2824881 /cors.py | |
| parent | 28afed69d3f5a04c86aa87ad18e15ec55bdc7a82 (diff) | |
| download | parry-8213c244eaca9a65a51ccb8422b3adb45485ef43.tar.gz parry-8213c244eaca9a65a51ccb8422b3adb45485ef43.zip | |
Subclass HTTPReponse in order to ensure the presence of CORS headers
Diffstat (limited to 'cors.py')
| -rw-r--r-- | cors.py | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -0,0 +1,31 @@ +# Copyright (c) 2018, George Tokmaji + +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. + +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +from bottle import HTTPResponse, route, response, hook + +class ParryHTTPResponse(HTTPResponse): + def __init__(self, body="", status=None, headers=None, **more_headers): + more_headers["Access-Control-Allow-Origin"] = "*" + more_headers["Access-Control-Allow-Methods"] = "GET, POST, HEAD, PUT, PATCH, DELETE" + more_headers["Access-Control-Allow-Headers"] = "*" + super().__init__(body, status, headers, **more_headers) + +@route("<path:path>", method="OPTIONS") +def options(path): + return ParryHTTPResponse(status=204) + +@hook("after_request") +def enable_cors(): + response.headers["Access-Control-Allow-Origin"] = "*" + response.headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, PATCH, HEAD" |
