From 45a48aa0e74d77c789011a17e59686e5d23beeac Mon Sep 17 00:00:00 2001 From: kakwa Date: Fri, 19 Aug 2016 00:04:13 +0200 Subject: [PATCH] use civetweb --- CMakeLists.txt | 11 ++++--- cmake/Findlibasyncd.cmake | 7 ----- cmake/Findlibcivetweb.cmake | 7 +++++ src/lib/rfc3161.c | 61 +++++++++++++++++++++++++++++-------- 4 files changed, 62 insertions(+), 24 deletions(-) delete mode 100755 cmake/Findlibasyncd.cmake create mode 100755 cmake/Findlibcivetweb.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 6412708..2de104a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,9 @@ set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -DUTS_VERSION='\"${VERSION}\"'") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/") -find_package(libasyncd REQUIRED) -MESSAGE( STATUS "Find Header Directory for libasyncd: " ${LIBASYNCD_INCLUDE_DIR}) -MESSAGE( STATUS "Find Dynamic Library for libasyncd: " ${LIBASYNCD_LIBRARIES}) +find_package(libcivetweb REQUIRED) +MESSAGE( STATUS "Find Header Directory for libcivetweb: " ${LIBCIVETWEB_INCLUDE_DIR}) +MESSAGE( STATUS "Find Dynamic Library for libcivetweb: " ${LIBCIVETWEB_LIBRARIES}) find_package(OpenSSL REQUIRED) MESSAGE( STATUS "OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}") @@ -22,7 +22,7 @@ MESSAGE( STATUS "OpenSSL libraries: ${OPENSSL_LIBRARIES}") include_directories( ./inc/ - ${LIBASYNCD_INCLUDE_DIR} + ${LIBCIVETWEB_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR} ) @@ -30,10 +30,11 @@ add_executable(uts-server src/cmd/uts-server.c src/lib/rfc3161.c src/lib/utils.c + src/lib/ini.c ) target_link_libraries(uts-server - ${LIBASYNCD_LIBRARIES} + ${LIBCIVETWEB_LIBRARIES} ${OPENSSL_LIBRARIES} ) diff --git a/cmake/Findlibasyncd.cmake b/cmake/Findlibasyncd.cmake deleted file mode 100755 index 44ecdda..0000000 --- a/cmake/Findlibasyncd.cmake +++ /dev/null @@ -1,7 +0,0 @@ -if (NOT LIBASYNCD_LIBRARIES) - find_path(LIBASYNCD_INCLUDE_DIR asyncd.h ${_LIBASYNCD_PATHS} PATH_SUFFIXES include include/asyncd/) -endif () - -if (NOT LIBASYNCD_LIBRARIES) - find_library(LIBASYNCD_LIBRARIES NAMES asyncd ${_LIBASYNCD_PATHS} PATH_SUFFIXES lib) -endif () diff --git a/cmake/Findlibcivetweb.cmake b/cmake/Findlibcivetweb.cmake new file mode 100755 index 0000000..6c5e4f9 --- /dev/null +++ b/cmake/Findlibcivetweb.cmake @@ -0,0 +1,7 @@ +if (NOT LIBCIVETWEB_LIBRARIES) + find_path(LIBCIVETWEB_INCLUDE_DIR civetweb.h ${_LIBCIVETWEB_PATHS} PATH_SUFFIXES include include/civetweb/) +endif () + +if (NOT LIBCIVETWEB_LIBRARIES) + find_library(LIBCIVETWEB_LIBRARIES NAMES civetweb ${_LIBCIVETWEB_PATHS} PATH_SUFFIXES lib) +endif () diff --git a/src/lib/rfc3161.c b/src/lib/rfc3161.c index b89be1d..0ec1e5a 100644 --- a/src/lib/rfc3161.c +++ b/src/lib/rfc3161.c @@ -7,8 +7,9 @@ #include #include #include -#include +#include +/* int ts_http_respond(short event, ad_conn_t *conn, void *userdata) { if (event & AD_EVENT_READ) { if (ad_http_get_status(conn) == AD_HTTP_REQ_DONE) { @@ -28,17 +29,53 @@ int http_default_handler(short event, ad_conn_t *conn, void *userdata) { } return AD_OK; } +*/ + +// This function will be called by civetweb on every new request. +static int begin_request_handler(struct mg_connection *conn) +{ + const struct mg_request_info *request_info = mg_get_request_info(conn); + char content[100]; + + // Prepare the message we're going to send + int content_length = snprintf(content, sizeof(content), + "Hello from civetweb! Remote port: %d", + request_info->remote_port); + + // Send HTTP reply to the client + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: %d\r\n" // Always set Content-Length + "\r\n" + "%s", + content_length, content); + + // Returning non-zero tells civetweb that our function has replied to + // the client, and civetweb should not send client any more data. + return 1; +} int http_server_start() { - //SSL_load_error_strings(); - //SSL_library_init(); - ad_log_level(AD_LOG_DEBUG); - ad_server_t *server = ad_server_new(); - ad_server_set_option(server, "server.port", "8888"); - //ad_server_set_ssl_ctx(server, - // ad_server_ssl_ctx_create_simple("ssl.cert", "ssl.pkey")); - ad_server_register_hook(server, ad_http_handler, NULL); // HTTP Parser is also a hook. - ad_server_register_hook_on_method(server, "GET", ts_http_respond, NULL); - ad_server_register_hook(server, http_default_handler, NULL); - return ad_server_start(server); + struct mg_context *ctx; + struct mg_callbacks callbacks; + + // List of options. Last element must be NULL. + const char *options[] = {"listening_ports", "8080", NULL}; + + // Prepare callbacks structure. We have only one callback, the rest are NULL. + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + + // Start the web server. + ctx = mg_start(&callbacks, NULL, options); + + // Wait until user hits "enter". Server is running in separate thread. + // Navigating to http://localhost:8080 will invoke begin_request_handler(). + getchar(); + + // Stop the server. + mg_stop(ctx); + + return 0; }