17 |
andreas |
1 |
/*
|
|
|
2 |
* Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
|
|
|
3 |
*
|
|
|
4 |
* All rights reserved. No warranty, explicit or implicit, provided.
|
|
|
5 |
*
|
|
|
6 |
* NOTICE: All information contained herein is, and remains
|
|
|
7 |
* the property of Andreas Theofilu and his suppliers, if any.
|
|
|
8 |
* The intellectual and technical concepts contained
|
|
|
9 |
* herein are proprietary to Andreas Theofilu and its suppliers and
|
|
|
10 |
* may be covered by European and Foreign Patents, patents in process,
|
|
|
11 |
* and are protected by trade secret or copyright law.
|
|
|
12 |
* Dissemination of this information or reproduction of this material
|
|
|
13 |
* is strictly forbidden unless prior written permission is obtained
|
|
|
14 |
* from Andreas Theofilu.
|
|
|
15 |
*
|
|
|
16 |
* Author: Andreas Theofilu <andreas@theosys.at>
|
|
|
17 |
*/
|
|
|
18 |
#ifndef controller_h
|
|
|
19 |
#define controller_h
|
|
|
20 |
|
|
|
21 |
#include <sstream>
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Base controller for handling http requests.
|
|
|
25 |
*/
|
|
|
26 |
class Controller
|
|
|
27 |
{
|
|
|
28 |
public:
|
|
|
29 |
/**
|
|
|
30 |
* Check if given path and method are handled by this controller.
|
|
|
31 |
*/
|
|
|
32 |
virtual bool validPath(const char *path, const char *method) = 0;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Handles given request.
|
|
|
36 |
*/
|
|
|
37 |
virtual int handleRequest(struct MHD_Connection* connection,
|
|
|
38 |
const char *url, const char *method, const char *upload_data,
|
19 |
andreas |
39 |
size_t *upload_data_size, void **ptr) = 0;
|
17 |
andreas |
40 |
};
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The dynamic controller is a controller for creating user defined pages.
|
|
|
44 |
*/
|
|
|
45 |
class DynamicController : public Controller
|
|
|
46 |
{
|
|
|
47 |
public:
|
|
|
48 |
virtual bool validPath(const char *path, const char *method) = 0;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* User defined http response.
|
|
|
52 |
*/
|
|
|
53 |
virtual void createResponse(struct MHD_Connection *connection,
|
|
|
54 |
const char *url, const char *method, const char *upload_data,
|
19 |
andreas |
55 |
size_t *upload_data_size, std::stringstream& response, void **ptr) = 0;
|
40 |
andreas |
56 |
|
17 |
andreas |
57 |
virtual int handleRequest(struct MHD_Connection* connection,
|
|
|
58 |
const char *url, const char *method, const char *upload_data,
|
19 |
andreas |
59 |
size_t *upload_data_size, void **ptr)
|
17 |
andreas |
60 |
{
|
|
|
61 |
std::stringstream response_string;
|
19 |
andreas |
62 |
createResponse(connection, url, method, upload_data, upload_data_size, response_string, ptr);
|
17 |
andreas |
63 |
|
|
|
64 |
//Send response.
|
|
|
65 |
struct MHD_Response *response = MHD_create_response_from_buffer(strlen(response_string.str().c_str()),
|
|
|
66 |
(void *)response_string.str().c_str(), MHD_RESPMEM_MUST_COPY);
|
|
|
67 |
int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
|
|
|
68 |
MHD_destroy_response(response);
|
|
|
69 |
|
|
|
70 |
return ret;
|
|
|
71 |
}
|
|
|
72 |
};
|
|
|
73 |
|
|
|
74 |
#endif /* controller_h */
|