Skip to content

Commit

Permalink
up: fix stream and fscock client send request error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 16, 2022
1 parent 5fbeb78 commit 4cfd9e1
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 123 deletions.
51 changes: 43 additions & 8 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ abstract class AbstractClient implements ClientInterface
// retry times, when an error occurred.
'retry' => 3,
'method' => 'GET', // 'POST'
'version' => '1.1', // http version
'baseUrl' => '',
'timeout' => 5,
'timeout' => 5, // seconds
// enable SSL verify
'sslVerify' => false,
// request headers
Expand Down Expand Up @@ -123,6 +124,11 @@ abstract class AbstractClient implements ClientInterface
*/
protected array $cookies = [];

/**
* @var array Record debug info on $options['debug'] = true
*/
private array $_debugInfo = [];

/**************************************************************************
* response data
*************************************************************************/
Expand Down Expand Up @@ -186,11 +192,9 @@ public static function driverName(): string
}

/**
* SimpleCurl constructor.
*
* @param array $options
* Class constructor.
*
* @throws RuntimeException
* @param array $options = self::$defaultOptions
*/
public function __construct(array $options = [])
{
Expand Down Expand Up @@ -692,12 +696,15 @@ protected function resetOptions(): static
}

/**
* reset request: headers, cookies, debugInfo
*
* @return $this
*/
public function resetRequest(): static
{
$this->headers = [];
$this->cookies = [];
$this->headers = $this->cookies = [];

$this->_debugInfo = [];
return $this;
}

Expand Down Expand Up @@ -729,10 +736,20 @@ public function resetResponse(): static
return $this;
}

/**
* Reset the request and response info.
*
* @return static
*/
public function resetRuntime(): static
{
return $this->resetRequest()->resetResponse();
}

/**
* Reset the last time headers,cookies,options,response data.
*
* @return $this
* @return static
*/
public function reset(): static
{
Expand Down Expand Up @@ -844,6 +861,24 @@ public function setDebug(mixed $debug): static
return $this;
}

/**
* add debug info
*/
public function addDebugInfo(string $key, mixed $value): void
{
$this->_debugInfo[$key] = $value;
}

/**
* Get debug info on options.debug=true
*
* @return array
*/
public function getDebugInfo(): array
{
return $this->_debugInfo;
}

/**
* @param int $retry
*
Expand Down
12 changes: 9 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
*/
class Client
{
public const DRIVER_CURL = 'curl';
public const DRIVER_FILE = 'file';
public const DRIVER_FOPEN = 'fopen';
public const DRIVER_FSOCK = 'fsock';
public const DRIVER_STREAM = 'stream';

/**
* The supported drivers
*
Expand Down Expand Up @@ -77,8 +83,8 @@ public static function factory(array $config): AbstractClient
$name = $config['driver'] ?? '';
$class = self::$drivers[$name] ?? '';

// auto select driver
if (!$class) {
// auto select
foreach (self::$drivers as $driverClass) {
if ($driverClass::isAvailable()) {
$class = $driverClass;
Expand Down Expand Up @@ -125,7 +131,7 @@ public static function setDefaultDriver(ClientInterface $defaultDriver): void

/**
* @param string $method
* @param array $args
* @param array $args
*
* @return AbstractClient
*/
Expand All @@ -137,7 +143,7 @@ public static function __callStatic(string $method, array $args)
}

if (method_exists($client, $method)) {
return $client->reset()->$method(...$args);
return $client->resetRuntime()->$method(...$args);
}

