From e4bedda9ac3854ceb3d7b88a83155558e9ea6062 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 7 Sep 2016 08:34:25 +0200 Subject: [PATCH] debug option to print curl requests --- src/shpub/Cli.php | 15 +++++++++++++-- src/shpub/Command/Like.php | 20 +++++++++++++------- src/shpub/Command/Reply.php | 10 +++++----- src/shpub/Config.php | 7 +++++++ src/shpub/Request.php | 26 ++++++++++++++++++++++++-- 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/shpub/Cli.php b/src/shpub/Cli.php index 34ef479..62f824e 100644 --- a/src/shpub/Cli.php +++ b/src/shpub/Cli.php @@ -35,13 +35,13 @@ class Cli case 'like': $this->requireValidHost(); - $cmd = new Command_Like($this->cfg->host); + $cmd = new Command_Like($this->cfg); $cmd->run($res->command->args['url']); break; case 'reply': $this->requireValidHost(); - $cmd = new Command_Reply($this->cfg->host); + $cmd = new Command_Reply($this->cfg); $cmd->run( $res->command->args['url'], implode(' ', $res->command->args['text']) @@ -83,6 +83,7 @@ class Cli if ($opts['user'] !== null) { $this->cfg->host->user = $opts['user']; } + $this->cfg->setDebug($opts['debug']); return $res; } catch (\Exception $exc) { @@ -124,6 +125,16 @@ class Cli 'default' => null, ) ); + $optParser->addOption( + 'debug', + array( + 'short_name' => '-d', + 'long_name' => '--debug', + 'description' => 'Verbose output', + 'action' => 'StoreTrue', + 'default' => false, + ) + ); $cmd = $optParser->addCommand('connect'); $cmd->addOption( diff --git a/src/shpub/Command/Like.php b/src/shpub/Command/Like.php index 7daafc2..26f615a 100644 --- a/src/shpub/Command/Like.php +++ b/src/shpub/Command/Like.php @@ -4,13 +4,13 @@ namespace shpub; class Command_Like { /** - * @var Config_Host + * @var Config */ - protected $host; + protected $cfg; - public function __construct($host) + public function __construct($cfg) { - $this->host = $host; + $this->cfg = $cfg; } public function run($url) @@ -27,11 +27,17 @@ class Command_Like ] ); - $req = new Request($this->host); + $req = new Request($this->cfg->host, $this->cfg); $res = $req->send($body); $postUrl = $res->getHeader('Location'); - echo "Like created at server\n"; - echo $postUrl . "\n"; + if ($postUrl === null) { + Log::err('Error: Server sent no "Location" header and said:'); + Log::err($res->getBody()); + exit(20); + } else { + echo "Like created at server\n"; + echo $postUrl . "\n"; + } } } ?> diff --git a/src/shpub/Command/Reply.php b/src/shpub/Command/Reply.php index cb09ba7..327111d 100644 --- a/src/shpub/Command/Reply.php +++ b/src/shpub/Command/Reply.php @@ -4,13 +4,13 @@ namespace shpub; class Command_Reply { /** - * @var Config_Host + * @var Config */ - protected $host; + protected $cfg; - public function __construct($host) + public function __construct($cfg) { - $this->host = $host; + $this->cfg = $cfg; } public function run($url, $text) @@ -28,7 +28,7 @@ class Command_Reply ] ); - $req = new Request($this->host); + $req = new Request($this->cfg->host, $this->cfg); $res = $req->send($body); $postUrl = $res->getHeader('Location'); echo "Reply created at server\n"; diff --git a/src/shpub/Config.php b/src/shpub/Config.php index d41e45d..c9ebb4d 100644 --- a/src/shpub/Config.php +++ b/src/shpub/Config.php @@ -12,6 +12,8 @@ class Config */ public $host; + public $debug = false; + protected function getConfigFilePath() { if (!isset($_SERVER['HOME'])) { @@ -114,5 +116,10 @@ class Config } return '"' . $val . '"'; } + + public function setDebug($debug) + { + $this->debug = $debug; + } } ?> diff --git a/src/shpub/Request.php b/src/shpub/Request.php index 2896c81..96d7d44 100644 --- a/src/shpub/Request.php +++ b/src/shpub/Request.php @@ -4,23 +4,30 @@ namespace shpub; class Request { public $req; + public $cfg; - public function __construct($host) + public function __construct($host, $cfg) { + $this->cfg = $cfg; $this->req = new \HTTP_Request2($host->endpoints->micropub, 'POST'); + $this->req->setHeader('User-Agent: shpub'); if (version_compare(PHP_VERSION, '5.6.0', '<')) { //correct ssl validation on php 5.5 is a pain, so disable $this->req->setConfig('ssl_verify_host', false); $this->req->setConfig('ssl_verify_peer', false); } $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded'); - $this->req->setHeader('Authorization', 'Bearer ' . $host->token); + $this->req->setHeader('authorization', 'Bearer ' . $host->token); } public function send($body) { $this->req->setBody($body); + if ($this->cfg->debug) { + $this->printCurl(); + } $res = $this->req->send(); + if (intval($res->getStatus() / 100) != 2) { Log::err( 'Server returned an error status code ' . $res->getStatus() @@ -30,4 +37,19 @@ class Request } return $res; } + + protected function printCurl() + { + $command = 'curl'; + if ($this->req->getMethod() != 'GET') { + $command .= ' -X ' . $this->req->getMethod(); + } + foreach ($this->req->getHeaders() as $key => $val) { + $command .= ' -H ' . escapeshellarg($key . ': ' . $val); + } + $command .= ' --data ' . escapeshellarg($this->req->getBody()); + $command .= ' ' . escapeshellarg((string) $this->req->getUrl()); + + echo $command . "\n"; + } } \ No newline at end of file -- 2.30.2