22 #include <webview/server.h> 23 #include <webview/request_dispatcher.h> 24 #include <webview/request.h> 25 #include <webview/request_manager.h> 26 #include <webview/access_log.h> 27 #include <core/threading/thread.h> 28 #include <core/exception.h> 29 #include <core/exceptions/system.h> 30 #include <logging/logger.h> 32 #include <sys/socket.h> 36 #include <microhttpd.h> 60 bool enable_ipv4,
bool enable_ipv6)
63 __dispatcher = dispatcher;
65 __request_manager = NULL;
68 __ssl_cert_mem = NULL;
70 unsigned int flags = MHD_NO_FLAG;
71 #if MHD_VERSION >= 0x00090280 72 if (enable_ipv4 && enable_ipv6) {
73 flags |= MHD_USE_DUAL_STACK;
74 }
else if (enable_ipv6) {
75 flags |= MHD_USE_IPv6;
76 }
else if (! enable_ipv4 && ! enable_ipv6) {
81 __daemon = MHD_start_daemon(flags,
85 WebRequestDispatcher::process_request_cb,
87 MHD_OPTION_NOTIFY_COMPLETED,
88 WebRequestDispatcher::request_completed_cb, (
void *)__dispatcher,
89 MHD_OPTION_URI_LOG_CALLBACK,
90 WebRequestDispatcher::uri_log_cb, (
void *)__dispatcher,
93 if ( __daemon == NULL ) {
110 const char *key_pem_filepath,
const char *cert_pem_filepath,
112 bool enable_ipv4,
bool enable_ipv6)
115 __dispatcher = dispatcher;
117 __request_manager = NULL;
119 __ssl_key_mem = read_file(key_pem_filepath);
120 __ssl_cert_mem = read_file(cert_pem_filepath);
121 if (cipher_suite == NULL) {
122 cipher_suite = WEBVIEW_DEFAULT_CIPHERS;
125 unsigned int flags = MHD_USE_SSL;
126 #if MHD_VERSION >= 0x00090280 127 if (enable_ipv4 && enable_ipv6) {
128 flags |= MHD_USE_DUAL_STACK;
129 }
else if (enable_ipv6) {
130 flags |= MHD_USE_IPv6;
131 }
else if (! enable_ipv4 && ! enable_ipv6) {
136 __daemon = MHD_start_daemon(flags,
140 WebRequestDispatcher::process_request_cb,
141 (
void *)__dispatcher,
142 MHD_OPTION_NOTIFY_COMPLETED,
143 WebRequestDispatcher::request_completed_cb, (
void *)__dispatcher,
144 MHD_OPTION_URI_LOG_CALLBACK,
145 WebRequestDispatcher::uri_log_cb, (
void *)__dispatcher,
146 MHD_OPTION_HTTPS_MEM_KEY, __ssl_key_mem,
147 MHD_OPTION_HTTPS_MEM_CERT, __ssl_cert_mem,
148 MHD_OPTION_HTTPS_PRIORITIES, cipher_suite,
151 if ( __daemon == NULL ) {
159 WebServer::~WebServer()
161 if (__request_manager) {
162 __request_manager->set_server(NULL);
165 MHD_stop_daemon(__daemon);
169 if (__ssl_key_mem) free(__ssl_key_mem);
170 if (__ssl_cert_mem) free(__ssl_cert_mem);
179 WebServer::read_file(
const char *filename)
181 FILE *f = fopen(filename,
"rb");
187 if ((fseek(f, 0, SEEK_END) != 0) || ((size = ftell(f)) == 1)) {
189 throw Exception(
"Cannot determine file size of %s", filename);
191 fseek(f, 0, SEEK_SET);
195 throw Exception(
"File %s has zero length", filename);
196 }
else if (size > 1024 * 1024) {
199 throw Exception(
"File %s is unexpectedly large", filename);
202 char *rv = (
char *)malloc(size);
203 if (fread(rv, size, 1, f) != 1) {
207 throw FileReadException(filename, terrno);
223 __dispatcher->setup_basic_auth(realm, verifier);
231 WebServer::setup_access_log(
const char *filename)
233 __dispatcher->setup_access_log(filename);
244 request_manager->set_server(
this);
245 __request_manager = request_manager;
252 WebServer::active_requests()
const 254 return __dispatcher->active_requests();
261 WebServer::last_request_completion_time()
const 263 return __dispatcher->last_request_completion_time();
273 fd_set read_fd, write_fd, except_fd;
275 FD_ZERO(&read_fd); FD_ZERO(&write_fd); FD_ZERO(&except_fd);
276 if ( MHD_get_fdset(__daemon, &read_fd, &write_fd, &except_fd, &max_fd) != MHD_YES ) {
278 __logger->log_warn(
"WebviewThread",
"Could not get microhttpd fdsets");
281 select(max_fd + 1, &read_fd, &write_fd, &except_fd, NULL);
283 Thread::set_cancel_state(Thread::CANCEL_DISABLED, &old_state);
285 Thread::set_cancel_state(old_state);
File could not be opened.
Fawkes library namespace.
A class for handling time.
Interface for user verification.
Base class for exceptions in Fawkes.
Probides information about ongoing requests.