Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SolrPhpClient/Apache/Solr/HttpTransport/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class Apache_Solr_HttpTransport_Abstract implements Apache_Solr_HttpTra
* @var float
*/
private $_defaultTimeout = false;

/**
* Get the current default timeout setting (initially the default_socket_timeout ini setting)
* in seconds
Expand Down
26 changes: 26 additions & 0 deletions SolrPhpClient/Apache/Solr/HttpTransport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,33 @@ function __destruct()
// close our curl session
curl_close($this->_curl);
}

public function setAuthenticationCredentials($username, $password)
{
// add the options to our curl handle
curl_setopt_array($this->_curl, array(
CURLOPT_USERPWD => $username . ":" . $password,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
));
}

public function setProxy($proxy, $port, $username = '', $password = '')
{
// add the options to our curl handle
curl_setopt_array($this->_curl, array(
CURLOPT_PROXY => $proxy,
CURLOPT_PROXYPORT => $port
));

if ($username != '' && $password != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
CURLOPT_PROXYUSERPWD => "$username:$password"
));
}
}

public function performGetRequest($url, $timeout = false)
{
// check the timeout value
Expand Down
98 changes: 98 additions & 0 deletions SolrPhpClient/Apache/Solr/HttpTransport/CurlNoReuse.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,27 @@ class Apache_Solr_HttpTransport_CurlNoReuse extends Apache_Solr_HttpTransport_Ab
* SVN ID meta data for this class
*/
const SVN_ID = '$Id:$';

private $_authString = false;
private $_proxy = '';
private $_proxyPort = '';
private $_proxyUsername = '';
private $_proxyPassword = '';

public function setAuthenticationCredentials($username, $password)
{
// this is how curl wants it for the CURLOPT_USERPWD
$this->_authString = $username . ":" . $password;
}

public function setProxy($proxy, $port, $username = '', $password = '')
{
$this->_proxy = $proxy;
$this->_proxyPort = $port;
$this->_proxyUsername = $username;
$this->_proxyPassword = $password;
}

public function performGetRequest($url, $timeout = false)
{
// check the timeout value
Expand Down Expand Up @@ -84,6 +104,32 @@ public function performGetRequest($url, $timeout = false)
// set the timeout
CURLOPT_TIMEOUT => $timeout
));

// set auth if appropriate
if ($this->_authString !== false)
{
curl_setopt_array($curl, array(
CURLOPT_USERPWD => $this->_authString,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
));
}

// set proxy
if ($this->_proxy != '' && $this->_proxyPort != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXY => $this->_proxy,
CURLOPT_PROXYPORT => $this->_proxyPort
));

if ($this->_proxyUsername != '' && $this->_proxyPassword != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
CURLOPT_PROXYUSERPWD => "$this->_proxyUsername:$this->_proxyPassword"
));
}
}

// make the request
$responseBody = curl_exec($curl);
Expand Down Expand Up @@ -130,6 +176,32 @@ public function performHeadRequest($url, $timeout = false)
CURLOPT_TIMEOUT => $timeout
));

// set auth if appropriate
if ($this->_authString !== false)
{
curl_setopt_array($curl, array(
CURLOPT_USERPWD => $this->_authString,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
));
}

// set proxy
if ($this->_proxy != '' && $this->_proxyPort != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXY => $this->_proxy,
CURLOPT_PROXYPORT => $this->_proxyPort
));

if ($this->_proxyUsername != '' && $this->_proxyPassword != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
CURLOPT_PROXYUSERPWD => "$this->_proxyUsername:$this->_proxyPassword"
));
}
}

// make the request
$responseBody = curl_exec($curl);

Expand Down Expand Up @@ -181,6 +253,32 @@ public function performPostRequest($url, $postData, $contentType, $timeout = fal
CURLOPT_TIMEOUT => $timeout
));

// set auth if appropriate
if ($this->_authString !== false)
{
curl_setopt_array($curl, array(
CURLOPT_USERPWD => $this->_authString,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
));
}

