xref: /unit/src/nxt_php_sapi.c (revision 446)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
386Smax.romanov@nginx.com  * Copyright (C) Max Romanov
40Sigor@sysoev.ru  * Copyright (C) Valentin V. Bartenev
50Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
60Sigor@sysoev.ru  */
70Sigor@sysoev.ru 
80Sigor@sysoev.ru #include "php.h"
90Sigor@sysoev.ru #include "SAPI.h"
100Sigor@sysoev.ru #include "php_main.h"
110Sigor@sysoev.ru #include "php_variables.h"
120Sigor@sysoev.ru 
130Sigor@sysoev.ru #include <nxt_main.h>
14*446Sigor@sysoev.ru #include <nxt_router.h>
150Sigor@sysoev.ru 
160Sigor@sysoev.ru 
17142Smax.romanov@nginx.com static nxt_int_t nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf);
180Sigor@sysoev.ru 
1986Smax.romanov@nginx.com static nxt_int_t nxt_php_run(nxt_task_t *task,
2086Smax.romanov@nginx.com                       nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg);
210Sigor@sysoev.ru 
2286Smax.romanov@nginx.com #if PHP_MAJOR_VERSION >= 7
2386Smax.romanov@nginx.com #   define NXT_PHP7 1
2486Smax.romanov@nginx.com #   if PHP_MINOR_VERSION >= 1
2586Smax.romanov@nginx.com #       define NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 1
2686Smax.romanov@nginx.com #   else
2786Smax.romanov@nginx.com #       define NXT_HAVE_PHP_INTERRUPTS 1
2886Smax.romanov@nginx.com #   endif
2986Smax.romanov@nginx.com #   define NXT_HAVE_PHP_IGNORE_CWD 1
3086Smax.romanov@nginx.com #else
3186Smax.romanov@nginx.com #   define NXT_HAVE_PHP_INTERRUPTS 1
3286Smax.romanov@nginx.com #   if PHP_MINOR_VERSION >= 4
3386Smax.romanov@nginx.com #       define NXT_HAVE_PHP_IGNORE_CWD 1
3486Smax.romanov@nginx.com #   endif
3586Smax.romanov@nginx.com #endif
360Sigor@sysoev.ru 
370Sigor@sysoev.ru static int nxt_php_startup(sapi_module_struct *sapi_module);
380Sigor@sysoev.ru static int nxt_php_send_headers(sapi_headers_struct *sapi_headers);
390Sigor@sysoev.ru static char *nxt_php_read_cookies(void);
400Sigor@sysoev.ru static void nxt_php_register_variables(zval *track_vars_array);
4186Smax.romanov@nginx.com static void nxt_php_log_message(char *message
4286Smax.romanov@nginx.com #ifdef NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE
4386Smax.romanov@nginx.com                                 , int syslog_type_int
4486Smax.romanov@nginx.com #endif
4586Smax.romanov@nginx.com );
460Sigor@sysoev.ru 
470Sigor@sysoev.ru #ifdef NXT_PHP7
480Sigor@sysoev.ru static size_t nxt_php_unbuffered_write(const char *str,
490Sigor@sysoev.ru     size_t str_length TSRMLS_DC);
500Sigor@sysoev.ru static size_t nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC);
510Sigor@sysoev.ru #else
520Sigor@sysoev.ru static int nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC);
530Sigor@sysoev.ru static int nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC);
540Sigor@sysoev.ru #endif
550Sigor@sysoev.ru 
56114Smax.romanov@nginx.com static void nxt_php_flush(void *server_context);
57114Smax.romanov@nginx.com 
580Sigor@sysoev.ru 
590Sigor@sysoev.ru static sapi_module_struct  nxt_php_sapi_module =
600Sigor@sysoev.ru {
610Sigor@sysoev.ru     (char *) "cli-server",
62259Sigor@sysoev.ru     (char *) "unit",
630Sigor@sysoev.ru 
640Sigor@sysoev.ru     nxt_php_startup,             /* startup */
650Sigor@sysoev.ru     php_module_shutdown_wrapper, /* shutdown */
660Sigor@sysoev.ru 
670Sigor@sysoev.ru     NULL,                        /* activate */
680Sigor@sysoev.ru     NULL,                        /* deactivate */
690Sigor@sysoev.ru 
700Sigor@sysoev.ru     nxt_php_unbuffered_write,    /* unbuffered write */
71114Smax.romanov@nginx.com     nxt_php_flush,               /* flush */
720Sigor@sysoev.ru     NULL,                        /* get uid */
730Sigor@sysoev.ru     NULL,                        /* getenv */
740Sigor@sysoev.ru 
750Sigor@sysoev.ru     php_error,                   /* error handler */
760Sigor@sysoev.ru 
770Sigor@sysoev.ru     NULL,                        /* header handler */
780Sigor@sysoev.ru     nxt_php_send_headers,        /* send headers handler */
790Sigor@sysoev.ru     NULL,                        /* send header handler */
800Sigor@sysoev.ru 
810Sigor@sysoev.ru     nxt_php_read_post,           /* read POST data */
820Sigor@sysoev.ru     nxt_php_read_cookies,        /* read Cookies */
830Sigor@sysoev.ru 
840Sigor@sysoev.ru     nxt_php_register_variables,  /* register server variables */
850Sigor@sysoev.ru     nxt_php_log_message,         /* log message */
8686Smax.romanov@nginx.com     NULL,                        /* get request time */
8786Smax.romanov@nginx.com     NULL,                        /* terminate process */
880Sigor@sysoev.ru 
8986Smax.romanov@nginx.com     NULL,                        /* php_ini_path_override */
9086Smax.romanov@nginx.com #ifdef NXT_HAVE_PHP_INTERRUPTS
9186Smax.romanov@nginx.com     NULL,                        /* block_interruptions */
9286Smax.romanov@nginx.com     NULL,                        /* unblock_interruptions */
9386Smax.romanov@nginx.com #endif
9486Smax.romanov@nginx.com     NULL,                        /* default_post_reader */
9586Smax.romanov@nginx.com     NULL,                        /* treat_data */
9686Smax.romanov@nginx.com     NULL,                        /* executable_location */
9786Smax.romanov@nginx.com 
9886Smax.romanov@nginx.com     0,                           /* php_ini_ignore */
9986Smax.romanov@nginx.com #ifdef NXT_HAVE_PHP_IGNORE_CWD
10086Smax.romanov@nginx.com     0,                           /* php_ini_ignore_cwd */
10186Smax.romanov@nginx.com #endif
10286Smax.romanov@nginx.com     NULL,                        /* get_fd */
10386Smax.romanov@nginx.com 
10486Smax.romanov@nginx.com     NULL,                        /* force_http_10 */
10586Smax.romanov@nginx.com 
10686Smax.romanov@nginx.com     NULL,                        /* get_target_uid */
10786Smax.romanov@nginx.com     NULL,                        /* get_target_gid */
10886Smax.romanov@nginx.com 
10986Smax.romanov@nginx.com     NULL,                        /* input_filter */
11086Smax.romanov@nginx.com 
11186Smax.romanov@nginx.com     NULL,                        /* ini_defaults */
11286Smax.romanov@nginx.com     0,                           /* phpinfo_as_text */
11386Smax.romanov@nginx.com 
11486Smax.romanov@nginx.com     NULL,                        /* ini_entries */
11586Smax.romanov@nginx.com     NULL,                        /* additional_functions */
11686Smax.romanov@nginx.com     NULL                         /* input_filter_init */
1170Sigor@sysoev.ru };
1180Sigor@sysoev.ru 
11986Smax.romanov@nginx.com typedef struct {
12086Smax.romanov@nginx.com     nxt_task_t           *task;
12186Smax.romanov@nginx.com     nxt_app_rmsg_t       *rmsg;
12286Smax.romanov@nginx.com     nxt_app_request_t    r;
12386Smax.romanov@nginx.com     nxt_str_t            script;
12486Smax.romanov@nginx.com     nxt_app_wmsg_t       *wmsg;
12586Smax.romanov@nginx.com     nxt_mp_t             *mem_pool;
126206Smax.romanov@nginx.com 
127206Smax.romanov@nginx.com     size_t               body_preread_size;
12886Smax.romanov@nginx.com } nxt_php_run_ctx_t;
12986Smax.romanov@nginx.com 
13086Smax.romanov@nginx.com nxt_inline nxt_int_t nxt_php_write(nxt_php_run_ctx_t *ctx,
13186Smax.romanov@nginx.com                       const u_char *data, size_t len,
13286Smax.romanov@nginx.com                       nxt_bool_t flush, nxt_bool_t last);
13386Smax.romanov@nginx.com 
1340Sigor@sysoev.ru 
1350Sigor@sysoev.ru static nxt_str_t nxt_php_path;
1360Sigor@sysoev.ru static nxt_str_t nxt_php_root;
1370Sigor@sysoev.ru static nxt_str_t nxt_php_script;
138142Smax.romanov@nginx.com static nxt_str_t nxt_php_index = nxt_string("index.php");
139142Smax.romanov@nginx.com 
140142Smax.romanov@nginx.com static void
141142Smax.romanov@nginx.com nxt_php_strdup(nxt_str_t *dst, nxt_str_t *src)
142142Smax.romanov@nginx.com {
143142Smax.romanov@nginx.com     dst->start = malloc(src->length + 1);
144142Smax.romanov@nginx.com     nxt_memcpy(dst->start, src->start, src->length);
145142Smax.romanov@nginx.com     dst->start[src->length] = '\0';
146142Smax.romanov@nginx.com 
147142Smax.romanov@nginx.com     dst->length = src->length;
148142Smax.romanov@nginx.com }
149142Smax.romanov@nginx.com 
150142Smax.romanov@nginx.com static void
151142Smax.romanov@nginx.com nxt_php_str_trim_trail(nxt_str_t *str, u_char t)
152142Smax.romanov@nginx.com {
153142Smax.romanov@nginx.com     while (str->length > 0 && str->start[str->length - 1] == t) {
154142Smax.romanov@nginx.com         str->length--;
155142Smax.romanov@nginx.com     }
156142Smax.romanov@nginx.com 
157142Smax.romanov@nginx.com     str->start[str->length] = '\0';
158142Smax.romanov@nginx.com }
159142Smax.romanov@nginx.com 
160142Smax.romanov@nginx.com 
161142Smax.romanov@nginx.com static void
162142Smax.romanov@nginx.com nxt_php_str_trim_lead(nxt_str_t *str, u_char t)
163142Smax.romanov@nginx.com {
164142Smax.romanov@nginx.com     while (str->length > 0 && str->start[0] == t) {
165142Smax.romanov@nginx.com         str->length--;
166142Smax.romanov@nginx.com         str->start++;
167142Smax.romanov@nginx.com     }
168142Smax.romanov@nginx.com }
1690Sigor@sysoev.ru 
170258Sigor@sysoev.ru static uint32_t  compat[] = {
171360Sigor@sysoev.ru     NXT_VERNUM, NXT_DEBUG,
172258Sigor@sysoev.ru };
173258Sigor@sysoev.ru 
1740Sigor@sysoev.ru 
175216Sigor@sysoev.ru NXT_EXPORT nxt_application_module_t  nxt_app_module = {
176258Sigor@sysoev.ru     sizeof(compat),
177258Sigor@sysoev.ru     compat,
178216Sigor@sysoev.ru     nxt_string("php"),
179216Sigor@sysoev.ru     nxt_string(PHP_VERSION),
18086Smax.romanov@nginx.com     nxt_php_init,
181216Sigor@sysoev.ru     nxt_php_run,
182421Smax.romanov@nginx.com     NULL,
18386Smax.romanov@nginx.com };
1840Sigor@sysoev.ru 
1850Sigor@sysoev.ru 
18686Smax.romanov@nginx.com static nxt_int_t
187142Smax.romanov@nginx.com nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
1880Sigor@sysoev.ru {
189142Smax.romanov@nginx.com     nxt_str_t           *root, *path, *script, *index;
190142Smax.romanov@nginx.com     nxt_php_app_conf_t  *c;
191142Smax.romanov@nginx.com 
192142Smax.romanov@nginx.com     c = &conf->u.php;
193142Smax.romanov@nginx.com 
194142Smax.romanov@nginx.com     if (c->root.length == 0) {
195142Smax.romanov@nginx.com         nxt_log_emerg(task->log, "php root is empty");
196142Smax.romanov@nginx.com         return NXT_ERROR;
197142Smax.romanov@nginx.com     }
198142Smax.romanov@nginx.com 
199142Smax.romanov@nginx.com     root = &nxt_php_root;
200142Smax.romanov@nginx.com     path = &nxt_php_path;
201142Smax.romanov@nginx.com     script = &nxt_php_script;
202142Smax.romanov@nginx.com     index = &nxt_php_index;
203142Smax.romanov@nginx.com 
204142Smax.romanov@nginx.com     nxt_php_strdup(root, &c->root);
205142Smax.romanov@nginx.com 
206142Smax.romanov@nginx.com     nxt_php_str_trim_trail(root, '/');
207142Smax.romanov@nginx.com 
208142Smax.romanov@nginx.com     if (c->script.length > 0) {
209142Smax.romanov@nginx.com         nxt_php_str_trim_lead(&c->script, '/');
210142Smax.romanov@nginx.com 
211142Smax.romanov@nginx.com         path->length = root->length + c->script.length + 1;
212142Smax.romanov@nginx.com         path->start = malloc(path->length + 1);
213142Smax.romanov@nginx.com 
214142Smax.romanov@nginx.com         nxt_memcpy(path->start, root->start, root->length);
215142Smax.romanov@nginx.com         path->start[root->length] = '/';
216142Smax.romanov@nginx.com 
217142Smax.romanov@nginx.com         nxt_memcpy(path->start + root->length + 1,
218142Smax.romanov@nginx.com                    c->script.start, c->script.length);
219142Smax.romanov@nginx.com 
220142Smax.romanov@nginx.com         path->start[path->length] = '\0';
221142Smax.romanov@nginx.com 
222142Smax.romanov@nginx.com 
223142Smax.romanov@nginx.com         script->length = c->script.length + 1;
224142Smax.romanov@nginx.com         script->start = malloc(script->length + 1);
225142Smax.romanov@nginx.com         script->start[0] = '/';
226142Smax.romanov@nginx.com         nxt_memcpy(script->start + 1, c->script.start, c->script.length);
227142Smax.romanov@nginx.com         script->start[script->length] = '\0';
228142Smax.romanov@nginx.com 
229142Smax.romanov@nginx.com         nxt_log_error(NXT_LOG_INFO, task->log,
230142Smax.romanov@nginx.com                       "(ABS_MODE) php script \"%V\" root: \"%V\"",
231142Smax.romanov@nginx.com                       script, root);
232277Sigor@sysoev.ru 
233142Smax.romanov@nginx.com     } else {
234142Smax.romanov@nginx.com         nxt_log_error(NXT_LOG_INFO, task->log,
235142Smax.romanov@nginx.com                       "(non ABS_MODE) php root: \"%V\"", root);
236142Smax.romanov@nginx.com     }
237142Smax.romanov@nginx.com 
238142Smax.romanov@nginx.com     if (c->index.length > 0) {
239142Smax.romanov@nginx.com         nxt_php_strdup(index, &c->index);
240142Smax.romanov@nginx.com     }
241142Smax.romanov@nginx.com 
242142Smax.romanov@nginx.com     sapi_startup(&nxt_php_sapi_module);
243142Smax.romanov@nginx.com     nxt_php_startup(&nxt_php_sapi_module);
244142Smax.romanov@nginx.com 
2450Sigor@sysoev.ru     return NXT_OK;
2460Sigor@sysoev.ru }
2470Sigor@sysoev.ru 
2480Sigor@sysoev.ru 
24986Smax.romanov@nginx.com static nxt_int_t
25086Smax.romanov@nginx.com nxt_php_read_request(nxt_task_t *task, nxt_app_rmsg_t *rmsg,
25186Smax.romanov@nginx.com     nxt_php_run_ctx_t *ctx)
2520Sigor@sysoev.ru {
25386Smax.romanov@nginx.com     u_char                    *p;
25486Smax.romanov@nginx.com     size_t                    s;
255114Smax.romanov@nginx.com     nxt_int_t                 rc;
25694Smax.romanov@nginx.com     nxt_str_t                 script_name;
25786Smax.romanov@nginx.com     nxt_app_request_header_t  *h;
2580Sigor@sysoev.ru 
25986Smax.romanov@nginx.com     h = &ctx->r.header;
2600Sigor@sysoev.ru 
261114Smax.romanov@nginx.com #define RC(S)                                                                 \
262114Smax.romanov@nginx.com     do {                                                                      \
263114Smax.romanov@nginx.com         rc = (S);                                                             \
264114Smax.romanov@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {                                    \
265114Smax.romanov@nginx.com             goto fail;                                                        \
266114Smax.romanov@nginx.com         }                                                                     \
267114Smax.romanov@nginx.com     } while(0)
2680Sigor@sysoev.ru 
269114Smax.romanov@nginx.com #define NXT_READ(dst)                                                         \
270114Smax.romanov@nginx.com     RC(nxt_app_msg_read_str(task, rmsg, (dst)))
271114Smax.romanov@nginx.com 
272114Smax.romanov@nginx.com     NXT_READ(&h->method);
273114Smax.romanov@nginx.com     NXT_READ(&h->target);
274114Smax.romanov@nginx.com     NXT_READ(&h->path);
275114Smax.romanov@nginx.com 
276114Smax.romanov@nginx.com     RC(nxt_app_msg_read_size(task, rmsg, &s));
27786Smax.romanov@nginx.com     if (s > 0) {
27886Smax.romanov@nginx.com         s--;
279112Smax.romanov@nginx.com         h->query.start = h->target.start + s;
280112Smax.romanov@nginx.com         h->query.length = h->target.length - s;
2810Sigor@sysoev.ru 
282112Smax.romanov@nginx.com         if (h->path.start == NULL) {
283112Smax.romanov@nginx.com             h->path.start = h->target.start;
284112Smax.romanov@nginx.com             h->path.length = s - 1;
28586Smax.romanov@nginx.com         }
2860Sigor@sysoev.ru     }
2870Sigor@sysoev.ru 
288112Smax.romanov@nginx.com     if (h->path.start == NULL) {
289112Smax.romanov@nginx.com         h->path = h->target;
290112Smax.romanov@nginx.com     }
291112Smax.romanov@nginx.com 
29286Smax.romanov@nginx.com     if (nxt_php_path.start == NULL) {
293112Smax.romanov@nginx.com         if (h->path.start[h->path.length - 1] == '/') {
294142Smax.romanov@nginx.com             script_name = nxt_php_index;
295277Sigor@sysoev.ru 
29694Smax.romanov@nginx.com         } else {
29794Smax.romanov@nginx.com             script_name.length = 0;
29894Smax.romanov@nginx.com         }
29994Smax.romanov@nginx.com 
300112Smax.romanov@nginx.com         ctx->script.length = nxt_php_root.length + h->path.length +
30194Smax.romanov@nginx.com                              script_name.length;
30286Smax.romanov@nginx.com         ctx->script.start = nxt_mp_nget(ctx->mem_pool,
30386Smax.romanov@nginx.com             ctx->script.length + 1);
30486Smax.romanov@nginx.com 
30586Smax.romanov@nginx.com         p = ctx->script.start;
30686Smax.romanov@nginx.com 
30786Smax.romanov@nginx.com         nxt_memcpy(p, nxt_php_root.start, nxt_php_root.length);
30886Smax.romanov@nginx.com         p += nxt_php_root.length;
30994Smax.romanov@nginx.com 
310112Smax.romanov@nginx.com         nxt_memcpy(p, h->path.start, h->path.length);
311112Smax.romanov@nginx.com         p += h->path.length;
31294Smax.romanov@nginx.com 
31394Smax.romanov@nginx.com         if (script_name.length > 0) {
31494Smax.romanov@nginx.com             nxt_memcpy(p, script_name.start, script_name.length);
31594Smax.romanov@nginx.com             p += script_name.length;
31694Smax.romanov@nginx.com         }
31794Smax.romanov@nginx.com 
31894Smax.romanov@nginx.com         p[0] = '\0';
319277Sigor@sysoev.ru 
32086Smax.romanov@nginx.com     } else {
32186Smax.romanov@nginx.com         ctx->script = nxt_php_path;
32286Smax.romanov@nginx.com     }
32386Smax.romanov@nginx.com 
324114Smax.romanov@nginx.com     NXT_READ(&h->version);
325114Smax.romanov@nginx.com 
326114Smax.romanov@nginx.com     NXT_READ(&ctx->r.remote);
327268Sigor@sysoev.ru     NXT_READ(&ctx->r.local);
32886Smax.romanov@nginx.com 
329114Smax.romanov@nginx.com     NXT_READ(&h->host);
330114Smax.romanov@nginx.com     NXT_READ(&h->cookie);
331114Smax.romanov@nginx.com     NXT_READ(&h->content_type);
332114Smax.romanov@nginx.com     NXT_READ(&h->content_length);
33386Smax.romanov@nginx.com 
334114Smax.romanov@nginx.com     RC(nxt_app_msg_read_size(task, rmsg, &s));
33586Smax.romanov@nginx.com     h->parsed_content_length = s;
33686Smax.romanov@nginx.com 
337305Smax.romanov@nginx.com     RC(nxt_app_msg_read_size(task, ctx->rmsg, &ctx->body_preread_size));
338305Smax.romanov@nginx.com 
339114Smax.romanov@nginx.com #undef NXT_READ
340114Smax.romanov@nginx.com #undef RC
34194Smax.romanov@nginx.com 
34286Smax.romanov@nginx.com     /* Further headers read moved to nxt_php_register_variables. */
3430Sigor@sysoev.ru     return NXT_OK;
344114Smax.romanov@nginx.com 
345114Smax.romanov@nginx.com fail:
346114Smax.romanov@nginx.com 
347114Smax.romanov@nginx.com     return rc;
3480Sigor@sysoev.ru }
3490Sigor@sysoev.ru 
3500Sigor@sysoev.ru 
35186Smax.romanov@nginx.com static nxt_int_t
35286Smax.romanov@nginx.com nxt_php_run(nxt_task_t *task,
35386Smax.romanov@nginx.com     nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg)
3540Sigor@sysoev.ru {
355114Smax.romanov@nginx.com     nxt_int_t                 rc;
35686Smax.romanov@nginx.com     zend_file_handle          file_handle;
35786Smax.romanov@nginx.com     nxt_php_run_ctx_t         run_ctx;
35886Smax.romanov@nginx.com     nxt_app_request_header_t  *h;
3590Sigor@sysoev.ru 
36086Smax.romanov@nginx.com     if (nxt_php_root.length == 0) {
3610Sigor@sysoev.ru         return NXT_ERROR;
3620Sigor@sysoev.ru     }
3630Sigor@sysoev.ru 
36486Smax.romanov@nginx.com     nxt_memzero(&run_ctx, sizeof(run_ctx));
36586Smax.romanov@nginx.com 
36686Smax.romanov@nginx.com     run_ctx.task = task;
36786Smax.romanov@nginx.com     run_ctx.rmsg = rmsg;
36886Smax.romanov@nginx.com     run_ctx.wmsg = wmsg;
36986Smax.romanov@nginx.com 
37086Smax.romanov@nginx.com     run_ctx.mem_pool = nxt_mp_create(1024, 128, 256, 32);
3710Sigor@sysoev.ru 
37286Smax.romanov@nginx.com     h = &run_ctx.r.header;
37386Smax.romanov@nginx.com 
374114Smax.romanov@nginx.com     rc = nxt_php_read_request(task, rmsg, &run_ctx);
375114Smax.romanov@nginx.com 
376114Smax.romanov@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
377114Smax.romanov@nginx.com         goto fail;
378114Smax.romanov@nginx.com     }
37986Smax.romanov@nginx.com 
38086Smax.romanov@nginx.com     SG(server_context) = &run_ctx;
381112Smax.romanov@nginx.com     SG(request_info).request_uri = (char *) h->target.start;
38286Smax.romanov@nginx.com     SG(request_info).request_method = (char *) h->method.start;
3830Sigor@sysoev.ru 
3840Sigor@sysoev.ru     SG(request_info).proto_num = 1001;
3850Sigor@sysoev.ru 
38686Smax.romanov@nginx.com     SG(request_info).query_string = (char *) h->query.start;
38786Smax.romanov@nginx.com     SG(request_info).content_length = h->parsed_content_length;
3880Sigor@sysoev.ru 
38986Smax.romanov@nginx.com     if (h->content_type.start != NULL) {
39086Smax.romanov@nginx.com         SG(request_info).content_type = (char *) h->content_type.start;
3910Sigor@sysoev.ru     }
3920Sigor@sysoev.ru 
3930Sigor@sysoev.ru     SG(sapi_headers).http_response_code = 200;
3940Sigor@sysoev.ru 
3950Sigor@sysoev.ru     SG(request_info).path_translated = NULL;
3960Sigor@sysoev.ru 
3970Sigor@sysoev.ru     file_handle.type = ZEND_HANDLE_FILENAME;
39886Smax.romanov@nginx.com     file_handle.filename = (char *) run_ctx.script.start;
3990Sigor@sysoev.ru     file_handle.free_filename = 0;
4000Sigor@sysoev.ru     file_handle.opened_path = NULL;
4010Sigor@sysoev.ru 
402142Smax.romanov@nginx.com     nxt_debug(task, "handle.filename = '%s'", run_ctx.script.start);
403142Smax.romanov@nginx.com 
40486Smax.romanov@nginx.com     if (nxt_php_path.start != NULL) {
40586Smax.romanov@nginx.com         nxt_debug(task, "run script %V in absolute mode", &nxt_php_path);
406277Sigor@sysoev.ru 
40786Smax.romanov@nginx.com     } else {
40886Smax.romanov@nginx.com         nxt_debug(task, "run script %V", &run_ctx.script);
40986Smax.romanov@nginx.com     }
4100Sigor@sysoev.ru 
4110Sigor@sysoev.ru     if (nxt_slow_path(php_request_startup() == FAILURE)) {
412142Smax.romanov@nginx.com         nxt_debug(task, "php_request_startup() failed");
41386Smax.romanov@nginx.com         goto fail;
4140Sigor@sysoev.ru     }
4150Sigor@sysoev.ru 
4160Sigor@sysoev.ru     php_execute_script(&file_handle TSRMLS_CC);
4170Sigor@sysoev.ru     php_request_shutdown(NULL);
4180Sigor@sysoev.ru 
41986Smax.romanov@nginx.com     nxt_app_msg_flush(task, wmsg, 1);
42086Smax.romanov@nginx.com 
42186Smax.romanov@nginx.com     nxt_mp_destroy(run_ctx.mem_pool);
42286Smax.romanov@nginx.com 
4230Sigor@sysoev.ru     return NXT_OK;
42486Smax.romanov@nginx.com 
42586Smax.romanov@nginx.com fail:
42686Smax.romanov@nginx.com 
42786Smax.romanov@nginx.com     nxt_mp_destroy(run_ctx.mem_pool);
42886Smax.romanov@nginx.com 
42986Smax.romanov@nginx.com     return NXT_ERROR;
43086Smax.romanov@nginx.com }
43186Smax.romanov@nginx.com 
43286Smax.romanov@nginx.com 
43386Smax.romanov@nginx.com nxt_inline nxt_int_t
43486Smax.romanov@nginx.com nxt_php_write(nxt_php_run_ctx_t *ctx, const u_char *data, size_t len,
43586Smax.romanov@nginx.com     nxt_bool_t flush, nxt_bool_t last)
43686Smax.romanov@nginx.com {
43786Smax.romanov@nginx.com     nxt_int_t  rc;
43886Smax.romanov@nginx.com 
439114Smax.romanov@nginx.com     if (len > 0) {
440114Smax.romanov@nginx.com         rc = nxt_app_msg_write_raw(ctx->task, ctx->wmsg, data, len);
441277Sigor@sysoev.ru 
442114Smax.romanov@nginx.com     } else {
443114Smax.romanov@nginx.com         rc = NXT_OK;
444114Smax.romanov@nginx.com     }
44586Smax.romanov@nginx.com 
44686Smax.romanov@nginx.com     if (flush || last) {
44786Smax.romanov@nginx.com         rc = nxt_app_msg_flush(ctx->task, ctx->wmsg, last);
44886Smax.romanov@nginx.com     }
44986Smax.romanov@nginx.com 
45086Smax.romanov@nginx.com     return rc;
4510Sigor@sysoev.ru }
4520Sigor@sysoev.ru 
4530Sigor@sysoev.ru 
4540Sigor@sysoev.ru static int
4550Sigor@sysoev.ru nxt_php_startup(sapi_module_struct *sapi_module)
4560Sigor@sysoev.ru {
4570Sigor@sysoev.ru    return php_module_startup(sapi_module, NULL, 0);
4580Sigor@sysoev.ru }
4590Sigor@sysoev.ru 
4600Sigor@sysoev.ru 
4610Sigor@sysoev.ru #ifdef NXT_PHP7
4620Sigor@sysoev.ru static size_t
4630Sigor@sysoev.ru nxt_php_unbuffered_write(const char *str, size_t str_length TSRMLS_DC)
4640Sigor@sysoev.ru #else
4650Sigor@sysoev.ru static int
4660Sigor@sysoev.ru nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC)
4670Sigor@sysoev.ru #endif
4680Sigor@sysoev.ru {
469114Smax.romanov@nginx.com     nxt_int_t          rc;
47086Smax.romanov@nginx.com     nxt_php_run_ctx_t  *ctx;
4710Sigor@sysoev.ru 
47286Smax.romanov@nginx.com     ctx = SG(server_context);
4730Sigor@sysoev.ru 
474114Smax.romanov@nginx.com     rc = nxt_php_write(ctx, (u_char *) str, str_length, 1, 0);
475114Smax.romanov@nginx.com 
476114Smax.romanov@nginx.com     if (nxt_fast_path(rc == NXT_OK)) {
477114Smax.romanov@nginx.com         return str_length;
478114Smax.romanov@nginx.com     }
4790Sigor@sysoev.ru 
480114Smax.romanov@nginx.com     // TODO handle NXT_AGAIN
481114Smax.romanov@nginx.com     php_handle_aborted_connection();
482114Smax.romanov@nginx.com     return 0;
483114Smax.romanov@nginx.com }
484114Smax.romanov@nginx.com 
485114Smax.romanov@nginx.com 
486114Smax.romanov@nginx.com static void
487114Smax.romanov@nginx.com nxt_php_flush(void *server_context)
488114Smax.romanov@nginx.com {
489114Smax.romanov@nginx.com     nxt_php_run_ctx_t  *ctx;
490114Smax.romanov@nginx.com 
491114Smax.romanov@nginx.com     ctx = server_context;
492114Smax.romanov@nginx.com 
493114Smax.romanov@nginx.com     (void) nxt_app_msg_flush(ctx->task, ctx->wmsg, 0);
4940Sigor@sysoev.ru }
4950Sigor@sysoev.ru 
4960Sigor@sysoev.ru 
4970Sigor@sysoev.ru static int
4980Sigor@sysoev.ru nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
4990Sigor@sysoev.ru {
5000Sigor@sysoev.ru     size_t               len;
501114Smax.romanov@nginx.com     u_char               *status, buf[64];
502114Smax.romanov@nginx.com     nxt_int_t            rc;
503114Smax.romanov@nginx.com     nxt_php_run_ctx_t    *ctx;
5040Sigor@sysoev.ru     sapi_header_struct   *h;
5050Sigor@sysoev.ru     zend_llist_position  zpos;
5060Sigor@sysoev.ru 
5070Sigor@sysoev.ru     static const u_char default_repsonse[]
508431Sigor@sysoev.ru         = "Status: 200\r\n"
5090Sigor@sysoev.ru           "\r\n";
5100Sigor@sysoev.ru 
511431Sigor@sysoev.ru     static const u_char status_200[] = "Status: 200";
51286Smax.romanov@nginx.com     static const u_char cr_lf[] = "\r\n";
51386Smax.romanov@nginx.com 
51486Smax.romanov@nginx.com     ctx = SG(server_context);
5150Sigor@sysoev.ru 
516114Smax.romanov@nginx.com #define RC(S)                                                                 \
517114Smax.romanov@nginx.com     do {                                                                      \
518114Smax.romanov@nginx.com         rc = (S);                                                             \
519114Smax.romanov@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {                                    \
520114Smax.romanov@nginx.com             goto fail;                                                        \
521114Smax.romanov@nginx.com         }                                                                     \
522114Smax.romanov@nginx.com     } while(0)
523114Smax.romanov@nginx.com 
5240Sigor@sysoev.ru     if (SG(request_info).no_headers == 1) {
525114Smax.romanov@nginx.com         RC(nxt_php_write(ctx, default_repsonse, sizeof(default_repsonse) - 1,
526114Smax.romanov@nginx.com                       1, 0));
5270Sigor@sysoev.ru         return SAPI_HEADER_SENT_SUCCESSFULLY;
5280Sigor@sysoev.ru     }
5290Sigor@sysoev.ru 
5300Sigor@sysoev.ru     if (SG(sapi_headers).http_status_line) {
531114Smax.romanov@nginx.com         status = (u_char *) SG(sapi_headers).http_status_line;
5320Sigor@sysoev.ru         len = nxt_strlen(status);
5330Sigor@sysoev.ru 
534431Sigor@sysoev.ru         if (len < 12) {
535431Sigor@sysoev.ru             goto fail;
536431Sigor@sysoev.ru         }
537431Sigor@sysoev.ru 
538431Sigor@sysoev.ru         RC(nxt_php_write(ctx, status_200, sizeof(status_200) - 4, 0, 0));
539431Sigor@sysoev.ru         RC(nxt_php_write(ctx, status + 9, 3, 0, 0));
5400Sigor@sysoev.ru 
5410Sigor@sysoev.ru     } else if (SG(sapi_headers).http_response_code) {
54286Smax.romanov@nginx.com         status = nxt_sprintf(buf, buf + sizeof(buf), "%03d",
5430Sigor@sysoev.ru                         SG(sapi_headers).http_response_code);
54486Smax.romanov@nginx.com         len = status - buf;
54586Smax.romanov@nginx.com 
546431Sigor@sysoev.ru         RC(nxt_php_write(ctx, status_200, sizeof(status_200) - 4, 0, 0));
547114Smax.romanov@nginx.com         RC(nxt_php_write(ctx, buf, len, 0, 0));
5480Sigor@sysoev.ru 
5490Sigor@sysoev.ru     } else {
550431Sigor@sysoev.ru         RC(nxt_php_write(ctx, status_200, sizeof(status_200) - 1, 0, 0));
5510Sigor@sysoev.ru     }
5520Sigor@sysoev.ru 
553114Smax.romanov@nginx.com     RC(nxt_php_write(ctx, cr_lf, sizeof(cr_lf) - 1, 0, 0));
5540Sigor@sysoev.ru 
5550Sigor@sysoev.ru     h = zend_llist_get_first_ex(&sapi_headers->headers, &zpos);
5560Sigor@sysoev.ru 
5570Sigor@sysoev.ru     while (h) {
558114Smax.romanov@nginx.com         RC(nxt_php_write(ctx, (u_char *) h->header, h->header_len, 0, 0));
559114Smax.romanov@nginx.com         RC(nxt_php_write(ctx, cr_lf, sizeof(cr_lf) - 1, 0, 0));
5600Sigor@sysoev.ru 
5610Sigor@sysoev.ru         h = zend_llist_get_next_ex(&sapi_headers->headers, &zpos);
5620Sigor@sysoev.ru     }
5630Sigor@sysoev.ru 
564114Smax.romanov@nginx.com     RC(nxt_php_write(ctx, cr_lf, sizeof(cr_lf) - 1, 1, 0));
565114Smax.romanov@nginx.com 
566114Smax.romanov@nginx.com #undef RC
5670Sigor@sysoev.ru 
5680Sigor@sysoev.ru     return SAPI_HEADER_SENT_SUCCESSFULLY;
569114Smax.romanov@nginx.com 
570114Smax.romanov@nginx.com fail:
571114Smax.romanov@nginx.com 
572114Smax.romanov@nginx.com     // TODO handle NXT_AGAIN
573114Smax.romanov@nginx.com     return SAPI_HEADER_SEND_FAILED;
5740Sigor@sysoev.ru }
5750Sigor@sysoev.ru 
5760Sigor@sysoev.ru 
5770Sigor@sysoev.ru #ifdef NXT_PHP7
5780Sigor@sysoev.ru static size_t
5790Sigor@sysoev.ru nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC)
5800Sigor@sysoev.ru #else
5810Sigor@sysoev.ru static int
5820Sigor@sysoev.ru nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC)
5830Sigor@sysoev.ru #endif
5840Sigor@sysoev.ru {
585206Smax.romanov@nginx.com     size_t                    size, rest;
58686Smax.romanov@nginx.com     nxt_php_run_ctx_t         *ctx;
58786Smax.romanov@nginx.com     nxt_app_request_header_t  *h;
5880Sigor@sysoev.ru 
58986Smax.romanov@nginx.com     ctx = SG(server_context);
59086Smax.romanov@nginx.com     h = &ctx->r.header;
5910Sigor@sysoev.ru 
592206Smax.romanov@nginx.com     rest = (size_t) h->parsed_content_length - SG(read_post_bytes);
59386Smax.romanov@nginx.com 
59486Smax.romanov@nginx.com     nxt_debug(ctx->task, "nxt_php_read_post %O", rest);
5950Sigor@sysoev.ru 
5960Sigor@sysoev.ru     if (rest == 0) {
5970Sigor@sysoev.ru         return 0;
5980Sigor@sysoev.ru     }
5990Sigor@sysoev.ru 
600206Smax.romanov@nginx.com     rest = nxt_min(ctx->body_preread_size, (size_t) count_bytes);
601206Smax.romanov@nginx.com     size = nxt_app_msg_read_raw(ctx->task, ctx->rmsg, buffer, rest);
6020Sigor@sysoev.ru 
603206Smax.romanov@nginx.com     ctx->body_preread_size -= size;
6040Sigor@sysoev.ru 
60586Smax.romanov@nginx.com     return size;
6060Sigor@sysoev.ru }
6070Sigor@sysoev.ru 
6080Sigor@sysoev.ru 
6090Sigor@sysoev.ru static char *
6100Sigor@sysoev.ru nxt_php_read_cookies(TSRMLS_D)
6110Sigor@sysoev.ru {
61286Smax.romanov@nginx.com     nxt_php_run_ctx_t  *ctx;
6130Sigor@sysoev.ru 
61486Smax.romanov@nginx.com     ctx = SG(server_context);
6150Sigor@sysoev.ru 
61694Smax.romanov@nginx.com     nxt_debug(ctx->task, "nxt_php_read_cookies");
61794Smax.romanov@nginx.com 
61886Smax.romanov@nginx.com     return (char *) ctx->r.header.cookie.start;
6190Sigor@sysoev.ru }
6200Sigor@sysoev.ru 
6210Sigor@sysoev.ru 
6220Sigor@sysoev.ru static void
6230Sigor@sysoev.ru nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
6240Sigor@sysoev.ru {
625114Smax.romanov@nginx.com     u_char                    *colon;
62686Smax.romanov@nginx.com     nxt_str_t                 n, v;
62786Smax.romanov@nginx.com     nxt_int_t                 rc;
628114Smax.romanov@nginx.com     nxt_str_t                 host, server_name, server_port;
62986Smax.romanov@nginx.com     nxt_task_t                *task;
63086Smax.romanov@nginx.com     nxt_php_run_ctx_t         *ctx;
63186Smax.romanov@nginx.com     nxt_app_request_header_t  *h;
6320Sigor@sysoev.ru 
633114Smax.romanov@nginx.com     static nxt_str_t def_host = nxt_string("localhost");
634114Smax.romanov@nginx.com     static nxt_str_t def_port = nxt_string("80");
635114Smax.romanov@nginx.com 
63686Smax.romanov@nginx.com     ctx = SG(server_context);
6370Sigor@sysoev.ru 
63886Smax.romanov@nginx.com     h = &ctx->r.header;
63986Smax.romanov@nginx.com     task = ctx->task;
6400Sigor@sysoev.ru 
64186Smax.romanov@nginx.com     nxt_debug(task, "php register variables");
6420Sigor@sysoev.ru 
643114Smax.romanov@nginx.com #define NXT_PHP_SET(n, v)                                                     \
644142Smax.romanov@nginx.com     nxt_debug(task, "php: register %s='%V'", n, &v);                          \
645114Smax.romanov@nginx.com     php_register_variable_safe((char *) (n), (char *) (v).start,              \
646114Smax.romanov@nginx.com                                (v).length, track_vars_array TSRMLS_CC)        \
647114Smax.romanov@nginx.com 
648114Smax.romanov@nginx.com     NXT_PHP_SET("SERVER_PROTOCOL", h->version);
64986Smax.romanov@nginx.com 
65086Smax.romanov@nginx.com /*
65186Smax.romanov@nginx.com  * 'SCRIPT_NAME'
65286Smax.romanov@nginx.com  * Contains the current script's path. This is useful for pages which need to
65386Smax.romanov@nginx.com  * point to themselves. The __FILE__ constant contains the full path and
65486Smax.romanov@nginx.com  * filename of the current (i.e. included) file.
65586Smax.romanov@nginx.com  */
65686Smax.romanov@nginx.com 
65786Smax.romanov@nginx.com /*
65886Smax.romanov@nginx.com  * 'SCRIPT_FILENAME'
65986Smax.romanov@nginx.com  * The absolute pathname of the currently executing script.
66086Smax.romanov@nginx.com  */
66186Smax.romanov@nginx.com 
66286Smax.romanov@nginx.com /*
66386Smax.romanov@nginx.com  * 'DOCUMENT_ROOT'
66486Smax.romanov@nginx.com  * The document root directory under which the current script is executing,
66586Smax.romanov@nginx.com  * as defined in the server's configuration file.
66686Smax.romanov@nginx.com  */
6670Sigor@sysoev.ru 
66886Smax.romanov@nginx.com     if (nxt_php_script.start != NULL) {
66986Smax.romanov@nginx.com     // ABS_MODE
67086Smax.romanov@nginx.com /*
67186Smax.romanov@nginx.com  * 'PHP_SELF'
67286Smax.romanov@nginx.com  * The filename of the currently executing script, relative to the document
67386Smax.romanov@nginx.com  * root. For instance, $_SERVER['PHP_SELF'] in a script at the address
67486Smax.romanov@nginx.com  * http://example.com/foo/bar.php would be /foo/bar.php. The __FILE__ constant
67586Smax.romanov@nginx.com  * contains the full path and filename of the current (i.e. included) file.
67686Smax.romanov@nginx.com  * If PHP is running as a command-line processor this variable contains the
67786Smax.romanov@nginx.com  * script name since PHP 4.3.0. Previously it was not available.
67886Smax.romanov@nginx.com  */
679114Smax.romanov@nginx.com         NXT_PHP_SET("PHP_SELF", nxt_php_script);
680114Smax.romanov@nginx.com         NXT_PHP_SET("SCRIPT_NAME", nxt_php_script);
681277Sigor@sysoev.ru 
68286Smax.romanov@nginx.com     } else {
683114Smax.romanov@nginx.com         NXT_PHP_SET("PHP_SELF", h->path);
684114Smax.romanov@nginx.com         NXT_PHP_SET("SCRIPT_NAME", h->path);
68586Smax.romanov@nginx.com     }
6860Sigor@sysoev.ru 
687114Smax.romanov@nginx.com     NXT_PHP_SET("SCRIPT_FILENAME", ctx->script);
688114Smax.romanov@nginx.com     NXT_PHP_SET("DOCUMENT_ROOT", nxt_php_root);
6890Sigor@sysoev.ru 
690114Smax.romanov@nginx.com     NXT_PHP_SET("REQUEST_METHOD", h->method);
691114Smax.romanov@nginx.com     NXT_PHP_SET("REQUEST_URI", h->target);
6920Sigor@sysoev.ru 
69386Smax.romanov@nginx.com     if (h->query.start != NULL) {
694114Smax.romanov@nginx.com         NXT_PHP_SET("QUERY_STRING", h->query);
6950Sigor@sysoev.ru     }
6960Sigor@sysoev.ru 
69786Smax.romanov@nginx.com     if (h->content_type.start != NULL) {
698114Smax.romanov@nginx.com         NXT_PHP_SET("CONTENT_TYPE", h->content_type);
6990Sigor@sysoev.ru     }
7000Sigor@sysoev.ru 
70186Smax.romanov@nginx.com     if (h->content_length.start != NULL) {
702114Smax.romanov@nginx.com         NXT_PHP_SET("CONTENT_LENGTH", h->content_length);
703114Smax.romanov@nginx.com     }
704114Smax.romanov@nginx.com 
705114Smax.romanov@nginx.com     host = h->host;
706114Smax.romanov@nginx.com     if (host.length == 0) {
707114Smax.romanov@nginx.com         host = def_host;
70886Smax.romanov@nginx.com     }
7090Sigor@sysoev.ru 
710114Smax.romanov@nginx.com     server_name = host;
711114Smax.romanov@nginx.com     colon = nxt_memchr(host.start, ':', host.length);
712114Smax.romanov@nginx.com 
713114Smax.romanov@nginx.com     if (colon != NULL) {
714114Smax.romanov@nginx.com         server_name.length = colon - host.start;
715114Smax.romanov@nginx.com 
716114Smax.romanov@nginx.com         server_port.start = colon + 1;
717114Smax.romanov@nginx.com         server_port.length = host.length - server_name.length - 1;
718277Sigor@sysoev.ru 
719114Smax.romanov@nginx.com     } else {
720114Smax.romanov@nginx.com         server_port = def_port;
721114Smax.romanov@nginx.com     }
722114Smax.romanov@nginx.com 
723114Smax.romanov@nginx.com     NXT_PHP_SET("SERVER_NAME", server_name);
724114Smax.romanov@nginx.com     NXT_PHP_SET("SERVER_PORT", server_port);
725114Smax.romanov@nginx.com 
726114Smax.romanov@nginx.com     NXT_PHP_SET("REMOTE_ADDR", ctx->r.remote);
727268Sigor@sysoev.ru     NXT_PHP_SET("SERVER_ADDR", ctx->r.local);
728114Smax.romanov@nginx.com 
72994Smax.romanov@nginx.com     while (nxt_app_msg_read_str(task, ctx->rmsg, &n) == NXT_OK) {
73086Smax.romanov@nginx.com         if (nxt_slow_path(n.length == 0)) {
73186Smax.romanov@nginx.com             break;
7320Sigor@sysoev.ru         }
7330Sigor@sysoev.ru 
73494Smax.romanov@nginx.com         rc = nxt_app_msg_read_str(task, ctx->rmsg, &v);
73594Smax.romanov@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
73694Smax.romanov@nginx.com             break;
73794Smax.romanov@nginx.com         }
73894Smax.romanov@nginx.com 
739114Smax.romanov@nginx.com         NXT_PHP_SET(n.start, v);
7400Sigor@sysoev.ru     }
741114Smax.romanov@nginx.com 
742114Smax.romanov@nginx.com #undef NXT_PHP_SET
7430Sigor@sysoev.ru }
7440Sigor@sysoev.ru 
7450Sigor@sysoev.ru 
7460Sigor@sysoev.ru static void
74786Smax.romanov@nginx.com nxt_php_log_message(char *message
74886Smax.romanov@nginx.com #ifdef NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE
74986Smax.romanov@nginx.com                                 , int syslog_type_int
75086Smax.romanov@nginx.com #endif
75186Smax.romanov@nginx.com )
7520Sigor@sysoev.ru {
7530Sigor@sysoev.ru     return;
7540Sigor@sysoev.ru }
755