@@ -122,7 +122,6 @@ typedef struct {
122
122
nxt_app_request_t r ;
123
123
nxt_str_t script ;
124
124
nxt_app_wmsg_t * wmsg ;
125
- nxt_mp_t * mem_pool ;
126
125
127
126
size_t body_preread_size ;
128
127
} nxt_php_run_ctx_t ;
@@ -137,15 +136,6 @@ static nxt_str_t nxt_php_root;
137
136
static nxt_str_t nxt_php_script ;
138
137
static nxt_str_t nxt_php_index = nxt_string ("index.php" );
139
138
140
- static void
141
- nxt_php_strdup (nxt_str_t * dst , nxt_str_t * src )
142
- {
143
- dst -> start = malloc (src -> length + 1 );
144
- nxt_memcpy (dst -> start , src -> start , src -> length );
145
- dst -> start [src -> length ] = '\0' ;
146
-
147
- dst -> length = src -> length ;
148
- }
149
139
150
140
static void
151
141
nxt_php_str_trim_trail (nxt_str_t * str , u_char t )
@@ -183,15 +173,24 @@ NXT_EXPORT nxt_application_module_t nxt_app_module = {
183
173
};
184
174
185
175
176
+ nxt_inline u_char *
177
+ nxt_realpath (const void * c )
178
+ {
179
+ return (u_char * ) realpath (c , NULL );
180
+ }
181
+
182
+
186
183
static nxt_int_t
187
184
nxt_php_init (nxt_task_t * task , nxt_common_app_conf_t * conf )
188
185
{
186
+ u_char * p ;
187
+ nxt_str_t rpath ;
189
188
nxt_str_t * root , * path , * script , * index ;
190
189
nxt_php_app_conf_t * c ;
191
190
192
191
c = & conf -> u .php ;
193
192
194
- if (c -> root . length == 0 ) {
193
+ if (c -> root == NULL ) {
195
194
nxt_log_emerg (task -> log , "php root is empty" );
196
195
return NXT_ERROR ;
197
196
}
@@ -201,30 +200,57 @@ nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
201
200
script = & nxt_php_script ;
202
201
index = & nxt_php_index ;
203
202
204
- nxt_php_strdup (root , & c -> root );
203
+ root -> start = nxt_realpath (c -> root );
204
+ if (nxt_slow_path (root -> start == NULL )) {
205
+ nxt_log_emerg (task -> log , "root realpath(%s) failed %E" ,
206
+ c -> root , nxt_errno );
207
+ return NXT_ERROR ;
208
+ }
209
+
210
+ root -> length = nxt_strlen (root -> start );
205
211
206
212
nxt_php_str_trim_trail (root , '/' );
207
213
208
214
if (c -> script .length > 0 ) {
209
215
nxt_php_str_trim_lead (& c -> script , '/' );
210
216
211
- path -> length = root -> length + c -> script .length + 1 ;
212
- path -> start = malloc (path -> length + 1 );
217
+ path -> length = root -> length + 1 + c -> script .length ;
218
+ path -> start = nxt_malloc (path -> length );
219
+ if (nxt_slow_path (path -> start == NULL )) {
220
+ return NXT_ERROR ;
221
+ }
213
222
214
- nxt_memcpy (path -> start , root -> start , root -> length );
215
- path -> start [ root -> length ] = '/' ;
223
+ p = nxt_cpymem (path -> start , root -> start , root -> length );
224
+ * p ++ = '/' ;
216
225
217
- nxt_memcpy (path -> start + root -> length + 1 ,
218
- c -> script .start , c -> script .length );
226
+ nxt_memcpy (p , c -> script .start , c -> script .length );
219
227
220
- path -> start [path -> length ] = '\0' ;
228
+ rpath .start = nxt_realpath (path -> start );
229
+ if (nxt_slow_path (rpath .start == NULL )) {
230
+ nxt_log_emerg (task -> log , "script realpath(%V) failed %E" ,
231
+ path , nxt_errno );
232
+ return NXT_ERROR ;
233
+ }
221
234
235
+ rpath .length = nxt_strlen (rpath .start );
236
+
237
+ if (!nxt_str_start (& rpath , root -> start , root -> length )) {
238
+ nxt_log_emerg (task -> log , "script is not under php root" );
239
+ return NXT_ERROR ;
240
+ }
241
+
242
+ nxt_free (path -> start );
243
+
244
+ * path = rpath ;
222
245
223
246
script -> length = c -> script .length + 1 ;
224
- script -> start = malloc (script -> length + 1 );
247
+ script -> start = nxt_malloc (script -> length );
248
+ if (nxt_slow_path (script -> start == NULL )) {
249
+ return NXT_ERROR ;
250
+ }
251
+
225
252
script -> start [0 ] = '/' ;
226
253
nxt_memcpy (script -> start + 1 , c -> script .start , c -> script .length );
227
- script -> start [script -> length ] = '\0' ;
228
254
229
255
nxt_log_error (NXT_LOG_INFO , task -> log ,
230
256
"(ABS_MODE) php script \"%V\" root: \"%V\"" ,
@@ -236,7 +262,13 @@ nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
236
262
}
237
263
238
264
if (c -> index .length > 0 ) {
239
- nxt_php_strdup (index , & c -> index );
265
+ index -> length = c -> index .length ;
266
+ index -> start = nxt_malloc (index -> length );
267
+ if (nxt_slow_path (index -> start == NULL )) {
268
+ return NXT_ERROR ;
269
+ }
270
+
271
+ nxt_memcpy (index -> start , c -> index .start , c -> index .length );
240
272
}
241
273
242
274
sapi_startup (& nxt_php_sapi_module );
@@ -299,24 +331,18 @@ nxt_php_read_request(nxt_task_t *task, nxt_app_rmsg_t *rmsg,
299
331
300
332
ctx -> script .length = nxt_php_root .length + h -> path .length +
301
333
script_name .length ;
302
- ctx -> script .start = nxt_mp_nget (ctx -> mem_pool ,
303
- ctx -> script .length + 1 );
304
-
305
- p = ctx -> script .start ;
306
-
307
- nxt_memcpy (p , nxt_php_root .start , nxt_php_root .length );
308
- p += nxt_php_root .length ;
334
+ p = ctx -> script .start = nxt_malloc (ctx -> script .length );
335
+ if (nxt_slow_path (p == NULL )) {
336
+ return NXT_ERROR ;
337
+ }
309
338
310
- nxt_memcpy (p , h -> path .start , h -> path .length );
311
- p += h -> path .length ;
339
+ p = nxt_cpymem (p , nxt_php_root .start , nxt_php_root .length );
340
+ p = nxt_cpymem ( p , h -> path .start , h -> path . length ) ;
312
341
313
342
if (script_name .length > 0 ) {
314
343
nxt_memcpy (p , script_name .start , script_name .length );
315
- p += script_name .length ;
316
344
}
317
345
318
- p [0 ] = '\0' ;
319
-
320
346
} else {
321
347
ctx -> script = nxt_php_path ;
322
348
}
@@ -357,18 +383,12 @@ nxt_php_run(nxt_task_t *task,
357
383
nxt_php_run_ctx_t run_ctx ;
358
384
nxt_app_request_header_t * h ;
359
385
360
- if (nxt_php_root .length == 0 ) {
361
- return NXT_ERROR ;
362
- }
363
-
364
386
nxt_memzero (& run_ctx , sizeof (run_ctx ));
365
387
366
388
run_ctx .task = task ;
367
389
run_ctx .rmsg = rmsg ;
368
390
run_ctx .wmsg = wmsg ;
369
391
370
- run_ctx .mem_pool = nxt_mp_create (1024 , 128 , 256 , 32 );
371
-
372
392
h = & run_ctx .r .header ;
373
393
374
394
rc = nxt_php_read_request (task , rmsg , & run_ctx );
@@ -410,6 +430,7 @@ nxt_php_run(nxt_task_t *task,
410
430
411
431
if (nxt_slow_path (php_request_startup () == FAILURE )) {
412
432
nxt_debug (task , "php_request_startup() failed" );
433
+ rc = NXT_ERROR ;
413
434
goto fail ;
414
435
}
415
436
@@ -418,15 +439,15 @@ nxt_php_run(nxt_task_t *task,
418
439
419
440
nxt_app_msg_flush (task , wmsg , 1 );
420
441
421
- nxt_mp_destroy (run_ctx .mem_pool );
422
-
423
- return NXT_OK ;
442
+ rc = NXT_OK ;
424
443
425
444
fail :
426
445
427
- nxt_mp_destroy (run_ctx .mem_pool );
446
+ if (run_ctx .script .start != nxt_php_path .start ) {
447
+ nxt_free (run_ctx .script .start );
448
+ }
428
449
429
- return NXT_ERROR ;
450
+ return rc ;
430
451
}
431
452
432
453
@@ -750,5 +771,9 @@ nxt_php_log_message(char *message
750
771
#endif
751
772
)
752
773
{
753
- return ;
774
+ nxt_php_run_ctx_t * ctx ;
775
+
776
+ ctx = SG (server_context );
777
+
778
+ nxt_log (ctx -> task , NXT_LOG_NOTICE , "php message: %s" , message );
754
779
}
0 commit comments