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