2025-01-13 02:35:49 +10:00
|
|
|
#include "socket_up.h"
|
|
|
|
#include "socket_handler.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2025-01-14 04:53:43 +10:00
|
|
|
#include <iterator>
|
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
|
|
|
|
struct request_info {
|
|
|
|
std::string method, path, params;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string get_string_from_regex(std::string text, std::regex pattern) {
|
|
|
|
auto parts_begin = std::sregex_iterator(
|
|
|
|
text.begin(),
|
|
|
|
text.end(),
|
|
|
|
pattern
|
|
|
|
);
|
|
|
|
auto parts_end = std::sregex_iterator();
|
|
|
|
if (std::distance(parts_begin, parts_end)) {
|
|
|
|
std::smatch part_match = *parts_begin;
|
|
|
|
return part_match.str();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
struct request_info parse_request(const std::string& request) {
|
|
|
|
std::regex request_line_regex(
|
|
|
|
"(GET|POST|PUT|DELETE) .* HTTP/",
|
|
|
|
std::regex_constants::ECMAScript | std::regex_constants::icase
|
|
|
|
);
|
|
|
|
std::string request_line = get_string_from_regex(request, request_line_regex);
|
|
|
|
|
|
|
|
std::regex method_regex("^[A-Z]+");
|
|
|
|
std::regex path_regex("/[^? ]*");
|
|
|
|
std::regex params_regex(R"(\?[^ ]*)");
|
|
|
|
|
|
|
|
struct request_info this_request;
|
|
|
|
|
|
|
|
this_request.method = get_string_from_regex(request_line, method_regex);
|
|
|
|
this_request.path = get_string_from_regex(request_line, path_regex);
|
|
|
|
this_request.params = get_string_from_regex(request_line, params_regex);
|
|
|
|
|
|
|
|
return this_request;
|
|
|
|
}
|
|
|
|
|
2025-01-13 04:58:58 +10:00
|
|
|
|
2025-01-13 02:35:49 +10:00
|
|
|
int main() {
|
|
|
|
|
|
|
|
SOCKET listenSocket = SocketUp::config_socket_windows();
|
2025-01-13 04:58:58 +10:00
|
|
|
SocketHandler::working_with_client_windows(listenSocket);
|
2025-01-13 02:35:49 +10:00
|
|
|
|
2025-01-13 04:58:58 +10:00
|
|
|
closesocket(listenSocket);
|
|
|
|
WSACleanup();
|
2025-01-13 02:35:49 +10:00
|
|
|
return 0;
|
|
|
|
}
|