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