ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
ascii_query::HttpResponse Struct Reference

HTTP response builder. More...

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

Public Member Functions

 HttpResponse ()=default
 
 HttpResponse (int code, std::string text, std::string content_type, std::string response_body)
 
HttpResponsesetHeader (const std::string &name, const std::string &value)
 Set a header.
 
std::string serialize () const
 Serialize response to HTTP/1.1 format.
 

Static Public Member Functions

static HttpResponse ok (const std::string &content_type, const std::string &body)
 
static HttpResponse json (const std::string &body)
 
static HttpResponse html (const std::string &body)
 
static HttpResponse text (const std::string &body)
 
static HttpResponse notFound (const std::string &message="Not Found")
 
static HttpResponse badRequest (const std::string &message="Bad Request")
 
static HttpResponse serverError (const std::string &message="Internal Server Error")
 
static HttpResponse noContent ()
 

Data Fields

int status_code = 200
 
std::string status_text = "OK"
 
std::unordered_map< std::string, std::string > headers
 
std::string body
 

Detailed Description

HTTP response builder.

Definition at line 94 of file http_server.h.

Constructor & Destructor Documentation

◆ HttpResponse() [1/2]

ascii_query::HttpResponse::HttpResponse ( )
default

◆ HttpResponse() [2/2]

ascii_query::HttpResponse::HttpResponse ( int  code,
std::string  text,
std::string  content_type,
std::string  response_body 
)
inline

Definition at line 101 of file http_server.h.

102 : status_code(code), status_text(std::move(text)), body(std::move(response_body)) {
103 headers["Content-Type"] = std::move(content_type);
104 }
std::unordered_map< std::string, std::string > headers
Definition http_server.h:97
static HttpResponse text(const std::string &body)

References headers.

Member Function Documentation

◆ badRequest()

static HttpResponse ascii_query::HttpResponse::badRequest ( const std::string &  message = "Bad Request")
inlinestatic

Definition at line 127 of file http_server.h.

127 {
128 return HttpResponse(400, "Bad Request", "application/json", R"({"error":")" + json::escape(message) + R"("})");
129 }
std::string escape(const std::string &str)
Escape a string for JSON output.
Definition json.h:30

References ascii_query::json::escape(), and HttpResponse().

◆ html()

static HttpResponse ascii_query::HttpResponse::html ( const std::string &  body)
inlinestatic

Definition at line 115 of file http_server.h.

115 {
116 return ok("text/html; charset=utf-8", body);
117 }
static HttpResponse ok(const std::string &content_type, const std::string &body)

References body, and ok().

◆ json()

static HttpResponse ascii_query::HttpResponse::json ( const std::string &  body)
inlinestatic

Definition at line 111 of file http_server.h.

111 {
112 return ok("application/json", body);
113 }

References body, and ok().

◆ noContent()

static HttpResponse ascii_query::HttpResponse::noContent ( )
inlinestatic

Definition at line 136 of file http_server.h.

136 {
137 return HttpResponse(204, "No Content", "text/plain", "");
138 }

References HttpResponse().

◆ notFound()

static HttpResponse ascii_query::HttpResponse::notFound ( const std::string &  message = "Not Found")
inlinestatic

Definition at line 123 of file http_server.h.

123 {
124 return HttpResponse(404, "Not Found", "application/json", R"({"error":")" + json::escape(message) + R"("})");
125 }

References ascii_query::json::escape(), and HttpResponse().

Referenced by ascii_query::HttpServer::HttpServer().

◆ ok()

static HttpResponse ascii_query::HttpResponse::ok ( const std::string &  content_type,
const std::string &  body 
)
inlinestatic

Definition at line 107 of file http_server.h.

107 {
108 return HttpResponse(200, "OK", content_type, body);
109 }

References body, and HttpResponse().

Referenced by html(), json(), and text().

◆ serialize()

std::string ascii_query::HttpResponse::serialize ( ) const

Serialize response to HTTP/1.1 format.

Definition at line 62 of file http_server.cpp.

62 {
63 std::ostringstream oss;
64
65 // Status line
66 oss << "HTTP/1.1 " << status_code << " " << status_text << "\r\n";
67
68 // Headers
69 bool has_content_length = false;
70 bool has_connection = false;
71
72 for (const auto &[key, value] : headers) {
73 oss << key << ": " << value << "\r\n";
74 std::string lower_key = key;
75 std::transform(lower_key.begin(), lower_key.end(), lower_key.begin(),
76 [](unsigned char c) { return std::tolower(c); });
77 if (lower_key == "content-length")
78 has_content_length = true;
79 if (lower_key == "connection")
80 has_connection = true;
81 }
82
83 // Add Content-Length if not present
84 if (!has_content_length) {
85 oss << "Content-Length: " << body.size() << "\r\n";
86 }
87
88 // Add Connection: close if not present
89 if (!has_connection) {
90 oss << "Connection: close\r\n";
91 }
92
93 // CORS headers for browser access
94 oss << "Access-Control-Allow-Origin: *\r\n";
95 oss << "Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS\r\n";
96 oss << "Access-Control-Allow-Headers: Content-Type\r\n";
97
98 // End headers
99 oss << "\r\n";
100
101 // Body
102 oss << body;
103
104 return oss.str();
105}

References body, headers, status_code, and status_text.

◆ serverError()

static HttpResponse ascii_query::HttpResponse::serverError ( const std::string &  message = "Internal Server Error")
inlinestatic

Definition at line 131 of file http_server.h.

131 {
132 return HttpResponse(500, "Internal Server Error", "application/json",
133 R"({"error":")" + json::escape(message) + R"("})");

References ascii_query::json::escape(), and HttpResponse().

◆ setHeader()

HttpResponse & ascii_query::HttpResponse::setHeader ( const std::string &  name,
const std::string &  value 
)
inline

Set a header.

Definition at line 143 of file http_server.h.

143 {
144 headers[name] = value;
145 return *this;
146 }

References headers.

◆ text()

static HttpResponse ascii_query::HttpResponse::text ( const std::string &  body)
inlinestatic

Definition at line 119 of file http_server.h.

119 {
120 return ok("text/plain", body);
121 }

References body, and ok().

Field Documentation

◆ body

std::string ascii_query::HttpResponse::body

Definition at line 98 of file http_server.h.

Referenced by html(), json(), ok(), serialize(), and text().

◆ headers

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

Definition at line 97 of file http_server.h.

Referenced by HttpResponse(), serialize(), and setHeader().

◆ status_code

int ascii_query::HttpResponse::status_code = 200

Definition at line 95 of file http_server.h.

Referenced by serialize().

◆ status_text

std::string ascii_query::HttpResponse::status_text = "OK"

Definition at line 96 of file http_server.h.

Referenced by serialize().


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