LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

commit 82ac92a98b18f3b654fca001f6a3fe54359bb5c0
parent e2527200c921520f509e9fdcdb0774ff9541f6f5
Author: Ahwx <ahwx@ahwx.org>
Date: Sun, 20 Aug 2023 12:21:38 +0200

fix: gateway timeout on calling fallback instances (merge pull request #16 from davidovski/fix_fallback) Fix Gateway Timeout on fallback

Diffstat:
Mapi.php | 5++++-
Mengines/duckduckgo/text.php | 7+++++++
Mengines/google/text.php | 3+++
Mengines/librex/text.php | 14++++++++------

4 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/api.php b/api.php
@@ -26,7 +26,10 @@
     switch ($type)
     {
         case 0:
-            require "engines/google/text.php";
+            $engine=$config->preferred_engines['text'];
+            if (is_null($engine))
+                $engine = "google";
+            require "engines/$engine/text.php";
             $results = get_text_results($query, $page);
             break;
         case 1:
diff --git a/engines/duckduckgo/text.php b/engines/duckduckgo/text.php
@@ -72,6 +72,13 @@
             curl_multi_exec($mh, $running);
         } while ($running);
 
+        if (curl_getinfo($google_ch)['http_code'] != '200') 
+        {
+            require "engines/librex/text.php";
+            return get_librex_results($query, $page);
+        }
+
+
 
         if ($special_search != 0)
         {
diff --git a/engines/google/text.php b/engines/google/text.php
@@ -169,6 +169,9 @@
     function print_text_results($results)
     {
 
+        if (empty($results))
+            return;
+
         $special = $results[0];
 
         if (array_key_exists("did_you_mean", $special)) 
diff --git a/engines/librex/text.php b/engines/librex/text.php
@@ -4,6 +4,9 @@
     {
         global $config;
 
+        if (isset($_REQUEST["nfb"]) && $_REQUEST["nfb"] == "1")
+            return array();
+
         if (!$config->instance_fallback) 
             return array();
 
@@ -24,16 +27,12 @@
         do {
             $tries++;
 
-            // after "too many" requests, give up
-            if ($tries > 5)
-                return array();
-
             $instance = array_pop($instances);
 
             if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
                 continue;
 
-            $url = $instance . "api.php?q=$query_encoded&p=$page&t=0";
+            $url = $instance . "api.php?q=$query_encoded&p=$page&t=0&nfb=1";
 
             $librex_ch = curl_init($url);
             curl_setopt_array($librex_ch, $config->curl_settings);
@@ -44,7 +43,10 @@
             $code = curl_getinfo($librex_ch)["http_code"];
             $results = json_decode($response, true);
 
-        } while ( $results == null || count($results) <= 1);
+        } while ( !empty($instances) && ($results == null || count($results) <= 1));
+
+        if (empty($instances))
+            return array();
 
         return array_values($results);
     }