Skip to content
Snippets Groups Projects
Commit f16c4661 authored by Demian Katz's avatar Demian Katz
Browse files

Fixed bug: Solr URL failover support broke proxy support.

parent cbf24c9d
No related merge requests found
......@@ -252,25 +252,26 @@ class Connector implements \Zend\Log\LoggerAwareInterface
if (count($params) > 0) {
$urlSuffix .= '?' . implode('&', $params->request());
}
$client = $this->createClient(null, 'POST');
switch ($format) {
case 'xml':
$client->setEncType('text/xml; charset=UTF-8');
$body = $document->asXML();
break;
case 'json':
$client->setEncType('application/json');
$body = $document->asJSON();
break;
default:
throw new InvalidArgumentException(
"Unable to serialize to selected format: {$format}"
);
}
$client->setRawBody($body);
$client->getRequest()->getHeaders()
->addHeaderLine('Content-Length', strlen($body));
return $this->trySolrUrls($client, $urlSuffix);
$callback = function ($client) use ($document, $format) {
switch ($format) {
case 'xml':
$client->setEncType('text/xml; charset=UTF-8');
$body = $document->asXML();
break;
case 'json':
$client->setEncType('application/json');
$body = $document->asJSON();
break;
default:
throw new InvalidArgumentException(
"Unable to serialize to selected format: {$format}"
);
}
$client->setRawBody($body);
$client->getRequest()->getHeaders()
->addHeaderLine('Content-Length', strlen($body));
};
return $this->trySolrUrls('POST', $urlSuffix, $callback);
}
/**
......@@ -348,36 +349,34 @@ class Connector implements \Zend\Log\LoggerAwareInterface
$paramString = implode('&', $params->request());
if (strlen($paramString) > self::MAX_GET_URL_LENGTH) {
$method = Request::METHOD_POST;
$callback = function ($client) use ($paramString) {
$client->setRawBody($paramString);
$client->setEncType(HttpClient::ENC_URLENCODED);
$client->setHeaders(['Content-Length' => strlen($paramString)]);
};
} else {
$method = Request::METHOD_GET;
}
if ($method === Request::METHOD_POST) {
$client = $this->createClient(null, $method);
$client->setRawBody($paramString);
$client->setEncType(HttpClient::ENC_URLENCODED);
$client->setHeaders(['Content-Length' => strlen($paramString)]);
} else {
$urlSuffix .= '?' . $paramString;
$client = $this->createClient(null, $method);
$callback = null;
}
$this->debug(sprintf('Query %s', $paramString));
return $this->trySolrUrls($client, $urlSuffix);
return $this->trySolrUrls($method, $urlSuffix, $callback);
}
/**
* Try all Solr URLs until we find one that works (or throw an exception).
*
* @param HttpClient $client Prepared HTTP client
* @param string $urlSuffix Suffix to append to all URLs tried.
* @param string $method HTTP method to use
* @param string $urlSuffix Suffix to append to all URLs tried
* @param Callable $callback Callback to configure client (null for none)
*
* @return string Response body
*
* @throws RemoteErrorException SOLR signaled a server error (HTTP 5xx)
* @throws RequestErrorException SOLR signaled a client error (HTTP 4xx)
*/
protected function trySolrUrls($client, $urlSuffix)
protected function trySolrUrls($method, $urlSuffix, $callback = null)
{
// This exception should never get thrown; it's just a safety in case
// something unanticipated occurs.
......@@ -385,7 +384,10 @@ class Connector implements \Zend\Log\LoggerAwareInterface
// Loop through all base URLs and try them in turn until one works.
foreach ((array)$this->url as $base) {
$client->setUri($base . $urlSuffix);
$client = $this->createClient($base . $urlSuffix, $method);
if (is_callable($callback)) {
$callback($client);
}
try {
return $this->send($client);
} catch (\Exception $ex) {
......@@ -434,7 +436,7 @@ class Connector implements \Zend\Log\LoggerAwareInterface
/**
* Create the HTTP client.
*
* @param string $url Target URL (null to leave unset)
* @param string $url Target URL
* @param string $method Request method
*
* @return HttpClient
......@@ -444,9 +446,7 @@ class Connector implements \Zend\Log\LoggerAwareInterface
$client = new HttpClient();
$client->setAdapter($this->adapter);
$client->setOptions(['timeout' => $this->timeout]);
if (null !== $url) {
$client->setUri($url);
}
$client->setUri($url);
$client->setMethod($method);
if ($this->proxy) {
$this->proxy->proxify($client);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment