gen_http_statuses_inserts.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. #
  3. # Generate code and header inserts for HTTP statues
  4. #
  5. # Copyright (c) 2019-2021 Karlson2k (Evgeny Grin) <k2k@yandex.ru>
  6. #
  7. # Copying and distribution of this file, with or without modification, are
  8. # permitted in any medium without royalty provided the copyright notice
  9. # and this notice are preserved. This file is offered as-is, without any
  10. # warranty.
  11. wget -nv https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv -O http-status-codes-1.csv || exit
  12. echo Generating...
  13. echo '/**
  14. * @defgroup httpcode HTTP response codes.
  15. * These are the status codes defined for HTTP responses.
  16. * See: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  17. * Registry export date: '"$(date -u +%Y-%m-%d)"'
  18. * @{
  19. */
  20. ' > header_insert_statuses.h && \
  21. gawk -e 'BEGIN {FPAT = "([^,]*)|(\"[^\"]+\")"}
  22. FNR > 1 {
  23. gsub(/^\[|^"\[|\]"$|\]$/, "", $3)
  24. gsub(/\]\[/, "; ", $3)
  25. if (sub(/ *\(OBSOLETED\)/, "", $2)) {
  26. $3 = "(OBSOLETED) " $3
  27. }
  28. if ($1 == 306) {
  29. $2 = "Switch Proxy"
  30. $3 = "Not used! " $3
  31. }
  32. if ($2 != "Unassigned" && $2 != "(Unused)") {
  33. printf ("/* %s %-22s %s. */\n", $1, "\"" $2 "\".", $3)
  34. printf ("#define MHD_HTTP_%-27s %s\n", toupper(gensub(/[^A-Za-z0-0]/, "_", "g", $2)), $1)
  35. } else {
  36. print ""
  37. }
  38. }' http-status-codes-1.csv >> header_insert_statuses.h && \
  39. echo '
  40. /* Not registered non-standard codes */
  41. /* 449 "Reply With". MS IIS extension. */
  42. #define MHD_HTTP_RETRY_WITH 449
  43. /* 450 "Blocked by Windows Parental Controls". MS extension. */
  44. #define MHD_HTTP_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS 450
  45. /* 509 "Bandwidth Limit Exceeded". Apache extension. */
  46. #define MHD_HTTP_BANDWIDTH_LIMIT_EXCEEDED 509
  47. ' >> header_insert_statuses.h && \
  48. gawk -e 'BEGIN {
  49. FPAT = "([^,]*)|(\"[^\"]+\")"
  50. hundreds[1]="one"
  51. hundreds[2]="two"
  52. hundreds[3]="three"
  53. hundreds[4]="four"
  54. hundreds[5]="five"
  55. hundreds[6]="six"
  56. prev_num=0
  57. prev_reason=""
  58. prev_desc=""
  59. num=0
  60. reason=""
  61. desc=""
  62. }
  63. FNR > 1 {
  64. gsub(/^\[|^"\[|\]"$|\]$/, "", $3)
  65. gsub(/\]\[/, "; ", $3)
  66. num = $1
  67. reason = $2
  68. desc = $3
  69. if (sub(/ *\(OBSOLETED\)/, "", reason)) {
  70. desc = "(OBSOLETED) " desc
  71. }
  72. if (num % 100 == 0) {
  73. if (num != 100) {
  74. printf (" /* %s */ %-36s /* %s */\n};\n\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\")", prev_desc)
  75. }
  76. prev_num = num;
  77. print "static const struct _MHD_cstr_w_len " hundreds[$1/100] "_hundred[] = {"
  78. }
  79. if (num == 306) {
  80. reason = "Switch Proxy"
  81. desc = "Not used! " desc
  82. }
  83. if (reason == "Unassigned" || reason == "(Unused)") next
  84. if (prev_num != num)
  85. printf (" /* %s */ %-36s /* %s */\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\"),", prev_desc)
  86. while(++prev_num < num) {
  87. if (prev_num == 449) {prev_reason="Reply With"; prev_desc="MS IIS extension";}
  88. else if (prev_num == 450) {prev_reason="Blocked by Windows Parental Controls"; prev_desc="MS extension";}
  89. else if (prev_num == 509) {prev_reason="Bandwidth Limit Exceeded"; prev_desc="Apache extension";}
  90. else {prev_reason="Unknown"; prev_desc="Not used";}
  91. if (prev_reason=="Unknown") printf (" /* %s */ %-36s /* %s */\n", prev_num, "{\""prev_reason"\", 0},", prev_desc)
  92. else printf (" /* %s */ %-36s /* %s */\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\"),", prev_desc)
  93. }
  94. prev_num = num
  95. prev_reason = reason
  96. prev_desc = desc
  97. }
  98. END {
  99. printf (" /* %s */ %-36s /* %s */\n};\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\")", prev_desc)
  100. }' http-status-codes-1.csv > code_insert_statuses.c && \
  101. echo OK && \
  102. rm http-status-codes-1.csv || exit