Skip to content

Commit

Permalink
feat(http): add content-length header to responses
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxpiper committed Oct 28, 2020
1 parent fbac4bf commit cdd6016
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
30 changes: 30 additions & 0 deletions app/Http/Middleware/AddContentLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Ushahidi\App\Http\Middleware;

use Closure;

class AddContentLength
{
/**
* Add content-length header to responses before being sent.
* This only happens if it hasn't been already set.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);

if (!$response->isEmpty() && !$response->headers->get('Content-Length')) {
$response->headers->set(
'Content-Length',
// ensure that we get byte count by using 8bit encoding
mb_strlen($response->getContent(), '8bit')
);
}
return $response->prepare($request);
}
}
4 changes: 3 additions & 1 deletion bootstrap/gwcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
if ($origin) {
header('Access-Control-Allow-origin: ' . $origin);
}
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$body = json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
header('Content-length: ' . mb_strlen($body, '8bit'));
echo $body;

# END request processing
exit();
3 changes: 2 additions & 1 deletion bootstrap/lumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
*/

$app->middleware([
Ushahidi\App\Http\Middleware\AddContentLength::class,
Ushahidi\App\Multisite\DetectSiteMiddleware::class,
Barryvdh\Cors\HandleCors::class,
Ushahidi\App\Http\Middleware\MaintenanceMode::class,
Ushahidi\App\Http\Middleware\SetLocale::class
Ushahidi\App\Http\Middleware\SetLocale::class,
]);

$app->routeMiddleware([
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/bootstrap/RestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,22 @@ public function theResponseIsJson()
{
$data = json_decode($this->response->getBody(true), true);

// The response should have appropriate headers
$content_type = $this->response->getHeaderLine('Content-Type') ?? "";
$content_length = $this->response->getHeaderLine('Content-Length');
if (!$content_type) {
throw new \Exception('HTTP header Content-Type missing');
}
if (stripos($content_type, 'application/json') === false) {
throw new \Exception('HTTP header Content-Type is not "application/json", instead: '.$content_type);
}
if (!$content_length) {
throw new \Exception('HTTP header Content-Length is missing');
}
if (intval($content_length) != mb_strlen($this->response->getBody(), '8bit')) {
throw new \Exception('HTTP header Content-Length doesn\'t match content size');
}

// Check for NULL not empty - since [] and {} will be empty but valid
if ($data === null) {
// Get further error info
Expand Down

0 comments on commit cdd6016

Please sign in to comment.