parent
8fdd4be2ac
commit
7ccbc81ea4
11 changed files with 168 additions and 103 deletions
@ -1,3 +1,9 @@ |
|||||||
clear |
clear |
||||||
|
rm -rf ./bin/linux-x86_64-Debug/server |
||||||
premake5 gmake2 |
premake5 gmake2 |
||||||
make |
make |
||||||
|
|
||||||
|
if [ "$1" == "-exec" ]; then |
||||||
|
echo STARTING PROGRAM |
||||||
|
./bin/linux-x86_64-Debug/server/server |
||||||
|
fi |
@ -0,0 +1,91 @@ |
|||||||
|
#include "http_packet.h" |
||||||
|
#include <iomanip> |
||||||
|
|
||||||
|
HttpRequest::HttpRequest(std::string method, |
||||||
|
std::string url, |
||||||
|
std::string version, |
||||||
|
HttpParams params, |
||||||
|
std::string body)
|
||||||
|
: method(method), url(url), version(version), params(params), body(body) {} |
||||||
|
|
||||||
|
HttpRequest::HttpRequest(std::string data) |
||||||
|
{ |
||||||
|
log(data) |
||||||
|
auto lines = string_split(data, "\n"); |
||||||
|
auto first_line = std::string(lines->at(0)); |
||||||
|
auto req_data = string_split(first_line, " "); |
||||||
|
method = req_data->at(0); |
||||||
|
url = req_data->at(1); |
||||||
|
version = req_data->at(2); |
||||||
|
body = std::string(lines->at(lines->size() - 1)); |
||||||
|
lines->erase(lines->begin()); |
||||||
|
lines->erase(lines->end()); |
||||||
|
int i = 0; |
||||||
|
for (auto line : *lines) |
||||||
|
{ |
||||||
|
log(i << "|" << line)
|
||||||
|
i++; |
||||||
|
} |
||||||
|
log(method << url << version) |
||||||
|
log("BODY|" << body) |
||||||
|
for (auto line : *lines) |
||||||
|
{ |
||||||
|
auto param = string_split(line, ":", 1); |
||||||
|
if (param->size() >= 2)
|
||||||
|
{ |
||||||
|
string_trim(param->at(1)); |
||||||
|
params.insert({param->at(0), param->at(1)}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for (auto pair : params) |
||||||
|
{ |
||||||
|
log(pair.second) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
std::string HttpRequest::to_string() |
||||||
|
{ |
||||||
|
std::string request = ""; |
||||||
|
request += method + " " + url + " " + version; |
||||||
|
std::string param_order[] =
|
||||||
|
{ |
||||||
|
"Host", |
||||||
|
"User-Agent", |
||||||
|
"Accept", |
||||||
|
"Accept-Language", |
||||||
|
"Accept-Encoding", |
||||||
|
"Connection", |
||||||
|
"Upgrade-Insecure-Request", |
||||||
|
"Sec-Fetch-Dest", |
||||||
|
"Sec-Fetch-Mode", |
||||||
|
"Sec-Fetch-Site" |
||||||
|
}; |
||||||
|
if (params.size()) for ( const auto& param : param_order) if (params[param].length()) request += param + ": " + params[param] + "\r\n"; |
||||||
|
request += "\r\n"; |
||||||
|
if (body.length()) request += body; |
||||||
|
return request; |
||||||
|
} |
||||||
|
|
||||||
|
HttpReponse::HttpReponse(std::string status, |
||||||
|
std::string version, |
||||||
|
HttpParams params, |
||||||
|
std::string body) |
||||||
|
: status(status), version(version), params(params), body(body) {} |
||||||
|
|
||||||
|
HttpReponse::HttpReponse(std::string data) |
||||||
|
{ |
||||||
|
status = ""; |
||||||
|
version = ""; |
||||||
|
body = ""; |
||||||
|
} |
||||||
|
|
||||||
|
std::string HttpReponse::to_string() |
||||||
|
{ |
||||||
|
std::string reponse = ""; |
||||||
|
reponse = version + " " + status + " \r\n"; |
||||||
|
if (params.size()) for ( const auto& pair : params) reponse += pair.first + ": " + pair.second + " \r\n"; |
||||||
|
reponse += "\r\n"; |
||||||
|
if (body.length()) reponse += body; |
||||||
|
return reponse; |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <commun.h> |
||||||
|
#include "http_tools.h" |
||||||
|
|
||||||
|
using HttpParams = std::unordered_map<std::string, std::string>; |
||||||
|
|
||||||
|
class HttpRequest
|
||||||
|
{ |
||||||
|
public: |
||||||
|
HttpRequest(std::string method, |
||||||
|
std::string url, |
||||||
|
std::string version, |
||||||
|
HttpParams params, |
||||||
|
std::string body); |
||||||
|
|
||||||
|
HttpRequest(std::string data); |
||||||
|
|
||||||
|
std::string to_string(); |
||||||
|
|
||||||
|
std::string method; |
||||||
|
std::string url; |
||||||
|
std::string version; |
||||||
|
HttpParams params; |
||||||
|
std::string body; |
||||||
|
}; |
||||||
|
|
||||||
|
class HttpReponse |
||||||
|
{ |
||||||
|
public: |
||||||
|
HttpReponse(std::string status, |
||||||
|
std::string version, |
||||||
|
HttpParams params, |
||||||
|
std::string body); |
||||||
|
|
||||||
|
HttpReponse(std::string data); |
||||||
|
|
||||||
|
std::string to_string(); |
||||||
|
|
||||||
|
std::string status; |
||||||
|
std::string version; |
||||||
|
HttpParams params; |
||||||
|
std::string body; |
||||||
|
}; |
@ -1,42 +0,0 @@ |
|||||||
#include "http_parser.h" |
|
||||||
|
|
||||||
HttpRequest http_parser(std::string req) |
|
||||||
{ |
|
||||||
std::string url = "", method = "", body = ""; |
|
||||||
std::unordered_map<std::string, std::string> params; |
|
||||||
|
|
||||||
auto lines = string_split(req, "\n"); |
|
||||||
|
|
||||||
auto first_line = string_split(lines->at(0), " "); |
|
||||||
method = string_to_upper(first_line->at(0)); |
|
||||||
url = first_line->at(1); |
|
||||||
body = lines->at(lines->size() - 1); |
|
||||||
|
|
||||||
lines->erase(lines->begin()); |
|
||||||
lines->pop_back(); |
|
||||||
|
|
||||||
for (auto line : *lines) |
|
||||||
{ |
|
||||||
auto param = string_split(line, ":", 1); |
|
||||||
if (param->size() == 2) |
|
||||||
{ |
|
||||||
params.insert({ param->at(0), string_trim(param->at(1))}); |
|
||||||
} |
|
||||||
} |
|
||||||
return HttpRequest { |
|
||||||
http_resolve_methode(method), |
|
||||||
url, |
|
||||||
params, |
|
||||||
body |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
HttpMethod http_resolve_methode(std::string method) |
|
||||||
{ |
|
||||||
if (method.compare("GET")) return HttpMethod::GET; |
|
||||||
else if (method.compare("POST")) return HttpMethod::POST; |
|
||||||
else if (method.compare("PUT")) return HttpMethod::PUT; |
|
||||||
else if (method.compare("DELETE")) return HttpMethod::DELETE; |
|
||||||
else return HttpMethod::NONE; |
|
||||||
} |
|
@ -1,26 +0,0 @@ |
|||||||
#include <unordered_map> |
|
||||||
|
|
||||||
#include <commun.h> |
|
||||||
|
|
||||||
#include "http_tools.h" |
|
||||||
|
|
||||||
enum class HttpMethod |
|
||||||
{ |
|
||||||
NONE = 0, |
|
||||||
GET = 1, |
|
||||||
POST = 2, |
|
||||||
PUT = 3, |
|
||||||
DELETE = 4, |
|
||||||
}; |
|
||||||
|
|
||||||
class HttpRequest
|
|
||||||
{ |
|
||||||
public: |
|
||||||
HttpMethod method; |
|
||||||
std::string url; |
|
||||||
std::unordered_map<std::string, std::string> params; |
|
||||||
std::string body; |
|
||||||
}; |
|
||||||
|
|
||||||
HttpRequest http_parser(std::string req); |
|
||||||
HttpMethod http_resolve_methode(std::string method); |
|
@ -1,6 +1,7 @@ |
|||||||
#include <commun.h> |
#pragma once |
||||||
|
|
||||||
#include "http_parser.h" |
#include <commun.h> |
||||||
|
#include "http_packet.h" |
||||||
|
|
||||||
using namespace Bk::Net; |
using namespace Bk::Net; |
||||||
void http_server(); |
void http_server(); |
||||||
|
@ -1,8 +1,9 @@ |
|||||||
#include <cstring> |
#pragma once |
||||||
|
|
||||||
|
#include <cstring> |
||||||
#include <commun.h> |
#include <commun.h> |
||||||
|
|
||||||
std::string string_to_lower(std::string& str); |
std::string string_to_lower(std::string& str); |
||||||
std::string string_to_upper(std::string& str); |
std::string string_to_upper(std::string& str); |
||||||
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter, int cpt = -1); |
std::unique_ptr<std::vector<std::string>> string_split(std::string& str, std::string delimiter, int cpt = -1); |
||||||
std::string string_trim(const std::string& str, const std::string& whitespace = " "); |
void string_trim(std::string& str, const std::string& whitespace = " \b\0"); |
Loading…
Reference in New Issue