Skip to content

ComposesRequestContent->toString() and immutable request content #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: 4.x-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to this project will be documented in this file. This project adheres
to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com).

## [4.0.0] - t.b.d

* `AbstractRequest::content` now takes an instance of ComposesRequestContent. Strings can no longer be passed directly as content. Select one of the implementations: JsonData, MultipartFormData or UrlEncodedFormData, or create your own by implementing the interface.
* `AbstractRequest::content` is now nullable. You can create a request with just a scriptFileName and no content.
* `AbstractRequest->setContent()` is removed. Making the request content immutable once set in the constructor.


## [3.1.7] - 2021-12-07

* Make sure length values are within valid bounds
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ $urlEncodedContent = new UrlEncodedFormData(
]
);

$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $urlEncodedContent );
$postRequest = new PostRequest( '/path/to/target/script.php', $urlEncodedContent );

$response = $client->sendRequest( $connection, $postRequest );
```
Expand Down Expand Up @@ -713,7 +713,7 @@ $multipartContent = new MultipartFormData(
]
);

$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $multipartContent );
$postRequest = new PostRequest( '/path/to/target/script.php', $multipartContent );

$response = $client->sendRequest( $connection, $postRequest );
```
Expand Down Expand Up @@ -812,7 +812,7 @@ $jsonContent = new JsonData(
]
);

$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $jsonContent );
$postRequest = new PostRequest( '/path/to/target/script.php', $jsonContent );

