diff --git a/HTTPHandler.cpp b/HTTPHandler.cpp index ccaa4b1..a2aedb0 100644 --- a/HTTPHandler.cpp +++ b/HTTPHandler.cpp @@ -7,48 +7,55 @@ #include "Log.h" namespace http { - + int HTTPHandler::processCommand(std::string request, core::TCPSession *session, std::stringstream &data) { - coreutils::Log(coreutils::LOG_DEBUG_4) << "DATA[" << request << "]"; - - if(mode == REQUEST) { - - coreutils::PString request1(request); - HTTPRequest httpRequest(request1); - - HTTPSession *httpSession = static_cast(session->server).httpSessions.findSessionByHeader(httpRequest); + coreutils::Log(coreutils::LOG_DEBUG_1) << "DATA[" << request << "]"; + coreutils::PString request1(request); - std::stringstream content; - - if(static_cast(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") << "'"; - } + switch(mode) { + case REQUEST: + httpRequest = new HTTPRequest(request1); + session->server.commands.grabInput(session, *this); + mode = IMF; + break; - 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()); + case IMF: + httpRequest->parse(request1); + if(request == "") { + session->server.commands.clearGrab(session); + mode = REQUEST; + processHTTPRequest(session, data); } - - grabInput(); - mode = IMFHEADER; - } - else if(mode == IMFHEADER) { - HTTPHeader header(content); - releaseGrab(); - mode = REQUEST; - } - + } + return true; } + bool HTTPHandler::processHTTPRequest(core::TCPSession *session, std::stringstream &data) { + + HTTPSession *httpSession = static_cast(session->server).httpSessions.findSessionByHeader(httpRequest); + + std::stringstream content; + + if(static_cast(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; + } + } diff --git a/HTTPHandler.h b/HTTPHandler.h index f92552a..cad952b 100644 --- a/HTTPHandler.h +++ b/HTTPHandler.h @@ -4,6 +4,7 @@ #include "Command.h" #include "TCPSession.h" #include "Log.h" +#include "HTTPRequest.h" namespace http { @@ -13,7 +14,9 @@ namespace http { int processCommand(std::string request, core::TCPSession *session, std::stringstream &data) override; private: - enum Mode { REQUEST, IMFHEADER }; + HTTPRequest *httpRequest; + bool processHTTPRequest(core::TCPSession *session, std::stringstream &data); + enum Mode { REQUEST, IMF }; Mode mode = REQUEST; }; diff --git a/HTTPPage.h b/HTTPPage.h index 0af1c0f..dcafa15 100644 --- a/HTTPPage.h +++ b/HTTPPage.h @@ -25,7 +25,7 @@ namespace http { virtual int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, - HTTPRequest &httpRequest, + HTTPRequest *httpRequest, std::stringstream &data) { return false; } diff --git a/HTTPPageList.cpp b/HTTPPageList.cpp index 96bc4a0..89edd26 100644 --- a/HTTPPageList.cpp +++ b/HTTPPageList.cpp @@ -2,12 +2,11 @@ namespace http { - bool HTTPPageList::processRequest(HTTPRequest &httpRequest, core::TCPSession *session, HTTPSession *httpSession, std::stringstream &data) { - httpRequest.response.setProtocol(httpRequest.request.getProtocol()); + bool HTTPPageList::processRequest(HTTPRequest *httpRequest, core::TCPSession *session, HTTPSession *httpSession, std::stringstream &data) { + httpRequest->response.setProtocol(httpRequest->request.getProtocol()); for(auto *page : pages) { - if(page->check(httpRequest.request.getURI())) { - page->processCommand(httpRequest.request.getURI(), session, httpSession, httpRequest, data); - return true; + if(page->check(httpRequest->request.getURI())) { + return page->processCommand(httpRequest->request.getURI(), session, httpSession, httpRequest, data); } } return false; diff --git a/HTTPPageList.h b/HTTPPageList.h index e085ed6..6c0e45d 100644 --- a/HTTPPageList.h +++ b/HTTPPageList.h @@ -38,7 +38,7 @@ namespace http { 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 = ""); diff --git a/HTTPRequest.h b/HTTPRequest.h index c176139..e35af44 100644 --- a/HTTPRequest.h +++ b/HTTPRequest.h @@ -5,21 +5,23 @@ #include "IMFMessage.h" #include "IMFRequest.h" #include "IMFResponse.h" +#include "Exception.h" +#include "Log.h" namespace http { class HTTPRequest : public coreutils::IMFMessage { public: - HTTPRequest(); - HTTPRequest(coreutils::PString &in) { - request = coreutils::IMFRequest(in); - parse(in); - } + HTTPRequest(coreutils::PString &in); coreutils::IMFRequest request; coreutils::IMFResponse response; - + + private: + std::string key; + std::string value; + }; } diff --git a/HTTPServer b/HTTPServer index 8765efd..3db25ef 100755 Binary files a/HTTPServer and b/HTTPServer differ diff --git a/HTTPSessions.cpp b/HTTPSessions.cpp index c4e82c2..b7eec25 100644 --- a/HTTPSessions.cpp +++ b/HTTPSessions.cpp @@ -5,13 +5,13 @@ namespace http { - HTTPSession * HTTPSessions::findSessionByHeader(HTTPRequest &httpRequest) { - std::string sessionId = httpRequest.getHeaderKeyPairValue("Cookie", "sessionId"); + HTTPSession * HTTPSessions::findSessionByHeader(HTTPRequest *httpRequest) { + std::string sessionId = httpRequest->getHeaderKeyPairValue("Cookie", "sessionId"); HTTPSession *session = findSessionById(sessionId, httpRequest); return session; } - HTTPSession * HTTPSessions::findSessionById(std::string sessionId, HTTPRequest &httpRequest) { + HTTPSession * HTTPSessions::findSessionById(std::string sessionId, HTTPRequest *httpRequest) { HTTPSession *httpSession; if(sessionId.length() > 0) { std::map::iterator ix; @@ -19,13 +19,13 @@ namespace http { httpSession = ix->second; if(ix == sessions.end()) { httpSession = createHTTPSession(); - httpRequest.response.setCookie("sessionId", httpSession->getSessionId()); + httpRequest->response.setCookie("sessionId", httpSession->getSessionId()); } coreutils::Log(coreutils::LOG_DEBUG_1) << "http session: " << "(" << sessionId << ") " << httpSession; } else { httpSession = createHTTPSession(); - httpRequest.response.setCookie("sessionId", httpSession->getSessionId()); + httpRequest->response.setCookie("sessionId", httpSession->getSessionId()); } return httpSession; } diff --git a/HTTPSessions.h b/HTTPSessions.h index 7d44d50..8301ac7 100644 --- a/HTTPSessions.h +++ b/HTTPSessions.h @@ -12,8 +12,8 @@ namespace http { class HTTPSessions : public core::Command { public: - HTTPSession * findSessionByHeader(HTTPRequest &httpRequest); - HTTPSession * findSessionById(std::string sessionId, HTTPRequest &httpRequest); + HTTPSession * findSessionByHeader(HTTPRequest *httpRequest); + HTTPSession * findSessionById(std::string sessionId, HTTPRequest *httpRequest); int processCommand(std::string request, core::TCPSession *session, std::stringstream &data); diff --git a/__configure.h b/__configure.h index 0e415ea..666544c 100644 --- a/__configure.h +++ b/__configure.h @@ -5,7 +5,7 @@ namespace http { 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 << "
" << std::endl; data << "

System Configuration

" << std::endl; @@ -27,7 +27,7 @@ namespace http { data << " " << std::endl; data << "
" << std::endl; - httpRequest.response.addHeader("Content-Type", "text/html"); + httpRequest->response.addHeader("Content-Type", "text/html"); return true; } diff --git a/__editview.h b/__editview.h index 1ba0d21..2d648cb 100644 --- a/__editview.h +++ b/__editview.h @@ -7,7 +7,7 @@ namespace http { 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 << "
" << std::endl; data << "" << std::endl; @@ -70,7 +70,7 @@ namespace http { data << "" << std::endl; data << "