Skip to content

Commit a969a6d

Browse files
jethronmatus-tomlein
authored andcommitted
Add Emitter Server Anonymization support (#152)
1 parent f312b59 commit a969a6d

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/Constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Constants {
6161
const DEFAULT_SSL = false;
6262
const DEFAULT_REQ_TYPE = "POST";
6363
const NO_RETRY_STATUS_CODES = array(400, 401, 403, 410, 422);
64+
const SERVER_ANONYMIZATION = "SP-Anonymous";
6465

6566
/**
6667
* Settings for the Synchronous Emitter

src/Emitters/CurlEmitter.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CurlEmitter extends Emitter {
3131
private $debug;
3232
private $requests_results;
3333
private $debug_payloads;
34+
private $server_anoymization;
3435

3536
// Curl Specific Parameters
3637

@@ -49,14 +50,17 @@ class CurlEmitter extends Emitter {
4950
* @param int|null $buffer_size - Emitter buffer size
5051
* @param bool $debug - Debug mode
5152
* @param int|null $curl_timeout - Maximum time the request is allowed to take, in seconds
53+
* @param bool|null $server_anonymization - Whether to enable Server Anonymization and not collect the IP or Network User ID. Defaults to false.
5254
*/
53-
public function __construct($uri, $protocol = NULL, $type = NULL, $buffer_size = NULL, $debug = false, $curl_timeout = NULL) {
55+
public function __construct($uri, $protocol = NULL, $type = NULL, $buffer_size = NULL, $debug = false, $curl_timeout = NULL, $server_anonymization = false) {
5456
$this->type = $this->getRequestType($type);
5557
$this->url = $this->getCollectorUrl($this->type, $uri, $protocol);
5658
$this->curl_limit = $this->type == "POST" ? self::CURL_AMOUNT_POST : self::CURL_AMOUNT_GET;
5759
$this->rolling_window = $this->type == "POST" ? self::CURL_WINDOW_POST : self::CURL_WINDOW_GET;
5860
$this->curl_timeout = $curl_timeout;
5961

62+
$this->server_anonymization = $server_anonymization;
63+
6064
// If debug is on create a requests_results array
6165
if ($debug === true) {
6266
$this->debug = true;
@@ -222,19 +226,22 @@ private function rollingCurl($curls, $debug) {
222226
*/
223227
private function getCurlRequest($payload, $type) {
224228
$ch = curl_init($this->url);
229+
$header = array();
225230
if ($type == "POST") {
226-
$header = array(
227-
'Content-Type: '.self::POST_CONTENT_TYPE,
228-
'Accept: '.self::POST_ACCEPT,
229-
'Content-Length: '.strlen($payload));
231+
$header[] = 'Content-Type: '.self::POST_CONTENT_TYPE;
232+
$header[] = 'Accept: '.self::POST_ACCEPT;
233+
$header[] = 'Content-Length: '.strlen($payload);
230234
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
231235
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
232-
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
233236
}
234237
else {
235238
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
236239
curl_setopt($ch, CURLOPT_URL, $this->url."?".$payload);
237240
}
241+
242+
if ($this->server_anonymization) $header[] = self::SERVER_ANONYMIZATION.": *";
243+
244+
if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
238245
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
239246
if ($this->curl_timeout != NULL) {
240247
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);

src/Emitters/SocketEmitter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SocketEmitter extends Emitter {
3636
private $requests_results;
3737
private $max_retry_attempts;
3838
private $retry_backoff_ms;
39+
private $server_anonymization;
3940

4041
// Socket Parameters
4142

@@ -53,14 +54,16 @@ class SocketEmitter extends Emitter {
5354
* @param bool|null $debug - If debug is on
5455
* @param int|null $max_retry_attempts - The maximum number of times to retry a request. Defaults to 1.
5556
* @param int|null $retry_backoff_ms - The number of milliseconds to backoff before retrying a request. Defaults to 100ms.
57+
* @param bool|null $server_anonymization - Whether to enable Server Anonymization and not collect the IP or Network User ID. Defaults to false.
5658
*/
57-
public function __construct($uri, $ssl = NULL, $type = NULL, $timeout = NULL, $buffer_size = NULL, $debug = NULL, $max_retry_attempts = NULL, $retry_backoff_ms = NULL) {
59+
public function __construct($uri, $ssl = NULL, $type = NULL, $timeout = NULL, $buffer_size = NULL, $debug = NULL, $max_retry_attempts = NULL, $retry_backoff_ms = NULL, $server_anonymization = false) {
5860
$this->type = $this->getRequestType($type);
5961
$this->uri = $uri;
6062
$this->ssl = $ssl == NULL ? self::DEFAULT_SSL : (bool) $ssl;
6163
$this->timeout = $timeout == NULL ? self::SOCKET_TIMEOUT : $timeout;
6264
$this->max_retry_attempts = $max_retry_attempts;
6365
$this->retry_backoff_ms = $retry_backoff_ms;
66+
$this->server_anonymization = $server_anonymization;
6467

6568
// If debug is on create a requests_results array
6669
if ($debug === true) {
@@ -240,13 +243,15 @@ private function getRequestBody($uri, $data, $type) {
240243
$req.= "Host: ".$uri."\r\n";
241244
$req.= "Content-Type: ".self::POST_CONTENT_TYPE."\r\n";
242245
$req.= "Content-length: ".strlen($data)."\r\n";
246+
if ($this->server_anonymization) $req.= self::SERVER_ANONYMIZATION.": *\r\n";
243247
$req.= "Accept: ".self::POST_ACCEPT."\r\n\r\n";
244248
$req.= $data."\r\n\r\n";
245249
}
246250
else {
247251
$req = "GET http://".$uri.self::GET_PATH."?".$data." ";
248252
$req.= "HTTP/1.1\r\n";
249253
$req.= "Host: ".$uri."\r\n";
254+
if ($this->server_anonymization) $req.= self::SERVER_ANONYMIZATION.": *\r\n";
250255
$req.= "Query: ".$data."\r\n";
251256
$req.= "\r\n";
252257
}

src/Emitters/SyncEmitter.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class SyncEmitter extends Emitter {
3535
private $requests_results;
3636
private $max_retry_attempts;
3737
private $retry_backoff_ms;
38+
private $server_anonymization;
3839

3940
/**
4041
* Creates a Synchronous Emitter
@@ -46,12 +47,14 @@ class SyncEmitter extends Emitter {
4647
* @param bool|null $debug - If debug is on
4748
* @param int|null $max_retry_attempts - The maximum number of times to retry a request. Defaults to 1.
4849
* @param int|null $retry_backoff_ms - The number of milliseconds to backoff before retrying a request. Defaults to 100ms.
50+
* @param bool|null $server_anonymization - Whether to enable Server Anonymization and not collect the IP or Network User ID. Defaults to false.
4951
*/
50-
public function __construct($uri, $protocol = NULL, $type = NULL, $buffer_size = NULL, $debug = false, $max_retry_attempts = NULL, $retry_backoff_ms = NULL) {
52+
public function __construct($uri, $protocol = NULL, $type = NULL, $buffer_size = NULL, $debug = false, $max_retry_attempts = NULL, $retry_backoff_ms = NULL, $server_anonymization = false) {
5153
$this->type = $this->getRequestType($type);
5254
$this->url = $this->getCollectorUrl($this->type, $uri, $protocol);
5355
$this->max_retry_attempts = $max_retry_attempts;
5456
$this->retry_backoff_ms = $retry_backoff_ms;
57+
$this->server_anonymization = $server_anonymization;
5558

5659
// If debug is on create a requests_results array
5760
if ($debug === true) {
@@ -115,20 +118,23 @@ private function curlRequest($data, $type, $retry_request_manager = NULL) {
115118

116119
// Create a cURL handle, set transfer options and execute
117120
$ch = curl_init($this->url);
121+
$header = array();
118122
if ($type == 'POST') {
119123
$json_data = json_encode($data);
120-
$header = array(
121-
'Content-Type: '.self::POST_CONTENT_TYPE,
122-
'Accept: '.self::POST_ACCEPT,
123-
'Content-Length: '.strlen($json_data));
124+
$header[] = 'Content-Type: '.self::POST_CONTENT_TYPE;
125+
$header[] = 'Accept: '.self::POST_ACCEPT;
126+
$header[] = 'Content-Length: '.strlen($json_data);
124127
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
125128
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
126-
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
127129
}
128130
else {
129131
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
130132
curl_setopt($ch, CURLOPT_URL, $this->url."?".http_build_query($data));
131133
}
134+
135+
if ($this->server_anonymization) $header[] = self::SERVER_ANONYMIZATION.': *';
136+
if ($header) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
137+
132138
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
133139
curl_exec($ch);
134140

0 commit comments

Comments
 (0)