]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Add CORS headers to REST API responses 1207/head
authorArthurHoaro <arthur@hoa.ro>
Mon, 13 Aug 2018 10:21:10 +0000 (12:21 +0200)
committerArthurHoaro <arthur@hoa.ro>
Mon, 13 Aug 2018 10:21:10 +0000 (12:21 +0200)
Fixes #1174

index.php

index 4b86a3e2c17ece7338ad04646cedee6150bbbd47..9c1e4999ed68ef717142b66b4511403918ed8eb6 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1858,6 +1858,7 @@ $app->group('/api/v1', function() {
 })->add('\Shaarli\Api\ApiMiddleware');
 
 $response = $app->run(true);
+
 // Hack to make Slim and Shaarli router work together:
 // If a Slim route isn't found and NOT API call, we call renderPage().
 if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v1') === false) {
@@ -1865,5 +1866,12 @@ if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v
     header('Content-Type: text/html; charset=utf-8');
     renderPage($conf, $pluginManager, $linkDb, $history, $sessionManager, $loginManager);
 } else {
+    $response = $response
+        ->withHeader('Access-Control-Allow-Origin', '*')
+        ->withHeader(
+            'Access-Control-Allow-Headers',
+            'X-Requested-With, Content-Type, Accept, Origin, Authorization'
+        )
+        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
     $app->respond($response);
 }