reorder
[m6w6/ext-http] / php_http_header_parser.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 typedef struct php_http_header_parser_state_spec {
16 php_http_header_parser_state_t state;
17 unsigned need_data:1;
18 } php_http_header_parser_state_spec_t;
19
20 static const php_http_header_parser_state_spec_t php_http_header_parser_states[] = {
21 {PHP_HTTP_HEADER_PARSER_STATE_START, 1},
22 {PHP_HTTP_HEADER_PARSER_STATE_KEY, 1},
23 {PHP_HTTP_HEADER_PARSER_STATE_VALUE, 1},
24 {PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX, 1},
25 {PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE, 0},
26 {PHP_HTTP_HEADER_PARSER_STATE_DONE, 0}
27 };
28
29 php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser TSRMLS_DC)
30 {
31 if (!parser) {
32 parser = emalloc(sizeof(*parser));
33 }
34 memset(parser, 0, sizeof(*parser));
35
36 TSRMLS_SET_CTX(parser->ts);
37
38 return parser;
39 }
40
41 php_http_header_parser_state_t php_http_header_parser_state_push(php_http_header_parser_t *parser, unsigned argc, ...)
42 {
43 va_list va_args;
44 unsigned i;
45 php_http_header_parser_state_t state = 0;
46
47 /* short circuit */
48 ZEND_PTR_STACK_RESIZE_IF_NEEDED((&parser->stack), argc);
49
50 va_start(va_args, argc);
51 for (i = 0; i < argc; ++i) {
52 state = va_arg(va_args, php_http_header_parser_state_t);
53 zend_ptr_stack_push(&parser->stack, (void *) state);
54 }
55 va_end(va_args);
56
57 return state;
58 }
59
60 php_http_header_parser_state_t php_http_header_parser_state_is(php_http_header_parser_t *parser)
61 {
62 if (parser->stack.top) {
63 return (php_http_header_parser_state_t) parser->stack.elements[parser->stack.top - 1];
64 }
65
66 return PHP_HTTP_HEADER_PARSER_STATE_START;
67 }
68
69 php_http_header_parser_state_t php_http_header_parser_state_pop(php_http_header_parser_t *parser)
70 {
71 if (parser->stack.top) {
72 return (php_http_header_parser_state_t) zend_ptr_stack_pop(&parser->stack);
73 }
74
75 return PHP_HTTP_HEADER_PARSER_STATE_START;
76 }
77
78 void php_http_header_parser_dtor(php_http_header_parser_t *parser)
79 {
80 zend_ptr_stack_destroy(&parser->stack);
81 php_http_info_dtor(&parser->info);
82 PTR_FREE(parser->_key.str);
83 PTR_FREE(parser->_val.str);
84 }
85
86 void php_http_header_parser_free(php_http_header_parser_t **parser)
87 {
88 if (*parser) {
89 php_http_header_parser_dtor(*parser);
90 efree(*parser);
91 *parser = NULL;
92 }
93 }
94
95 STATUS php_http_header_parser_parse(php_http_header_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg)
96 {
97 TSRMLS_FETCH_FROM_CTX(parser->ts);
98
99 while (buffer->used || !php_http_header_parser_states[php_http_header_parser_state_is(parser)].need_data) {
100 #if 0
101 const char *state[] = {"START", "KEY", "VALUE", "HEADER_DONE", "DONE"};
102 fprintf(stderr, "#HP: %s (avail:%zu, num:%d)\n", php_http_header_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_header_parser_state_is(parser)], buffer->used, headers?zend_hash_num_elements(headers):0);
103 _dpf(0, buffer->data, buffer->used);
104 #endif
105 switch (php_http_header_parser_state_pop(parser)) {
106 case PHP_HTTP_HEADER_PARSER_STATE_FAILURE:
107 return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
108
109 case PHP_HTTP_HEADER_PARSER_STATE_START: {
110 char *ptr = buffer->data;
111
112 while (ptr - buffer->data < buffer->used && PHP_HTTP_IS_CTYPE(space, *ptr)) {
113 ++ptr;
114 }
115
116 php_http_buffer_cut(buffer, 0, ptr - buffer->data);
117 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_KEY);
118 break;
119 }
120
121 case PHP_HTTP_HEADER_PARSER_STATE_KEY: {
122 const char *colon, *eol_str = NULL;
123 int eol_len = 0;
124
125 if (buffer->data == (eol_str = php_http_locate_bin_eol(buffer->data, buffer->used, &eol_len))) {
126 /* end of headers */
127 php_http_buffer_cut(buffer, 0, eol_len);
128 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_DONE);
129 } else if (php_http_info_parse(&parser->info, php_http_buffer_fix(buffer)->data TSRMLS_CC)) {
130 /* new message starting with request/response line */
131 if (callback_func) {
132 callback_func(callback_arg, &headers, &parser->info TSRMLS_CC);
133 }
134 php_http_info_dtor(&parser->info);
135 php_http_buffer_cut(buffer, 0, eol_str + eol_len - buffer->data);
136 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
137 } else if ((colon = memchr(buffer->data, ':', buffer->used)) && (!eol_str || eol_str > colon)) {
138 /* header: string */
139 parser->_key.str = estrndup(buffer->data, parser->_key.len = colon - buffer->data);
140 while (PHP_HTTP_IS_CTYPE(space, *++colon) && *colon != '\n' && *colon != '\r');
141 php_http_buffer_cut(buffer, 0, colon - buffer->data);
142 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
143 } else {
144 /* neither reqeust/response line nor header: string */
145 return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
146 }
147 break;
148 }
149
150 case PHP_HTTP_HEADER_PARSER_STATE_VALUE: {
151 const char *eol_str;
152 int eol_len;
153
154 #define SET_ADD_VAL(slen, eol_len) \
155 do { \
156 const char *ptr = buffer->data; \
157 size_t len = slen; \
158 \
159 while (len > 0 && PHP_HTTP_IS_CTYPE(space, *ptr)) { \
160 ++ptr; \
161 --len; \
162 } \
163 while (len > 0 && PHP_HTTP_IS_CTYPE(space, ptr[len - 1])) { \
164 --len; \
165 } \
166 \
167 if (len > 0) { \
168 if (parser->_val.str) { \
169 parser->_val.str = erealloc(parser->_val.str, parser->_val.len + len + 2); \
170 parser->_val.str[parser->_val.len++] = ' '; \
171 memcpy(&parser->_val.str[parser->_val.len], ptr, len); \
172 parser->_val.len += len; \
173 parser->_val.str[parser->_val.len] = '\0'; \
174 } else { \
175 parser->_val.len = len; \
176 parser->_val.str = estrndup(ptr, len); \
177 } \
178 } \
179 php_http_buffer_cut(buffer, 0, slen + eol_len); \
180 } while (0)
181
182 if ((eol_str = php_http_locate_bin_eol(buffer->data, buffer->used, &eol_len))) {
183 SET_ADD_VAL(eol_str - buffer->data, eol_len);
184
185 if (buffer->used) {
186 if (*buffer->data != '\t' && *buffer->data != ' ') {
187 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
188 break;
189 } else {
190 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
191 break;
192 }
193 }
194 }
195
196 if (flags & PHP_HTTP_HEADER_PARSER_CLEANUP) {
197 if (buffer->used) {
198 SET_ADD_VAL(buffer->used, 0);
199 }
200 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
201 } else {
202 return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX);
203 }
204 break;
205 }
206
207 case PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX:
208 if (*buffer->data == ' ' || *buffer->data == '\t') {
209 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
210 } else {
211 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
212 }
213 break;
214
215 case PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE:
216 if (parser->_key.str && parser->_val.str) {
217 zval array, **exist;
218
219 if (!headers && callback_func) {
220 callback_func(callback_arg, &headers, NULL TSRMLS_CC);
221 }
222
223 INIT_PZVAL_ARRAY(&array, headers);
224 php_http_pretty_key(parser->_key.str, parser->_key.len, 1, 1);
225 if (SUCCESS == zend_symtable_find(headers, parser->_key.str, parser->_key.len + 1, (void *) &exist)) {
226 convert_to_array(*exist);
227 add_next_index_stringl(*exist, parser->_val.str, parser->_val.len, 0);
228 } else {
229 add_assoc_stringl_ex(&array, parser->_key.str, parser->_key.len + 1, parser->_val.str, parser->_val.len, 0);
230 }
231 parser->_val.str = NULL;
232 }
233
234 PTR_SET(parser->_key.str, NULL);
235 PTR_SET(parser->_val.str, NULL);
236
237 php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_KEY);
238 break;
239
240 case PHP_HTTP_HEADER_PARSER_STATE_DONE:
241 return PHP_HTTP_HEADER_PARSER_STATE_DONE;
242 }
243 }
244
245 return php_http_header_parser_state_is(parser);
246 }
247
248 /*
249 * Local variables:
250 * tab-width: 4
251 * c-basic-offset: 4
252 * End:
253 * vim600: noet sw=4 ts=4 fdm=marker
254 * vim<600: noet sw=4 ts=4
255 */
256