Format example/accept_header.cc

The file predates the clang-format hook, so any edit to it drags the
whole file into the diff. Reformat it on its own first.
This commit is contained in:
yhirose
2026-08-26 22:52:04 -04:00
parent 794a997d8c
commit efe709c45f

View File

@@ -21,7 +21,8 @@ int main() {
}
// Example 2: Accept header with quality values
std::string accept2 = "text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8";
std::string accept2 =
"text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8";
std::vector<std::string> result2;
if (detail::parse_accept_header(accept2, result2)) {
std::cout << "\nExample 2: " << accept2 << std::endl;
@@ -34,7 +35,8 @@ int main() {
}
// Example 3: Browser-like Accept header
std::string accept3 = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
std::string accept3 = "text/html,application/xhtml+xml,application/"
"xml;q=0.9,image/webp,*/*;q=0.8";
std::vector<std::string> result3;
if (detail::parse_accept_header(accept3, result3)) {
std::cout << "\nExample 3: " << accept3 << std::endl;
@@ -57,7 +59,7 @@ int main() {
",application/json" // empty entry
};
for (const auto& invalid_accept : invalid_examples) {
for (const auto &invalid_accept : invalid_examples) {
std::vector<std::string> temp_result;
std::cout << "\nTesting invalid: " << invalid_accept << std::endl;
if (detail::parse_accept_header(invalid_accept, temp_result)) {
@@ -71,7 +73,7 @@ int main() {
std::cout << "\n=== Server Usage Example ===" << std::endl;
Server svr;
svr.Get("/api/data", [](const Request& req, Response& res) {
svr.Get("/api/data", [](const Request &req, Response &res) {
// Get Accept header
auto accept_header = req.get_header_value("Accept");
if (accept_header.empty()) {
@@ -97,13 +99,16 @@ int main() {
std::string response_content;
std::string content_type;
for (const auto& type : preferred_types) {
if (type == "application/json" || type == "application/*" || type == "*/*") {
response_content = "{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}";
for (const auto &type : preferred_types) {
if (type == "application/json" || type == "application/*" ||
type == "*/*") {
response_content =
"{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}";
content_type = "application/json";
break;
} else if (type == "text/html" || type == "text/*") {
response_content = "<html><body><h1>Hello, World!</h1><p>Data: 1, 2, 3</p></body></html>";
response_content = "<html><body><h1>Hello, World!</h1><p>Data: 1, 2, "
"3</p></body></html>";
content_type = "text/html";
break;
} else if (type == "text/plain") {
@@ -125,10 +130,16 @@ int main() {
});
std::cout << "Server configured. You can test it with:" << std::endl;
std::cout << " curl -H \"Accept: application/json\" http://localhost:8080/api/data" << std::endl;
std::cout << " curl -H \"Accept: text/html\" http://localhost:8080/api/data" << std::endl;
std::cout << " curl -H \"Accept: text/plain\" http://localhost:8080/api/data" << std::endl;
std::cout << " curl -H \"Accept: text/html;q=0.9,application/json;q=1.0\" http://localhost:8080/api/data" << std::endl;
std::cout
<< " curl -H \"Accept: application/json\" http://localhost:8080/api/data"
<< std::endl;
std::cout << " curl -H \"Accept: text/html\" http://localhost:8080/api/data"
<< std::endl;
std::cout << " curl -H \"Accept: text/plain\" http://localhost:8080/api/data"
<< std::endl;
std::cout << " curl -H \"Accept: text/html;q=0.9,application/json;q=1.0\" "
"http://localhost:8080/api/data"
<< std::endl;
return 0;
}