1from libc.stdint cimport uint16_t, uint32_t, uint64_t
2
3
4cdef extern from "../vendor/http-parser/http_parser.h":
5    ctypedef int (*http_data_cb) (http_parser*,
6                                  const char *at,
7                                  size_t length) except -1
8
9    ctypedef int (*http_cb) (http_parser*) except -1
10
11    struct http_parser:
12        unsigned int type
13        unsigned int flags
14        unsigned int state
15        unsigned int header_state
16        unsigned int index
17
18        uint32_t nread
19        uint64_t content_length
20
21        unsigned short http_major
22        unsigned short http_minor
23        unsigned int status_code
24        unsigned int method
25        unsigned int http_errno
26
27        unsigned int upgrade
28
29        void *data
30
31    struct http_parser_settings:
32        http_cb      on_message_begin
33        http_data_cb on_url
34        http_data_cb on_status
35        http_data_cb on_header_field
36        http_data_cb on_header_value
37        http_cb      on_headers_complete
38        http_data_cb on_body
39        http_cb      on_message_complete
40        http_cb      on_chunk_header
41        http_cb      on_chunk_complete
42
43    enum http_parser_type:
44        HTTP_REQUEST,
45        HTTP_RESPONSE,
46        HTTP_BOTH
47
48    enum http_errno:
49        HPE_OK,
50        HPE_CB_message_begin,
51        HPE_CB_url,
52        HPE_CB_header_field,
53        HPE_CB_header_value,
54        HPE_CB_headers_complete,
55        HPE_CB_body,
56        HPE_CB_message_complete,
57        HPE_CB_status,
58        HPE_CB_chunk_header,
59        HPE_CB_chunk_complete,
60        HPE_INVALID_EOF_STATE,
61        HPE_HEADER_OVERFLOW,
62        HPE_CLOSED_CONNECTION,
63        HPE_INVALID_VERSION,
64        HPE_INVALID_STATUS,
65        HPE_INVALID_METHOD,
66        HPE_INVALID_URL,
67        HPE_INVALID_HOST,
68        HPE_INVALID_PORT,
69        HPE_INVALID_PATH,
70        HPE_INVALID_QUERY_STRING,
71        HPE_INVALID_FRAGMENT,
72        HPE_LF_EXPECTED,
73        HPE_INVALID_HEADER_TOKEN,
74        HPE_INVALID_CONTENT_LENGTH,
75        HPE_INVALID_CHUNK_SIZE,
76        HPE_INVALID_CONSTANT,
77        HPE_INVALID_INTERNAL_STATE,
78        HPE_STRICT,
79        HPE_PAUSED,
80        HPE_UNKNOWN
81
82    enum flags:
83        F_CHUNKED,
84        F_CONNECTION_KEEP_ALIVE,
85        F_CONNECTION_CLOSE,
86        F_CONNECTION_UPGRADE,
87        F_TRAILING,
88        F_UPGRADE,
89        F_SKIPBODY,
90        F_CONTENTLENGTH
91
92    enum http_method:
93        DELETE, GET, HEAD, POST, PUT, CONNECT, OPTIONS, TRACE, COPY,
94        LOCK, MKCOL, MOVE, PROPFIND, PROPPATCH, SEARCH, UNLOCK, BIND,
95        REBIND, UNBIND, ACL, REPORT, MKACTIVITY, CHECKOUT, MERGE,
96        MSEARCH, NOTIFY, SUBSCRIBE, UNSUBSCRIBE, PATCH, PURGE, MKCALENDAR,
97        LINK, UNLINK
98
99    void http_parser_init(http_parser *parser, http_parser_type type)
100
101    size_t http_parser_execute(http_parser *parser,
102                               const http_parser_settings *settings,
103                               const char *data,
104                               size_t len)
105
106    int http_should_keep_alive(const http_parser *parser)
107
108    void http_parser_settings_init(http_parser_settings *settings)
109
110    const char *http_errno_name(http_errno err)
111    const char *http_errno_description(http_errno err)
112    const char *http_method_str(http_method m)
113
114    # URL Parser
115
116    enum http_parser_url_fields:
117        UF_SCHEMA   = 0,
118        UF_HOST     = 1,
119        UF_PORT     = 2,
120        UF_PATH     = 3,
121        UF_QUERY    = 4,
122        UF_FRAGMENT = 5,
123        UF_USERINFO = 6,
124        UF_MAX      = 7
125
126    struct http_parser_url_field_data:
127        uint16_t off
128        uint16_t len
129
130    struct http_parser_url:
131        uint16_t field_set
132        uint16_t port
133        http_parser_url_field_data[<int>UF_MAX] field_data
134
135    void http_parser_url_init(http_parser_url *u)
136
137    int http_parser_parse_url(const char *buf,
138                              size_t buflen,
139                              int is_connect,
140                              http_parser_url *u)
141