From f16c46617e0a8208c30990132ac659f6aa45ab8c Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 15 Sep 2015 12:48:49 -0400
Subject: [PATCH] Fixed bug: Solr URL failover support broke proxy support.

---
 .../VuFindSearch/Backend/Solr/Connector.php   | 74 +++++++++----------
 1 file changed, 37 insertions(+), 37 deletions(-)

diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php
index 0fe16e9700c..19faee1ffe9 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Connector.php
@@ -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);
-- 
GitLab