-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathngx_esi_parser.h
120 lines (94 loc) · 3.76 KB
/
ngx_esi_parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Copyright (c) 2007 Todd A. Fisher
* You can redistribute it and/or modify it under the same terms as Mozilla Public License 1.1.
*/
#ifndef ESI_PARSER_H
#define ESI_PARSER_H
#include <sys/types.h>
/* how much output to hold in memory before sending out */
#define ESI_OUTPUT_BUFFER_SIZE 1024
char *esi_strndup( const char *str, size_t len );
/*
* ESI Attribute is a single attribute with name and value
*
* e.g. for an esi include tag:
*
* <esi:include src='/foo/bar/' timeout='10'/>
*
* 2 attributes would be allocated
*
* attrs[0]->name => 'src'
* attrs[0]->value => '/foo/bar/'
*
* attrs[1]->name => 'timeout'
* attrs[1]->value => '10'
*
* */
typedef struct _ESIAttr {
char *name;
char *value;
struct _ESIAttr *next;
}ESIAttribute;
ESIAttribute *esi_attribute_new( const char *name, size_t name_length, const char *value, size_t value_length );
ESIAttribute *esi_attribute_copy( ESIAttribute *attribute );
void esi_attribute_free( ESIAttribute *attribute );
typedef void (*esi_start_tag_cb)(const void *data,
const char *name_start,
size_t name_length,
ESIAttribute *attributes,
void *user_data);
typedef void (*esi_end_tag_cb)(const void *data,
const char *name_start,
size_t name_length,
void *user_data);
typedef void (*esi_output_cb)(const void *data,
size_t length,
void *user_data);
typedef struct _ESIParser {
int cs;
int prev_state;
void *user_data;
const char *mark;
size_t overflow_data_size; /* current size of the overflow data buffer */
char *overflow_data; /* overflow buffer if execute finishes and we are not in a final state store the parse data */
size_t echobuffer_allocated; /* amount of memory allocated for the echobuffer */
size_t echobuffer_index; /* current write position of the last echo'ed character */
char *echobuffer; /* echo buffer if the parse state is not 0 we store the characters here */
const char *tag_text; /* start pointer in data */
size_t tag_text_length; /* length from tag_text within data */
const char *attr_key; /* start pointer in data */
size_t attr_key_length;
const char *attr_value; /* start pointer in data */
size_t attr_value_length;
ESIAttribute *attributes, *last;
/* this memory will be pass to the output_cb when either it's full
* or eof is encountered */
char output_buffer[ESI_OUTPUT_BUFFER_SIZE+1];
size_t output_buffer_size;
esi_start_tag_cb start_tag_handler;
esi_end_tag_cb end_tag_handler;
esi_output_cb output_handler;
} ESIParser;
/* create a new Edge Side Include Parser */
ESIParser *esi_parser_new();
void esi_parser_free( ESIParser *parser );
/* initialize the parser */
int esi_parser_init( ESIParser *parser );
/*
* send a chunk of data to the parser, the internal parser state is returned
*/
int esi_parser_execute( ESIParser *parser, const char *data, size_t length );
/*
* let the parser know that it has reached the end and it should flush any remaining data to the desired output device
*/
int esi_parser_finish( ESIParser *parser );
/*
* setup a callback to execute when a new esi: start tag is encountered
* this will fire for all block tags e.g. <esi:try>, <esi:attempt> and also
* inline tags <esi:inline src='cache-key'/> <esi:include src='dest'/>
*/
void esi_parser_start_tag_handler( ESIParser *parser, esi_start_tag_cb callback );
void esi_parser_end_tag_handler( ESIParser *parser, esi_end_tag_cb callback );
/* setup a callback to recieve data ready for output */
void esi_parser_output_handler( ESIParser *parser, esi_output_cb output_handler );
#endif