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
7This script extracts HTTP status codes from mozilla.org
8and formats them to fit inside of a C enum along with
9comments.
10
11The output may appear somewhat redundant but it strives
12to
13a) be human readable
14b) eliminate the need to look up status manually,
15c) be machine parseable for table generation
16
17The output is sorted for convenience.
18
19Usage:
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
28from lxml import html
29import requests
30import re
31
32page = requests.get('https://developer.mozilla.org/en-US/docs/Web/HTTP/Status')
33tree = html.fromstring(page.content)
34
35codes = tree.xpath('//code/text()')
36
37codes2 = {}
38for 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
44keys = sorted(codes2.keys())
45for 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}')
54