diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2022-01-31 19:16:55 +0100 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2022-01-31 19:16:55 +0100 |
commit | f26b60718163cfe454df368099ca1901acafc047 (patch) | |
tree | 5446a485e682f281ee6b46e02ea41d397b05f186 /src/httpd.c | |
parent | unicode queries now work 0.0.19 (diff) | |
download | sear.c-f26b60718163cfe454df368099ca1901acafc047.tar sear.c-f26b60718163cfe454df368099ca1901acafc047.tar.gz sear.c-f26b60718163cfe454df368099ca1901acafc047.tar.bz2 sear.c-f26b60718163cfe454df368099ca1901acafc047.tar.lz sear.c-f26b60718163cfe454df368099ca1901acafc047.tar.xz sear.c-f26b60718163cfe454df368099ca1901acafc047.tar.zst sear.c-f26b60718163cfe454df368099ca1901acafc047.zip |
Diffstat (limited to 'src/httpd.c')
-rw-r--r-- | src/httpd.c | 60 |
1 files changed, 49 insertions, 11 deletions
diff --git a/src/httpd.c b/src/httpd.c index 39604ef..23f4b60 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -129,6 +129,7 @@ enum MHD_Result sc_httpd (void * cls, char add_form[128]; const char * l = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "l"); const char * h = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "h"); + const char * f = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "f"); snprintf(add_form, 128, "%s%s%d%s", h ? "<input type=hidden name=h value=h />" : "", l ? "<input type=hidden name=l value=" : "<!-- Odgovor na dokončno vprašanje o Življenju, Vesolju in sploh Vsem je ", l ? atoi(l) : 42, @@ -187,24 +188,61 @@ retry: q = i_am_retarded ? *i_am_retarded : NULL; #endif if (q) { - const char * l = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "l"); - if (MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "f") && q->results_length > 0) { - mhdrmm = MHD_RESPMEM_PERSISTENT; /* no need to generate HTML if */ - content_type = "text/plain"; /* we have a feeling of luck! */ + if (f && q->results_length > 0) { + mhdrmm = MHD_RESPMEM_PERSISTENT;/* no need to gen HTML */ + content_type = "text/plain"; /* we have a feeling of luck! */ response = SC_I18N_HORSESHOE_RESPONSE; status_code = 307; - location = q->results[0]->url ? q->results[0]->url : SC_I18N_NO_HREFLINK; +#define LOCHORSE (q->results[0]->url ? q->results[0]->url : SC_I18N_NO_HREFLINK) + char * out = alloca(strlen(LOCHORSE)+1); + strcpy(out, LOCHORSE); + if (h) + sc_https2http(LOCHORSE); + location = out; } else response = sc_queryhtml(q, add_form, atoi(l ? l : "0")); /* MHD_create_response_from_buffer will free response (; */ SC_CUE(c, c->queries_lock); } else { SC_CUE(c, c->queries_lock); - sc_query_google(query, c, NULL, opt); - if (already_retried++) { - char * safequery = htmlspecialchars(query); - response = malloc(strlen((char*) sc_hp)+strlen(safequery)*2+strlen(SC_I18N_HP_ERROR_HEADING)+strlen(SC_I18N_HP_ERROR_BODY)+strlen(add_form)); - sprintf(response, (char *) sc_hp, safequery, safequery, add_form, SC_I18N_HP_ERROR_HEADING, SC_I18N_HP_ERROR_BODY); - free(safequery); + enum sc_return r = sc_query_google(query, c, NULL, opt); + if (already_retried++ || r == SC_CAPTCHA) { + status_code = 570+ABS(r); + if (r == SC_CAPTCHA && strlen(query) < 4096) { + if (getenv("SC_FALLBACK")) { + status_code = 307; + location = alloca(strlen(getenv("SC_FALLBACK")) + + 256 + strlen(query)*3); + sprintf(location, "%sl=%d&q=", getenv("SC_FALLBACK"), + atoi(l ? l : "")); + urlencode(location+strlen(location), query); + if (opt & SC_OPT_IMAGE) + strcat(location, "&i=i"); + if (h) + strcat(location, "&h=h"); + if (f) + strcat(location, "&f=f"); + } + char * safequery = htmlspecialchars(query); + response = malloc(strlen((char*) sc_hp) + + strlen(safequery) * 2 + + strlen(SC_I18N_HP_CAPTCHA_HEADING) + + strlen(SC_I18N_HP_CAPTCHA_BODY) + + strlen(add_form)); + sprintf(response, (char *) sc_hp, safequery, + safequery, add_form, SC_I18N_HP_CAPTCHA_HEADING, + SC_I18N_HP_CAPTCHA_BODY); + free(safequery); + } else { + char * safequery = htmlspecialchars(query); + response = malloc(strlen((char*) sc_hp) + strlen(safequery)*2 + + strlen(SC_I18N_HP_ERROR_HEADING) + + strlen(SC_I18N_HP_ERROR_BODY) + + strlen(add_form)); + sprintf(response, (char *) sc_hp, safequery, safequery, + add_form, SC_I18N_HP_ERROR_HEADING, + SC_I18N_HP_ERROR_BODY); + free(safequery); + } } else goto retry; } } |