simplified http get and added server header
This commit is contained in:
+17
-26
@@ -23,6 +23,7 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -872,6 +873,7 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
|
|||||||
size_t argPos = 1;
|
size_t argPos = 1;
|
||||||
string uri = args[argPos++];
|
string uri = args[argPos++];
|
||||||
ostringstream result;
|
ostringstream result;
|
||||||
|
int type = -1;
|
||||||
|
|
||||||
if (strncmp(uri.c_str(), "/data/", 6) == 0) {
|
if (strncmp(uri.c_str(), "/data/", 6) == 0) {
|
||||||
string clazz = "", name = "";
|
string clazz = "", name = "";
|
||||||
@@ -979,15 +981,7 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
|
|||||||
result << ",\n \"lastup\": " << setw(0) << dec << static_cast<unsigned>(maxLastUp);
|
result << ",\n \"lastup\": " << setw(0) << dec << static_cast<unsigned>(maxLastUp);
|
||||||
result << "\n }";
|
result << "\n }";
|
||||||
result << "\n}";
|
result << "\n}";
|
||||||
string str = result.str();
|
type = 6;
|
||||||
result.str("");
|
|
||||||
result.clear();
|
|
||||||
result << "HTTP/1.0 200 OK\r\n";
|
|
||||||
result << "Content-Type: application/json;charset=utf-8\r\n";
|
|
||||||
result << "Content-Length: " << setw(0) << dec << static_cast<unsigned>(str.length());
|
|
||||||
result << "\r\nConnection: close\r\n\r\n";
|
|
||||||
result << str;
|
|
||||||
return result.str();
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (uri.length() < 1 || uri[0] != '/' || uri.find("//") != string::npos || uri.find("..") != string::npos) {
|
if (uri.length() < 1 || uri[0] != '/' || uri.find("//") != string::npos || uri.find("..") != string::npos) {
|
||||||
@@ -997,7 +991,6 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
|
|||||||
if (uri[uri.length()-1] == '/')
|
if (uri[uri.length()-1] == '/')
|
||||||
filename += "index.html";
|
filename += "index.html";
|
||||||
|
|
||||||
int type = -1;
|
|
||||||
size_t pos = filename.find_last_of('.');
|
size_t pos = filename.find_last_of('.');
|
||||||
if (pos != string::npos && pos != filename.length() - 1 && pos >= filename.length() - 5) {
|
if (pos != string::npos && pos != filename.length() - 1 && pos >= filename.length() - 5) {
|
||||||
string ext = filename.substr(pos+1);
|
string ext = filename.substr(pos+1);
|
||||||
@@ -1027,10 +1020,18 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
|
|||||||
} else {
|
} else {
|
||||||
ifs >> result.rdbuf();
|
ifs >> result.rdbuf();
|
||||||
ifs.close();
|
ifs.close();
|
||||||
string str = result.str();
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string data = ret==RESULT_OK ? result.str() : "";
|
||||||
result.str("");
|
result.str("");
|
||||||
result.clear();
|
result.clear();
|
||||||
result << "HTTP/1.0 200 OK\r\nContent-Type: ";
|
result << "HTTP/1.0 ";
|
||||||
|
switch (ret) {
|
||||||
|
case RESULT_OK:
|
||||||
|
result << "200 OK\r\nContent-Type: ";
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 1:
|
case 1:
|
||||||
result << "text/css";
|
result << "text/css";
|
||||||
@@ -1054,19 +1055,8 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
|
|||||||
result << "text/html";
|
result << "text/html";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
result << "\r\nContent-Length: " << setw(0) << dec << static_cast<unsigned>(str.length());
|
result << "\r\nContent-Length: " << setw(0) << dec << static_cast<unsigned>(data.length());
|
||||||
result << "\r\nConnection: close\r\n\r\n";
|
break;
|
||||||
result << str;
|
|
||||||
connected = false;
|
|
||||||
return result.str();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.str("");
|
|
||||||
result.clear();
|
|
||||||
result << "HTTP/1.0 ";
|
|
||||||
switch (ret) {
|
|
||||||
case RESULT_ERR_NOTFOUND:
|
case RESULT_ERR_NOTFOUND:
|
||||||
result << "404 Not Found";
|
result << "404 Not Found";
|
||||||
break;
|
break;
|
||||||
@@ -1079,7 +1069,8 @@ string MainLoop::executeGet(vector<string> &args, bool& connected)
|
|||||||
result << "500 Internal Server Error";
|
result << "500 Internal Server Error";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
result << "\r\nConnection: close\r\n\r\n";
|
result << "\r\nServer: ebusd/" PACKAGE_VERSION "\r\n\r\n";
|
||||||
|
result << data;
|
||||||
connected = false;
|
connected = false;
|
||||||
return result.str();
|
return result.str();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user