Skip to content

Commit 4f3b619

Browse files
committed
- code cleanups
- cleanup progress data from session vars as soon as all post data has been readden (upload_progress.cleanup ini setting allows to disable this)
1 parent d352196 commit 4f3b619

15 files changed

+278
-27
lines changed

ext/session/php_session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ typedef struct _php_ps_globals {
166166

167167
php_session_rfc1867_progress *rfc1867_progress;
168168
zend_bool rfc1867_enabled; /* session.upload_progress.enabled */
169+
zend_bool rfc1867_cleanup; /* session.upload_progress.cleanup */
169170
smart_str rfc1867_prefix; /* session.upload_progress.prefix */
170171
smart_str rfc1867_name; /* session.upload_progress.name */
171172
long rfc1867_freq; /* session.upload_progress.freq */

ext/session/session.c

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ PHP_INI_BEGIN()
707707
/* Upload progress */
708708
STD_PHP_INI_BOOLEAN("session.upload_progress.enabled",
709709
"1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_enabled, php_ps_globals, ps_globals)
710+
STD_PHP_INI_BOOLEAN("session.upload_progress.cleanup",
711+
"1", ZEND_INI_PERDIR, OnUpdateBool, rfc1867_cleanup, php_ps_globals, ps_globals)
710712
STD_PHP_INI_ENTRY("session.upload_progress.prefix",
711713
"upload_progress_", ZEND_INI_PERDIR, OnUpdateSmartStr, rfc1867_prefix, php_ps_globals, ps_globals)
712714
STD_PHP_INI_ENTRY("session.upload_progress.name",
@@ -2057,17 +2059,30 @@ static const zend_module_dep session_deps[] = { /* {{{ */
20572059
* Upload hook handling *
20582060
************************ */
20592061

2060-
static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2062+
static zend_bool early_find_sid_in(zval *dest, int where, php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
20612063
{
20622064
zval **ppid;
20632065

2066+
if (!PG(http_globals)[where]) {
2067+
return 0;
2068+
}
2069+
2070+
if (zend_hash_find(Z_ARRVAL_P(PG(http_globals)[where]), PS(session_name), progress->sname_len+1, (void **)&ppid) == SUCCESS
2071+
&& Z_TYPE_PP(ppid) == IS_STRING) {
2072+
zval_dtor(dest);
2073+
ZVAL_ZVAL(dest, *ppid, 1, 0);
2074+
return 1;
2075+
}
2076+
2077+
return 0;
2078+
} /* }}} */
2079+
2080+
static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2081+
{
2082+
20642083
if (PS(use_cookies)) {
20652084
sapi_module.treat_data(PARSE_COOKIE, NULL, NULL TSRMLS_CC);
2066-
if (PG(http_globals)[TRACK_VARS_COOKIE]
2067-
&& zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_COOKIE]), PS(session_name), progress->sname_len+1, (void **)&ppid) == SUCCESS) {
2068-
zval_dtor(&progress->sid);
2069-
ZVAL_ZVAL(&progress->sid, *ppid, 1, 0);
2070-
convert_to_string(&progress->sid);
2085+
if (early_find_sid_in(&progress->sid, TRACK_VARS_COOKIE, progress TSRMLS_CC)) {
20712086
progress->apply_trans_sid = 0;
20722087
return;
20732088
}
@@ -2076,16 +2091,10 @@ static void php_session_rfc1867_early_find_sid(php_session_rfc1867_progress *pro
20762091
return;
20772092
}
20782093
sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
2079-
if (PG(http_globals)[TRACK_VARS_GET]
2080-
&& zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_GET]), PS(session_name), progress->sname_len+1, (void **)&ppid) == SUCCESS) {
2081-
2082-
zval_dtor(&progress->sid);
2083-
ZVAL_ZVAL(&progress->sid, *ppid, 1, 0);
2084-
convert_to_string(&progress->sid);
2085-
}
2094+
early_find_sid_in(&progress->sid, TRACK_VARS_GET, progress TSRMLS_CC);
20862095
} /* }}} */
20872096

2088-
static zend_bool php_session_rfc1867_check_cancel_upload(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2097+
static zend_bool php_check_cancel_upload(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
20892098
{
20902099
zval **progress_ary, **cancel_upload;
20912100

@@ -2098,8 +2107,7 @@ static zend_bool php_session_rfc1867_check_cancel_upload(php_session_rfc1867_pro
20982107
if (zend_hash_find(Z_ARRVAL_PP(progress_ary), "cancel_upload", sizeof("cancel_upload"), (void**)&cancel_upload) != SUCCESS) {
20992108
return 0;
21002109
}
2101-
return zend_is_true(*cancel_upload);
2102-
2110+
return Z_TYPE_PP(cancel_upload) == IS_BOOL && Z_LVAL_PP(cancel_upload);
21032111
} /* }}} */
21042112

21052113
static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, int force_update TSRMLS_DC) /* {{{ */
@@ -2126,14 +2134,22 @@ static void php_session_rfc1867_update(php_session_rfc1867_progress *progress, i
21262134
php_session_initialize(TSRMLS_C);
21272135
PS(session_status) = php_session_active;
21282136
IF_SESSION_VARS() {
2129-
if (!progress->cancel_upload && php_session_rfc1867_check_cancel_upload(progress TSRMLS_CC)) {
2130-
progress->cancel_upload = 1;
2131-
}
2137+
progress->cancel_upload = php_check_cancel_upload(progress TSRMLS_CC);
21322138
ZEND_SET_SYMBOL_WITH_LENGTH(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1, progress->data, 2, 0);
21332139
}
21342140
php_session_flush(TSRMLS_C);
21352141
} /* }}} */
21362142

2143+
static void php_session_rfc1867_cleanup(php_session_rfc1867_progress *progress TSRMLS_DC) /* {{{ */
2144+
{
2145+
php_session_initialize(TSRMLS_C);
2146+
PS(session_status) = php_session_active;
2147+
IF_SESSION_VARS() {
2148+
zend_hash_del(Z_ARRVAL_P(PS(http_session_vars)), progress->key.c, progress->key.len+1);
2149+
}
2150+
php_session_flush(TSRMLS_C);
2151+
} /* }}} */
2152+
21372153
static int php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra TSRMLS_DC) /* {{{ */
21382154
{
21392155
php_session_rfc1867_progress *progress;
@@ -2161,6 +2177,10 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
21612177
multipart_event_formdata *data = (multipart_event_formdata *) event_data;
21622178
size_t value_len;
21632179

2180+
if (Z_TYPE(progress->sid) && progress->key.c) {
2181+
break;
2182+
}
2183+
21642184
/* orig callback may have modified *data->newlength */
21652185
if (data->newlength) {
21662186
value_len = *data->newlength;
@@ -2174,7 +2194,6 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
21742194
if (name_len == progress->sname_len && memcmp(data->name, PS(session_name), name_len) == 0) {
21752195
zval_dtor(&progress->sid);
21762196
ZVAL_STRINGL(&progress->sid, (*data->value), value_len, 1);
2177-
convert_to_string(&progress->sid);
21782197

21792198
} else if (name_len == PS(rfc1867_name).len && memcmp(data->name, PS(rfc1867_name).c, name_len) == 0) {
21802199
smart_str_free(&progress->key);
@@ -2200,10 +2219,10 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
22002219
/* First FILE_START event, initializing data */
22012220
if (!progress->data) {
22022221

2203-
if (PS(rfc1867_freq) == 0) {
2204-
progress->update_step = 0;
2222+
if (PS(rfc1867_freq) >= 0) {
2223+
progress->update_step = PS(rfc1867_freq);
22052224
} else if (PS(rfc1867_freq) < 0) { // % of total size
2206-
progress->update_step = progress->content_length * PS(rfc1867_freq) / 100;
2225+
progress->update_step = progress->content_length * -PS(rfc1867_freq) / 100;
22072226
}
22082227
progress->next_update = 0;
22092228
progress->next_update_time = 0.0;
@@ -2241,7 +2260,7 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
22412260
add_assoc_null_ex(progress->current_file, "tmp_name", sizeof("tmp_name"));
22422261
add_assoc_long_ex(progress->current_file, "error", sizeof("error"), 0);
22432262

2244-
add_assoc_long_ex(progress->current_file, "done", sizeof("done"), 0);
2263+
add_assoc_bool_ex(progress->current_file, "done", sizeof("done"), 0);
22452264
add_assoc_long_ex(progress->current_file, "start_time", sizeof("start_time"), (long)time(NULL));
22462265
add_assoc_zval_ex(progress->current_file, "bytes_processed", sizeof("bytes_processed"), progress->current_file_bytes_processed);
22472266

@@ -2287,9 +2306,13 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
22872306
multipart_event_end *data = (multipart_event_end *) event_data;
22882307

22892308
if (Z_TYPE(progress->sid) && progress->key.c) {
2290-
add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
2291-
Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2292-
php_session_rfc1867_update(progress, 1 TSRMLS_CC);
2309+
if (PS(rfc1867_cleanup)) {
2310+
php_session_rfc1867_cleanup(progress TSRMLS_CC);
2311+
} else {
2312+
add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
2313+
Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
2314+
php_session_rfc1867_update(progress, 1 TSRMLS_CC);
2315+
}
22932316
php_rshutdown_session_globals(TSRMLS_C);
22942317
}
22952318

ext/session/tests/rfc1867.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ session.name=PHPSESSID
1010
session.use_cookies=1
1111
session.use_only_cookies=0
1212
session.upload_progress.enabled=1
13+
session.upload_progress.cleanup=0
1314
session.upload_progress.prefix=upload_progress_
1415
session.upload_progress.name=PHP_SESSION_UPLOAD_PROGRESS
1516
session.upload_progress.freq=1%
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
--TEST--
2+
session rfc1867
3+
--INI--
4+
file_uploads=1
5+
error_reporting=E_ALL&~E_NOTICE
6+
comment=debug builds show some additional E_NOTICE errors
7+
upload_max_filesize=1024
8+
session.save_path=
9+
session.name=PHPSESSID
10+
session.use_cookies=1
11+
session.use_only_cookies=0
12+
session.upload_progress.enabled=1
13+
session.upload_progress.cleanup=1
14+
session.upload_progress.prefix=upload_progress_
15+
session.upload_progress.name=PHP_SESSION_UPLOAD_PROGRESS
16+
session.upload_progress.freq=1%
17+
session.upload_progress.min_freq=0.000000001
18+
--SKIPIF--
19+
<?php include('skipif.inc'); ?>
20+
--COOKIE--
21+
PHPSESSID=rfc1867-tests
22+
--GET--
23+
PHPSESSID=rfc1867-tests-get
24+
--POST_RAW--
25+
Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737
26+
-----------------------------20896060251896012921717172737
27+
Content-Disposition: form-data; name="PHPSESSID"
28+
29+
rfc1867-tests-post
30+
-----------------------------20896060251896012921717172737
31+
Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"
32+
33+
rfc1867_cleanup.php
34+
-----------------------------20896060251896012921717172737
35+
Content-Disposition: form-data; name="file1"; filename="file1.txt"
36+
37+
1
38+
-----------------------------20896060251896012921717172737
39+
Content-Disposition: form-data; name="file2"; filename="file2.txt"
40+
41+
2
42+
-----------------------------20896060251896012921717172737--
43+
--FILE--
44+
<?php
45+
session_start();
46+
var_dump(session_id());
47+
var_dump(basename(__FILE__) == $_POST[ini_get("session.upload_progress.name")]);
48+
var_dump($_FILES);
49+
var_dump($_SESSION["upload_progress_" . basename(__FILE__)]);
50+
session_destroy();
51+
?>
52+
--EXPECTF--
53+
string(%d) "rfc1867-tests"
54+
bool(true)
55+
array(2) {
56+
[%u|b%"file1"]=>
57+
array(5) {
58+
[%u|b%"name"]=>
59+
%string|unicode%(9) "file1.txt"
60+
[%u|b%"type"]=>
61+
%string|unicode%(0) ""
62+
[%u|b%"tmp_name"]=>
63+
%string|unicode%(%d) "%s"
64+
[%u|b%"error"]=>
65+
int(0)
66+
[%u|b%"size"]=>
67+
int(1)
68+
}
69+
[%u|b%"file2"]=>
70+
array(5) {
71+
[%u|b%"name"]=>
72+
%string|unicode%(9) "file2.txt"
73+
[%u|b%"type"]=>
74+
%string|unicode%(0) ""
75+
[%u|b%"tmp_name"]=>
76+
%string|unicode%(%d) "%s"
77+
[%u|b%"error"]=>
78+
int(0)
79+
[%u|b%"size"]=>
80+
int(1)
81+
}
82+
}
83+
NULL

ext/session/tests/rfc1867_disabled.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ session.name=PHPSESSID
1010
session.use_cookies=1
1111
session.use_only_cookies=0
1212
session.upload_progress.enabled=0
13+
session.upload_progress.cleanup=0
1314
session.upload_progress.prefix=upload_progress_
1415
session.upload_progress.name=PHP_SESSION_UPLOAD_PROGRESS
1516
session.upload_progress.freq=1%

ext/session/tests/rfc1867_disabled_2.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ session.name=PHPSESSID
1010
session.use_cookies=1
1111
session.use_only_cookies=0
1212
session.upload_progress.enabled=1
13+
session.upload_progress.cleanup=0
1314
session.upload_progress.prefix=upload_progress_
1415
session.upload_progress.name=PHP_SESSION_UPLOAD_PROGRESS
1516
session.upload_progress.freq=1%

0 commit comments

Comments
 (0)