ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
ascii_query::HttpRequest Struct Reference

Parsed HTTP request. More...

#include <src/tooling/query/http_server.h>

Public Member Functions

std::string param (const std::string &name, const std::string &default_value="") const
 Get a query parameter value.
bool hasParam (const std::string &name) const
 Check if a query parameter exists (flag-style, like &break).
int paramInt (const std::string &name, int default_value=0) const
 Get a query parameter as integer.
std::string header (const std::string &name, const std::string &default_value="") const
 Get a header value.

Data Fields

std::string method
 GET, POST, etc.
std::string path
 Request path (without query string).
std::string query_string
 Query string (after ?).
std::unordered_map< std::string, std::string > headers
 Request headers.
std::unordered_map< std::string, std::string > params
 Parsed query parameters.
std::string body
 Request body.

Detailed Description

Parsed HTTP request.

HTTP response builder

Parsed HTTP request

Definition at line 39 of file http_server.h.

Member Function Documentation

◆ hasParam()

bool ascii_query::HttpRequest::hasParam ( const std::string & name) const
inlinenodiscard

Check if a query parameter exists (flag-style, like &break).

Parameters
nameParameter name
Returns
true if parameter is present

Definition at line 63 of file http_server.h.

63 {
64 return params.find(name) != params.end();
65 }
std::unordered_map< std::string, std::string > params
Parsed query parameters.
Definition http_server.h:44

References params.

◆ header()

std::string ascii_query::HttpRequest::header ( const std::string & name,
const std::string & default_value = "" ) const
nodiscard

Get a header value.

Parameters
nameHeader name (case-insensitive)
default_valueDefault if not found
Returns
Header value or default

Definition at line 44 of file http_server.cpp.

44 {
45 // Case-insensitive header lookup
46 std::string lower_name = name;
47 std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), [](unsigned char c) { return std::tolower(c); });
48
49 for (const auto &[key, value] : headers) {
50 std::string lower_key = key;
51 std::transform(lower_key.begin(), lower_key.end(), lower_key.begin(), [](unsigned char c) { return std::tolower(c); });
52 if (lower_key == lower_name) {
53 return value;
54 }
55 }
56 return default_value;
57}
std::unordered_map< std::string, std::string > headers
Request headers.
Definition http_server.h:43

References headers.

◆ param()

std::string ascii_query::HttpRequest::param ( const std::string & name,
const std::string & default_value = "" ) const
inlinenodiscard

Get a query parameter value.

Parameters
nameParameter name
default_valueDefault if not found
Returns
Parameter value or default

Definition at line 53 of file http_server.h.

53 {
54 auto it = params.find(name);
55 return (it != params.end()) ? it->second : default_value;
56 }

References params.

◆ paramInt()

int ascii_query::HttpRequest::paramInt ( const std::string & name,
int default_value = 0 ) const
inlinenodiscard

Get a query parameter as integer.

Parameters
nameParameter name
default_valueDefault if not found or not a number
Returns
Parameter value or default

Definition at line 73 of file http_server.h.

73 {
74 auto it = params.find(name);
75 if (it == params.end())
76 return default_value;
77 try {
78 return std::stoi(it->second);
79 } catch (...) {
80 return default_value;
81 }
82 }

References params.

Field Documentation

◆ body

std::string ascii_query::HttpRequest::body

Request body.

Definition at line 45 of file http_server.h.

◆ headers

std::unordered_map<std::string, std::string> ascii_query::HttpRequest::headers

Request headers.

Definition at line 43 of file http_server.h.

Referenced by header().

◆ method

std::string ascii_query::HttpRequest::method

GET, POST, etc.

Definition at line 40 of file http_server.h.

◆ params

std::unordered_map<std::string, std::string> ascii_query::HttpRequest::params

Parsed query parameters.

Definition at line 44 of file http_server.h.

Referenced by hasParam(), param(), and paramInt().

◆ path

std::string ascii_query::HttpRequest::path

Request path (without query string).

Definition at line 41 of file http_server.h.

◆ query_string

std::string ascii_query::HttpRequest::query_string

Query string (after ?).

Definition at line 42 of file http_server.h.


The documentation for this struct was generated from the following files: