cpp_server/request.hpp

52 lines
1.1 KiB
C++

#pragma once
#include "common.hpp"
#include "cookie.hpp"
#include <unordered_map>
#include <vector>
using std::string_view;
using std::string;
struct Request {
HttpMethod method = HttpMethod::UNKNOWN;
string_view path;
string_view query;
string_view version;
string_view body;
std::unordered_map<string_view, string_view> headers;
std::unordered_map<string_view, string_view> params; // URL parameters
std::vector<Cookie> cookies; // Parsed cookies
size_t content_length = 0;
bool valid = false;
string_view get_cookie(string_view name) const {
return CookieHelpers::get_cookie(cookies, name);
}
string session_id() const {
return string(get_cookie("session_id"));
}
string get_param(string_view key) const {
auto it = params.find(key);
return it != params.end() ? string(it->second) : string{};
}
int get_param_int(string_view key) const {
auto it = params.find(key);
if (it == params.end()) return 0;
int result = 0;
for (char c : it->second) {
if (c >= '0' && c <= '9') {
result = result * 10 + (c - '0');
} else {
break;
}
}
return result;
}
};