diff options
Diffstat (limited to '')
-rw-r--r-- | src/api.c | 6 | ||||
-rw-r--r-- | src/hp.html | 11 | ||||
-rw-r--r-- | src/httpd.c | 38 | ||||
-rw-r--r-- | src/log.c | 23 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/structs.c | 27 |
6 files changed, 67 insertions, 40 deletions
@@ -142,11 +142,7 @@ struct sc_query * sc_query_google (const char * s, struct sc_cache * c, struct s * result dates are sometimes relative ("an hour ago") and heavily depend on the client location, based on IP. - we won't parse those yet * I couldn't find anything with ratings, so we won't parse thouse either yet - * captcha: google knows that this nokia phone we're pretending to be doesn't support javascript - - the request limiting captcha must work on a phone without javascript. it is probably loaded inside an iframe, but has - origin protection, so we can't just solve it client-side. we would have to proxy images and create some sort of a session - based http request-response based user interface so we can ask the user to complete the captcha. this is not yet - implemeted and will be hard work. + * captcha: google knows that this nokia phone we're pretending to be doesn't support javascript, but does not care, and loads an obfuscated captcha anyways that would be hard to defeat for now without some kind of chromium emulation we really don't want. */ int rs = 1; char * xpath = NULL; diff --git a/src/hp.html b/src/hp.html index 7d1307b..e1ab301 100644 --- a/src/hp.html +++ b/src/hp.html @@ -1,14 +1,16 @@ <!DOCTYPE html> <html lang=sl> <!-- this file is a printf format. be sure to escape percent signs with percent percent. --> - <!-- this format requires the following types (in order): query string, query string, result info string, results html string --> + <!-- this format requires the following types (in order): query string, query string, additional form elements, result info string, results html string --> <head> <meta charset=UTF-8 /> <title> %s :: sear.c </title> <meta name=viewport content="width=device-width, initial-scale=1.0"> - <link rel=stylesheet href=//sijanec.eu/assets/css/styles.css?ref=sear.c /> <!-- TODO: direktno vstavljanje v dokument --> + <style> + INSERT-STYLESHEET-HERE + </style> <link rel="shortcut icon" href=data:image/x-icon;, type=image/x-icon> <!-- prevents favicon lookups --> <link rel=icon href=data:;base64,iVBORw0KGgo=> <link rel=search type=application/opensearchdescription+xml href=/osdd.xml> @@ -67,12 +69,13 @@ </style> </head> <body> - <form class=container> + <form class=container action=.> <input accesskey=4 type=text name=q value="%s" placeholder="sear.c ..." /> <!-- see www.standardaccesskeys.com --> <input type=submit value=🔍 /> <!-- magnifying glass emoji --> <input type=submit name=f value=Ʊ /> <!-- horseshoe unicode character --> <input type=submit name=i value=🖼 /> <!-- framed picture emoji - img search --> - <input type=submit name=v value=🎬 hidden=hidden /> <!-- that thing they use in movies - vid search N/I --> + <!-- <input type=submit name=v value=🎬/> --> <!-- that thing they use in movies - vid search N/I --> + %s </form> <h3> %s 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; @@ -13,6 +13,7 @@ const char * sc_log_str (int t) { } /* interestingly, gcc figures out there's no way for code to reach this section, therefore there's no warning "-Wreturn-type" */ } +#ifdef SC_LOGMEM int sc_logentry_free (struct sc_logentry * l) { free(l->message); l->message = NULL; free(l); @@ -22,8 +23,11 @@ struct sc_logentry * sc_logentry_init () { struct sc_logentry * l = calloc(1, sizeof(struct sc_logentry)); return l; } +#endif int sc_push_log (unsigned char t, struct sc_cache * c, const char * ca, char * f, size_t l, unsigned short int isf, char * m, ...) { #define SC_PLL c->logentries[c->logentries_length-1] + char * compiled_message = NULL; +#ifdef SC_LOGMEM if (!c) return -1; pthread_rwlock_t * lock = c->logentries_lock; @@ -34,6 +38,7 @@ int sc_push_log (unsigned char t, struct sc_cache * c, const char * ca, char * f if (c->logentries_sizeof <= c->logentries_length) SC_BIGGER_ARRAY(c->logentries, sc_logentry, 1); c->logentries_length++; +#endif size_t strlenm = strlen(m); size_t va_count = parse_printf_format(m, 0, NULL); if (isf && va_count > 0) { @@ -41,21 +46,29 @@ int sc_push_log (unsigned char t, struct sc_cache * c, const char * ca, char * f va_start(ap, m); va_copy(ap2, ap); strlenm = vsnprintf(NULL, 0, m, ap); - SC_PLL->message = malloc(sizeof(char)*strlenm+1); - vsnprintf(SC_PLL->message, strlenm+1, m, ap2); + compiled_message = malloc(sizeof(char)*strlenm+1); + vsnprintf(compiled_message, strlenm+1, m, ap2); va_end(ap); va_end(ap2); } else { - SC_PLL->message = malloc(sizeof(char)*strlenm+1); - strcpy(SC_PLL->message, m); + compiled_message = malloc(sizeof(char)*strlenm+1); + strcpy(compiled_message, m); } +#ifdef SC_LOGMEM SC_PLL->file = f; SC_PLL->line = l; SC_PLL->function = ca; SC_PLL->time = time(NULL); SC_PLL->type = t; - fprintf(stderr, "[sear.c] %s %s()@%s:%lu: %s\n", sc_log_str(t), ca, f, l, SC_PLL->message); /* in posix, this is thread safe */ + SC_PLL->message = compiled_message; +#endif + fprintf(stderr, "[sear.c] %s %s()@%s:%zu: %s\n", sc_log_str(t), ca, f, l, compiled_message); /* in posix, this is thread safe */ +#ifdef SC_LOGMEM if (lock && pthread_rwlock_unlock(lock)) return -4; +#endif +#ifndef SC_LOGMEM + free(compiled_message); +#endif return 1; } @@ -72,7 +72,7 @@ int main (int argc, char ** argv) { fflush(stderr); rc: xmlCleanupParser(); + MHD_stop_daemon(d); /* stop the daemon first and the free, threads might still be running */ sc_cache_free(c); - MHD_stop_daemon(d); return rs; } diff --git a/src/structs.c b/src/structs.c index dce460e..83d19b9 100644 --- a/src/structs.c +++ b/src/structs.c @@ -19,6 +19,8 @@ } while (0); #define SC_OPT_TYPE unsigned char #define SC_OPT_IMAGE (1 << 0) +#define SC_STR(x) #x +#ifdef SC_LOGMEM struct sc_logentry { unsigned char type; /* SC_LOG_ERROR, SC_LOG_WARNING, SC_LOG_INFO, SC_LOG_DEBUG */ size_t line; @@ -29,7 +31,7 @@ struct sc_logentry { }; int sc_logentry_free (struct sc_logentry * l); /* defined in log.c */ struct sc_logentry * sc_logentry_init (); /* defined in log.c */ - +#endif struct sc_result { struct sc_query * query; /* nofree - free from sc_cache */ char * url; /* yesfree - url of referer page when image searching */ @@ -90,38 +92,41 @@ int sc_query_free (struct sc_query * q) { struct sc_cache { SC_IN_STRUCT_ARRAY(struct sc_query, queries); /* yesfree */ pthread_rwlock_t * queries_lock; +#ifdef SC_LOGMEM SC_IN_STRUCT_ARRAY(struct sc_logentry, logentries); /* yesfree */ pthread_rwlock_t * logentries_lock; +#endif }; struct sc_cache * sc_cache_init() { +#define SC_CILI(name) do { name##_lock = malloc(sizeof(pthread_rwlock_t)); pthread_rwlock_init(name##_lock, NULL); } while (0) struct sc_cache * c = calloc(1, sizeof(struct sc_cache)); c->queries_sizeof = SC_ALLOC_CHUNK; - c->logentries_sizeof = SC_ALLOC_CHUNK; c->queries = calloc(c->queries_sizeof, sizeof(struct sc_query *)); +#ifdef SC_LOGMEM + c->logentries_sizeof = SC_ALLOC_CHUNK; c->logentries = calloc(c->logentries_sizeof, sizeof(struct sc_logentry *)); - for (size_t i = 0; i < c->logentries_sizeof; i++) { - /* c->queries[i] = sc_query_init(); */ /* queries are not inited for performance reasons, they are inited by query function */ - /* c->queries[i]->cache = c; */ + for (size_t i = 0; i < c->logentries_sizeof; i++) c->logentries[i] = sc_logentry_init(); - } -#define SC_CILI(name) do { name##_lock = malloc(sizeof(pthread_rwlock_t)); pthread_rwlock_init(name##_lock, NULL); } while (0) - SC_CILI(c->queries); SC_CILI(c->logentries); +#endif + SC_CILI(c->queries); return c; } int sc_cache_free(struct sc_cache * c) { + #define SC_CFLD(name) do { pthread_rwlock_destroy(name##_lock); free(name##_lock); } while(0) if (!c) return -1; - fprintf(stderr, "c->queries_sizeof = %lu\n", c->queries_sizeof); + fprintf(stderr, "c->queries_sizeof = %zu\n", c->queries_sizeof); for (size_t i = 0; i < c->queries_sizeof; i++) sc_query_free(c->queries[i]); free(c->queries); +#ifdef SC_LOGMEM for (size_t i = 0; i < c->logentries_sizeof; i++) sc_logentry_free(c->logentries[i]); + SC_CFLD(c->logentries); free(c->logentries); - #define SC_CFLD(name) do { pthread_rwlock_destroy(name##_lock); free(name##_lock); } while(0) +#endif SC_CFLD(c->queries); - SC_CFLD(c->logentries); free(c); return 1; } |