"Number of seconds to limit metadata query run time, 0=unlimited.", 0 },
#define ARGP_KEY_HTTP_ADDR 0x100D
{ "listen-address", ARGP_KEY_HTTP_ADDR, "ADDR", 0, "HTTP address to listen on.", 0 },
+ { "home-redirect", 'h', "URL", 0, "Custom homepage - redirect.", 0 },
+ { "home-html", 'H', "FILE", 0, "Custom homepage - htmlfile.", 0 },
{ NULL, 0, NULL, 0, NULL, 0 },
};
static bool requires_koji_sigcache_mapping = false;
#endif
static unsigned metadata_maxtime_s = 5;
+static string cust_homepage_redirect = "";
+static string cust_homepage_file = "";
static void set_metric(const string& key, double value);
static void inc_metric(const string& key);
addr_info = arg;
break;
// case 'h': argp_state_help (state, stderr, ARGP_HELP_LONG|ARGP_HELP_EXIT_OK);
+ case 'h':
+ cust_homepage_redirect = arg;
+ break;
+ case 'H':
+ cust_homepage_file = arg;
+ break;
default: return ARGP_ERR_UNKNOWN;
}
static struct MHD_Response*
handle_root (off_t* size)
{
+ MHD_Response* r;
+ if (cust_homepage_file != "")
+ try
+ {
+ int fd = open (cust_homepage_file.c_str(), O_RDONLY);
+ if (fd != -1) {
+ struct stat buf;
+ stat (cust_homepage_file.c_str(), &buf);
+ r = MHD_create_response_from_fd(buf.st_size, fd);
+ // NB: MHD owns and handles the fd from now. Must not close()!
+ if (r != NULL)
+ {
+ *size = buf.st_size;
+ add_mhd_response_header (r, "Content-Type", "text/html");
+ }
+ } else {
+ throw libc_exception (errno, "cannot open file " + cust_homepage_file);
+ }
+ return r;
+ }
+ catch (const reportable_exception& e)
+ {
+ e.report(clog);
+ }
+
static string version = "debuginfod (" + string (PACKAGE_NAME) + ") "
- + string (PACKAGE_VERSION);
- MHD_Response* r = MHD_create_response_from_buffer (version.size (),
- (void *) version.c_str (),
- MHD_RESPMEM_PERSISTENT);
+ + string (PACKAGE_VERSION);
+ r = MHD_create_response_from_buffer (version.size (),
+ (void *) version.c_str (),
+ MHD_RESPMEM_PERSISTENT);
if (r != NULL)
{
*size = version.size ();
if (webapi_cors)
// add ACAO header for all successful requests
add_mhd_response_header (r, "Access-Control-Allow-Origin", "*");
- rc = MHD_queue_response (connection, MHD_HTTP_OK, r);
- http_code = MHD_HTTP_OK;
+ if ((cust_homepage_redirect) != "" && (url1 == "/"))
+ {
+ // redirect to given custom --homepage
+ MHD_add_response_header(r, "Location", cust_homepage_redirect.c_str());
+ rc = MHD_queue_response (connection, MHD_HTTP_FOUND, r);
+ http_code = MHD_HTTP_FOUND;
+ }
+ else
+ {
+ rc = MHD_queue_response (connection, MHD_HTTP_OK, r);
+ http_code = MHD_HTTP_OK;
+ }
MHD_destroy_response (r);
}
catch (const reportable_exception& e)
--- /dev/null
+#!/usr/bin/env bash
+#
+# Copyright (C) 2022 Red Hat, Inc.
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+type socat 2>/dev/null || exit 77
+
+. $srcdir/debuginfod-subr.sh # includes set -e
+
+set -x
+
+# This variable is essential and ensures no time-race for claiming ports occurs
+# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
+base=10200
+get_ports
+
+tempfiles vlog$PORT1
+errfiles vlog$PORT1
+
+# Test 1: Make sure attempt to open non-existent --home-html is handled gracefully
+rurl="https://sourceware.org/elfutils/Debuginfod.html"
+env LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS= ${abs_builddir}/../debuginfod/debuginfod \
+ $VERBOSE -p $PORT1 --home-html=non-existent.html --home-redirect=$rurl > vlog$PORT1 2>&1 &
+PID1=$!
+# Server must become ready
+wait_ready $PORT1 'ready' 1
+echo -e 'GET / HTTP/1.1\nHost: localhost\n' | socat - TCP:127.0.0.1:$PORT1 > response.txt
+tempfiles response.txt
+# If non-existent --home-html is passed, server should only send headers
+# incl. the --home-redirect in this case ...
+grep -F "Location: $rurl" response.txt
+# ... followed by the version id.
+tail -1 response.txt | grep -F 'debuginfod'
+kill $PID1
+wait $PID1
+
+# Test 2: Test valid --home-redirect
+echo "<html><body>hiya from debuginfod</body></html>" > home.html
+tempfiles home.html
+rurl="https://sourceware.org/elfutils/Debuginfod.html"
+env LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS= ${abs_builddir}/../debuginfod/debuginfod \
+ $VERBOSE -p $PORT1 --home-html=home.html --home-redirect=$rurl > vlog$PORT1 2>&1 &
+PID1=$!
+# Server must become ready
+wait_ready $PORT1 'ready' 1
+echo -e 'GET / HTTP/1.1\nHost: localhost\n' | socat - TCP:127.0.0.1:$PORT1 > response.txt
+tempfiles response.txt
+grep -F 'hiya from debuginfod' response.txt
+grep -F "Location: $rurl" response.txt
+kill $PID1
+wait $PID1
+
+PID1=0
+exit 0