experimental stream message parser
authorMichael Wallner <mike@php.net>
Fri, 24 Feb 2012 14:29:59 +0000 (14:29 +0000)
committerMichael Wallner <mike@php.net>
Fri, 24 Feb 2012 14:29:59 +0000 (14:29 +0000)
package.xml
php_http_message.c
php_http_message_parser.c
php_http_message_parser.h

index 222c516437c13d65a11f2c7696fd73feca36e8d7..2e2dec07e02184bdf31861305d38e391aed2b9ac 100644 (file)
@@ -27,9 +27,9 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be
   <email>mike@php.net</email>
   <active>yes</active>
  </lead>
- <date>2012-01-23</date>
+ <date>2012-02-17</date>
  <version>
-  <release>2.0.0dev</release>
+  <release>2.0.0dev5</release>
   <api>2.0.0</api>
  </version>
  <stability>
@@ -38,6 +38,11 @@ Extended HTTP support. Again. Keep in mind that it's got the major version 2, be
  </stability>
  <license>BSD, revised</license>
  <notes><![CDATA[
+* Improved test coverage [1] and fixed a lot of issues with the cookie, params, querystring,
+  persistent handles, request factory, etag, stream filters, encoding streams, negotiation
+  and HTTP message info code.
+
+[1] http://dev.iworks.at/ext-http/lcov/ext/http/index.html
 ]]></notes>
  <contents>
   <dir name="/">
index 7c91ac5551077a7224f1696d51e23adbb72bb4be..ab2c86225f8c1328dc6495dfacfe55e9506b8d83 100644 (file)
@@ -17,7 +17,7 @@ PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **messa
        php_http_message_t *old = *message;
 
        /* advance message */
-       if (old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
+       if (!old || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
                (*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
                (*message)->parent = old;
                (*headers) = &((*message)->hdrs);
@@ -1282,25 +1282,36 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 
 PHP_METHOD(HttpMessage, __construct)
 {
-       int length = 0;
-       char *message = NULL;
+       zval *zmessage = NULL;
+       php_http_message_t *msg = NULL;
        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
        with_error_handling(EH_THROW, php_http_exception_class_entry) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
-                       if (message && length) {
-                               php_http_message_t *msg = php_http_message_parse(NULL, message, length TSRMLS_CC);
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!", &zmessage) && zmessage) {
+                       if (Z_TYPE_P(zmessage) == IS_RESOURCE) {
+                               php_stream *s;
+                               php_http_message_parser_t p;
+
+                               php_stream_from_zval(s, &zmessage);
+                               if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
+                                       php_http_message_parser_parse_stream(&p, s, &msg);
+                                       php_http_message_parser_dtor(&p);
+                               }
+                       } else {
+                               zmessage = php_http_ztyp(IS_STRING, zmessage);
+                               msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage) TSRMLS_CC);
+                               zval_ptr_dtor(&zmessage);
+                       }
 
-                               if (!msg) {
-                                       php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, message);
-                               } else {
-                                       php_http_message_dtor(obj->message);
-                                       obj->message = msg;{
-                                       if (obj->message->parent) {
-                                               obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
-                                       }
+                       if (msg) {
+                               php_http_message_dtor(obj->message);
+                               obj->message = msg;
+                               if (obj->message->parent) {
+                                       obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
                                }
-                       } end_error_handling();
+                       } else {
+                               php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, Z_STRVAL_P(zmessage));
+                       }
                }
                if (!obj->message) {
                        obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
index cf4d4fa3ec48cd0aabeea3cac9198770abff7506..4162e3710cf060f027840bf92a16a4524de54803 100644 (file)
@@ -102,6 +102,77 @@ PHP_HTTP_API void php_http_message_parser_free(php_http_message_parser_t **parse
        }
 }
 
+PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_stream *s, php_http_message_t **message)
+{
+       php_http_buffer_t buf;
+       TSRMLS_FETCH_FROM_CTX(parser->ts);
+
+       php_http_buffer_init_ex(&buf, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC);
+
+       while (!php_stream_eof(s)) {
+               size_t len = 0;
+
+               switch (php_http_message_parser_state_is(parser)) {
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_START:
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER:
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
+                               /* read line */
+                               php_stream_get_line(s, buf.data, buf.free, &len);
+                               buf.used += len;
+                               buf.free -= len;
+                               break;
+
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB:
+                               /* read all */
+                               len = php_stream_read(s, buf.data, buf.free);
+                               buf.used += len;
+                               buf.free -= len;
+                               break;
+
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH:
+                               /* read body_length */
+                               len = php_stream_read(s, buf.data, MIN(buf.free, parser->body_length));
+                               buf.used += len;
+                               buf.free -= len;
+                               break;
+
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED:
+                               /* duh, this is very naive */
+                               if (len) {
+                                       size_t read = php_stream_read(s, buf.data, MIN(len, buf.free));
+
+                                       buf.used += read;
+                                       buf.free -= read;
+
+                                       len -= read;
+                               } else {
+                                       php_stream_get_line(s, buf.data, buf.free, &len);
+                                       buf.used += len;
+                                       buf.free -= len;
+
+                                       len = strtoul(buf.data - len, NULL, 16);
+                               }
+                               break;
+
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_BODY:
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:
+                               /* should not occur */
+                               abort();
+                               break;
+
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_DONE:
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE:
+                               php_http_buffer_dtor(&buf);
+                               return php_http_message_parser_state_is(parser);
+               }
+
+               php_http_message_parser_parse(parser, &buf, 0, message);
+       }
+
+       php_http_buffer_dtor(&buf);
+       return PHP_HTTP_MESSAGE_PARSER_STATE_DONE;
+}
+
 
 PHP_HTTP_API 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)
 {
@@ -113,7 +184,7 @@ PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse(php_h
        while (buffer->used || !php_http_message_parser_states[php_http_message_parser_state_is(parser)].need_data) {
 #if 0
                const char *state[] = {"START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "DONE"};
-               fprintf(stderr, "#MP: %s (%d)\n", php_http_message_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_message_parser_state_is(parser)], (*message)->type);
+               fprintf(stderr, "#MP: %s (%d)\n", php_http_message_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_message_parser_state_is(parser)], message && *message ? (*message)->type : -1);
                _dpf(0, buffer->data, buffer->used);
 #endif
 
index 31c30040117ff2877f3028bf36e7c8c02db0c777..bb7bfdd5be17b410e448f8ae900c54efed737736 100644 (file)
@@ -53,6 +53,7 @@ PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_state_pop(p
 PHP_HTTP_API void php_http_message_parser_dtor(php_http_message_parser_t *parser);
 PHP_HTTP_API void php_http_message_parser_free(php_http_message_parser_t **parser);
 PHP_HTTP_API 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);
+PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_stream *s, php_http_message_t **message);
 
 #endif