diff options
Diffstat (limited to 'nginx/ngx_http_js_module.c')
-rw-r--r-- | nginx/ngx_http_js_module.c | 160 |
1 files changed, 21 insertions, 139 deletions
diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c index 9a1f977e..e4db83fa 100644 --- a/nginx/ngx_http_js_module.c +++ b/nginx/ngx_http_js_module.c @@ -12,15 +12,8 @@ #include "ngx_js.h" -#define NJS_HEADER_SEMICOLON 0x1 -#define NJS_HEADER_SINGLE 0x2 -#define NJS_HEADER_ARRAY 0x4 - - typedef struct { - njs_vm_t *vm; - ngx_array_t *imports; - ngx_array_t *paths; + NGX_JS_COMMON_CONF; ngx_str_t content; ngx_str_t header_filter; @@ -42,12 +35,9 @@ typedef struct { } ngx_http_js_loc_conf_t; -typedef struct { - ngx_str_t name; - ngx_str_t path; - u_char *file; - ngx_uint_t line; -} ngx_http_js_import_t; +#define NJS_HEADER_SEMICOLON 0x1 +#define NJS_HEADER_SINGLE 0x2 +#define NJS_HEADER_ARRAY 0x4 typedef struct { @@ -263,8 +253,6 @@ static void ngx_http_js_handle_event(ngx_http_request_t *r, njs_vm_event_t vm_event, njs_value_t *args, njs_uint_t nargs); static ngx_int_t ngx_http_js_init(ngx_conf_t *cf); -static char *ngx_http_js_import(ngx_conf_t *cf, ngx_command_t *cmd, - void *conf); static char *ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_js_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_js_content(ngx_conf_t *cf, ngx_command_t *cmd, @@ -301,7 +289,7 @@ static ngx_command_t ngx_http_js_commands[] = { { ngx_string("js_import"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13, - ngx_http_js_import, + ngx_js_import, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL }, @@ -4182,10 +4170,10 @@ static ngx_int_t ngx_http_js_merge_vm(ngx_conf_t *cf, ngx_http_js_loc_conf_t *conf, ngx_http_js_loc_conf_t *prev) { - ngx_str_t *path, *s; - ngx_uint_t i; - ngx_array_t *imports, *paths; - ngx_http_js_import_t *import, *pi; + ngx_str_t *path, *s; + ngx_uint_t i; + ngx_array_t *imports, *paths; + ngx_js_named_path_t *import, *pi; if (prev->imports != NGX_CONF_UNSET_PTR && prev->vm == NULL) { if (ngx_http_js_init_conf_vm(cf, prev) != NGX_OK) { @@ -4210,7 +4198,7 @@ ngx_http_js_merge_vm(ngx_conf_t *cf, ngx_http_js_loc_conf_t *conf, } else { imports = ngx_array_create(cf->pool, 4, - sizeof(ngx_http_js_import_t)); + sizeof(ngx_js_named_path_t)); if (imports == NULL) { return NGX_ERROR; } @@ -4288,17 +4276,17 @@ ngx_http_js_merge_vm(ngx_conf_t *cf, ngx_http_js_loc_conf_t *conf, static ngx_int_t ngx_http_js_init_conf_vm(ngx_conf_t *cf, ngx_http_js_loc_conf_t *conf) { - size_t size; - u_char *start, *end, *p; - ngx_str_t *m, file; - njs_int_t rc; - njs_str_t text, path; - ngx_uint_t i; - njs_value_t *value; - njs_vm_opt_t options; - ngx_pool_cleanup_t *cln; - njs_opaque_value_t lvalue, exception; - ngx_http_js_import_t *import; + size_t size; + u_char *start, *end, *p; + ngx_str_t *m, file; + njs_int_t rc; + njs_str_t text, path; + ngx_uint_t i; + njs_value_t *value; + njs_vm_opt_t options; + ngx_pool_cleanup_t *cln; + njs_opaque_value_t lvalue, exception; + ngx_js_named_path_t *import; static const njs_str_t line_number_key = njs_str("lineNumber"); static const njs_str_t file_name_key = njs_str("fileName"); @@ -4466,112 +4454,6 @@ ngx_http_js_init(ngx_conf_t *cf) static char * -ngx_http_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) -{ - ngx_http_js_loc_conf_t *jlcf = conf; - - u_char *p, *end, c; - ngx_int_t from; - ngx_str_t *value, name, path; - ngx_http_js_import_t *import; - - value = cf->args->elts; - from = (cf->args->nelts == 4); - - if (from) { - if (ngx_strcmp(value[2].data, "from") != 0) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid parameter \"%V\"", &value[2]); - return NGX_CONF_ERROR; - } - } - - name = value[1]; - path = (from ? value[3] : value[1]); - - if (!from) { - end = name.data + name.len; - - for (p = end - 1; p >= name.data; p--) { - if (*p == '/') { - break; - } - } - - name.data = p + 1; - name.len = end - p - 1; - - if (name.len < 3 - || ngx_memcmp(&name.data[name.len - 3], ".js", 3) != 0) - { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "cannot extract export name from file path " - "\"%V\", use extended \"from\" syntax", &path); - return NGX_CONF_ERROR; - } - - name.len -= 3; - } - - if (name.len == 0) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty export name"); - return NGX_CONF_ERROR; - } - - p = name.data; - end = name.data + name.len; - - while (p < end) { - c = ngx_tolower(*p); - - if (*p != '_' && (c < 'a' || c > 'z')) { - if (p == name.data) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "cannot start " - "with \"%c\" in export name \"%V\"", *p, - &name); - return NGX_CONF_ERROR; - } - - if (*p < '0' || *p > '9') { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character " - "\"%c\" in export name \"%V\"", *p, - &name); - return NGX_CONF_ERROR; - } - } - - p++; - } - - if (ngx_strchr(path.data, '\'') != NULL) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid character \"'\" " - "in file path \"%V\"", &path); - return NGX_CONF_ERROR; - } - - if (jlcf->imports == NGX_CONF_UNSET_PTR) { - jlcf->imports = ngx_array_create(cf->pool, 4, - sizeof(ngx_http_js_import_t)); - if (jlcf->imports == NULL) { - return NGX_CONF_ERROR; - } - } - - import = ngx_array_push(jlcf->imports); - if (import == NULL) { - return NGX_CONF_ERROR; - } - - import->name = name; - import->path = path; - import->file = cf->conf_file->file.name.data; - import->line = cf->conf_file->line; - - return NGX_CONF_OK; -} - - -static char * ngx_http_js_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value, *fname; |