From cf08375d323eeed0b94e6dcf23d27ef8b5befeca Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Sun, 24 Feb 2019 09:36:42 -0800 Subject: [PATCH] Initial repository. --- HTTPServer.h | 27 +++++++++++++++++++++++++++ HTTPServer.h~ | 26 ++++++++++++++++++++++++++ HTTPService.h | 27 +++++++++++++++++++++++++++ _GET.h | 30 ++++++++++++++++++++++++++++++ _GET.h~ | 28 ++++++++++++++++++++++++++++ _POST.h | 24 ++++++++++++++++++++++++ compile | 31 +++++++++++++++++++++++++++++++ compile~ | 31 +++++++++++++++++++++++++++++++ main.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ main.cpp~ | 41 +++++++++++++++++++++++++++++++++++++++++ 10 files changed, 306 insertions(+) create mode 100644 HTTPServer.h create mode 100644 HTTPServer.h~ create mode 100644 HTTPService.h create mode 100644 _GET.h create mode 100644 _GET.h~ create mode 100644 _POST.h create mode 100755 compile create mode 100755 compile~ create mode 100644 main.cpp create mode 100644 main.cpp~ diff --git a/HTTPServer.h b/HTTPServer.h new file mode 100644 index 0000000..1d89c62 --- /dev/null +++ b/HTTPServer.h @@ -0,0 +1,27 @@ +#ifndef __HTTPServer_h__ +#define __HTTPServer_h__ + +#include "includes" +#include "EPoll.h" +#include "TCPServerSocket.h" +#include "Session.h" +#include "IPAddress.h" +#include "HTTPService.h" + +namespace http { + + class HTTPServer : public core::TCPServerSocket { + + public: + HTTPServer(core::EPoll &ePoll, core::IPAddress address) : core::TCPServerSocket(ePoll, address) {} + + protected: + core::Service * getService() override { + return new HTTPService(*this); + } + + }; + +} + +#endif diff --git a/HTTPServer.h~ b/HTTPServer.h~ new file mode 100644 index 0000000..2a9ce08 --- /dev/null +++ b/HTTPServer.h~ @@ -0,0 +1,26 @@ +#ifndef __HTTPServer_h__ +#define __HTTPServer_h__ + +#include "includes" +#include "EPoll.h" +#include "TCPServerSocket.h" +#include "Session.h" +#include "IPAddress.h" + +namespace http { + + class HTTPServer : public core::TCPServerSocket { + + public: + HTTPServer(core::EPoll &ePoll, core::IPAddress address) : core::TCPServerSocket(ePoll, address) {} + + protected: + core::Service * getService() override { + return new HTTPService(*this); + } + + }; + +} + +#endif diff --git a/HTTPService.h b/HTTPService.h new file mode 100644 index 0000000..26d420a --- /dev/null +++ b/HTTPService.h @@ -0,0 +1,27 @@ +#ifndef __HTTPService_h__ +#define __HTTPService_h__ + +#include "Service.h" +#include "HTTPServer.h" +#include "_GET.h" +#include "_POST.h" + +namespace http { + + class HTTPService : public core::Service { + + public: + HTTPService::HTTPService(HTTPServer &server) : core::Service(server) { + commands.add(get, "GET "); + commands.add(post, "POST "); + } + + private: + _GET get; + _POST post; + + }; + +} + +#endif diff --git a/_GET.h b/_GET.h new file mode 100644 index 0000000..25c2354 --- /dev/null +++ b/_GET.h @@ -0,0 +1,30 @@ +#ifndef ___GET_h__ +#define ___GET_h__ + +#include "Command.h" +#include "Session.h" +#include "Header.h" +#include "Response.h" + +namespace http { + + class _GET : public core::Command { + + public: + int processCommand(std::string request, core::Session *session) override { + core::Header header(request); + std::string data = "

This Is A Test

"; + core::Response response(); + response.setProtocol(header.getProtocol()); + response.setCode("200"); + response.setText("OK"); + response.setMimeType("text/html"); + session->out << response.getOutput(data); + session->send(); + } + + }; + +} + +#endif diff --git a/_GET.h~ b/_GET.h~ new file mode 100644 index 0000000..13450b5 --- /dev/null +++ b/_GET.h~ @@ -0,0 +1,28 @@ +#ifndef ___GET_h__ +#define ___GET_h__ + +#include "Command.h" +#include "Session.h" + +namespace http { + + class _GET : public core::Command { + + public: + int processCommand(std::string request, core::Session *session) override { + core::Header header(request); + std::string data = "

This Is A Test

