gen_http_statuses_inserts.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. #
  3. # Generate code and header inserts for HTTP statues
  4. #
  5. # Copyright (c) 2019 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") {
  30. print "/* " $1 sprintf("%-24s", " \"" $2 "\". ") $3 ". */"
  31. print "#define MHD_HTTP_" 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. }
  55. FNR > 1 {
  56. gsub(/^\[|^"\[|\]"$|\]$/, "", $3)
  57. gsub(/\]\[/, "; ", $3)
  58. if ($1 % 100 == 0) {
  59. if ($1 != 100) { printf("\n};\n\n") }
  60. prev_num=$1 - 1;
  61. print "static const char *const " hundreds[$1/100] "_hundred[] = {"
  62. }
  63. if ($1 == 306) {
  64. $2 = "Switch Proxy"
  65. $3 = "Not used! " $3
  66. }
  67. if ($2 == "Unassigned") next
  68. while(++prev_num != $1) {
  69. if (prev_num == 449) {reason="Reply With"; desc="MS IIS extension";}
  70. else if (prev_num == 450) {reason="Blocked by Windows Parental Controls"; desc="MS extension";}
  71. else if (prev_num == 509) {reason="Bandwidth Limit Exceeded"; desc="Apache extension";}
  72. else {reason="Unknown"; desc="Not used";}
  73. printf (",\n /* %s */ %-24s /* %s */", prev_num, "\"" reason "\"", desc)
  74. }
  75. if ($1 % 100 != 0) { print "," }
  76. printf (" /* %s */ %-24s /* %s */", $1, "\""$2"\"", $3)
  77. }
  78. END {printf("\n};\n")}' http-status-codes-1.csv >> code_insert_statuses.c && \
  79. echo OK && \
  80. rm http-status-codes-1.csv || exit