ls-sfparser.h revision fbc6cc04
1fbc6cc04SDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2fbc6cc04SDmitri Tikhonov/*
3fbc6cc04SDmitri TikhonovMIT License
4fbc6cc04SDmitri Tikhonov
5fbc6cc04SDmitri TikhonovCopyright (c) 2020 LiteSpeed Technologies Inc
6fbc6cc04SDmitri Tikhonov
7fbc6cc04SDmitri TikhonovPermission is hereby granted, free of charge, to any person obtaining a copy
8fbc6cc04SDmitri Tikhonovof this software and associated documentation files (the "Software"), to deal
9fbc6cc04SDmitri Tikhonovin the Software without restriction, including without limitation the rights
10fbc6cc04SDmitri Tikhonovto use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11fbc6cc04SDmitri Tikhonovcopies of the Software, and to permit persons to whom the Software is
12fbc6cc04SDmitri Tikhonovfurnished to do so, subject to the following conditions:
13fbc6cc04SDmitri Tikhonov
14fbc6cc04SDmitri TikhonovThe above copyright notice and this permission notice shall be included in all
15fbc6cc04SDmitri Tikhonovcopies or substantial portions of the Software.
16fbc6cc04SDmitri Tikhonov
17fbc6cc04SDmitri TikhonovTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18fbc6cc04SDmitri TikhonovIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19fbc6cc04SDmitri TikhonovFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20fbc6cc04SDmitri TikhonovAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21fbc6cc04SDmitri TikhonovLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22fbc6cc04SDmitri TikhonovOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23fbc6cc04SDmitri TikhonovSOFTWARE.
24fbc6cc04SDmitri Tikhonov*/
25fbc6cc04SDmitri Tikhonov
26fbc6cc04SDmitri Tikhonov/* The LiteSpeed Structured Fields Parser parses structured fields decribed in
27fbc6cc04SDmitri Tikhonov * https://tools.ietf.org/html/draft-ietf-httpbis-header-structure-19
28fbc6cc04SDmitri Tikhonov *
29fbc6cc04SDmitri Tikhonov * It provides a simple streaming interface which allows the user to process
30fbc6cc04SDmitri Tikhonov * structured fields in any manner.
31fbc6cc04SDmitri Tikhonov */
32fbc6cc04SDmitri Tikhonov
33fbc6cc04SDmitri Tikhonov#ifndef LS_SFPARSER_H
34fbc6cc04SDmitri Tikhonov#define LS_SFPARSER_H 1
35fbc6cc04SDmitri Tikhonov
36fbc6cc04SDmitri Tikhonovenum ls_sf_dt
37fbc6cc04SDmitri Tikhonov{   /* LS SF DT: LiteSpeed Structured Field Data Type */
38fbc6cc04SDmitri Tikhonov    LS_SF_DT_INTEGER,
39fbc6cc04SDmitri Tikhonov    LS_SF_DT_DECIMAL,
40fbc6cc04SDmitri Tikhonov
41fbc6cc04SDmitri Tikhonov    /* Name only applies to dictionary names.  They may repeat: the
42fbc6cc04SDmitri Tikhonov     * parser does not drop duplicates.
43fbc6cc04SDmitri Tikhonov     */
44fbc6cc04SDmitri Tikhonov    LS_SF_DT_NAME,
45fbc6cc04SDmitri Tikhonov
46fbc6cc04SDmitri Tikhonov    /* Parameter name may apply to any applicable preceding Item or
47fbc6cc04SDmitri Tikhonov     * Inner List.
48fbc6cc04SDmitri Tikhonov     */
49fbc6cc04SDmitri Tikhonov    LS_SF_DT_PARAM_NAME,
50fbc6cc04SDmitri Tikhonov
51fbc6cc04SDmitri Tikhonov    /* The returned string does not include enclosing double quotes. */
52fbc6cc04SDmitri Tikhonov    LS_SF_DT_STRING,
53fbc6cc04SDmitri Tikhonov
54fbc6cc04SDmitri Tikhonov    LS_SF_DT_TOKEN,
55fbc6cc04SDmitri Tikhonov
56fbc6cc04SDmitri Tikhonov    /* The byte sequence is not base64-decoded; it is up to the caller
57fbc6cc04SDmitri Tikhonov     * to do so.  The returned string does not include the enclosing
58fbc6cc04SDmitri Tikhonov     * colons.
59fbc6cc04SDmitri Tikhonov     */
60fbc6cc04SDmitri Tikhonov    LS_SF_DT_BYTESEQ,
61fbc6cc04SDmitri Tikhonov
62fbc6cc04SDmitri Tikhonov    /* Note that true boolean values are serialized *without* the values.
63fbc6cc04SDmitri Tikhonov     * The parser makes one up and passes a pointer to its internal buffer.
64fbc6cc04SDmitri Tikhonov     */
65fbc6cc04SDmitri Tikhonov    LS_SF_DT_BOOLEAN,
66fbc6cc04SDmitri Tikhonov
67fbc6cc04SDmitri Tikhonov    /* The Inner List has a beginning and an end.  The returned strings
68fbc6cc04SDmitri Tikhonov     * are opening and closing parentheses.
69fbc6cc04SDmitri Tikhonov     */
70fbc6cc04SDmitri Tikhonov    LS_SF_DT_INNER_LIST_BEGIN,
71fbc6cc04SDmitri Tikhonov    LS_SF_DT_INNER_LIST_END,
72fbc6cc04SDmitri Tikhonov};
73fbc6cc04SDmitri Tikhonov
74fbc6cc04SDmitri Tikhonov
75fbc6cc04SDmitri Tikhonovenum ls_sf_tlt
76fbc6cc04SDmitri Tikhonov{   /* LS SF TLT: LiteSpeed Structured Field Top-Level Type */
77fbc6cc04SDmitri Tikhonov    LS_SF_TLT_DICTIONARY,
78fbc6cc04SDmitri Tikhonov    LS_SF_TLT_LIST,
79fbc6cc04SDmitri Tikhonov    LS_SF_TLT_ITEM,
80fbc6cc04SDmitri Tikhonov};
81fbc6cc04SDmitri Tikhonov
82fbc6cc04SDmitri Tikhonov
83fbc6cc04SDmitri Tikhonov/* Return 0 if parsed correctly, -1 on error, -2 if ran out of memory. */
84fbc6cc04SDmitri Tikhonovint
85fbc6cc04SDmitri Tikhonovls_sf_parse (
86fbc6cc04SDmitri Tikhonov    /* Expected type of top-level input.  This tells the parser how to
87fbc6cc04SDmitri Tikhonov     * parse the input.
88fbc6cc04SDmitri Tikhonov     */
89fbc6cc04SDmitri Tikhonov    enum ls_sf_tlt,
90fbc6cc04SDmitri Tikhonov
91fbc6cc04SDmitri Tikhonov    /* Input; does not have to be NUL-terminated: */
92fbc6cc04SDmitri Tikhonov    const char *input, size_t input_sz,
93fbc6cc04SDmitri Tikhonov
94fbc6cc04SDmitri Tikhonov    /* Callback function to call each time a token is parsed.  A non-zero
95fbc6cc04SDmitri Tikhonov     * return value indicates that parsing should stop.
96fbc6cc04SDmitri Tikhonov     */
97fbc6cc04SDmitri Tikhonov    int (*callback)(
98fbc6cc04SDmitri Tikhonov        /* The first argument to the callback is user-specified additional
99fbc6cc04SDmitri Tikhonov         * data.
100fbc6cc04SDmitri Tikhonov         */
101fbc6cc04SDmitri Tikhonov        void *user_data,
102fbc6cc04SDmitri Tikhonov        /* The second argument is the data type. */
103fbc6cc04SDmitri Tikhonov        enum ls_sf_dt,
104fbc6cc04SDmitri Tikhonov        /* The third and fourth arguments are NUL-terminated string and
105fbc6cc04SDmitri Tikhonov         * its length, respectively.  The string can be modified, because
106fbc6cc04SDmitri Tikhonov         * the parser makes a copy.
107fbc6cc04SDmitri Tikhonov         */
108fbc6cc04SDmitri Tikhonov        char *str, size_t len,
109fbc6cc04SDmitri Tikhonov        /* Offset to the token in the input buffer.  In the special case
110fbc6cc04SDmitri Tikhonov         * of an implicit boolean value, this value is negative: this is
111fbc6cc04SDmitri Tikhonov         * because this value is not present in the input buffer.
112fbc6cc04SDmitri Tikhonov         */
113fbc6cc04SDmitri Tikhonov        int off),
114fbc6cc04SDmitri Tikhonov
115fbc6cc04SDmitri Tikhonov    /* Additional data to pass to the callback: */
116fbc6cc04SDmitri Tikhonov    void *user_data,
117fbc6cc04SDmitri Tikhonov
118fbc6cc04SDmitri Tikhonov    /* Allocate memory from this memory buffer.  If set to NULL, regular
119fbc6cc04SDmitri Tikhonov     * system memory allocator will be used.
120fbc6cc04SDmitri Tikhonov     */
121fbc6cc04SDmitri Tikhonov    char *mem_buf, size_t mem_buf_sz);
122fbc6cc04SDmitri Tikhonov
123fbc6cc04SDmitri Tikhonov
124fbc6cc04SDmitri Tikhonov
125fbc6cc04SDmitri Tikhonov/* Convenience array with type names. */
126fbc6cc04SDmitri Tikhonovextern const char *const ls_sf_dt2str[];
127fbc6cc04SDmitri Tikhonov
128fbc6cc04SDmitri Tikhonov
129fbc6cc04SDmitri Tikhonov#endif
130