"; + core::Response response(); + response.setProtocol(header.getProtocol()); + response.setCode("200"); + response.setText("OK"); + response.setMimeType("text/html"); + session->out << response.getOutput(data); + session->send(); + } + + }; + +} + +#endif diff --git a/_POST.h b/_POST.h new file mode 100644 index 0000000..77291e9 --- /dev/null +++ b/_POST.h @@ -0,0 +1,24 @@ +#ifndef ___POST_h__ +#define ___POST_h__ + +#include "Command.h" +#include "Session.h" + +namespace http { + + class _POST : public core::Command { + + public: + int processCommand(std::string request, core::Session &session) override { + core::Header header(request); + std::string data = "

This Is A Test

"; + core::Response response(); + session.out << response.getOutput(data); + session.send(); + } + + }; + +} + +#endif diff --git a/compile b/compile new file mode 100755 index 0000000..a890bd2 --- /dev/null +++ b/compile @@ -0,0 +1,31 @@ +#!/bin/bash + +#for file in *.cpp +#do +# filename="${file%.*}" +# list="$list $filename.o" +# echo -n "Compiling $filename..." +# g++ -c $file -I../ServerCore +# if [ $? = '0' ] +# then +# echo "OK" +# else +# echo "ERROR" +# exit -1 +# fi + +done + +wait +echo -n "Building static library libServerCore.a..." +ar rcs libServerCore.a $list +if [ $? = '0' ] +then + echo "OK" +else + echo "ERROR" + exit -1 +fi + +rm *.o +rm *~ diff --git a/compile~ b/compile~ new file mode 100755 index 0000000..ee4703f --- /dev/null +++ b/compile~ @@ -0,0 +1,31 @@ +#!/bin/bash + +for file in *.cpp +do + filename="${file%.*}" + list="$list $filename.o" + echo -n "Compiling $filename..." + g++ -c $file -I../ServerCore + if [ $? = '0' ] + then + echo "OK" + else + echo "ERROR" + exit -1 + fi + +done + +wait +echo -n "Building static library libServerCore.a..." +ar rcs libServerCore.a $list +if [ $? = '0' ] +then + echo "OK" +else + echo "ERROR" + exit -1 +fi + +rm *.o +rm *~ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9068abd --- /dev/null +++ b/main.cpp @@ -0,0 +1,41 @@ +#include "includes" +#include "EPoll.h" +#include "ConsoleServer.h" +#include "HTTPServer.h" +#include "Exception.h" +#include "File.h" +#include "Log.h" +#include "IPAddress.h" + +int main(int argc, char **argv) { + + try { + + core::Log(new core::File("/tmp/http.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); + core::Log(core::LOG_INFO) << "HTTP Server starting. Build " << __DATE__ << " " << __TIME__; + + std::string ipAddress = "0.0.0.0"; + + EPoll ePoll; + + core::HTTPServer http(ePoll, core::IPAddress(ipAddress, 8090), "http"); + + core::ConsoleServer console(ePoll, core::IPAddress(ipAddress, 1027)); + console.service->commands.add(ePoll); + console.service->commands.add(http); + console.service->commands.add(console); + ePoll.start(4, 1000); + + while(true) + sleep(300); + + ePoll.stop(); + + } + + catch(core::Exception exception) { + std::cout << exception.text << " Error reason is '" << strerror(exception.errorNumber) << "' in file " << exception.file << " at line " << exception.line << std::endl; + sleep(10); + } + +} diff --git a/main.cpp~ b/main.cpp~ new file mode 100644 index 0000000..2d55c84 --- /dev/null +++ b/main.cpp~ @@ -0,0 +1,41 @@ +#include "includes" +#include "EPoll.h" +#include "ConsoleServer.h" +#include "HTTPServer.h" +#include "Exception.h" +#include "File.h" +#include "Log.h" +#include "IPAddress.h" + +int main(int argc, char **argv) { + + try { + + core::Log(new core::File("/tmp/http.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); + core::Log(core::LOG_INFO) << "HTTP Server starting. Build " << __DATE__ << " " << __TIME__; + + std::string ipAddress = "0.0.0.0"; + + EPoll ePoll; + + core::HTTPServer http(ePoll, core::IPAddress(ipAddress, 8090), "http"); + + core::ConsoleServer console(ePoll, core::IPAddress(ipAddress, 1027)); + console.service.commands.add(ePoll); + console.service.commands.add(http); + console.service.commands.add(console); + ePoll.start(4, 1000); + + while(true) + sleep(300); + + ePoll.stop(); + + } + + catch(core::Exception exception) { + std::cout << exception.text << " Error reason is '" << strerror(exception.errorNumber) << "' in file " << exception.file << " at line " << exception.line << std::endl; + sleep(10); + } + +}