/* {{{ char *http_etag(void *, size_t, http_send_mode) */
PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
{
- php_stream_statbuf ssb;
- char ssb_buf[128] = {0};
- size_t ssb_len;
void *ctx = http_etag_init();
- switch (data_mode)
- {
- case SEND_DATA:
- http_etag_update(ctx, data_ptr, data_len);
- break;
-
- case SEND_RSRC:
- {
- if (php_stream_stat((php_stream *) data_ptr, &ssb)) {
- efree(ctx);
- return NULL;
- }
- ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", ssb.sb.st_mtime, ssb.sb.st_ino, ssb.sb.st_size);
- http_etag_update(ctx, ssb_buf, ssb_len);
+ if (data_mode == SEND_DATA) {
+ http_etag_update(ctx, data_ptr, data_len);
+ } else {
+ STATUS ss = FAILURE;
+ php_stream_statbuf ssb;
+
+ if (data_mode == SEND_RSRC) {
+ ss = php_stream_stat((php_stream *) data_ptr, &ssb);
+ } else {
+ ss = php_stream_stat_path((char *) data_ptr, &ssb);
}
- break;
-
- default:
- {
- if (php_stream_stat_path((char *) data_ptr, &ssb)) {
- efree(ctx);
- return NULL;
- }
- ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", ssb.sb.st_mtime, ssb.sb.st_ino, ssb.sb.st_size);
+
+ if (SUCCESS != ss) {
+ http_etag_free(&ctx);
+ return NULL;
+ } else {
+ size_t ssb_len;
+ char ssb_buf[128] = {0};
+
+ ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", (long) ssb.sb.st_mtime,
+ (long) ssb.sb.st_ino,
+ (long) ssb.sb.st_size);
http_etag_update(ctx, ssb_buf, ssb_len);
}
- break;
}
-
- return http_etag_finish(ctx);
+
+ return http_etag_finish(&ctx);
}
/* }}} */
return 1;
}
- quoted_etag = (char *) emalloc(strlen(etag) + 3);
- sprintf(quoted_etag, "\"%s\"", etag);
-
+ spprintf("ed_etag, 0, "\"%s\"", etag);
if (!strchr(Z_STRVAL_P(zetag), ',')) {
result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
} else {
result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
}
efree(quoted_etag);
+
return result;
}
/* }}} */
return SUCCESS;
}
+ /* start ob_etaghandler */
+ return http_start_ob_etaghandler();
+}
+/* }}} */
+
+PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D)
+{
/* already running? */
if (php_ob_handler_used("ob_etaghandler" TSRMLS_CC)) {
http_error(HE_WARNING, HTTP_E_RUNTIME, "ob_etaghandler can only be used once");
return FAILURE;
}
- /* we want the etag handler to run */
HTTP_G(etag).started = 1;
return php_start_ob_buffer_named("ob_etaghandler", HTTP_G(send).buffer_size, 1 TSRMLS_CC);
}
-/* }}} */
+
+PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D)
+{
+ if (HTTP_G(etag).started) {
+ HTTP_G(etag).started = 0;
+ http_etag_free(&HTTP_G(etag).ctx);
+ return 1;
+ }
+ return 0;
+}
/* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
-PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
+void _http_ob_etaghandler(char *output, uint output_len,
char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
{
/* passthru */
/* finish */
if (mode & PHP_OUTPUT_HANDLER_END) {
char *sent_header = NULL;
- char *etag = http_etag_finish(HTTP_G(etag).ctx);
+ char *etag = http_etag_finish(&HTTP_G(etag).ctx);
http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL));
http_send_etag_ex(etag, strlen(etag), &sent_header);
}
/* stop on-the-fly etag generation */
- if (cache_etag = HTTP_G(etag).started) {
- /* interrupt ob_etaghandler */
- HTTP_G(etag).started = 0;
- }
+ cache_etag = http_interrupt_ob_etaghandler();
/* enable partial dl and resume */
http_send_header_string("Accept-Ranges: bytes");
/* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
if (!no_cache && cache_etag) {
char *etag = NULL;
-
- if (!(etag = http_etag(data_ptr, data_size, data_mode))) {
- http_error(HE_NOTICE, HTTP_E_RUNTIME, "Failed to generate ETag for data source");
- } else {
+
+ if (etag = http_etag(data_ptr, data_size, data_mode)) {
char *sent_header = NULL;
http_send_etag_ex(etag, 32, &sent_header);
static inline void *_http_etag_init(TSRMLS_D)
{
void *ctx = NULL;
- long mode = INI_INT("http.etag_mode");
+ long mode = HTTP_G(etag).mode;
switch (mode)
{
return ctx;
}
+#define http_etag_free(cp) _http_etag_free((cp) TSRMLS_CC)
+static inline void _http_etag_free(void **ctx_ptr TSRMLS_DC)
+{
+ long mode = HTTP_G(etag).mode;
+
+ switch (mode)
+ {
+ case HTTP_ETAG_CRC32:
+ if (*((unsigned int **) ctx_ptr)) {
+ efree(*((unsigned int **) ctx_ptr));
+ *((unsigned int **) ctx_ptr) = NULL;
+ }
+ break;
+
+ case HTTP_ETAG_SHA1:
+ if (*((PHP_SHA1_CTX **) ctx_ptr)) {
+ efree(*((PHP_SHA1_CTX **) ctx_ptr));
+ *((PHP_SHA1_CTX **) ctx_ptr) = NULL;
+ }
+ break;
+
+ case HTTP_ETAG_MD5:
+#ifndef HTTP_HAVE_MHASH
+ default:
+#endif
+ if (*((PHP_MD5_CTX **) ctx_ptr)) {
+ efree(*((PHP_MD5_CTX **) ctx_ptr));
+ *((PHP_MD5_CTX **) ctx_ptr) = NULL;
+ }
+ break;
+
+#ifdef HTTP_HAVE_MHASH
+ default:
+ /* mhash gets already freed in http_etag_finish() */
+ if (*((MHASH *) ctx_ptr)) {
+ mhash_deinit(*((MHASH *) ctx_ptr), NULL);
+ *((MHASH *) ctx_ptr) = NULL;
+ }
+ break;
+#endif
+ }
+}
+
#define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
-static inline char *_http_etag_finish(void *ctx TSRMLS_DC)
+static inline char *_http_etag_finish(void **ctx_ptr TSRMLS_DC)
{
char *etag = NULL;
unsigned char digest[20];
- long mode = INI_INT("http.etag_mode");
+ long mode = HTTP_G(etag).mode;
switch (mode)
{
case HTTP_ETAG_CRC32:
- *((unsigned int *) ctx) = ~*((unsigned int *) ctx);
- etag = http_etag_digest((const unsigned char *) ctx, sizeof(unsigned int));
- efree(ctx);
+ **((unsigned int **) ctx_ptr) = ~**((unsigned int **) ctx_ptr);
+ etag = http_etag_digest(*((const unsigned char **) ctx_ptr), sizeof(unsigned int));
break;
case HTTP_ETAG_SHA1:
- PHP_SHA1Final(digest, ctx);
+ PHP_SHA1Final(digest, *((PHP_SHA1_CTX **) ctx_ptr));
etag = http_etag_digest(digest, 20);
- efree(ctx);
break;
case HTTP_ETAG_MD5:
#ifndef HTTP_HAVE_MHASH
default:
#endif
- PHP_MD5Final(digest, ctx);
+ PHP_MD5Final(digest, *((PHP_MD5_CTX **) ctx_ptr));
etag = http_etag_digest(digest, 16);
- efree(ctx);
break;
#ifdef HTTP_HAVE_MHASH
default:
{
- unsigned char *mhash_digest = mhash_end_m(ctx, http_etag_alloc_mhash_digest);
+ unsigned char *mhash_digest = mhash_end_m(*((MHASH *) ctx_ptr), http_etag_alloc_mhash_digest);
etag = http_etag_digest(mhash_digest, mhash_get_block_size(mode));
efree(mhash_digest);
+ /* avoid double free */
+ *((MHASH *) ctx_ptr) = NULL;
}
break;
#endif
}
+ http_etag_free(ctx_ptr);
+
return etag;
}
}
}
+#define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
+extern void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
+
#define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC);
#define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len, const char *cache_control, size_t cc_len TSRMLS_DC);
-#define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
-PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
+#define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C)
+PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D);
+#define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C)
+PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D);
#endif