diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2021-08-22 03:18:03 +0200 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2021-08-22 03:18:03 +0200 |
commit | 387fb72ee084aca883b35d37d177958715bffab0 (patch) | |
tree | a203233340481b87dc9690bf8bb3cebea1cbb9c7 /src/httpd.c | |
parent | added i386 (diff) | |
download | sear.c-387fb72ee084aca883b35d37d177958715bffab0.tar sear.c-387fb72ee084aca883b35d37d177958715bffab0.tar.gz sear.c-387fb72ee084aca883b35d37d177958715bffab0.tar.bz2 sear.c-387fb72ee084aca883b35d37d177958715bffab0.tar.lz sear.c-387fb72ee084aca883b35d37d177958715bffab0.tar.xz sear.c-387fb72ee084aca883b35d37d177958715bffab0.tar.zst sear.c-387fb72ee084aca883b35d37d177958715bffab0.zip |
Diffstat (limited to 'src/httpd.c')
-rw-r--r-- | src/httpd.c | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/httpd.c b/src/httpd.c index 6e2c4bf..61c775d 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -1,19 +1,24 @@ -char * sc_queryhtml (struct sc_query * q) { /* remember to free returned string in the caller */ /* caller takes care of locking */ +char * sc_https2http (char * i) { + if (i && strlen(i) >= 4 && i[4] == 's') + memmove(i+4, i+5, strlen(i)-3); + return i; +} +char * sc_queryhtml (struct sc_query * q, const char * h, 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); resultshtml[0] = '\0'; - for (size_t i = 0; i < q->results_length; i++) { + for (size_t i = 0; i < q->results_length && (!l || i < l); i++) { #define SC_HRC(string, wanted) \ if (string##_written+wanted >= string##_sizeof) { \ string##_sizeof = (string##_written+wanted+1)*SC_REALLOC_K; \ string = realloc(string, string##_sizeof); \ } -#define SC_HRF "<div class=result id=result%lu><h4><a href=\"%s\" accesskey=%lu>%s</a> " \ +#define SC_HRF "<div class=result id=result%zu><h4><a href=\"%s\" accesskey=%zu>%s</a> " \ "<span class=breadcrumb>%s</span></h4><p>%s</p></div>" -#define SC_HIF "<a class=result id=result%lu href=\"%s\" accesskey=%lu><img data-title=\"%s\"" \ +#define SC_HIF "<a class=result id=result%zu href=\"%s\" accesskey=%zu><img data-title=\"%s\"" \ "data-breadcrumb=\"%s\" src=\"%s\" /></a>" -#define SC_HRA i, safeurl ? safeurl : SC_I18N_NO_HREFLINK, i, safetitle ? safetitle : SC_I18N_NO_TITLE, \ +#define SC_HRA i, safeurl ? h ? sc_https2http(safeurl) : safeurl : SC_I18N_NO_HREFLINK, i, safetitle ? safetitle : SC_I18N_NO_TITLE, \ safebreadcrumbs ? safebreadcrumbs : safeurl ? safeurl : SC_I18N_NO_HREFLINK, safebody ? safebody : SC_I18N_NO_DESCRIPTION char * safetitle = htmlspecialchars(q->results[i]->title); /* htmlspecialchars returns NULL if input is null */ char * safebody = htmlspecialchars(q->results[i]->desc); @@ -34,7 +39,7 @@ char * sc_queryhtml (struct sc_query * q) { /* remember to free returned string free(safebody); free(safeurl); } -#define SC_HRS SC_I18N_NUMBER_OF_RESULTS ": %ld | " SC_I18N_QUERY_TIME ": %s" +#define SC_HRS SC_I18N_NUMBER_OF_RESULTS ": %zu | " SC_I18N_QUERY_TIME ": %s" char formatted_time[128]; struct tm tm; localtime_r(&q->lookup_time, &tm); @@ -42,12 +47,13 @@ char * sc_queryhtml (struct sc_query * q) { /* remember to free returned string char queryinfo[256]; snprintf(queryinfo, 256, SC_HRS, q->results_length, formatted_time); char * safequery = htmlspecialchars(q->string); - char * response = malloc(strlen((char *) sc_hp)+2*strlen(safequery)+strlen(queryinfo)+strlen(resultshtml)); - sprintf(response, (char *) sc_hp, safequery, safequery, queryinfo, resultshtml); + char * response = malloc(strlen((char *) sc_hp)+2*strlen(safequery)+strlen(queryinfo)+strlen(resultshtml)+strlen("<input type=hidden name=h value=h />")); + sprintf(response, (char *) sc_hp, safequery, safequery, h ? "<input type=hidden name=h value=h />" : "", queryinfo, resultshtml); free(safequery); free(resultshtml); return response; } +#ifdef SC_LOGMEM char * sc_logshtml (struct sc_cache * c) { /* remember to free on caller, remember not to report errors here whilst locked */ char * html = malloc(SC_ALLOC_CHUNK); html[0] = '\0'; @@ -59,8 +65,8 @@ char * sc_logshtml (struct sc_cache * c) { /* remember to free on caller, rememb return NULL; } for (size_t i = 0; i < c->logentries_length; i++) { -#define SC_HLF "<div class=result id=log%lu>[<span class=%s>%s</span>] %s " \ - "<a href=\"" SC_I18N_GIT_URL "/src/branch/master/%s#L%lu\">%s()@%s:%lu</a>: %s</div>" +#define SC_HLF "<div class=result id=log%zu>[<span class=%s>%s</span>] %s " \ + "<a href=\"" SC_I18N_GIT_URL "/src/branch/master/%s#L%zu\">%s()@%s:%zu</a>: %s</div>" #define SC_HLA i, \ sc_log_str(c->logentries[i]->type), \ sc_log_str(c->logentries[i]->type), \ @@ -84,7 +90,8 @@ char * sc_logshtml (struct sc_cache * c) { /* remember to free on caller, rememb pthread_rwlock_unlock(c->logentries_lock); return html; } -int sc_httpd (void * cls, +#endif +enum MHD_Result sc_httpd (void * cls, struct MHD_Connection * connection, const char * url, const char * method, @@ -138,6 +145,7 @@ int sc_httpd (void * cls, sprintf(response, sc_osdd, host); content_type = "application/opensearchdescription+xml"; break; +#ifdef SC_LOGMEM case 'l': /* logs.html */ { char * logshtml = sc_logshtml(c); @@ -146,10 +154,11 @@ int sc_httpd (void * cls, free(logshtml); } break; +#endif } if (!response) { - response = malloc(strlen((char *) sc_hp)+strlen(SC_I18N_HP_HEADING)+strlen(SC_I18N_HP_BODY)); - sprintf(response, (char *) sc_hp, "", "", SC_I18N_HP_HEADING, SC_I18N_HP_BODY); + response = malloc(strlen((char *) sc_hp)+strlen(SC_I18N_HP_HEADING)+strlen(SC_I18N_HP_BODY)+strlen("<input type=hidden name=h value=h />")); + sprintf(response, (char *) sc_hp, "", "", MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "h") ? "<input type=hidden name=h value=h />" : "", SC_I18N_HP_HEADING, SC_I18N_HP_BODY); } } else { int already_retried = 0; @@ -159,7 +168,8 @@ retry: if (!strcmp(c->queries[i]->string, query) && c->queries[i]->opt == opt) q = c->queries[i]; if (q) { - response = sc_queryhtml(q); /* MHD_create_response_from_buffer will free response (; */ + const char * l = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "l"); + response = sc_queryhtml(q, MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "h" /* insecure http */), 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) { status_code = 307; location = q->results[0]->url ? q->results[0]->url : SC_I18N_NO_HREFLINK; |