throw new InvalidArgumentException('call invalid class method: ' . $method);
Expand Down
3 changes: 3 additions & 0 deletions src/ClientConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
class ClientConst
{
public const CONTENT_TYPE = 'Content-Type';
public const USERAGENT = 'User-Agent';

public const HTML = 'text/html';
public const JSON = 'application/json';
public const XML = 'application/xml';
Expand Down
13 changes: 10 additions & 3 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,28 @@ public function request(
public function setResponseCreator(callable $responseCreator): static;

/**
* reset options, request headers, cookies, response data...
* reset all: options, request headers, cookies, response data...
*
* @return static
*/
public function reset(): static;

/**
* Reset request data
* Reset the request and response info.
*
* @return static
*/
public function resetRuntime(): static;

/**
* Reset request data: headers, cookies, debugInfo
*
* @return static
*/
public function resetRequest(): static;

/**
* Reset response data
* Reset response data:
*
* @return static
*/
Expand Down
43 changes: 22 additions & 21 deletions src/Curl/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,28 @@ class CurlClient extends AbstractClient implements CurlClientInterface
* save request and response info, data from curl_getinfo()
*
* @link https://secure.php.net/manual/zh/function.curl-getinfo.php
* contains key:
* "url"
* "content_type"
* "http_code"
* "header_size"
* "request_size"
* "filetime"
* "ssl_verify_result"
* "redirect_count"
* "total_time"
* "namelookup_time"
* "connect_time"
* "pretransfer_time"
* "size_upload"
* "size_download"
* "speed_download"
* "speed_upload"
* "download_content_length"
* "upload_content_length"
* "starttransfer_time"
* "redirect_time"
* @var array = [
* "url" => '',
* "content_type" => '',
* "http_code" => 400,
* "header_size" => '',
* "request_size" => '',
* "filetime" => '',
* "ssl_verify_result" => '',
* "redirect_count" => '',
* "total_time" => '',
* "namelookup_time" => '',
* "connect_time" => '',
* "pretransfer_time" => '',
* "size_upload" => '',
* "size_download" => '',
* "speed_download" => '',
* "speed_upload" => '',
* "download_content_length" => '',
* "upload_content_length" => '',
* "starttransfer_time" => '',
* "redirect_time" => '',
* ]
*/
private array $_responseInfo = [];

Expand Down
6 changes: 6 additions & 0 deletions src/FOpenClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function request(
// set timeout
stream_set_timeout($this->handle, (int)$options['timeout']);

if ($this->isDebug()) {
$this->addDebugInfo('url', $url);
$this->addDebugInfo('options', $options);
$this->addDebugInfo('data', $data);
}

// read response
// $content = \stream_get_contents($this->handle);
while (!feof($this->handle)) {
Expand Down
29 changes: 25 additions & 4 deletions src/FSockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,36 @@ public function request(
throw new ClientException($error, $errno);
}

$string = $this->buildRawHttpData($info, $headers, $options, $data);
$this->buildRequestAndWrite($handle, $info, $headers, $options, $data);

return $this;
}

/**
* @param resource $handle
* @param array $info
* @param array $headers
* @param array $options
* @param mixed $data
*
* @return void
*/
protected function buildRequestAndWrite($handle, array $info, array $headers, array $options, mixed $data): void
{
$timeout = (int)$options['timeout'];
$request = $this->buildRawHttpData($info, $headers, $options, $data);

if ($this->isDebug()) {
$this->addDebugInfo('urlInfo', $info);
$this->addDebugInfo('options', $options);
$this->addDebugInfo('request', $request);
}

// set timeout
stream_set_timeout($handle, $timeout);

// send request
if (false === fwrite($handle, $string)) {
if (false === fwrite($handle, $request)) {
throw new RequestException('send request to server is fail');
}

Expand All @@ -123,8 +146,6 @@ public function request(

// parse raw response
$this->parseResponse();

return $this;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/FileClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function request(
// merge global options data.
$options = array_merge($this->options, $options);

if ($this->isDebug()) {
$this->addDebugInfo('url', $url);
$this->addDebugInfo('options', $options);
$this->addDebugInfo('data', $data);
}

try {
$reqCtx = $this->buildStreamContext($url, $headers, $options, $data);
$fullUrl = UrlHelper::encode2($this->fullUrl);
Expand Down
Loading

0 comments on commit 4cfd9e1

Please sign in to comment.