|
| 1 | +#include "httplib.h" |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +int main() { |
| 5 | + using namespace httplib; |
| 6 | + |
| 7 | + // Example usage of parse_accept_header function |
| 8 | + std::cout << "=== Accept Header Parser Example ===" << std::endl; |
| 9 | + |
| 10 | + // Example 1: Simple Accept header |
| 11 | + std::string accept1 = "text/html,application/json,text/plain"; |
| 12 | + std::vector<std::string> result1; |
| 13 | + if (detail::parse_accept_header(accept1, result1)) { |
| 14 | + std::cout << "\nExample 1: " << accept1 << std::endl; |
| 15 | + std::cout << "Parsed order:" << std::endl; |
| 16 | + for (size_t i = 0; i < result1.size(); ++i) { |
| 17 | + std::cout << " " << (i + 1) << ". " << result1[i] << std::endl; |
| 18 | + } |
| 19 | + } else { |
| 20 | + std::cout << "\nExample 1: Failed to parse Accept header" << std::endl; |
| 21 | + } |
| 22 | + |
| 23 | + // Example 2: Accept header with quality values |
| 24 | + std::string accept2 = "text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8"; |
| 25 | + std::vector<std::string> result2; |
| 26 | + if (detail::parse_accept_header(accept2, result2)) { |
| 27 | + std::cout << "\nExample 2: " << accept2 << std::endl; |
| 28 | + std::cout << "Parsed order (sorted by priority):" << std::endl; |
| 29 | + for (size_t i = 0; i < result2.size(); ++i) { |
| 30 | + std::cout << " " << (i + 1) << ". " << result2[i] << std::endl; |
| 31 | + } |
| 32 | + } else { |
| 33 | + std::cout << "\nExample 2: Failed to parse Accept header" << std::endl; |
| 34 | + } |
| 35 | + |
| 36 | + // Example 3: Browser-like Accept header |
| 37 | + std::string accept3 = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; |
| 38 | + std::vector<std::string> result3; |
| 39 | + if (detail::parse_accept_header(accept3, result3)) { |
| 40 | + std::cout << "\nExample 3: " << accept3 << std::endl; |
| 41 | + std::cout << "Parsed order:" << std::endl; |
| 42 | + for (size_t i = 0; i < result3.size(); ++i) { |
| 43 | + std::cout << " " << (i + 1) << ". " << result3[i] << std::endl; |
| 44 | + } |
| 45 | + } else { |
| 46 | + std::cout << "\nExample 3: Failed to parse Accept header" << std::endl; |
| 47 | + } |
| 48 | + |
| 49 | + // Example 4: Invalid Accept header examples |
| 50 | + std::cout << "\n=== Invalid Accept Header Examples ===" << std::endl; |
| 51 | + |
| 52 | + std::vector<std::string> invalid_examples = { |
| 53 | + "text/html;q=1.5,application/json", // q > 1.0 |
| 54 | + "text/html;q=-0.1,application/json", // q < 0.0 |
| 55 | + "text/html;q=invalid,application/json", // invalid q value |
| 56 | + "invalidtype,application/json", // invalid media type |
| 57 | + ",application/json" // empty entry |
| 58 | + }; |
| 59 | + |
| 60 | + for (const auto& invalid_accept : invalid_examples) { |
| 61 | + std::vector<std::string> temp_result; |
| 62 | + std::cout << "\nTesting invalid: " << invalid_accept << std::endl; |
| 63 | + if (detail::parse_accept_header(invalid_accept, temp_result)) { |
| 64 | + std::cout << " Unexpectedly succeeded!" << std::endl; |
| 65 | + } else { |
| 66 | + std::cout << " Correctly rejected as invalid" << std::endl; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Example 4: Server usage example |
| 71 | + std::cout << "\n=== Server Usage Example ===" << std::endl; |
| 72 | + Server svr; |
| 73 | + |
| 74 | + svr.Get("/api/data", [](const Request& req, Response& res) { |
| 75 | + // Get Accept header |
| 76 | + auto accept_header = req.get_header_value("Accept"); |
| 77 | + if (accept_header.empty()) { |
| 78 | + accept_header = "*/*"; // Default if no Accept header |
| 79 | + } |
| 80 | + |
| 81 | + // Parse accept header to get preferred content types |
| 82 | + std::vector<std::string> preferred_types; |
| 83 | + if (!detail::parse_accept_header(accept_header, preferred_types)) { |
| 84 | + // Invalid Accept header |
| 85 | + res.status = 400; // Bad Request |
| 86 | + res.set_content("Invalid Accept header", "text/plain"); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + std::cout << "Client Accept header: " << accept_header << std::endl; |
| 91 | + std::cout << "Preferred types in order:" << std::endl; |
| 92 | + for (size_t i = 0; i < preferred_types.size(); ++i) { |
| 93 | + std::cout << " " << (i + 1) << ". " << preferred_types[i] << std::endl; |
| 94 | + } |
| 95 | + |
| 96 | + // Choose response format based on client preference |
| 97 | + std::string response_content; |
| 98 | + std::string content_type; |
| 99 | + |
| 100 | + for (const auto& type : preferred_types) { |
| 101 | + if (type == "application/json" || type == "application/*" || type == "*/*") { |
| 102 | + response_content = "{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}"; |
| 103 | + content_type = "application/json"; |
| 104 | + break; |
| 105 | + } else if (type == "text/html" || type == "text/*") { |
| 106 | + response_content = "<html><body><h1>Hello, World!</h1><p>Data: 1, 2, 3</p></body></html>"; |
| 107 | + content_type = "text/html"; |
| 108 | + break; |
| 109 | + } else if (type == "text/plain") { |
| 110 | + response_content = "Hello, World!\nData: 1, 2, 3"; |
| 111 | + content_type = "text/plain"; |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (response_content.empty()) { |
| 117 | + // No supported content type found |
| 118 | + res.status = 406; // Not Acceptable |
| 119 | + res.set_content("No acceptable content type found", "text/plain"); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + res.set_content(response_content, content_type); |
| 124 | + std::cout << "Responding with: " << content_type << std::endl; |
| 125 | + }); |
| 126 | + |
| 127 | + std::cout << "Server configured. You can test it with:" << std::endl; |
| 128 | + std::cout << " curl -H \"Accept: application/json\" http://localhost:8080/api/data" << std::endl; |
| 129 | + std::cout << " curl -H \"Accept: text/html\" http://localhost:8080/api/data" << std::endl; |
| 130 | + std::cout << " curl -H \"Accept: text/plain\" http://localhost:8080/api/data" << std::endl; |
| 131 | + std::cout << " curl -H \"Accept: text/html;q=0.9,application/json;q=1.0\" http://localhost:8080/api/data" << std::endl; |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
0 commit comments