thread safety
[m6w6/ext-http] / php_http_message_parser.c
index e2751a415e145e6634abfe6c565e80e3054c2ce4..fce24ed2cffc4e49eae22f457f66c5dad3303f9f 100644 (file)
@@ -74,10 +74,10 @@ PHP_HTTP_API void php_http_message_parser_dtor(php_http_message_parser_t *parser
        php_http_header_parser_dtor(&parser->header);
        zend_stack_destroy(&parser->stack);
        if (parser->dechunk) {
-               php_http_encoding_stream_free(&parser->dechunk TSRMLS_CC);
+               php_http_encoding_stream_free(&parser->dechunk);
        }
        if (parser->inflate) {
-               php_http_encoding_stream_free(&parser->inflate TSRMLS_CC);
+               php_http_encoding_stream_free(&parser->inflate);
        }
 }
 
@@ -91,7 +91,7 @@ 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(php_http_message_parser_t *parser, php_http_buffer *buffer, unsigned flags, php_http_message_t **message)
+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)
 {
        TSRMLS_FETCH_FROM_CTX(parser->ts);
        char *str = NULL;
@@ -101,8 +101,8 @@ 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) %zu\n",
-                               state[php_http_message_parser_state_is(parser)], (*message)->type, buffer->used);
+               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);
+               _dpf(0, buffer->data, buffer->used);
 #endif
 
                switch (php_http_message_parser_state_pop(parser))
