- fix too aggressive caching behavour of HttpResponse
authorMichael Wallner <mike@php.net>
Tue, 28 Mar 2006 09:50:47 +0000 (09:50 +0000)
committerMichael Wallner <mike@php.net>
Tue, 28 Mar 2006 09:50:47 +0000 (09:50 +0000)
http_response_object.c
http_send_api.c
package2.xml
tests/HttpResponse_001.phpt
tests/HttpResponse_003.phpt
tests/HttpResponse_004.phpt

index 12a9849427d8f376efc58fe2a0476db8680f60ef..918647dc721db4c14269f865728491afcefc6892 100644 (file)
@@ -1086,21 +1086,24 @@ PHP_METHOD(HttpResponse, send)
        /* caching */
        if (zval_is_true(GET_STATIC_PROP(cache))) {
                zval *cctl, *cctl_p, *etag, *etag_p, *lmod, *lmod_p;
-
+               
                etag = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag), &etag_p);
                lmod = convert_to_type_ex(IS_LONG, GET_STATIC_PROP(lastModified), &lmod_p);
                cctl = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(cacheControl), &cctl_p);
-
-               http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
-               http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : HTTP_GET_REQUEST_TIME(), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
-
+               
+               if (Z_LVAL_P(lmod) || Z_STRLEN_P(etag)) {
+                       http_send_cache_control(Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
+                       if (Z_STRLEN_P(etag)) {
+                               http_send_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag));
+                       }
+                       if (Z_LVAL_P(lmod)) {
+                               http_send_last_modified(Z_LVAL_P(lmod));
+                       }
+               }
+               
                if (etag_p) zval_ptr_dtor(&etag_p);
                if (lmod_p) zval_ptr_dtor(&lmod_p);
                if (cctl_p) zval_ptr_dtor(&cctl_p);
-       
-               if (php_ob_handler_used("blackhole" TSRMLS_CC)) {
-                       RETURN_TRUE;
-               }
        }
 
        /* content type */
@@ -1156,7 +1159,7 @@ PHP_METHOD(HttpResponse, send)
                case SEND_DATA:
                {
                        zval *zdata_p, *zdata = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(data), &zdata_p);
-                       RETVAL_SUCCESS(http_send_data_ex(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), 1));
+                       RETVAL_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
                        if (zdata_p) zval_ptr_dtor(&zdata_p);
                        return;
                }
@@ -1167,7 +1170,7 @@ PHP_METHOD(HttpResponse, send)
                        zval *the_stream_p, *the_stream = convert_to_type_ex(IS_LONG, GET_STATIC_PROP(stream), &the_stream_p);
                        the_stream->type = IS_RESOURCE;
                        php_stream_from_zval(the_real_stream, &the_stream);
-                       RETVAL_SUCCESS(http_send_stream_ex(the_real_stream, 0, 1));
+                       RETVAL_SUCCESS(http_send_stream(the_real_stream));
                        if (the_stream_p) zval_ptr_dtor(&the_stream_p);
                        return;
                }
@@ -1175,7 +1178,7 @@ PHP_METHOD(HttpResponse, send)
                default:
                {
                        zval *file_p;
-                       RETVAL_SUCCESS(http_send_file_ex(Z_STRVAL_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(file), &file_p)), 1));
+                       RETVAL_SUCCESS(http_send_file(Z_STRVAL_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(file), &file_p))));
                        if (file_p) zval_ptr_dtor(&file_p);
                        return;
                }
