1. <?php
  2. /**
  3. * Validates HTTP response configuration for short-lived tasks.
  4. *
  5. * @param array $config An array containing the response configuration.
  6. * Example:
  7. * [
  8. * 'status_code' => 200,
  9. * 'headers' => [
  10. * 'Content-Type' => 'application/json',
  11. * 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  12. * 'Pragma' => 'no-cache',
  13. * 'Expires' => '0',
  14. * ],
  15. * 'body_length_limit' => 1024, // in bytes
  16. * ]
  17. *
  18. * @return bool True if the configuration is valid, false otherwise.
  19. */
  20. function validateResponseConfig(array $config): bool
  21. {
  22. // Check if status code is a valid HTTP status code (100-599)
  23. if (!is_int($config['status_code']) || $config['status_code'] < 100 || $config['status_code'] > 599) {
  24. error_log("Invalid status code: " . var_export($config['status_code'], true));
  25. return false;
  26. }
  27. // Check if headers is an array
  28. if (!is_array($config['headers'])) {
  29. error_log("Headers must be an array: " . var_export($config['headers'], true));
  30. return false;
  31. }
  32. // Validate specific header keys (example: Content-Type, Cache-Control)
  33. if (isset($config['headers']['Content-Type']) && !is_string($config['headers']['Content-Type'])) {
  34. error_log("Invalid Content-Type header: " . var_export($config['headers']['Content-Type'], true));
  35. return false;
  36. }
  37. if (isset($config['headers']['Cache-Control']) && !is_string($config['headers']['Cache-Control'])) {
  38. error_log("Invalid Cache-Control header: " . var_export($config['headers']['Cache-Control'], true));
  39. return false;
  40. }
  41. if (isset($config['headers']['Pragma']) && !is_string($config['headers']['Pragma'])) {
  42. error_log("Invalid Pragma header: " . var_export($config['headers']['Pragma'], true));
  43. return false;
  44. }
  45. if (isset($config['headers']['Expires']) && !is_string($config['headers']['Expires'])) {
  46. error_log("Invalid Expires header: " . var_export($config['headers']['Expires'], true));
  47. return false;
  48. }
  49. // Check body length limit
  50. if (!is_int($config['body_length_limit']) || $config['body_length_limit'] <= 0) {
  51. error_log("Invalid body_length_limit: " . var_export($config['body_length_limit'], true));
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Example usage
  58. */
  59. $validConfig = [
  60. 'status_code' => 200,
  61. 'headers' => [
  62. 'Content-Type' => 'application/json',
  63. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  64. 'Pragma' => 'no-cache',
  65. 'Expires' => '0',
  66. ],
  67. 'body_length_limit' => 1024,
  68. ];
  69. $invalidConfig = [
  70. 'status_code' => 600, // Invalid status code
  71. 'headers' => 'not an array', // Invalid headers
  72. 'body_length_limit' => -1, // Invalid body length
  73. ];
  74. if (validateResponseConfig($validConfig)) {
  75. echo "Valid configuration.\n";
  76. } else {
  77. echo "Invalid configuration.\n";
  78. }
  79. if (validateResponseConfig($invalidConfig)) {
  80. echo "Valid configuration.\n";
  81. } else {
  82. echo "Invalid configuration.\n";
  83. }
  84. ?>

Add your comment