Skip to content

Commit bc4e23c

Browse files
committed
Merge pull request #7 from launchdarkly/pk/fork_curl
Pk/fork curl
2 parents f334db5 + e92591b commit bc4e23c

File tree

1 file changed

+15
-73
lines changed

1 file changed

+15
-73
lines changed

src/LaunchDarkly/EventProcessor.php

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -63,86 +63,28 @@ protected function flush() {
6363
return;
6464
}
6565

66-
$socket = $this->createSocket();
67-
68-
if (!$socket) {
69-
error_log("LaunchDarkly unable to open socket");
70-
return;
71-
}
7266
$payload = json_encode($this->_queue);
7367

74-
$body = $this->createBody($payload);
68+
$args = $this->createArgs($payload);
7569

76-
return $this->makeRequest($socket, $body);
70+
return $this->makeRequest($args);
7771
}
7872

79-
private function createSocket() {
80-
if ($this->_socket_failed) {
81-
return false;
82-
}
83-
84-
$protocol = $this->_ssl ? "ssl" : "tcp";
85-
86-
try {
87-
88-
$socket = @pfsockopen($protocol . "://" . $this->_host, $this->_port, $errno, $errstr, $this->_timeout);
89-
90-
if ($errno != 0) {
91-
$this->_socket_failed = true;
92-
return false;
93-
}
94-
95-
return $socket;
96-
} catch (Exception $e) {
97-
error_log("LaunchDarkly caught $e");
98-
$this->socket_failed = true;
99-
return false;
100-
}
101-
}
102-
103-
private function createBody($content) {
104-
$req = "";
105-
$req.= "POST /api/events/bulk HTTP/1.1\r\n";
106-
$req.= "Host: " . $this->_host . "\r\n";
107-
$req.= "Content-Type: application/json\r\n";
108-
$req.= "Authorization: api_key " . $this->_apiKey . "\r\n";
109-
$req.= "User-Agent: PHPClient/" . LDClient::VERSION . "\r\n";
110-
$req.= "Accept: application/json\r\n";
111-
$req.= "Content-length: " . strlen($content) . "\r\n";
112-
$req.= "\r\n";
113-
$req.= $content;
114-
return $req;
73+
private function createArgs($payload) {
74+
$scheme = $this->_ssl ? "https://" : "http://";
75+
$args = " -X POST";
76+
$args.= " -H 'Content-Type: application/json'";
77+
$args.= " -H " . escapeshellarg("Authorization: api_key " . $this->_apiKey);
78+
$args.= " -H 'User-Agent: PHPClient/" . LDClient::VERSION . "'";
79+
$args.= " -H 'Accept: application/json'";
80+
$args.= " -d " . escapeshellarg($payload);
81+
$args.= " " . escapeshellarg($scheme . $this->_host . ":" . $this->_port . "/api/events/bulk");
82+
return $args;
11583
}
11684

117-
private function makeRequest($socket, $req, $retry = true) {
118-
$bytes_written = 0;
119-
$bytes_total = strlen($req);
120-
$closed = false;
121-
122-
while (!$closed && $bytes_written < $bytes_total) {
123-
try {
124-
$written = @fwrite($socket, substr($req, $bytes_written));
125-
} catch (Exception $e) {
126-
error_log("LaunchDarkly caught $e");
127-
$closed = true;
128-
}
129-
if (!isset($written) || !$written) {
130-
$closed = true;
131-
} else {
132-
$bytes_written += $written;
133-
}
134-
}
135-
136-
if ($closed) {
137-
fclose($socket);
138-
if ($retry) {
139-
error_log("LaunchDarkly retrying send");
140-
$socket = $this->createSocket();
141-
if ($socket) return $this->makeRequest($socket, $req, false);
142-
}
143-
return false;
144-
}
145-
85+
private function makeRequest($args) {
86+
$cmd = "/usr/bin/env curl " . $args . ">> /dev/null 2>&1 &";
87+
shell_exec($cmd);
14688
return true;
14789
}
14890

0 commit comments

Comments
 (0)