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 <utils/time/time.h>
26 
27 #include <map>
28 #include <memory>
29 #include <microhttpd.h>
30 #include <string>
31 #include <vector>
32 
33 namespace fawkes {
34 
35 class WebRequestProcessor;
36 class WebUrlManager;
37 class WebPageHeaderGenerator;
38 class WebPageFooterGenerator;
39 class StaticWebReply;
40 class DynamicWebReply;
41 class WebUserVerifier;
42 class WebRequest;
43 class WebviewAccessLog;
44 class Mutex;
45 
47 {
48 public:
49  WebRequestDispatcher(WebUrlManager * url_manager,
50  WebPageHeaderGenerator *headergen = 0,
51  WebPageFooterGenerator *footergen = 0);
53 
54  static int process_request_cb(void * callback_data,
55  struct MHD_Connection *connection,
56  const char * url,
57  const char * method,
58  const char * version,
59  const char * upload_data,
60  size_t * upload_data_size,
61  void ** session_data);
62 
63  static void request_completed_cb(void * cls,
64  struct MHD_Connection * connection,
65  void ** con_cls,
66  enum MHD_RequestTerminationCode toe);
67 
68  static void *uri_log_cb(void *cls, const char *uri);
69 
70  void setup_basic_auth(const char *realm, WebUserVerifier *verifier);
71  void setup_access_log(const char *filename);
72  void setup_cors(bool allow_all, std::vector<std::string> &&origins, unsigned int max_age);
73 
74  unsigned int active_requests() const;
76 
77 private:
78  struct MHD_Response *prepare_static_response(StaticWebReply *sreply);
79  int queue_static_reply(struct MHD_Connection *connection,
80  WebRequest * request,
81  StaticWebReply * sreply);
82  int queue_dynamic_reply(struct MHD_Connection *connection,
83  WebRequest * request,
84  DynamicWebReply * sreply);
85  int queue_basic_auth_fail(struct MHD_Connection *connection, WebRequest *request);
86  int process_request(struct MHD_Connection *connection,
87  const char * url,
88  const char * method,
89  const char * version,
90  const char * upload_data,
91  size_t * upload_data_size,
92  void ** session_data);
93  void *log_uri(const char *uri);
94 
95  void request_completed(WebRequest *request, MHD_RequestTerminationCode term_code);
96 
97 private:
98  WebUrlManager * url_manager_;
99  WebviewAccessLog *access_log_;
100 
101  std::string active_baseurl_;
102  WebPageHeaderGenerator *page_header_generator_;
103  WebPageFooterGenerator *page_footer_generator_;
104 
105  char * realm_;
106  WebUserVerifier *user_verifier_;
107 
108  unsigned int active_requests_;
109  fawkes::Time * last_request_completion_time_;
110  fawkes::Mutex *active_requests_mutex_;
111 
112  bool cors_allow_all_;
113  std::vector<std::string> cors_origins_;
114  unsigned int cors_max_age_;
115 };
116 
117 } // end namespace fawkes
118 
119 #endif
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.
Fawkes library namespace.
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.
Definition: time.h:92
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.
WebRequestDispatcher(WebUrlManager *url_manager, WebPageHeaderGenerator *headergen=0, WebPageFooterGenerator *footergen=0)
Constructor.
void setup_basic_auth(const char *realm, WebUserVerifier *verifier)
Setup basic authentication.
Interface for user verification.
Definition: user_verifier.h:28
Interface for HTML header generator.
Webview access_log writer.
Definition: access_log.h:32
Manage URL mappings.
Definition: url_manager.h:39
Dynamic web reply.
Definition: reply.h:125
Web request meta data carrier.
Definition: request.h:41
Time last_request_completion_time() const
Get time when last request was completed.
Interface for HTML footer generator.
static void * uri_log_cb(void *cls, const char *uri)
Callback for new requests.
void setup_access_log(const char *filename)
Setup access log.
Mutex mutual exclusion lock.
Definition: mutex.h:32
Static web reply.
Definition: reply.h:135