Commit 3fdc280cb2331fd16fd484c1a75e86b8a1abcacc

Authored by Nodeduino
1 parent 607e1103

Move HTTP specific implementations out of Hue classes into HttpHandler and provi…

…de new functions abstracting said implementations, rename some old HttpHandler functions to match their task and clean up some stuff
hueplusplus/HttpHandler.cpp 100755 → 100644
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 19
20 #include "include/HttpHandler.h" 20 #include "include/HttpHandler.h"
21 #include <iostream> 21 #include <iostream>
  22 +#include <memory>
22 #include <stdexcept> 23 #include <stdexcept>
23 #include <stdio.h> // printf, sprintf 24 #include <stdio.h> // printf, sprintf
24 #include <stdlib.h> // exit 25 #include <stdlib.h> // exit
@@ -36,7 +37,7 @@ class SocketCloser { @@ -36,7 +37,7 @@ class SocketCloser {
36 private: int s; 37 private: int s;
37 }; 38 };
38 39
39 -std::string HttpHandler::sendRequest(const std::string & msg, const std::string & adr, int port) 40 +std::string HttpHandler::send(const std::string & msg, const std::string & adr, int port)
40 { 41 {
41 // create socket 42 // create socket
42 int socketFD = socket(AF_INET, SOCK_STREAM, 0); 43 int socketFD = socket(AF_INET, SOCK_STREAM, 0);
@@ -125,9 +126,9 @@ std::string HttpHandler::sendRequest(const std::string &amp; msg, const std::string @@ -125,9 +126,9 @@ std::string HttpHandler::sendRequest(const std::string &amp; msg, const std::string
125 return response; 126 return response;
126 } 127 }
127 128
128 -std::string HttpHandler::sendRequestGetBody(const std::string & msg, const std::string & adr, int port) 129 +std::string HttpHandler::sendGetHTTPBody(const std::string & msg, const std::string & adr, int port)
129 { 130 {
130 - std::string response = sendRequest(msg, adr, port); 131 + std::string response = send(msg, adr, port);
131 size_t start = response.find("\r\n\r\n"); 132 size_t start = response.find("\r\n\r\n");
132 if (start == std::string::npos) 133 if (start == std::string::npos)
133 { 134 {
@@ -216,3 +217,95 @@ std::vector&lt;std::string&gt; HttpHandler::sendMulticast(const std::string &amp; msg, con @@ -216,3 +217,95 @@ std::vector&lt;std::string&gt; HttpHandler::sendMulticast(const std::string &amp; msg, con
216 217
217 return returnString; 218 return returnString;
218 } 219 }
  220 +
  221 +std::string HttpHandler::sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  222 +{
  223 + std::string request;
  224 + // Protocol reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
  225 + // Request-Line
  226 + request.append(method); // Method
  227 + request.append(" "); // Separation
  228 + request.append(uri); // Request-URI
  229 + request.append(" "); // Separation
  230 + request.append("HTTP/1.0"); // HTTP-Version
  231 + request.append("\r\n"); // Ending
  232 + // Entities
  233 + request.append("Content-Type:"); // entity-header
  234 + request.append(" "); // Separation
  235 + request.append(content_type); // media-type
  236 + request.append("\r\n"); // Entity ending
  237 + request.append("Content-Length:"); // entity-header
  238 + request.append(" "); // Separation
  239 + request.append(std::to_string(body.size())); // length
  240 + request.append("\r\n\r\n"); // Entity ending & Request-Line ending
  241 + request.append(body); // message-body
  242 + request.append("\r\n\r\n"); // Ending
  243 +
  244 + return sendGetHTTPBody(request.c_str(), adr, port);
  245 +}
  246 +
  247 +std::string HttpHandler::GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  248 +{
  249 + return sendHTTPRequest("GET", uri, content_type, body, adr, port);
  250 +}
  251 +
  252 +std::string HttpHandler::POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  253 +{
  254 + return sendHTTPRequest("POST", uri, content_type, body, adr, port);
  255 +}
  256 +
  257 +std::string HttpHandler::PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port)
  258 +{
  259 + return sendHTTPRequest("PUT", uri, content_type, body, adr, port);
  260 +}
  261 +
  262 +Json::Value HttpHandler::GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port)
  263 +{
  264 + std::string response = GETString(uri, "application/json", body.toStyledString(), adr, port);
  265 +
  266 + std::string error;
  267 + Json::Value result;
  268 + Json::CharReaderBuilder builder;
  269 + builder["collectComments"] = false;
  270 + std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());
  271 + if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error))
  272 + {
  273 + std::cout << "Error while parsing JSON in function SendRequest() of HueLight: " << error << std::endl;
  274 + throw(std::runtime_error("Error while parsing JSON in function SendRequest() of HueLight"));
  275 + }
  276 + return result;
  277 +}
  278 +
  279 +Json::Value HttpHandler::POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port)
  280 +{
  281 + std::string response = POSTString(uri, "application/json", body.toStyledString(), adr, port);
  282 +
  283 + std::string error;
  284 + Json::Value result;
  285 + Json::CharReaderBuilder builder;
  286 + builder["collectComments"] = false;
  287 + std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());
  288 + if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error))
  289 + {
  290 + std::cout << "Error while parsing JSON in function SendRequest() of HueLight: " << error << std::endl;
  291 + throw(std::runtime_error("Error while parsing JSON in function SendRequest() of HueLight"));
  292 + }
  293 + return result;
  294 +}
  295 +
  296 +Json::Value HttpHandler::PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port)
  297 +{
  298 + std::string response = PUTString(uri, "application/json", body.toStyledString(), adr, port);
  299 +
  300 + std::string error;
  301 + Json::Value result;
  302 + Json::CharReaderBuilder builder;
  303 + builder["collectComments"] = false;
  304 + std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());
  305 + if (!reader->parse(response.c_str(), response.c_str() + response.length(), &result, &error))
  306 + {
  307 + std::cout << "Error while parsing JSON in function SendRequest() of HueLight: " << error << std::endl;
  308 + throw(std::runtime_error("Error while parsing JSON in function SendRequest() of HueLight"));
  309 + }
  310 + return result;
  311 +}
