mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-09-06 08:33:48 +00:00
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:
@@ -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;
|
||||||
@@ -57,7 +59,7 @@ int main() {
|
|||||||
",application/json" // empty entry
|
",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::vector<std::string> temp_result;
|
||||||
std::cout << "\nTesting invalid: " << invalid_accept << std::endl;
|
std::cout << "\nTesting invalid: " << invalid_accept << std::endl;
|
||||||
if (detail::parse_accept_header(invalid_accept, temp_result)) {
|
if (detail::parse_accept_header(invalid_accept, temp_result)) {
|
||||||
@@ -71,7 +73,7 @@ int main() {
|
|||||||
std::cout << "\n=== Server Usage Example ===" << std::endl;
|
std::cout << "\n=== Server Usage Example ===" << std::endl;
|
||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
svr.Get("/api/data", [](const Request& req, Response& res) {
|
svr.Get("/api/data", [](const Request &req, Response &res) {
|
||||||
// Get Accept header
|
// Get Accept header
|
||||||
auto accept_header = req.get_header_value("Accept");
|
auto accept_header = req.get_header_value("Accept");
|
||||||
if (accept_header.empty()) {
|
if (accept_header.empty()) {
|
||||||
@@ -97,13 +99,16 @@ int main() {
|
|||||||
std::string response_content;
|
std::string response_content;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user