Fixed some issues in HTTP, added GET param support, fixed a bug, etc... lots of maintenaince.

This commit is contained in:
Thulinma 2011-10-16 16:45:55 +02:00
parent bdcfe32619
commit c596264255
5 changed files with 160 additions and 74 deletions

View file

@ -90,50 +90,89 @@ void Authorize( Json::Value & Request, Json::Value & Storage, Json::Value & Resp
} }
void CheckConfig(Json::Value & in, Json::Value & out){ 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){ 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() { int main() {
time_t lastuplink = 0;
Socket::Server API_Socket = Socket::Server(4242, "0.0.0.0", true); Socket::Server API_Socket = Socket::Server(4242, "0.0.0.0", true);
Socket::Connection TempConn; Socket::Server Stats_Socket = Socket::Server("/tmp/ddv_statistics", true);
Socket::Connection Incoming;
std::vector< ConnectedUser > users; std::vector< ConnectedUser > users;
HTTP::Parser HTTP_R, HTTP_S; Json::Value Request = Json::Value(Json::objectValue);
Json::Value Request; Json::Value Response = Json::Value(Json::objectValue);
Json::Value Response; Json::Value Storage = Json::Value(Json::objectValue);
Json::Value Storage;
Json::Reader JsonParse; Json::Reader JsonParse;
std::string jsonp;
JsonParse.parse(ReadFile("config.json"), Storage, false); JsonParse.parse(ReadFile("config.json"), Storage, false);
Storage["config"] = Json::Value(Json::objectValue);
Storage["account"]["gearbox"]["password"] = Json::Value("7e0f87b116377621a75a6440ac74dcf4"); Storage["account"]["gearbox"]["password"] = Json::Value("7e0f87b116377621a75a6440ac74dcf4");
while (API_Socket.connected()){ while (API_Socket.connected()){
usleep(10000); //sleep for 10 ms - prevents 100% CPU time usleep(10000); //sleep for 10 ms - prevents 100% CPU time
TempConn = API_Socket.accept(); Incoming = API_Socket.accept();
if (TempConn.connected()){users.push_back(TempConn);} if (Incoming.connected()){users.push_back(Incoming);}
if (users.size() > 0){ if (users.size() > 0){
for( std::vector< ConnectedUser >::iterator it = users.end() - 1; it >= users.begin(); it--) { for( std::vector< ConnectedUser >::iterator it = users.end() - 1; it >= users.begin(); it--) {
if( !(*it).C.connected() ) { if (!it->C.connected()){
(*it).C.close(); it->C.close();
users.erase(it); 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 Response.clear(); //make sure no data leaks from previous requests
std::cout << "Body: " << HTTP_R.body << std::endl; if (!JsonParse.parse(it->H.GetVar("command"), Request, false)){
std::cout << "Command: " << HTTP_R.GetVar("command") << std::endl; Log("HTTP", "Failed to parse JSON: "+it->H.GetVar("command"), Storage);
JsonParse.parse(HTTP_R.GetVar("command"), Request, false); Response["authorize"]["status"] = "INVALID";
std::cout << Request.toStyledString() << std::endl; }else{
std::cout << "Request: " << Request.toStyledString() << std::endl;
Authorize(Request, Storage, Response, (*it)); Authorize(Request, Storage, Response, (*it));
if ((*it).Authorized){ if (it->Authorized){
//Parse config and streams from the request. //Parse config and streams from the request.
if (Request.isMember("config")){CheckConfig(Request["config"], Storage["config"]);} if (Request.isMember("config")){CheckConfig(Request["config"], Storage["config"]);}
if (Request.isMember("streams")){CheckStreams(Request["streams"], Storage["streams"]);} //if (Request.isMember("streams")){CheckStreams(Request["streams"], Storage["streams"]);}
//sent current configuration, no matter if it was changed or not //sent current configuration, no matter if it was changed or not
Response["streams"] = Storage["streams"]; //Response["streams"] = Storage["streams"];
Response["config"] = Storage["config"]; Response["config"] = Storage["config"];
//add the current unix time to the config, for syncing reasons //add required data to the current unix time to the config, for syncing reasons
Response["config"]["time"] = (Json::Value::UInt)time(0); Response["config"]["time"] = (Json::Value::UInt)time(0);
if (!Response["config"].isMember("serverid")){Response["config"]["serverid"] = "";}
//sent any available logs and statistics //sent any available logs and statistics
Response["log"] = Storage["log"]; Response["log"] = Storage["log"];
Response["statistics"] = Storage["statistics"]; Response["statistics"] = Storage["statistics"];
@ -141,12 +180,20 @@ int main() {
Storage["log"].clear(); Storage["log"].clear();
Storage["statistics"].clear(); Storage["statistics"].clear();
} }
(*it).H.Clean(); }
(*it).H.protocol = "HTTP/1.1"; jsonp = "";
(*it).H.SetHeader( "Content-Type", "text/javascript" ); if (it->H.GetVar("callback") != ""){jsonp = it->H.GetVar("callback");}
(*it).H.SetBody( Response.toStyledString() + "\n\n" ); if (it->H.GetVar("jsonp") != ""){jsonp = it->H.GetVar("jsonp");}
(*it).C.write( (*it).H.BuildResponse( "200", "OK" ) ); it->H.Clean();
(*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();
} }
} }
} }

View file

@ -14,9 +14,9 @@ void HTTP::Parser::Clean(){
method = "GET"; method = "GET";
url = "/"; url = "/";
protocol = "HTTP/1.1"; protocol = "HTTP/1.1";
body = ""; body.clear();
length = 0; length = 0;
HTTPbuffer = ""; HTTPbuffer.clear();
headers.clear(); headers.clear();
vars.clear(); vars.clear();
} }
@ -172,7 +172,10 @@ bool HTTP::Parser::parse(){
if (f != std::string::npos){url = tmpA.substr(0, f); tmpA.erase(0, f+1);} if (f != std::string::npos){url = tmpA.substr(0, f); tmpA.erase(0, f+1);}
f = tmpA.find(' '); f = tmpA.find(' ');
if (f != std::string::npos){protocol = tmpA.substr(0, f); tmpA.erase(0, f+1);} if (f != std::string::npos){protocol = tmpA.substr(0, f); tmpA.erase(0, f+1);}
/// \todo Include GET variable parsing? if (url.find('?') != std::string::npos){
std::string queryvars = url.substr(url.find('?')+1);
parseVars(queryvars); //parse GET variables
}
}else{ }else{
if (tmpA.size() == 0){ if (tmpA.size() == 0){
seenHeaders = true; seenHeaders = true;
@ -191,22 +194,7 @@ bool HTTP::Parser::parse(){
if (HTTPbuffer.length() >= length){ if (HTTPbuffer.length() >= length){
body = HTTPbuffer.substr(0, length); body = HTTPbuffer.substr(0, length);
HTTPbuffer.erase(0, length); HTTPbuffer.erase(0, length);
std::string tmppost = body; parseVars(body); //parse POST variables
std::string varname;
std::string varval;
while (tmppost.find('=') != std::string::npos){
size_t found = tmppost.find('=');
varname = urlunescape((char*)tmppost.substr(0, found).c_str());
tmppost.erase(0, found+1);
found = tmppost.find('&');
varval = urlunescape((char*)tmppost.substr(0, found).c_str());
SetVar(varname, varval);
if (found == std::string::npos){
tmppost.clear();
}else{
tmppost.erase(0, found+1);
}
}
return true; return true;
}else{ }else{
return false; return false;
@ -229,6 +217,26 @@ void HTTP::Parser::SendResponse(Socket::Connection & conn, std::string code, std
conn.write(tmp); conn.write(tmp);
} }
/// Parses GET or POST-style variable data.
/// Saves to internal variable structure using HTTP::Parser::SetVar.
void HTTP::Parser::parseVars(std::string & data){
std::string varname;
std::string varval;
while (data.find('=') != std::string::npos){
size_t found = data.find('=');
varname = urlunescape(data.substr(0, found));
data.erase(0, found+1);
found = data.find('&');
varval = urlunescape(data.substr(0, found));
SetVar(varname, varval);
if (found == std::string::npos){
data.clear();
}else{
data.erase(0, found+1);
}
}
}
/// Sends data as HTTP/1.1 bodypart to conn. /// Sends data as HTTP/1.1 bodypart to conn.
/// HTTP/1.1 chunked encoding is automatically applied if needed. /// HTTP/1.1 chunked encoding is automatically applied if needed.
/// \param conn The Socket::Connection to send the part over. /// \param conn The Socket::Connection to send the part over.
@ -257,24 +265,26 @@ void HTTP::Parser::SendBodyPart(Socket::Connection & conn, std::string bodypart)
} }
} }
/// Unescapes URLencoded C-strings to a std::string. /// Unescapes URLencoded std::strings.
/// This function *will* destroy the input data! std::string HTTP::Parser::urlunescape(std::string in){
std::string HTTP::Parser::urlunescape(char *s){ std::string out;
char *p; for (unsigned int i = 0; i < in.length(); ++i){
for (p = s; *s != '\0'; ++s){ if (in[i] == '%'){
if (*s == '%'){ char tmp = 0;
if (*++s != '\0'){ ++i;
*p = unhex(*s) << 4; if (i < in.length()){
tmp = unhex(in[i]) << 4;
} }
if (*++s != '\0'){ ++i;
*p++ += unhex(*s); if (i < in.length()){
tmp += unhex(in[i]);
} }
out += tmp;
} else { } else {
if (*s == '+'){*p++ = ' ';}else{*p++ = *s;} if (in[i] == '+'){out += ' ';}else{out += in[i];}
} }
} }
*p = '\0'; return out;
return std::string(s);
} }
/// Helper function for urlunescape. /// Helper function for urlunescape.

View file

@ -30,7 +30,7 @@ namespace HTTP{
void SendBodyPart(Socket::Connection & conn, std::string bodypart); void SendBodyPart(Socket::Connection & conn, std::string bodypart);
void Clean(); void Clean();
bool CleanForNext(); bool CleanForNext();
std::string urlunescape(char *s); ///< Unescapes URLencoded C-strings to a std::string. std::string urlunescape(std::string in);
std::string body; std::string body;
std::string method; std::string method;
std::string url; std::string url;
@ -40,6 +40,7 @@ namespace HTTP{
bool seenHeaders; bool seenHeaders;
bool seenReq; bool seenReq;
bool parse(); bool parse();
void parseVars(std::string & data);
std::string HTTPbuffer; std::string HTTPbuffer;
std::map<std::string, std::string> headers; std::map<std::string, std::string> headers;
std::map<std::string, std::string> vars; std::map<std::string, std::string> vars;

View file

@ -10,10 +10,35 @@
#if DEBUG >= 1 #if DEBUG >= 1
#include <iostream> #include <iostream>
#endif #endif
#include <sys/types.h>
#include <pwd.h>
std::map<pid_t, std::string> Util::Procs::plist; std::map<pid_t, std::string> Util::Procs::plist;
bool Util::Procs::handler_set = false; bool Util::Procs::handler_set = false;
/// Sets the current process' running user
void Util::setUser(std::string username){
if (username != "root"){
struct passwd * user_info = getpwnam(username.c_str());
if (!user_info){
#if DEBUG >= 1
fprintf(stderr, "Error: could not setuid %s: could not get PID\n", username.c_str());
#endif
return 1;
}else{
if (setuid(user_info->pw_uid) != 0){
#if DEBUG >= 1
fprintf(stderr, "Error: could not setuid %s: not allowed\n", username.c_str());
#endif
}else{
#if DEBUG >= 3
fprintf(stderr, "Changed user to %s\n", username.c_str());
#endif
}
}
}
}
/// Used internally to capture child signals and update plist. /// Used internally to capture child signals and update plist.
void Util::Procs::childsig_handler(int signum){ void Util::Procs::childsig_handler(int signum){
if (signum != SIGCHLD){return;} if (signum != SIGCHLD){return;}

View file

@ -27,4 +27,7 @@ namespace Util{
static pid_t getPid(std::string name); static pid_t getPid(std::string name);
static std::string getName(pid_t name); static std::string getName(pid_t name);
}; };
static setUser(std::string user);
}; };