hueplusplus/Hue.cpp 100755 → 100644
@@ -56,7 +56,7 @@ std::vector&lt;HueFinder::HueIdentification&gt; HueFinder::FindBridges() const @@ -56,7 +56,7 @@ std::vector&lt;HueFinder::HueIdentification&gt; HueFinder::FindBridges() const
56 unsigned int start = p.first.find("//") + 2; 56 unsigned int start = p.first.find("//") + 2;
57 unsigned int length = p.first.find(":", start) - start; 57 unsigned int length = p.first.find(":", start) - start;
58 bridge.ip = p.first.substr(start, length); 58 bridge.ip = p.first.substr(start, length);
59 - std::string desc = HttpHandler().sendRequestGetBody("GET /description.xml HTTP/1.0\r\nContent-Type: application/xml\r\nContent-Length: 0\r\n\r\n\r\n\r\n", bridge.ip); 59 + std::string desc = HttpHandler().GETString("/description.xml", "application/xml", "", bridge.ip);
60 std::smatch matchResult; 60 std::smatch matchResult;
61 if (std::regex_search(desc, manufRegex) && std::regex_search(desc, manURLRegex) && std::regex_search(desc, modelRegex) && std::regex_search(desc, matchResult, serialRegex)) 61 if (std::regex_search(desc, manufRegex) && std::regex_search(desc, manURLRegex) && std::regex_search(desc, modelRegex) && std::regex_search(desc, matchResult, serialRegex))
62 { 62 {
@@ -106,29 +106,13 @@ const std::map&lt;std::string, std::string&gt;&amp; HueFinder::GetAllUsernames() const @@ -106,29 +106,13 @@ const std::map&lt;std::string, std::string&gt;&amp; HueFinder::GetAllUsernames() const
106 return usernames; 106 return usernames;
107 } 107 }
108 108
  109 +//! \todo Remove duplicate code found in HueFinder::RequestUsername and Hue::requestUsername