@@ -149,7 +149,7 @@ PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse(php_h
 
                        case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
                        {
-                               zval *h, **h_cl = NULL, **h_cr = NULL, **h_te = NULL;
+                               zval *h, *h_loc = NULL, *h_con = NULL, **h_cl = NULL, **h_cr = NULL, **h_te = NULL;
 
                                if ((h = php_http_message_header(*message, ZEND_STRL("Transfer-Encoding"), 1))) {
                                        zend_hash_update(&(*message)->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), &h, sizeof(zval *), (void *) &h_te);
@@ -163,107 +163,147 @@ PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse(php_h
                                        zend_hash_del(&(*message)->hdrs, "Content-Range", sizeof("Content-Range"));
                                }
 
-                               if ((h = php_http_message_header(*message, ZEND_STRL("Content-Encoding"), 1))) {
-                                       if (strstr(Z_STRVAL_P(h), "gzip") || strstr(Z_STRVAL_P(h), "x-gzip") || strstr(Z_STRVAL_P(h), "deflate")) {
-                                               parser->inflate = php_http_encoding_stream_init(parser->inflate, php_http_encoding_stream_get_inflate_ops(), 0 TSRMLS_CC);
-                                               zend_hash_update(&(*message)->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &h, sizeof(zval *), NULL);
-                                               zend_hash_del(&(*message)->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
-                                       } else {
-                                               zval_ptr_dtor(&h);
-                                       }
-                               }
-
                                /* default */
                                MAKE_STD_ZVAL(h);
                                ZVAL_LONG(h, 0);
                                zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
 
-                               if (h_te) {
-                                       if (strstr(Z_STRVAL_PP(h_te), "chunked")) {
-                                               parser->dechunk = php_http_encoding_stream_init(parser->dechunk, php_http_encoding_stream_get_dechunk_ops(), 0 TSRMLS_CC);
-                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED);
+                               /* so, if curl sees a 3xx code, a Location header and a Connection:close header
+                                * it decides not to read the response body.
+                                */
+                               if ((flags & PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS)
+                               &&      (*message)->type == PHP_HTTP_RESPONSE
+                               &&      (*message)->http.info.response.code/100 == 3
+                               &&      (h_loc = php_http_message_header(*message, ZEND_STRL("Location"), 1))
+                               &&      (h_con = php_http_message_header(*message, ZEND_STRL("Connection"), 1))
+                               ) {
+                                       if (php_http_match(Z_STRVAL_P(h_con), "close", PHP_HTTP_MATCH_WORD)) {
+                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
+                                               zval_ptr_dtor(&h_loc);
+                                               zval_ptr_dtor(&h_con);
                                                break;
                                        }
                                }
+                               if (h_loc) {
+                                       zval_ptr_dtor(&h_loc);
+                               }
+                               if (h_con) {
+                                       zval_ptr_dtor(&h_con);
+                               }
 
-                               if (h_cl) {
-                                       char *stop;
-
-                                       parser->body_length = strtoul(Z_STRVAL_PP(h_cl), &stop, 10);
-
-                                       if (stop != Z_STRVAL_PP(h_cl)) {
-                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
-                                               break;
+                               if ((h = php_http_message_header(*message, ZEND_STRL("Content-Encoding"), 1))) {
+                                       if (php_http_match(Z_STRVAL_P(h), "gzip", PHP_HTTP_MATCH_WORD)
+                                       ||      php_http_match(Z_STRVAL_P(h), "x-gzip", PHP_HTTP_MATCH_WORD)
+                                       ||      php_http_match(Z_STRVAL_P(h), "deflate", PHP_HTTP_MATCH_WORD)
+                                       ) {
+                                               if (parser->inflate) {
+                                                       php_http_encoding_stream_reset(&parser->inflate);
+                                               } else {
+                                                       parser->inflate = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_inflate_ops(), 0 TSRMLS_CC);
+                                               }
+                                               zend_hash_update(&(*message)->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &h, sizeof(zval *), NULL);
+                                               zend_hash_del(&(*message)->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
+                                       } else {
+                                               zval_ptr_dtor(&h);
                                        }
                                }
 
-                               if (h_cr) {
-                                       ulong total = 0, start = 0, end = 0;
+                               if ((flags & PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES)) {
+                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
+                               } else {
+                                       if (h_te) {
+                                               if (strstr(Z_STRVAL_PP(h_te), "chunked")) {
+                                                       parser->dechunk = php_http_encoding_stream_init(parser->dechunk, php_http_encoding_stream_get_dechunk_ops(), 0 TSRMLS_CC);
+                                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED);
+                                                       break;
+                                               }
+                                       }
 
-                                       if (!strncasecmp(Z_STRVAL_PP(h_cr), "bytes", lenof("bytes"))
-                                       && (    Z_STRVAL_P(h)[lenof("bytes")] == ':'
-                                               ||      Z_STRVAL_P(h)[lenof("bytes")] == ' '
-                                               ||      Z_STRVAL_P(h)[lenof("bytes")] == '='
-                                               )
-                                       ) {
-                                               char *total_at = NULL, *end_at = NULL;
-                                               char *start_at = Z_STRVAL_PP(h_cr) + sizeof("bytes");
-
-                                               start = strtoul(start_at, &end_at, 10);
-                                               if (end_at) {
-                                                       end = strtoul(end_at + 1, &total_at, 10);
-                                                       if (total_at && strncmp(total_at + 1, "*", 1)) {
-                                                               total = strtoul(total_at + 1, NULL, 10);
-                                                       }
+                                       if (h_cl) {
+                                               char *stop;
+
+                                               parser->body_length = strtoul(Z_STRVAL_PP(h_cl), &stop, 10);
 
-                                                       if (end >= start && (!total || end < total)) {
-                                                               parser->body_length = end + 1 - start;
-                                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
-                                                               break;
+                                               if (stop != Z_STRVAL_PP(h_cl)) {
+                                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
+                                                       break;
+                                               }
+                                       }
+
+                                       if (h_cr) {
+                                               ulong total = 0, start = 0, end = 0;
+
+                                               if (!strncasecmp(Z_STRVAL_PP(h_cr), "bytes", lenof("bytes"))
+                                               && (    Z_STRVAL_P(h)[lenof("bytes")] == ':'
+                                                       ||      Z_STRVAL_P(h)[lenof("bytes")] == ' '
+                                                       ||      Z_STRVAL_P(h)[lenof("bytes")] == '='
+                                                       )
+                                               ) {
+                                                       char *total_at = NULL, *end_at = NULL;
+                                                       char *start_at = Z_STRVAL_PP(h_cr) + sizeof("bytes");
+
+                                                       start = strtoul(start_at, &end_at, 10);
+                                                       if (end_at) {
+                                                               end = strtoul(end_at + 1, &total_at, 10);
+                                                               if (total_at && strncmp(total_at + 1, "*", 1)) {
+                                                                       total = strtoul(total_at + 1, NULL, 10);
+                                                               }
+
+                                                               if (end >= start && (!total || end < total)) {
+                                                                       parser->body_length = end + 1 - start;
+                                                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
+                                                                       break;
+                                                               }
                                                        }
                                                }
                                        }
-                               }
 
 
-                               if ((*message)->type == PHP_HTTP_REQUEST) {
-                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
-                               } else {
-                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
+                                       if ((*message)->type == PHP_HTTP_REQUEST) {
+                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
+                                       } else {
+                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
+                                       }
                                }
                                break;
                        }
 
                        case PHP_HTTP_MESSAGE_PARSER_STATE_BODY:
                        {
-                               zval *zcl;
+                               if (len) {
+                                       zval *zcl;
 
-                               if (parser->inflate) {
-                                       char *dec_str = NULL;
-                                       size_t dec_len;
+                                       if (parser->inflate) {
+                                               char *dec_str = NULL;
+                                               size_t dec_len;
 
-                                       if (SUCCESS != php_http_encoding_stream_update(parser->inflate, str, len, &dec_str, &dec_len TSRMLS_CC)) {
-                                               return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
-                                       }
+                                               if (SUCCESS != php_http_encoding_stream_update(parser->inflate, str, len, &dec_str, &dec_len)) {
+                                                       return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
+                                               }
 
-                                       if (str != buffer->data) {
-                                               STR_FREE(str);
+                                               if (str != buffer->data) {
+                                                       STR_FREE(str);
+                                               }
+                                               str = dec_str;
+                                               len = dec_len;
                                        }
-                                       str = dec_str;
-                                       len = dec_len;
-                               }
 
-                               php_stream_write(php_http_message_body_stream(&(*message)->body), str, len);
-                               php_http_buffer_cut(buffer, 0, cut);
+                                       php_stream_write(php_http_message_body_stream(&(*message)->body), str, len);
 
-                               /* keep track */
-                               MAKE_STD_ZVAL(zcl);
-                               ZVAL_LONG(zcl, php_http_message_body_size(&(*message)->body));
-                               zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), &zcl, sizeof(zval *), NULL);
+                                       /* keep track */
+                                       MAKE_STD_ZVAL(zcl);
+                                       ZVAL_LONG(zcl, php_http_message_body_size(&(*message)->body));
+                                       zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), &zcl, sizeof(zval *), NULL);
+                               }
+
+                               if (cut) {
+                                       php_http_buffer_cut(buffer, 0, cut);
+                               }
 
                                if (str != buffer->data) {
                                        STR_FREE(str);
                                }
+
                                str = NULL;
                                len = 0;
                                cut = 0;
@@ -302,10 +342,10 @@ PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse(php_h
                                 *              - body done
                                 *      N:      - parse ahaed
                                 */
-                               size_t dec_len;
                                char *dec_str = NULL;
+                               size_t dec_len;
 
-                               if (SUCCESS != php_http_encoding_stream_update(parser->dechunk, buffer->data, buffer->used, &dec_str, &dec_len TSRMLS_CC)) {
+                               if (SUCCESS != php_http_encoding_stream_update(parser->dechunk, buffer->data, buffer->used, &dec_str, &dec_len)) {
                                        return FAILURE;
                                }
 
@@ -327,10 +367,10 @@ PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse(php_h
                                php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
 
                                if (parser->dechunk) {
-                                       char *dec_str;
+                                       char *dec_str = NULL;
                                        size_t dec_len;
 
-                                       if (SUCCESS != php_http_encoding_stream_finish(parser->dechunk, &dec_str, &dec_len TSRMLS_CC)) {
+                                       if (SUCCESS != php_http_encoding_stream_finish(parser->dechunk, &dec_str, &dec_len)) {
                                                return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
                                        }
                                        php_http_encoding_stream_dtor(parser->dechunk);