13
13
from django .utils .cache import patch_vary_headers
14
14
15
15
from corsheaders .conf import conf
16
+ from corsheaders .conf import Settings
16
17
from corsheaders .signals import check_request_enabled
17
18
18
19
ACCESS_CONTROL_ALLOW_ORIGIN = "access-control-allow-origin"
@@ -35,8 +36,10 @@ def __init__(
35
36
Callable [[HttpRequest ], HttpResponseBase ]
36
37
| Callable [[HttpRequest ], Awaitable [HttpResponseBase ]]
37
38
),
39
+ conf : Settings = conf ,
38
40
) -> None :
39
41
self .get_response = get_response
42
+ self .conf = conf
40
43
if asyncio .iscoroutinefunction (self .get_response ):
41
44
# Mark the class as async-capable, but do the actual switch
42
45
# inside __call__ to avoid swapping out dunder methods
@@ -105,34 +108,38 @@ def add_response_headers(
105
108
except ValueError :
106
109
return response
107
110
108
- if conf .CORS_ALLOW_CREDENTIALS :
111
+ if self . conf .CORS_ALLOW_CREDENTIALS :
109
112
response [ACCESS_CONTROL_ALLOW_CREDENTIALS ] = "true"
110
113
111
114
if (
112
- not conf .CORS_ALLOW_ALL_ORIGINS
115
+ not self . conf .CORS_ALLOW_ALL_ORIGINS
113
116
and not self .origin_found_in_white_lists (origin , url )
114
117
and not self .check_signal (request )
115
118
):
116
119
return response
117
120
118
- if conf .CORS_ALLOW_ALL_ORIGINS and not conf .CORS_ALLOW_CREDENTIALS :
121
+ if self . conf .CORS_ALLOW_ALL_ORIGINS and not self . conf .CORS_ALLOW_CREDENTIALS :
119
122
response [ACCESS_CONTROL_ALLOW_ORIGIN ] = "*"
120
123
else :
121
124
response [ACCESS_CONTROL_ALLOW_ORIGIN ] = origin
122
125
123
- if len (conf .CORS_EXPOSE_HEADERS ):
126
+ if len (self . conf .CORS_EXPOSE_HEADERS ):
124
127
response [ACCESS_CONTROL_EXPOSE_HEADERS ] = ", " .join (
125
- conf .CORS_EXPOSE_HEADERS
128
+ self . conf .CORS_EXPOSE_HEADERS
126
129
)
127
130
128
131
if request .method == "OPTIONS" :
129
- response [ACCESS_CONTROL_ALLOW_HEADERS ] = ", " .join (conf .CORS_ALLOW_HEADERS )
130
- response [ACCESS_CONTROL_ALLOW_METHODS ] = ", " .join (conf .CORS_ALLOW_METHODS )
131
- if conf .CORS_PREFLIGHT_MAX_AGE :
132
- response [ACCESS_CONTROL_MAX_AGE ] = str (conf .CORS_PREFLIGHT_MAX_AGE )
132
+ response [ACCESS_CONTROL_ALLOW_HEADERS ] = ", " .join (
133
+ self .conf .CORS_ALLOW_HEADERS
134
+ )
135
+ response [ACCESS_CONTROL_ALLOW_METHODS ] = ", " .join (
136
+ self .conf .CORS_ALLOW_METHODS
137
+ )
138
+ if self .conf .CORS_PREFLIGHT_MAX_AGE :
139
+ response [ACCESS_CONTROL_MAX_AGE ] = str (self .conf .CORS_PREFLIGHT_MAX_AGE )
133
140
134
141
if (
135
- conf .CORS_ALLOW_PRIVATE_NETWORK
142
+ self . conf .CORS_ALLOW_PRIVATE_NETWORK
136
143
and request .headers .get (ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK ) == "true"
137
144
):
138
145
response [ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK ] = "true"
@@ -141,28 +148,28 @@ def add_response_headers(
141
148
142
149
def origin_found_in_white_lists (self , origin : str , url : SplitResult ) -> bool :
143
150
return (
144
- (origin == "null" and origin in conf .CORS_ALLOWED_ORIGINS )
151
+ (origin == "null" and origin in self . conf .CORS_ALLOWED_ORIGINS )
145
152
or self ._url_in_whitelist (url )
146
153
or self .regex_domain_match (origin )
147
154
)
148
155
149
156
def regex_domain_match (self , origin : str ) -> bool :
150
157
return any (
151
158
re .match (domain_pattern , origin )
152
- for domain_pattern in conf .CORS_ALLOWED_ORIGIN_REGEXES
159
+ for domain_pattern in self . conf .CORS_ALLOWED_ORIGIN_REGEXES
153
160
)
154
161
155
162
def is_enabled (self , request : HttpRequest ) -> bool :
156
163
return bool (
157
- re .match (conf .CORS_URLS_REGEX , request .path_info )
164
+ re .match (self . conf .CORS_URLS_REGEX , request .path_info )
158
165
) or self .check_signal (request )
159
166
160
167
def check_signal (self , request : HttpRequest ) -> bool :
161
168
signal_responses = check_request_enabled .send (sender = None , request = request )
162
169
return any (return_value for function , return_value in signal_responses )
163
170
164
171
def _url_in_whitelist (self , url : SplitResult ) -> bool :
165
- origins = [urlsplit (o ) for o in conf .CORS_ALLOWED_ORIGINS ]
172
+ origins = [urlsplit (o ) for o in self . conf .CORS_ALLOWED_ORIGINS ]
166
173
return any (
167
174
origin .scheme == url .scheme and origin .netloc == url .netloc
168
175
for origin in origins
0 commit comments