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 // 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; std::vector<std::string> result2;
if (detail::parse_accept_header(accept2, result2)) { if (detail::parse_accept_header(accept2, result2)) {
std::cout << "\nExample 2: " << accept2 << std::endl; std::cout << "\nExample 2: " << accept2 << std::endl;
@@ -34,7 +35,8 @@ int main() {
} }
// Example 3: Browser-like Accept header // 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; std::vector<std::string> result3;
if (detail::parse_accept_header(accept3, result3)) { if (detail::parse_accept_header(accept3, result3)) {
std::cout << "\nExample 3: " << accept3 << std::endl; std::cout << "\nExample 3: " << accept3 << std::endl;
@@ -98,12 +100,15 @@ int main() {
std::string content_type; std::string content_type;
for (const auto &type : preferred_types) { for (const auto &type : preferred_types) {
if (type == "application/json" || type == "application/*" || type == "*/*") { if (type == "application/json" || type == "application/*" ||
response_content = "{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}"; type == "*/*") {
response_content =
"{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}";
content_type = "application/json"; content_type = "application/json";
break; break;
} else if (type == "text/html" || type == "text/*") { } 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"; content_type = "text/html";
break; break;
} else if (type == "text/plain") { } else if (type == "text/plain") {
@@ -125,10 +130,16 @@ int main() {
}); });
std::cout << "Server configured. You can test it with:" << std::endl; 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
std::cout << " curl -H \"Accept: text/html\" http://localhost:8080/api/data" << std::endl; << " curl -H \"Accept: application/json\" http://localhost:8080/api/data"
std::cout << " curl -H \"Accept: text/plain\" http://localhost:8080/api/data" << std::endl; << 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: 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; return 0;
} }