Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fruux/sabre-http
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Feb 19, 2015
2 parents e6cfa76 + b96b7df commit d0851b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
File renamed without changes.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ sabre/http
This library provides a toolkit to make working with the HTTP protocol easier.

Most PHP scripts run within a HTTP request but accessing information about the
HTTP request is cumbersome at least, mainly do to superglobals and the CGI
standard.
HTTP request is cumbersome at least.

There's bad practices, inconsistencies and confusion. This library is
effectively a wrapper around the following PHP constructs:
Expand Down
26 changes: 18 additions & 8 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function send(RequestInterface $request) {

$response = $this->doRequest($request);

$code = (int)$response->getStatus();
$code = (int) $response->getStatus();

// We are doing in-PHP redirects, because curl's
// FOLLOW_LOCATION throws errors when PHP is configured with
Expand Down Expand Up @@ -133,6 +133,7 @@ function send(RequestInterface $request) {
}

} catch (ClientException $e) {

$this->emit('exception', [$request, $e, &$retry, $retryCount]);

// If retry was still set to false, it means no event handler
Expand All @@ -141,7 +142,9 @@ function send(RequestInterface $request) {
if (!$retry) {
throw $e;
}

}

if ($retry) {
$retryCount++;
}
Expand Down Expand Up @@ -212,6 +215,7 @@ function poll() {
$this->curlMultiHandle,
$messagesInQueue
);

if ($status && $status['msg'] === CURLMSG_DONE) {

$resourceId = intval($status['handle']);
Expand All @@ -221,23 +225,23 @@ function poll() {
$errorCallback,
$retryCount,
) = $this->curlMultiMap[$resourceId];

unset($this->curlMultiMap[$resourceId]);

$curlResult = $this->parseCurlResult(curl_multi_getcontent($status['handle']), $status['handle']);

$retry = false;

if ($curlResult['status'] === self::STATUS_CURLERROR) {

$e = new ClientException($curlResult['curl_errmsg'], $curlResult['curl_errno']);
$this->emit('exception', [$request, $e, &$retry, $retryCount]);

if ($retry) {
$retryCount++;
$this->sendASyncInternal($request, $successCallback, $errorCallback, $retryCount);
goto messageQueue;
}

$curlResult['request'] = $request;

if ($errorCallback) {
$errorCallback($curlResult);
}
Expand All @@ -248,21 +252,30 @@ function poll() {
$this->emit('error:' . $curlResult['http_code'], [$request, $curlResult['response'], &$retry, $retryCount]);

if ($retry) {

$retryCount++;
$this->sendASyncInternal($request, $successCallback, $errorCallback, $retryCount);
goto messageQueue;

}

$curlResult['request'] = $request;

if ($errorCallback) {
$errorCallback($curlResult);
}

} else {

$this->emit('afterRequest', [$request, $curlResult['response']]);

if ($successCallback) {
$successCallback($curlResult['response']);
}

}
}

} while ($messagesInQueue > 0);

return $stillRunning;
Expand Down Expand Up @@ -334,7 +347,6 @@ protected function doRequest(RequestInterface $request) {

curl_setopt_array($this->curlHandle, $settings);
$response = $this->curlExec($this->curlHandle);

$response = $this->parseCurlResult($response, $this->curlHandle);

if ($response['status'] === self::STATUS_CURLERROR) {
Expand Down Expand Up @@ -485,7 +497,6 @@ protected function parseCurlResult($response, $curlHandle) {
unset($response);

// In the case of 100 Continue, or redirects we'll have multiple lists
//
// of headers for each separate HTTP response. We can easily split this
// because they are separated by \r\n\r\n
$headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));
Expand Down Expand Up @@ -519,7 +530,7 @@ protected function parseCurlResult($response, $curlHandle) {
}

/**
* Sends a asynchrous http request.
* Sends an asynchronous HTTP request.
*
* We keep this in a separate method, so we can call it without triggering
* the beforeRequest event and don't do the poll().
Expand Down Expand Up @@ -558,7 +569,6 @@ protected function sendAsyncInternal(RequestInterface $request, callable $succes
*
* @param resource $curlHandle
* @return string
*/
protected function curlExec($curlHandle) {

Expand Down

0 comments on commit d0851b3

Please sign in to comment.