Subversion Repositories heating

Rev

Rev 19 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 *
 * Author: Andreas Theofilu <andreas@theosys.at>
 */
#ifndef controller_h
#define controller_h

#include <sstream>

/**
 * Base controller for handling http requests.
 */
class Controller
{
        public:
                /**
                * Check if given path and method are handled by this controller.
                */
                virtual bool validPath(const char *path, const char *method) = 0;
        
                /**
                * Handles given request.
                */
                virtual int handleRequest(struct MHD_Connection* connection,
                                                          const char *url, const char *method, const char *upload_data,
                                                          size_t *upload_data_size, void **ptr) = 0;
};

/**
 * The dynamic controller is a controller for creating user defined pages.
 */
class DynamicController : public Controller
{
        public:
                virtual bool validPath(const char *path, const char *method) = 0;
        
                /**
                * User defined http response.
                */
                virtual void createResponse(struct MHD_Connection *connection,
                                const char *url, const char *method, const char *upload_data,
                                size_t *upload_data_size, std::stringstream& response, void **ptr) = 0;

                virtual int handleRequest(struct MHD_Connection* connection,
                                                          const char *url, const char *method, const char *upload_data,
                                                          size_t *upload_data_size, void **ptr)
                {
                        std::stringstream response_string;
                        createResponse(connection, url, method, upload_data, upload_data_size, response_string, ptr);

                        //Send response.
                        struct MHD_Response *response = MHD_create_response_from_buffer(strlen(response_string.str().c_str()),
                                                                                                                        (void *)response_string.str().c_str(), MHD_RESPMEM_MUST_COPY);
                        int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
                        MHD_destroy_response(response);

                        return ret;
                }
};

#endif /* controller_h */