Skip to content

Commit 47189ca

Browse files
committed
Fix bug #50087: NSAPI performance improvements
1 parent 473d754 commit 47189ca

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ PHP NEWS
2020
- Fixed memory leak in extension loading when an error occurs on Windows.
2121
(Pierre)
2222

23+
- Fixed bug #50087 (NSAPI performance improvements). (Uwe Schindler)
2324
- Fixed bug #50152 (ReflectionClass::hasProperty behaves like isset() not
2425
property_exists). (Felipe)
2526
- Fixed bug #50146 (property_exists: Closure object cannot have properties).

sapi/nsapi/nsapi.c

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ static size_t nsapi_client_size = sizeof(nsapi_client)/sizeof(nsapi_client[0]);
131131
/* this parameters to "Service"/"Error" are NSAPI ones which should not be php.ini keys and are excluded */
132132
static char *nsapi_exclude_from_ini_entries[] = { "fn", "type", "method", "directive", "code", "reason", "script", "bucket", NULL };
133133

134-
static char *nsapi_strdup(char *str)
135-
{
136-
if (str != NULL) {
137-
return STRDUP(str);
138-
}
139-
return NULL;
140-
}
141-
142134
static void nsapi_free(void *addr)
143135
{
144136
if (addr != NULL) {
@@ -500,7 +492,7 @@ static int php_nsapi_remove_header(sapi_header_struct *sapi_header TSRMLS_DC)
500492
nsapi_request_context *rc = (nsapi_request_context *)SG(server_context);
501493

502494
/* copy the header, because NSAPI needs reformatting and we do not want to change the parameter */
503-
header_name = nsapi_strdup(sapi_header->header);
495+
header_name = pool_strdup(rc->sn->pool, sapi_header->header);
504496

505497
/* extract name, this works, if only the header without ':' is given, too */
506498
if (p = strchr(header_name, ':')) {
@@ -514,7 +506,7 @@ static int php_nsapi_remove_header(sapi_header_struct *sapi_header TSRMLS_DC)
514506

515507
/* remove the header */
516508
param_free(pblock_remove(header_name, rc->rq->srvhdrs));
517-
nsapi_free(header_name);
509+
pool_free(rc->sn->pool, header_name);
518510

519511
return ZEND_HASH_APPLY_KEEP;
520512
}
@@ -538,7 +530,7 @@ static int sapi_nsapi_header_handler(sapi_header_struct *sapi_header, sapi_heade
538530
case SAPI_HEADER_ADD:
539531
case SAPI_HEADER_REPLACE:
540532
/* copy the header, because NSAPI needs reformatting and we do not want to change the parameter */
541-
header_name = nsapi_strdup(sapi_header->header);
533+
header_name = pool_strdup(rc->sn->pool, sapi_header->header);
542534

543535
/* split header and align pointer for content */
544536
header_content = strchr(header_name, ':');
@@ -561,7 +553,7 @@ static int sapi_nsapi_header_handler(sapi_header_struct *sapi_header, sapi_heade
561553
pblock_nvinsert(header_name, header_content, rc->rq->srvhdrs);
562554
}
563555

564-
nsapi_free(header_name);
556+
pool_free(rc->sn->pool, header_name);
565557
return SAPI_HEADER_ADD;
566558

567559
default:
@@ -750,28 +742,25 @@ static void sapi_nsapi_register_server_variables(zval *track_vars_array TSRMLS_D
750742

751743
/* Create full Request-URI & Script-Name */
752744
if (SG(request_info).request_uri) {
745+
pos = strlen(SG(request_info).request_uri);
746+
753747
if (SG(request_info).query_string) {
754748
spprintf(&value, 0, "%s?%s", SG(request_info).request_uri, SG(request_info).query_string);
755749
if (value) {
756750
php_register_variable("REQUEST_URI", value, track_vars_array TSRMLS_CC);
757751
efree(value);
758752
}
759753
} else {
760-
php_register_variable("REQUEST_URI", SG(request_info).request_uri, track_vars_array TSRMLS_CC);
754+
php_register_variable_safe("REQUEST_URI", SG(request_info).request_uri, pos, track_vars_array TSRMLS_CC);
761755
}
762756

763-
if (value = nsapi_strdup(SG(request_info).request_uri)) {
764-
if (rc->path_info) {
765-
pos = strlen(SG(request_info).request_uri) - strlen(rc->path_info);
766-
if (pos>=0) {
767-
value[pos] = '\0';
768-
} else {
769-
value[0]='\0';
770-
}
757+
if (rc->path_info) {
758+
pos -= strlen(rc->path_info);
759+
if (pos<0) {
760+
pos = 0;
771761
}
772-
php_register_variable("SCRIPT_NAME", value, track_vars_array TSRMLS_CC);
773-
nsapi_free(value);
774762
}
763+
php_register_variable_safe("SCRIPT_NAME", SG(request_info).request_uri, pos, track_vars_array TSRMLS_CC);
775764
}
776765
php_register_variable("SCRIPT_FILENAME", SG(request_info).path_translated, track_vars_array TSRMLS_CC);
777766

@@ -1014,21 +1003,25 @@ int NSAPI_PUBLIC php5_execute(pblock *pb, Session *sn, Request *rq)
10141003
}
10151004
}
10161005

1017-
request_context = (nsapi_request_context *)MALLOC(sizeof(nsapi_request_context));
1006+
request_context = (nsapi_request_context *)pool_malloc(sn->pool, sizeof(nsapi_request_context));
1007+
if (!request_context) {
1008+
log_error(LOG_CATASTROPHE, pblock_findval("fn", pb), sn, rq, "Insufficient memory to process PHP request!");
1009+
return REQ_ABORTED;
1010+
}
10181011
request_context->pb = pb;
10191012
request_context->sn = sn;
10201013
request_context->rq = rq;
10211014
request_context->read_post_bytes = 0;
10221015
request_context->fixed_script = fixed_script;
10231016
request_context->http_error = (error_directive) ? rq->status_num : 0;
1024-
request_context->path_info = nsapi_strdup(path_info);
1017+
request_context->path_info = path_info;
10251018

10261019
SG(server_context) = request_context;
1027-
SG(request_info).query_string = nsapi_strdup(query_string);
1028-
SG(request_info).request_uri = nsapi_strdup(uri);
1029-
SG(request_info).request_method = nsapi_strdup(request_method);
1030-
SG(request_info).path_translated = nsapi_strdup(path_translated);
1031-
SG(request_info).content_type = nsapi_strdup(content_type);
1020+
SG(request_info).query_string = query_string;
1021+
SG(request_info).request_uri = uri;
1022+
SG(request_info).request_method = request_method;
1023+
SG(request_info).path_translated = path_translated;
1024+
SG(request_info).content_type = content_type;
10321025
SG(request_info).content_length = (content_length == NULL) ? 0 : strtoul(content_length, 0, 0);
10331026
SG(sapi_headers).http_response_code = (error_directive) ? rq->status_num : 200;
10341027

@@ -1068,14 +1061,7 @@ int NSAPI_PUBLIC php5_execute(pblock *pb, Session *sn, Request *rq)
10681061
}
10691062
}
10701063

1071-
nsapi_free(request_context->path_info);
1072-
nsapi_free(SG(request_info).query_string);
1073-
nsapi_free(SG(request_info).request_uri);
1074-
nsapi_free((void*)(SG(request_info).request_method));
1075-
nsapi_free(SG(request_info).path_translated);
1076-
nsapi_free((void*)(SG(request_info).content_type));
1077-
1078-
FREE(request_context);
1064+
pool_free(sn->pool, request_context);
10791065
SG(server_context) = NULL;
10801066

10811067
return retval;

0 commit comments

Comments
 (0)