2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
19 typedef struct php_http_message_parser_state_spec
{
20 php_http_message_parser_state_t state
;
22 } php_http_message_parser_state_spec_t
;
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}
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"};
41 if (state
< 0 || state
> (sizeof(states
)/sizeof(char*))-1) {
48 php_http_message_parser_t
*php_http_message_parser_init(php_http_message_parser_t
*parser TSRMLS_DC
)
51 parser
= emalloc(sizeof(*parser
));
53 memset(parser
, 0, sizeof(*parser
));
55 TSRMLS_SET_CTX(parser
->ts
);
57 php_http_header_parser_init(&parser
->header TSRMLS_CC
);
62 php_http_message_parser_state_t
php_http_message_parser_state_push(php_http_message_parser_t
*parser
, unsigned argc
, ...)
64 php_http_message_parser_state_t state
= PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
;
70 ZEND_PTR_STACK_RESIZE_IF_NEEDED((&parser
->stack
), argc
);
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
);
83 php_http_message_parser_state_t
php_http_message_parser_state_is(php_http_message_parser_t
*parser
)
85 if (parser
->stack
.top
) {
86 return (php_http_message_parser_state_t
) parser
->stack
.elements
[parser
->stack
.top
- 1];
88 return PHP_HTTP_MESSAGE_PARSER_STATE_START
;
91 php_http_message_parser_state_t
php_http_message_parser_state_pop(php_http_message_parser_t
*parser
)
93 if (parser
->stack
.top
) {
94 return (php_http_message_parser_state_t
) zend_ptr_stack_pop(&parser
->stack
);
96 return PHP_HTTP_MESSAGE_PARSER_STATE_START
;
99 void php_http_message_parser_dtor(php_http_message_parser_t
*parser
)
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
);
107 if (parser
->inflate
) {
108 php_http_encoding_stream_free(&parser
->inflate
);
112 void php_http_message_parser_free(php_http_message_parser_t
**parser
)
115 php_http_message_parser_dtor(*parser
);
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
)
123 php_http_message_parser_state_t state
= PHP_HTTP_MESSAGE_PARSER_STATE_START
;
124 TSRMLS_FETCH_FROM_CTX(parser
->ts
);
127 php_http_buffer_resize_ex(buf
, 0x1000, 1, 0);
132 fprintf(stderr
, "#SP: %s (f:%u)\n", php_http_message_parser_state_name(state
), flags
);
134 /* resize if needed */
135 if (buf
->free
< 0x1000) {
136 php_http_buffer_resize_ex(buf
, 0x1000, 1, 0);
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
:
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 */
146 int c
= php_stream_getc(s
);
150 justread
= php_http_buffer_append(buf
, s
, 1);
153 php_http_buffer_account(buf
, justread
);
156 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB
:
158 justread
= php_stream_read(s
, buf
->data
+ buf
->used
, buf
->free
);
159 php_http_buffer_account(buf
, justread
);
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
);
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
));
173 php_http_buffer_account(buf
, justread
);
175 parser
->body_length
-= justread
;
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
);
181 parser
->body_length
= strtoul(buf
->data
+ buf
->used
- justread
, NULL
, 16);
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 */
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
);
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
);
206 return PHP_HTTP_MESSAGE_PARSER_STATE_DONE
;
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
)
215 TSRMLS_FETCH_FROM_CTX(parser
->ts
);
217 while (buffer
->used
|| !php_http_message_parser_states
[php_http_message_parser_state_is(parser
)].need_data
) {
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
)),
222 message
&& *message
? (*message
)->type
: -1,
225 _dpf(0, buffer
->data
, buffer
->used
);
228 switch (php_http_message_parser_state_pop(parser
))
230 case PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
:
231 return php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
);
233 case PHP_HTTP_MESSAGE_PARSER_STATE_START
:
235 char *ptr
= buffer
->data
;
237 while (ptr
- buffer
->data
< buffer
->used
&& PHP_HTTP_IS_CTYPE(space
, *ptr
)) {
241 php_http_buffer_cut(buffer
, 0, ptr
- buffer
->data
);
244 php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER
);
249 case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER
:
251 unsigned header_parser_flags
= (flags
& PHP_HTTP_MESSAGE_PARSER_CLEANUP
) ? PHP_HTTP_HEADER_PARSER_CLEANUP
: 0;
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
;
257 case PHP_HTTP_HEADER_PARSER_STATE_DONE
:
258 php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE
);
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
);
265 php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE
);
271 case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE
:
273 zval
*h
, *h_loc
= NULL
, *h_con
= NULL
, **h_cl
= NULL
, **h_cr
= NULL
, **h_te
= NULL
;
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
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"));
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
);
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
);
295 zend_hash_update(&(*message
)->hdrs
, "Content-Range", sizeof("Content-Range"), &h
, sizeof(zval
*), (void *) &h_cr
);
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.
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))
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
);
318 zval_ptr_dtor(&h_loc
);
321 zval_ptr_dtor(&h_con
);
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
)
329 if (parser
->inflate
) {
330 php_http_encoding_stream_reset(&parser
->inflate
);
332 parser
->inflate
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_inflate_ops(), 0 TSRMLS_CC
);
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"));
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
);
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
);
353 ulong total
= 0, start
= 0, end
= 0;
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")] == '='
361 char *total_at
= NULL
, *end_at
= NULL
;
362 char *start_at
= Z_STRVAL_PP(h_cr
) + sizeof("bytes");
364 start
= strtoul(start_at
, &end_at
, 10);
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);
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
);
383 if (Z_TYPE_PP(h_cl
) == IS_STRING
) {
384 parser
->body_length
= strtoul(Z_STRVAL_PP(h_cl
), &stop
, 10);
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
);
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
);
397 if ((*message
)->type
== PHP_HTTP_REQUEST
) {
398 php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE
);
400 php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB
);
406 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY
:
409 /* FIXME: what if we re-use the parser? */
410 if (parser
->inflate
) {
411 char *dec_str
= NULL
;
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
);
418 if (str
!= buffer
->data
) {
425 php_stream_write(php_http_message_body_stream((*message
)->body
), str
, len
);
430 php_http_buffer_cut(buffer
, 0, cut
);
433 if (str
!= buffer
->data
) {
443 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB
:
449 php_http_message_parser_state_push(parser
, 2, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE
, PHP_HTTP_MESSAGE_PARSER_STATE_BODY
);
453 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH
:
455 len
= MIN(parser
->body_length
, buffer
->used
);
459 parser
->body_length
-= len
;
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
);
465 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED
:
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
475 char *dec_str
= NULL
;
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
;
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
);
490 php_http_message_parser_state_push(parser
, 2, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED
, PHP_HTTP_MESSAGE_PARSER_STATE_BODY
);
495 case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE
:
497 php_http_message_parser_state_push(parser
, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE
);
499 if (parser
->dechunk
&& parser
->dechunk
->ctx
) {
500 char *dec_str
= NULL
;
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
);
506 php_http_encoding_stream_dtor(parser
->dechunk
);
508 if (dec_str
&& dec_len
) {
512 php_http_message_parser_state_push(parser
, 2, PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL
, PHP_HTTP_MESSAGE_PARSER_STATE_BODY
);
519 case PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL
:
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
);
528 case PHP_HTTP_MESSAGE_PARSER_STATE_DONE
:
530 char *ptr
= buffer
->data
;
532 while (ptr
- buffer
->data
< buffer
->used
&& PHP_HTTP_IS_CTYPE(space
, *ptr
)) {
536 php_http_buffer_cut(buffer
, 0, ptr
- buffer
->data
);
538 if (!(flags
& PHP_HTTP_MESSAGE_PARSER_GREEDY
)) {
539 return PHP_HTTP_MESSAGE_PARSER_STATE_DONE
;
546 return php_http_message_parser_state_is(parser
);
549 zend_class_entry
*php_http_message_parser_class_entry
;
550 static zend_object_handlers php_http_message_parser_object_handlers
;
552 zend_object_value
php_http_message_parser_object_new(zend_class_entry
*ce TSRMLS_DC
)
554 return php_http_message_parser_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
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
)
559 php_http_message_parser_object_t
*o
;
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
);
572 o
->parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
574 o
->buffer
= php_http_buffer_new();
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
;
582 void php_http_message_parser_object_free(void *object TSRMLS_DC
)
584 php_http_message_parser_object_t
*o
= (php_http_message_parser_object_t
*) object
;
587 php_http_message_parser_free(&o
->parser
);
590 php_http_buffer_free(&o
->buffer
);
592 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
596 ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageParser_getState
, 0, 0, 0)
598 static PHP_METHOD(HttpMessageParser
, getState
)
600 php_http_message_parser_object_t
*parser_obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
602 zend_parse_parameters_none();
603 /* always return the real state */
604 RETVAL_LONG(php_http_message_parser_state_is(parser_obj
->parser
));
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
)
612 static PHP_METHOD(HttpMessageParser
, parse
)
614 php_http_message_parser_object_t
*parser_obj
;
620 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "slz", &data_str
, &data_len
, &flags
, &zmsg
), invalid_arg
, return);
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
));
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);
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
)
637 static PHP_METHOD(HttpMessageParser
, stream
)
639 php_http_message_parser_object_t
*parser_obj
;
640 zend_error_handling zeh
;
641 zval
*zmsg
, *zstream
;
645 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "rlz", &zstream
, &flags
, &zmsg
), invalid_arg
, return);
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
);
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
));
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);
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
)
667 PHP_MINIT_FUNCTION(http_message_parser
)
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
;
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
);
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
);
702 * vim600: noet sw=4 ts=4 fdm=marker
703 * vim<600: noet sw=4 ts=4