Skip to content

Commit

Permalink
Update wsgi.py
Browse files Browse the repository at this point in the history
Very quick and dirty hack to fix encode#66
  • Loading branch information
allanice001 committed Aug 21, 2017
1 parent 1a2519c commit c990f27
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion apistar/frameworks/wsgi.py
Expand Up @@ -93,6 +93,14 @@ def create_http_injector(self) -> Injector:
def __call__(self,
environ: typing.Dict[str, typing.Any],
start_response: typing.Callable):
def add_cors_headers(status, headers, exc_info=None):
headers = Headers(headers)
headers.add('Access-Control-Allow-Origin', '*')
headers.add('Access-Control-Allow-Credentials', True)
headers.add('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin')
headers.add('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS, POST, PUT')
return start_response(status, headers.to_list(), exc_info)

state = {
'wsgi_environ': environ,
'kwargs': None,
Expand All @@ -101,6 +109,9 @@ def __call__(self,
method = environ['REQUEST_METHOD'].upper()
path = environ['SCRIPT_NAME'] + environ['PATH_INFO']
try:
if method == "OPTIONS":
add_cors_headers("200 Ok", [("Content-Type", "text/plain")])
return [b'200 Ok']
handler, kwargs = self.router.lookup(path, method)
state['kwargs'] = kwargs
response = self.http_injector.run(handler, state=state)
Expand All @@ -118,7 +129,11 @@ def __call__(self,
status_text = str(response.status)

headers = list(response.headers.items())
headers.append(('content-type', response.content_type))
headers.append(('Access-Control-Allow-Origin', '*'))
headers.append(('Access-Control-Allow-Credentials', True))
headers.append(('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin'))
headers.append(('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS, POST, PUT'))
headers.append(('Content-Type', response.content_type))

if isinstance(response.content, (bytes, str)):
content = [response.content]
Expand Down

0 comments on commit c990f27

Please sign in to comment.