$response = $client->sendRequest( $connection, $postRequest );
```
Expand Down
8 changes: 3 additions & 5 deletions bin/examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ function printResponse( ProvidesResponseData $response )

$workerPath = __DIR__ . '/exampleWorker.php';

$request = new PostRequest( $workerPath );

printLine( "\n" );
printLine( 'hollodotme/fast-cgi-client examples', 'blue', true );
printLine( 'Worker script: ' . $workerPath, 'blue' );
Expand All @@ -60,7 +58,7 @@ function printResponse( ProvidesResponseData $response )
printLine( 'CODE: $client->sendRequest( $request );', 'red' );
printLine( "\n" );

$request->setContent( new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single synchronous request'] ) );
$request = new PostRequest( $workerPath, new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single synchronous request'] ) );

sleep( 2 );

Expand All @@ -76,7 +74,7 @@ function printResponse( ProvidesResponseData $response )
printLine( 'CODE: $client->sendAsyncRequest( $request );', 'red' );
printLine( "\n" );

$request->setContent( new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request'] ) );
$request = new PostRequest( $workerPath, new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request'] ) );

sleep( 2 );

Expand Down Expand Up @@ -110,7 +108,7 @@ function printResponse( ProvidesResponseData $response )
printLine( ' );', 'red' );
printLine( "\n" );

$request->setContent( new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request with callback'] ) );
$request = new PostRequest( $workerPath, new UrlEncodedFormData( ['sleep' => 1, 'key' => 'single asynchronous request with callback'] ) );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response )
{
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/ComposesRequestContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ interface ComposesRequestContent
{
public function getContentType() : string;

public function getContent() : string;
public function toString() : string;
}
2 changes: 1 addition & 1 deletion src/RequestContents/JsonData.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getContentType() : string
* @return string
* @throws RuntimeException
*/
public function getContent() : string
public function toString() : string
{
$json = json_encode( $this->data, $this->encodingOptions, $this->encodingDepth );

Expand Down
2 changes: 1 addition & 1 deletion src/RequestContents/MultipartFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getContentType() : string
return 'multipart/form-data; boundary=' . self::BOUNDARY_ID;
}

public function getContent() : string
public function toString() : string
{
$data = [];

Expand Down
2 changes: 1 addition & 1 deletion src/RequestContents/UrlEncodedFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getContentType() : string
return 'application/x-www-form-urlencoded';
}

public function getContent() : string
public function toString() : string
{
return http_build_query( $this->formData );
}
Expand Down
36 changes: 12 additions & 24 deletions src/Requests/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ abstract class AbstractRequest implements ProvidesRequestData

private string $contentType = 'application/x-www-form-urlencoded';

private int $contentLength = 0;

private ?ComposesRequestContent $content;

/** @var array<string, mixed> */
Expand All @@ -54,10 +52,10 @@ abstract class AbstractRequest implements ProvidesRequestData
public function __construct( string $scriptFilename, ?ComposesRequestContent $content = null )
{
$this->scriptFilename = $scriptFilename;
$this->content = $content;

if (null !== $content) {
$this->setContent( $content );
$this->setContentType( $content->getContentType() );
$this->contentType = $content->getContentType();
}
}

Expand Down Expand Up @@ -131,11 +129,6 @@ public function setServerProtocol( string $serverProtocol ) : void
$this->serverProtocol = $serverProtocol;
}

public function getContentType() : string
{
return $this->contentType;
}

public function setContentType( string $contentType ) : void
{
$this->contentType = $contentType;
Expand All @@ -146,17 +139,17 @@ public function getContent() : ?ComposesRequestContent
return $this->content;
}

public function setContent( ComposesRequestContent $content ) : void
{
$this->content = $content;
$this->contentLength = strlen( $content->getContent() );
}
public function getContentLength() : int
{
return $this->content ? strlen($this->content->toString()) : 0;
}

/**
* @param string $key
* @param mixed $value
*/
public function setCustomVar( string $key, $value ) : void
public function getContentType() : string
{
return $this->contentType;
}

public function setCustomVar( string $key, mixed $value ) : void
{
$this->customVars[ $key ] = $value;
}
Expand Down Expand Up @@ -192,11 +185,6 @@ public function getScriptFilename() : string
return $this->scriptFilename;
}

public function getContentLength() : int
{
return $this->contentLength;
}

/**
* @return array<string, mixed>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Sockets/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private function getRequestPackets( ProvidesRequestData $request ) : string
$requestPackets .= $this->packetEncoder->encodePacket(
self::STDIN,
substr(
$request->getContent()->getContent(),
$request->getContent()->toString(),
$offset,
self::REQ_MAX_CONTENT_SIZE
),
Expand Down
51 changes: 24 additions & 27 deletions tests/Integration/Async/AsyncRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public function testAsyncRequestsWillRespondToCallbackIfRequestsExceedPhpFpmMaxC
$results = [];
$expectedResults = range( 0, $limit - 1 );

$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response ) use ( &$results )
{
$results[] = (int)$response->getBody();
}
);


for ( $i = 0; $i < $limit; $i++ )
{
$request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) );
$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response ) use ( &$results )
{
$results[] = (int)$response->getBody();
}
);

$client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request );
}
Expand Down Expand Up @@ -105,17 +105,15 @@ public function testAsyncRequestsWillRespondToCallbackIfRequestsExceedPhpFpmMaxC
$results = [];
$expectedResults = range( 0, $limit - 1 );

$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response ) use ( &$results )
{
$results[] = (int)$response->getBody();
}
);

for ( $i = 0; $i < $limit; $i++ )
{
$request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) );
$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response ) use ( &$results )
{
$results[] = (int)$response->getBody();
}
);

$client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request );
}
Expand Down Expand Up @@ -161,11 +159,10 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil
$results = [];
$expectedResults = range( 0, $limit - 1 );

$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' );

for ( $i = 0; $i < $limit; $i++ )
{
$request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) );
$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) );

$client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request );
}
Expand Down Expand Up @@ -203,17 +200,17 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil
$results = [];
$expectedResults = range( 0, $limit - 1 );

$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php' );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response ) use ( &$results )
{
$results[] = (int)$response->getBody();
}
);


for ( $i = 0; $i < $limit; $i++ )
{
$request->setContent( new UrlEncodedFormData( ['test-key' => $i] ) );
$request = new PostRequest( dirname( __DIR__ ) . '/Workers/worker.php', new UrlEncodedFormData( ['test-key' => $i] ) );
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response ) use ( &$results )
{
$results[] = (int)$response->getBody();
}
);

$client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/FileUpload/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testCanUploadFiles( array $files ) : void
];

$multipartFormData = new MultipartFormData( $formData, $files );
$postRequest = PostRequest::newWithRequestContent(
$postRequest = new PostRequest(
dirname( __DIR__ ) . '/Workers/fileUploadWorker.php',
$multipartFormData
);
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/NetworkSocket/NetworkSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function testCanReadResponses() : void

$socketIdOne = $this->client->sendAsyncRequest( $this->connection, $request );

$request->setContent( new UrlEncodedFormData( ['test-key' => 'test'] ) );
$request = new PostRequest( $this->getWorkerPath( 'worker.php' ), new UrlEncodedFormData( ['test-key' => 'test'] ) );

$socketIdTwo = $this->client->sendAsyncRequest( $this->connection, $request );

Expand Down Expand Up @@ -418,12 +418,12 @@ static function ( $buffer ) use ( $unitTest, $data, &$passCounter )
*/
public function testCanGetLengthOfSentContent( int $length ) : void
{
$content = str_repeat( 'a', $length );
$content = new UrlEncodedFormData(['test' => str_repeat( 'a', $length )]);
$request = new PostRequest( $this->getWorkerPath( 'lengthWorker.php' ), $content );
$request->setContentType( '*/*' );
$result = $this->client->sendRequest( $this->connection, $request );

self::assertEquals( $length, $result->getBody() );
self::assertEquals( $length + 5, $result->getBody() );
}

/**
Expand Down
Loading