Got it compiling and somewhat working, except post.
This commit is contained in:
parent
eb58482085
commit
b6311f80d4
28
HTTPConnection.h
Normal file
28
HTTPConnection.h
Normal 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
|
@ -10,17 +10,18 @@ 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_1) << "DATA[" << request << "]";
|
|
||||||
coreutils::PString request1(request);
|
coreutils::PString request1(request);
|
||||||
|
|
||||||
if(!httpRequest) {
|
if(!httpRequest) {
|
||||||
httpRequest = new HTTPRequest(request1);
|
httpRequest = new HTTPRequest(request1);
|
||||||
session->server.commands.grabInput(session, *this);
|
// session->server.commands.grabInput(session, *this);
|
||||||
|
processHTTPRequest(session, data);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
httpRequest->parse(request1);
|
httpRequest->parse(request1);
|
||||||
if(request == "") {
|
if(request == "") {
|
||||||
session->server.commands.clearGrab(session);
|
// session->server.commands.clearGrab(session);
|
||||||
|
processHTTPRequest(session, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -33,22 +34,25 @@ namespace http {
|
|||||||
std::stringstream content;
|
std::stringstream content;
|
||||||
|
|
||||||
if(static_cast<HTTPServer &>(session->server).pageList.processRequest(httpRequest, session, httpSession, 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");
|
std::string contentType = httpRequest->getHeader("Content-Type");
|
||||||
httpRequest->response.setText("OK");
|
if(contentType == "multipart/form-data") {
|
||||||
data << httpRequest->response.getResponse(content.str());
|
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 {
|
else {
|
||||||
httpRequest->response.setCode("404");
|
httpRequest->response.setCode("404");
|
||||||
httpRequest->response.setText("Not Found");
|
httpRequest->response.setText("Not Found");
|
||||||
data << httpRequest->response.getResponse(content.str());
|
data << httpRequest->response.getResponse(content.str());
|
||||||
}
|
}
|
||||||
|
delete httpRequest;
|
||||||
|
httpRequest = NULL;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
#include "HTTPRequest.h"
|
|
||||||
#include "Exception.h"
|
|
||||||
#include "Log.h"
|
|
||||||
|
|
||||||
namespace http {
|
|
||||||
|
|
||||||
// HTTPRequest::HTTPRequest(coreutils::PString &in) {
|
|
||||||
// request = coreutils::IMFRequest(in);
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -10,10 +10,13 @@
|
|||||||
|
|
||||||
namespace http {
|
namespace http {
|
||||||
|
|
||||||
class HTTPRequest : public coreutils::IMFMessage, public coreutils::IMFRequest {
|
class HTTPRequest : public coreutils::IMFMessage {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HTTPRequest(coreutils::PString &in) : IMFRequest(in), IMFMessage(in) {}
|
HTTPRequest(coreutils::PString &in) {
|
||||||
|
request.parse(in);
|
||||||
|
parse(in);
|
||||||
|
}
|
||||||
|
|
||||||
coreutils::IMFRequest request;
|
coreutils::IMFRequest request;
|
||||||
coreutils::IMFResponse response;
|
coreutils::IMFResponse response;
|
||||||
|
BIN
HTTPServer
BIN
HTTPServer
Binary file not shown.
17
HTTPServer.h
17
HTTPServer.h
@ -4,28 +4,35 @@
|
|||||||
#include "TCPServer.h"
|
#include "TCPServer.h"
|
||||||
#include "HTTPSessions.h"
|
#include "HTTPSessions.h"
|
||||||
#include "HTTPPageList.h"
|
#include "HTTPPageList.h"
|
||||||
|
#include "HTTPConnection.h"
|
||||||
#include "HTTPHandler.h"
|
#include "HTTPHandler.h"
|
||||||
|
|
||||||
namespace http {
|
namespace http {
|
||||||
|
|
||||||
|
class TCPSession;
|
||||||
|
|
||||||
class HTTPServer : public core::TCPServer {
|
class HTTPServer : public core::TCPServer {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HTTPServer(core::EPoll &ePoll, core::IPAddress ipAddress, HTTPSessions &httpSessions)
|
HTTPServer(core::EPoll &ePoll, core::IPAddress ipAddress, HTTPSessions &httpSessions)
|
||||||
: TCPServer(ePoll, ipAddress), httpSessions(httpSessions) {
|
: TCPServer(ePoll, ipAddress), httpSessions(httpSessions) {
|
||||||
commands.add(getHandler, "GET");
|
commands.add(getHandler, "GET");
|
||||||
commands.add(postHandler, "POST");
|
commands.add(postHandler, "POST");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
core::TCPSession * getSocketAccept(core::EPoll &epoll) override {
|
||||||
|
return new HTTPConnection(ePoll, *this);
|
||||||
|
}
|
||||||
|
|
||||||
HTTPSessions &httpSessions;
|
HTTPSessions &httpSessions;
|
||||||
HTTPPageList pageList;
|
HTTPPageList pageList;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HTTPHandler getHandler;
|
HTTPHandler getHandler;
|
||||||
HTTPHandler postHandler;
|
HTTPHandler postHandler;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user