aboutsummaryrefslogtreecommitdiff
path: root/contrib/gen_http_statuses_inserts.sh
blob: 6c8e6454792f1c1a32be3a096f76439eb0cbb3fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash

#
#   Generate code and header inserts for HTTP statues
#

#   Copyright (c) 2019 Karlson2k (Evgeny Grin) <k2k@yandex.ru>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved. This file is offered as-is, without any
#   warranty.

wget -nv https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv -O http-status-codes-1.csv || exit
echo Generating...
echo "/**
 * @defgroup httpcode HTTP response codes.
 * These are the status codes defined for HTTP responses.
 * See: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
 * Registry export date: $(date -u +%Y-%m-%d)
 * @{
 */
" > header_insert_statuses.h && \
gawk -e 'BEGIN {FPAT = "([^,]*)|(\"[^\"]+\")"}
FNR > 1 {
  gsub(/^\[|^"\[|\]"$|\]$/, "", $3)
  gsub(/\]\[/, "; ", $3)
  if ($1 == 306) { 
    $2 = "Switch Proxy" 
    $3 = "Not used! " $3
  }
  if ($2 != "Unassigned") {
    print "/* " $1 sprintf("%-24s", " \"" $2 "\". ") $3 ". */"
    print "#define MHD_HTTP_" toupper(gensub(/[^A-Za-z0-0]/, "_", "g", $2)) " "$1""
  } else {
    print ""
  }
}' http-status-codes-1.csv >> header_insert_statuses.h && \
echo '
/* Not registered non-standard codes */
/* 449 "Reply With".          MS IIS extension. */
#define MHD_HTTP_RETRY_WITH 449

/* 450 "Blocked by Windows Parental Controls". MS extension. */
#define MHD_HTTP_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS 450

/* 509 "Bandwidth Limit Exceeded". Apache extension. */
#define MHD_HTTP_BANDWIDTH_LIMIT_EXCEEDED 509
' >> header_insert_statuses.h && \
gawk -e 'BEGIN {
  FPAT = "([^,]*)|(\"[^\"]+\")"
  hundreds[1]="one"
  hundreds[2]="two"
  hundreds[3]="three"
  hundreds[4]="four"
  hundreds[5]="five"
  hundreds[6]="six"
  prev_num=0
  prev_reason=""
  prev_desc=""
  num=0
  reason=""
  desc=""
}
FNR > 1 {
  gsub(/^\[|^"\[|\]"$|\]$/, "", $3)
  gsub(/\]\[/, "; ", $3)
  num = $1
  reason = $2
  desc = $3
  if (num % 100 == 0) {
    if (num != 100) {
      printf ("  /* %s */ %-24s /* %s */\n};\n\n", prev_num, "\""prev_reason"\"", prev_desc)
    }
    prev_num = num;
    print "static const char *const " hundreds[$1/100] "_hundred[] = {"
  }
  if (num == 306) { 
    reason = "Switch Proxy" 
    desc = "Not used! " desc
  }
  if (reason == "Unassigned") next
  if (prev_num != num)
    printf ("  /* %s */ %-24s /* %s */\n", prev_num, "\""prev_reason"\",", prev_desc)
  while(++prev_num < num) {
    if (prev_num == 449) {prev_reason="Reply With"; prev_desc="MS IIS extension";}
    else if (prev_num == 450) {prev_reason="Blocked by Windows Parental Controls"; prev_desc="MS extension";}
    else if (prev_num == 509) {prev_reason="Bandwidth Limit Exceeded"; prev_desc="Apache extension";}
    else {prev_reason="Unknown"; prev_desc="Not used";}
    printf ("  /* %s */ %-24s /* %s */\n", prev_num, "\""prev_reason"\",", prev_desc)
  }
  prev_num = num
  prev_reason = reason
  prev_desc = desc
}
END {
  printf ("  /* %s */ %-24s /* %s */\n};\n", prev_num, "\""prev_reason"\"", prev_desc)
}' http-status-codes-1.csv > code_insert_statuses.c && \
echo OK && \
rm http-status-codes-1.csv || exit