On Wed, Apr 10, 2002 at 08:21:01PM +0200, [EMAIL PROTECTED] wrote:
> SG() is a macro that wraps around TSRM, the Thread Safe Resource Manager. 
> If the SAPI is build with ZTS (Zend Thread Safety) it is protected 
> automatically.

Excellent, thank you. At this point, given that the SG() macros ensure
thread safety, I have a patch that corrects 1 of the 2 problems we've
been seeing in the apache2filter. The second problem that remains to
be fixed is that variables from the query string are not being found in
the symbol table. I will attack this problem next.

This patch corrects intermittent SEGVs in the apache2filter. The
patch is against the 4.0.2RC2 branch/tag, and includes changes
from HEAD that were made to work with new bucket allocator code in
apache 2.

-aaron


Index: sapi/apache2filter/sapi_apache2.c
===================================================================
RCS file: /repository/php4/sapi/apache2filter/sapi_apache2.c,v
retrieving revision 1.61.2.1
diff -u -u -r1.61.2.1 sapi_apache2.c
--- sapi/apache2filter/sapi_apache2.c   14 Mar 2002 10:57:00 -0000      1.61.2.1
+++ sapi/apache2filter/sapi_apache2.c   10 Apr 2002 19:24:32 -0000
@@ -48,6 +48,7 @@
 {
        apr_bucket *b;
        apr_bucket_brigade *bb;
+       apr_bucket_alloc_t *ba;
        php_struct *ctx;
        uint now;
 
@@ -55,10 +56,11 @@
 
        if (str_length == 0) return 0;
        
-       bb = apr_brigade_create(ctx->f->r->pool);
+       ba = ctx->f->r->connection->bucket_alloc;
+       bb = apr_brigade_create(ctx->f->r->pool, ba);
        while (str_length > 0) {
                now = MIN(str_length, 4096);
-               b = apr_bucket_transient_create(str, now);
+               b = apr_bucket_transient_create(str, now, ba);
                APR_BRIGADE_INSERT_TAIL(bb, b);
                str += now;
                str_length -= now;
@@ -161,6 +163,7 @@
 {
        php_struct *ctx = server_context;
        apr_bucket_brigade *bb;
+       apr_bucket_alloc_t *ba;
        apr_bucket *b;
 
        if (!server_context)
@@ -171,8 +174,9 @@
         * all further flush buckets.
         */
        
-       bb = apr_brigade_create(ctx->f->r->pool);
-       b = apr_bucket_flush_create();
+       ba = ctx->f->r->connection->bucket_alloc;
+       bb = apr_brigade_create(ctx->f->r->pool, ba);
+       b = apr_bucket_flush_create(ba);
        APR_BRIGADE_INSERT_TAIL(bb, b);
        if (ap_pass_brigade(ctx->f->next, bb) != APR_SUCCESS) {
                php_handle_aborted_connection();
@@ -230,13 +234,6 @@
 
 AP_MODULE_DECLARE_DATA module php4_module;
 
-#define INIT_CTX \
-       if (ctx == NULL) { \
-               /* Initialize filter context */ \
-               SG(server_context) = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));  \
-               ctx->bb = apr_brigade_create(f->c->pool); \
-       }
-
 static int php_input_filter(ap_filter_t *f, apr_bucket_brigade *bb, 
                ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
 {
@@ -254,7 +251,10 @@
 
        ctx = SG(server_context);
 
-       INIT_CTX;
+       if (ctx == NULL) {
+               /* Initialize filter context */
+               SG(server_context) = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
+       }
 
        if ((rv = ap_get_brigade(f->next, bb, mode, block, readbytes)) != APR_SUCCESS) 
{
                return rv;
@@ -327,79 +327,64 @@
        ap_add_common_vars(f->r);
        ap_add_cgi_vars(f->r);
 
-       ctx = SG(server_context);
-       INIT_CTX;
-
-       ctx->f = f;
-
-       /* states:
-        * 0: request startup
-        * 1: collecting data
-        * 2: script execution and request shutdown
-        */
-       if (ctx->state == 0) {
-       
-               apply_config(conf);
-               
-               ctx->state++;
-
-               php_apache_request_ctor(f, ctx TSRMLS_CC);
-       }
-
-       /* moves all buckets from bb to ctx->bb */
-       ap_save_brigade(f, &ctx->bb, &bb, f->r->pool);
-
-       /* If we have received all data from the previous filters,
-        * we "flatten" the buckets by creating a single string buffer.
-        */
-       if (ctx->state == 1 && APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(ctx->bb))) {
-               zend_file_handle zfd;
-               apr_bucket *eos;
-
-               /* We want to execute only one script per request.
-                * A bug in Apache or other filters could cause us
-                * to reenter php_filter during script execution, so
-                * we protect ourselves here.
-                */
-               ctx->state = 2;
-
-               /* Handle phpinfo/phpcredits/built-in images */
-               if (!php_handle_special_queries(TSRMLS_C)) {
-
-                       b = APR_BRIGADE_FIRST(ctx->bb);
-                       
-                       if (APR_BUCKET_IS_FILE(b)) {
-                               const char *path;
-
-                               apr_file_name_get(&path, ((apr_bucket_file *) 
b->data)->fd);
-                               
-                               zfd.type = ZEND_HANDLE_FILENAME;
-                               zfd.filename = (char *) path;
-                               zfd.free_filename = 0;
-                               zfd.opened_path = NULL;
-
-                               php_execute_script(&zfd TSRMLS_CC);
-                       } else {
-                               
-#define PHP_NO_DATA "The PHP Filter did not receive suitable input data"
-                               
-                               eos = apr_bucket_transient_create(PHP_NO_DATA, 
sizeof(PHP_NO_DATA)-1);
-                               APR_BRIGADE_INSERT_HEAD(bb, eos);
-                       }
-               }
-               
-               php_apache_request_dtor(f TSRMLS_CC);
+    if (f->ctx == NULL) {
+        /* Initialize filter context */
+        f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
+        ctx->f = f;
+    }
+
+    /* Handle phpinfo/phpcredits/built-in images */
+    if (ctx->request_processed) {
+        return ap_pass_brigade(f->next, bb);
+    }
+
+    APR_BRIGADE_FOREACH(b, bb) {
+        zend_file_handle zfd;
+
+        if (!ctx->request_processed && APR_BUCKET_IS_FILE(b)) {
+            const char *path;
+            apr_bucket_brigade *prebb = bb;
+
+            /* Split the brigade into two brigades before and after
+             * the file bucket. Leave the "after the FILE" brigade
+             * in the original bb, so it gets passed outside of this
+             * loop. */
+            bb = apr_brigade_split(prebb, b);
+
+            /* Pass the "before the FILE" brigade here
+             * (if it's non-empty). */
+            if (!APR_BRIGADE_EMPTY(prebb)) {
+                apr_status_t rv;
+                rv = ap_pass_brigade(f->next, prebb);
+                /* XXX: destroy the prebb, since we know we're
+                 * done with it? */
+                if (rv != APR_SUCCESS) {
+                    php_handle_aborted_connection();
+                }
+            }
+
+            SG(server_context) = ctx;
+            apply_config(conf);
+            php_apache_request_ctor(f, ctx TSRMLS_CC);
+
+            apr_file_name_get(&path, ((apr_bucket_file *) b->data)->fd);
+            zfd.type = ZEND_HANDLE_FILENAME;
+            zfd.filename = (char *) path;
+            zfd.free_filename = 0;
+            zfd.opened_path = NULL;
+
+            php_execute_script(&zfd TSRMLS_CC);
+            php_apache_request_dtor(f TSRMLS_CC);
+            
+            ctx->request_processed = 1;
+
+            /* Delete the FILE bucket from the brigade. */
+            apr_bucket_delete(b);
+        }
+    }
 
-               SG(server_context) = 0;
-               /* Pass EOS bucket to next filter to signal end of request */
-               eos = apr_bucket_eos_create();
-               APR_BRIGADE_INSERT_TAIL(bb, eos);
-               
-               return ap_pass_brigade(f->next, bb);
-       } else
-               apr_brigade_destroy(bb);
-
-       return APR_SUCCESS;
+    /* Pass whatever is left on the brigade. */
+    return ap_pass_brigade(f->next, bb);
 }
 
 static apr_status_t
Index: sapi/apache2filter/php_apache.h
===================================================================
RCS file: /repository/php4/sapi/apache2filter/php_apache.h,v
retrieving revision 1.10
diff -u -u -r1.10 php_apache.h
--- sapi/apache2filter/php_apache.h     28 Feb 2002 08:27:20 -0000      1.10
+++ sapi/apache2filter/php_apache.h     10 Apr 2002 19:24:33 -0000
@@ -21,7 +21,6 @@
 
 typedef struct php_struct {
        int state;
-       apr_bucket_brigade *bb;
        ap_filter_t *f;
        /* Length of post_data buffer */
        int post_len;
@@ -29,6 +28,8 @@
        int post_idx;
        /* Buffer for request body filter */
        char *post_data;
+    /* Whether or not we've processed PHP in the output filters yet. */
+    int request_processed;
 } php_struct;
 
 int php_apache_register_module(void);

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to