1
0
mirror of https://github.com/kakwa/uts-server synced 2024-12-04 23:15:54 +01:00

use civetweb

This commit is contained in:
kakwa 2016-08-19 00:04:13 +02:00
parent b1248f43b4
commit 45a48aa0e7
4 changed files with 62 additions and 24 deletions

View File

@ -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}
)

View File

@ -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 ()

7
cmake/Findlibcivetweb.cmake Executable file
View File

@ -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 ()

View File

@ -7,8 +7,9 @@
#include <openssl/rand.h>
#include <openssl/ts.h>
#include <openssl/bn.h>
#include <asyncd/asyncd.h>
#include <civetweb.h>
/*
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;
}