index f8bb733e9fc9aefde6e8192c0b781ea6cb1132c8..82bdff01402d8108d8eaca91a95e7fe166c02f22 100644 (file)
@@ -238,18 +238,19 @@ PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char *
 {
        STATUS status;
        char *etag_header;
+       size_t etag_header_len;
 
        if (!etag_len){
                http_error_ex(HE_WARNING, HTTP_E_HEADER, "Attempt to send empty ETag (previous: %s)\n", HTTP_G->send.unquoted_etag);
                return FAILURE;
        }
 
+       etag_header_len = spprintf(&etag_header, 0, "ETag: \"%s\"", etag);
+       status = http_send_header_string_ex(etag_header, etag_header_len, 1);
+       
        /* remember */
        STR_SET(HTTP_G->send.unquoted_etag, estrndup(etag, etag_len));
 
-       etag_len = spprintf(&etag_header, 0, "ETag: \"%s\"", etag);
-       status = http_send_header_string_ex(etag_header, etag_len, 1);
-       
        if (sent_header) {
                *sent_header = etag_header;
        } else {
@@ -299,7 +300,6 @@ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_s
        void *s = NULL;
        HashTable ranges;
        http_range_status range_status;
-       int cache_etag = http_interrupt_ob_etaghandler();
        
        if (!data_ptr) {
                return FAILURE;
@@ -408,12 +408,16 @@ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_s
                case RANGE_NO:
                {
                        zend_hash_destroy(&ranges);
-                       
+
                        /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
-                       if (!no_cache && cache_etag) {
+                       if (!no_cache && (http_interrupt_ob_etaghandler() || (HTTP_G->send.unquoted_etag != NULL))) {
                                char *etag = NULL;
                                
-                               if ((etag = http_etag(data_ptr, data_size, data_mode))) {
+                               if (HTTP_G->send.unquoted_etag) {
+                                       etag = estrdup(HTTP_G->send.unquoted_etag);
+                               }
+                               
+                               if (etag || (etag = http_etag(data_ptr, data_size, data_mode))) {
                                        char *sent_header = NULL;
                                        
                                        http_send_etag_ex(etag, strlen(etag), &sent_header);
@@ -421,6 +425,8 @@ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_s
                                                return http_exit_ex(304, sent_header, NULL, 0);
                                        } else {
                                                STR_FREE(sent_header);
+                                               /* no caching for Last-Modified if ETags really don't match */
+                                               no_cache = http_got_server_var("HTTP_IF_NONE_MATCH");
                                        }
                                        efree(etag);
                                }
index 647e072f9680aac4725c199c697a40fd21758c0a..bdc6fd11c40f9064fa39b0d15eb6f8f9d415d7e4 100644 (file)
@@ -49,6 +49,7 @@ HttpResponse
 * Fixed Bug #7192: Build against libcurl >= 7.15.2 fails
 * Fixed access of super globals
 * Fixed sending userspace streams
+* Fixed too aggressive caching behavour of HttpResponse
 ]]></notes>
  <contents>
   <dir name="/">
index 56b1913fa3d8a959956f51d6646f9540abe52514..21db4588423de6a6f1fe04d9f03c4fcad71c4d60 100644 (file)
@@ -15,11 +15,11 @@ HttpResponse::send();
 ?>
 --EXPECTF--
 X-Powered-By: PHP/%s
-ETag: "3858f62230ac3c915f300c664312c63f"
 Cache-Control: public, must-revalidate, max-age=3600
 Last-Modified: %s, %d %s 20%d %d:%d:%d GMT
 Content-Type: %s
 Accept-Ranges: bytes
+ETag: "3858f62230ac3c915f300c664312c63f"
 Content-Length: 6
 
 foobar
index 9780dda00cb1c67b7cfcbcf028c89a582aeecab1..22f46749a1e3721e3a063b41f55f3366ed2bc394 100644 (file)
@@ -19,11 +19,11 @@ HttpResponse::send();
 ?>
 --EXPECTF--
 X-Powered-By: PHP/%s
-ETag: "%s"
 Cache-Control: public, must-revalidate, max-age=3600
 Last-Modified: %s, %d %s 20%d %d:%d:%d GMT
 Content-Type: %s
 Accept-Ranges: bytes
+ETag: "%s"
 Content-Encoding: gzip
 Vary: Accept-Encoding
 
index e8a1d0f758b05279da903213bec4ed8022100f7c..4e6d30c2d77198500cff073074918110ebba4770 100644 (file)
@@ -5,22 +5,23 @@ HttpResponse - send cached gzipped data
 include 'skip.inc';
 checkcgi();
 checkmin(5.1);
-skpif(!http_support(HTTP_SUPPORT_ENCODINGS), "need zlib support");
+skipif(!http_support(HTTP_SUPPORT_ENCODINGS), "need zlib support");
 ?>
---ENV--
-HTTP_ACCEPT_ENCODING=gzip
-HTTP_IF_NONE_MATCH="80b285463881575891e86ba7bfecb4d0"
 --FILE--
 <?php
+$_SERVER["HTTP_IF_NONE_MATCH"] = '"900150983cd24fb0d6963f7d28e17f72"';
+$_SERVER["HTTP_ACCEPT_ENCODING"] = "gzip";
 HttpResponse::setGzip(true);
 HttpResponse::setCache(true);
 HttpResponse::setCacheControl('public', 3600);
-HttpResponse::setData(file_get_contents(__FILE__));
+HttpResponse::setData("abc");
 HttpResponse::send();
 ?>
 --EXPECTF--
 Status: 304
 X-Powered-By: PHP/%s
 Cache-Control: public, must-revalidate, max-age=3600
-ETag: "80b285463881575891e86ba7bfecb4d0"
-Content-type: %s
+Last-Modified: %s
+Content-Type: %s
+Accept-Ranges: bytes
+ETag: "900150983cd24fb0d6963f7d28e17f72"