Skip to content

Commit

Permalink
Fixed Client not rewinding the request body before submitting, fixed …
Browse files Browse the repository at this point in the history
…Stream reporting zero size after writing
  • Loading branch information
biohzrdmx committed Aug 1, 2024
1 parent a7494f7 commit db894bf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vecode/caldera-http",
"description": "HTTP message and factory implementation, part of Vecode Caldera",
"version": "1.2",
"version": "1.3",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
13 changes: 9 additions & 4 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

namespace Caldera\Http;

use CURLFile;
use RuntimeException;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

use Caldera\Http\Response;
use Caldera\Http\Stream;
use CURLFile;
use RuntimeException;

class Client implements ClientInterface {

Expand Down Expand Up @@ -384,10 +385,14 @@ public function sendRequest(RequestInterface $request): ResponseInterface {
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($request->getMethod()));
}
$body = $request->getBody();
if ( $body->getSize() > 0 && $body->isSeekable() ) {
$body->rewind();
}
if ($this->files) {
# Request has files, send them directly
$fields = [];
parse_str($request->getBody()->getContents(), $fields);
parse_str($body->getContents(), $fields);
foreach ($this->files as $name => $file) {
$title = pathinfo($file, PATHINFO_BASENAME);
$mime = mime_content_type($file) ?: null;
Expand All @@ -396,7 +401,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
} else {
# Request hasn't files, just send the body
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody()->getContents());
curl_setopt($ch, CURLOPT_POSTFIELDS, $body->getContents());
}
}
# File download
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function write($string) {
if (! $this->writable ) {
throw new RuntimeException('Cannot write to a non-writable stream');
}
$this->size = 0;
$this->size = null;
if (false === $result = fwrite($this->stream, $string)) {
throw new RuntimeException('Unable to write to stream');
}
Expand Down Expand Up @@ -301,4 +301,4 @@ public function getMetadata($key = null) {
return isset( $meta[$key] ) ? $meta[$key] : null;
}

}
}

0 comments on commit db894bf

Please sign in to comment.