Got it compiling and somewhat working, except post.

This commit is contained in:
Brad Arant 2020-11-29 13:55:01 -08:00
parent eb58482085
commit b6311f80d4
6 changed files with 64 additions and 34 deletions

28
HTTPConnection.h Normal file
View File

@ -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

View File

@ -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<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());
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;
}

View File

@ -1,12 +0,0 @@
#include "HTTPRequest.h"
#include "Exception.h"
#include "Log.h"
namespace http {
// HTTPRequest::HTTPRequest(coreutils::PString &in) {
// request = coreutils::IMFRequest(in);
// }
}

View File

@ -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;

Binary file not shown.

View File

@ -4,10 +4,13 @@
#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:
@ -17,6 +20,10 @@ namespace http {
commands.add(postHandler, "POST");
}
core::TCPSession * getSocketAccept(core::EPoll &epoll) override {
return new HTTPConnection(ePoll, *this);
}
HTTPSessions &httpSessions;
HTTPPageList pageList;