Fixed some issues in HTTP, added GET param support, fixed a bug, etc... lots of maintenaince.
This commit is contained in:
parent
bdcfe32619
commit
c596264255
5 changed files with 160 additions and 74 deletions
|
@ -90,63 +90,110 @@ void Authorize( Json::Value & Request, Json::Value & Storage, Json::Value & Resp
|
|||
}
|
||||
|
||||
void CheckConfig(Json::Value & in, Json::Value & out){
|
||||
|
||||
Json::ValueIterator jit;
|
||||
if (in.isObject()){
|
||||
for (jit = in.begin(); jit != in.end(); jit++){
|
||||
if (out.isObject() && out.isMember(jit.key().asString())){
|
||||
Log("CONF", "Updated configuration value "+jit.key().asString(), out);
|
||||
}else{
|
||||
Log("CONF", "New configuration value "+jit.key().asString(), out);
|
||||
}
|
||||
}
|
||||
if (out.isObject()){
|
||||
for (jit = out.begin(); jit != out.end(); jit++){
|
||||
if (!in.isMember(jit.key().asString())){
|
||||
Log("CONF", "Deleted configuration value "+jit.key().asString(), out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out = in;
|
||||
}
|
||||
|
||||
void CheckStreams(Json::Value & in, Json::Value & out){
|
||||
|
||||
Json::ValueIterator jit;
|
||||
if (in.isObject()){
|
||||
for (jit = in.begin(); jit != in.end(); jit++){
|
||||
if (out.isObject() && out.isMember(jit.key().asString())){
|
||||
Log("STRM", "Updated stream "+jit.key().asString(), out);
|
||||
}else{
|
||||
Log("STRM", "New stream "+jit.key().asString(), out);
|
||||
}
|
||||
}
|
||||
if (out.isObject()){
|
||||
for (jit = out.begin(); jit != out.end(); jit++){
|
||||
if (!in.isMember(jit.key().asString())){
|
||||
Log("STRM", "Deleted stream "+jit.key().asString(), out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out = in;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Socket::Server API_Socket = Socket::Server( 4242, "0.0.0.0", true );
|
||||
Socket::Connection TempConn;
|
||||
time_t lastuplink = 0;
|
||||
Socket::Server API_Socket = Socket::Server(4242, "0.0.0.0", true);
|
||||
Socket::Server Stats_Socket = Socket::Server("/tmp/ddv_statistics", true);
|
||||
Socket::Connection Incoming;
|
||||
std::vector< ConnectedUser > users;
|
||||
HTTP::Parser HTTP_R, HTTP_S;
|
||||
Json::Value Request;
|
||||
Json::Value Response;
|
||||
Json::Value Storage;
|
||||
Json::Value Request = Json::Value(Json::objectValue);
|
||||
Json::Value Response = Json::Value(Json::objectValue);
|
||||
Json::Value Storage = Json::Value(Json::objectValue);
|
||||
Json::Reader JsonParse;
|
||||
std::string jsonp;
|
||||
JsonParse.parse(ReadFile("config.json"), Storage, false);
|
||||
Storage["config"] = Json::Value(Json::objectValue);
|
||||
Storage["account"]["gearbox"]["password"] = Json::Value("7e0f87b116377621a75a6440ac74dcf4");
|
||||
while (API_Socket.connected()){
|
||||
usleep(10000); //sleep for 10 ms - prevents 100% CPU time
|
||||
TempConn = API_Socket.accept();
|
||||
if (TempConn.connected()){users.push_back(TempConn);}
|
||||
Incoming = API_Socket.accept();
|
||||
if (Incoming.connected()){users.push_back(Incoming);}
|
||||
if (users.size() > 0){
|
||||
for( std::vector< ConnectedUser >::iterator it = users.end() - 1; it >= users.begin(); it-- ) {
|
||||
if( !(*it).C.connected() ) {
|
||||
(*it).C.close();
|
||||
users.erase( it );
|
||||
for( std::vector< ConnectedUser >::iterator it = users.end() - 1; it >= users.begin(); it--) {
|
||||
if (!it->C.connected()){
|
||||
it->C.close();
|
||||
users.erase(it);
|
||||
}
|
||||
if ((*it).H.Read((*it).C)){
|
||||
if (it->H.Read(it->C)){
|
||||
Response.clear(); //make sure no data leaks from previous requests
|
||||
std::cout << "Body: " << HTTP_R.body << std::endl;
|
||||
std::cout << "Command: " << HTTP_R.GetVar("command") << std::endl;
|
||||
JsonParse.parse(HTTP_R.GetVar("command"), Request, false);
|
||||
std::cout << Request.toStyledString() << std::endl;
|
||||
Authorize(Request, Storage, Response, (*it));
|
||||
if ((*it).Authorized){
|
||||
//Parse config and streams from the request.
|
||||
if (Request.isMember("config")){CheckConfig(Request["config"], Storage["config"]);}
|
||||
if (Request.isMember("streams")){CheckStreams(Request["streams"], Storage["streams"]);}
|
||||
//sent current configuration, no matter if it was changed or not
|
||||
Response["streams"] = Storage["streams"];
|
||||
Response["config"] = Storage["config"];
|
||||
//add the current unix time to the config, for syncing reasons
|
||||
Response["config"]["time"] = (Json::Value::UInt)time(0);
|
||||
//sent any available logs and statistics
|
||||
Response["log"] = Storage["log"];
|
||||
Response["statistics"] = Storage["statistics"];
|
||||
//clear log and statistics to prevent useless data transfer
|
||||
Storage["log"].clear();
|
||||
Storage["statistics"].clear();
|
||||
if (!JsonParse.parse(it->H.GetVar("command"), Request, false)){
|
||||
Log("HTTP", "Failed to parse JSON: "+it->H.GetVar("command"), Storage);
|
||||
Response["authorize"]["status"] = "INVALID";
|
||||
}else{
|
||||
std::cout << "Request: " << Request.toStyledString() << std::endl;
|
||||
Authorize(Request, Storage, Response, (*it));
|
||||
if (it->Authorized){
|
||||
//Parse config and streams from the request.
|
||||
if (Request.isMember("config")){CheckConfig(Request["config"], Storage["config"]);}
|
||||
//if (Request.isMember("streams")){CheckStreams(Request["streams"], Storage["streams"]);}
|
||||
//sent current configuration, no matter if it was changed or not
|
||||
//Response["streams"] = Storage["streams"];
|
||||
Response["config"] = Storage["config"];
|
||||
//add required data to the current unix time to the config, for syncing reasons
|
||||
Response["config"]["time"] = (Json::Value::UInt)time(0);
|
||||
if (!Response["config"].isMember("serverid")){Response["config"]["serverid"] = "";}
|
||||
//sent any available logs and statistics
|
||||
Response["log"] = Storage["log"];
|
||||
Response["statistics"] = Storage["statistics"];
|
||||
//clear log and statistics to prevent useless data transfer
|
||||
Storage["log"].clear();
|
||||
Storage["statistics"].clear();
|
||||
}
|
||||
}
|
||||
(*it).H.Clean();
|
||||
(*it).H.protocol = "HTTP/1.1";
|
||||
(*it).H.SetHeader( "Content-Type", "text/javascript" );
|
||||
(*it).H.SetBody( Response.toStyledString() + "\n\n" );
|
||||
(*it).C.write( (*it).H.BuildResponse( "200", "OK" ) );
|
||||
(*it).H.Clean();
|
||||
jsonp = "";
|
||||
if (it->H.GetVar("callback") != ""){jsonp = it->H.GetVar("callback");}
|
||||
if (it->H.GetVar("jsonp") != ""){jsonp = it->H.GetVar("jsonp");}
|
||||
it->H.Clean();
|
||||
it->H.protocol = "HTTP/1.0";
|
||||
it->H.SetHeader("Content-Type", "text/javascript");
|
||||
if (jsonp == ""){
|
||||
it->H.SetBody(Response.toStyledString()+"\n\n");
|
||||
}else{
|
||||
it->H.SetBody(jsonp+"("+Response.toStyledString()+");\n\n");
|
||||
}
|
||||
it->C.write(it->H.BuildResponse("200", "OK"));
|
||||
it->H.Clean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue