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::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)
HttpResponse & setHeader (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 96 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 103 of file http_server.h.

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

References body, headers, status_code, status_text, and text().

Member Function Documentation

◆ badRequest()

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

Definition at line 128 of file http_server.h.

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

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

◆ html()

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

Definition at line 116 of file http_server.h.

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

References body, HttpResponse(), and ok().

◆ json()

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

Definition at line 112 of file http_server.h.

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

References body, HttpResponse(), and ok().

◆ noContent()

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()

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

Definition at line 124 of file http_server.h.

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

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

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

◆ ok()

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

Definition at line 108 of file http_server.h.

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

References body, and HttpResponse().

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

◆ serialize()

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

Serialize response to HTTP/1.1 format.

Definition at line 63 of file http_server.cpp.

63 {
64 std::ostringstream oss;
65
66 // Status line
67 oss << "HTTP/1.1 " << status_code << " " << status_text << "\r\n";
68
69 // Headers
70 bool has_content_length = false;
71 bool has_connection = false;
72
73 for (const auto &[key, value] : headers) {
74 oss << key << ": " << value << "\r\n";
75 std::string lower_key = key;
76 std::transform(lower_key.begin(), lower_key.end(), lower_key.begin(), [](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()

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

Definition at line 132 of file http_server.h.

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

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, and HttpResponse().

◆ text()

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

Definition at line 120 of file http_server.h.

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

References body, HttpResponse(), and ok().

Referenced by HttpResponse().

Field Documentation

◆ body

std::string ascii_query::HttpResponse::body

Definition at line 100 of file http_server.h.

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

◆ headers

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

Definition at line 99 of file http_server.h.

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

◆ status_code

int ascii_query::HttpResponse::status_code = 200

Definition at line 97 of file http_server.h.

Referenced by HttpResponse(), and serialize().

◆ status_text

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

Definition at line 98 of file http_server.h.

Referenced by HttpResponse(), and serialize().


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