gen_http_methods_insert.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. #
  3. # Generate header insert for HTTP methods
  4. #
  5. # Copyright (c) 2015-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 http://www.iana.org/assignments/http-methods/methods.csv -O methods.csv || exit
  12. echo Generating...
  13. echo '/**
  14. * @defgroup methods HTTP methods
  15. * HTTP methods (as strings).
  16. * See: https://www.iana.org/assignments/http-methods/http-methods.xml
  17. * Registry export date: '"$(date -u +%Y-%m-%d)"'
  18. * @{
  19. */
  20. /* Main HTTP methods. */' > header_insert_methods.h && \
  21. gawk -e 'BEGIN {FPAT = "([^,]*)|(\"[^\"]+\")"}
  22. function strnumcmp(s1, s2, n1, n2, a1, a2)
  23. {
  24. n1 = length(s1)
  25. n2 = length(s2)
  26. if (0 == n1 && 0 == n2)
  27. return 0
  28. if (0 == n1 && 0 < n2)
  29. return -1
  30. if (0 < n1 && 0 == n2)
  31. return 1
  32. n1 = match(s1, /^[^0-9]+/, a1)
  33. n2 = match(s2, /^[^0-9]+/, a2)
  34. if (0 != n1)
  35. {
  36. if (0 == n2)
  37. return 1
  38. if ((a1[0] "") < (a2[0] ""))
  39. return -1
  40. if ((a1[0] "") > (a2[0] ""))
  41. return 1
  42. }
  43. else
  44. {
  45. if (0 != n2)
  46. return -1
  47. }
  48. s1 = substr(s1, length(a1[0]) + 1)
  49. s2 = substr(s2, length(a2[0]) + 1)
  50. n1 = match(s1, /^[0-9]+/, a1)
  51. n2 = match(s2, /^[0-9]+/, a2)
  52. if (0 != n1)
  53. {
  54. if (0 == n2)
  55. return 1
  56. if ((a1[0] + 0) < (a2[0] + 0))
  57. return -1
  58. if ((a1[0] + 0) > (a2[0] + 0))
  59. return 1
  60. }
  61. else
  62. {
  63. if (0 != n2)
  64. return -1
  65. }
  66. return strnumcmp(substr(s1, length(a1[0]) + 1), substr(s2, length(a2[0]) + 1))
  67. }
  68. function sort_indices(i1, v1, i2, v2)
  69. {
  70. return strnumcmp(gensub(/[^0-9A-Za-z]+/, " ", "g", i1), gensub(/[^0-9A-Za-z]+/, " ", "g", i2))
  71. }
  72. FNR > 1 {
  73. mthd_m = $1
  74. reference_m = $4
  75. gsub(/^\[|^"\[|\]"$|\]$/, "", reference_m)
  76. gsub(/\]\[/, "; ", reference_m)
  77. if (reference_m ~ /^RFC911[0-2]/ && mthd_m != "*") {
  78. if ($2 == "yes")
  79. { safe_m = "Safe. " }
  80. else
  81. { safe_m = "Not safe." }
  82. if ($3 == "yes")
  83. { indem_m = "Idempotent. " }
  84. else
  85. { indem_m = "Not idempotent." }
  86. idx_str = reference_m
  87. main_methods[idx_str] = sprintf ("%s\n", "/* " safe_m " " indem_m " " reference_m ". */")
  88. mthd_tkn = gensub(/\*/, "ASTERISK", "g", mthd_m)
  89. gsub(/[^a-zA-Z0-9_]/, "_", mthd_tkn)
  90. main_methods[idx_str] = (main_methods[idx_str] sprintf ("%-32s \"%s\"\n", "#define MHD_HTTP_METHOD_" toupper(mthd_tkn), mthd_m))
  91. }
  92. }
  93. END {
  94. n = asort(main_methods, main_methods, "sort_indices")
  95. for (i = 1; i <= n; i++)
  96. printf("%s", main_methods[i])
  97. }
  98. ' methods.csv >> header_insert_methods.h && \
  99. echo '
  100. /* Additional HTTP methods. */' >> header_insert_methods.h && \
  101. gawk -e 'BEGIN {FPAT = "([^,]*)|(\"[^\"]+\")"}
  102. FNR > 1 {
  103. gsub(/^\[|^"\[|\]"$|\]$/, "", $4)
  104. gsub(/\]\[/, "; ", $4)
  105. if ($4 !~ /^RFC911[0-2]/ || $1 == "*") {
  106. if ($2 == "yes")
  107. { safe_m = "Safe. " }
  108. else
  109. { safe_m = "Not safe." }
  110. if ($3 == "yes")
  111. { indem_m = "Idempotent. " }
  112. else
  113. { indem_m = "Not idempotent." }
  114. print "/* " safe_m " " indem_m " " $4 ". */"
  115. mthd = gensub(/\*/, "ASTERISK", "g", $1)
  116. mthd = gensub(/[^a-zA-Z0-9_]/, "_", "g", mthd)
  117. printf ("%-38s \"%s\"\n", "#define MHD_HTTP_METHOD_" toupper(mthd), $1)
  118. }
  119. }' methods.csv >> header_insert_methods.h && \
  120. echo '
  121. /** @} */ /* end of group methods */
  122. ' >> header_insert_methods.h &&
  123. echo OK && \
  124. rm methods.csv || exit