Fixed http parsing

unix_test
anulax1225 ago%!(EXTRA string=1 year)
parent 8fdd4be2ac
commit 7ccbc81ea4
  1. 6
      build
  2. 1
      sandbox/client/client.cpp
  3. 3
      sandbox/commun.h
  4. 91
      sandbox/server/http/http_packet.cpp
  5. 44
      sandbox/server/http/http_packet.h
  6. 42
      sandbox/server/http/http_parser.cpp
  7. 26
      sandbox/server/http/http_parser.h
  8. 24
      sandbox/server/http/http_server.cpp
  9. 5
      sandbox/server/http/http_server.h
  10. 20
      sandbox/server/http/http_tools.cpp
  11. 7
      sandbox/server/http/http_tools.h

@ -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

@ -1,5 +1,4 @@
#include <sstream> #include <sstream>
#include "../commun.h" #include "../commun.h"
using namespace Bk::Net; using namespace Bk::Net;

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <unordered_map>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>
@ -8,4 +9,4 @@
#define log(str) std::cout << str << "\n"; #define log(str) std::cout << str << "\n";
#define input(ref) std::cin >> ref; #define input(ref) std::cin >> ref;
#define PORT 10001 #define PORT 8080

@ -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);

@ -5,21 +5,21 @@ void http_server()
IpAddress ip("127.0.0.1"); IpAddress ip("127.0.0.1");
Socket sock(ip, PORT, IpProtocol::TCP); Socket sock(ip, PORT, IpProtocol::TCP);
bool running = sock.init() && sock.start(5); bool running = sock.init() && sock.start(5);
char input = 'N'; char input = 'n';
do do
{ {
Connection conn = sock.ack(); Connection conn = sock.ack();
if (conn >= 0) if (conn >= 0)
{ {
log("New Conn")
std::string http_request(http_handler(sock, conn)); std::string http_request(http_handler(sock, conn));
if (http_request == "") continue; if (http_request == "") continue;
HttpRequest req = http_parser(http_request); HttpRequest req(http_request);
log("Http request"); log("to string")
log("Method " << (int)req.method) log(req.to_string())
log("URL " << req.url) close(conn);
log("Body " << req.body)
} }
log("Close?") log("Close?")
input(input); input(input);
@ -30,19 +30,9 @@ void http_server()
std::string http_handler(Socket& sock, Connection conn) std::string http_handler(Socket& sock, Connection conn)
{ {
Packet req; Packet req;
bool reading = true; while(req.append_data(sock.recv(conn, 4)));
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(); int req_size = req.size();
std::unique_ptr<char[]> req_test = req.pull<char>(req_size); std::unique_ptr<char[]> req_test = req.pull<char>(req_size);
if (req_size) return std::string(req_test.release(), req_size); if (req_size) return std::string(req_test.release(), req_size);
return ""; return "";
} }

@ -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();

@ -18,26 +18,26 @@ std::string string_to_upper(std::string& str)
return str; 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>> 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)); std::unique_ptr<std::vector<std::string>> splits(new std::vector<std::string>(0));
size_t pos = 0; size_t pos = 0;
while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0) { while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0)
{
splits->push_back(s.substr(0, pos)); splits->push_back(s.substr(0, pos));
s.erase(0, pos + delimiter.length()); s.erase(0, pos + delimiter.length());
} }
splits->push_back(s); if (s.length()) splits->push_back(s);
return splits; return splits;
} }
std::string string_trim(const std::string& str, const std::string& whitespace) void string_trim(std::string& str, const std::string& whitespace)
{ {
const auto strBegin = str.find_first_not_of(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 strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1; if (strBegin != std::string::npos)
{
return str.substr(strBegin, strRange); str.erase(0, strBegin);
}
} }

@ -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…
Cancel
Save