parent
d7043dede0
commit
75ecb8e33c
9 changed files with 24 additions and 237 deletions
@ -1,12 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include <unordered_map> |
||||
#include <iostream> |
||||
#include <vector> |
||||
#include <string> |
||||
#include <bakanet.h> |
||||
|
||||
#define log(str) std::cout << str << "\n"; |
||||
#define input(ref) std::cin >> ref; |
||||
|
||||
#define PORT 8080 |
@ -1,79 +0,0 @@ |
||||
#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) |
||||
{ |
||||
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()); |
||||
|
||||
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)}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
std::string HttpRequest::to_string() |
||||
{ |
||||
std::string request = ""; |
||||
request += method + " " + url + " " + version + "\r\n"; |
||||
std::string param_order[] =
|
||||
{ |
||||
"Host", |
||||
"User-Agent", |
||||
"Accept", |
||||
"Accept-Language", |
||||
"Accept-Encoding", |
||||
"Connection", |
||||
"Upgrade-Insecure-Requests", |
||||
"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; |
||||
} |
@ -1,44 +0,0 @@ |
||||
#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,38 +0,0 @@ |
||||
#include "http_server.h" |
||||
|
||||
void http_server()
|
||||
{ |
||||
IpAddress ip("127.0.0.1"); |
||||
Socket* sock = Socket::Create(ip, PORT, IpProtocol::TCP).release(); |
||||
bool running = sock->init() && sock->start(5); |
||||
char input = 'n'; |
||||
|
||||
do |
||||
{ |
||||
Connection conn = sock->ack(); |
||||
if (conn >= 0)
|
||||
{ |
||||
log("New Conn") |
||||
std::string http_request(http_handler(sock, conn)); |
||||
|
||||
if (http_request == "") continue; |
||||
HttpRequest req(http_request); |
||||
log("to string") |
||||
log(req.to_string()) |
||||
close(conn); |
||||
} |
||||
log("Close?") |
||||
input(input); |
||||
} while (input != 'y'); |
||||
delete sock; |
||||
} |
||||
|
||||
std::string http_handler(Socket* sock, Connection conn)
|
||||
{ |
||||
Packet req; |
||||
while(req.append_data(sock->obtain(conn, 4))); |
||||
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,8 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include <commun.h> |
||||
#include "http_packet.h" |
||||
|
||||
using namespace Bk::Net; |
||||
void http_server(); |
||||
std::string http_handler(Socket* sock, Connection conn); |
@ -1,43 +0,0 @@ |
||||
#include "http_tools.h" |
||||
|
||||
std::string string_to_lower(std::string& str) |
||||
{ |
||||
for (int i = 0; i < str.length(); i++) |
||||
{ |
||||
str[i] = std::tolower(str[i]); |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
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& str, std::string delimiter, int cpt) |
||||
{ |
||||
std::string s(str); |
||||
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()); |
||||
} |
||||
if (s.length()) splits->push_back(s); |
||||
return splits; |
||||
} |
||||
|
||||
void string_trim(std::string& str, const std::string& whitespace) |
||||
{ |
||||
const auto strBegin = str.find_first_not_of(whitespace); |
||||
const auto strEnd = str.find_last_not_of(whitespace); |
||||
if (strBegin != std::string::npos) |
||||
{ |
||||
str.erase(0, strBegin); |
||||
} |
||||
} |
@ -1,9 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include <cstring> |
||||
#include <commun.h> |
||||
|
||||
std::string string_to_lower(std::string& str); |
||||
std::string string_to_upper(std::string& str); |
||||
std::unique_ptr<std::vector<std::string>> string_split(std::string& str, std::string delimiter, int cpt = -1); |
||||
void string_trim(std::string& str, const std::string& whitespace = " \b\0"); |
Loading…
Reference in New Issue