This commit is contained in:
Brad Arant 2020-08-05 11:54:58 -07:00
parent 0510ad5dc7
commit bbab38de3a
20 changed files with 90 additions and 79 deletions

View File

@ -7,48 +7,55 @@
#include "Log.h" #include "Log.h"
namespace http { namespace http {
int HTTPHandler::processCommand(std::string request, core::TCPSession *session, std::stringstream &data) { int HTTPHandler::processCommand(std::string request, core::TCPSession *session, std::stringstream &data) {
coreutils::Log(coreutils::LOG_DEBUG_4) << "DATA[" << request << "]"; coreutils::Log(coreutils::LOG_DEBUG_1) << "DATA[" << request << "]";
coreutils::PString request1(request);
if(mode == REQUEST) {
coreutils::PString request1(request);
HTTPRequest httpRequest(request1);
HTTPSession *httpSession = static_cast<HTTPServer &>(session->server).httpSessions.findSessionByHeader(httpRequest);
std::stringstream content; switch(mode) {
case REQUEST:
if(static_cast<HTTPServer &>(session->server).pageList.processRequest(httpRequest, session, httpSession, content)) { httpRequest = new HTTPRequest(request1);
std::string contentType = httpRequest.getHeader("Content-Type"); session->server.commands.grabInput(session, *this);
if(contentType == "multipart/form-data") { mode = IMF;
coreutils::IMFFormData *formdata = (coreutils::IMFFormData *)httpRequest.getBody(); break;
coreutils::Log(coreutils::LOG_DEBUG_2) << "username is '" << formdata->getByName("username") << "'";
}
httpRequest.response.setCode("200"); case IMF:
httpRequest.response.setText("OK"); httpRequest->parse(request1);
data << httpRequest.response.getResponse(content.str()); if(request == "") {
} session->server.commands.clearGrab(session);
mode = REQUEST;
else { processHTTPRequest(session, data);
httpRequest.response.setCode("404");
httpRequest.response.setText("Not Found");
data << httpRequest.response.getResponse(content.str());
} }
}
grabInput();
mode = IMFHEADER;
}
else if(mode == IMFHEADER) {
HTTPHeader header(content);
releaseGrab();
mode = REQUEST;
}
return true; return true;
} }
bool HTTPHandler::processHTTPRequest(core::TCPSession *session, std::stringstream &data) {
HTTPSession *httpSession = static_cast<HTTPServer &>(session->server).httpSessions.findSessionByHeader(httpRequest);
std::stringstream content;
if(static_cast<HTTPServer &>(session->server).pageList.processRequest(httpRequest, session, httpSession, content)) {
std::string contentType = httpRequest->getHeader("Content-Type");
if(contentType == "multipart/form-data") {
coreutils::IMFFormData *formdata = (coreutils::IMFFormData *)httpRequest->getBody();
coreutils::Log(coreutils::LOG_DEBUG_2) << "username is '" << formdata->getByName("username") << "'";
}
httpRequest->response.setCode("200");
httpRequest->response.setText("OK");
data << httpRequest->response.getResponse(content.str());
}
else {
httpRequest->response.setCode("404");
httpRequest->response.setText("Not Found");
data << httpRequest->response.getResponse(content.str());
}
return true;
}
} }

View File

