Skip to content

Commit

Permalink
fix(query): Fix request query missing
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterRO94 committed Jul 8, 2024
1 parent 5f7b2ed commit f708454
Show file tree
Hide file tree
Showing 9 changed files with 3,397 additions and 3,017 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ update:
build:
docker-compose build

run: xdebug-init
${DOCKER_COMPOSE_RUN} app bash -c "/var/www/html/whp --channel-uuid=$(channel) --forward-url=$(url)"
build.whp:
${DOCKER_COMPOSE_EXEC_WWW} app bash -c "./whp app:build"
docker compose build whp

run: up
up:
docker compose up -d

xdebug-init:
@if [ $$USER = 'vagrant' ]; then \
Expand Down Expand Up @@ -104,3 +109,6 @@ chl-first:
${CONV_CHL_DR} \
-c "${CONV_CHL_CMD} --first-release"


ssh:
${DOCKER_COMPOSE_EXEC_WWW} app bash
2 changes: 1 addition & 1 deletion src/app/Commands/Concerns/ForwardsProxyWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function onMessage(string $forwardUrl): callable
$timestamp = Carbon::now()->setTimezone($tz)->toDateTimeString();
render("<h2 class='font-bold italic text-center text-lime-500'>Webhook received: {$timestamp} ({$tz})</h2>");

$requestData = RequestData::fromRaw($data->request);
$requestData = RequestData::fromRaw((array) $data->request);
$start = microtime(true);
$response = RequestForwarder::make($data->request)->forward($forwardUrl);
$forwardedInSeconds = round(microtime(true) - $start, 3) . 's';
Expand Down
14 changes: 6 additions & 8 deletions src/app/Commands/ProxyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use InvalidArgumentException;
use React\EventLoop\LoopInterface;
use Throwable;
use function Laravel\Prompts\text;

class ProxyCommand extends Command
{
Expand All @@ -39,13 +40,10 @@ protected function getLoop(): LoopInterface

public function handle(): void
{
$channelIdentifier = $this->option('channel') ?? $this->ask('Enter the channel UUID or webhook URL:');

if (empty($channelIdentifier)) {
$this->error('The channel identifier is required');

return;
}
$channelIdentifier = $this->option('channel') ?? text(
'Enter the channel UUID or webhook URL:',
required: true,
);

try {
$channelUuid = $this->webhookProxy->parseChannelUuid($channelIdentifier);
Expand All @@ -55,7 +53,7 @@ public function handle(): void
return;
}

$forwardUrl = $this->option('forward-url') ?? $this->ask('Enter the URL to forward to:');
$forwardUrl = $this->option('forward-url') ?? text('Enter the URL to forward to:');

if (empty($forwardUrl)) {
$this->error('The forward URL is required');
Expand Down
22 changes: 13 additions & 9 deletions src/app/Proxy/RequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@
namespace App\Proxy;

use GuzzleHttp\Psr7\Request;
use stdClass;

class RequestData
{
public readonly array $query;

public readonly array $headers;


public function __construct(
public readonly string $method,
protected array|object|string $body,
array|stdClass $headers,
array|object $query = [],
array|object $headers = [],
) {
$this->headers = static::normalizeHeaders($headers);
$this->query = (array) $query;
}

public static function fromRaw(array|stdClass $request): static
public static function fromRaw(array $request): static
{
$request = (array) $request;

return new static(
$request['method'],
$request['body'],
$request['query'],
$request['headers'],
);
}

protected static function normalizeHeaders(array|stdClass $headers): array
protected static function normalizeHeaders(array|object $headers): array
{
$headers = (array) $headers;

return array_map(fn($header) => is_array($header) ? $header[0] : $header, $headers);
return array_map(
fn($header) => is_array($header) ? $header[0] : $header,
(array) $headers,
);
}

public function body(): string
Expand Down
17 changes: 12 additions & 5 deletions src/app/Proxy/RequestForwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class RequestForwarder
{
protected RequestData $payload;

protected Client $httpClient;

public function __construct(object $request)
{
$contentType = $request->headers->{'content-type'}[0] ?? 'application/json';
$contentType = $request->headers->{'content-type'}[0] ?? 'application/json';

$body = str_contains($contentType, 'text') && isset($request->body->raw)
? $request->body->raw
Expand All @@ -27,8 +29,13 @@ public function __construct(object $request)
$this->payload = new RequestData(
$request->method,
$body,
(array) $request->headers
(array) $request->query,
(array) $request->headers,
);

$this->httpClient = new Client([
'verify' => false,
]);
}

public static function make(object $request): static
Expand All @@ -38,10 +45,10 @@ public static function make(object $request): static

public function forward(string $url): ResponseInterface
{
$client = new Client();

try {
return $client->send($this->payload->toRequest($url));
return $this->httpClient->send($this->payload->toRequest($url), [
'query' => $this->payload->query,
]);
} catch (RequestException|ProviderException|BadResponseException $e) {
return $e->getResponse() ?? new Response(status: 500, body: $e->getMessage());
} catch (GuzzleException $e) {
Expand Down
Binary file modified src/builds/whp
Binary file not shown.
3 changes: 1 addition & 2 deletions src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
"php": "^8.1",
"guzzlehttp/guzzle": "^7.7",
"guzzlehttp/psr7": "^2.5",
"nunomaduro/termwind": "^1.15.1",
"ratchet/rfc6455": "^0.3.1",
"react/socket": "^1.12"
},
"require-dev": {
"laravel-zero/framework": "^10.0.2",
"laravel-zero/framework": "^11.0",
"laravel/pint": "^1.8",
"mockery/mockery": "^1.5.1",
"pestphp/pest": "^2.5"
Expand Down
Loading

0 comments on commit f708454

Please sign in to comment.