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,
87 MHD_OPTION_NOTIFY_COMPLETED,
89 MHD_OPTION_URI_LOG_CALLBACK,
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,
141 (
void *)__dispatcher,
142 MHD_OPTION_NOTIFY_COMPLETED,
144 MHD_OPTION_URI_LOG_CALLBACK,
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 ) {
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) {
244 request_manager->set_server(
this);
245 __request_manager = request_manager;
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);
unsigned int active_requests() const
Get number of active requests.
void setup_basic_auth(const char *realm, WebUserVerifier *verifier)
Setup basic authentication.
File could not be opened.
unsigned int active_requests() const
Get number of active requests.
WebServer(unsigned short int port, WebRequestDispatcher *dispatcher, fawkes::Logger *logger=0, bool enable_ipv4=true, bool enable_ipv6=true)
Constructor.
Fawkes library namespace.
void process()
Process requests.
static void request_completed_cb(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
Process request completion.
A class for handling time.
thread cannot be cancelled
static int process_request_cb(void *callback_data, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **session_data)
Process request callback for libmicrohttpd.
static void set_cancel_state(CancelState new_state, CancelState *old_state=0)
Set the cancel state of the current thread.
void setup_basic_auth(const char *realm, WebUserVerifier *verifier)
Setup basic authentication.
Interface for user verification.
Base class for exceptions in Fawkes.
void setup_request_manager(WebRequestManager *request_manager)
Setup this server as request manager.
Probides information about ongoing requests.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Time last_request_completion_time() const
Get time when last request was completed.
Time last_request_completion_time() const
Get time when last request was completed.
static void * uri_log_cb(void *cls, const char *uri)
Callback for new requests.
void setup_access_log(const char *filename)
Setup access log.
void setup_access_log(const char *filename)
Setup access log.