diff --git a/Makefile b/Makefile index b0d5c45..479abfb 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,18 @@ CC = gcc CFLAGS = -Wall -g -O2 LDFLAGS = -lmicrohttpd +OBJFILES = main.o handlers.o +DEPS = handlers.h all: nasuh-server -nasuh-server: main.c - $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +nasuh-server: $(OBJFILES) + $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) clean: - rm -f nasuh-server + rm -f nasuh-server *.o # end of Makefile diff --git a/handlers.c b/handlers.c new file mode 100644 index 0000000..704ff67 --- /dev/null +++ b/handlers.c @@ -0,0 +1,41 @@ +// +// Created by hamo on 11/19/22. +// + +#include "handlers.h" + +enum MHD_Result + handle_all(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; +} diff --git a/handlers.h b/handlers.h new file mode 100644 index 0000000..f80caa0 --- /dev/null +++ b/handlers.h @@ -0,0 +1,25 @@ +// +// Created by hamo on 11/19/22. +// +#include +#include +#include + +#ifndef NASUH_HANDLERS_H +#define NASUH_HANDLERS_H + +#define PAGE "nasuh search engine"\ + "all your programming needs" + +enum MHD_Result + handle_all(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); + + +#endif //NASUH_HANDLERS_H diff --git a/handlers.o b/handlers.o new file mode 100644 index 0000000..0a18a15 Binary files /dev/null and b/handlers.o differ diff --git a/main.c b/main.c index 1e8ca94..ff8dc0d 100644 --- a/main.c +++ b/main.c @@ -1,46 +1,8 @@ #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; -} +#include +#include "handlers.h" int main(int argc, char ** argv) { @@ -54,7 +16,7 @@ int main(int argc, atoi(argv[1]), NULL, NULL, - &ahc_echo, + &handle_all, PAGE, MHD_OPTION_END); if (d == NULL) diff --git a/main.o b/main.o new file mode 100644 index 0000000..077d869 Binary files /dev/null and b/main.o differ