// set proxy
if ($this->_proxy != '' && $this->_proxyPort != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXY => $this->_proxy,
CURLOPT_PROXYPORT => $this->_proxyPort
));

if ($this->_proxyUsername != '' && $this->_proxyPassword != '')
{
curl_setopt_array($this->_curl, array(
CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
CURLOPT_PROXYUSERPWD => "$this->_proxyUsername:$this->_proxyPassword"
));
}
}

// make the request
$responseBody = curl_exec($curl);

Expand Down
48 changes: 45 additions & 3 deletions SolrPhpClient/Apache/Solr/HttpTransport/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ class Apache_Solr_HttpTransport_FileGetContents extends Apache_Solr_HttpTranspor
*/
private $_getContext, $_headContext, $_postContext;

/**
* For POST operations, we're already using the Header context value for
* specifying the content type too, so we have to keep our computed
* authorization header around
*
* @var string
*/
private $_authHeader = "";

/**
* Initializes our reuseable get and post stream contexts
*/
Expand All @@ -70,7 +79,40 @@ public function __construct()
$this->_headContext = stream_context_create();
$this->_postContext = stream_context_create();
}

public function setAuthenticationCredentials($username, $password)
{
// compute the Authorization header
$this->_authHeader = "Authorization: Basic " . base64_encode($username . ":" . $password);

// set it now for get and head contexts
stream_context_set_option($this->_getContext, 'http', 'header', $this->_authHeader);
stream_context_set_option($this->_headContext, 'http', 'header', $this->_authHeader);

// for post, it'll be set each time, so add an \r\n so it can be concatenated
// with the Content-Type
$this->_authHeader .= "\r\n";
}

public function setProxy($proxy, $port, $username = '', $password = '')
{
$proxy = "tcp://$proxy:$port";

// set it now for get and head contexts
stream_context_set_option($this->_getContext, 'http', 'proxy', $proxy);
stream_context_set_option($this->_headContext, 'http', 'proxy', $proxy);

if ($username != '' && $password != '')
{
$authProxy = base64_encode("$username:$password");
$proxyAutorization = "Proxy-Authorization: Basic $authProxy";

// set it now for get and head contexts
stream_context_set_option($this->_getContext, 'http', 'header', $proxyAutorization);
stream_context_set_option($this->_headContext, 'http', 'header', $proxyAutorization);
}
}

public function performGetRequest($url, $timeout = false)
{
// set the timeout if specified
Expand All @@ -87,7 +129,7 @@ public function performGetRequest($url, $timeout = false)
// use the default timeout pulled from default_socket_timeout otherwise
stream_context_set_option($this->_getContext, 'http', 'timeout', $this->getDefaultTimeout());
}

// $http_response_headers will be updated by the call to file_get_contents later
// see http://us.php.net/manual/en/wrappers.http.php for documentation
// Unfortunately, it will still create a notice in analyzers if we don't set it here
Expand Down Expand Up @@ -136,8 +178,8 @@ public function performPostRequest($url, $rawPost, $contentType, $timeout = fals
// set HTTP method
'method' => 'POST',

// Add our posted content type
'header' => "Content-Type: $contentType",
// Add our posted content type (and auth header - see setAuthentication)
'header' => "{$this->_authHeader}Content-Type: {$contentType}",

// the posted content
'content' => $rawPost,
Expand Down
24 changes: 23 additions & 1 deletion SolrPhpClient/Apache/Solr/HttpTransport/Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,29 @@ public function getDefaultTimeout();
* @param float $timeout
*/
public function setDefaultTimeout($timeout);


/**
* Set authentication credentials to pass along with the requests.
*
* These will be used to perform HTTP Basic authentication.
*
* @param string $username
* @param string $password
*/
public function setAuthenticationCredentials($username, $password);

/**
* Set proxy.
*
* These will be used to perform HTTP connection througt proxy.
*
* @param string $proxy
* @param string $port
* @param string $username
* @param string $password
*/
public function setProxy($proxy, $port, $username = '', $password = '');

/**
* Perform a GET HTTP operation with an optional timeout and return the response
* contents, use getLastResponseHeaders to retrieve HTTP headers
Expand Down
Loading