Initial repository.
This commit is contained in:
commit
cf08375d32
27
HTTPServer.h
Normal file
27
HTTPServer.h
Normal file
@ -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
|
26
HTTPServer.h~
Normal file
26
HTTPServer.h~
Normal file
@ -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
|
27
HTTPService.h
Normal file
27
HTTPService.h
Normal file
@ -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
|
30
_GET.h
Normal file
30
_GET.h
Normal file
@ -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 = "<html><head></head><body><h1>This Is A Test</h1></body></html>";
|
||||||
|
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
|
28
_GET.h~
Normal file
28
_GET.h~
Normal file
@ -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 = "<html><head></head><body><h1>This Is A Test</h1></body></html>";
|
||||||
|
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
|
24
_POST.h
Normal file
24
_POST.h
Normal file
@ -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 = "<html><head></head><body><h1>This Is A Test</h1></body></html>";
|
||||||
|
core::Response response();
|
||||||
|
session.out << response.getOutput(data);
|
||||||
|
session.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
31
compile
Executable file
31
compile
Executable file
@ -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 *~
|
31
compile~
Executable file
31
compile~
Executable file
@ -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 *~
|
41
main.cpp
Normal file
41
main.cpp
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
41
main.cpp~
Normal file
41
main.cpp~
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user