📄 ngx_http_ssi_filter_module.c
字号:
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "inlcusion may be either virtual=\"%V\" or file=\"%V\"", uri, file); return NGX_HTTP_SSI_ERROR; } if (uri == NULL && file == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no parameter in \"include\" SSI command"); return NGX_HTTP_SSI_ERROR; } if (set && stub) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"set\" and \"stub\" may not be used together " "in \"include\" SSI command"); return NGX_HTTP_SSI_ERROR; } if (wait) { if (uri == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"wait\" may not be used with file=\"%V\"", file); return NGX_HTTP_SSI_ERROR; } if (wait->len == 2 && ngx_strncasecmp(wait->data, (u_char *) "no", 2) == 0) { wait = NULL; } else if (wait->len != 3 || ngx_strncasecmp(wait->data, (u_char *) "yes", 3) != 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "invalid value \"%V\" in the \"wait\" parameter", wait); return NGX_HTTP_SSI_ERROR; } } if (uri == NULL) { uri = file; } rc = ngx_http_ssi_evaluate_string(r, ctx, uri, NGX_HTTP_SSI_ADD_PREFIX); if (rc != NGX_OK) { return rc; } dst = uri->data; src = uri->data; ngx_unescape_uri(&dst, &src, uri->len, NGX_UNESCAPE_URI); len = (uri->data + uri->len) - src; if (len) { dst = ngx_copy(dst, src, len); } uri->len = dst - uri->data; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ssi include: \"%V\"", uri); args.len = 0; args.data = NULL; flags = 0; if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) { return NGX_HTTP_SSI_ERROR; } psr = NULL; mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module); if (stub) { if (mctx->blocks) { bl = mctx->blocks->elts; for (i = 0; i < mctx->blocks->nelts; i++) { if (stub->len == bl[i].name.len && ngx_strncmp(stub->data, bl[i].name.data, stub->len) == 0) { goto found; } } } ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "\"stub\"=\"%V\" for \"include\" not found", stub); return NGX_HTTP_SSI_ERROR; found: psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t)); if (psr == NULL) { return NGX_ERROR; } psr->handler = ngx_http_ssi_stub_output; if (bl[i].count++) { out = NULL; ll = &out; for (tl = bl[i].bufs; tl; tl = tl->next) { if (ctx->free) { cl = ctx->free; ctx->free = ctx->free->next; b = cl->buf; } else { b = ngx_alloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; } ngx_memcpy(b, tl->buf, sizeof(ngx_buf_t)); b->pos = b->start; *ll = cl; cl->next = NULL; ll = &cl->next; } psr->data = out; } else { psr->data = bl[i].bufs; } } if (set) { key = 0; for (i = 0; i < set->len; i++) { set->data[i] = ngx_tolower(set->data[i]); key = ngx_hash(key, set->data[i]); } psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t)); if (psr == NULL) { return NGX_ERROR; } psr->handler = ngx_http_ssi_set_variable; psr->data = ngx_http_ssi_get_variable(r, set, key); if (psr->data == NULL) { if (mctx->variables == NULL) { mctx->variables = ngx_list_create(r->pool, 4, sizeof(ngx_http_ssi_var_t)); if (mctx->variables == NULL) { return NGX_ERROR; } } var = ngx_list_push(mctx->variables); if (var == NULL) { return NGX_ERROR; } var->name = *set; var->key = key; var->value = ngx_http_ssi_null_string; psr->data = &var->value; } flags |= NGX_HTTP_SUBREQUEST_IN_MEMORY; } rc = ngx_http_subrequest(r, uri, &args, &sr, psr, flags); if (rc == NGX_DONE) { return NGX_DONE; } if (rc == NGX_ERROR) { return NGX_HTTP_SSI_ERROR; } if (wait == NULL && set == NULL) { return NGX_OK; } if (rc == NGX_AGAIN) { if (ctx->wait == NULL) { ctx->wait = sr; } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "only one subrequest may be waited at the same time"); } } return rc;}static ngx_int_tngx_http_ssi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc){ ngx_chain_t *out; if (rc == NGX_ERROR || r->connection->error || r->request_output) { return rc; } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ssi stub output: \"%V?%V\"", &r->uri, &r->args); out = data; if (!r->header_sent) { if (ngx_http_set_content_type(r) == NGX_ERROR) { return NGX_ERROR; } if (ngx_http_send_header(r) == NGX_ERROR) { return NGX_ERROR; } } return ngx_http_output_filter(r, out);}static ngx_int_tngx_http_ssi_set_variable(ngx_http_request_t *r, void *data, ngx_int_t rc){ ngx_str_t *value = data; if (r->upstream) { value->len = r->upstream->buffer.last - r->upstream->buffer.pos; value->data = r->upstream->buffer.pos; } return rc;}static ngx_int_tngx_http_ssi_echo(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx, ngx_str_t **params){ u_char *p; uintptr_t len; ngx_int_t key; ngx_uint_t i; ngx_buf_t *b; ngx_str_t *var, *value, *enc, text; ngx_chain_t *cl; ngx_http_variable_value_t *vv; var = params[NGX_HTTP_SSI_ECHO_VAR]; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ssi echo \"%V\"", var); key = 0; for (i = 0; i < var->len; i++) { var->data[i] = ngx_tolower(var->data[i]); key = ngx_hash(key, var->data[i]); } value = ngx_http_ssi_get_variable(r, var, key); if (value == NULL) { vv = ngx_http_get_variable(r, var, key, 1); if (vv == NULL) { return NGX_HTTP_SSI_ERROR; } if (!vv->not_found) { text.data = vv->data; text.len = vv->len; value = &text; } } if (value == NULL) { value = params[NGX_HTTP_SSI_ECHO_DEFAULT]; if (value == NULL) { value = &ngx_http_ssi_none; } else if (value->len == 0) { return NGX_OK; } } else { if (value->len == 0) { return NGX_OK; } } enc = params[NGX_HTTP_SSI_ECHO_ENCODING]; if (enc) { if (enc->len == 4 && ngx_strncmp(enc->data, "none", 4) == 0) { ctx->encoding = NGX_HTTP_SSI_NO_ENCODING; } else if (enc->len == 3 && ngx_strncmp(enc->data, "url", 3) == 0) { ctx->encoding = NGX_HTTP_SSI_URL_ENCODING; } else if (enc->len == 6 && ngx_strncmp(enc->data, "entity", 6) == 0) { ctx->encoding = NGX_HTTP_SSI_ENTITY_ENCODING; } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "unknown encoding \"%V\" in the \"echo\" command", enc); } } switch (ctx->encoding) { case NGX_HTTP_SSI_NO_ENCODING: break; case NGX_HTTP_SSI_URL_ENCODING: len = 2 * ngx_escape_uri(NULL, value->data, value->len, NGX_ESCAPE_HTML); if (len) { p = ngx_palloc(r->pool, value->len + len); if (p == NULL) { return NGX_HTTP_SSI_ERROR; } (void) ngx_escape_uri(p, value->data, value->len, NGX_ESCAPE_HTML); value->len += len; value->data = p; } break; case NGX_HTTP_SSI_ENTITY_ENCODING: len = ngx_escape_html(NULL, value->data, value->len); if (len) { p = ngx_palloc(r->pool, value->len + len); if (p == NULL) { return NGX_HTTP_SSI_ERROR; } (void) ngx_escape_html(p, value->data, value->len); value->len += len; value->data = p; } break; } b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_HTTP_SSI_ERROR; } cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_HTTP_SSI_ERROR; } b->memory = 1; b->pos = value->data; b->last = value->data + value->len; cl->buf = b; cl->next = NULL; *ctx->last_out = cl; ctx->last_out = &cl->next; return NGX_OK;}static ngx_int_tngx_http_ssi_config(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx, ngx_str_t **params){ ngx_str_t *value; value = params[NGX_HTTP_SSI_CONFIG_TIMEFMT]; if (value) { ctx->timefmt.len = value->len; ctx->timefmt.data = ngx_palloc(r->pool, value->len + 1); if (ctx->timefmt.data == NULL) { return NGX_HTTP_SSI_ERROR; } ngx_cpystrn(ctx->timefmt.data, value->data, value->len + 1); } value = params[NGX_HTTP_SSI_CONFIG_ERRMSG]; if (value) { ctx->errmsg = *value; } return NGX_OK;}static ngx_int_tngx_http_ssi_set(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ctx, ngx_str_t **params){ ngx_int_t key, rc; ngx_uint_t i; ngx_str_t *name, *value, *vv; ngx_http_ssi_var_t *var; ngx_http_ssi_ctx_t *mctx; mctx = ngx_http_get_module_ctx(r->main, ngx_http_ssi_filter_module); if (mctx->variables == NULL) { mctx->variables = ngx_list_create(r->pool, 4, sizeof(ngx_http_ssi_var_t)); if (mctx->variables == NULL) { return NGX_ERROR; } } name = params[NGX_HTTP_SSI_SET_VAR]; value = params[NGX_HTTP_SSI_SET_VALUE]; ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ssi set \"%V\" \"%V\"", name, value); rc = ngx_http_ssi_evaluate_string(r, ctx, value, 0); if (rc != NGX_OK) { return rc; } key = 0; for (i = 0; i < name->len; i++) { name->data[i] = ngx_tolower(name->data[i]); key = ngx_hash(key, name->data[i]); } vv = ngx_http_ssi_get_variable(r, name, key
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -