parent
d9659374c7
commit
ef1389d5d1
27 changed files with 28 additions and 237 deletions
@ -0,0 +1,10 @@ |
|||||||
|
docs/ |
||||||
|
**.log |
||||||
|
bin/ |
||||||
|
bin-int/ |
||||||
|
.vscode/ |
||||||
|
.vs/ |
||||||
|
**.sln |
||||||
|
**Makefile |
||||||
|
**.make |
||||||
|
**.vcxproj* |
@ -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,48 +0,0 @@ |
|||||||
#include "http_server.h" |
|
||||||
|
|
||||||
void http_server()
|
|
||||||
{ |
|
||||||
IpAddress ip("127.0.0.1"); |
|
||||||
Socket sock(ip, PORT, IpProtocol::TCP); |
|
||||||
bool running = sock.init() && sock.start(5); |
|
||||||
char input = 'N'; |
|
||||||
|
|
||||||
do |
|
||||||
{ |
|
||||||
Connection conn = sock.ack(); |
|
||||||
if (conn >= 0)
|
|
||||||
{ |
|
||||||
std::string http_request(http_handler(sock, conn)); |
|
||||||
|
|
||||||
if (http_request == "") continue; |
|
||||||
HttpRequest req = http_parser(http_request); |
|
||||||
log("Http request"); |
|
||||||
log("Method " << (int)req.method) |
|
||||||
log("URL " << req.url) |
|
||||||
log("Body " << req.body) |
|
||||||
} |
|
||||||
log("Close?") |
|
||||||
input(input); |
|
||||||
} while (input != 'y'); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
std::string http_handler(Socket& sock, Connection conn)
|
|
||||||
{ |
|
||||||
Packet req; |
|
||||||
bool reading = true; |
|
||||||
while(reading) |
|
||||||
{ |
|
||||||
std::vector<char> raw_data; |
|
||||||
raw_data = sock.recv(conn, 4); |
|
||||||
reading = req.append_data(raw_data); |
|
||||||
} |
|
||||||
|
|
||||||
close(conn); |
|
||||||
|
|
||||||
int req_size = req.size(); |
|
||||||
std::unique_ptr<char[]> req_test = req.pull<char>(req_size); |
|
||||||
|
|
||||||
if (req_size) return std::string(req_test.release(), req_size); |
|
||||||
return ""; |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
#include <commun.h> |
|
||||||
|
|
||||||
#include "http_parser.h" |
|
||||||
|
|
||||||
using namespace Bk::Net; |
|
||||||
void http_server(); |
|
||||||
std::string http_handler(Socket& sock, Connection conn); |
|
@ -1,34 +0,0 @@ |
|||||||
#include "http_tools.h" |
|
||||||
|
|
||||||
std::string string_to_upper(std::string& str) |
|
||||||
{ |
|
||||||
for (int i = 0; i < str.length(); i++) |
|
||||||
{ |
|
||||||
str[i] = std::toupper(str[i]); |
|
||||||
} |
|
||||||
return str; |
|
||||||
} |
|
||||||
|
|
||||||
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter, int cpt) |
|
||||||
{ |
|
||||||
std::unique_ptr<std::vector<std::string>> splits(new std::vector<std::string>(0)); |
|
||||||
size_t pos = 0; |
|
||||||
while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0) { |
|
||||||
splits->push_back(s.substr(0, pos)); |
|
||||||
s.erase(0, pos + delimiter.length()); |
|
||||||
} |
|
||||||
splits->push_back(s); |
|
||||||
return splits; |
|
||||||
} |
|
||||||
|
|
||||||
std::string string_trim(const std::string& str, const std::string& whitespace) |
|
||||||
{ |
|
||||||
const auto strBegin = str.find_first_not_of(whitespace); |
|
||||||
if (strBegin == std::string::npos) |
|
||||||
return ""; // no content
|
|
||||||
|
|
||||||
const auto strEnd = str.find_last_not_of(whitespace); |
|
||||||
const auto strRange = strEnd - strBegin + 1; |
|
||||||
|
|
||||||
return str.substr(strBegin, strRange); |
|
||||||
} |
|
@ -1,7 +0,0 @@ |
|||||||
#include <cstring> |
|
||||||
|
|
||||||
#include <commun.h> |
|
||||||
|
|
||||||
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::string string_trim(const std::string& str, const std::string& whitespace = " "); |
|
Loading…
Reference in New Issue