⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_memcached_module.c

📁 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
        p += sizeof("VALUE ") - 1;        if (ngx_strncmp(p, ctx->key.data, ctx->key.len) != 0) {            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                          "memcached sent invalid key in response \"%V\" "                          "for key \"%V\"",                          &line, &ctx->key);            return NGX_HTTP_UPSTREAM_INVALID_HEADER;        }        p += ctx->key.len;        if (*p++ != ' ') {            goto no_valid;        }        /* skip flags */        while (*p) {            if (*p++ == ' ') {                goto length;            }        }        goto no_valid;    length:        len = p;        while (*p && *p++ != CR) { /* void */ }        r->headers_out.content_length_n = ngx_atoof(len, p - len - 1);        if (r->headers_out.content_length_n == -1) {            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                          "memcached sent invalid length in response \"%V\" "                          "for key \"%V\"",                          &line, &ctx->key);            return NGX_HTTP_UPSTREAM_INVALID_HEADER;        }        u->headers_in.status_n = 200;        u->state->status = 200;        u->buffer.pos = p + 1;        return NGX_OK;    }    if (ngx_strcmp(p, "END\x0d") == 0) {        ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,                      "key: \"%V\" was not found by memcached", &ctx->key);        u->headers_in.status_n = 404;        u->state->status = 404;        return NGX_OK;    }no_valid:    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                  "memcached sent invalid response: \"%V\"", &line);    return NGX_HTTP_UPSTREAM_INVALID_HEADER;}static ngx_int_tngx_http_memcached_filter_init(void *data){    ngx_http_memcached_ctx_t  *ctx = data;    ngx_http_upstream_t  *u;    u = ctx->request->upstream;    u->length += NGX_HTTP_MEMCACHED_END;    return NGX_OK;}static ngx_int_tngx_http_memcached_filter(void *data, ssize_t bytes){    ngx_http_memcached_ctx_t  *ctx = data;    u_char               *last;    ngx_buf_t            *b;    ngx_chain_t          *cl, **ll;    ngx_http_upstream_t  *u;    u = ctx->request->upstream;    b = &u->buffer;    if (u->length == ctx->rest) {        if (ngx_strncmp(b->last,                   ngx_http_memcached_end + NGX_HTTP_MEMCACHED_END - ctx->rest,                   ctx->rest)            != 0)        {            ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,                          "memcached sent invalid trailer");        }        u->length = 0;        ctx->rest = 0;        return NGX_OK;    }    for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {        ll = &cl->next;    }    cl = ngx_chain_get_free_buf(ctx->request->pool, &u->free_bufs);    if (cl == NULL) {        return NGX_ERROR;    }    cl->buf->flush = 1;    cl->buf->memory = 1;    *ll = cl;    last = b->last;    cl->buf->pos = last;    b->last += bytes;    cl->buf->last = b->last;    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,                   "memcached filter bytes:%z size:%z length:%z rest:%z",                   bytes, b->last - b->pos, u->length, ctx->rest);    if (bytes <= (ssize_t) (u->length - NGX_HTTP_MEMCACHED_END)) {        u->length -= bytes;        return NGX_OK;    }    last += u->length - NGX_HTTP_MEMCACHED_END;    if (ngx_strncmp(last, ngx_http_memcached_end, b->last - last) != 0) {        ngx_log_error(NGX_LOG_ERR, ctx->request->connection->log, 0,                      "memcached sent invalid trailer");    }    ctx->rest -= b->last - last;    b->last = last;    cl->buf->last = last;    u->length = ctx->rest;    return NGX_OK;}static voidngx_http_memcached_abort_request(ngx_http_request_t *r){    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "abort http memcached request");    return;}static voidngx_http_memcached_finalize_request(ngx_http_request_t *r, ngx_int_t rc){    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "finalize http memcached request");    return;}static void *ngx_http_memcached_create_loc_conf(ngx_conf_t *cf){    ngx_http_memcached_loc_conf_t  *conf;    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_memcached_loc_conf_t));    if (conf == NULL) {        return NGX_CONF_ERROR;    }    /*     * set by ngx_pcalloc():     *     *     conf->upstream.bufs.num = 0;     *     conf->upstream.next_upstream = 0;     *     conf->upstream.temp_path = NULL;     *     conf->upstream.schema = { 0, NULL };     *     conf->upstream.uri = { 0, NULL };     *     conf->upstream.location = NULL;     *     *     conf->index = 0;     */    conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;    conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;    conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;    conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;    /* the hardcoded values */    conf->upstream.cyclic_temp_file = 0;    conf->upstream.buffering = 0;    conf->upstream.ignore_client_abort = 0;    conf->upstream.send_lowat = 0;    conf->upstream.bufs.num = 0;    conf->upstream.busy_buffers_size = 0;    conf->upstream.max_temp_file_size = 0;    conf->upstream.temp_file_write_size = 0;    conf->upstream.intercept_errors = 1;    conf->upstream.intercept_404 = 1;    conf->upstream.pass_request_headers = 0;    conf->upstream.pass_request_body = 0;    return conf;}static char *ngx_http_memcached_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child){    ngx_http_memcached_loc_conf_t *prev = parent;    ngx_http_memcached_loc_conf_t *conf = child;    ngx_conf_merge_msec_value(conf->upstream.connect_timeout,                              prev->upstream.connect_timeout, 60000);    ngx_conf_merge_msec_value(conf->upstream.send_timeout,                              prev->upstream.send_timeout, 60000);    ngx_conf_merge_msec_value(conf->upstream.read_timeout,                              prev->upstream.read_timeout, 60000);    ngx_conf_merge_size_value(conf->upstream.buffer_size,                              prev->upstream.buffer_size,                              (size_t) ngx_pagesize);    ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,                              prev->upstream.next_upstream,                              (NGX_CONF_BITMASK_SET                               |NGX_HTTP_UPSTREAM_FT_ERROR                               |NGX_HTTP_UPSTREAM_FT_TIMEOUT));    if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {        conf->upstream.next_upstream = NGX_CONF_BITMASK_SET                                       |NGX_HTTP_UPSTREAM_FT_OFF;    }    return NGX_CONF_OK;}static char *ngx_http_memcached_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_memcached_loc_conf_t *lcf = conf;    ngx_str_t                 *value;    ngx_url_t                  u;    ngx_http_core_loc_conf_t  *clcf;    if (lcf->upstream.schema.len) {        return "is duplicate";    }    value = cf->args->elts;    ngx_memzero(&u, sizeof(ngx_url_t));    u.url = value[1];    u.no_resolve = 1;    lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);    if (lcf->upstream.upstream == NULL) {        return NGX_CONF_ERROR;    }    lcf->upstream.schema.len = sizeof("memcached://") - 1;    lcf->upstream.schema.data = (u_char *) "memcached://";    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);    clcf->handler = ngx_http_memcached_handler;    lcf->upstream.location = clcf->name;    if (clcf->name.data[clcf->name.len - 1] == '/') {        clcf->auto_redirect = 1;    }    lcf->index = ngx_http_get_variable_index(cf, &ngx_http_memcached_key);    if (lcf->index == NGX_ERROR) {        return NGX_CONF_ERROR;    }    return NGX_CONF_OK;}static char *ngx_http_memcached_upstream_max_fails_unsupported(ngx_conf_t *cf,    ngx_command_t *cmd, void *conf){    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,         "\"memcached_upstream_max_fails\" is not supported, "         "use the \"max_fails\" parameter of the \"server\" directive ",         "inside the \"upstream\" block");    return NGX_CONF_ERROR;}static char *ngx_http_memcached_upstream_fail_timeout_unsupported(ngx_conf_t *cf,    ngx_command_t *cmd, void *conf){    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,         "\"memcached_upstream_fail_timeout\" is not supported, "         "use the \"fail_timeout\" parameter of the \"server\" directive ",         "inside the \"upstream\" block");    return NGX_CONF_ERROR;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -