libwebsockets
Lightweight C library for HTML5 websockets
Toggle main menu visibility
Loading...
Searching...
No Matches
lws-tokenize.h
Go to the documentation of this file.
1
/*
2
* libwebsockets - small server side websockets and web server implementation
3
*
4
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to
8
* deal in the Software without restriction, including without limitation the
9
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
* sell copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
* IN THE SOFTWARE.
23
*/
24
25
/* Do not treat - as a terminal character, so "my-token" is one token */
26
#define LWS_TOKENIZE_F_MINUS_NONTERM (1 << 0)
27
/* Separately report aggregate colon-delimited tokens */
28
#define LWS_TOKENIZE_F_AGG_COLON (1 << 1)
29
/* Enforce sequencing for a simple token , token , token ... list */
30
#define LWS_TOKENIZE_F_COMMA_SEP_LIST (1 << 2)
31
/* Allow more characters in the tokens and less delimiters... default is
32
* only alphanumeric + underscore in tokens */
33
#define LWS_TOKENIZE_F_RFC7230_DELIMS (1 << 3)
34
/* Do not treat . as a terminal character, so "warmcat.com" is one token */
35
#define LWS_TOKENIZE_F_DOT_NONTERM (1 << 4)
36
/* If something starts looking like a float, like 1.2, force to be string token.
37
* This lets you receive dotted-quads like 192.168.0.1 as string tokens, and
38
* avoids illegal float format detection like 1.myserver.com */
39
#define LWS_TOKENIZE_F_NO_FLOATS (1 << 5)
40
/* Instead of LWS_TOKZE_INTEGER, report integers as any other string token */
41
#define LWS_TOKENIZE_F_NO_INTEGERS (1 << 6)
42
/* # makes the rest of the line a comment */
43
#define LWS_TOKENIZE_F_HASH_COMMENT (1 << 7)
44
/* Do not treat / as a terminal character, so "multipart/related" is one token */
45
#define LWS_TOKENIZE_F_SLASH_NONTERM (1 << 8)
46
/* Do not treat * as a terminal character, so "myfile*" is one token */
47
#define LWS_TOKENIZE_F_ASTERISK_NONTERM (1 << 9)
48
/* Do not treat = as a terminal character, so "x=y" is one token */
49
#define LWS_TOKENIZE_F_EQUALS_NONTERM (1 << 10)
50
/* Do not treat : as a terminal character, so ::1 is one token */
51
#define LWS_TOKENIZE_F_COLON_NONTERM (1 << 11)
52
53
/* We're just tokenizing a chunk, don't treat running out of input as final */
54
#define LWS_TOKENIZE_F_EXPECT_MORE (1 << 12)
55
/* Do not treat + as a terminal character, so "a+b" is one token */
56
#define LWS_TOKENIZE_F_PLUS_NONTERM (1 << 13)
57
/* Emit chunks for tokens that exceed the collection max limit */
58
#define LWS_TOKENIZE_F_CHUNK (1 << 14)
59
60
typedef
enum
{
61
62
LWS_TOKZE_ERRS
= 7,
/* the number of errors defined */
63
64
LWS_TOKZE_TOO_LONG
= -7,
/* token too long */
65
LWS_TOKZE_WANT_READ
= -6,
/* need more input */
66
LWS_TOKZE_ERR_BROKEN_UTF8
= -5,
/* malformed or partial utf8 */
67
LWS_TOKZE_ERR_UNTERM_STRING
= -4,
/* ended while we were in "" */
68
LWS_TOKZE_ERR_MALFORMED_FLOAT
= -3,
/* like 0..1 or 0.1.1 */
69
LWS_TOKZE_ERR_NUM_ON_LHS
= -2,
/* like 123= or 0.1= */
70
LWS_TOKZE_ERR_COMMA_LIST
= -1,
/* like ",tok", or, "tok,," */
71
72
LWS_TOKZE_ENDED
= 0,
/* no more content */
73
74
/* Note: results have ordinal 1+, EOT is 0 and errors are < 0 */
75
76
LWS_TOKZE_DELIMITER
,
/* a delimiter appeared */
77
LWS_TOKZE_TOKEN
,
/* a token appeared */
78
LWS_TOKZE_INTEGER
,
/* an integer appeared */
79
LWS_TOKZE_FLOAT
,
/* a float appeared */
80
LWS_TOKZE_TOKEN_NAME_EQUALS
,
/* token [whitespace] = */
81
LWS_TOKZE_TOKEN_NAME_COLON
,
/* token [whitespace] : (only with
82
LWS_TOKENIZE_F_AGG_COLON flag) */
83
LWS_TOKZE_QUOTED_STRING
,
/* "*", where * may have any char */
84
LWS_TOKZE_TOKEN_CHUNK
,
/* a token chunk appeared */
85
LWS_TOKZE_QUOTED_STRING_CHUNK
,
/* a quoted string chunk appeared */
86
87
}
lws_tokenize_elem
;
88
89
/*
90
* helper enums to allow caller to enforce legal delimiter sequencing, eg
91
* disallow "token,,token", "token,", and ",token"
92
*/
93
94
enum
lws_tokenize_delimiter_tracking
{
95
LWSTZ_DT_NEED_FIRST_CONTENT
,
96
LWSTZ_DT_NEED_DELIM
,
97
LWSTZ_DT_NEED_NEXT_CONTENT
,
98
};
99
100
typedef
enum
{
101
LWS_TOKZS_LEADING_WHITESPACE
,
102
LWS_TOKZS_QUOTED_STRING
,
103
LWS_TOKZS_TOKEN
,
104
LWS_TOKZS_TOKEN_POST_TERMINAL
105
}
lws_tokenize_state
;
106
107
typedef
struct
lws_tokenize
{
108
char
collect
[256];
/* token length limit */
109
const
char
*
start
;
110
const
char
*
token
;
111
size_t
len
;
112
size_t
token_len
;
113
114
lws_tokenize_state
state
;
115
116
int
line
;
117
int
effline
;
118
119
uint16_t
flags
;
120
uint8_t
delim
;
121
122
int8_t
e
;
123
uint8_t
reset_token
:1;
124
uint8_t
crlf
:1;
125
uint8_t
dry
:1;
126
}
lws_tokenize_t
;
127
141
142
LWS_VISIBLE
LWS_EXTERN
void
143
lws_tokenize_init
(
struct
lws_tokenize
*ts,
const
char
*start,
int
flags);
144
163
164
LWS_VISIBLE
LWS_EXTERN
lws_tokenize_elem
165
lws_tokenize
(
struct
lws_tokenize
*ts);
166
176
177
LWS_VISIBLE
LWS_EXTERN
int
178
lws_tokenize_cstr
(
struct
lws_tokenize
*ts,
char
*str,
size_t
max);
179
180
181
/*
182
* lws_strexp: flexible string expansion helper api
183
*
184
* This stateful helper can handle multiple separate input chunks and multiple
185
* output buffer loads with arbitrary boundaries between literals and expanded
186
* symbols. This allows it to handle fragmented input as well as arbitrarily
187
* long symbol expansions that are bigger than the output buffer itself.
188
*
189
* A user callback is used to convert symbol names to the symbol value.
190
*
191
* A single byte buffer for input and another for output can process any
192
* length substitution then. The state object is around 64 bytes on a 64-bit
193
* system and it only uses 8 bytes stack.
194
*/
195
196
197
typedef
int (*
lws_strexp_expand_cb
)(
void
*priv,
const
char
*name,
char
*out,
198
size_t
*pos,
size_t
olen,
size_t
*exp_ofs);
199
200
typedef
struct
lws_strexp
{
201
char
name
[32];
202
lws_strexp_expand_cb
cb
;
203
void
*
priv
;
204
char
*
out
;
205
size_t
olen
;
206
size_t
pos
;
207
208
size_t
exp_ofs
;
209
210
uint8_t
name_pos
;
211
char
state
;
212
}
lws_strexp_t
;
213
214
enum
{
215
LSTRX_DONE
,
/* it completed OK */
216
LSTRX_FILLED_OUT
,
/* out buf filled and needs resetting */
217
LSTRX_FATAL_NAME_TOO_LONG
= -1,
/* fatal */
218
LSTRX_FATAL_NAME_UNKNOWN
= -2,
219
};
220
221
238
LWS_VISIBLE
LWS_EXTERN
void
239
lws_strexp_init
(
lws_strexp_t
*exp,
void
*priv,
lws_strexp_expand_cb
cb,
240
char
*out,
size_t
olen);
241
258
LWS_VISIBLE
LWS_EXTERN
void
259
lws_strexp_reset_out
(
lws_strexp_t
*exp,
char
*out,
size_t
olen);
260
283
LWS_VISIBLE
LWS_EXTERN
int
284
lws_strexp_expand
(
lws_strexp_t
*exp,
const
char
*in,
size_t
len,
285
size_t
*pused_in,
size_t
*pused_out);
286
299
LWS_VISIBLE
LWS_EXTERN
int
300
lws_strcmp_wildcard
(
const
char
*wildcard,
size_t
wlen,
const
char
*check,
301
size_t
clen);
uint16_t
unsigned short uint16_t
Definition
libwebsockets.h:698
LWS_EXTERN
#define LWS_EXTERN
Definition
libwebsockets.h:296
uint8_t
unsigned char uint8_t
Definition
libwebsockets.h:699
LWS_VISIBLE
#define LWS_VISIBLE
Definition
libwebsockets.h:291
lws_tokenize_delimiter_tracking
lws_tokenize_delimiter_tracking
Definition
lws-tokenize.h:94
LWSTZ_DT_NEED_NEXT_CONTENT
@ LWSTZ_DT_NEED_NEXT_CONTENT
Definition
lws-tokenize.h:97
LWSTZ_DT_NEED_DELIM
@ LWSTZ_DT_NEED_DELIM
Definition
lws-tokenize.h:96
LWSTZ_DT_NEED_FIRST_CONTENT
@ LWSTZ_DT_NEED_FIRST_CONTENT
Definition
lws-tokenize.h:95
lws_tokenize::state
lws_tokenize_state state
Definition
lws-tokenize.h:114
lws_strexp_init
LWS_VISIBLE LWS_EXTERN void lws_strexp_init(lws_strexp_t *exp, void *priv, lws_strexp_expand_cb cb, char *out, size_t olen)
lws_tokenize::dry
uint8_t dry
Definition
lws-tokenize.h:125
lws_strexp::name
char name[32]
Definition
lws-tokenize.h:201
lws_tokenize::reset_token
uint8_t reset_token
Definition
lws-tokenize.h:123
lws_tokenize::token
const char * token
Definition
lws-tokenize.h:110
lws_tokenize::len
size_t len
Definition
lws-tokenize.h:111
lws_tokenize_t
struct lws_tokenize lws_tokenize_t
lws_strexp_expand_cb
int(* lws_strexp_expand_cb)(void *priv, const char *name, char *out, size_t *pos, size_t olen, size_t *exp_ofs)
Definition
lws-tokenize.h:197
lws_tokenize::token_len
size_t token_len
Definition
lws-tokenize.h:112
lws_strexp_reset_out
LWS_VISIBLE LWS_EXTERN void lws_strexp_reset_out(lws_strexp_t *exp, char *out, size_t olen)
lws_strexp::cb
lws_strexp_expand_cb cb
Definition
lws-tokenize.h:202
lws_tokenize
LWS_VISIBLE LWS_EXTERN lws_tokenize_elem lws_tokenize(struct lws_tokenize *ts)
lws_strexp::out
char * out
Definition
lws-tokenize.h:204
lws_tokenize::line
int line
Definition
lws-tokenize.h:116
lws_strexp::state
char state
Definition
lws-tokenize.h:211
lws_tokenize::e
int8_t e
Definition
lws-tokenize.h:122
lws_strexp::priv
void * priv
Definition
lws-tokenize.h:203
lws_strexp::pos
size_t pos
Definition
lws-tokenize.h:206
lws_strexp_expand
LWS_VISIBLE LWS_EXTERN int lws_strexp_expand(lws_strexp_t *exp, const char *in, size_t len, size_t *pused_in, size_t *pused_out)
lws_tokenize::crlf
uint8_t crlf
Definition
lws-tokenize.h:124
LSTRX_DONE
@ LSTRX_DONE
Definition
lws-tokenize.h:215
LSTRX_FATAL_NAME_TOO_LONG
@ LSTRX_FATAL_NAME_TOO_LONG
Definition
lws-tokenize.h:217
LSTRX_FATAL_NAME_UNKNOWN
@ LSTRX_FATAL_NAME_UNKNOWN
Definition
lws-tokenize.h:218
LSTRX_FILLED_OUT
@ LSTRX_FILLED_OUT
Definition
lws-tokenize.h:216
lws_tokenize_init
LWS_VISIBLE LWS_EXTERN void lws_tokenize_init(struct lws_tokenize *ts, const char *start, int flags)
lws_tokenize_state
lws_tokenize_state
Definition
lws-tokenize.h:100
LWS_TOKZS_TOKEN
@ LWS_TOKZS_TOKEN
Definition
lws-tokenize.h:103
LWS_TOKZS_TOKEN_POST_TERMINAL
@ LWS_TOKZS_TOKEN_POST_TERMINAL
Definition
lws-tokenize.h:104
LWS_TOKZS_QUOTED_STRING
@ LWS_TOKZS_QUOTED_STRING
Definition
lws-tokenize.h:102
LWS_TOKZS_LEADING_WHITESPACE
@ LWS_TOKZS_LEADING_WHITESPACE
Definition
lws-tokenize.h:101
lws_tokenize_elem
lws_tokenize_elem
Definition
lws-tokenize.h:60
LWS_TOKZE_ERR_NUM_ON_LHS
@ LWS_TOKZE_ERR_NUM_ON_LHS
Definition
lws-tokenize.h:69
LWS_TOKZE_WANT_READ
@ LWS_TOKZE_WANT_READ
Definition
lws-tokenize.h:65
LWS_TOKZE_TOKEN
@ LWS_TOKZE_TOKEN
Definition
lws-tokenize.h:77
LWS_TOKZE_QUOTED_STRING
@ LWS_TOKZE_QUOTED_STRING
Definition
lws-tokenize.h:83
LWS_TOKZE_ERR_MALFORMED_FLOAT
@ LWS_TOKZE_ERR_MALFORMED_FLOAT
Definition
lws-tokenize.h:68
LWS_TOKZE_DELIMITER
@ LWS_TOKZE_DELIMITER
Definition
lws-tokenize.h:76
LWS_TOKZE_QUOTED_STRING_CHUNK
@ LWS_TOKZE_QUOTED_STRING_CHUNK
Definition
lws-tokenize.h:85
LWS_TOKZE_TOKEN_CHUNK
@ LWS_TOKZE_TOKEN_CHUNK
Definition
lws-tokenize.h:84
LWS_TOKZE_TOKEN_NAME_EQUALS
@ LWS_TOKZE_TOKEN_NAME_EQUALS
Definition
lws-tokenize.h:80
LWS_TOKZE_TOO_LONG
@ LWS_TOKZE_TOO_LONG
Definition
lws-tokenize.h:64
LWS_TOKZE_FLOAT
@ LWS_TOKZE_FLOAT
Definition
lws-tokenize.h:79
LWS_TOKZE_ERR_BROKEN_UTF8
@ LWS_TOKZE_ERR_BROKEN_UTF8
Definition
lws-tokenize.h:66
LWS_TOKZE_ERRS
@ LWS_TOKZE_ERRS
Definition
lws-tokenize.h:62
LWS_TOKZE_INTEGER
@ LWS_TOKZE_INTEGER
Definition
lws-tokenize.h:78
LWS_TOKZE_ENDED
@ LWS_TOKZE_ENDED
Definition
lws-tokenize.h:72
LWS_TOKZE_TOKEN_NAME_COLON
@ LWS_TOKZE_TOKEN_NAME_COLON
Definition
lws-tokenize.h:81
LWS_TOKZE_ERR_UNTERM_STRING
@ LWS_TOKZE_ERR_UNTERM_STRING
Definition
lws-tokenize.h:67
LWS_TOKZE_ERR_COMMA_LIST
@ LWS_TOKZE_ERR_COMMA_LIST
Definition
lws-tokenize.h:70
lws_tokenize::flags
uint16_t flags
Definition
lws-tokenize.h:119
lws_tokenize::delim
uint8_t delim
Definition
lws-tokenize.h:120
lws_strexp::name_pos
uint8_t name_pos
Definition
lws-tokenize.h:210
lws_tokenize::collect
char collect[256]
Definition
lws-tokenize.h:108
lws_tokenize::start
const char * start
Definition
lws-tokenize.h:109
lws_tokenize::effline
int effline
Definition
lws-tokenize.h:117
lws_strexp::exp_ofs
size_t exp_ofs
Definition
lws-tokenize.h:208
lws_tokenize_cstr
LWS_VISIBLE LWS_EXTERN int lws_tokenize_cstr(struct lws_tokenize *ts, char *str, size_t max)
lws_strexp_t
struct lws_strexp lws_strexp_t
lws_strcmp_wildcard
LWS_VISIBLE LWS_EXTERN int lws_strcmp_wildcard(const char *wildcard, size_t wlen, const char *check, size_t clen)
lws_strexp::olen
size_t olen
Definition
lws-tokenize.h:205
lws_strexp
Definition
lws-tokenize.h:200
lws_tokenize
Definition
lws-tokenize.h:107
include
libwebsockets
lws-tokenize.h
Generated on
for libwebsockets by
1.18.0