Skip to content

Commit

Permalink
ref(querystring): 3rd party package used for dealing with qs
Browse files Browse the repository at this point in the history
  • Loading branch information
sladg committed Mar 3, 2023
1 parent 0c149be commit 534832d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 43 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "project",
"description": "Bref is a framework to write and deploy serverless PHP applications on AWS Lambda.",
"homepage": "https://bref.sh",
"keywords": ["bref", "serverless", "aws", "lambda", "faas"],
"keywords": [ "bref", "serverless", "aws", "lambda", "faas" ],
"autoload": {
"psr-4": {
"Bref\\": "src"
Expand Down Expand Up @@ -33,7 +33,8 @@
"nyholm/psr7": "^1.2",
"psr/container": "^1.0|^2.0",
"symfony/yaml": "^3.1|^4.0|^5.0|^6.0",
"symfony/http-client": "^4.4|^5.0|^6.0"
"symfony/http-client": "^4.4|^5.0|^6.0",
"crwlr/query-string": "^1.0"
},
"require-dev": {
"aws/aws-sdk-php": "^3.172",
Expand All @@ -56,7 +57,7 @@
"static-analysis": [
"vendor/bin/phpstan analyse"
],
"tests": [
"unit-tests": [
"vendor/bin/phpunit --testsuite small"
]
},
Expand Down
43 changes: 3 additions & 40 deletions src/Event/Http/HttpRequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bref\Event\InvalidLambdaEvent;
use Bref\Event\LambdaEvent;
use Crwlr\QueryString\Query;

/**
* Represents a Lambda event that comes from a HTTP request.
Expand Down Expand Up @@ -347,49 +348,11 @@ public function isFormatV2(): bool
* converts them to underscores. This method works around this issue so the
* requested query array returns the proper keys with dots.
*
* This method is heavily inspired by https://github.com/crwlrsoft/url.
*
* @return array<string, string>
*/
private function queryStringToArray(string $query): array
{
parse_str($query, $array);
// Matches keys in the query that contain a dot
if (! preg_match('/(?:^|&)([^\[=&]*\.)/', $query)) {
return $array;
}

// Regex to find keys in query string.
$keyRegex = '/(?:^|&)([^=&\[]+)(?:[=&\[]|$)/';
$encodedQuery = urldecode($query);
preg_match_all($keyRegex, $encodedQuery, $matches);
$brokenKeys = [];

// Create mapping of broken keys to original proper keys.
foreach ($matches[1] as $value) {
if (strpos($value, '.') !== false) {
$random = bin2hex(random_bytes(10));
$brokenKeys[$random] = $value;
}
}

// Modify the $query to be able to parse string again.
$placeholder = array_flip($brokenKeys);
$modifiedQuery = preg_replace_callback($keyRegex, function ($matches) use ($placeholder) {
if (! isset($placeholder[$matches[1]])) {
return $matches[0];
}
return str_replace($matches[1], $placeholder[$matches[1]], $matches[0]);
}, $encodedQuery);

$output = [];
parse_str($modifiedQuery, $array);

// Recreate the array with the proper keys.
foreach ($array as $key => $value) {
$output[$brokenKeys[$key] ?? $key] = $value;
}

return $output;
$queryString = Query::fromString($query);
return $queryString->toArray();
}
}

0 comments on commit 534832d

Please sign in to comment.