2
2
from collections import namedtuple
3
3
from urllib .parse import unquote_plus , urlparse
4
4
5
+ def fail (response , body ):
6
+ """Sets up response to fail with the provided response body.
7
+
8
+ Args:
9
+ response: the wptserve Response that was passed to main
10
+ body: the HTTP response body to use
11
+ """
12
+ response .status = (400 , "Bad Request" )
13
+ response .headers .set (b"Content-Type" , b"text/plain" )
14
+ response .content = body
15
+
5
16
def headers_to_ascii (headers ):
6
17
"""Converts a header map with binary values to one with ASCII values.
7
18
@@ -22,18 +33,15 @@ def headers_to_ascii(headers):
22
33
header_map [pair [0 ].decode ("ASCII" )] = values
23
34
return header_map
24
35
25
-
26
- def handle_cors_headers_and_preflight (request , response ):
27
- """Applies CORS logic common to many entrypoints.
36
+ def attach_origin_and_credentials_headers (request , response ):
37
+ """Attaches Access-Control-Allow-Origin and Access-Control-Allow-Credentials
38
+ response headers to a response, if the request indicates they're needed.
39
+ Only intended for internal use.
28
40
29
41
Args:
30
42
request: the wptserve Request that was passed to main
31
43
response: the wptserve Response that was passed to main
32
-
33
- Returns True if the request is a CORS preflight, which is entirely handled by
34
- this function, so that the calling function should immediately return.
35
44
"""
36
- # Append CORS headers if needed
37
45
if b"origin" in request .headers :
38
46
response .headers .set (b"Access-Control-Allow-Origin" ,
39
47
request .headers .get (b"origin" ))
@@ -42,20 +50,50 @@ def handle_cors_headers_and_preflight(request, response):
42
50
response .headers .set (b"Access-Control-Allow-Credentials" ,
43
51
request .headers .get (b"credentials" ))
44
52
53
+ def handle_cors_headers_fail_if_preflight (request , response ):
54
+ """Adds CORS headers if necessary. In the case of CORS preflights, generates
55
+ a failure response. To be used when CORS preflights are not expected.
56
+
57
+ Args:
58
+ request: the wptserve Request that was passed to main
59
+ response: the wptserve Response that was passed to main
60
+
61
+ Returns True if the request is a CORS preflight, in which case the calling
62
+ function should immediately return.
63
+ """
64
+ # Handle CORS preflight requests.
65
+ if request .method == u"OPTIONS" :
66
+ fail (response , "CORS preflight unexpectedly received." )
67
+ return True
68
+
69
+ # Append CORS headers if needed
70
+ attach_origin_and_credentials_headers (request , response )
71
+ return False
72
+
73
+ def handle_cors_headers_and_preflight (request , response ):
74
+ """Applies CORS logic, either adding CORS headers to response or generating
75
+ an entire response to preflights.
76
+
77
+ Args:
78
+ request: the wptserve Request that was passed to main
79
+ response: the wptserve Response that was passed to main
80
+
81
+ Returns True if the request is a CORS preflight, in which case the calling
82
+ function should immediately return.
83
+ """
84
+ # Append CORS headers if needed
85
+ attach_origin_and_credentials_headers (request , response )
86
+
45
87
# Handle CORS preflight requests.
46
88
if not request .method == u"OPTIONS" :
47
89
return False
48
90
49
91
if not b"Access-Control-Request-Method" in request .headers :
50
- response .status = (400 , b"Bad Request" )
51
- response .headers .set (b"Content-Type" , b"text/plain" )
52
- response .content = "Failed to get access-control-request-method in preflight!"
92
+ fail (response , "Failed to get access-control-request-method in preflight!" )
53
93
return True
54
94
55
95
if not b"Access-Control-Request-Headers" in request .headers :
56
- response .status = (400 , b"Bad Request" )
57
- response .headers .set (b"Content-Type" , b"text/plain" )
58
- response .content = "Failed to get access-control-request-headers in preflight!"
96
+ fail (response , "Failed to get access-control-request-headers in preflight!" )
59
97
return True
60
98
61
99
response .headers .set (b"Access-Control-Allow-Methods" ,
0 commit comments