From a10a8fb335e5a817e1a9add49ee179394eea67c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Sun, 26 Dec 2021 19:52:31 +0100 Subject: fixed parser, fixed leak, O(log n) storage - tsearch(3) - 0.0.17 --- src/httpd.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/httpd.c') diff --git a/src/httpd.c b/src/httpd.c index 514f57a..39604ef 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -3,7 +3,7 @@ char * sc_https2http (char * i) { memmove(i+4, i+5, strlen(i)-3); return i; } -char * sc_queryhtml (struct sc_query * q, const char * add_form, size_t l) { /* remember to free returned string in the caller */ /* caller takes care of freeing */ +char * sc_queryhtml (const struct sc_query * q, const char * add_form, size_t l) { /* remember to free returned string in the caller */ /* caller takes care of freeing */ size_t resultshtml_written = 0; size_t resultshtml_sizeof = SC_ALLOC_CHUNK; char * resultshtml = malloc(resultshtml_sizeof); @@ -120,12 +120,12 @@ enum MHD_Result sc_httpd (void * cls, char * location = "//git.sijanec.eu/sijanec/sear.c"; char * content_type = "text/html"; int status_code = MHD_HTTP_OK; - SC_OPT_TYPE opt = 0; + SC_OPT_TYPE opt = SC_OPT_INIT; if (MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "i")) opt |= SC_OPT_IMAGE; if (!host) host = ""; - struct sc_query * q = NULL; + const struct sc_query * q = NULL; 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"); @@ -172,18 +172,30 @@ enum MHD_Result sc_httpd (void * cls, } } else { int already_retried = 0; + const struct sc_query query_to_find = { + .string = (char *) query, + .opt = opt + }; retry: SC_CRLE(c, c->queries_lock); +#ifdef SC_OLD_STORAGE for (size_t i = 0; i < c->queries_length; i++) - if (!strcmp(c->queries[i]->string, query) && c->queries[i]->opt == opt) + if (!sc_query_compar(c->queries[i], &query_to_find)) q = c->queries[i]; +#else /* tfind(3) also requires a pointer to the variable that holds rootp! */ + const struct sc_query ** i_am_retarded = tfind(&query_to_find, &c->qrp, SC_COMPAR_CAST sc_query_compar); + q = i_am_retarded ? *i_am_retarded : NULL; +#endif if (q) { const char * l = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "l"); - response = sc_queryhtml(q, add_form, atoi(l ? l : "0")); /* MHD_create_response_from_buffer will free response (; */ 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! */ + response = SC_I18N_HORSESHOE_RESPONSE; status_code = 307; location = q->results[0]->url ? q->results[0]->url : SC_I18N_NO_HREFLINK; - } + } 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); -- cgit v1.2.3