feat: add HttpRequest method

This commit is contained in:
Axel Cocat
2021-08-06 16:55:05 +02:00
parent 8b77940be7
commit 84f201b6b0
9 changed files with 8448 additions and 1 deletions

58
HttpManager.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef ELUNA_HTTP_MANAGER_H
#define ELUNA_HTTP_MANAGER_H
#include <regex>
#include "libs/httplib.h"
#include "libs/rigtorp/SPSCQueue.h"
struct HttpWorkItem
{
public:
HttpWorkItem(int funcRef, const std::string& httpVerb, const std::string& url, const std::string& body, const std::string &contentType, const httplib::Headers& headers);
int funcRef;
std::string httpVerb;
std::string url;
std::string body;
std::string contentType;
httplib::Headers headers;
};
struct HttpResponse
{
public:
HttpResponse(int funcRef, int statusCode, const std::string& body, const httplib::Headers& headers);
int funcRef;
int statusCode;
std::string body;
httplib::Headers headers;
};
class HttpManager
{
public:
HttpManager();
void PushRequest(HttpWorkItem* item);
void StartHttpWorker();
void HandleHttpResponses();
private:
void HttpWorkerThread();
bool ParseUrl(const std::string& url, std::string& host, std::string& path);
httplib::Result DoRequest(httplib::Client& client, HttpWorkItem* req, const std::string& path);
rigtorp::SPSCQueue<HttpWorkItem*> httpWorkQueue;
rigtorp::SPSCQueue<HttpResponse*> httpResponseQueue;
std::thread httpWorkerThread;
bool startedHttpWorkerThread;
std::atomic_bool httpCancelationToken;
std::condition_variable httpCondVar;
std::mutex httpCondVarMutex;
std::regex parseUrlRegex;
};
#endif // #ifndef ELUNA_HTTP_MANAGER_H