Fawkes API Fawkes Development Version
request_dispatcher.h
1
2/***************************************************************************
3 * request_dispatcher.h - Web request dispatcher
4 *
5 * Created: Mon Oct 13 22:44:33 2008
6 * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#ifndef _LIBS_WEBVIEW_REQUEST_DISPATCHER_H_
23#define _LIBS_WEBVIEW_REQUEST_DISPATCHER_H_
24
25#include "microhttpd_compat.h"
26
27#include <utils/time/time.h>
28
29#include <map>
30#include <memory>
31#include <microhttpd.h>
32#include <string>
33#include <vector>
34
35namespace fawkes {
36
37class WebRequestProcessor;
38class WebUrlManager;
39class WebPageHeaderGenerator;
40class WebPageFooterGenerator;
41class StaticWebReply;
42class DynamicWebReply;
43class WebUserVerifier;
44class WebRequest;
45class WebviewAccessLog;
46class Mutex;
47
49{
50public:
52 WebPageHeaderGenerator *headergen = 0,
53 WebPageFooterGenerator *footergen = 0);
55
56 static MHD_RESULT process_request_cb(void * callback_data,
57 struct MHD_Connection *connection,
58 const char * url,
59 const char * method,
60 const char * version,
61 const char * upload_data,
62 size_t * upload_data_size,
63 void ** session_data);
64
65 static void request_completed_cb(void * cls,
66 struct MHD_Connection * connection,
67 void ** con_cls,
68 enum MHD_RequestTerminationCode toe);
69
70 static void *uri_log_cb(void *cls, const char *uri);
71
72 void setup_basic_auth(const char *realm, WebUserVerifier *verifier);
73 void setup_access_log(const char *filename);
74 void setup_cors(bool allow_all, std::vector<std::string> &&origins, unsigned int max_age);
75
76 unsigned int active_requests() const;
78
79private:
80 struct MHD_Response *prepare_static_response(StaticWebReply *sreply);
81 MHD_RESULT queue_static_reply(struct MHD_Connection *connection,
82 WebRequest * request,
83 StaticWebReply * sreply);
84 MHD_RESULT queue_dynamic_reply(struct MHD_Connection *connection,
85 WebRequest * request,
86 DynamicWebReply * sreply);
87 MHD_RESULT queue_basic_auth_fail(struct MHD_Connection *connection, WebRequest *request);
88 MHD_RESULT process_request(struct MHD_Connection *connection,
89 const char * url,
90 const char * method,
91 const char * version,
92 const char * upload_data,
93 size_t * upload_data_size,
94 void ** session_data);
95 void * log_uri(const char *uri);
96
97 void request_completed(WebRequest *request, MHD_RequestTerminationCode term_code);
98
99private:
100 WebUrlManager * url_manager_;
101 WebviewAccessLog *access_log_;
102
103 std::string active_baseurl_;
104 WebPageHeaderGenerator *page_header_generator_;
105 WebPageFooterGenerator *page_footer_generator_;
106
107 char * realm_;
108 WebUserVerifier *user_verifier_;
109
110 unsigned int active_requests_;
111 fawkes::Time * last_request_completion_time_;
112 fawkes::Mutex *active_requests_mutex_;
113
114 bool cors_allow_all_;
115 std::vector<std::string> cors_origins_;
116 unsigned int cors_max_age_;
117};
118
119} // end namespace fawkes
120
121#endif
Dynamic web reply.
Definition: reply.h:126
Mutex mutual exclusion lock.
Definition: mutex.h:33
Static web reply.
Definition: reply.h:136
A class for handling time.
Definition: time.h:93
Interface for HTML footer generator.
Interface for HTML header generator.
Web request dispatcher.
unsigned int active_requests() const
Get number of active requests.
void setup_cors(bool allow_all, std::vector< std::string > &&origins, unsigned int max_age)
Setup cross-origin resource sharing.
void setup_basic_auth(const char *realm, WebUserVerifier *verifier)
Setup basic authentication.
static void * uri_log_cb(void *cls, const char *uri)
Callback for new requests.
static void request_completed_cb(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
Process request completion.
static MHD_RESULT 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.
void setup_access_log(const char *filename)
Setup access log.
Time last_request_completion_time() const
Get time when last request was completed.
WebRequestDispatcher(WebUrlManager *url_manager, WebPageHeaderGenerator *headergen=0, WebPageFooterGenerator *footergen=0)
Constructor.
Web request meta data carrier.
Definition: request.h:42
Manage URL mappings.
Definition: url_manager.h:40
Interface for user verification.
Definition: user_verifier.h:29
Webview access_log writer.
Definition: access_log.h:33
Fawkes library namespace.