@@ -122,7 +122,6 @@ typedef struct {
122122 nxt_app_request_t r ;
123123 nxt_str_t script ;
124124 nxt_app_wmsg_t * wmsg ;
125- nxt_mp_t * mem_pool ;
126125
127126 size_t body_preread_size ;
128127} nxt_php_run_ctx_t ;
@@ -137,15 +136,6 @@ static nxt_str_t nxt_php_root;
137136static nxt_str_t nxt_php_script ;
138137static nxt_str_t nxt_php_index = nxt_string ("index.php" );
139138
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- }
149139
150140static void
151141nxt_php_str_trim_trail (nxt_str_t * str , u_char t )
@@ -183,15 +173,24 @@ NXT_EXPORT nxt_application_module_t nxt_app_module = {
183173};
184174
185175
176+ nxt_inline u_char *
177+ nxt_realpath (const void * c )
178+ {
179+ return (u_char * ) realpath (c , NULL );
180+ }
181+
182+
186183static nxt_int_t
187184nxt_php_init (nxt_task_t * task , nxt_common_app_conf_t * conf )
188185{
186+ u_char * p ;
187+ nxt_str_t rpath ;
189188 nxt_str_t * root , * path , * script , * index ;
190189 nxt_php_app_conf_t * c ;
191190
192191 c = & conf -> u .php ;
193192
194- if (c -> root . length == 0 ) {
193+ if (c -> root == NULL ) {
195194 nxt_log_emerg (task -> log , "php root is empty" );
196195 return NXT_ERROR ;
197196 }
@@ -201,30 +200,57 @@ nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
201200 script = & nxt_php_script ;
202201 index = & nxt_php_index ;
203202
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 );
205211
206212 nxt_php_str_trim_trail (root , '/' );
207213
208214 if (c -> script .length > 0 ) {
209215 nxt_php_str_trim_lead (& c -> script , '/' );
210216
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+ }
213222
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 ++ = '/' ;
216225
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 );
219227
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+ }
221234
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 ;
222245
223246 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+
225252 script -> start [0 ] = '/' ;
226253 nxt_memcpy (script -> start + 1 , c -> script .start , c -> script .length );
227- script -> start [script -> length ] = '\0' ;
228254
229255 nxt_log_error (NXT_LOG_INFO , task -> log ,
230256 "(ABS_MODE) php script \"%V\" root: \"%V\"" ,
@@ -236,7 +262,13 @@ nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
236262 }
237263
238264 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 );
240272 }
241273
242274 sapi_startup (& nxt_php_sapi_module );
@@ -299,24 +331,18 @@ nxt_php_read_request(nxt_task_t *task, nxt_app_rmsg_t *rmsg,
299331
300332 ctx -> script .length = nxt_php_root .length + h -> path .length +
301333 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+ }
309338
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 ) ;
312341
313342 if (script_name .length > 0 ) {
314343 nxt_memcpy (p , script_name .start , script_name .length );
315- p += script_name .length ;
316344 }
317345
318- p [0 ] = '\0' ;
319-
320346 } else {
321347 ctx -> script = nxt_php_path ;
322348 }
@@ -357,18 +383,12 @@ nxt_php_run(nxt_task_t *task,
357383 nxt_php_run_ctx_t run_ctx ;
358384 nxt_app_request_header_t * h ;
359385
360- if (nxt_php_root .length == 0 ) {
361- return NXT_ERROR ;
362- }
363-
364386 nxt_memzero (& run_ctx , sizeof (run_ctx ));
365387
366388 run_ctx .task = task ;
367389 run_ctx .rmsg = rmsg ;
368390 run_ctx .wmsg = wmsg ;
369391
370- run_ctx .mem_pool = nxt_mp_create (1024 , 128 , 256 , 32 );
371-
372392 h = & run_ctx .r .header ;
373393
374394 rc = nxt_php_read_request (task , rmsg , & run_ctx );
@@ -410,6 +430,7 @@ nxt_php_run(nxt_task_t *task,
410430
411431 if (nxt_slow_path (php_request_startup () == FAILURE )) {
412432 nxt_debug (task , "php_request_startup() failed" );
433+ rc = NXT_ERROR ;
413434 goto fail ;
414435 }
415436
@@ -418,15 +439,15 @@ nxt_php_run(nxt_task_t *task,
418439
419440 nxt_app_msg_flush (task , wmsg , 1 );
420441
421- nxt_mp_destroy (run_ctx .mem_pool );
422-
423- return NXT_OK ;
442+ rc = NXT_OK ;
424443
425444fail :
426445
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+ }
428449
429- return NXT_ERROR ;
450+ return rc ;
430451}
431452
432453
@@ -750,5 +771,9 @@ nxt_php_log_message(char *message
750771#endif
751772)
752773{
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 );
754779}
0 commit comments