gen_http_statuses_inserts.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 ($1 == 306) {
  26. $2 = "Switch Proxy"
  27. $3 = "Not used! " $3
  28. }
  29. if ($2 != "Unassigned" && $2 != "(Unused)") {
  30. printf ("/* %s %-22s %s. */\n", $1, "\"" $2 "\".", $3)
  31. printf ("#define MHD_HTTP_%-27s %s\n", toupper(gensub(/[^A-Za-z0-0]/, "_", "g", $2)), $1)
  32. } else {
  33. print ""
  34. }
  35. }' http-status-codes-1.csv >> header_insert_statuses.h && \
  36. echo '
  37. /* Not registered non-standard codes */
  38. /* 449 "Reply With". MS IIS extension. */
  39. #define MHD_HTTP_RETRY_WITH 449
  40. /* 450 "Blocked by Windows Parental Controls". MS extension. */
  41. #define MHD_HTTP_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS 450
  42. /* 509 "Bandwidth Limit Exceeded". Apache extension. */
  43. #define MHD_HTTP_BANDWIDTH_LIMIT_EXCEEDED 509
  44. ' >> header_insert_statuses.h && \
  45. gawk -e 'BEGIN {
  46. FPAT = "([^,]*)|(\"[^\"]+\")"
  47. hundreds[1]="one"
  48. hundreds[2]="two"
  49. hundreds[3]="three"
  50. hundreds[4]="four"
  51. hundreds[5]="five"
  52. hundreds[6]="six"
  53. prev_num=0
  54. prev_reason=""
  55. prev_desc=""
  56. num=0
  57. reason=""
  58. desc=""
  59. }
  60. FNR > 1 {
  61. gsub(/^\[|^"\[|\]"$|\]$/, "", $3)
  62. gsub(/\]\[/, "; ", $3)
  63. num = $1
  64. reason = $2
  65. desc = $3
  66. if (num % 100 == 0) {
  67. if (num != 100) {
  68. printf (" /* %s */ %-36s /* %s */\n};\n\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\")", prev_desc)
  69. }
  70. prev_num = num;
  71. print "static const struct _MHD_str_w_len " hundreds[$1/100] "_hundred[] = {"
  72. }
  73. if (num == 306) {
  74. reason = "Switch Proxy"
  75. desc = "Not used! " desc
  76. }
  77. if (reason == "Unassigned" || reason == "(Unused)") next
  78. if (prev_num != num)
  79. printf (" /* %s */ %-36s /* %s */\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\"),", prev_desc)
  80. while(++prev_num < num) {
  81. if (prev_num == 449) {prev_reason="Reply With"; prev_desc="MS IIS extension";}
  82. else if (prev_num == 450) {prev_reason="Blocked by Windows Parental Controls"; prev_desc="MS extension";}
  83. else if (prev_num == 509) {prev_reason="Bandwidth Limit Exceeded"; prev_desc="Apache extension";}
  84. else {prev_reason="Unknown"; prev_desc="Not used";}
  85. if (prev_reason=="Unknown") printf (" /* %s */ %-36s /* %s */\n", prev_num, "{\""prev_reason"\", 0},", prev_desc)
  86. else printf (" /* %s */ %-36s /* %s */\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\"),", prev_desc)
  87. }
  88. prev_num = num
  89. prev_reason = reason
  90. prev_desc = desc
  91. }
  92. END {
  93. printf (" /* %s */ %-36s /* %s */\n};\n", prev_num, "_MHD_S_STR_W_LEN (\""prev_reason"\")", prev_desc)
  94. }' http-status-codes-1.csv > code_insert_statuses.c && \
  95. echo OK && \
  96. rm http-status-codes-1.csv || exit