diff --git a/hueplusplus/HttpHandler.cpp b/hueplusplus/HttpHandler.cpp index 53994c5..2bf1eee 100755 --- a/hueplusplus/HttpHandler.cpp +++ b/hueplusplus/HttpHandler.cpp @@ -37,7 +37,7 @@ class SocketCloser { private: int s; }; -std::string HttpHandler::send(const std::string & msg, const std::string & adr, int port) +std::string HttpHandler::send(const std::string & msg, const std::string & adr, int port) const { // create socket int socketFD = socket(AF_INET, SOCK_STREAM, 0); @@ -126,7 +126,7 @@ std::string HttpHandler::send(const std::string & msg, const std::string & adr, return response; } -std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::string & adr, int port) +std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::string & adr, int port) const { std::string response = send(msg, adr, port); size_t start = response.find("\r\n\r\n"); @@ -139,7 +139,7 @@ std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::str return response; } -std::vector HttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) +std::vector HttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const { hostent *server; // host information sockaddr_in server_addr; // server address @@ -218,7 +218,7 @@ std::vector HttpHandler::sendMulticast(const std::string & msg, con return returnString; } -std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port) +std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const { std::string request; // Protocol reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html @@ -244,23 +244,23 @@ std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, st return sendGetHTTPBody(request.c_str(), adr, port); } -std::string HttpHandler::GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) +std::string HttpHandler::GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const { return sendHTTPRequest("GET", uri, content_type, body, adr, port); } -std::string HttpHandler::POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) +std::string HttpHandler::POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const { return sendHTTPRequest("POST", uri, content_type, body, adr, port); } -std::string HttpHandler::PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) +std::string HttpHandler::PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port) const { return sendHTTPRequest("PUT", uri, content_type, body, adr, port); } //! \todo Get rid of duplicate code in GETJson, POSTJson and PUTJson -Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port) +Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const { std::string response = GETString(uri, "application/json", body.toStyledString(), adr, port); @@ -277,7 +277,7 @@ Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const return result; } -Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port) +Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const { std::string response = POSTString(uri, "application/json", body.toStyledString(), adr, port); @@ -294,7 +294,7 @@ Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value& body, cons return result; } -Json::Value HttpHandler::PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port) +Json::Value HttpHandler::PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port) const { std::string response = PUTString(uri, "application/json", body.toStyledString(), adr, port); diff --git a/hueplusplus/include/HttpHandler.h b/hueplusplus/include/HttpHandler.h index 88a16f9..59560a4 100755 --- a/hueplusplus/include/HttpHandler.h +++ b/hueplusplus/include/HttpHandler.h @@ -24,9 +24,10 @@ #include #include "json/json.h" +#include "IHttpHandler.h" //! Class to handle http requests and multicast requests -class HttpHandler +class HttpHandler : public IHttpHandler { public: //! \brief Function that sends a given message to the specified host and returns the response. @@ -36,7 +37,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return String containing the response of the host - std::string send(const std::string &msg, const std::string &adr, int port=80); + std::string send(const std::string &msg, const std::string &adr, int port=80) const; //! \brief Function that sends a given message to the specified host and returns the body of the response. //! @@ -46,7 +47,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent. Default is 80 //! \return String containing the body of the response of the host - std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80); + std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const; //! \brief Function that sends a multicast request with the specified message. //! @@ -56,7 +57,7 @@ public: //! \param port Optional integer that specifies the port to which the request is sent. Default is 1900 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5 //! \return Vector containing strings of each answer received - std::vector sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5); + std::vector sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const; //! \brief Function that sends a HTTP request with the given method to the specified host and returns the body of the response. //! @@ -69,7 +70,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return String containing the body of the response of the host - std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80); + std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const; //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response. //! @@ -81,7 +82,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return String containing the body of the response of the host - std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80); + std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const; //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response. //! @@ -93,7 +94,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return String containing the body of the response of the host - std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80); + std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const; //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response. //! @@ -105,7 +106,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return String containing the body of the response of the host - std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80); + std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const; //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response. //! @@ -116,7 +117,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return Json::Value containing the parsed body of the response of the host - Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80); + Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const; //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response. //! @@ -127,7 +128,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return Json::Value containing the parsed body of the response of the host - Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80); + Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const; //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response. //! @@ -138,7 +139,7 @@ public: //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 //! \return Json::Value containing the parsed body of the response of the host - Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80); + Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const; }; #endif diff --git a/hueplusplus/include/IHttpHandler.h b/hueplusplus/include/IHttpHandler.h new file mode 100644 index 0000000..b16dfa1 --- /dev/null +++ b/hueplusplus/include/IHttpHandler.h @@ -0,0 +1,147 @@ +/** + \file IHttpHandler.h + Copyright Notice\n + Copyright (C) 2017 Jan Rogall - developer\n + Copyright (C) 2017 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#ifndef _IHTTPHANDLER_H +#define _IHTTPHANDLER_H + +#include +#include + +#include "json/json.h" + +class IHttpHandler +{ +public: + + //! \brief Virtual dtor + virtual ~IHttpHandler() = default; + + //! \brief Virtual function that should send a given message to a specified host and return the response. + //! + //! It should return a string containing the response of the host. + //! \param msg String that contains the message that should be sent to the specified address + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request should be sent to. Default is 80 + //! \return String containing the response of the host + virtual std::string send(const std::string &msg, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual Function that should given message to a specified host and return the body of the response. + //! + //! It should return a string containing only the body of the response of the host. + //! Note if no body is found a runtime error is thrown! + //! \param msg String that contains the message that should sent to the specified address + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request should be sent. Default is 80 + //! \return String containing the body of the response of the host + virtual std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80) const = 0; + + //! \brief Virtual function that should send a multicast request with a specified message. + //! + //! It should return a vector containing all responses the multicast request got back + //! \param msg String that contains the request that should be sent to the specified address + //! \param adr Optional String that contains an ip or hostname in dotted decimal notation, default is "239.255.255.250" + //! \param port Optional integer that specifies the port to which the request should be sent. Default is 1900 + //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5 + //! \return Vector containing strings of each answer received + virtual std::vector sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; + + //! \brief Virtual function that should send a HTTP request with the given method to the specified host and return the body of the response. + //! + //! It should return a string containing only the body of the response of the host. + //! Note body can also be left empty! + //! \param method String that contains the HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... + //! \param uri String that contains the uniform resource identifier + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... + //! \param body String that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return String containing the body of the response of the host + virtual std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response. + //! + //! It should return a string containing only the body of the response of the host. + //! Note body can also be left empty! + //! \param uri String that contains the uniform resource identifier + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... + //! \param body String that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return String containing the body of the response of the host + virtual std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual function that should send a HTTP POST request to the specified host and returns the body of the response. + //! + //! It should return a string containing only the body of the response of the host. + //! Note body can also be left empty! + //! \param uri String that contains the uniform resource identifier + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... + //! \param body String that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return String containing the body of the response of the host + virtual std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. + //! + //! It should return a string containing only the body of the response of the host. + //! Note body can also be left empty! + //! \param uri String that contains the uniform resource identifier + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ... + //! \param body String that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return String containing the body of the response of the host + virtual std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual function that should send a HTTP GET request to the specified host and return the body of the response. + //! + //! It should return a Json::Value parsed from the body of the response of the host. + //! Note body can also be left empty! + //! \param uri String that contains the uniform resource identifier + //! \param body Json::Value that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return Json::Value containing the parsed body of the response of the host + virtual Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual function that should send a HTTP POST request to the specified host and return the body of the response. + //! + //! It should return a Json::Value parsed from the body of the response of the host. + //! Note body can also be left empty! + //! \param uri String that contains the uniform resource identifier + //! \param body Json::Value that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return Json::Value containing the parsed body of the response of the host + virtual Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; + + //! \brief Virtual function that should send a HTTP PUT request to the specified host and return the body of the response. + //! + //! It should return a Json::Value parsed from the body of the response of the host. + //! Note body can also be left empty! + //! \param uri String that contains the uniform resource identifier + //! \param body Json::Value that contains the data of the request + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 + //! \return Json::Value containing the parsed body of the response of the host + virtual Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80) const = 0; +}; + +#endif