Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for compressing post payload #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Example config:
'timeout' => 3,
// the token must match 'upload.token' config in XHGui
'token' => 'token',
// whether to gzip compress the payload
'compress' => true,
),
```

Expand Down
22 changes: 17 additions & 5 deletions src/Saver/UploadSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ class UploadSaver implements SaverInterface
private $url;
/** @var int */
private $timeout;
/** @var bool */
private $compress;

public function __construct($url, $token, $timeout)
public function __construct($url, $token, $timeout, $compress)
{
$this->url = $url;
if ($token) {
$this->url .= '?&token=' . $token;
}

$this->timeout = $timeout;
$this->compress = $compress;
}

public function isSupported()
Expand All @@ -29,16 +32,17 @@ public function isSupported()
public function save(array $data)
{
$json = json_encode($data);
$this->submit($this->url, $json);
$this->submit($this->url, $json, $this->hasCompression());

return true;
}

/**
* @param string $url
* @param string $payload
* @param bool $compress
*/
private function submit($url, $payload)
private function submit($url, $payload, $compress)
{
$ch = curl_init($url);
if (!$ch) {
Expand All @@ -49,13 +53,13 @@ private function submit($url, $payload)
// Prefer to receive JSON back
'Accept: application/json',
// The sent data is JSON
'Content-Type: application/json',
$compress ? 'Content-Type: application/json+gzip' : 'Content-Type: application/json',
);

$res = curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_POSTFIELDS => $compress ? gzencode($payload) : $payload,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => $this->timeout,
Expand All @@ -80,4 +84,12 @@ private function submit($url, $payload)
throw new ProfilerException($message);
}
}

/**
* @return bool
*/
private function hasCompression()
{
return $this->compress && function_exists('gzencode');
}
}
3 changes: 2 additions & 1 deletion src/SaverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public static function create($saveHandler, array $config = array())
'url' => null,
'token' => null,
'timeout' => 3,
'compress' => false,
);
$userConfig = isset($config['save.handler.upload']) && is_array($config['save.handler.upload']) ? $config['save.handler.upload'] : array();
$saverConfig = array_merge($defaultConfig, $userConfig);
$saver = new Saver\UploadSaver($saverConfig['url'] ?: $saverConfig['uri'], $saverConfig['token'], $saverConfig['timeout']);
$saver = new Saver\UploadSaver($saverConfig['url'] ?: $saverConfig['uri'], $saverConfig['token'], $saverConfig['timeout'], $saverConfig['compress']);
break;

case Profiler::SAVER_STACK:
Expand Down