Selaa lähdekoodia

Add cors support

moritan 9 vuotta sitten
vanhempi
commit
ae6b85b376
1 muutettua tiedostoa jossa 39 lisäystä ja 0 poistoa
  1. 39
    0
      searx/rest-server.py

+ 39
- 0
searx/rest-server.py Näytä tiedosto

@@ -145,6 +145,45 @@ def get_locale():
145 145
 
146 146
     return locale
147 147
 
148
+@app.before_request
149
+def option_autoreply():
150
+    """ Always reply 200 on OPTIONS request """
151
+
152
+    if request.method == 'OPTIONS':
153
+        resp = app.make_default_options_response()
154
+
155
+        headers = None
156
+        if 'ACCESS_CONTROL_REQUEST_HEADERS' in request.headers:
157
+            headers = request.headers['ACCESS_CONTROL_REQUEST_HEADERS']
158
+
159
+        h = resp.headers
160
+
161
+        # Allow the origin which made the XHR
162
+        h['Access-Control-Allow-Origin'] = request.headers['Origin']
163
+        # Allow the actual method
164
+        h['Access-Control-Allow-Methods'] = request.headers['Access-Control-Request-Method']
165
+        # Allow for 10 seconds
166
+        h['Access-Control-Max-Age'] = "10"
167
+
168
+        # We also keep current headers
169
+        if headers is not None:
170
+            h['Access-Control-Allow-Headers'] = headers
171
+
172
+        return resp
173
+
174
+
175
+@app.after_request
176
+def set_allow_origin(resp):
177
+    """ Set origin for GET, POST, PUT, DELETE requests """
178
+
179
+    h = resp.headers
180
+
181
+    # Allow crossdomain for other HTTP Verbs
182
+    if request.method != 'OPTIONS' and 'Origin' in request.headers:
183
+        h['Access-Control-Allow-Origin'] = request.headers['Origin']
184
+
185
+
186
+    return resp
148 187
 
149 188
 if __name__ == '__main__':
150 189
     app.run(