Skip to content

Commit

Permalink
Response object.
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 5, 2016
1 parent 50bcb63 commit 173c6b1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,6 @@ public function getResponseCode():int {
return (int)$this->curl->getInfo(CURLINFO_HTTP_CODE);
}

public function getResponse() {
$response = new Response();

// until Response is developed, pass back the Curl object.
return $this->curl;
}

private function curlInit($options = []) {
$defaultOptions = [];

Expand Down Expand Up @@ -169,6 +162,7 @@ private function curlInit($options = []) {
// The returntransfer option MUST be set, otherwise the promise resolution
// callbacks will not be able to get the content of the HTTP requests.
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_HEADER] = true;

$this->curl->setOptArray($options);
}
Expand Down
6 changes: 5 additions & 1 deletion src/RequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public function tick() {
$request = $this->matchRequest($info["handle"]);
if($request->getResponseCode() === 200) {
$requestIndex = array_search($request, $this->requestArray);
$curl = $request->getCurlHandle();
$this->deferredArray[$requestIndex]->resolve(
$request->getResponse()
new Response(
$this->curlMulti->getContent($curl),
$curl->getInfo()
)
);
}

Expand Down
67 changes: 67 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
<?php
namespace phpgt\fetch;

/**
* @property-read bool $bodyUsed Indicates whether the body has been read yet
* @property-read Headers $headers Contains the Headers object associated
* with the response
* @property-read bool $ok Whether the response was successful (status in
* the range 200-299) or not
* @property-read int $status The status code of the response
* @property-read string $statusText The status message corresponding to the
* status code
* @property-read string $type Type of the response (basic, cors, error
* or opaque)
* @property-read string $url The URL of the response
*
*/
class Response {

private $curlInfo;
private $headers = [];
private $body;

public function __construct(string $rawResponse, array $curlInfo) {
$headerSize = $curlInfo["header_size"];
$headerString = substr($rawResponse, 0, $headerSize);
foreach(explode("\n", $headerString) as $line) {
$kvp = explode(":", $line);
if(!isset($kvp[1])) {
continue;
}

$this->headers[trim($kvp[0])] = trim($kvp[1]);
}

$this->body = substr($rawResponse, $headerSize);
$this->curlInfo = $curlInfo;
}

// TODO: Move these functions to the `Body` trait. (Issue #8).

public function arrayBuffer() {

}

public function blob() {

}

public function clone() {

}

public function error() {

}

public function formData() {

}

public function json() {

}

public function redirect() {

}

public function text() {

}
}#

0 comments on commit 173c6b1

Please sign in to comment.