diff options
| author | frosty <gabriel@bwaaa.monster> | 2026-05-04 20:42:12 -0400 |
|---|---|---|
| committer | frosty <gabriel@bwaaa.monster> | 2026-05-04 20:43:27 -0400 |
| commit | ba6dae676a5c268d6b4265d26b9556ba1cfa6923 (patch) | |
| tree | a556a64316856f9426924b21d1824632f92c6b84 | |
| parent | a9db276fd872951769451142137a5e0f88ed15bc (diff) | |
| download | omnisearch-ba6dae676a5c268d6b4265d26b9556ba1cfa6923.tar.gz | |
feat: remove domain field from config and derive domain from headers
| -rw-r--r-- | example-config.ini | 1 | ||||
| -rw-r--r-- | src/Config.c | 6 | ||||
| -rw-r--r-- | src/Main.c | 33 | ||||
| -rw-r--r-- | templates/opensearch.xml | 2 |
4 files changed, 30 insertions, 12 deletions
diff --git a/example-config.ini b/example-config.ini index 0ab4016..2760c53 100644 --- a/example-config.ini +++ b/example-config.ini @@ -1,7 +1,6 @@ [server] host = 0.0.0.0 port = 8087 -domain = https://search.example.com # Default locale (default: en_gb) #locale = en_gb diff --git a/src/Config.c b/src/Config.c index 967a4b4..9883d45 100644 --- a/src/Config.c +++ b/src/Config.c @@ -65,11 +65,9 @@ int load_config(const char *filename, Config *config) { config->host[sizeof(config->host) - 1] = '\0'; } else if (strcmp(key, "port") == 0) { config->port = atoi(value); - } else if (strcmp(key, "domain") == 0) { - strncpy(config->domain, value, sizeof(config->domain) - 1); - config->domain[sizeof(config->domain) - 1] = '\0'; } else if (strcmp(key, "locale") == 0) { - strncpy(config->default_locale, value, sizeof(config->default_locale) - 1); + strncpy(config->default_locale, value, + sizeof(config->default_locale) - 1); config->default_locale[sizeof(config->default_locale) - 1] = '\0'; } } else if (strcmp(section, "proxy") == 0) { @@ -19,17 +19,38 @@ #include "Utility/Utility.h" Config global_config; - + int handle_opensearch(UrlParams *params) { (void)params; - extern Config global_config; TemplateContext ctx = new_context(); - context_set(&ctx, "domain", global_config.domain); + + const char *http_host = beaker_get_header("Host"); + if (http_host == NULL) { + http_host = "localhost"; + } + + const char *req_scheme = + "https"; // not sure if it's a good idea to just assume https, but you + // should probably be using https for anything other than testing + // or local network anyways. + + if (strncmp(http_host, "localhost", 9) == 0 || + strncmp(http_host, "127.", 4) == 0 || + strncmp(http_host, "192.168.", 8) == 0 || + strncmp(http_host, "10.", 3) == 0) { + req_scheme = "http"; + } + + context_set(&ctx, "domain", http_host); + context_set(&ctx, "scheme", req_scheme); + char *rendered = render_template("opensearch.xml", &ctx); - serve_data(rendered, strlen(rendered), "application/opensearchdescription+xml"); + serve_data(rendered, strlen(rendered), + "application/opensearchdescription+xml"); free(rendered); free_context(&ctx); + return 0; } @@ -46,7 +67,6 @@ int main() { Config cfg = {.host = DEFAULT_HOST, .port = DEFAULT_PORT, - .domain = "", .default_locale = "en_gb", .proxy = "", .proxy_list_file = "", @@ -75,7 +95,8 @@ int main() { if (loaded > 0) { fprintf(stderr, "[INFO] Loaded %d locales\n", loaded); } else { - fprintf(stderr, "[WARN] No locales loaded (make sure to run from omnisearch directory)\n"); + fprintf(stderr, "[WARN] No locales loaded (make sure to run from " + "omnisearch directory)\n"); } apply_engines_config(cfg.engines); diff --git a/templates/opensearch.xml b/templates/opensearch.xml index 8544b09..14d3760 100644 --- a/templates/opensearch.xml +++ b/templates/opensearch.xml @@ -4,7 +4,7 @@ xmlns:moz="http://www.mozilla.org/2006/browser/search/"> <ShortName>OmniSearch</ShortName> <Description>Lightweight metasearch engine</Description> - <Url type="text/html" method="get" template="{{domain}}/search?q={searchTerms}"/> + <Url type="text/html" method="get" template="{{scheme}}://{{domain}}/search?q={searchTerms}"/> <InputEncoding>UTF-8</InputEncoding> <OutputEncoding>UTF-8</OutputEncoding> <moz:SearchForm>{{domain}}/</moz:SearchForm> |
