diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0686131 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +nasuh-server +.idea/ + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..65c5671 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +# compile main.c and link it dynamically to the libmicrohttpd library +# (libmicrohttpd.so must be in the library search path) +# (this Makefile is for GNU make) + +CC = gcc +CFLAGS = -Wall -g -O2 +LDFLAGS = -lmicrohttpd + +all: nasuh-server + +nasuh-server: main.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + rm -f main + +# end of Makefile diff --git a/main.c b/main.c new file mode 100644 index 0000000..1e8ca94 --- /dev/null +++ b/main.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#define PAGE "nasuh search engine"\ + "all your programming needs" + +static enum MHD_Result +ahc_echo(void * cls, + struct MHD_Connection * connection, + const char * url, + const char * method, + const char * version, + const char * upload_data, + size_t * upload_data_size, + void ** ptr) { + static int dummy; + const char * page = cls; + struct MHD_Response * response; + int ret; + + if (0 != strcmp(method, "GET")) + return MHD_NO; /* unexpected method */ + if (&dummy != *ptr) + { + /* The first time only the headers are valid, + do not respond in the first round... */ + *ptr = &dummy; + return MHD_YES; + } + if (0 != *upload_data_size) + return MHD_NO; /* upload data in a GET!? */ + *ptr = NULL; /* clear context pointer */ + response = MHD_create_response_from_buffer (strlen(page), + (void*) page, + MHD_RESPMEM_PERSISTENT); + ret = MHD_queue_response(connection, + MHD_HTTP_OK, + response); + MHD_destroy_response(response); + return ret; +} + +int main(int argc, + char ** argv) { + struct MHD_Daemon * d; + if (argc != 2) { + printf("%s PORT\n", + argv[0]); + return 1; + } + d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, + atoi(argv[1]), + NULL, + NULL, + &ahc_echo, + PAGE, + MHD_OPTION_END); + if (d == NULL) + return 1; + (void) getc (stdin); + MHD_stop_daemon(d); + return 0; +} \ No newline at end of file