@ -4,6 +4,7 @@
#include "Command.h" #include "Command.h"
#include "TCPSession.h" #include "TCPSession.h"
#include "Log.h" #include "Log.h"
#include "HTTPRequest.h"
namespace http { namespace http {
@ -13,7 +14,9 @@ namespace http {
int processCommand(std::string request, core::TCPSession *session, std::stringstream &data) override; int processCommand(std::string request, core::TCPSession *session, std::stringstream &data) override;
private: private:
enum Mode { REQUEST, IMFHEADER }; HTTPRequest *httpRequest;
bool processHTTPRequest(core::TCPSession *session, std::stringstream &data);
enum Mode { REQUEST, IMF };
Mode mode = REQUEST; Mode mode = REQUEST;
}; };

View File

@ -25,7 +25,7 @@ namespace http {
virtual int processCommand(std::string request, virtual int processCommand(std::string request,
core::TCPSession *session, core::TCPSession *session,
HTTPSession *httpSession, HTTPSession *httpSession,
HTTPRequest &httpRequest, HTTPRequest *httpRequest,
std::stringstream &data) { std::stringstream &data) {
return false; return false;
} }

View File

@ -2,12 +2,11 @@
namespace http { namespace http {
bool HTTPPageList::processRequest(HTTPRequest &httpRequest, core::TCPSession *session, HTTPSession *httpSession, std::stringstream &data) { bool HTTPPageList::processRequest(HTTPRequest *httpRequest, core::TCPSession *session, HTTPSession *httpSession, std::stringstream &data) {
httpRequest.response.setProtocol(httpRequest.request.getProtocol()); httpRequest->response.setProtocol(httpRequest->request.getProtocol());
for(auto *page : pages) { for(auto *page : pages) {
if(page->check(httpRequest.request.getURI())) { if(page->check(httpRequest->request.getURI())) {
page->processCommand(httpRequest.request.getURI(), session, httpSession, httpRequest, data); return page->processCommand(httpRequest->request.getURI(), session, httpSession, httpRequest, data);
return true;
} }
} }
return false; return false;

View File

@ -38,7 +38,7 @@ namespace http {
add(workflow_js, "/__workflow_js"); add(workflow_js, "/__workflow_js");
} }
bool processRequest(HTTPRequest &httpRequest, core::TCPSession *session, HTTPSession *httpSession, std::stringstream &data); bool processRequest(HTTPRequest *httpRequest, core::TCPSession *session, HTTPSession *httpSession, std::stringstream &data);
void add(HTTPPage &page, std::string name = ""); void add(HTTPPage &page, std::string name = "");

View File

@ -5,21 +5,23 @@
#include "IMFMessage.h" #include "IMFMessage.h"
#include "IMFRequest.h" #include "IMFRequest.h"
#include "IMFResponse.h" #include "IMFResponse.h"
#include "Exception.h"
#include "Log.h"
namespace http { namespace http {
class HTTPRequest : public coreutils::IMFMessage { class HTTPRequest : public coreutils::IMFMessage {
public: public:
HTTPRequest(); HTTPRequest(coreutils::PString &in);
HTTPRequest(coreutils::PString &in) {
request = coreutils::IMFRequest(in);
parse(in);
}
coreutils::IMFRequest request; coreutils::IMFRequest request;
coreutils::IMFResponse response; coreutils::IMFResponse response;
private:
std::string key;
std::string value;
}; };
} }

Binary file not shown.

View File

@ -5,13 +5,13 @@
namespace http { namespace http {
HTTPSession * HTTPSessions::findSessionByHeader(HTTPRequest &httpRequest) { HTTPSession * HTTPSessions::findSessionByHeader(HTTPRequest *httpRequest) {
std::string sessionId = httpRequest.getHeaderKeyPairValue("Cookie", "sessionId"); std::string sessionId = httpRequest->getHeaderKeyPairValue("Cookie", "sessionId");
HTTPSession *session = findSessionById(sessionId, httpRequest); HTTPSession *session = findSessionById(sessionId, httpRequest);
return session; return session;
} }
HTTPSession * HTTPSessions::findSessionById(std::string sessionId, HTTPRequest &httpRequest) { HTTPSession * HTTPSessions::findSessionById(std::string sessionId, HTTPRequest *httpRequest) {
HTTPSession *httpSession; HTTPSession *httpSession;
if(sessionId.length() > 0) { if(sessionId.length() > 0) {
std::map<std::string, HTTPSession*>::iterator ix; std::map<std::string, HTTPSession*>::iterator ix;
@ -19,13 +19,13 @@ namespace http {
httpSession = ix->second; httpSession = ix->second;
if(ix == sessions.end()) { if(ix == sessions.end()) {
httpSession = createHTTPSession(); httpSession = createHTTPSession();
httpRequest.response.setCookie("sessionId", httpSession->getSessionId()); httpRequest->response.setCookie("sessionId", httpSession->getSessionId());
} }
coreutils::Log(coreutils::LOG_DEBUG_1) << "http session: " << "(" << sessionId << ") " << httpSession; coreutils::Log(coreutils::LOG_DEBUG_1) << "http session: " << "(" << sessionId << ") " << httpSession;
} else { } else {
httpSession = createHTTPSession(); httpSession = createHTTPSession();
httpRequest.response.setCookie("sessionId", httpSession->getSessionId()); httpRequest->response.setCookie("sessionId", httpSession->getSessionId());
} }
return httpSession; return httpSession;
} }

View File

@ -12,8 +12,8 @@ namespace http {
class HTTPSessions : public core::Command { class HTTPSessions : public core::Command {
public: public:
HTTPSession * findSessionByHeader(HTTPRequest &httpRequest); HTTPSession * findSessionByHeader(HTTPRequest *httpRequest);
HTTPSession * findSessionById(std::string sessionId, HTTPRequest &httpRequest); HTTPSession * findSessionById(std::string sessionId, HTTPRequest *httpRequest);
int processCommand(std::string request, core::TCPSession *session, std::stringstream &data); int processCommand(std::string request, core::TCPSession *session, std::stringstream &data);

View File

@ -5,7 +5,7 @@ namespace http {
class __configure : public HTTPPage { class __configure : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << "<form name=\"configure\" action=\"setupadmin\" method=\"POST\">" << std::endl; data << "<form name=\"configure\" action=\"setupadmin\" method=\"POST\">" << std::endl;
data << " <div class=\"window\"><h1>System Configuration</h1>" << std::endl; data << " <div class=\"window\"><h1>System Configuration</h1>" << std::endl;
@ -27,7 +27,7 @@ namespace http {
data << " <input type=\"button\" onmousedown=\"process('/mainmenu','configure', 'main'); return true;\" name=\"button1\" value=\"Update Configuration\">" << std::endl; data << " <input type=\"button\" onmousedown=\"process('/mainmenu','configure', 'main'); return true;\" name=\"button1\" value=\"Update Configuration\">" << std::endl;
data << " </div></form>" << std::endl; data << " </div></form>" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/html"); httpRequest->response.addHeader("Content-Type", "text/html");
return true; return true;
} }

View File

@ -7,7 +7,7 @@ namespace http {
class __editview : public HTTPPage { class __editview : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << " <div style=\"position: relative;\">" << std::endl; data << " <div style=\"position: relative;\">" << std::endl;
data << "" << std::endl; data << "" << std::endl;
@ -70,7 +70,7 @@ namespace http {
data << "" << std::endl; data << "" << std::endl;
data << " <script src=\"/__editview_js\" />" << std::endl; data << " <script src=\"/__editview_js\" />" << std::endl;
httpRequest.response.addHeader("Content-Type", "script/javascript"); httpRequest->response.addHeader("Content-Type", "script/javascript");
return 0; return 0;
} }

View File

@ -7,7 +7,7 @@ namespace http {
class __editview_js : public HTTPPage { class __editview_js : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << " var mainpage;" << std::endl; data << " var mainpage;" << std::endl;
data << " var result;" << std::endl; data << " var result;" << std::endl;
data << " var mousedownx;" << std::endl; data << " var mousedownx;" << std::endl;
@ -205,7 +205,7 @@ namespace http {
data << "" << std::endl; data << "" << std::endl;
data << " }" << std::endl; data << " }" << std::endl;
httpRequest.response.addHeader("Content-Type", "script/javascript"); httpRequest->response.addHeader("Content-Type", "script/javascript");
return 0; return 0;
} }

View File

@ -10,12 +10,12 @@ namespace http {
int processCommand(std::string request, int processCommand(std::string request,
core::TCPSession *session, core::TCPSession *session,
HTTPSession *httpSession, HTTPSession *httpSession,
HTTPRequest &httpRequest, HTTPRequest *httpRequest,
std::stringstream &data) override { std::stringstream &data) override {
data << std::string(header_data, 806); data << std::string(header_data, 806);
httpRequest.response.addHeader("Content-Type", "image/x-icon"); httpRequest->response.addHeader("Content-Type", "image/x-icon");
return true; return true;
} }

View File

@ -8,7 +8,7 @@ namespace http {
class __index : public HTTPPage { class __index : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << "<html>" << std::endl; data << "<html>" << std::endl;
data << " <head>" << std::endl; data << " <head>" << std::endl;
@ -24,7 +24,7 @@ namespace http {
data << " </body>" << std::endl; data << " </body>" << std::endl;
data << "</html>" << std::endl; data << "</html>" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/html"); httpRequest->response.addHeader("Content-Type", "text/html");
return true; return true;
} }
}; };

View File

@ -10,7 +10,7 @@ namespace http {
int processCommand(std::string request, int processCommand(std::string request,
core::TCPSession *session, core::TCPSession *session,
HTTPSession *httpSession, HTTPSession *httpSession,
HTTPRequest &httpRequest, HTTPRequest *httpRequest,
std::stringstream &data) override { std::stringstream &data) override {
data << "<div>" << std::endl; data << "<div>" << std::endl;
@ -36,7 +36,7 @@ namespace http {
data << " </div>" << std::endl; data << " </div>" << std::endl;
data << "</div>" << std::endl; data << "</div>" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/html"); httpRequest->response.addHeader("Content-Type", "text/html");
return true; return true;
} }

View File

@ -7,7 +7,7 @@ namespace http {
class __script : public HTTPPage { class __script : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << "function serverSend(url, type, receiver, formData, callback) {" << std::endl; data << "function serverSend(url, type, receiver, formData, callback) {" << std::endl;
data << " var server = new XMLHttpRequest();" << std::endl; data << " var server = new XMLHttpRequest();" << std::endl;
@ -45,7 +45,7 @@ namespace http {
data << " }" << std::endl; data << " }" << std::endl;
data << "}" << std::endl; data << "}" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/javascript"); httpRequest->response.addHeader("Content-Type", "text/javascript");
return true; return true;
} }

View File

@ -5,7 +5,7 @@ namespace http {
class __setupadmin : public HTTPPage { class __setupadmin : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << "<form name=\"setupadmin\" action=\"setupadmin\" method=\"POST\">" << std::endl; data << "<form name=\"setupadmin\" action=\"setupadmin\" method=\"POST\">" << std::endl;
data << " <div class=\"window\"><p>Please enter credential information" << std::endl; data << " <div class=\"window\"><p>Please enter credential information" << std::endl;
@ -27,7 +27,7 @@ namespace http {
data << " <input type=\"button\" onmousedown=\"process('/mainmenu','setupadmin', 'main'); return true;\" name=\"button1\" value=\"Set Admin Profile\">" << std::endl; data << " <input type=\"button\" onmousedown=\"process('/mainmenu','setupadmin', 'main'); return true;\" name=\"button1\" value=\"Set Admin Profile\">" << std::endl;
data << " </div></form>" << std::endl; data << " </div></form>" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/html"); httpRequest->response.addHeader("Content-Type", "text/html");
return true; return true;
} }

View File

@ -7,7 +7,7 @@ namespace http {
class __style : public HTTPPage { class __style : public HTTPPage {
int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override { int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest *httpRequest, std::stringstream &data) override {
data << "body {background: #006;" << std::endl; data << "body {background: #006;" << std::endl;
data << " color: #fff;" << std::endl; data << " color: #fff;" << std::endl;
@ -20,7 +20,7 @@ namespace http {
data << " padding: 15px;" << std::endl; data << " padding: 15px;" << std::endl;
data << " }" << std::endl; data << " }" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/css"); httpRequest->response.addHeader("Content-Type", "text/css");
return true; return true;
} }
}; };

View File

@ -11,7 +11,7 @@ namespace http {
int processCommand(std::string request, int processCommand(std::string request,
core::TCPSession *session, core::TCPSession *session,
HTTPSession *httpSession, HTTPSession *httpSession,
HTTPRequest &httpRequest, HTTPRequest *httpRequest,
std::stringstream &data) override { std::stringstream &data) override {
coreutils::Directory directory("/home/bradarant/jetserver/views"); coreutils::Directory directory("/home/bradarant/jetserver/views");
@ -39,7 +39,7 @@ namespace http {
data << "</div>" << std::endl; data << "</div>" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/html"); httpRequest->response.addHeader("Content-Type", "text/html");
return true; return true;
} }

View File

@ -10,7 +10,7 @@ namespace http {
int processCommand(std::string request, int processCommand(std::string request,
core::TCPSession *session, core::TCPSession *session,
HTTPSession *httpSession, HTTPSession *httpSession,
HTTPRequest &httpRequest, HTTPRequest *httpRequest,
std::stringstream &data) override { std::stringstream &data) override {
data << "<div class=\"window\">\ data << "<div class=\"window\">\
@ -20,7 +20,7 @@ namespace http {
<input type=\"button\" onmousedown=\"getPage('/setupadmin','main'); return true;\" name=\"button1\" value=\"Configure\">\ <input type=\"button\" onmousedown=\"getPage('/setupadmin','main'); return true;\" name=\"button1\" value=\"Configure\">\
</div>"; </div>";
httpRequest.response.addHeader("Content-Type", "text/html"); httpRequest->response.addHeader("Content-Type", "text/html");
return true; return true;
} }