Skip to content

Commit dae981a

Browse files
Merge pull request #13 from codelathe/feature/include_debug_option
Included the debug option.
2 parents 76548fd + b258a64 commit dae981a

File tree

1 file changed

+90
-9
lines changed

1 file changed

+90
-9
lines changed

fccloudapi.php

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,10 @@ public function getPolicyName() {
21092109
// -----------------------------------------------------------------------
21102110
class APICore {
21112111

2112+
// request debug
2113+
public $debug = false;
2114+
public $debugMessages = [];
2115+
21122116
public $curl_handle;
21132117
public $server_url;
21142118
public $start_time;
@@ -2118,8 +2122,9 @@ class APICore {
21182122
public $xsrf_not_set;
21192123
//public $cookie_data;
21202124

2121-
public function __construct($SERVER_URL) {
2125+
public function __construct($SERVER_URL, $debug = false) {
21222126
$this->server_url = $SERVER_URL;
2127+
$this->debug = $debug;
21232128
$this->init($SERVER_URL);
21242129
}
21252130

@@ -2184,7 +2189,10 @@ protected function doGET($url) {
21842189
);
21852190
curl_setopt($this->curl_handle, CURLOPT_HTTPHEADER, $headers);
21862191
}
2187-
return curl_exec($this->curl_handle);
2192+
$this->preRequestDebug();
2193+
$result = curl_exec($this->curl_handle);
2194+
$result = $this->afterRequestDebug('GET', $url, '', $result, true);
2195+
return $result;
21882196
}
21892197

21902198
protected function doPOST($url, $postdata) {
@@ -2206,7 +2214,10 @@ protected function doPOST($url, $postdata) {
22062214
// var_dump( $this->cookie_data);
22072215
curl_setopt($this->curl_handle, CURLOPT_HTTPHEADER, $headers);
22082216
}
2209-
return curl_exec($this->curl_handle);
2217+
$this->preRequestDebug();
2218+
$result = curl_exec($this->curl_handle);
2219+
$result = $this->afterRequestDebug('POST', $url, $postdata, $result, true);
2220+
return $result;
22102221
}
22112222

22122223
protected function parseHeader($result)
@@ -2254,6 +2265,70 @@ protected function parseHeader($result)
22542265
return $buffer;
22552266
}
22562267

2268+
protected function preRequestDebug()
2269+
{
2270+
// clean up the debug buffer
2271+
$this->debugMessages = [];
2272+
2273+
if ($this->debug) {
2274+
curl_setopt($this->curl_handle, CURLOPT_HEADER, true);
2275+
curl_setopt($this->curl_handle, CURLINFO_HEADER_OUT, true);
2276+
}
2277+
}
2278+
2279+
protected function afterRequestDebug(string $method, string $url, string $postData, string $result, $removeHeaders = false): string
2280+
{
2281+
if ($this->debug) {
2282+
2283+
// request
2284+
$this->debugMessages['Request'] = "$method $url";
2285+
$body = [];
2286+
parse_str($postData, $body);
2287+
$this->debugMessages['Request Body'] = $body;
2288+
2289+
// request headers
2290+
$rawRequest = curl_getinfo($this->curl_handle, CURLINFO_HEADER_OUT);
2291+
$lines = explode(PHP_EOL, trim($rawRequest));
2292+
array_shift($lines); // remove the first line and keep the headers
2293+
$headers = [];
2294+
foreach ($lines as $line) {
2295+
[$header, $value] = explode(':', $line);
2296+
$headers[trim($header)] = trim($value);
2297+
}
2298+
$this->debugMessages['Request Headers'] = $headers;
2299+
2300+
// request cookies
2301+
$rawCookies = curl_getinfo($this->curl_handle, CURLINFO_COOKIELIST);
2302+
$cookies = [];
2303+
foreach ($rawCookies as $item) {
2304+
$pieces = preg_split('/\s+/', $item);
2305+
$cookies[$pieces[5]] = $pieces[6];
2306+
}
2307+
$this->debugMessages['Request Cookies'] = $cookies;
2308+
2309+
// Response code
2310+
$this->debugMessages['Response Code'] = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE);
2311+
2312+
// Response Headers and body
2313+
[$rawHeaders, $body] = explode(PHP_EOL . PHP_EOL, $result);
2314+
$lines = explode(PHP_EOL, trim($rawHeaders));
2315+
array_shift($lines);
2316+
$headers = [];
2317+
foreach ($lines as $line) {
2318+
[$header, $value] = explode(':', $line);
2319+
$headers[trim($header)] = trim($value);
2320+
}
2321+
$this->debugMessages['Response Headers'] = $headers;
2322+
$body = trim($body);
2323+
$this->debugMessages['Response Body'] = $body;
2324+
2325+
if ($removeHeaders) {
2326+
return $body;
2327+
}
2328+
}
2329+
return $result;
2330+
}
2331+
22572332
protected function doPOSTWithHeader($url, $postdata) {
22582333
//clear token first
22592334
$this->xsrf_token = "";
@@ -2264,7 +2339,10 @@ protected function doPOSTWithHeader($url, $postdata) {
22642339
curl_setopt($this->curl_handle, CURLOPT_POSTFIELDS, $postdata);
22652340
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, 1);
22662341
curl_setopt($this->curl_handle, CURLOPT_HEADER, 1);
2267-
$result = curl_exec($this->curl_handle);
2342+
$this->preRequestDebug();
2343+
$result = curl_exec($this->curl_handle);
2344+
$this->afterRequestDebug('POST', $url, $postdata, $result);
2345+
22682346
return $this->parseHeader($result);
22692347
}
22702348

@@ -2279,7 +2357,10 @@ protected function doPOSTWithAgent($url, $postdata, $agent )
22792357
curl_setopt($this->curl_handle, CURLOPT_POSTFIELDS, $postdata);
22802358
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
22812359
curl_setopt($this->curl_handle, CURLOPT_HEADER, 1);
2282-
$result = curl_exec($this->curl_handle);
2360+
$this->preRequestDebug();
2361+
$result = curl_exec($this->curl_handle);
2362+
$this->afterRequestDebug('POST', $url, $postdata, $result);
2363+
22832364
return $this->parseHeader($result);
22842365
}
22852366

@@ -2371,8 +2452,8 @@ protected function doChunkedUpload($url, $filechunkpath, $filename) {
23712452

23722453
class CloudAPI extends APICore {
23732454

2374-
public function __construct($SERVER_URL) {
2375-
parent::__construct($SERVER_URL);
2455+
public function __construct($SERVER_URL, $debug = false) {
2456+
parent::__construct($SERVER_URL, $debug);
23762457
}
23772458

23782459
public function __destruct() {
@@ -4562,8 +4643,8 @@ public function getfilelistforshare($path, $start = "", $limit = "", $sortby , $
45624643
class CloudAdminAPI extends APICore
45634644
{
45644645

4565-
public function __construct($SERVER_URL) {
4566-
parent::__construct($SERVER_URL);
4646+
public function __construct($SERVER_URL, $debug = false) {
4647+
parent::__construct($SERVER_URL, $debug);
45674648
}
45684649

45694650
public function __destruct() {

0 commit comments

Comments
 (0)