/
zzxxx97
/
flexcell
Обзор
Документация
Войти
/
zzxxx97
/
flexcell
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
unHttpRequestParser.cpp
86 строк
3 KB
Makar
Добавил все файлы backend проекта
25 авг 2025, 15:14
25 авг 2025, 15:14
e72d232
Код
Авторство
О чём код?
#include "unHttpRequestParser.h" #include <sstream> #include <iostream> #include <boost/algorithm/string.hpp> // �����������, ������� ��������� ������ � ���� ������ HttpRequestParser::HttpRequestParser(const std::string &request) { parse(request); } // ��������� ������ HTTP-������� const std::string& HttpRequestParser::get_method() const { return method_; } // ��������� URL �� ������� const std::string& HttpRequestParser::get_url() const { return url_; } // ��������� ������ HTTP-��������� const std::string& HttpRequestParser::get_version() const { return version_; } // ��������� ���������� ������� const std::map<std::string, std::string>& HttpRequestParser::get_headers() const { return headers_; } // ��������� ���� ������� const std::string& HttpRequestParser::get_body() const { return body_; } // ����� ��� �������� ����� ������� void HttpRequestParser::parse(const std::string &request) { size_t header_end_pos = request.find("\r\n\r\n"); if (header_end_pos != std::string::npos) { std::istringstream request_stream(request.substr(0, header_end_pos + 4)); std::string request_line; std::getline(request_stream, request_line); std::vector<std::string> request_parts; boost::split(request_parts, request_line, boost::is_any_of(" ")); if (request_parts.size() >= 3) { method_ = request_parts[0]; url_ = request_parts[1]; version_ = request_parts[2]; } parse_headers(request_stream); if (headers_.find("Content-Length") != headers_.end()) { size_t content_length = std::stoul(headers_["Content-Length"]); size_t body_start_pos = header_end_pos + 4; // ����� \r\n\r\n body_ = request.substr(body_start_pos, content_length); // ��������� ��������� ���� // std::cout << "Read body: " << body_ << std::endl; } } } // ����� ��� �������� ���������� void HttpRequestParser::parse_headers(std::istringstream &stream) { std::string line; while (std::getline(stream, line) && !line.empty()) { size_t delimiter_pos = line.find(":"); if (delimiter_pos != std::string::npos) { std::string key = line.substr(0, delimiter_pos); std::string value = line.substr(delimiter_pos + 1); boost::trim(key); boost::trim(value); headers_[key] = value; } } } std::vector<std::string> HttpRequestParser::get_path_parts() const { std::vector<std::string> parts; boost::split(parts, url_, boost::is_any_of("/"), boost::token_compress_on); return parts; }