109 std::string HueFinder::RequestUsername(const std::string & ip) const 110 std::string HueFinder::RequestUsername(const std::string & ip) const
110 { 111 {
111 std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link button was presed we got 30 seconds to get our username for control 112 std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link button was presed we got 30 seconds to get our username for control
112 113
113 Json::Value request; 114 Json::Value request;
114 - request["devicetype"] = "HuePlusPlus#System User";  
115 -  
116 - // POST /api HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: <length>\r\n\r\n<content>\r\n\r\n  
117 -  
118 - std::string post;  
119 - post.append("POST /api HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ");  
120 - post.append(std::to_string(request.toStyledString().size()));  
121 - post.append("\r\n\r\n");  
122 - post.append(request.toStyledString());  
123 - post.append("\r\n\r\n");  
124 -  
125 - std::cout << post << std::endl;  
126 - std::cout << ip << std::endl;  
127 -  
128 - Json::CharReaderBuilder builder;  
129 - builder["collectComments"] = false;  
130 - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());  
131 - std::string error; 115 + request["devicetype"] = "HuePlusPlus#User";
132 116
133 Json::Value answer; 117 Json::Value answer;
134 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); 118 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
@@ -138,12 +122,8 @@ std::string HueFinder::RequestUsername(const std::string &amp; ip) const @@ -138,12 +122,8 @@ std::string HueFinder::RequestUsername(const std::string &amp; ip) const
138 if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1)) 122 if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1))
139 { 123 {
140 lastCheck = std::chrono::steady_clock::now(); 124 lastCheck = std::chrono::steady_clock::now();
141 - std::string postAnswer = HttpHandler().sendRequestGetBody(post.c_str(), ip, 80);  
142 - if (!reader->parse(postAnswer.c_str(), postAnswer.c_str() + postAnswer.length(), &answer, &error))  
143 - {  
144 - std::cout << "Error while parsing JSON in getUsername of Hue: " << error << std::endl;  
145 - throw(std::runtime_error("Error while parsing JSON in getUsername of Hue"));  
146 - } 125 + answer = HttpHandler().GETJson("/api", request, ip);
  126 +
147 if (answer[0]["success"] != Json::nullValue) 127 if (answer[0]["success"] != Json::nullValue)
148 { 128 {
149 // [{"success":{"username": "83b7780291a6ceffbe0bd049104df"}}] 129 // [{"success":{"username": "83b7780291a6ceffbe0bd049104df"}}]
@@ -161,8 +141,8 @@ std::string HueFinder::RequestUsername(const std::string &amp; ip) const @@ -161,8 +141,8 @@ std::string HueFinder::RequestUsername(const std::string &amp; ip) const
161 } 141 }
162 142
163 143
164 -Hue::Hue(const std::string& ip, const std::string& username) :  
165 -ip(ip), 144 +Hue::Hue(const std::string& ip, const std::string& username) :
  145 +ip(ip),
166 username(username) 146 username(username)
167 { 147 {
168 simpleBrightnessStrategy = std::make_shared<SimpleBrightnessStrategy>(); 148 simpleBrightnessStrategy = std::make_shared<SimpleBrightnessStrategy>();
@@ -180,26 +160,9 @@ std::string Hue::getBridgeIP() @@ -180,26 +160,9 @@ std::string Hue::getBridgeIP()
180 void Hue::requestUsername(const std::string& ip) 160 void Hue::requestUsername(const std::string& ip)
181 { 161 {
182 std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link button was presed we got 30 seconds to get our username for control 162 std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link button was presed we got 30 seconds to get our username for control
183 -  
184 - Json::Value request;  
185 - request["devicetype"] = "Enwiro Smarthome#System User";  
186 -  
187 - // POST /api HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: <length>\r\n\r\n<content>\r\n\r\n  
188 -  
189 - std::string post;  
190 - post.append("POST /api HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ");  
191 - post.append(std::to_string(request.toStyledString().size()));  
192 - post.append("\r\n\r\n");  
193 - post.append(request.toStyledString());  
194 - post.append("\r\n\r\n");  
195 163
196 - std::cout << post << std::endl;  
197 - std::cout << ip << std::endl;  
198 -  
199 - Json::CharReaderBuilder builder;  
200 - builder["collectComments"] = false;  
201 - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());  
202 - std::string error; 164 + Json::Value request;
  165 + request["devicetype"] = "HuePlusPlus#User";
203 166
204 Json::Value answer; 167 Json::Value answer;
205 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); 168 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
@@ -209,12 +172,7 @@ void Hue::requestUsername(const std::string&amp; ip) @@ -209,12 +172,7 @@ void Hue::requestUsername(const std::string&amp; ip)
209 if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1)) 172 if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1))
210 { 173 {
211 lastCheck = std::chrono::steady_clock::now(); 174 lastCheck = std::chrono::steady_clock::now();
212 - std::string postAnswer = HttpHandler().sendRequestGetBody(post.c_str(), ip, 80);  
213 - if (!reader->parse(postAnswer.c_str(), postAnswer.c_str() + postAnswer.length(), &answer, &error))  
214 - {  
215 - std::cout << "Error while parsing JSON in getUsername of Hue: " << error << std::endl;  
216 - throw(std::runtime_error("Error while parsing JSON in getUsername of Hue"));  
217 - } 175 + answer = HttpHandler().GETJson("/api", request, ip);
218 if (answer[0]["success"] != Json::nullValue) 176 if (answer[0]["success"] != Json::nullValue)
219 { 177 {
220 // [{"success":{"username": "<username>"}}] 178 // [{"success":{"username": "<username>"}}]
@@ -326,30 +284,13 @@ void Hue::refreshState() @@ -326,30 +284,13 @@ void Hue::refreshState()
326 { 284 {
327 return; 285 return;
328 } 286 }
329 - std::string get;  
330 - get.append("GET /api/");  
331 - get.append(username);  
332 - get.append(" HTTP / 1.0\r\nContent - Type: application / json\r\nContent - Length: 0\r\n\r\n\r\n\r\n");  
333 -  
334 - Json::CharReaderBuilder builder;  
335 - builder["collectComments"] = false;  
336 - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());  
337 - std::string error;  
338 -  
339 - Json::Value answer;  
340 - std::string postAnswer = HttpHandler().sendRequestGetBody(get.c_str(), ip, 80);  
341 - //std::cout <<"\""<< postAnswer << "\"\n" << std::endl;  
342 - if (!reader->parse(postAnswer.c_str(), postAnswer.c_str() + postAnswer.length(), &answer, &error))  
343 - {  
344 - std::cout << "Error while parsing JSON in refreshState of Hue: " << error << std::endl;  
345 - throw(std::runtime_error("Error while parsing JSON in refreshState of Hue"));  
346 - } 287 + Json::Value answer = HttpHandler().GETJson("/api/"+username, Json::objectValue, ip);
347 if (answer.isObject() && answer.isMember("lights")) 288 if (answer.isObject() && answer.isMember("lights"))
348 { 289 {
349 state = answer; 290 state = answer;
350 } 291 }
351 else 292 else
352 { 293 {
353 - std::cout << "Answer in refreshState() of HttpHandler().sendRequestGetBody() is not expected!\n"; 294 + std::cout << "Answer in refreshState() of HttpHandler().sendGetHTTPBody() is not expected!\n";
354 } 295 }
355 } 296 }
hueplusplus/HueLight.cpp 100755 → 100644
@@ -78,7 +78,7 @@ bool HueLight::alert() @@ -78,7 +78,7 @@ bool HueLight::alert()
78 return false; 78 return false;
79 } 79 }
80 80
81 -HueLight::HueLight(const std::string& ip, const std::string& username, int id) 81 +HueLight::HueLight(const std::string& ip, const std::string& username, int id)
82 : HueLight(ip, username, id, nullptr, nullptr, nullptr) 82 : HueLight(ip, username, id, nullptr, nullptr, nullptr)
83 {} 83 {}
84 84
@@ -173,54 +173,16 @@ bool HueLight::OffNoRefresh(uint8_t transition) @@ -173,54 +173,16 @@ bool HueLight::OffNoRefresh(uint8_t transition)
173 173
174 Json::Value HueLight::SendPutRequest(const Json::Value& request) 174 Json::Value HueLight::SendPutRequest(const Json::Value& request)
175 { 175 {
176 - std::string put;  
177 - put.append("PUT /api/");  
178 - put.append(username);  
179 - put.append("/lights/");  
180 - put.append(std::to_string(id));  
181 - put.append("/state HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ");  
182 - put.append(std::to_string(request.toStyledString().size()));  
183 - put.append("\r\n\r\n");  
184 - put.append(request.toStyledString());  
185 - put.append("\r\n\r\n");  
186 -  
187 - Json::CharReaderBuilder builder;  
188 - builder["collectComments"] = false;  
189 - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());  
190 - std::string error;  
191 - std::string putAnswer = HttpHandler().sendRequestGetBody(put.c_str(), ip, 80);  
192 -  
193 - Json::Value result;  
194 - if (!reader->parse(putAnswer.c_str(), putAnswer.c_str() + putAnswer.length(), &result, &error))  
195 - {  
196 - std::cout << "Error while parsing JSON in function SendRequest() of HueLight: " << error << std::endl;  
197 - throw(std::runtime_error("Error while parsing JSON in function SendRequest() of HueLight"));  
198 - }  
199 - return result; 176 + return HttpHandler().PUTJson("/api/"+username+"/lights/"+std::to_string(id)+"/state", request, ip);
200 } 177 }
201 178
202 void HueLight::refreshState() 179 void HueLight::refreshState()
203 { 180 {
204 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); 181 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
205 std::cout << "\tRefreshing lampstate of lamp with id: " << id << ", ip: " << ip << "\n"; 182 std::cout << "\tRefreshing lampstate of lamp with id: " << id << ", ip: " << ip << "\n";
206 - // GET /api/<username>/lights/<id> HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: <length>\r\n\r\n<content>\r\n\r\n  
207 - std::string get;  
208 - get.append("GET /api/");  
209 - get.append(username);  
210 - get.append("/lights/");  
211 - get.append(std::to_string(id));  
212 - get.append(" HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: 0\r\n\r\n\r\n\r\n");  
213 -  
214 - Json::CharReaderBuilder builder;  
215 - builder["collectComments"] = false;  
216 - std::unique_ptr<Json::CharReader> reader = std::unique_ptr<Json::CharReader>(builder.newCharReader());  
217 - std::string error;  
218 - std::string getAnswer = HttpHandler().sendRequestGetBody(get.c_str(), ip, 80);  
219 - // todo check whether getAnswer is containing right information  
220 - if (!reader->parse(getAnswer.c_str(), getAnswer.c_str() + getAnswer.length(), &state, &error))  
221 - {  
222 - std::cout << "Error while parsing JSON in function refreshState() of HueLight: " << error << std::endl;  
223 - throw(std::runtime_error("Error while parsing JSON in function refreshState() of HueLight"));  
224 - } 183 +
  184 + state = HttpHandler().GETJson("/api/"+username+"/lights/"+std::to_string(id), Json::objectValue, ip);
  185 + //! \todo check whether getAnswer is containing right information
  186 +
225 std::cout << "\tRefresh state took: " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl; 187 std::cout << "\tRefresh state took: " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl;
226 -}  
227 \ No newline at end of file 188 \ No newline at end of file
  189 +}
