diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nxt_http_js.c | 133 | ||||
-rw-r--r-- | src/nxt_php_sapi.c | 69 | ||||
-rw-r--r-- | src/nxt_runtime.c | 31 |
3 files changed, 173 insertions, 60 deletions
diff --git a/src/nxt_http_js.c b/src/nxt_http_js.c index 5a08a309..72ba761f 100644 --- a/src/nxt_http_js.c +++ b/src/nxt_http_js.c @@ -15,15 +15,19 @@ static njs_int_t nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop, static njs_int_t nxt_http_js_ext_remote_addr(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); -static njs_int_t nxt_http_js_ext_get_arg(njs_vm_t *vm, +static njs_int_t nxt_http_js_ext_get_args(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); static njs_int_t nxt_http_js_ext_get_header(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); +static njs_int_t nxt_http_js_ext_keys_header(njs_vm_t *vm, + njs_value_t *value, njs_value_t *keys); static njs_int_t nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); +static njs_int_t nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, + njs_value_t *keys); static njs_external_t nxt_http_js_proto[] = { @@ -55,12 +59,11 @@ static njs_external_t nxt_http_js_proto[] = { }, { - .flags = NJS_EXTERN_OBJECT, + .flags = NJS_EXTERN_PROPERTY, .name.string = njs_str("args"), .enumerable = 1, - .u.object = { - .enumerable = 1, - .prop_handler = nxt_http_js_ext_get_arg, + .u.property = { + .handler = nxt_http_js_ext_get_args, } }, @@ -71,6 +74,7 @@ static njs_external_t nxt_http_js_proto[] = { .u.object = { .enumerable = 1, .prop_handler = nxt_http_js_ext_get_header, + .keys = nxt_http_js_ext_keys_header, } }, @@ -81,6 +85,7 @@ static njs_external_t nxt_http_js_proto[] = { .u.object = { .enumerable = 1, .prop_handler = nxt_http_js_ext_get_cookie, + .keys = nxt_http_js_ext_keys_cookie, } }, }; @@ -144,14 +149,13 @@ nxt_http_js_ext_remote_addr(njs_vm_t *vm, njs_object_prop_t *prop, static njs_int_t -nxt_http_js_ext_get_arg(njs_vm_t *vm, njs_object_prop_t *prop, +nxt_http_js_ext_get_args(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { - njs_int_t rc; - njs_str_t key; - nxt_array_t *args; - nxt_http_request_t *r; - nxt_http_name_value_t *nv, *start, *end; + njs_int_t ret; + njs_value_t *args; + njs_opaque_value_t val; + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -159,33 +163,18 @@ nxt_http_js_ext_get_arg(njs_vm_t *vm, njs_object_prop_t *prop, return NJS_DECLINED; } - rc = njs_vm_prop_name(vm, prop, &key); - if (rc != NJS_OK) { - njs_value_undefined_set(retval); - return NJS_DECLINED; - } - - args = nxt_http_arguments_parse(r); - if (nxt_slow_path(args == NULL)) { - return NJS_ERROR; - } - - start = args->elts; - end = start + args->nelts; + args = njs_value_arg(&val); - for (nv = start; nv < end; nv++) { + ret = njs_vm_query_string_parse(vm, r->args->start, + r->args->start + r->args->length, args); - if (key.length == nv->name_length - && memcmp(key.start, nv->name, nv->name_length) == 0) - { - return njs_vm_value_string_set(vm, retval, nv->value, - nv->value_length); - } + if (ret == NJS_ERROR) { + return NJS_ERROR; } - njs_value_undefined_set(retval); + njs_value_assign(retval, args); - return NJS_DECLINED; + return NJS_OK; } @@ -228,6 +217,41 @@ nxt_http_js_ext_get_header(njs_vm_t *vm, njs_object_prop_t *prop, static njs_int_t +nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) +{ + njs_int_t rc; + nxt_http_field_t *f; + nxt_http_request_t *r; + + rc = njs_vm_array_alloc(vm, keys, 4); + if (rc != NJS_OK) { + return NJS_ERROR; + } + + r = njs_vm_external(vm, nxt_js_proto_id, value); + if (r == NULL) { + return NJS_OK; + } + + nxt_list_each(f, r->fields) { + + value = njs_vm_array_push(vm, keys); + if (value == NULL) { + return NJS_ERROR; + } + + rc = njs_vm_value_string_set(vm, value, f->name, f->name_length); + if (rc != NJS_OK) { + return NJS_ERROR; + } + + } nxt_list_loop; + + return NJS_OK; +} + + +static njs_int_t nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { @@ -271,3 +295,46 @@ nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, return NJS_DECLINED; } + + +static njs_int_t +nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) +{ + njs_int_t rc; + nxt_array_t *cookies; + nxt_http_request_t *r; + nxt_http_name_value_t *nv, *start, *end; + + rc = njs_vm_array_alloc(vm, keys, 4); + if (rc != NJS_OK) { + return NJS_ERROR; + } + + r = njs_vm_external(vm, nxt_js_proto_id, value); + if (r == NULL) { + return NJS_OK; + } + + cookies = nxt_http_cookies_parse(r); + if (nxt_slow_path(cookies == NULL)) { + return NJS_ERROR; + } + + start = cookies->elts; + end = start + cookies->nelts; + + for (nv = start; nv < end; nv++) { + + value = njs_vm_array_push(vm, keys); + if (value == NULL) { + return NJS_ERROR; + } + + rc = njs_vm_value_string_set(vm, value, nv->name, nv->name_length); + if (rc != NJS_OK) { + return NJS_ERROR; + } + } + + return NJS_OK; +} diff --git a/src/nxt_php_sapi.c b/src/nxt_php_sapi.c index d2494938..32a13a70 100644 --- a/src/nxt_php_sapi.c +++ b/src/nxt_php_sapi.c @@ -102,10 +102,15 @@ static void nxt_php_str_trim_lead(nxt_str_t *str, u_char t); nxt_inline u_char *nxt_realpath(const void *c); static nxt_int_t nxt_php_do_301(nxt_unit_request_info_t *req); +static nxt_int_t nxt_php_handle_fs_err(nxt_unit_request_info_t *req); static void nxt_php_request_handler(nxt_unit_request_info_t *req); static void nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r); +#if (PHP_VERSION_ID < 70400) +static void nxt_zend_stream_init_fp(zend_file_handle *handle, FILE *fp, + const char *filename); +#endif static void nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r); nxt_inline void nxt_php_vcwd_chdir(nxt_unit_request_info_t *req, u_char *dir); @@ -980,6 +985,24 @@ nxt_php_do_301(nxt_unit_request_info_t *req) } +static nxt_int_t +nxt_php_handle_fs_err(nxt_unit_request_info_t *req) +{ + switch (nxt_errno) { + case ELOOP: + case EACCES: + case ENFILE: + return nxt_unit_response_init(req, NXT_HTTP_FORBIDDEN, 0, 0); + case ENOENT: + case ENOTDIR: + case ENAMETOOLONG: + return nxt_unit_response_init(req, NXT_HTTP_NOT_FOUND, 0, 0); + } + + return NXT_UNIT_ERROR; +} + + static void nxt_php_request_handler(nxt_unit_request_info_t *req) { @@ -1058,6 +1081,8 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) ret = stat(tpath, &sb); if (ret == 0 && S_ISDIR(sb.st_mode)) { ec = nxt_php_do_301(ctx->req); + } else if (ret == -1) { + ec = nxt_php_handle_fs_err(ctx->req); } nxt_unit_request_done(ctx->req, ec); @@ -1109,17 +1134,46 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) } +#if (PHP_VERSION_ID < 70400) +static void +nxt_zend_stream_init_fp(zend_file_handle *handle, FILE *fp, + const char *filename) +{ + nxt_memzero(handle, sizeof(zend_file_handle)); + handle->type = ZEND_HANDLE_FP; + handle->handle.fp = fp; + handle->filename = filename; +} +#else +#define nxt_zend_stream_init_fp zend_stream_init_fp +#endif + + static void nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) { + FILE *fp; #if (PHP_VERSION_ID < 50600) void *read_post; #endif + const char *filename; nxt_unit_field_t *f; zend_file_handle file_handle; - nxt_unit_req_debug(ctx->req, "PHP execute script %s", - ctx->script_filename.start); + filename = (const char *) ctx->script_filename.start; + + nxt_unit_req_debug(ctx->req, "PHP execute script %s", filename); + + fp = fopen(filename, "re"); + if (fp == NULL) { + nxt_int_t ec; + + nxt_unit_req_debug(ctx->req, "PHP fopen(\"%s\") failed", filename); + + ec = nxt_php_handle_fs_err(ctx->req); + nxt_unit_request_done(ctx->req, ec); + return; + } SG(server_context) = ctx; SG(options) |= SAPI_OPTION_NO_CHDIR; @@ -1179,16 +1233,7 @@ nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) nxt_php_vcwd_chdir(ctx->req, ctx->script_dirname.start); } - nxt_memzero(&file_handle, sizeof(file_handle)); - - file_handle.type = ZEND_HANDLE_FILENAME; -#if (PHP_VERSION_ID >= 80100) - file_handle.filename = zend_string_init((char *) ctx->script_filename.start, - ctx->script_filename.length, 0); - file_handle.primary_script = 1; -#else - file_handle.filename = (char *) ctx->script_filename.start; -#endif + nxt_zend_stream_init_fp(&file_handle, fp, filename); php_execute_script(&file_handle TSRMLS_CC); diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c index c7e4455e..cb5fe421 100644 --- a/src/nxt_runtime.c +++ b/src/nxt_runtime.c @@ -768,10 +768,10 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) rt->group = NXT_GROUP; rt->pid = NXT_PID; rt->log = NXT_LOG; - rt->modules = NXT_MODULES; - rt->state = NXT_STATE; + rt->modules = NXT_MODULESDIR; + rt->state = NXT_LIBSTATEDIR; rt->control = NXT_CONTROL_SOCK; - rt->tmp = NXT_TMP; + rt->tmp = NXT_TMPDIR; nxt_memzero(&rt->capabilities, sizeof(nxt_capabilities_t)); @@ -927,9 +927,10 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) static const char no_pid[] = "option \"--pid\" requires filename\n"; static const char no_log[] = "option \"--log\" requires filename\n"; static const char no_modules[] = - "option \"--modules\" requires directory\n"; - static const char no_state[] = "option \"--state\" requires directory\n"; - static const char no_tmp[] = "option \"--tmp\" requires directory\n"; + "option \"--modulesdir\" requires directory\n"; + static const char no_state[] = + "option \"--libstatedir\" requires directory\n"; + static const char no_tmp[] = "option \"--tmpdir\" requires directory\n"; static const char help[] = "\n" @@ -948,14 +949,14 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) " --log FILE set log filename\n" " default: \"" NXT_LOG "\"\n" "\n" - " --modules DIRECTORY set modules directory name\n" - " default: \"" NXT_MODULES "\"\n" + " --modulesdir DIR set modules directory name\n" + " default: \"" NXT_MODULESDIR "\"\n" "\n" - " --state DIRECTORY set state directory name\n" - " default: \"" NXT_STATE "\"\n" + " --libstatedir DIR set state directory name\n" + " default: \"" NXT_LIBSTATEDIR "\"\n" "\n" - " --tmp DIRECTORY set tmp directory name\n" - " default: \"" NXT_TMP "\"\n" + " --tmpdir DIR set tmp directory name\n" + " default: \"" NXT_TMPDIR "\"\n" "\n" " --user USER set non-privileged processes to run" " as specified user\n" @@ -1038,7 +1039,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) continue; } - if (nxt_strcmp(p, "--modules") == 0) { + if (nxt_strcmp(p, "--modulesdir") == 0) { if (*argv == NULL) { write(STDERR_FILENO, no_modules, nxt_length(no_modules)); return NXT_ERROR; @@ -1051,7 +1052,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) continue; } - if (nxt_strcmp(p, "--state") == 0) { + if (nxt_strcmp(p, "--libstatedir") == 0) { if (*argv == NULL) { write(STDERR_FILENO, no_state, nxt_length(no_state)); return NXT_ERROR; @@ -1064,7 +1065,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) continue; } - if (nxt_strcmp(p, "--tmp") == 0) { + if (nxt_strcmp(p, "--tmpdir") == 0) { if (*argv == NULL) { write(STDERR_FILENO, no_tmp, nxt_length(no_tmp)); return NXT_ERROR; |