Skip to content

Commit

Permalink
Added the option to pass an X_FORWARDED_FOR header with the ClientBui…
Browse files Browse the repository at this point in the history
…lder method: withXForwardedFor
  • Loading branch information
Eric Devenport authored and Eric Devenport committed May 13, 2024
1 parent 5c26726 commit 0dc5c31
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class ClientBuilder {
$proxy,
$logger,
$debugMode,
$licenses;
$licenses,
$ip;

public function __construct(Credentials $signer = null) {
$this->serializer = new NativeSerializer();
Expand All @@ -70,6 +71,7 @@ public function __construct(Credentials $signer = null) {
$this->logger = new MyLogger();
$this->debugMode = false;
$this->licenses = [];
$this->ip = null;
}

/**
Expand Down Expand Up @@ -165,6 +167,16 @@ public function withLicenses($licenses) {
return $this;
}

/**
* Allows the caller to include an X-Forwarded-For header in their request, passing on the end user's IP address
* @param string $ip The IP of the end user
* @return $this Returns <b>this</b> to accommodate method chaining.
*/
public function withXForwardedFor($ip) {
$this->ip = $ip;
return $this;
}

public function buildUSAutocompleteApiClient() {
$this->ensureURLPrefixNotNull(self::US_AUTOCOMPLETE_API_URL);
return new USAutoCompleteApiClient($this->buildSender(), $this->serializer);
Expand Down Expand Up @@ -214,7 +226,7 @@ private function buildSender() {
if ($this->httpSender != null)
return $this->httpSender;

$sender = new NativeSender($this->maxTimeout, $this->proxy, $this->debugMode);
$sender = new NativeSender($this->maxTimeout, $this->proxy, $this->debugMode, $this->ip);

$sender = new StatusCodeSender($sender);

Expand Down
10 changes: 8 additions & 2 deletions src/NativeSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class NativeSender implements Sender

private $maxTimeOut,
$proxy,
$debugMode;
$debugMode,
$ip;

public function __construct($maxTimeOut = 10000, Proxy $proxy = null, $debugMode = false)
public function __construct($maxTimeOut = 10000, Proxy $proxy = null, $debugMode = false, $ip = null)
{
$this->maxTimeOut = $maxTimeOut;
$this->proxy = $proxy;
$this->debugMode = $debugMode;
$this->ip = $ip;
}

function send(Request $smartyRequest)
Expand Down Expand Up @@ -80,6 +82,10 @@ private function buildRequest(Request $smartyRequest)

if ($smartyRequest->getReferer() != null)
curl_setopt($ch, CURLOPT_REFERER, $smartyRequest->getReferer());
if ($this->ip != null) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X_FORWARDED_FOR: $this->ip"));
$smartyRequest->setHeader('X_FORWARDED_FOR', $this->ip);
}

return $ch;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/XForwardedForTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace SmartyStreets\PhpSdk\Tests;

require_once(dirname(dirname(__FILE__)) . '/src/Request.php');
require_once(dirname(dirname(__FILE__)) . '/src/Response.php');
require_once(dirname(dirname(__FILE__)) . '/src/NativeSender.php');
require_once(dirname(dirname(__FILE__)) . '/src/Proxy.php');
require_once('Mocks/MockSender.php');
use SmartyStreets\PhpSdk\Proxy;
use SmartyStreets\PhpSdk\Request;
use SmartyStreets\PhpSdk\Response;
use SmartyStreets\PhpSdk\NativeSender;
use SmartyStreets\PhpSdk\Tests\Mocks\MockSender;
use PHPUnit\Framework\TestCase;

class XForwardedForTest extends TestCase {
public function testNativeSetOnQuery() {
$request = new Request();
//$licenses = ["one","two","three"];
//$inner = new MockSender(new Response(123, null, ""));
$sender = new NativeSender(10000, null, false, "0.0.0.0");

$sender->send($request);

$this->assertEquals("0.0.0.0", $request->getHeaders()["X_FORWARDED_FOR"]);
}

public function testNativeNotSet() {
$request = new Request();
//$inner = new MockSender(new Response(123, null, ""));
$sender = new NativeSender();

$sender->send($request);

$this->assertEquals(null, $request->getHeaders()["X_FORWARDED_FOR"]);
}
}

0 comments on commit 0dc5c31

Please sign in to comment.