Skip to content

Commit

Permalink
Start to deconstruct the ClientProtocolContext now that the queue is …
Browse files Browse the repository at this point in the history
…instantiated from the container.
  • Loading branch information
ChadSikorra committed Aug 5, 2023
1 parent 1676627 commit 2e0be27
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 100 deletions.
1 change: 0 additions & 1 deletion src/FreeDSx/Ldap/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ private function registerServerClasses(): void
private function makeClientProtocolHandler(): ClientProtocolHandler
{
return new ClientProtocolHandler(
options: $this->clientOptions,
clientQueueInstantiator: $this->get(ClientQueueInstantiator::class),
protocolHandlerFactory: $this->get(ClientProtocolHandlerFactory::class),
);
Expand Down
2 changes: 0 additions & 2 deletions src/FreeDSx/Ldap/Protocol/ClientProtocolHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class ClientProtocolHandler
private ?Entry $rootDse = null;

public function __construct(
private readonly ClientOptions $options,
private readonly ClientQueueInstantiator $clientQueueInstantiator,
private readonly ClientProtocolHandlerFactory $protocolHandlerFactory,
private readonly ControlBag $controls = new ControlBag(),
Expand Down Expand Up @@ -123,7 +122,6 @@ public function send(
controls: $controls,
protocolHandler: $this,
queue: $this->queue(),
options: $this->options,
);

$messageFrom = $this->protocolHandlerFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
*/
class ClientBasicHandler implements RequestHandlerInterface, ResponseHandlerInterface
{
public function __construct(private readonly ClientQueue $queue)
{
}

/**
* RFC 4511, A.1. These are considered result codes that do not indicate an error condition.
*/
Expand All @@ -54,11 +58,10 @@ class ClientBasicHandler implements RequestHandlerInterface, ResponseHandlerInte
*/
public function handleRequest(ClientProtocolContext $context): ?LdapMessageResponse
{
$queue = $context->getQueue();
$message = $context->messageToSend();
$queue->sendMessage($message);
$this->queue->sendMessage($message);

return $queue->getMessage($message->getMessageId());
return $this->queue->getMessage($message->getMessageId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use FreeDSx\Ldap\Operation\Request\ExtendedRequest;
use FreeDSx\Ldap\Protocol\Factory\ExtendedResponseFactory;
use FreeDSx\Ldap\Protocol\LdapMessageResponse;
use FreeDSx\Ldap\Protocol\Queue\ClientQueue;
use FreeDSx\Socket\Exception\ConnectionException;
use ReflectionClass;
use ReflectionException;
Expand All @@ -34,9 +35,13 @@ class ClientExtendedOperationHandler extends ClientBasicHandler
{
private ExtendedResponseFactory $extendedResponseFactory;

public function __construct(ExtendedResponseFactory $extendedResponseFactory = null)
{
public function __construct(
ClientQueue $queue,
ExtendedResponseFactory $extendedResponseFactory = null
) {
$this->extendedResponseFactory = $extendedResponseFactory ?? new ExtendedResponseFactory();

parent::__construct($queue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler;

use FreeDSx\Asn1\Exception\EncoderException;
use FreeDSx\Ldap\ClientOptions;
use FreeDSx\Ldap\Control\Control;
use FreeDSx\Ldap\Entry\Entry;
use FreeDSx\Ldap\Exception\BindException;
Expand All @@ -41,8 +40,6 @@ class ClientProtocolContext
*/
private array $controls;

private ClientOptions $options;

private ClientProtocolHandler $protocolHandler;

private ClientQueue $clientQueue;
Expand All @@ -56,13 +53,11 @@ public function __construct(
array $controls,
ClientProtocolHandler $protocolHandler,
ClientQueue $queue,
ClientOptions $options
) {
$this->request = $request;
$this->controls = $controls;
$this->protocolHandler = $protocolHandler;
$this->clientQueue = $queue;
$this->options = $options;
}

public function getRequest(): RequestInterface
Expand All @@ -78,21 +73,6 @@ public function getControls(): array
return $this->controls;
}

public function getProtocolHandler(): ClientProtocolHandler
{
return $this->protocolHandler;
}

public function getQueue(): ClientQueue
{
return $this->clientQueue;
}

public function getOptions(): ClientOptions
{
return $this->options;
}

public function messageToSend(): LdapMessageRequest
{
if ($this->sentMessage !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class ClientSaslBindHandler implements RequestHandlerInterface

private Sasl $sasl;

public function __construct(?Sasl $sasl = null)
{
public function __construct(
private readonly ClientQueue $queue,
?Sasl $sasl = null
) {
$this->sasl = $sasl ?? new Sasl();
}

Expand All @@ -77,11 +79,10 @@ public function handleRequest(ClientProtocolContext $context): ?LdapMessageRespo
$detectDowngrade = ($request->getMechanism() === '');
$mech = $this->selectSaslMech($request, $context);

$queue = $context->getQueue();
$message = $context->messageToSend();
$queue->sendMessage($message);
$this->queue->sendMessage($message);

$response = $queue->getMessage($message->getMessageId());
$response = $this->queue->getMessage($message->getMessageId());
$saslResponse = $response->getResponse();
if (!$saslResponse instanceof BindResponse) {
throw new ProtocolException(sprintf(
Expand All @@ -92,7 +93,12 @@ public function handleRequest(ClientProtocolContext $context): ?LdapMessageRespo
if ($saslResponse->getResultCode() !== ResultCode::SASL_BIND_IN_PROGRESS) {
return $response;
}
$response = $this->processSaslChallenge($request, $queue, $saslResponse, $mech);
$response = $this->processSaslChallenge(
$request,
$this->queue,
$saslResponse,
$mech
);
if (
$detectDowngrade
&& $response->getResponse() instanceof BindResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler;

use FreeDSx\Ldap\ClientOptions;
use FreeDSx\Ldap\Operation\Request\SearchRequest;
use FreeDSx\Ldap\Protocol\LdapMessageRequest;
use FreeDSx\Ldap\Protocol\LdapMessageResponse;
Expand All @@ -27,8 +28,11 @@ class ClientSearchHandler extends ClientBasicHandler
{
use ClientSearchTrait;

public function __construct(private readonly ClientQueue $queue)
{
public function __construct(
private readonly ClientQueue $queue,
private readonly ClientOptions $options,
) {
parent::__construct($this->queue);
}

/**
Expand All @@ -39,7 +43,7 @@ public function handleRequest(ClientProtocolContext $context): ?LdapMessageRespo
/** @var SearchRequest $request */
$request = $context->getRequest();
if ($request->getBaseDn() === null) {
$request->setBaseDn($context->getOptions()->getBaseDn() ?? null);
$request->setBaseDn($this->options->getBaseDn() ?? null);
}

return parent::handleRequest($context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FreeDSx\Ldap\Protocol\ClientProtocolHandler;

use Closure;
use FreeDSx\Ldap\ClientOptions;
use FreeDSx\Ldap\Control\Sync\SyncDoneControl;
use FreeDSx\Ldap\Control\Sync\SyncRequestControl;
use FreeDSx\Ldap\Exception\RuntimeException;
Expand Down Expand Up @@ -53,8 +54,11 @@ class ClientSyncHandler extends ClientBasicHandler

private ?Closure $cookieHandler = null;

public function __construct(private readonly ClientQueue $queue)
{
public function __construct(
private readonly ClientQueue $queue,
private readonly ClientOptions $options,
) {
parent::__construct($this->queue);
}

/**
Expand All @@ -65,7 +69,7 @@ public function handleRequest(ClientProtocolContext $context): ?LdapMessageRespo
/** @var SearchRequest $request */
$request = $context->getRequest();
if ($request->getBaseDn() === null) {
$request->setBaseDn($context->getOptions()->getBaseDn() ?? null);
$request->setBaseDn($this->options->getBaseDn() ?? null);
}

return parent::handleRequest($context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use FreeDSx\Asn1\Exception\EncoderException;
use FreeDSx\Ldap\Protocol\LdapMessageResponse;
use FreeDSx\Ldap\Protocol\Queue\ClientQueue;
use FreeDSx\Socket\Exception\ConnectionException;

/**
Expand All @@ -26,17 +27,21 @@ class ClientUnbindHandler implements RequestHandlerInterface
{
use MessageCreationTrait;

public function __construct(private readonly ClientQueue $queue)
{
}

/**
* {@inheritDoc}
* @throws EncoderException
* @throws ConnectionException
*/
public function handleRequest(ClientProtocolContext $context): ?LdapMessageResponse
{
$queue = $context->getQueue();
$message = $context->messageToSend();
$queue->sendMessage($message);
$queue->close();

$this->queue->sendMessage($message);
$this->queue->close();

return null;
}
Expand Down
43 changes: 32 additions & 11 deletions src/FreeDSx/Ldap/Protocol/Factory/ClientProtocolHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use FreeDSx\Ldap\Protocol\ClientProtocolHandler;
use FreeDSx\Ldap\Protocol\ClientProtocolHandler\RequestHandlerInterface;
use FreeDSx\Ldap\Protocol\ClientProtocolHandler\ResponseHandlerInterface;
use FreeDSx\Ldap\Protocol\Queue\ClientQueue;
use FreeDSx\Ldap\Protocol\Queue\ClientQueueInstantiator;

/**
Expand All @@ -39,15 +40,21 @@ public function __construct(
public function forRequest(Request\RequestInterface $request): RequestHandlerInterface
{
if ($request instanceof Request\SyncRequest) {
return new ClientProtocolHandler\ClientSyncHandler($this->queueInstantiator->make());
return new ClientProtocolHandler\ClientSyncHandler(
queue: $this->queue(),
options: $this->clientOptions,
);
} elseif ($request instanceof Request\SearchRequest) {
return new ClientProtocolHandler\ClientSearchHandler($this->queueInstantiator->make());
return new ClientProtocolHandler\ClientSearchHandler(
queue: $this->queue(),
options: $this->clientOptions,
);
} elseif ($request instanceof Request\UnbindRequest) {
return new ClientProtocolHandler\ClientUnbindHandler();
return new ClientProtocolHandler\ClientUnbindHandler($this->queue());
} elseif ($request instanceof Request\SaslBindRequest) {
return new ClientProtocolHandler\ClientSaslBindHandler();
return new ClientProtocolHandler\ClientSaslBindHandler($this->queue());
} else {
return new ClientProtocolHandler\ClientBasicHandler();
return new ClientProtocolHandler\ClientBasicHandler($this->queue());
}
}

Expand All @@ -57,18 +64,32 @@ public function forResponse(
): ResponseHandlerInterface {
if ($response instanceof Response\SearchResultDone || $response instanceof Response\SearchResultEntry || $response instanceof Response\SearchResultReference) {
return $request instanceof Request\SyncRequest
? new ClientProtocolHandler\ClientSyncHandler($this->queueInstantiator->make())
: new ClientProtocolHandler\ClientSearchHandler($this->queueInstantiator->make());
? new ClientProtocolHandler\ClientSyncHandler(
queue: $this->queue(),
options: $this->clientOptions,
)
: new ClientProtocolHandler\ClientSearchHandler(
queue: $this->queue(),
options: $this->clientOptions,
);
} elseif ($response instanceof Response\SyncInfoMessage) {
return new ClientProtocolHandler\ClientSyncHandler($this->queueInstantiator->make());
return new ClientProtocolHandler\ClientSyncHandler(
queue: $this->queue(),
options: $this->clientOptions,
);
} elseif ($response instanceof Operation\LdapResult && $response->getResultCode() === ResultCode::REFERRAL) {
return new ClientProtocolHandler\ClientReferralHandler($this->clientOptions);
} elseif ($request instanceof Request\ExtendedRequest && $request->getName() === Request\ExtendedRequest::OID_START_TLS) {
return new ClientProtocolHandler\ClientStartTlsHandler($this->queueInstantiator->make());
return new ClientProtocolHandler\ClientStartTlsHandler($this->queue());
} elseif ($response instanceof Response\ExtendedResponse) {
return new ClientProtocolHandler\ClientExtendedOperationHandler();
return new ClientProtocolHandler\ClientExtendedOperationHandler($this->queue());
} else {
return new ClientProtocolHandler\ClientBasicHandler();
return new ClientProtocolHandler\ClientBasicHandler($this->queue());
}
}

private function queue(): ClientQueue
{
return $this->queueInstantiator->make();
}
}
Loading

0 comments on commit 2e0be27

Please sign in to comment.