From a7080e51d4052778ff2b6575b0446492f26289b2 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 28 Mar 2026 15:49:49 +0100 Subject: [PATCH] Add RestUpload::waiting() to set connect and read timeouts --- .../php/webservices/rest/RestUpload.class.php | 29 +++++++++++++++++-- .../rest/unittest/RestUploadTest.class.php | 13 +++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/main/php/webservices/rest/RestUpload.class.php b/src/main/php/webservices/rest/RestUpload.class.php index ce43871..24ccb74 100755 --- a/src/main/php/webservices/rest/RestUpload.class.php +++ b/src/main/php/webservices/rest/RestUpload.class.php @@ -4,10 +4,12 @@ use util\MimeType; use webservices\rest\io\Parts; +/** @test webservices.rest.unittest.RestUploadTest */ class RestUpload { - const BOUNDARY = '---------------boundary1xp6132872336bc4'; + const BOUNDARY= '---------------boundary1xp6132872336bc4'; - private $endpoint, $parts; + private $endpoint, $request; + private $parts= null; /** * Creates a new upload instance @@ -18,7 +20,24 @@ class RestUpload { */ public function __construct($endpoint, $request) { $this->endpoint= $endpoint; - $this->parts= new Parts(self::BOUNDARY, $this->endpoint->open($request->with([ + $this->request= $request; + } + + /** + * Sets request timeouts for reading and connecting + * + * @param ?float $read + * @param ?float $connect + * @return self + */ + public function waiting($read= null, $connect= null) { + $this->request->waiting($read, $connect); + return $this; + } + + /** @return webservices.rest.io.Parts */ + public function open() { + return new Parts(self::BOUNDARY, $this->endpoint->open($this->request->with([ 'Content-Type' => ['multipart/form-data; boundary='.self::BOUNDARY], 'Content-Length' => [], 'Transfer-Encoding' => ['chunked'], @@ -33,6 +52,7 @@ public function __construct($endpoint, $request) { * @return self */ public function pass($name, $value) { + $this->parts??= $this->open(); $this->parts->begin(["Content-Disposition: form-data; name=\"{$name}\""]); $this->parts->write($value); return $this; @@ -48,6 +68,7 @@ public function pass($name, $value) { * @return self */ public function transfer($name, InputStream $in, $filename, $mime= null) { + $this->parts??= $this->open(); $this->parts->begin([ "Content-Disposition: form-data; name=\"{$name}\"; filename=\"{$filename}\"", 'Content-Type: '.($mime ?? MimeType::getByFilename($filename)) @@ -67,6 +88,7 @@ public function transfer($name, InputStream $in, $filename, $mime= null) { * @return io.streams.OutputStream */ public function stream($name, $filename, $mime= null): OutputStream { + $this->parts??= $this->open(); $this->parts->begin([ "Content-Disposition: form-data; name=\"{$name}\"; filename=\"{$filename}\"", 'Content-Type: '.($mime ?? MimeType::getByFilename($filename)) @@ -80,6 +102,7 @@ public function stream($name, $filename, $mime= null): OutputStream { * @return webservices.rest.RestResponse */ public function finish() { + $this->parts??= $this->open(); return $this->endpoint->finish($this->parts->finalize()); } } \ No newline at end of file diff --git a/src/test/php/webservices/rest/unittest/RestUploadTest.class.php b/src/test/php/webservices/rest/unittest/RestUploadTest.class.php index 3a31870..a581c75 100755 --- a/src/test/php/webservices/rest/unittest/RestUploadTest.class.php +++ b/src/test/php/webservices/rest/unittest/RestUploadTest.class.php @@ -16,6 +16,19 @@ public function can_create() { new RestUpload($this->newEndpoint(), new RestRequest('POST', '/')); } + #[Test] + public function waiting() { + $conn= (new RestUpload($this->newEndpoint(), new RestRequest('POST', '/'))) + ->waiting(600, 10) + ->open() + ->finalize() + ->connection() + ; + + Assert::equals(600, $conn->getTimeout()); + Assert::equals(10, $conn->getConnectTimeout()); + } + #[Test] public function pass_parameter() { $upload= new RestUpload($this->newEndpoint(), new RestRequest('POST', '/'));