summaryrefslogtreecommitdiff
path: root/src/webserver.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-11-13 19:31:03 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2014-11-13 19:31:03 +0100
commit7cffcc99678cb62c11f33bf6ff6979229754cbc7 (patch)
tree44fd9c53628283dde155e550a865b3bd216fb13e /src/webserver.c
parent7d239d4d733a00eb0d44b8a29d67c2ec312a1604 (diff)
downloadbiruda-7cffcc99678cb62c11f33bf6ff6979229754cbc7.tar.gz
biruda-7cffcc99678cb62c11f33bf6ff6979229754cbc7.tar.bz2
added a small built-in webserver
Diffstat (limited to 'src/webserver.c')
-rw-r--r--src/webserver.c62
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;
+}