Christopher Friedt | ef142c1 | 2022-10-10 13:03:24 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright(c) 2022 Meta |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | """Format HTTP Status codes for use in a C header |
| 6 | |
| 7 | This script extracts HTTP status codes from mozilla.org |
| 8 | and formats them to fit inside of a C enum along with |
| 9 | comments. |
| 10 | |
| 11 | The output may appear somewhat redundant but it strives |
| 12 | to |
| 13 | a) be human readable |
| 14 | b) eliminate the need to look up status manually, |
| 15 | c) be machine parseable for table generation |
| 16 | |
| 17 | The output is sorted for convenience. |
| 18 | |
| 19 | Usage: |
| 20 | ./scripts/net/enumerate_http_status.py |
| 21 | HTTP_100_CONTINUE = 100, /**< Continue */ |
| 22 | ... |
| 23 | HTTP_418_IM_A_TEAPOT = 418, /**< I'm a teapot */ |
| 24 | ... |
| 25 | HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511, /**< Network Authentication Required */ |
| 26 | """ |
| 27 | |
| 28 | from lxml import html |
| 29 | import requests |
| 30 | import re |
| 31 | |
| 32 | page = requests.get('https://developer.mozilla.org/en-US/docs/Web/HTTP/Status') |
| 33 | tree = html.fromstring(page.content) |
| 34 | |
| 35 | codes = tree.xpath('//code/text()') |
| 36 | |
| 37 | codes2 = {} |
| 38 | for c in codes: |
| 39 | if re.match('[0-9][0-9][0-9] [a-zA-Z].*', c): |
| 40 | key = int(c[0:3]) |
| 41 | val = c[4:] |
| 42 | codes2[key] = val |
| 43 | |
| 44 | keys = sorted(codes2.keys()) |
| 45 | for key in keys: |
| 46 | val = codes2[key] |
| 47 | enum_head = 'HTTP' |
| 48 | enum_body = f'{key}' |
| 49 | enum_tail = val.upper().replace(' ', '_').replace("'", '').replace('-', '_') |
| 50 | enum_label = '_'.join([enum_head, enum_body, enum_tail]) |
| 51 | comment = f'/**< {val} */' |
| 52 | |
| 53 | print(f'{enum_label} = {key}, {comment}') |