Converted triggers to HTTP downloader requests

This commit is contained in:
Thulinma 2017-10-31 22:47:30 +01:00
parent 11ea8b0ef2
commit 6bf980f706

View file

@ -16,7 +16,7 @@
#include "triggers.h" #include "triggers.h"
#include "bitfields.h" //for strToBool #include "bitfields.h" //for strToBool
#include "defines.h" //for FAIL_MSG and INFO_MSG #include "defines.h" //for FAIL_MSG and INFO_MSG
#include "http_parser.h" //for sending http request #include "downloader.h" //for sending http request
#include "procs.h" //for StartPiped #include "procs.h" //for StartPiped
#include "shared_memory.h" #include "shared_memory.h"
#include "util.h" #include "util.h"
@ -36,36 +36,16 @@ namespace Triggers{
return "true"; return "true";
} }
INFO_MSG("Executing %s trigger: %s (%s)", trigger.c_str(), value.c_str(), sync ? "blocking" : "asynchronous"); INFO_MSG("Executing %s trigger: %s (%s)", trigger.c_str(), value.c_str(), sync ? "blocking" : "asynchronous");
if (value.substr(0, 7) == "http://"){// interpret as url if (value.substr(0, 7) == "http://" || value.substr(0, 8) == "https://"){// interpret as url
std::string url = value.substr(value.find("://") + 3); // contains server+url HTTP::Downloader DL;
std::string server = url.substr(0, url.find('/')); DL.setHeader("X-Trigger", trigger);
int port = 80; DL.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (server.find(':') != std::string::npos){ HTTP::URL url(value);
port = atoi(server.data() + server.find(':') + 1); if (DL.post(url, payload, sync) && sync && DL.isOk()){
server.erase(server.find(':')); return DL.data();
} }
url = url.substr(url.find('/')); /// \TODO Send default response.
Socket::Connection conn(server, port, false);
HTTP::Parser H;
H.url = url;
H.method = "POST";
H.SetHeader("Host", server + ":" + JSON::Value((long long)port).toString());
H.SetHeader("Content-Type", "application/x-www-form-urlencoded");
H.SetHeader("X-Trigger", trigger);
H.SetBody(payload);
H.SendRequest(conn);
H.Clean();
if (sync){// if sync!=0 wait for response
while (conn && (!conn.spool() || !H.Read(conn))){}
conn.close();
/// \todo Handle errors!
return H.body;
}else{
conn.close();
return "true"; return "true";
}
}else{// send payload to stdin of newly forked process }else{// send payload to stdin of newly forked process
int fdIn = -1; int fdIn = -1;
int fdOut = -1; int fdOut = -1;