hueplusplus/include/HttpHandler.h
@@ -23,27 +23,30 @@ @@ -23,27 +23,30 @@
23 #include <string> 23 #include <string>
24 #include <vector> 24 #include <vector>
25 25
  26 +#include "json/json.h"
  27 +
26 //! Class to handle http requests and multicast requests 28 //! Class to handle http requests and multicast requests
27 class HttpHandler 29 class HttpHandler
28 { 30 {
29 public: 31 public:
30 - //! \brief Function that sends a http request with the specified message and returns the response. 32 + //! \brief Function that sends a given message to the specified host and returns the response.
31 //! 33 //!
32 //! It returns a string containing the response of the host. 34 //! It returns a string containing the response of the host.
33 - //! \param msg String that contains the request that is sent to the specified address 35 + //! \param msg String that contains the message that is sent to the specified address
34 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" 36 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
35 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 37 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
36 //! \return String containing the response of the host 38 //! \return String containing the response of the host
37 - std::string sendRequest(const std::string &msg, const std::string &adr, int port=80); 39 + std::string send(const std::string &msg, const std::string &adr, int port=80);
38 40
39 - //! \brief Function that sends a http request with the specified message and returns the body of the response. 41 + //! \brief Function that sends a given message to the specified host and returns the body of the response.
40 //! 42 //!
41 //! It returns a string containing only the body of the response of the host. 43 //! It returns a string containing only the body of the response of the host.
42 - //! \param msg String that contains the request that is sent to the specified address 44 + //! Note if no body is found a runtime error is thrown!
  45 + //! \param msg String that contains the message that is sent to the specified address
43 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" 46 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
44 //! \param port Optional integer that specifies the port to which the request is sent. Default is 80 47 //! \param port Optional integer that specifies the port to which the request is sent. Default is 80
45 //! \return String containing the body of the response of the host 48 //! \return String containing the body of the response of the host
46 - std::string sendRequestGetBody(const std::string &msg, const std::string &adr, int port = 80); 49 + std::string sendGetHTTPBody(const std::string &msg, const std::string &adr, int port = 80);
47 50
48 //! \brief Function that sends a multicast request with the specified message. 51 //! \brief Function that sends a multicast request with the specified message.
49 //! 52 //!
@@ -54,6 +57,88 @@ public: @@ -54,6 +57,88 @@ public:
54 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5 57 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5
55 //! \return Vector containing strings of each answer received 58 //! \return Vector containing strings of each answer received
56 std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5); 59 std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5);
  60 +
  61 + //! \brief Function that sends a HTTP request with the given method to the specified host and returns the body of the response.
  62 + //!
  63 + //! It returns a string containing only the body of the response of the host.
  64 + //! Note body can also be left empty!
  65 + //! \param method String that contains the HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ...
  66 + //! \param uri String that contains the uniform resource identifier
  67 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  68 + //! \param body String that contains the data of the request
  69 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  70 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  71 + //! \return String containing the body of the response of the host
  72 + std::string sendHTTPRequest(std::string method, std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  73 +
  74 + //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response.
  75 + //!
  76 + //! It returns a string containing only the body of the response of the host.
  77 + //! Note body can also be left empty!
  78 + //! \param uri String that contains the uniform resource identifier
  79 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  80 + //! \param body String that contains the data of the request
  81 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  82 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  83 + //! \return String containing the body of the response of the host
  84 + std::string GETString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  85 +
  86 + //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response.
  87 + //!
  88 + //! It returns a string containing only the body of the response of the host.
  89 + //! Note body can also be left empty!
  90 + //! \param uri String that contains the uniform resource identifier
  91 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  92 + //! \param body String that contains the data of the request
  93 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  94 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  95 + //! \return String containing the body of the response of the host
  96 + std::string POSTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  97 +
  98 + //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response.
  99 + //!
  100 + //! It returns a string containing only the body of the response of the host.
  101 + //! Note body can also be left empty!
  102 + //! \param uri String that contains the uniform resource identifier
  103 + //! \param content_type String that contains the type(MIME) of the body data e.g. "text/html", "application/json", ...
  104 + //! \param body String that contains the data of the request
  105 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  106 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  107 + //! \return String containing the body of the response of the host
  108 + std::string PUTString(std::string uri, std::string content_type, std::string body, const std::string &adr, int port=80);
  109 +
  110 + //! \brief Function that sends a HTTP GET request to the specified host and returns the body of the response.
  111 + //!
  112 + //! It returns a Json::Value parsed from the body of the response of the host.
  113 + //! Note body can also be left empty!
  114 + //! \param uri String that contains the uniform resource identifier
  115 + //! \param body Json::Value that contains the data of the request
  116 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  117 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  118 + //! \return Json::Value containing the parsed body of the response of the host
  119 + Json::Value GETJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80);
  120 +
  121 + //! \brief Function that sends a HTTP POST request to the specified host and returns the body of the response.
  122 + //!
  123 + //! It returns a Json::Value parsed from the body of the response of the host.
  124 + //! Note body can also be left empty!
  125 + //! \param uri String that contains the uniform resource identifier
  126 + //! \param body Json::Value that contains the data of the request
  127 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  128 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  129 + //! \return Json::Value containing the parsed body of the response of the host
  130 + Json::Value POSTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80);
  131 +
  132 + //! \brief Function that sends a HTTP PUT request to the specified host and returns the body of the response.
  133 + //!
  134 + //! It returns a Json::Value parsed from the body of the response of the host.
  135 + //! Note body can also be left empty!
  136 + //! \param uri String that contains the uniform resource identifier
  137 + //! \param body Json::Value that contains the data of the request
  138 + //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
  139 + //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
  140 + //! \return Json::Value containing the parsed body of the response of the host
  141 + Json::Value PUTJson(std::string uri, const Json::Value& body, const std::string &adr, int port=80);
57 }; 142 };
58 143
59 #endif 144 #endif