commit 23a9cdf5014a318953fd9cbc6095dc1fb01ff0f5
Author: aszlig <aszlig@nix.build>
Date:   Thu Sep 19 00:30:08 2019 +0200

    proxy: Allow use of Unix domain sockets for OSRM
    
    This essentially sets CURLOPT_UNIX_SOCKET_PATH for a few specific API
    requests that go to OSRM and uses a placeholder string that is later
    being replaced by Nix with a PHP array that maps profiles to the
    corresponding Unix domain socket paths where the OSRM backend process is
    running.
    
    Those Unix sockets are only used as soon as the profile URLs are set to
    the special value "internal" so that we have a way to change this at
    runtime (at least for now).
    
    Signed-off-by: aszlig <aszlig@nix.build>

diff --git a/lib/Controller/RoutingProxyController.php b/lib/Controller/RoutingProxyController.php
index 7a69155..bccac25 100644
--- a/lib/Controller/RoutingProxyController.php
+++ b/lib/Controller/RoutingProxyController.php
@@ -22,6 +22,7 @@ use OCP\Http\Client\IClientService;
 
 class RoutingProxyController extends Controller {
     const USER_AGENT = 'Nextcloud Maps (https://github.com/nextcloud/maps)';
+    const UNIX_SOCKET_MAP = @unixSocketMap@;
 
     private $logger;
     private $config;
@@ -85,7 +86,8 @@ class RoutingProxyController extends Controller {
      * remote server.
      */
     private function proxyResponse(string $baseUrl, string $path,
-                                   array $extraQuery = []): Response {
+                                   array $extraQuery = [],
+                                   string $sockPath = null): Response {
         if ($baseUrl === '') {
             $response = new Response();
             $response->setStatus(Http::STATUS_NOT_ACCEPTABLE);
@@ -94,10 +96,14 @@ class RoutingProxyController extends Controller {
         $url = $baseUrl . '/' . ltrim($path, '/');
         $url .= $this->buildQueryStringArg($extraQuery);
         $client = $this->client->newClient();
-        $response = $client->get($url, [
+        $options = [
             'http_errors' => false,
             'headers' => ['User-Agent' => self::USER_AGENT]
-        ]);
+        ];
+        if ($sockPath !== null) {
+            $options['curl'] = [CURLOPT_UNIX_SOCKET_PATH => $sockPath];
+        }
+        $response = $client->get($url, $options);
         return new JSONResponse(json_decode($response->getBody(), true),
                                 $response->getStatusCode());
     }
@@ -126,7 +132,20 @@ class RoutingProxyController extends Controller {
             $response->setStatus(Http::STATUS_BAD_REQUEST);
             return $response;
         }
-        return $this->proxyResponse($url, $path);
+
+        if (
+            $url === 'internal' &&
+            array_key_exists($profile, self::UNIX_SOCKET_MAP)
+        ) {
+            return $this->proxyResponse(
+                'http://dummy.invalid/route/v1/'.$profile,
+                $path,
+                [],
+                self::UNIX_SOCKET_MAP[$profile]
+            );
+        } else {
+            return $this->proxyResponse($url, $path);
+        }
     }
 
     /**
