diff --git a/HTTPConnection.h b/HTTPConnection.h new file mode 100644 index 0000000..c0f3c5d --- /dev/null +++ b/HTTPConnection.h @@ -0,0 +1,28 @@ +#ifndef __HTTPConnection_h__ +#define __HTTPConnection_h__ + +#include "TCPSession.h" +#include "Log.h" + +namespace http { + + class HTTPConnection : public core::TCPSession { + + public: + + HTTPConnection(core::EPoll &ePoll, core::TCPServer &server) : TCPSession(ePoll, server, "HTTP Connection") {} + + void onDataReceived(char *data, int len) override { + + coreutils::Log(coreutils::LOG_DEBUG_1) << data; + + protocol(std::string(data, len)); + send(); + + } + + }; + +} + +#endif \ No newline at end of file diff --git a/HTTPHandler.cpp b/HTTPHandler.cpp index 9397fa3..e213b09 100644 --- a/HTTPHandler.cpp +++ b/HTTPHandler.cpp @@ -10,17 +10,18 @@ namespace http { int HTTPHandler::processCommand(std::string request, core::TCPSession *session, std::stringstream &data) { - coreutils::Log(coreutils::LOG_DEBUG_1) << "DATA[" << request << "]"; coreutils::PString request1(request); if(!httpRequest) { httpRequest = new HTTPRequest(request1); - session->server.commands.grabInput(session, *this); +// session->server.commands.grabInput(session, *this); + processHTTPRequest(session, data); } else { httpRequest->parse(request1); if(request == "") { - session->server.commands.clearGrab(session); +// session->server.commands.clearGrab(session); + processHTTPRequest(session, data); } } return true; @@ -33,22 +34,25 @@ namespace http { 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()); + 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()); + coreutils::Log(coreutils::LOG_DEBUG_1) << "Sending response to client..." << content.str(); } - else { - httpRequest->response.setCode("404"); - httpRequest->response.setText("Not Found"); - data << httpRequest->response.getResponse(content.str()); + httpRequest->response.setCode("404"); + httpRequest->response.setText("Not Found"); + data << httpRequest->response.getResponse(content.str()); } + delete httpRequest; + httpRequest = NULL; return true; } diff --git a/HTTPRequest.cpp b/HTTPRequest.cpp deleted file mode 100644 index bf687bb..0000000 --- a/HTTPRequest.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "HTTPRequest.h" -#include "Exception.h" -#include "Log.h" - -namespace http { - - // HTTPRequest::HTTPRequest(coreutils::PString &in) { - // request = coreutils::IMFRequest(in); - // } - -} - diff --git a/HTTPRequest.h b/HTTPRequest.h index a9c99c2..2978ef0 100644 --- a/HTTPRequest.h +++ b/HTTPRequest.h @@ -10,10 +10,13 @@ namespace http { - class HTTPRequest : public coreutils::IMFMessage, public coreutils::IMFRequest { + class HTTPRequest : public coreutils::IMFMessage { public: - HTTPRequest(coreutils::PString &in) : IMFRequest(in), IMFMessage(in) {} + HTTPRequest(coreutils::PString &in) { + request.parse(in); + parse(in); + } coreutils::IMFRequest request; coreutils::IMFResponse response; diff --git a/HTTPServer b/HTTPServer index 37b520c..a100259 100755 Binary files a/HTTPServer and b/HTTPServer differ diff --git a/HTTPServer.h b/HTTPServer.h index b98ffff..655ffe5 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -4,28 +4,35 @@ #include "TCPServer.h" #include "HTTPSessions.h" #include "HTTPPageList.h" +#include "HTTPConnection.h" #include "HTTPHandler.h" namespace http { - + + class TCPSession; + class HTTPServer : public core::TCPServer { - + public: - HTTPServer(core::EPoll &ePoll, core::IPAddress ipAddress, HTTPSessions &httpSessions) + HTTPServer(core::EPoll &ePoll, core::IPAddress ipAddress, HTTPSessions &httpSessions) : TCPServer(ePoll, ipAddress), httpSessions(httpSessions) { commands.add(getHandler, "GET"); commands.add(postHandler, "POST"); } + core::TCPSession * getSocketAccept(core::EPoll &epoll) override { + return new HTTPConnection(ePoll, *this); + } + HTTPSessions &httpSessions; HTTPPageList pageList; - + private: HTTPHandler getHandler; HTTPHandler postHandler; }; - + } #endif