|
|
|
@ -2,8 +2,20 @@ |
|
|
|
|
|
|
|
|
|
using namespace Bk::Net; |
|
|
|
|
|
|
|
|
|
enum class HttpMethod |
|
|
|
|
{ |
|
|
|
|
NONE = 0, |
|
|
|
|
GET = 1, |
|
|
|
|
POST = 2, |
|
|
|
|
PUT = 3, |
|
|
|
|
DELETE = 4, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
void http_server(); |
|
|
|
|
std::string http_handle(Socket& sock, Connection conn); |
|
|
|
|
std::string http_handler(Socket& sock, Connection conn); |
|
|
|
|
void http_parser(std::string req); |
|
|
|
|
HttpMethod resolve_methode(std::string method); |
|
|
|
|
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter); |
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{ |
|
|
|
@ -14,22 +26,23 @@ int main() |
|
|
|
|
void http_server()
|
|
|
|
|
{ |
|
|
|
|
IpAddress ip("127.0.0.1"); |
|
|
|
|
Socket sock(ip, 80, IpProtocol::TCP); |
|
|
|
|
Socket sock(ip, PORT, IpProtocol::TCP); |
|
|
|
|
bool running = sock.init() && sock.start(5); |
|
|
|
|
while (running) |
|
|
|
|
{ |
|
|
|
|
Connection conn = sock.ack(); |
|
|
|
|
if (conn >= 0)
|
|
|
|
|
{ |
|
|
|
|
log(http_handle(sock, conn)); |
|
|
|
|
std::string http_request(http_handler(sock, conn)); |
|
|
|
|
http_parser(http_request); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string http_handle(Socket& sock, Connection conn)
|
|
|
|
|
std::string http_handler(Socket& sock, Connection conn)
|
|
|
|
|
{ |
|
|
|
|
Packet req; |
|
|
|
|
|
|
|
|
|
bool reading = true; |
|
|
|
|
while(reading) |
|
|
|
|
{ |
|
|
|
@ -46,3 +59,48 @@ std::string http_handle(Socket& sock, Connection conn) |
|
|
|
|
if (req_size) return std::string(req_test.release(), req_size); |
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void http_parser(std::string req) |
|
|
|
|
{ |
|
|
|
|
auto lines = string_split(req, "\r\n"); |
|
|
|
|
for (auto line : *lines) |
|
|
|
|
{ |
|
|
|
|
auto param = string_split(line, ":"); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpMethod resolve_methode(std::string method) |
|
|
|
|
{ |
|
|
|
|
if (method == "GET") return HttpMethod::GET; |
|
|
|
|
else if (method == "POST") return HttpMethod::POST; |
|
|
|
|
else if (method == "PUT") return HttpMethod::PUT; |
|
|
|
|
else if (method == "DELETE") return HttpMethod::DELETE; |
|
|
|
|
else return HttpMethod::NONE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter) |
|
|
|
|
{ |
|
|
|
|
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) { |
|
|
|
|
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 = " \t") |
|
|
|
|
{ |
|
|
|
|
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); |
|
|
|
|
} |