Skip to content
Merged
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
29 changes: 26 additions & 3 deletions src/main/php/webservices/rest/RestUpload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'],
Expand All @@ -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;
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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());
}
}
13 changes: 13 additions & 0 deletions src/test/php/webservices/rest/unittest/RestUploadTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '/'));
Expand Down
Loading