summaryrefslogblamecommitdiffstats
path: root/src/main.c
blob: e03b0c2ee73348b9d785cef88c9db580e019050c (plain) (tree)
1
2
3
4
5
6
7
8
9
                   





                   
                 
                 
                   
                   










                              
                       


                      

                

























                                                                                                     


                                                                                              


                                                    


                           









                                                                                                                                          
                                                                                                       





                                                                                                                          
                  
                                                                                                                                                                                                          


                              


                                              
                                     



                        
                        




                                                                                                                    

                                         
                

                                          
   

                                                                                                   
                           


                         
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <printf.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <stdatomic.h>
#include <i18n.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <libxml/nanohttp.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <microhttpd.h>
#ifndef SC_OLD_STORAGE
#include <search.h>
#endif
#include <lib.c>
#include <url.c>
#define ABS(x) (((x) < 0) ? -(x) : (x))
enum sc_return {
	SC_BADCALL = -1,	/* calling the function the wrong way, I could actually just crash */
	SC_EMPTYRESPONSE = -2,	/* something weird, do not retry */
	SC_CAPTCHA = -3,	/* engine responded with a CAPTCHA, redirect to a different server */
	SC_NOIMGCLASS = -4,	/* couldn't find image class name from definition, do not retry */
	SC_NOCLASS = -5,	/* couldn't find result class name from def, do not retry */
	SC_NOHREF = -6		/* a result did not contain a href attribute */
};
const char * sc_return_str (enum sc_return r) {
	switch (r) {
		case SC_BADCALL:
			return "SC_BADCALL";
		case SC_EMPTYRESPONSE:
			return "SC_EMPTYRESPONSE";
		case SC_CAPTCHA:
			return "SC_CAPTCHA";
		case SC_NOIMGCLASS:
			return "SC_NOIMGCLASS";
		case SC_NOCLASS:
			return "SC_NOCLASS";
		case SC_NOHREF:
			return "SC_NOHREF";
	}
	return "SC_BADRETURN";
}
unsigned char sc_hp[] = { /* html page null terminated format string, from file src/hp.html */
#include <hp.xxd>
};
char sc_osdd[] = { /* xml document for opensearch */
#include <osdd.xxd>
};
char sc_css[] = { /* css */
#include <css.xxd>
};
char sc_robotstxt[] = "User-Agent: *\nDisallow: /\n";
char sc_securitytxt[] = "# This content information is provided by the developer of this opensource application.\n"
	"# The developer is not responsible for the actions of his software\n"
	"# This website IS NOT operated by the developer. Do not use the contact information below in hopes of contacting the webmaster\n"
	"# The following contact information is provided for reporting security bugs regarding the software, not for legal issues\n"
	"Contact: https://www.sijanec.eu/o.html#kontakt\n"
	"Acknowledgments: https://git.sijanec.eu/sijanec/sear.c\n"
	"Encryption: https://www.sijanec.eu/pgp-key.txt\n"
	"Expires: Thu, 31 Dec 2021 18:37:07 -0800\n"
	"Preferred-Languages: sl, en, de, hr\n";
#define SC_HTTP_PORT (getenv("SC_PORT") ? atoi(getenv("SC_PORT")) : 7327) /* SEAR on mobile keyboard */
#define SC_HTTP_RBUFSIZE 4096 /* initial size of http read buffer, increasning by K */
#define SC_HTTP_USER_AGENT "Nokia WAP Gateway 4.1 CD1/ECD13_D/4.1.04)" /* so google and others sends a minimal response */
#define SC_HTTP_HEADERS "User-Agent: " SC_HTTP_USER_AGENT "\r\n"
#include <structs.c>
#include <log.c>
#include <api.c>
#include <httpd.c>
/* this is new in my programs. I am now using _sizeof for the actual alloced size of the array and _length for the count of elements in array. this is done to decrease number of calls to realloc&amis */
void sc_signalhander (int s) {
	return;
}
int main (int argc, char ** argv) {
	int rs = 0;
	struct sc_cache * c = sc_cache_init();
	struct MHD_Daemon * d = NULL;
	if (!c) {
		rs = 1;
		goto rc;
	}
	xmlInitParser();
	d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, SC_HTTP_PORT, NULL, NULL, &sc_httpd, c, MHD_OPTION_END);
	if (!d) {
		rs = 2;
		goto rc;
	}
	signal(SIGTERM, sc_signalhander);
	signal(SIGINT, sc_signalhander);
	pause();
	fprintf(stderr, "cleaning up!\n");
	fflush(stderr);
rc:
	if (d)
		MHD_stop_daemon(d); /* stop the daemon and then free, threads might still be run */
	xmlCleanupParser();
	sc_cache_free(c);
	return rs;
}