1
0

accept_header.cc 5.7 KB

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