From 4e3c3f420f7aa9833b18009c8845f969186a72b3 Mon Sep 17 00:00:00 2001 From: Patrick Kaeding Date: Mon, 9 Mar 2015 15:57:48 -0700 Subject: [PATCH 1/6] try forking curl --- src/LaunchDarkly/EventProcessor.php | 87 +++++------------------------ 1 file changed, 14 insertions(+), 73 deletions(-) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index 30d8016dc..3c63ac162 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -63,86 +63,27 @@ protected function flush() { return; } - $socket = $this->createSocket(); - - if (!$socket) { - error_log("LaunchDarkly unable to open socket"); - return; - } $payload = json_encode($this->_queue); - $body = $this->createBody($payload); + $args = $this->createArgs($payload); - return $this->makeRequest($socket, $body); + return $this->makeRequest($args); } - private function createSocket() { - if ($this->_socket_failed) { - return false; - } - - $protocol = $this->_ssl ? "ssl" : "tcp"; - - try { - - $socket = @pfsockopen($protocol . "://" . $this->_host, $this->_port, $errno, $errstr, $this->_timeout); - - if ($errno != 0) { - $this->_socket_failed = true; - return false; - } - - return $socket; - } catch (Exception $e) { - error_log("LaunchDarkly caught $e"); - $this->socket_failed = true; - return false; - } - } - - private function createBody($content) { - $req = ""; - $req.= "POST /api/events/bulk HTTP/1.1\r\n"; - $req.= "Host: " . $this->_host . "\r\n"; - $req.= "Content-Type: application/json\r\n"; - $req.= "Authorization: api_key " . $this->_apiKey . "\r\n"; - $req.= "User-Agent: PHPClient/" . LDClient::VERSION . "\r\n"; - $req.= "Accept: application/json\r\n"; - $req.= "Content-length: " . strlen($content) . "\r\n"; - $req.= "\r\n"; - $req.= $content; - return $req; + private function createArgs($payload) { + $args = " -X POST"; + $args.= " -H 'Content-Type: application/json'"; + $args.= " -H 'Authorization: api_key " . escapeshellcmd($this->_apiKey) . "'"; + $args.= " -H 'User-Agent: PHPClient/" . LDClient::VERSION . "'"; + $args.= " -H 'Accept: application/json'"; + $args.= " -d " . escapeshellcmd($payload); + $args.= " " . escapeshellcmd($this->_host) . "/api/events/bulk"; + return $args; } - private function makeRequest($socket, $req, $retry = true) { - $bytes_written = 0; - $bytes_total = strlen($req); - $closed = false; - - while (!$closed && $bytes_written < $bytes_total) { - try { - $written = @fwrite($socket, substr($req, $bytes_written)); - } catch (Exception $e) { - error_log("LaunchDarkly caught $e"); - $closed = true; - } - if (!isset($written) || !$written) { - $closed = true; - } else { - $bytes_written += $written; - } - } - - if ($closed) { - fclose($socket); - if ($retry) { - error_log("LaunchDarkly retrying send"); - $socket = $this->createSocket(); - if ($socket) return $this->makeRequest($socket, $req, false); - } - return false; - } - + private function makeRequest($args) { + $command = "/usr/bin/env curl " . $args . "> /dev/null 2>&1 &" + shell_exec($command) return true; } From 6410b2c57ce0b4a838f7e63fc5ce0e5d5bb97d97 Mon Sep 17 00:00:00 2001 From: Patrick Kaeding Date: Mon, 9 Mar 2015 16:16:32 -0700 Subject: [PATCH 2/6] moved escaping function around --- src/LaunchDarkly/EventProcessor.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index 3c63ac162..d960091a0 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -73,17 +73,17 @@ protected function flush() { private function createArgs($payload) { $args = " -X POST"; $args.= " -H 'Content-Type: application/json'"; - $args.= " -H 'Authorization: api_key " . escapeshellcmd($this->_apiKey) . "'"; + $args.= " -H 'Authorization: api_key " . $this->_apiKey . "'"; $args.= " -H 'User-Agent: PHPClient/" . LDClient::VERSION . "'"; $args.= " -H 'Accept: application/json'"; - $args.= " -d " . escapeshellcmd($payload); - $args.= " " . escapeshellcmd($this->_host) . "/api/events/bulk"; + $args.= " -d " . $payload; + $args.= " " . $this->_host . "/api/events/bulk"; return $args; } private function makeRequest($args) { - $command = "/usr/bin/env curl " . $args . "> /dev/null 2>&1 &" - shell_exec($command) + $cmd = "/usr/bin/env curl " . escapeshellcmd($args) . "> /dev/null 2>&1 &"; + $out = shell_exec($cmd); return true; } From 567905e1ad8d8dc1edd4b9255bca46cb1edb1107 Mon Sep 17 00:00:00 2001 From: Patrick Kaeding Date: Mon, 9 Mar 2015 16:29:57 -0700 Subject: [PATCH 3/6] make flush a no-op --- src/LaunchDarkly/EventProcessor.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index d960091a0..e37441fc6 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -59,15 +59,16 @@ public function enqueue($event) { } protected function flush() { - if (empty($this->_queue)) { - return; - } + // if (empty($this->_queue)) { + // return; + // } - $payload = json_encode($this->_queue); + // $payload = json_encode($this->_queue); - $args = $this->createArgs($payload); + // $args = $this->createArgs($payload); - return $this->makeRequest($args); + // return $this->makeRequest($args); + return true; } private function createArgs($payload) { @@ -82,7 +83,7 @@ private function createArgs($payload) { } private function makeRequest($args) { - $cmd = "/usr/bin/env curl " . escapeshellcmd($args) . "> /dev/null 2>&1 &"; + $cmd = "/usr/bin/env curl " . escapeshellcmd($args) . ">> /tmp/curl.log 2>&1 &"; $out = shell_exec($cmd); return true; } From 9b5c5aa74a94fc9792ee15787118eb67a6d98dfb Mon Sep 17 00:00:00 2001 From: Patrick Kaeding Date: Mon, 9 Mar 2015 16:56:03 -0700 Subject: [PATCH 4/6] made flush do something again --- src/LaunchDarkly/EventProcessor.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index e37441fc6..5f5f21d69 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -59,16 +59,15 @@ public function enqueue($event) { } protected function flush() { - // if (empty($this->_queue)) { - // return; - // } + if (empty($this->_queue)) { + return; + } - // $payload = json_encode($this->_queue); + $payload = json_encode($this->_queue); - // $args = $this->createArgs($payload); + $args = $this->createArgs($payload); - // return $this->makeRequest($args); - return true; + return $this->makeRequest($args); } private function createArgs($payload) { From 5a594783712c5184a011465c7b2a7e19c69ee85b Mon Sep 17 00:00:00 2001 From: Patrick Kaeding Date: Mon, 9 Mar 2015 17:00:34 -0700 Subject: [PATCH 5/6] increased logging --- src/LaunchDarkly/EventProcessor.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index 5f5f21d69..5a2020913 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -71,18 +71,20 @@ protected function flush() { } private function createArgs($payload) { - $args = " -X POST"; + $scheme = $this->_ssl ? "https://" : "http://"; + $args = " -v -X POST"; $args.= " -H 'Content-Type: application/json'"; $args.= " -H 'Authorization: api_key " . $this->_apiKey . "'"; $args.= " -H 'User-Agent: PHPClient/" . LDClient::VERSION . "'"; $args.= " -H 'Accept: application/json'"; - $args.= " -d " . $payload; - $args.= " " . $this->_host . "/api/events/bulk"; + $args.= " -d '" . $payload . "'"; + $args.= " " . $scheme . $this->_host . ":" . $this->_port . "/api/events/bulk"; return $args; } private function makeRequest($args) { - $cmd = "/usr/bin/env curl " . escapeshellcmd($args) . ">> /tmp/curl.log 2>&1 &"; + error_log("curl args: " . $args); + $cmd = "/usr/bin/env curl " . $args . ">> /tmp/curl.log 2>&1 &"; $out = shell_exec($cmd); return true; } From e1f53ca9f16d7dcfba6e096d20a6c609f2724a51 Mon Sep 17 00:00:00 2001 From: Patrick Kaeding Date: Mon, 9 Mar 2015 17:44:35 -0700 Subject: [PATCH 6/6] fixed shell escaping --- src/LaunchDarkly/EventProcessor.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index 5a2020913..b2c813673 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -72,20 +72,19 @@ protected function flush() { private function createArgs($payload) { $scheme = $this->_ssl ? "https://" : "http://"; - $args = " -v -X POST"; + $args = " -X POST"; $args.= " -H 'Content-Type: application/json'"; - $args.= " -H 'Authorization: api_key " . $this->_apiKey . "'"; + $args.= " -H " . escapeshellarg("Authorization: api_key " . $this->_apiKey); $args.= " -H 'User-Agent: PHPClient/" . LDClient::VERSION . "'"; $args.= " -H 'Accept: application/json'"; - $args.= " -d '" . $payload . "'"; - $args.= " " . $scheme . $this->_host . ":" . $this->_port . "/api/events/bulk"; + $args.= " -d " . escapeshellarg($payload); + $args.= " " . escapeshellarg($scheme . $this->_host . ":" . $this->_port . "/api/events/bulk"); return $args; } private function makeRequest($args) { - error_log("curl args: " . $args); - $cmd = "/usr/bin/env curl " . $args . ">> /tmp/curl.log 2>&1 &"; - $out = shell_exec($cmd); + $cmd = "/usr/bin/env curl " . $args . ">> /dev/null 2>&1 &"; + shell_exec($cmd); return true; }