fixed bug with mixed case boundaries
[m6w6/ext-apfd] / php_apfd.c
index 8270fcba990d84fba65f0aa2f2c6a385822012ea..0e1d749d5680fd9fc8c3d957702e83871b2d828e 100644 (file)
 #include "php_ini.h"
 #include "ext/standard/info.h"
 #include "SAPI.h"
+#include "rfc1867.h"
 #include "php_apfd.h"
 
+#if PHP_VERSION_ID >= 70000
+
+#define APFD_SG(t) &PG(http_globals)[t]
+static inline sapi_post_entry *apfd_get_post_entry(const char *ct_str, size_t ct_len)
+{
+       return zend_hash_str_find_ptr(&SG(known_post_content_types), ct_str, ct_len);
+}
+
+static inline zval *apfd_backup_files(void)
+{
+       if (SG(rfc1867_uploaded_files)) {
+               destroy_uploaded_files_hash();
+       }
+       return NULL;
+}
+
+static inline void apfd_update_files(zval *files)
+{
+       Z_TRY_ADDREF_P(APFD_SG(TRACK_VARS_FILES));
+       zend_hash_str_update(&EG(symbol_table), "_FILES", sizeof("_FILES")-1, APFD_SG(TRACK_VARS_FILES));
+}
+
+#else
+
+#define APFD_SG(t) PG(http_globals)[t]
+static inline sapi_post_entry *apfd_get_post_entry(const char *ct_str, size_t ct_len TSRMLS_DC)
+{
+       sapi_post_entry *post_entry;
+
+       if (SUCCESS == zend_hash_find(&SG(known_post_content_types), ct_str, ct_len+1, (void *) &post_entry)) {
+               return post_entry;
+       }
+       return NULL;
+}
+
+static inline zval *apfd_backup_files(TSRMLS_D)
+{
+       return APFD_SG(TRACK_VARS_FILES);
+}
+
+static inline void apfd_update_files(zval *files TSRMLS_DC)
+{
+       if (files != APFD_SG(TRACK_VARS_FILES) && APFD_SG(TRACK_VARS_FILES)) {
+               Z_ADDREF_P(APFD_SG(TRACK_VARS_FILES));
+               zend_hash_update(&EG(symbol_table), "_FILES", sizeof("_FILES"), &APFD_SG(TRACK_VARS_FILES), sizeof(zval *), NULL);
+               if (files) {
+                       zval_ptr_dtor(&files);
+               }
+       }
+}
+#endif
+
 PHP_RINIT_FUNCTION(apfd)
 {
        /* populate form data on non-POST requests */
        if (SG(request_info).request_method && strcasecmp(SG(request_info).request_method, "POST") && SG(request_info).content_type && *SG(request_info).content_type) {
-               char *ct_str = zend_str_tolower_dup(SG(request_info).content_type, strlen(SG(request_info).content_type));
-               size_t ct_end = strcspn(ct_str, ";, ");
+               char *ct_str, *ct_dup = estrdup(SG(request_info).content_type);
+               size_t ct_end = strcspn(ct_dup, ";, ");
                sapi_post_entry *post_entry = NULL;
-               char delim;
-
-               SG(request_info).content_type_dup = ct_str;
 
-               delim = ct_str[ct_end];
-               ct_str[ct_end] = '\0';
+               SG(request_info).content_type_dup = ct_dup;
 
-               if (SUCCESS == zend_hash_find(&SG(known_post_content_types), ct_str, ct_end+1, (void *) &post_entry)) {
-                       zval *files = PG(http_globals)[TRACK_VARS_FILES];
-
-                       ct_str[ct_end] = delim;
+               ct_str = zend_str_tolower_dup(ct_dup, ct_end);
+               if ((post_entry = apfd_get_post_entry(ct_str, ct_end TSRMLS_CC))) {
+                       zval *files = apfd_backup_files(TSRMLS_C);
 
                        if (post_entry) {
                                SG(request_info).post_entry = post_entry;
@@ -51,19 +99,14 @@ PHP_RINIT_FUNCTION(apfd)
                                sapi_module.default_post_reader(TSRMLS_C);
                        }
 
-                       sapi_handle_post(PG(http_globals)[TRACK_VARS_POST] TSRMLS_CC);
+                       sapi_handle_post(APFD_SG(TRACK_VARS_POST) TSRMLS_CC);
 
                        /*
                         * the rfc1867 handler is an awkward buddy
                         */
-                       if (files != PG(http_globals)[TRACK_VARS_FILES] && PG(http_globals)[TRACK_VARS_FILES]) {
-                               Z_ADDREF_P(PG(http_globals)[TRACK_VARS_FILES]);
-                               zend_hash_update(&EG(symbol_table), "_FILES", sizeof("_FILES"), &PG(http_globals)[TRACK_VARS_FILES], sizeof(zval *), NULL);
-                               if (files) {
-                                       zval_ptr_dtor(&files);
-                               }
-                       }
+                       apfd_update_files(files TSRMLS_CC);
                }
+               efree(ct_str);
 
                if (SG(request_info).content_type_dup) {
                        efree(SG(request_info).content_type_dup);