public function testCanForceMultipartUploadsWhenApplying() { $b = new PostBody(); $b->forceMultipartUpload(true); $m = new Request('POST', '/'); $b->applyRequestHeaders($m); $this->assertContains('multipart/form-data', (string) $m->getHeader('Content-Type')); }
/** * Apply POST fields and files to a request to attempt to give an accurate * representation. * * @param RequestInterface $request Request to update * @param array $body Body to apply */ protected function addPostData(RequestInterface $request, array $body) { static $fields = ['string' => true, 'array' => true, 'NULL' => true, 'boolean' => true, 'double' => true, 'integer' => true]; $post = new PostBody(); foreach ($body as $key => $value) { if (isset($fields[gettype($value)])) { $post->setField($key, $value); } elseif ($value instanceof PostFileInterface) { $post->addFile($value); } else { $post->addFile(new PostFile($key, $value)); } } if ($request->getHeader('Content-Type') == 'multipart/form-data') { $post->forceMultipartUpload(true); } $request->setBody($post); }
public function testMultipartWithAmpersandInValue() { $b = new PostBody(); $b->setField('a', 'b&c=d'); $b->forceMultipartUpload(true); $this->assertEquals(['a' => 'b&c=d'], $b->getFields()); $m = new Request('POST', '/'); $b->applyRequestHeaders($m); $this->assertContains('multipart/form-data', $m->getHeader('Content-Type')); $this->assertTrue($m->hasHeader('Content-Length')); $contents = $b->getContents(); $this->assertContains('name="a"', $contents); $this->assertContains('b&c=d', $contents); }
public function testMultipartWithBase64Fields() { $b = new PostBody(); $b->setField('foo64', '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc='); $b->forceMultipartUpload(true); $this->assertEquals(['foo64' => '/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc='], $b->getFields()); $m = new Request('POST', '/'); $b->applyRequestHeaders($m); $this->assertContains('multipart/form-data', (string) $m->getHeader('Content-Type')); $this->assertTrue($m->hasHeader('Content-Length')); $contents = $b->getContents(); $this->assertContains('name="foo64"', $contents); $this->assertContains('/xA2JhWEqPcgyLRDdir9WSRi/khpb2Lh3ooqv+5VYoc=', $contents); }
/** * @param string $uri * @param array $query * @param array $body * @param string $method * @param array $options * @return mixed * @throws CPanelNotFoundException * @throws Exception * @throws WHMEmptyResponseException */ public function send($uri, array $query = array(), array $body = array(), $method = 'GET', $options = array()) { $url = $this->getRequestUrl($uri); $options = $this->getDefaultRequestOptions($options); if ($query) { $options["query"] = $query; } if ($body) { $postBody = new PostBody(); $postBody->forceMultipartUpload(true); $postBody->replaceFields($body); } try { /** @var RequestInterface $request */ $request = $this->client->createRequest($method, $url, $options); $request->setHeaders($this->auth->toArray()); if (!empty($postBody)) { $request->setBody($postBody); } /** @var ResponseInterface $response */ $response = $this->client->send($request); return $response->json(["object" => true]); } catch (RequestException $e) { $response = $e->getResponse(); if ($response) { switch ($response->getStatusCode()) { case 0: throw new CPanelNotFoundException("cPanel Not Found"); case 403: throw new Exception("Invalid Creds"); default: throw new Exception("cPanel Status: " . $response->getStatusCode()); } } else { throw new WHMEmptyResponseException($e->getMessage()); } } catch (Exception $e) { // Try again in case of SSH connection issues // @TODO add retries limitation if (substr_count($e->getMessage(), "SSH")) { return $this->send($uri, $query, $body, $method); } throw new Exception($e->getMessage(), $e->getCode(), $e); } }