Skip to content

Commit 1afc6ab

Browse files
authored
Always return json for api requests (librenms#12335)
even if the client does not properly request it
1 parent b162eff commit 1afc6ab

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

app/Http/Kernel.php

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Kernel extends HttpKernel
5353
],
5454

5555
'api' => [
56+
\App\Http\Middleware\EnforceJson::class,
5657
\Illuminate\Routing\Middleware\SubstituteBindings::class,
5758
'authenticate:token',
5859
],

app/Http/Middleware/EnforceJson.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*
3+
* EnforceJson.php
4+
*
5+
* -Description-
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* @package LibreNMS
21+
* @link http://librenms.org
22+
* @copyright 2020 Tony Murray
23+
* @author Tony Murray <[email protected]>
24+
*/
25+
26+
namespace App\Http\Middleware;
27+
28+
use Closure;
29+
30+
class EnforceJson
31+
{
32+
/**
33+
* Enforce json
34+
*
35+
* @param \Illuminate\Http\Request $request
36+
* @param \Closure $next
37+
* @return mixed
38+
*/
39+
public function handle($request, Closure $next)
40+
{
41+
$request->headers->set('Accept', 'application/json');
42+
43+
return $next($request);
44+
}
45+
}

html/api_v0.php

+24-28
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,55 @@
11
<?php
22

3-
/**
4-
* Laravel - A PHP Framework For Web Artisans
5-
*
6-
* @author Taylor Otwell <[email protected]>
7-
*/
3+
use Illuminate\Contracts\Http\Kernel;
4+
use Illuminate\Http\Request;
5+
6+
define('LARAVEL_START', microtime(true));
87

98
/*
109
|--------------------------------------------------------------------------
11-
| Register The Auto Loader
10+
| Check If Application Is Under Maintenance
1211
|--------------------------------------------------------------------------
1312
|
14-
| Composer provides a convenient, automatically generated class loader for
15-
| our application. We just need to utilize it! We'll simply require it
16-
| into the script here so that we don't have to worry about manual
17-
| loading any of our classes later on. It feels great to relax.
13+
| If the application is maintenance / demo mode via the "down" command we
14+
| will require this file so that any prerendered template can be shown
15+
| instead of starting the framework, which could cause an exception.
1816
|
1917
*/
2018

21-
require __DIR__ . '/../vendor/autoload.php';
19+
if (file_exists(__DIR__ . '/../storage/framework/maintenance.php')) {
20+
require __DIR__ . '/../storage/framework/maintenance.php';
21+
}
2222

2323
/*
2424
|--------------------------------------------------------------------------
25-
| Turn On The Lights
25+
| Register The Auto Loader
2626
|--------------------------------------------------------------------------
2727
|
28-
| We need to illuminate PHP development, so let us turn on the lights.
29-
| This bootstraps the framework and gets it ready for use, then it
30-
| will load up this application so that we can run it and send
31-
| the responses back to the browser and delight our users.
28+
| Composer provides a convenient, automatically generated class loader for
29+
| this application. We just need to utilize it! We'll simply require it
30+
| into the script here so we don't need to manually load our classes.
3231
|
3332
*/
3433

35-
$app = require_once __DIR__ . '/../bootstrap/app.php';
34+
require __DIR__ . '/../vendor/autoload.php';
3635

3736
/*
3837
|--------------------------------------------------------------------------
3938
| Run The Application
4039
|--------------------------------------------------------------------------
4140
|
42-
| Once we have the application, we can handle the incoming request
43-
| through the kernel, and send the associated response back to
44-
| the client's browser allowing them to enjoy the creative
45-
| and wonderful application we have prepared for them.
41+
| Once we have the application, we can handle the incoming request using
42+
| the application's HTTP kernel. Then, we will send the response back
43+
| to this client's browser, allowing them to enjoy our application.
4644
|
4745
*/
4846

49-
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
50-
51-
$_SERVER['HTTP_ACCEPT'] = 'application/json'; // force json accepted
47+
$app = require_once __DIR__ . '/../bootstrap/app.php';
5248

53-
$response = $kernel->handle(
54-
$request = Illuminate\Http\Request::capture()
55-
);
49+
$kernel = $app->make(Kernel::class);
5650

57-
$response->send();
51+
$response = tap($kernel->handle(
52+
$request = Request::capture()
53+
))->send();
5854

5955
$kernel->terminate($request, $response);

0 commit comments

Comments
 (0)