fix issues with requests for favicons in multi-server setting, a=chris

Chris Pollett [2022-07-10 00:Jul:th]
fix issues with requests for favicons in multi-server setting, a=chris
Filename
src/controllers/SearchController.php
diff --git a/src/controllers/SearchController.php b/src/controllers/SearchController.php
index 6f49aeb54..e3c1c2260 100755
--- a/src/controllers/SearchController.php
+++ b/src/controllers/SearchController.php
@@ -1882,6 +1882,10 @@ EOD;
         } else {
             $nonnet_crawl_items = [];
         }
+        if (!empty($ui_flags['field'])) {
+            return $this->fieldRequest($ui_flags['field'], $crawl_time,
+                $network_crawl_items + $nonnet_crawl_items);
+        }
         $nonnet_crawl_times = array_diff($nonnet_crawl_times,
             $network_crawl_times);
         $all_crawl_times = array_values(array_merge($nonnet_crawl_times,
@@ -1926,42 +1930,6 @@ EOD;
             is_string($crawl_item[self::UI_FLAGS])) {
             $ui_flags = explode(",", $crawl_item[self::UI_FLAGS]);
         }
-        if (!empty($ui_flags['field'])) {
-            $request_field = $ui_flags['field'];
-            if ($request_field == "favicon") {
-                if (!empty($crawl_item[self::THUMB])) {
-                    preg_match('/data\:(image\/[a-zA-Z\-]+)\;base64\,/',
-                        $crawl_item[self::THUMB], $matches);
-                    $icon_code = $matches[0] ?? false;
-                    if (!empty($icon_code)) {
-                        $crawl_item[$request_field] =
-                            base64_decode(substr($crawl_item[self::THUMB],
-                            strlen($icon_code)));
-                        $image_type = $matches[1] ?? false;
-                    }
-                }
-                if (empty($crawl_item[$request_field])) {
-                    $thumb = imagecreatetruecolor(16, 16);
-                    if (!empty($thumb)) {
-                        imagesavealpha($thumb, true);
-                        $trans_colour = imagecolorallocatealpha($thumb,
-                            211, 211, 211, 0);
-                        imagefill($thumb, 0, 0, $trans_colour);
-                        $image_type = "image/png";
-                        ob_start();
-                        imagepng($thumb);
-                        $crawl_item[$request_field] = ob_get_contents();
-                        imagedestroy($thumb);
-                        ob_end_clean();
-                    }
-                }
-                if (!empty($image_type)) {
-                    $this->web_site->header("Content-Type: $image_type");
-                }
-            }
-            echo $crawl_item[$request_field] ?? "";
-            return;
-        }
         $data = [];
         if ($crawl_item == null) {
             if ($cached_link == true) {
@@ -2052,6 +2020,62 @@ EOD;
         }
         echo $new_doc;
     }
+    /**
+     * A fieldRequest is a special kind of cache request in which only
+     * one field (usually, the favicon field) of a crawl item is desired
+     * for a particular crawl time from a list of $crawl_items of
+     * a similar type from several different crawl times. This methof
+     * takes, a $request_field, a $crawl_time, and an associative array
+     * $crawl_items of pairs timestamp => $crawl_item and outputs to the
+     * current stream with appropriate type HTTP headers the desired
+     * field (in the case of favicons (does some processing to make image out
+     * of a data url)).
+     *
+     * @param string $request_field field desire out of crawl_item
+     * @param int $crawl_time timestamp of crawl_item in list of $crawl_items
+     * @param array $crawl_items pairs timestamp => $crawl_item of crawl item
+     *  to look through
+     */
+    public function fieldRequest($request_field, $crawl_time, $crawl_items)
+    {
+        $crawl_item = [];
+        if (!empty($crawl_items[$crawl_time])) {
+            $crawl_item = $crawl_items[$crawl_time];
+        }
+        if ($request_field == "favicon") {
+            if (!empty($crawl_item[self::THUMB])) {
+                preg_match('/data\:(image\/[a-zA-Z\-]+)\;base64\,/',
+                    $crawl_item[self::THUMB], $matches);
+                $icon_code = $matches[0] ?? false;
+                if (!empty($icon_code)) {
+                    $crawl_item[$request_field] =
+                        base64_decode(substr($crawl_item[self::THUMB],
+                        strlen($icon_code)));
+                    $image_type = $matches[1] ?? false;
+                }
+            }
+            if (empty($crawl_item[$request_field])) {
+                $thumb = imagecreatetruecolor(16, 16);
+                if (!empty($thumb)) {
+                    imagesavealpha($thumb, true);
+                    $trans_colour = imagecolorallocatealpha($thumb,
+                        211, 211, 211, 0);
+                    imagefill($thumb, 0, 0, $trans_colour);
+                    $image_type = "image/png";
+                    ob_start();
+                    imagepng($thumb);
+                    $crawl_item[$request_field] = ob_get_contents();
+                    imagedestroy($thumb);
+                    ob_end_clean();
+                }
+            }
+            if (!empty($image_type)) {
+                $this->web_site->header("Content-Type: $image_type");
+            }
+        }
+        echo $crawl_item[$request_field] ?? "";
+        return;
+    }
     /**
      * Makes an HTML web page for an image cache item
      *
ViewGit