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> 14446Sigor@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 #ifdef NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 42*579Svbart@nginx.com static void nxt_php_log_message(char *message, int syslog_type_int); 43*579Svbart@nginx.com #else 44*579Svbart@nginx.com static void nxt_php_log_message(char *message); 4586Smax.romanov@nginx.com #endif 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; 125206Smax.romanov@nginx.com 126206Smax.romanov@nginx.com size_t body_preread_size; 12786Smax.romanov@nginx.com } nxt_php_run_ctx_t; 12886Smax.romanov@nginx.com 12986Smax.romanov@nginx.com nxt_inline nxt_int_t nxt_php_write(nxt_php_run_ctx_t *ctx, 13086Smax.romanov@nginx.com const u_char *data, size_t len, 13186Smax.romanov@nginx.com nxt_bool_t flush, nxt_bool_t last); 13286Smax.romanov@nginx.com 1330Sigor@sysoev.ru 1340Sigor@sysoev.ru static nxt_str_t nxt_php_path; 1350Sigor@sysoev.ru static nxt_str_t nxt_php_root; 1360Sigor@sysoev.ru static nxt_str_t nxt_php_script; 137142Smax.romanov@nginx.com static nxt_str_t nxt_php_index = nxt_string("index.php"); 138142Smax.romanov@nginx.com 139142Smax.romanov@nginx.com 140142Smax.romanov@nginx.com static void 141142Smax.romanov@nginx.com nxt_php_str_trim_trail(nxt_str_t *str, u_char t) 142142Smax.romanov@nginx.com { 143142Smax.romanov@nginx.com while (str->length > 0 && str->start[str->length - 1] == t) { 144142Smax.romanov@nginx.com str->length--; 145142Smax.romanov@nginx.com } 146142Smax.romanov@nginx.com 147142Smax.romanov@nginx.com str->start[str->length] = '\0'; 148142Smax.romanov@nginx.com } 149142Smax.romanov@nginx.com 150142Smax.romanov@nginx.com 151142Smax.romanov@nginx.com static void 152142Smax.romanov@nginx.com nxt_php_str_trim_lead(nxt_str_t *str, u_char t) 153142Smax.romanov@nginx.com { 154142Smax.romanov@nginx.com while (str->length > 0 && str->start[0] == t) { 155142Smax.romanov@nginx.com str->length--; 156142Smax.romanov@nginx.com str->start++; 157142Smax.romanov@nginx.com } 158142Smax.romanov@nginx.com } 1590Sigor@sysoev.ru 160258Sigor@sysoev.ru static uint32_t compat[] = { 161360Sigor@sysoev.ru NXT_VERNUM, NXT_DEBUG, 162258Sigor@sysoev.ru }; 163258Sigor@sysoev.ru 1640Sigor@sysoev.ru 165216Sigor@sysoev.ru NXT_EXPORT nxt_application_module_t nxt_app_module = { 166258Sigor@sysoev.ru sizeof(compat), 167258Sigor@sysoev.ru compat, 168216Sigor@sysoev.ru nxt_string("php"), 169216Sigor@sysoev.ru nxt_string(PHP_VERSION), 17086Smax.romanov@nginx.com nxt_php_init, 171216Sigor@sysoev.ru nxt_php_run, 172421Smax.romanov@nginx.com NULL, 17386Smax.romanov@nginx.com }; 1740Sigor@sysoev.ru 1750Sigor@sysoev.ru 176462Smax.romanov@nginx.com nxt_inline u_char * 177462Smax.romanov@nginx.com nxt_realpath(const void *c) 178462Smax.romanov@nginx.com { 179462Smax.romanov@nginx.com return (u_char *) realpath(c, NULL); 180462Smax.romanov@nginx.com } 181462Smax.romanov@nginx.com 182462Smax.romanov@nginx.com 18386Smax.romanov@nginx.com static nxt_int_t 184142Smax.romanov@nginx.com nxt_php_init(nxt_task_t *task, nxt_common_app_conf_t *conf) 1850Sigor@sysoev.ru { 186462Smax.romanov@nginx.com u_char *p; 187462Smax.romanov@nginx.com nxt_str_t rpath; 188142Smax.romanov@nginx.com nxt_str_t *root, *path, *script, *index; 189142Smax.romanov@nginx.com nxt_php_app_conf_t *c; 190142Smax.romanov@nginx.com 191142Smax.romanov@nginx.com c = &conf->u.php; 192142Smax.romanov@nginx.com 193462Smax.romanov@nginx.com if (c->root == NULL) { 194564Svbart@nginx.com nxt_alert(task, "php root is empty"); 195142Smax.romanov@nginx.com return NXT_ERROR; 196142Smax.romanov@nginx.com } 197142Smax.romanov@nginx.com 198142Smax.romanov@nginx.com root = &nxt_php_root; 199142Smax.romanov@nginx.com path = &nxt_php_path; 200142Smax.romanov@nginx.com script = &nxt_php_script; 201142Smax.romanov@nginx.com index = &nxt_php_index; 202142Smax.romanov@nginx.com 203462Smax.romanov@nginx.com root->start = nxt_realpath(c->root); 204462Smax.romanov@nginx.com if (nxt_slow_path(root->start == NULL)) { 205564Svbart@nginx.com nxt_alert(task, "root realpath(%s) failed %E", c->root, nxt_errno); 206462Smax.romanov@nginx.com return NXT_ERROR; 207462Smax.romanov@nginx.com } 208462Smax.romanov@nginx.com 209462Smax.romanov@nginx.com root->length = nxt_strlen(root->start); 210142Smax.romanov@nginx.com 211142Smax.romanov@nginx.com nxt_php_str_trim_trail(root, '/'); 212142Smax.romanov@nginx.com 213142Smax.romanov@nginx.com if (c->script.length > 0) { 214142Smax.romanov@nginx.com nxt_php_str_trim_lead(&c->script, '/'); 215142Smax.romanov@nginx.com 216462Smax.romanov@nginx.com path->length = root->length + 1 + c->script.length; 217464Smax.romanov@nginx.com path->start = nxt_malloc(path->length + 1); 218462Smax.romanov@nginx.com if (nxt_slow_path(path->start == NULL)) { 219462Smax.romanov@nginx.com return NXT_ERROR; 220462Smax.romanov@nginx.com } 221142Smax.romanov@nginx.com 222462Smax.romanov@nginx.com p = nxt_cpymem(path->start, root->start, root->length); 223462Smax.romanov@nginx.com *p++ = '/'; 224462Smax.romanov@nginx.com 225464Smax.romanov@nginx.com p = nxt_cpymem(p, c->script.start, c->script.length); 226464Smax.romanov@nginx.com *p = '\0'; 227142Smax.romanov@nginx.com 228462Smax.romanov@nginx.com rpath.start = nxt_realpath(path->start); 229462Smax.romanov@nginx.com if (nxt_slow_path(rpath.start == NULL)) { 230564Svbart@nginx.com nxt_alert(task, "script realpath(%V) failed %E", path, nxt_errno); 231462Smax.romanov@nginx.com return NXT_ERROR; 232462Smax.romanov@nginx.com } 233462Smax.romanov@nginx.com 234462Smax.romanov@nginx.com rpath.length = nxt_strlen(rpath.start); 235142Smax.romanov@nginx.com 236462Smax.romanov@nginx.com if (!nxt_str_start(&rpath, root->start, root->length)) { 237564Svbart@nginx.com nxt_alert(task, "script is not under php root"); 238462Smax.romanov@nginx.com return NXT_ERROR; 239462Smax.romanov@nginx.com } 240142Smax.romanov@nginx.com 241462Smax.romanov@nginx.com nxt_free(path->start); 242462Smax.romanov@nginx.com 243462Smax.romanov@nginx.com *path = rpath; 244142Smax.romanov@nginx.com 245142Smax.romanov@nginx.com script->length = c->script.length + 1; 246462Smax.romanov@nginx.com script->start = nxt_malloc(script->length); 247462Smax.romanov@nginx.com if (nxt_slow_path(script->start == NULL)) { 248462Smax.romanov@nginx.com return NXT_ERROR; 249462Smax.romanov@nginx.com } 250462Smax.romanov@nginx.com 251142Smax.romanov@nginx.com script->start[0] = '/'; 252142Smax.romanov@nginx.com nxt_memcpy(script->start + 1, c->script.start, c->script.length); 253142Smax.romanov@nginx.com 254142Smax.romanov@nginx.com nxt_log_error(NXT_LOG_INFO, task->log, 255142Smax.romanov@nginx.com "(ABS_MODE) php script \"%V\" root: \"%V\"", 256142Smax.romanov@nginx.com script, root); 257277Sigor@sysoev.ru 258142Smax.romanov@nginx.com } else { 259142Smax.romanov@nginx.com nxt_log_error(NXT_LOG_INFO, task->log, 260142Smax.romanov@nginx.com "(non ABS_MODE) php root: \"%V\"", root); 261142Smax.romanov@nginx.com } 262142Smax.romanov@nginx.com 263142Smax.romanov@nginx.com if (c->index.length > 0) { 264462Smax.romanov@nginx.com index->length = c->index.length; 265462Smax.romanov@nginx.com index->start = nxt_malloc(index->length); 266462Smax.romanov@nginx.com if (nxt_slow_path(index->start == NULL)) { 267462Smax.romanov@nginx.com return NXT_ERROR; 268462Smax.romanov@nginx.com } 269462Smax.romanov@nginx.com 270462Smax.romanov@nginx.com nxt_memcpy(index->start, c->index.start, c->index.length); 271142Smax.romanov@nginx.com } 272142Smax.romanov@nginx.com 273142Smax.romanov@nginx.com sapi_startup(&nxt_php_sapi_module); 274142Smax.romanov@nginx.com nxt_php_startup(&nxt_php_sapi_module); 275142Smax.romanov@nginx.com 2760Sigor@sysoev.ru return NXT_OK; 2770Sigor@sysoev.ru } 2780Sigor@sysoev.ru 2790Sigor@sysoev.ru 28086Smax.romanov@nginx.com static nxt_int_t 28186Smax.romanov@nginx.com nxt_php_read_request(nxt_task_t *task, nxt_app_rmsg_t *rmsg, 28286Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx) 2830Sigor@sysoev.ru { 28486Smax.romanov@nginx.com u_char *p; 28586Smax.romanov@nginx.com size_t s; 286114Smax.romanov@nginx.com nxt_int_t rc; 28794Smax.romanov@nginx.com nxt_str_t script_name; 28886Smax.romanov@nginx.com nxt_app_request_header_t *h; 2890Sigor@sysoev.ru 29086Smax.romanov@nginx.com h = &ctx->r.header; 2910Sigor@sysoev.ru 292114Smax.romanov@nginx.com #define RC(S) \ 293114Smax.romanov@nginx.com do { \ 294114Smax.romanov@nginx.com rc = (S); \ 295114Smax.romanov@nginx.com if (nxt_slow_path(rc != NXT_OK)) { \ 296114Smax.romanov@nginx.com goto fail; \ 297114Smax.romanov@nginx.com } \ 298114Smax.romanov@nginx.com } while(0) 2990Sigor@sysoev.ru 300114Smax.romanov@nginx.com #define NXT_READ(dst) \ 301114Smax.romanov@nginx.com RC(nxt_app_msg_read_str(task, rmsg, (dst))) 302114Smax.romanov@nginx.com 303114Smax.romanov@nginx.com NXT_READ(&h->method); 304114Smax.romanov@nginx.com NXT_READ(&h->target); 305114Smax.romanov@nginx.com NXT_READ(&h->path); 306114Smax.romanov@nginx.com 307114Smax.romanov@nginx.com RC(nxt_app_msg_read_size(task, rmsg, &s)); 30886Smax.romanov@nginx.com if (s > 0) { 30986Smax.romanov@nginx.com s--; 310112Smax.romanov@nginx.com h->query.start = h->target.start + s; 311112Smax.romanov@nginx.com h->query.length = h->target.length - s; 3120Sigor@sysoev.ru 313112Smax.romanov@nginx.com if (h->path.start == NULL) { 314112Smax.romanov@nginx.com h->path.start = h->target.start; 315112Smax.romanov@nginx.com h->path.length = s - 1; 31686Smax.romanov@nginx.com } 3170Sigor@sysoev.ru } 3180Sigor@sysoev.ru 319112Smax.romanov@nginx.com if (h->path.start == NULL) { 320112Smax.romanov@nginx.com h->path = h->target; 321112Smax.romanov@nginx.com } 322112Smax.romanov@nginx.com 32386Smax.romanov@nginx.com if (nxt_php_path.start == NULL) { 324112Smax.romanov@nginx.com if (h->path.start[h->path.length - 1] == '/') { 325142Smax.romanov@nginx.com script_name = nxt_php_index; 326277Sigor@sysoev.ru 32794Smax.romanov@nginx.com } else { 32894Smax.romanov@nginx.com script_name.length = 0; 329463Smax.romanov@nginx.com script_name.start = NULL; 33094Smax.romanov@nginx.com } 33194Smax.romanov@nginx.com 332112Smax.romanov@nginx.com ctx->script.length = nxt_php_root.length + h->path.length + 33394Smax.romanov@nginx.com script_name.length; 334464Smax.romanov@nginx.com p = ctx->script.start = nxt_malloc(ctx->script.length + 1); 335462Smax.romanov@nginx.com if (nxt_slow_path(p == NULL)) { 336462Smax.romanov@nginx.com return NXT_ERROR; 337462Smax.romanov@nginx.com } 33886Smax.romanov@nginx.com 339462Smax.romanov@nginx.com p = nxt_cpymem(p, nxt_php_root.start, nxt_php_root.length); 340462Smax.romanov@nginx.com p = nxt_cpymem(p, h->path.start, h->path.length); 34194Smax.romanov@nginx.com 34294Smax.romanov@nginx.com if (script_name.length > 0) { 343464Smax.romanov@nginx.com p = nxt_cpymem(p, script_name.start, script_name.length); 34494Smax.romanov@nginx.com } 34594Smax.romanov@nginx.com 346464Smax.romanov@nginx.com *p = '\0'; 347464Smax.romanov@nginx.com 34886Smax.romanov@nginx.com } else { 34986Smax.romanov@nginx.com ctx->script = nxt_php_path; 35086Smax.romanov@nginx.com } 35186Smax.romanov@nginx.com 352114Smax.romanov@nginx.com NXT_READ(&h->version); 353114Smax.romanov@nginx.com 354114Smax.romanov@nginx.com NXT_READ(&ctx->r.remote); 355268Sigor@sysoev.ru NXT_READ(&ctx->r.local); 35686Smax.romanov@nginx.com 357114Smax.romanov@nginx.com NXT_READ(&h->host); 358114Smax.romanov@nginx.com NXT_READ(&h->cookie); 359114Smax.romanov@nginx.com NXT_READ(&h->content_type); 360114Smax.romanov@nginx.com NXT_READ(&h->content_length); 36186Smax.romanov@nginx.com 362114Smax.romanov@nginx.com RC(nxt_app_msg_read_size(task, rmsg, &s)); 36386Smax.romanov@nginx.com h->parsed_content_length = s; 36486Smax.romanov@nginx.com 365305Smax.romanov@nginx.com RC(nxt_app_msg_read_size(task, ctx->rmsg, &ctx->body_preread_size)); 366305Smax.romanov@nginx.com 367114Smax.romanov@nginx.com #undef NXT_READ 368114Smax.romanov@nginx.com #undef RC 36994Smax.romanov@nginx.com 37086Smax.romanov@nginx.com /* Further headers read moved to nxt_php_register_variables. */ 3710Sigor@sysoev.ru return NXT_OK; 372114Smax.romanov@nginx.com 373114Smax.romanov@nginx.com fail: 374114Smax.romanov@nginx.com 375114Smax.romanov@nginx.com return rc; 3760Sigor@sysoev.ru } 3770Sigor@sysoev.ru 3780Sigor@sysoev.ru 37986Smax.romanov@nginx.com static nxt_int_t 38086Smax.romanov@nginx.com nxt_php_run(nxt_task_t *task, 38186Smax.romanov@nginx.com nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *wmsg) 3820Sigor@sysoev.ru { 383114Smax.romanov@nginx.com nxt_int_t rc; 38486Smax.romanov@nginx.com zend_file_handle file_handle; 38586Smax.romanov@nginx.com nxt_php_run_ctx_t run_ctx; 38686Smax.romanov@nginx.com nxt_app_request_header_t *h; 3870Sigor@sysoev.ru 38886Smax.romanov@nginx.com nxt_memzero(&run_ctx, sizeof(run_ctx)); 38986Smax.romanov@nginx.com 39086Smax.romanov@nginx.com run_ctx.task = task; 39186Smax.romanov@nginx.com run_ctx.rmsg = rmsg; 39286Smax.romanov@nginx.com run_ctx.wmsg = wmsg; 39386Smax.romanov@nginx.com 39486Smax.romanov@nginx.com h = &run_ctx.r.header; 39586Smax.romanov@nginx.com 396114Smax.romanov@nginx.com rc = nxt_php_read_request(task, rmsg, &run_ctx); 397114Smax.romanov@nginx.com 398114Smax.romanov@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 399114Smax.romanov@nginx.com goto fail; 400114Smax.romanov@nginx.com } 40186Smax.romanov@nginx.com 40286Smax.romanov@nginx.com SG(server_context) = &run_ctx; 403112Smax.romanov@nginx.com SG(request_info).request_uri = (char *) h->target.start; 40486Smax.romanov@nginx.com SG(request_info).request_method = (char *) h->method.start; 4050Sigor@sysoev.ru 4060Sigor@sysoev.ru SG(request_info).proto_num = 1001; 4070Sigor@sysoev.ru 40886Smax.romanov@nginx.com SG(request_info).query_string = (char *) h->query.start; 40986Smax.romanov@nginx.com SG(request_info).content_length = h->parsed_content_length; 4100Sigor@sysoev.ru 41186Smax.romanov@nginx.com if (h->content_type.start != NULL) { 41286Smax.romanov@nginx.com SG(request_info).content_type = (char *) h->content_type.start; 4130Sigor@sysoev.ru } 4140Sigor@sysoev.ru 4150Sigor@sysoev.ru SG(sapi_headers).http_response_code = 200; 4160Sigor@sysoev.ru 4170Sigor@sysoev.ru SG(request_info).path_translated = NULL; 4180Sigor@sysoev.ru 4190Sigor@sysoev.ru file_handle.type = ZEND_HANDLE_FILENAME; 42086Smax.romanov@nginx.com file_handle.filename = (char *) run_ctx.script.start; 4210Sigor@sysoev.ru file_handle.free_filename = 0; 4220Sigor@sysoev.ru file_handle.opened_path = NULL; 4230Sigor@sysoev.ru 424142Smax.romanov@nginx.com nxt_debug(task, "handle.filename = '%s'", run_ctx.script.start); 425142Smax.romanov@nginx.com 42686Smax.romanov@nginx.com if (nxt_php_path.start != NULL) { 42786Smax.romanov@nginx.com nxt_debug(task, "run script %V in absolute mode", &nxt_php_path); 428277Sigor@sysoev.ru 42986Smax.romanov@nginx.com } else { 43086Smax.romanov@nginx.com nxt_debug(task, "run script %V", &run_ctx.script); 43186Smax.romanov@nginx.com } 4320Sigor@sysoev.ru 4330Sigor@sysoev.ru if (nxt_slow_path(php_request_startup() == FAILURE)) { 434142Smax.romanov@nginx.com nxt_debug(task, "php_request_startup() failed"); 435462Smax.romanov@nginx.com rc = NXT_ERROR; 43686Smax.romanov@nginx.com goto fail; 4370Sigor@sysoev.ru } 4380Sigor@sysoev.ru 4390Sigor@sysoev.ru php_execute_script(&file_handle TSRMLS_CC); 4400Sigor@sysoev.ru php_request_shutdown(NULL); 4410Sigor@sysoev.ru 44286Smax.romanov@nginx.com nxt_app_msg_flush(task, wmsg, 1); 44386Smax.romanov@nginx.com 444462Smax.romanov@nginx.com rc = NXT_OK; 44586Smax.romanov@nginx.com 44686Smax.romanov@nginx.com fail: 44786Smax.romanov@nginx.com 448462Smax.romanov@nginx.com if (run_ctx.script.start != nxt_php_path.start) { 449462Smax.romanov@nginx.com nxt_free(run_ctx.script.start); 450462Smax.romanov@nginx.com } 45186Smax.romanov@nginx.com 452462Smax.romanov@nginx.com return rc; 45386Smax.romanov@nginx.com } 45486Smax.romanov@nginx.com 45586Smax.romanov@nginx.com 45686Smax.romanov@nginx.com nxt_inline nxt_int_t 45786Smax.romanov@nginx.com nxt_php_write(nxt_php_run_ctx_t *ctx, const u_char *data, size_t len, 45886Smax.romanov@nginx.com nxt_bool_t flush, nxt_bool_t last) 45986Smax.romanov@nginx.com { 46086Smax.romanov@nginx.com nxt_int_t rc; 46186Smax.romanov@nginx.com 462114Smax.romanov@nginx.com if (len > 0) { 463114Smax.romanov@nginx.com rc = nxt_app_msg_write_raw(ctx->task, ctx->wmsg, data, len); 464277Sigor@sysoev.ru 465114Smax.romanov@nginx.com } else { 466114Smax.romanov@nginx.com rc = NXT_OK; 467114Smax.romanov@nginx.com } 46886Smax.romanov@nginx.com 46986Smax.romanov@nginx.com if (flush || last) { 47086Smax.romanov@nginx.com rc = nxt_app_msg_flush(ctx->task, ctx->wmsg, last); 47186Smax.romanov@nginx.com } 47286Smax.romanov@nginx.com 47386Smax.romanov@nginx.com return rc; 4740Sigor@sysoev.ru } 4750Sigor@sysoev.ru 4760Sigor@sysoev.ru 4770Sigor@sysoev.ru static int 4780Sigor@sysoev.ru nxt_php_startup(sapi_module_struct *sapi_module) 4790Sigor@sysoev.ru { 4800Sigor@sysoev.ru return php_module_startup(sapi_module, NULL, 0); 4810Sigor@sysoev.ru } 4820Sigor@sysoev.ru 4830Sigor@sysoev.ru 4840Sigor@sysoev.ru #ifdef NXT_PHP7 4850Sigor@sysoev.ru static size_t 4860Sigor@sysoev.ru nxt_php_unbuffered_write(const char *str, size_t str_length TSRMLS_DC) 4870Sigor@sysoev.ru #else 4880Sigor@sysoev.ru static int 4890Sigor@sysoev.ru nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC) 4900Sigor@sysoev.ru #endif 4910Sigor@sysoev.ru { 492114Smax.romanov@nginx.com nxt_int_t rc; 49386Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx; 4940Sigor@sysoev.ru 49586Smax.romanov@nginx.com ctx = SG(server_context); 4960Sigor@sysoev.ru 497114Smax.romanov@nginx.com rc = nxt_php_write(ctx, (u_char *) str, str_length, 1, 0); 498114Smax.romanov@nginx.com 499114Smax.romanov@nginx.com if (nxt_fast_path(rc == NXT_OK)) { 500114Smax.romanov@nginx.com return str_length; 501114Smax.romanov@nginx.com } 5020Sigor@sysoev.ru 503114Smax.romanov@nginx.com // TODO handle NXT_AGAIN 504114Smax.romanov@nginx.com php_handle_aborted_connection(); 505114Smax.romanov@nginx.com return 0; 506114Smax.romanov@nginx.com } 507114Smax.romanov@nginx.com 508114Smax.romanov@nginx.com 509114Smax.romanov@nginx.com static void 510114Smax.romanov@nginx.com nxt_php_flush(void *server_context) 511114Smax.romanov@nginx.com { 512114Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx; 513114Smax.romanov@nginx.com 514114Smax.romanov@nginx.com ctx = server_context; 515114Smax.romanov@nginx.com 516114Smax.romanov@nginx.com (void) nxt_app_msg_flush(ctx->task, ctx->wmsg, 0); 5170Sigor@sysoev.ru } 5180Sigor@sysoev.ru 5190Sigor@sysoev.ru 5200Sigor@sysoev.ru static int 5210Sigor@sysoev.ru nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) 5220Sigor@sysoev.ru { 5230Sigor@sysoev.ru size_t len; 524114Smax.romanov@nginx.com u_char *status, buf[64]; 525114Smax.romanov@nginx.com nxt_int_t rc; 526114Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx; 5270Sigor@sysoev.ru sapi_header_struct *h; 5280Sigor@sysoev.ru zend_llist_position zpos; 5290Sigor@sysoev.ru 5300Sigor@sysoev.ru static const u_char default_repsonse[] 531431Sigor@sysoev.ru = "Status: 200\r\n" 5320Sigor@sysoev.ru "\r\n"; 5330Sigor@sysoev.ru 534431Sigor@sysoev.ru static const u_char status_200[] = "Status: 200"; 53586Smax.romanov@nginx.com static const u_char cr_lf[] = "\r\n"; 53686Smax.romanov@nginx.com 53786Smax.romanov@nginx.com ctx = SG(server_context); 5380Sigor@sysoev.ru 539114Smax.romanov@nginx.com #define RC(S) \ 540114Smax.romanov@nginx.com do { \ 541114Smax.romanov@nginx.com rc = (S); \ 542114Smax.romanov@nginx.com if (nxt_slow_path(rc != NXT_OK)) { \ 543114Smax.romanov@nginx.com goto fail; \ 544114Smax.romanov@nginx.com } \ 545114Smax.romanov@nginx.com } while(0) 546114Smax.romanov@nginx.com 5470Sigor@sysoev.ru if (SG(request_info).no_headers == 1) { 548114Smax.romanov@nginx.com RC(nxt_php_write(ctx, default_repsonse, sizeof(default_repsonse) - 1, 549114Smax.romanov@nginx.com 1, 0)); 5500Sigor@sysoev.ru return SAPI_HEADER_SENT_SUCCESSFULLY; 5510Sigor@sysoev.ru } 5520Sigor@sysoev.ru 5530Sigor@sysoev.ru if (SG(sapi_headers).http_status_line) { 554114Smax.romanov@nginx.com status = (u_char *) SG(sapi_headers).http_status_line; 5550Sigor@sysoev.ru len = nxt_strlen(status); 5560Sigor@sysoev.ru 557431Sigor@sysoev.ru if (len < 12) { 558431Sigor@sysoev.ru goto fail; 559431Sigor@sysoev.ru } 560431Sigor@sysoev.ru 561431Sigor@sysoev.ru RC(nxt_php_write(ctx, status_200, sizeof(status_200) - 4, 0, 0)); 562431Sigor@sysoev.ru RC(nxt_php_write(ctx, status + 9, 3, 0, 0)); 5630Sigor@sysoev.ru 5640Sigor@sysoev.ru } else if (SG(sapi_headers).http_response_code) { 56586Smax.romanov@nginx.com status = nxt_sprintf(buf, buf + sizeof(buf), "%03d", 5660Sigor@sysoev.ru SG(sapi_headers).http_response_code); 56786Smax.romanov@nginx.com len = status - buf; 56886Smax.romanov@nginx.com 569431Sigor@sysoev.ru RC(nxt_php_write(ctx, status_200, sizeof(status_200) - 4, 0, 0)); 570114Smax.romanov@nginx.com RC(nxt_php_write(ctx, buf, len, 0, 0)); 5710Sigor@sysoev.ru 5720Sigor@sysoev.ru } else { 573431Sigor@sysoev.ru RC(nxt_php_write(ctx, status_200, sizeof(status_200) - 1, 0, 0)); 5740Sigor@sysoev.ru } 5750Sigor@sysoev.ru 576114Smax.romanov@nginx.com RC(nxt_php_write(ctx, cr_lf, sizeof(cr_lf) - 1, 0, 0)); 5770Sigor@sysoev.ru 5780Sigor@sysoev.ru h = zend_llist_get_first_ex(&sapi_headers->headers, &zpos); 5790Sigor@sysoev.ru 5800Sigor@sysoev.ru while (h) { 581114Smax.romanov@nginx.com RC(nxt_php_write(ctx, (u_char *) h->header, h->header_len, 0, 0)); 582114Smax.romanov@nginx.com RC(nxt_php_write(ctx, cr_lf, sizeof(cr_lf) - 1, 0, 0)); 5830Sigor@sysoev.ru 5840Sigor@sysoev.ru h = zend_llist_get_next_ex(&sapi_headers->headers, &zpos); 5850Sigor@sysoev.ru } 5860Sigor@sysoev.ru 587114Smax.romanov@nginx.com RC(nxt_php_write(ctx, cr_lf, sizeof(cr_lf) - 1, 1, 0)); 588114Smax.romanov@nginx.com 589114Smax.romanov@nginx.com #undef RC 5900Sigor@sysoev.ru 5910Sigor@sysoev.ru return SAPI_HEADER_SENT_SUCCESSFULLY; 592114Smax.romanov@nginx.com 593114Smax.romanov@nginx.com fail: 594114Smax.romanov@nginx.com 595114Smax.romanov@nginx.com // TODO handle NXT_AGAIN 596114Smax.romanov@nginx.com return SAPI_HEADER_SEND_FAILED; 5970Sigor@sysoev.ru } 5980Sigor@sysoev.ru 5990Sigor@sysoev.ru 6000Sigor@sysoev.ru #ifdef NXT_PHP7 6010Sigor@sysoev.ru static size_t 6020Sigor@sysoev.ru nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC) 6030Sigor@sysoev.ru #else 6040Sigor@sysoev.ru static int 6050Sigor@sysoev.ru nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC) 6060Sigor@sysoev.ru #endif 6070Sigor@sysoev.ru { 608206Smax.romanov@nginx.com size_t size, rest; 60986Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx; 61086Smax.romanov@nginx.com nxt_app_request_header_t *h; 6110Sigor@sysoev.ru 61286Smax.romanov@nginx.com ctx = SG(server_context); 61386Smax.romanov@nginx.com h = &ctx->r.header; 6140Sigor@sysoev.ru 615206Smax.romanov@nginx.com rest = (size_t) h->parsed_content_length - SG(read_post_bytes); 61686Smax.romanov@nginx.com 61786Smax.romanov@nginx.com nxt_debug(ctx->task, "nxt_php_read_post %O", rest); 6180Sigor@sysoev.ru 6190Sigor@sysoev.ru if (rest == 0) { 6200Sigor@sysoev.ru return 0; 6210Sigor@sysoev.ru } 6220Sigor@sysoev.ru 623206Smax.romanov@nginx.com rest = nxt_min(ctx->body_preread_size, (size_t) count_bytes); 624206Smax.romanov@nginx.com size = nxt_app_msg_read_raw(ctx->task, ctx->rmsg, buffer, rest); 6250Sigor@sysoev.ru 626206Smax.romanov@nginx.com ctx->body_preread_size -= size; 6270Sigor@sysoev.ru 62886Smax.romanov@nginx.com return size; 6290Sigor@sysoev.ru } 6300Sigor@sysoev.ru 6310Sigor@sysoev.ru 6320Sigor@sysoev.ru static char * 6330Sigor@sysoev.ru nxt_php_read_cookies(TSRMLS_D) 6340Sigor@sysoev.ru { 63586Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx; 6360Sigor@sysoev.ru 63786Smax.romanov@nginx.com ctx = SG(server_context); 6380Sigor@sysoev.ru 63994Smax.romanov@nginx.com nxt_debug(ctx->task, "nxt_php_read_cookies"); 64094Smax.romanov@nginx.com 64186Smax.romanov@nginx.com return (char *) ctx->r.header.cookie.start; 6420Sigor@sysoev.ru } 6430Sigor@sysoev.ru 6440Sigor@sysoev.ru 6450Sigor@sysoev.ru static void 6460Sigor@sysoev.ru nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) 6470Sigor@sysoev.ru { 648114Smax.romanov@nginx.com u_char *colon; 64986Smax.romanov@nginx.com nxt_str_t n, v; 65086Smax.romanov@nginx.com nxt_int_t rc; 651114Smax.romanov@nginx.com nxt_str_t host, server_name, server_port; 65286Smax.romanov@nginx.com nxt_task_t *task; 65386Smax.romanov@nginx.com nxt_php_run_ctx_t *ctx; 65486Smax.romanov@nginx.com nxt_app_request_header_t *h; 6550Sigor@sysoev.ru 656114Smax.romanov@nginx.com static nxt_str_t def_host = nxt_string("localhost"); 657114Smax.romanov@nginx.com static nxt_str_t def_port = nxt_string("80"); 658114Smax.romanov@nginx.com 65986Smax.romanov@nginx.com ctx = SG(server_context); 6600Sigor@sysoev.ru 66186Smax.romanov@nginx.com h = &ctx->r.header; 66286Smax.romanov@nginx.com task = ctx->task; 6630Sigor@sysoev.ru 66486Smax.romanov@nginx.com nxt_debug(task, "php register variables"); 6650Sigor@sysoev.ru 666114Smax.romanov@nginx.com #define NXT_PHP_SET(n, v) \ 667142Smax.romanov@nginx.com nxt_debug(task, "php: register %s='%V'", n, &v); \ 668114Smax.romanov@nginx.com php_register_variable_safe((char *) (n), (char *) (v).start, \ 669114Smax.romanov@nginx.com (v).length, track_vars_array TSRMLS_CC) \ 670114Smax.romanov@nginx.com 671114Smax.romanov@nginx.com NXT_PHP_SET("SERVER_PROTOCOL", h->version); 67286Smax.romanov@nginx.com 67386Smax.romanov@nginx.com /* 67486Smax.romanov@nginx.com * 'SCRIPT_NAME' 67586Smax.romanov@nginx.com * Contains the current script's path. This is useful for pages which need to 67686Smax.romanov@nginx.com * point to themselves. The __FILE__ constant contains the full path and 67786Smax.romanov@nginx.com * filename of the current (i.e. included) file. 67886Smax.romanov@nginx.com */ 67986Smax.romanov@nginx.com 68086Smax.romanov@nginx.com /* 68186Smax.romanov@nginx.com * 'SCRIPT_FILENAME' 68286Smax.romanov@nginx.com * The absolute pathname of the currently executing script. 68386Smax.romanov@nginx.com */ 68486Smax.romanov@nginx.com 68586Smax.romanov@nginx.com /* 68686Smax.romanov@nginx.com * 'DOCUMENT_ROOT' 68786Smax.romanov@nginx.com * The document root directory under which the current script is executing, 68886Smax.romanov@nginx.com * as defined in the server's configuration file. 68986Smax.romanov@nginx.com */ 6900Sigor@sysoev.ru 69186Smax.romanov@nginx.com if (nxt_php_script.start != NULL) { 69286Smax.romanov@nginx.com // ABS_MODE 69386Smax.romanov@nginx.com /* 69486Smax.romanov@nginx.com * 'PHP_SELF' 69586Smax.romanov@nginx.com * The filename of the currently executing script, relative to the document 69686Smax.romanov@nginx.com * root. For instance, $_SERVER['PHP_SELF'] in a script at the address 69786Smax.romanov@nginx.com * http://example.com/foo/bar.php would be /foo/bar.php. The __FILE__ constant 69886Smax.romanov@nginx.com * contains the full path and filename of the current (i.e. included) file. 69986Smax.romanov@nginx.com * If PHP is running as a command-line processor this variable contains the 70086Smax.romanov@nginx.com * script name since PHP 4.3.0. Previously it was not available. 70186Smax.romanov@nginx.com */ 702114Smax.romanov@nginx.com NXT_PHP_SET("PHP_SELF", nxt_php_script); 703114Smax.romanov@nginx.com NXT_PHP_SET("SCRIPT_NAME", nxt_php_script); 704277Sigor@sysoev.ru 70586Smax.romanov@nginx.com } else { 706114Smax.romanov@nginx.com NXT_PHP_SET("PHP_SELF", h->path); 707114Smax.romanov@nginx.com NXT_PHP_SET("SCRIPT_NAME", h->path); 70886Smax.romanov@nginx.com } 7090Sigor@sysoev.ru 710114Smax.romanov@nginx.com NXT_PHP_SET("SCRIPT_FILENAME", ctx->script); 711114Smax.romanov@nginx.com NXT_PHP_SET("DOCUMENT_ROOT", nxt_php_root); 7120Sigor@sysoev.ru 713114Smax.romanov@nginx.com NXT_PHP_SET("REQUEST_METHOD", h->method); 714114Smax.romanov@nginx.com NXT_PHP_SET("REQUEST_URI", h->target); 7150Sigor@sysoev.ru 71686Smax.romanov@nginx.com if (h->query.start != NULL) { 717114Smax.romanov@nginx.com NXT_PHP_SET("QUERY_STRING", h->query); 7180Sigor@sysoev.ru } 7190Sigor@sysoev.ru 72086Smax.romanov@nginx.com if (h->content_type.start != NULL) { 721114Smax.romanov@nginx.com NXT_PHP_SET("CONTENT_TYPE", h->content_type); 7220Sigor@sysoev.ru } 7230Sigor@sysoev.ru 72486Smax.romanov@nginx.com if (h->content_length.start != NULL) { 725114Smax.romanov@nginx.com NXT_PHP_SET("CONTENT_LENGTH", h->content_length); 726114Smax.romanov@nginx.com } 727114Smax.romanov@nginx.com 728114Smax.romanov@nginx.com host = h->host; 729114Smax.romanov@nginx.com if (host.length == 0) { 730114Smax.romanov@nginx.com host = def_host; 73186Smax.romanov@nginx.com } 7320Sigor@sysoev.ru 733114Smax.romanov@nginx.com server_name = host; 734114Smax.romanov@nginx.com colon = nxt_memchr(host.start, ':', host.length); 735114Smax.romanov@nginx.com 736114Smax.romanov@nginx.com if (colon != NULL) { 737114Smax.romanov@nginx.com server_name.length = colon - host.start; 738114Smax.romanov@nginx.com 739114Smax.romanov@nginx.com server_port.start = colon + 1; 740114Smax.romanov@nginx.com server_port.length = host.length - server_name.length - 1; 741277Sigor@sysoev.ru 742114Smax.romanov@nginx.com } else { 743114Smax.romanov@nginx.com server_port = def_port; 744114Smax.romanov@nginx.com } 745114Smax.romanov@nginx.com 746114Smax.romanov@nginx.com NXT_PHP_SET("SERVER_NAME", server_name); 747114Smax.romanov@nginx.com NXT_PHP_SET("SERVER_PORT", server_port); 748114Smax.romanov@nginx.com 749114Smax.romanov@nginx.com NXT_PHP_SET("REMOTE_ADDR", ctx->r.remote); 750268Sigor@sysoev.ru NXT_PHP_SET("SERVER_ADDR", ctx->r.local); 751114Smax.romanov@nginx.com 75294Smax.romanov@nginx.com while (nxt_app_msg_read_str(task, ctx->rmsg, &n) == NXT_OK) { 75386Smax.romanov@nginx.com if (nxt_slow_path(n.length == 0)) { 75486Smax.romanov@nginx.com break; 7550Sigor@sysoev.ru } 7560Sigor@sysoev.ru 75794Smax.romanov@nginx.com rc = nxt_app_msg_read_str(task, ctx->rmsg, &v); 75894Smax.romanov@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 75994Smax.romanov@nginx.com break; 76094Smax.romanov@nginx.com } 76194Smax.romanov@nginx.com 762114Smax.romanov@nginx.com NXT_PHP_SET(n.start, v); 7630Sigor@sysoev.ru } 764114Smax.romanov@nginx.com 765114Smax.romanov@nginx.com #undef NXT_PHP_SET 7660Sigor@sysoev.ru } 7670Sigor@sysoev.ru 7680Sigor@sysoev.ru 769*579Svbart@nginx.com #ifdef NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 7700Sigor@sysoev.ru static void 771*579Svbart@nginx.com nxt_php_log_message(char *message, int syslog_type_int) 772*579Svbart@nginx.com #else 773*579Svbart@nginx.com static void 774*579Svbart@nginx.com nxt_php_log_message(char *message) 77586Smax.romanov@nginx.com #endif 7760Sigor@sysoev.ru { 777*579Svbart@nginx.com nxt_thread_log_error(NXT_LOG_NOTICE, "php message: %s", message); 7780Sigor@sysoev.ru } 779