Skip to content

Commit

Permalink
Implement curl_setopt array
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Feb 21, 2019
1 parent 21af2a0 commit ea29347
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
140 changes: 140 additions & 0 deletions src/FetchInit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
namespace Gt\Fetch;

use Gt\Http\RequestMethod;

/**
* Converts a Fetch Init array to CURLOPT_* key-values.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax
*/
class FetchInit {
protected $curlOptArray;

public function __construct(array $init) {
$this->curlOptArray = [];

foreach($init as $key => $value) {
$function = "set" . ucfirst($key);
if(method_exists($this, $function)) {
call_user_func([$this, $function], $value);
}
else {
// TODO: Throw exception - unknown key.
}
}
}

public function asCurlOptArray():array {
return $this->curlOptArray;
}

/**
* @param string $value The request method, e.g., GET, POST.
*/
protected function setMethod(string $value) {
$this->curlOptArray[CURLOPT_CUSTOMREQUEST] =
RequestMethod::filterMethodName($value);
}

/**
* @param array $headers Any headers you want to add to your request,
* contained within an associative array.
*/
protected function setHeaders(array $headers):void {
$rawHeaders = [];

foreach($headers as $key => $value) {
$rawHeaders []= "$key: $value";
}

$this->curlOptArray[CURLOPT_HTTPHEADER] = $rawHeaders;
}

/**
* @param string $body Any body that you want to add to your request:
* this can be a string, associative array (form data) or binary object.
*/
protected function setBody($body):void {
$this->curlOptArray[CURLOPT_POSTFIELDS] = $body;
}

/**
* @param string $value The mode you want to use for the request,
* e.g., cors, no-cors, or same-origin
*/
protected function setMode(string $value):void {
// TODO: Is this even possible to configure server-side?
}

/**
* @param string $value The request credentials you want to use for the
* request: omit, same-origin, or include. To automatically send
* cookies for the current domain, this option must be provided.
*/
protected function setCredentials(string $value):void {
// TODO: Throw exception until cookies are implemented.
}

/**
* @param string $value The cache mode you want to use for the request.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
*/
protected function setCache(string $value):void {
// TODO: Throw exception until caching is implemented.
}

/**
* @param string $value The redirect mode to use: follow (automatically
* follow redirects) or manual (handle redirects manually).
*/
protected function setRedirect(string $value):void {
$this->curlOptArray[CURLOPT_FOLLOWLOCATION] =
strtolower($value) === "follow";
}

/**
* @param string $value A string specifying no-referrer, client,
* or a URL.
*/
protected function setReferrer(string $value):void {
switch(strtolower($value)) {
case "no-referrer":
$this->curlOptArray[CURLOPT_REFERER] = null;
break;

case "client":
// TODO: What is the significance of "client".
break;

default:
$this->curlOptArray[CURLOPT_REFERER] = $value;
break;
}
}

/**
* @param string $value Specifies the value of the referer HTTP header.
* May be one of no-referrer, no-referrer-when-downgrade, origin,
* origin-when-cross-origin, unsafe-url.
*/
protected function setReferrerPolicy(string $value):void {
// TODO: Need to understand how this works on the server.
}

/**
* @param string $value Contains the subresource integrity value of the
* request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
*/
protected function setIntegrity(string $value):void {
// TODO: Set a value to later check on the request, throwing exception on mismatch.
}

protected function setKeepalive(string $value):void {
$this->curlOptArray[CURLOPT_TCP_KEEPALIVE] = (int)$value;
}

protected function setSignal(string $value):void {
// TODO: Allow passing an AbortController to cancel a fetch early.
}
}
6 changes: 2 additions & 4 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ public function fetch($input, array $init = []):HttpPromise {
// TODO: Set init keys from RequestInterface here.
}

$curlOptArray = [];
foreach($init as $key => $value) {
// TODO: Convert fetch init values to curlOpt values here.
}
$fetchInit = new FetchInit($init);
$curlOptArray = $fetchInit->asCurlOptArray();

$this->requestResolver->add(
$uri,
Expand Down

0 comments on commit ea29347

Please sign in to comment.