<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>
</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="/">
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);
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);
}
}
+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)
{
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
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