typedef struct {
ngx_str_t content;
+ ngx_str_t header_filter;
} ngx_http_js_loc_conf_t;
static void ngx_http_js_content_write_event_handler(ngx_http_request_t *r);
static void ngx_http_js_content_finalize(ngx_http_request_t *r,
ngx_http_js_ctx_t *ctx);
+static ngx_int_t ngx_http_js_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_js_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_js_init_vm(ngx_http_request_t *r);
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_include(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_js_import(ngx_conf_t *cf, ngx_command_t *cmd,
0,
NULL },
+ { ngx_string("js_header_filter"),
+ NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_str_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_js_loc_conf_t, header_filter),
+ NULL },
+
ngx_null_command
};
static ngx_http_module_t ngx_http_js_module_ctx = {
NULL, /* preconfiguration */
- NULL, /* postconfiguration */
+ ngx_http_js_init, /* postconfiguration */
ngx_http_js_create_main_conf, /* create main configuration */
ngx_http_js_init_main_conf, /* init main configuration */
};
+static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
+
+
static njs_external_t ngx_http_js_ext_request[] = {
{
}
+static ngx_int_t
+ngx_http_js_header_filter(ngx_http_request_t *r)
+{
+ ngx_int_t rc;
+ njs_int_t pending;
+ ngx_http_js_ctx_t *ctx;
+ ngx_http_js_loc_conf_t *jlcf;
+
+ jlcf = ngx_http_get_module_loc_conf(r, ngx_http_js_module);
+
+ if (jlcf->header_filter.len == 0) {
+ return ngx_http_next_header_filter(r);
+ }
+
+ rc = ngx_http_js_init_vm(r);
+
+ if (rc == NGX_ERROR || rc == NGX_DECLINED) {
+ return NGX_ERROR;
+ }
+
+ ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
+
+ pending = njs_vm_pending(ctx->vm);
+
+ rc = ngx_js_call(ctx->vm, &jlcf->header_filter, &ctx->request,
+ r->connection->log);
+
+ if (rc == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ if (!pending && rc == NGX_AGAIN) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "async operation inside \"%V\" header filter",
+ &jlcf->header_filter);
+ return NGX_ERROR;
+ }
+
+ return ngx_http_next_header_filter(r);
+}
+
+
static ngx_int_t
ngx_http_js_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
}
+static ngx_int_t
+ngx_http_js_init(ngx_conf_t *cf)
+{
+ ngx_http_next_header_filter = ngx_http_top_header_filter;
+ ngx_http_top_header_filter = ngx_http_js_header_filter;
+
+ return NGX_OK;
+}
+
+
static char *
ngx_http_js_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
* set by ngx_pcalloc():
*
* conf->content = { 0, NULL };
+ * conf->header_filter = { 0, NULL };
*/
return conf;
ngx_http_js_loc_conf_t *conf = child;
ngx_conf_merge_str_value(conf->content, prev->content, "");
+ ngx_conf_merge_str_value(conf->header_filter, prev->header_filter, "");
return NGX_CONF_OK;
}