Skip to content

Commit

Permalink
v 2.5
Browse files Browse the repository at this point in the history
- improved general code flow
- wrapped data inside HTTPData
- reworked Synchronization.hpp
- removed pthread and using c++17 threads on linux
- unique_ptr implementation instead of raw ptr
- minor fixes
  • Loading branch information
M4iKZ committed Jul 24, 2023
1 parent 6956b4f commit 5cb2e14
Show file tree
Hide file tree
Showing 14 changed files with 570 additions and 675 deletions.
69 changes: 35 additions & 34 deletions source/Common/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,44 @@
#include <condition_variable>
#include <unordered_map>

#include "HTTPProtocol.hpp"
#include "Request.hpp"
#include "Response.hpp"

namespace Orpy
{
std::mutex _mutex;

const int64_t ReadSize = 1024 * 4 * 16;
const int64_t MaxFileSize = 1024 * 1024;
const int64_t MaxUploadSize = ReadSize * 64;
const size_t ReadSize = 1024 * 4 * 16;
const size_t MaxFileSize = 1024 * 1024;
const size_t MaxUploadSize = ReadSize * 64;

time_t setTime(time_t time = 10) //10 seconds should be enough?
{
return std::time(nullptr) + time;
}

struct SITEData
{
std::string redirect = "";

std::string path = "";
std::vector<std::string> langs;

std::vector<std::string> positions;

std::unordered_map<std::string, std::string> mimetypes = {};
std::vector<std::string> urls = {};

bool allowFiles = false;

std::string cookieDomain = "";
};

struct HTTPData
{
HTTPData(int p = 0, int clientfd = -1, std::string k = "", std::string ip = "") : fd(clientfd), key(k), phase(p), IP(ip), length(0), buffer({}) {}

~HTTPData() {}

int fd = 0;
int phase = 0;

Expand All @@ -37,41 +56,23 @@ namespace Orpy
std::string IP = "";

std::vector<char> buffer;
int position = 0;

//RESPONSE SEND FILE
bool isFile = false;
std::string fileName = "";
size_t fileSize = 0;
size_t headerSize = 0;
bool sentHeader = false;
int sent = 0;
std::streampos cursor = std::streampos();

time_t startTime = setTime();
};
// Conf
SITEData Conf;

struct SITEData
{
~SITEData() {}
// Request
HttpRequest request;

std::string redirect = "";

std::string path = "";
std::vector<std::string> langs;

std::vector<std::string> positions;
// Response
HttpResponse response;

std::unordered_map<std::string, std::string> mimetypes = {};
std::vector<std::string> urls = {};

bool allowFiles = false;
std::string DB = "";
time_t startTime = setTime();

std::string cookieDomain = "";

std::string emailSettings = "";
// Error
bool error = false;
};

void debug(std::string msg)
{
{
Expand Down
2 changes: 1 addition & 1 deletion source/Common/IHttp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Orpy
public:
virtual ~IHttp() = default;

virtual void elaborateData(HTTPData*) = 0;
virtual void elaborateData(std::unique_ptr<HTTPData>&) = 0;
};

extern "C"
Expand Down
29 changes: 4 additions & 25 deletions source/Common/Request.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once

#include "Common.hpp"
#include "Response.hpp"

namespace Orpy
{
struct MP_DATA
Expand All @@ -18,30 +15,21 @@ namespace Orpy

struct HttpRequest
{
int position = 0;

std::string method = "";

// IP
std::string clientIP = "";

// User-Agent
// User-Agent / Bot
std::string useragent = "";
bool isBot = false;

// User
std::string session_id = "";
std::string userid = "";

// URL
std::string URI = "";
std::string URL = "";
std::string URLname = ""; // for multilingual commands
std::string URL = "";
std::vector<std::string> commands;
int nCommands = 0;

std::string command_id = "";

// Host
std::string Host = "";

Expand All @@ -51,8 +39,8 @@ namespace Orpy
// Origin
std::string Origin = "";

// Encoding
// deflate and gzip not supported yet
// Supported Encoding
// not implemented yet
bool br = false;

// Content-Length
Expand All @@ -79,14 +67,5 @@ namespace Orpy
bool isFile = false;
std::string filePath = "";
std::string fileModifiedSince = "";

// Error
bool error = false;

// Conf
SITEData Conf;

// Repsponse
HttpResponse response; // MUST BE MOVED FROM HERE
};
}
54 changes: 12 additions & 42 deletions source/Common/Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
#include <chrono>
#include <iomanip>

#include "HTTPProtocol.hpp"

namespace Orpy
{
{
struct HttpResponse
{
//Status
Expand All @@ -23,49 +21,21 @@ namespace Orpy

std::string location = "";

// not implemented yet
bool compressed = false;

// FILE
bool isFile = false;
std::string fileLastEdit = "";

std::string fileName = "";

// Generates the response buffer from the fields in the struct
void getBuffer(std::vector<char>& buffer)
{
std::string response = "HTTP/1.1 " + HttpResponses.at(status) + "\r\n";

response += "Server: Orpy by M4iKZ\r\n";

if (status == 301 || status == 302 || status == 303 || status == 307)
response += "Location: " + location + "\r\n";
else
{
response += "Content-Type: " + content_type + "\r\n";

if (fileName != "") // download file instead to show it
response += "Content-Disposition: attachment; filename=" + fileName + "\r\n";

if (content != "" && content_length == 0)
content_length = content.size();

response += "Content-Length: " + std::to_string(content_length) + "\r\n";

if (isFile)
{
response += "Last-Modified: " + fileLastEdit + "\r\n";
response += "Cache-Control: must-revalidate\r\n";
}
}

response += "\r\n";

// Calculate the total length of the buffer
bufferSize = response.length() + content.length();

buffer.insert(buffer.begin(), response.begin(), response.end());

if (content != "")
buffer.insert(buffer.end(), content.begin(), content.end());
}
std::string attachmentName = "";
size_t fileSize = 0;

size_t headerSize = 0;
bool sentHeader = false;

int sent = 0;
std::streampos cursor = std::streampos();
};
}
Loading

0 comments on commit 5cb2e14

Please sign in to comment.