fix warning, and place bet on the safe side
[m6w6/ext-http] / php_http_message_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 #ifndef DBG_PARSER
16 # define DBG_PARSER 0
17 #endif
18
19 typedef struct php_http_message_parser_state_spec {
20 php_http_message_parser_state_t state;
21 unsigned need_data:1;
22 } php_http_message_parser_state_spec_t;
23
24 static const php_http_message_parser_state_spec_t php_http_message_parser_states[] = {
25 {PHP_HTTP_MESSAGE_PARSER_STATE_START, 1},
26 {PHP_HTTP_MESSAGE_PARSER_STATE_HEADER, 0},
27 {PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE, 0},
28 {PHP_HTTP_MESSAGE_PARSER_STATE_BODY, 0},
29 {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB, 1},
30 {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH, 1},
31 {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED, 1},
32 {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE, 0},
33 {PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL, 0},
34 {PHP_HTTP_MESSAGE_PARSER_STATE_DONE, 0}
35 };
36
37 #if DBG_PARSER
38 const char *php_http_message_parser_state_name(php_http_message_parser_state_t state) {
39 const char *states[] = {"START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "UPDATE_CL", "DONE"};
40
41 if (state < 0 || state > (sizeof(states)/sizeof(char*))-1) {
42 return "FAILURE";
43 }
44 return states[state];
45 }
46 #endif
47
48 php_http_message_parser_t *php_http_message_parser_init(php_http_message_parser_t *parser TSRMLS_DC)
49 {
50 if (!parser) {
51 parser = emalloc(sizeof(*parser));
52 }
53 memset(parser, 0, sizeof(*parser));
54
55 TSRMLS_SET_CTX(parser->ts);
56
57 php_http_header_parser_init(&parser->header TSRMLS_CC);
58
59 return parser;
60 }
61
62 php_http_message_parser_state_t php_http_message_parser_state_push(php_http_message_parser_t *parser, unsigned argc, ...)
63 {
64 php_http_message_parser_state_t state = PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE;
65 va_list va_args;
66 unsigned i;
67
68 if (argc > 0) {
69 /* short circuit */
70 ZEND_PTR_STACK_RESIZE_IF_NEEDED((&parser->stack), argc);
71
72 va_start(va_args, argc);
73 for (i = 0; i < argc; ++i) {
74 state = va_arg(va_args, php_http_message_parser_state_t);
75 zend_ptr_stack_push(&parser->stack, (void *) state);
76 }
77 va_end(va_args);
78 }
79
80 return state;
81 }
82
83 php_http_message_parser_state_t php_http_message_parser_state_is(php_http_message_parser_t *parser)
84 {
85 if (parser->stack.top) {
86 return (php_http_message_parser_state_t) parser->stack.elements[parser->stack.top - 1];
87 }
88 return PHP_HTTP_MESSAGE_PARSER_STATE_START;
89 }
90
91 php_http_message_parser_state_t php_http_message_parser_state_pop(php_http_message_parser_t *parser)
92 {
93 if (parser->stack.top) {
94 return (php_http_message_parser_state_t) zend_ptr_stack_pop(&parser->stack);
95 }
96 return PHP_HTTP_MESSAGE_PARSER_STATE_START;
97 }
98
99 void php_http_message_parser_dtor(php_http_message_parser_t *parser)
100 {
101 php_http_header_parser_dtor(&parser->header);
102 zend_ptr_stack_destroy(&parser->stack);
103 php_http_message_free(&parser->message);
104 if (parser->dechunk) {
105 php_http_encoding_stream_free(&parser->dechunk);
106 }
107 if (parser->inflate) {
108 php_http_encoding_stream_free(&parser->inflate);
109 }
110 }
111
112 void php_http_message_parser_free(php_http_message_parser_t **parser)
113 {
114 if (*parser) {
115 php_http_message_parser_dtor(*parser);
116 efree(*parser);
117 *parser = NULL;
118 }
119 }
120
121 php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_http_buffer_t *buf, php_stream *s, unsigned flags, php_http_message_t **message)
122 {
123 php_http_message_parser_state_t state = PHP_HTTP_MESSAGE_PARSER_STATE_START;
124 TSRMLS_FETCH_FROM_CTX(parser->ts);
125
126 if (!buf->data) {
127 php_http_buffer_resize_ex(buf, 0x1000, 1, 0);
128 }
129 while (1) {
130 size_t justread = 0;
131 #if DBG_PARSER
132 fprintf(stderr, "#SP: %s (f:%u)\n", php_http_message_parser_state_name(state), flags);
133 #endif
134 /* resize if needed */
135 if (buf->free < 0x1000) {
136 php_http_buffer_resize_ex(buf, 0x1000, 1, 0);
137 }
138 switch (state) {
139 case PHP_HTTP_MESSAGE_PARSER_STATE_START:
140 case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER:
141 case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
142 /* read line */
143 php_stream_get_line(s, buf->data + buf->used, buf->free, &justread);
144 /* if we fail reading a whole line, try a single char */
145 if (!justread) {
146 int c = php_stream_getc(s);
147
148 if (c != EOF) {
149 char s[1] = {c};
150 justread = php_http_buffer_append(buf, s, 1);
151 }
152 }
153 php_http_buffer_account(buf, justread);
154 break;
155
156 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB:
157 /* read all */
158 justread = php_stream_read(s, buf->data + buf->used, buf->free);
159 php_http_buffer_account(buf, justread);
160 break;
161
162 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH:
163 /* read body_length */
164 justread = php_stream_read(s, buf->data + buf->used, MIN(buf->free, parser->body_length));
165 php_http_buffer_account(buf, justread);
166 break;
167
168 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED:
169 /* duh, this is very naive */
170 if (parser->body_length) {
171 justread = php_stream_read(s, buf->data + buf->used, MIN(parser->body_length, buf->free));
172
173 php_http_buffer_account(buf, justread);
174
175 parser->body_length -= justread;
176 } else {
177 php_http_buffer_resize(buf, 24);
178 php_stream_get_line(s, buf->data, buf->free, &justread);
179 php_http_buffer_account(buf, justread);
180
181 parser->body_length = strtoul(buf->data + buf->used - justread, NULL, 16);
182 }
183 break;
184
185 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY:
186 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:
187 case PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL:
188 /* should not occur */
189 abort();
190 break;
191
192 case PHP_HTTP_MESSAGE_PARSER_STATE_DONE:
193 case PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE:
194 return php_http_message_parser_state_is(parser);
195 }
196
197 if (justread) {
198 state = php_http_message_parser_parse(parser, buf, flags, message);
199 } else if (php_stream_eof(s)) {
200 return php_http_message_parser_parse(parser, buf, flags | PHP_HTTP_MESSAGE_PARSER_CLEANUP, message);
201 } else {
202 return state;
203 }
204 }
205
206 return PHP_HTTP_MESSAGE_PARSER_STATE_DONE;
207 }
208
209
210 php_http_message_parser_state_t php_http_message_parser_parse(php_http_message_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, php_http_message_t **message)
211 {
212 char *str = NULL;
213 size_t len = 0;
214 size_t cut = 0;
215 TSRMLS_FETCH_FROM_CTX(parser->ts);
216
217 while (buffer->used || !php_http_message_parser_states[php_http_message_parser_state_is(parser)].need_data) {
218 #if DBG_PARSER
219 fprintf(stderr, "#MP: %s (f: %u, t:%d, l:%zu)\n",
220 php_http_message_parser_state_name(php_http_message_parser_state_is(parser)),
221 flags,
222 message && *message ? (*message)->type : -1,
223 buffer->used
224 );
225 _dpf(0, buffer->data, buffer->used);
226 #endif
227
228 switch (php_http_message_parser_state_pop(parser))
229 {
230 case PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE:
231 return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
232
233 case PHP_HTTP_MESSAGE_PARSER_STATE_START:
234 {
235 char *ptr = buffer->data;
236
237 while (ptr - buffer->data < buffer->used && PHP_HTTP_IS_CTYPE(space, *ptr)) {
238 ++ptr;
239 }
240
241 php_http_buffer_cut(buffer, 0, ptr - buffer->data);
242
243 if (buffer->used) {
244 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER);
245 }
246 break;
247 }
248
249 case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER:
250 {
251 unsigned header_parser_flags = (flags & PHP_HTTP_MESSAGE_PARSER_CLEANUP) ? PHP_HTTP_HEADER_PARSER_CLEANUP : 0;
252
253 switch (php_http_header_parser_parse(&parser->header, buffer, header_parser_flags, *message ? &(*message)->hdrs : NULL, (php_http_info_callback_t) php_http_message_info_callback, message)) {
254 case PHP_HTTP_HEADER_PARSER_STATE_FAILURE:
255 return PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE;
256
257 case PHP_HTTP_HEADER_PARSER_STATE_DONE:
258 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE);
259 break;
260
261 default:
262 if (buffer->used || !(flags & PHP_HTTP_MESSAGE_PARSER_CLEANUP)) {
263 return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER);
264 } else {
265 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE);
266 }
267 }
268 break;
269 }
270
271 case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
272 {
273 zval *h, *h_loc = NULL, *h_con = NULL, **h_cl = NULL, **h_cr = NULL, **h_te = NULL;
274
275 /* Content-Range has higher precedence than Content-Length,
276 * and content-length denotes the original length of the entity,
277 * so let's *NOT* remove CR/CL, because that would fundamentally
278 * change the meaning of the whole message
279 */
280 if ((h = php_http_message_header(*message, ZEND_STRL("Transfer-Encoding"), 1))) {
281 zend_hash_update(&(*message)->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &h, sizeof(zval *), (void *) &h_te);
282 zend_hash_del(&(*message)->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
283
284 /* reset */
285 MAKE_STD_ZVAL(h);
286 ZVAL_LONG(h, 0);
287 zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &h, sizeof(zval *), NULL);
288 } else if ((h = php_http_message_header(*message, ZEND_STRL("Content-Length"), 1))) {
289 zend_hash_update(&(*message)->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) &h, sizeof(zval *), (void *) &h_cl);
290 }
291
292 if ((h = php_http_message_header(*message, ZEND_STRL("Content-Range"), 1))) {
293 zend_hash_find(&(*message)->hdrs, ZEND_STRS("Content-Range"), (void *) &h_cr);
294 if (h != *h_cr) {
295 zend_hash_update(&(*message)->hdrs, "Content-Range", sizeof("Content-Range"), &h, sizeof(zval *), (void *) &h_cr);
296 } else {
297 zval_ptr_dtor(&h);
298 }
299 }
300
301 /* so, if curl sees a 3xx code, a Location header and a Connection:close header
302 * it decides not to read the response body.
303 */
304 if ((flags & PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS)
305 && (*message)->type == PHP_HTTP_RESPONSE
306 && (*message)->http.info.response.code/100 == 3
307 && (h_loc = php_http_message_header(*message, ZEND_STRL("Location"), 1))
308 && (h_con = php_http_message_header(*message, ZEND_STRL("Connection"), 1))
309 ) {
310 if (php_http_match(Z_STRVAL_P(h_con), "close", PHP_HTTP_MATCH_WORD)) {
311 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
312 zval_ptr_dtor(&h_loc);
313 zval_ptr_dtor(&h_con);
314 break;
315 }
316 }
317 if (h_loc) {
318 zval_ptr_dtor(&h_loc);
319 }
320 if (h_con) {
321 zval_ptr_dtor(&h_con);
322 }
323
324 if ((h = php_http_message_header(*message, ZEND_STRL("Content-Encoding"), 1))) {
325 if (php_http_match(Z_STRVAL_P(h), "gzip", PHP_HTTP_MATCH_WORD)
326 || php_http_match(Z_STRVAL_P(h), "x-gzip", PHP_HTTP_MATCH_WORD)
327 || php_http_match(Z_STRVAL_P(h), "deflate", PHP_HTTP_MATCH_WORD)
328 ) {
329 if (parser->inflate) {
330 php_http_encoding_stream_reset(&parser->inflate);
331 } else {
332 parser->inflate = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_inflate_ops(), 0 TSRMLS_CC);
333 }
334 zend_hash_update(&(*message)->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &h, sizeof(zval *), NULL);
335 zend_hash_del(&(*message)->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
336 } else {
337 zval_ptr_dtor(&h);
338 }
339 }
340
341 if ((flags & PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES)) {
342 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
343 } else {
344 if (h_te) {
345 if (strstr(Z_STRVAL_PP(h_te), "chunked")) {
346 parser->dechunk = php_http_encoding_stream_init(parser->dechunk, php_http_encoding_stream_get_dechunk_ops(), 0 TSRMLS_CC);
347 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED);
348 break;
349 }
350 }
351
352 if (h_cr) {
353 ulong total = 0, start = 0, end = 0;
354
355 if (!strncasecmp(Z_STRVAL_PP(h_cr), "bytes", lenof("bytes"))
356 && ( Z_STRVAL_PP(h_cr)[lenof("bytes")] == ':'
357 || Z_STRVAL_PP(h_cr)[lenof("bytes")] == ' '
358 || Z_STRVAL_PP(h_cr)[lenof("bytes")] == '='
359 )
360 ) {
361 char *total_at = NULL, *end_at = NULL;
362 char *start_at = Z_STRVAL_PP(h_cr) + sizeof("bytes");
363
364 start = strtoul(start_at, &end_at, 10);
365 if (end_at) {
366 end = strtoul(end_at + 1, &total_at, 10);
367 if (total_at && strncmp(total_at + 1, "*", 1)) {
368 total = strtoul(total_at + 1, NULL, 10);
369 }
370
371 if (end >= start && (!total || end <= total)) {
372 parser->body_length = end + 1 - start;
373 php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
374 break;
375 }
376 }
377 }
378 }
379
380 if (h_cl) {
381 char *stop;
382
383 if (Z_TYPE_PP(h_cl) == IS_STRING) {
384 parser->body_length = strtoul(Z_STRVAL_PP(h_cl), &stop, 10);
385
386 if (stop != Z_STRVAL_PP(h_cl)) {
387 php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
388 break;
389 }
390 } else if (Z_TYPE_PP(h_cl) == IS_LONG) {
391 parser->body_length = Z_LVAL_PP(h_cl);
392 php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
393 break;
394 }
395 }
396
397 if ((*message)->type == PHP_HTTP_REQUEST) {
398 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
399 } else {
400 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
401 }
402 }
403 break;
404 }
405
406 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY:
407 {
408 if (len) {
409 /* FIXME: what if we re-use the parser? */
410 if (parser->inflate) {
411 char *dec_str = NULL;
412 size_t dec_len;
413
414 if (SUCCESS != php_http_encoding_stream_update(parser->inflate, str, len, &dec_str, &dec_len)) {
415 return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
416 }
417
418 if (str != buffer->data) {
419 PTR_FREE(str);
420 }
421 str = dec_str;
422 len = dec_len;
423 }
424
425 php_stream_write(php_http_message_body_stream((*message)->body), str, len);
426
427 }
428
429 if (cut) {
430 php_http_buffer_cut(buffer, 0, cut);
431 }
432
433 if (str != buffer->data) {
434 PTR_FREE(str);
435 }
436
437 str = NULL;
438 len = 0;
439 cut = 0;
440 break;
441 }
442
443 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB:
444 {
445 str = buffer->data;
446 len = buffer->used;
447 cut = len;
448
449 php_http_message_parser_state_push(parser, 2, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
450 break;
451 }
452
453 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH:
454 {
455 len = MIN(parser->body_length, buffer->used);
456 str = buffer->data;
457 cut = len;
458
459 parser->body_length -= len;
460
461 php_http_message_parser_state_push(parser, 2, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
462 break;
463 }
464
465 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED:
466 {
467 /*
468 * - pass available data through the dechunk stream
469 * - pass decoded data along
470 * - if stream zeroed:
471 * Y: - cut processed string out of buffer, but leave length of unprocessed dechunk stream data untouched
472 * - body done
473 * N: - parse ahaed
474 */
475 char *dec_str = NULL;
476 size_t dec_len;
477
478 if (SUCCESS != php_http_encoding_stream_update(parser->dechunk, buffer->data, buffer->used, &dec_str, &dec_len)) {
479 return PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE;
480 }
481
482 str = dec_str;
483 len = dec_len;
484
485 if (php_http_encoding_stream_done(parser->dechunk)) {
486 cut = buffer->used - PHP_HTTP_BUFFER(parser->dechunk->ctx)->used;
487 php_http_message_parser_state_push(parser, 2, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
488 } else {
489 cut = buffer->used;
490 php_http_message_parser_state_push(parser, 2, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
491 }
492 break;
493 }
494
495 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:
496 {
497 php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
498
499 if (parser->dechunk && parser->dechunk->ctx) {
500 char *dec_str = NULL;
501 size_t dec_len;
502
503 if (SUCCESS != php_http_encoding_stream_finish(parser->dechunk, &dec_str, &dec_len)) {
504 return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
505 }
506 php_http_encoding_stream_dtor(parser->dechunk);
507
508 if (dec_str && dec_len) {
509 str = dec_str;
510 len = dec_len;
511 cut = 0;
512 php_http_message_parser_state_push(parser, 2, PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
513 }
514 }
515
516 break;
517 }
518
519 case PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL:
520 {
521 zval *zcl;
522 MAKE_STD_ZVAL(zcl);
523 ZVAL_LONG(zcl, php_http_message_body_size((*message)->body));
524 zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), &zcl, sizeof(zval *), NULL);
525 break;
526 }
527
528 case PHP_HTTP_MESSAGE_PARSER_STATE_DONE:
529 {
530 char *ptr = buffer->data;
531
532 while (ptr - buffer->data < buffer->used && PHP_HTTP_IS_CTYPE(space, *ptr)) {
533 ++ptr;
534 }
535
536 php_http_buffer_cut(buffer, 0, ptr - buffer->data);
537
538 if (!(flags & PHP_HTTP_MESSAGE_PARSER_GREEDY)) {
539 return PHP_HTTP_MESSAGE_PARSER_STATE_DONE;
540 }
541 break;
542 }
543 }
544 }
545
546 return php_http_message_parser_state_is(parser);
547 }
548
549 zend_class_entry *php_http_message_parser_class_entry;
550 static zend_object_handlers php_http_message_parser_object_handlers;
551
552 zend_object_value php_http_message_parser_object_new(zend_class_entry *ce TSRMLS_DC)
553 {
554 return php_http_message_parser_object_new_ex(ce, NULL, NULL TSRMLS_CC);
555 }
556
557 zend_object_value php_http_message_parser_object_new_ex(zend_class_entry *ce, php_http_message_parser_t *parser, php_http_message_parser_object_t **ptr TSRMLS_DC)
558 {
559 php_http_message_parser_object_t *o;
560
561 o = ecalloc(1, sizeof(php_http_message_parser_object_t));
562 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
563 object_properties_init((zend_object *) o, ce);
564
565 if (ptr) {
566 *ptr = o;
567 }
568
569 if (parser) {
570 o->parser = parser;
571 } else {
572 o->parser = php_http_message_parser_init(NULL TSRMLS_CC);
573 }
574 o->buffer = php_http_buffer_new();
575
576 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_parser_object_free, NULL TSRMLS_CC);
577 o->zv.handlers = &php_http_message_parser_object_handlers;
578
579 return o->zv;
580 }
581
582 void php_http_message_parser_object_free(void *object TSRMLS_DC)
583 {
584 php_http_message_parser_object_t *o = (php_http_message_parser_object_t *) object;
585
586 if (o->parser) {
587 php_http_message_parser_free(&o->parser);
588 }
589 if (o->buffer) {
590 php_http_buffer_free(&o->buffer);
591 }
592 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
593 efree(o);
594 }
595
596 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageParser_getState, 0, 0, 0)
597 ZEND_END_ARG_INFO();
598 static PHP_METHOD(HttpMessageParser, getState)
599 {
600 php_http_message_parser_object_t *parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
601
602 zend_parse_parameters_none();
603 /* always return the real state */
604 RETVAL_LONG(php_http_message_parser_state_is(parser_obj->parser));
605 }
606
607 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageParser_parse, 0, 0, 3)
608 ZEND_ARG_INFO(0, data)
609 ZEND_ARG_INFO(0, flags)
610 ZEND_ARG_INFO(1, message)
611 ZEND_END_ARG_INFO();
612 static PHP_METHOD(HttpMessageParser, parse)
613 {
614 php_http_message_parser_object_t *parser_obj;
615 zval *zmsg;
616 char *data_str;
617 int data_len;
618 long flags;
619
620 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &data_str, &data_len, &flags, &zmsg), invalid_arg, return);
621
622 parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
623 php_http_buffer_append(parser_obj->buffer, data_str, data_len);
624 RETVAL_LONG(php_http_message_parser_parse(parser_obj->parser, parser_obj->buffer, flags, &parser_obj->parser->message));
625
626 zval_dtor(zmsg);
627 if (parser_obj->parser->message) {
628 ZVAL_OBJVAL(zmsg, php_http_message_object_new_ex(php_http_message_class_entry, php_http_message_copy(parser_obj->parser->message, NULL), NULL TSRMLS_CC), 0);
629 }
630 }
631
632 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageParser_stream, 0, 0, 3)
633 ZEND_ARG_INFO(0, stream)
634 ZEND_ARG_INFO(0, flags)
635 ZEND_ARG_INFO(1, message)
636 ZEND_END_ARG_INFO();
637 static PHP_METHOD(HttpMessageParser, stream)
638 {
639 php_http_message_parser_object_t *parser_obj;
640 zend_error_handling zeh;
641 zval *zmsg, *zstream;
642 php_stream *s;
643 long flags;
644
645 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &zstream, &flags, &zmsg), invalid_arg, return);
646
647 zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
648 php_stream_from_zval(s, &zstream);
649 zend_restore_error_handling(&zeh TSRMLS_CC);
650
651 parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
652 RETVAL_LONG(php_http_message_parser_parse_stream(parser_obj->parser, parser_obj->buffer, s, flags, &parser_obj->parser->message));
653
654 zval_dtor(zmsg);
655 if (parser_obj->parser->message) {
656 ZVAL_OBJVAL(zmsg, php_http_message_object_new_ex(php_http_message_class_entry, php_http_message_copy(parser_obj->parser->message, NULL), NULL TSRMLS_CC), 0);
657 }
658 }
659
660 static zend_function_entry php_http_message_parser_methods[] = {
661 PHP_ME(HttpMessageParser, getState, ai_HttpMessageParser_getState, ZEND_ACC_PUBLIC)
662 PHP_ME(HttpMessageParser, parse, ai_HttpMessageParser_parse, ZEND_ACC_PUBLIC)
663 PHP_ME(HttpMessageParser, stream, ai_HttpMessageParser_stream, ZEND_ACC_PUBLIC)
664 {NULL, NULL, NULL}
665 };
666
667 PHP_MINIT_FUNCTION(http_message_parser)
668 {
669 zend_class_entry ce;
670
671 INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Parser", php_http_message_parser_methods);
672 php_http_message_parser_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
673 memcpy(&php_http_message_parser_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
674 php_http_message_parser_class_entry->create_object = php_http_message_parser_object_new;
675 php_http_message_parser_object_handlers.clone_obj = NULL;
676
677 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("CLEANUP"), PHP_HTTP_MESSAGE_PARSER_CLEANUP TSRMLS_CC);
678 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("DUMB_BODIES"), PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES TSRMLS_CC);
679 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("EMPTY_REDIRECTS"), PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS TSRMLS_CC);
680 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("GREEDY"), PHP_HTTP_MESSAGE_PARSER_GREEDY TSRMLS_CC);
681
682 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_FAILURE"), PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE TSRMLS_CC);
683 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_START"), PHP_HTTP_MESSAGE_PARSER_STATE_START TSRMLS_CC);
684 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_HEADER"), PHP_HTTP_MESSAGE_PARSER_STATE_HEADER TSRMLS_CC);
685 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_HEADER_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE TSRMLS_CC);
686 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY TSRMLS_CC);
687 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_DUMB"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB TSRMLS_CC);
688 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_LENGTH"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH TSRMLS_CC);
689 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_CHUNKED"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED TSRMLS_CC);
690 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE TSRMLS_CC);
691 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_UPDATE_CL"), PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL TSRMLS_CC);
692 zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_DONE TSRMLS_CC);
693
694 return SUCCESS;
695 }
696
697 /*
698 * Local variables:
699 * tab-width: 4
700 * c-basic-offset: 4
701 * End:
702 * vim600: noet sw=4 ts=4 fdm=marker
703 * vim<600: noet sw=4 ts=4
704 */
705