diff options
Diffstat (limited to 'src/webserver.c')
-rw-r--r-- | src/webserver.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/webserver.c b/src/webserver.c new file mode 100644 index 0000000..c53dd93 --- /dev/null +++ b/src/webserver.c @@ -0,0 +1,62 @@ +#include "webserver.h" + +#include <microhttpd.h> + +#include <string.h> +#include <stdio.h> + +static struct MHD_Daemon *daemon; + +static int handle_request( 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 ) +{ + struct MHD_Response *response; + int ret; + + if( strcmp( method, "GET" ) != 0 ) { + return MHD_NO; + } + + if( strcmp( version, "HTTP/1.1" ) != 0 ) { + return MHD_NO; + } + + fprintf( stderr, "http request: %s\n", url ); + + char *biruda_msg = "Welcome to biruda!"; + response = MHD_create_response_from_buffer( strlen( biruda_msg ), + (void *)biruda_msg, MHD_RESPMEM_MUST_COPY ); + ret = MHD_queue_response( connection, MHD_HTTP_OK, response ); + MHD_destroy_response( response ); + + return ret; +} + +int webserver_init( unsigned int port ) +{ + daemon = MHD_start_daemon( + MHD_USE_SELECT_INTERNALLY, port, + NULL, + NULL, + &handle_request, + MHD_OPTION_END ); + if( daemon == 0 ) { + return 1; + } + + puts( "http daemon started" ); + + return 0; +} + +void webserver_terminate( ) +{ + MHD_stop_daemon( daemon ); + puts( "http daemon stopped" ); +} + +int webserver_free( ) +{ + return 0; +} |