概述
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Sergey A. Osokin
*/
#define NGX_ESCAPE_REDIS 4
#define REDIS_AUTH_CMD "*2rn$4rnauthrn"
#define REDIS_GET_CMD "*2rn$3rngetrn"
#define REDIS_SELECT_CMD "*2rn$6rnselectrn"
#define REDIS_PLUSOKCRLF "+OKrn"
#define REDIS_ERR "$-1"
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
typedef struct {
ngx_http_upstream_conf_t upstream;
ngx_int_t index;
ngx_int_t db;
ngx_int_t auth;
ngx_uint_t gzip_flag;
} ngx_http_redis_loc_conf_t;
typedef struct {
size_t rest;
ngx_http_request_t *request;
ngx_str_t key;
} ngx_http_redis_ctx_t;
static ngx_int_t ngx_http_redis_create_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_redis_reinit_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_redis_process_header(ngx_http_request_t *r);
static ngx_int_t ngx_http_redis_filter_init(void *data);
static ngx_int_t ngx_http_redis_filter(void *data, ssize_t bytes);
static void ngx_http_redis_abort_request(ngx_http_request_t *r);
static void ngx_http_redis_finalize_request(ngx_http_request_t *r,
ngx_int_t rc);
static ngx_int_t ngx_http_redis_add_variables(ngx_conf_t *cf);
static void *ngx_http_redis_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_redis_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static char *ngx_http_redis_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_conf_bitmask_t ngx_http_redis_next_upstream_masks[] = {
{ ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
{ ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
{ ngx_string("invalid_response"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
{ ngx_string("not_found"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
{ ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_http_redis_commands[] = {
{ ngx_string("redis_pass"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_http_redis_pass,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
#if defined nginx_version && nginx_version >= 8022
{ ngx_string("redis_bind"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_upstream_bind_set_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.local),
NULL },
#endif
{ ngx_string("redis_connect_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.connect_timeout),
NULL },
{ ngx_string("redis_send_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.send_timeout),
NULL },
{ ngx_string("redis_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.buffer_size),
NULL },
{ ngx_string("redis_read_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.read_timeout),
NULL },
{ ngx_string("redis_next_upstream"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, upstream.next_upstream),
&ngx_http_redis_next_upstream_masks },
{ ngx_string("redis_gzip_flag"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_redis_loc_conf_t, gzip_flag),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_redis_module_ctx = {
ngx_http_redis_add_variables, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_redis_create_loc_conf, /* create location configration */
ngx_http_redis_merge_loc_conf /* merge location configration */
};
ngx_module_t ngx_http_redis_module = {
NGX_MODULE_V1,
&ngx_http_redis_module_ctx, /* module context */
ngx_http_redis_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/* Initialize additional var for hide "Content-Enconding: gzip" header */
static ngx_str_t ngx_http_redis_hide_headers[] = {
ngx_null_string
};
static ngx_str_t ngx_http_redis_key = ngx_string("redis_key");
static ngx_str_t ngx_http_redis_db = ngx_string("redis_db");
static ngx_str_t ngx_http_redis_auth = ngx_string("redis_auth");
static ngx_uint_t ngx_http_redis_db_index;
static ngx_uint_t ngx_http_redis_auth_index;
#define NGX_HTTP_REDIS_END (sizeof(ngx_http_redis_end) - 1)
static u_char ngx_http_redis_end[] = CRLF;
static ngx_int_t
ngx_http_redis_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_upstream_t *u;
ngx_http_redis_ctx_t *ctx;
ngx_http_redis_loc_conf_t *rlcf;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);//丢弃请求体
if (rc != NGX_OK) {
return rc;
}
if (ngx_http_set_content_type(r) != NGX_OK) {//设置Content-Type头部
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
#if defined nginx_version && nginx_version >= 8011
if (ngx_http_upstream_create(r) != NGX_OK) {//创建upstream
#else
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
if (u == NULL) {
#endif
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
#if defined nginx_version && nginx_version >= 8011
u = r->upstream;
#endif
#if defined nginx_version && nginx_version >= 8037
ngx_str_set(&u->schema, "redis://");
#else
u->schema.len = sizeof("redis://") - 1;
u->schema.data = (u_char *) "redis://";
#endif
#if defined nginx_version && nginx_version >= 8011
u->output.tag = (ngx_buf_tag_t) &ngx_http_redis_module;
#else
u->peer.log = r->connection->log;
u->peer.log_error = NGX_ERROR_ERR;
#endif
#if defined nginx_version && nginx_version >= 8011
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
#else
u->output.tag = (ngx_buf_tag_t) &ngx_http_redis_module;
#endif
u->conf = &rlcf->upstream;
u->create_request = ngx_http_redis_create_request;
u->reinit_request = ngx_http_redis_reinit_request;
u->process_header = ngx_http_redis_process_header;//收到上游服务器的相应后就会回调process_header方法
u->abort_request = ngx_http_redis_abort_request;
u->finalize_request = ngx_http_redis_finalize_request;
#if defined nginx_version && nginx_version < 8011
r->upstream = u;
#endif
ctx = ngx_palloc(r->pool, sizeof(ngx_http_redis_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->rest = NGX_HTTP_REDIS_END;
ctx->request = r;
ngx_http_set_ctx(r, ctx, ngx_http_redis_module);
//处理上游服务器响应数据的回调函数
u->input_filter_init = ngx_http_redis_filter_init;
u->input_filter = ngx_http_redis_filter;
u->input_filter_ctx = ctx;
#if defined nginx_version && nginx_version >= 8011
r->main->count++;
#endif
ngx_http_upstream_init(r);//初始化
return NGX_DONE;
}
static ngx_int_t
ngx_http_redis_create_request(ngx_http_request_t *r)
{
size_t len = 0;
uintptr_t escape;
ngx_buf_t *b;
ngx_chain_t *cl;
ngx_http_redis_ctx_t *ctx;
ngx_http_variable_value_t *vv[3];
ngx_http_redis_loc_conf_t *rlcf;
u_char lenbuf[NGX_INT_T_LEN];
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_redis_module);
//redis权限auth
vv[0] = ngx_http_get_indexed_variable(r, ngx_http_redis_auth_index);
if (vv[0] == NULL || vv[0]->not_found || vv[0]->len == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"no auth command provided" );
} else {
len += sizeof(REDIS_AUTH_CMD) + sizeof("$") - 1;
len += ngx_sprintf(lenbuf, "%d", vv[0]->len) - lenbuf;
len += sizeof(CRLF) - 1 + vv[0]->len;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"auth info: %s", vv[0]->data);
}
len += sizeof(CRLF) - 1;
//redis数据库
vv[1] = ngx_http_get_indexed_variable(r, ngx_http_redis_db_index);
/*
* If user do not select redis database in nginx.conf by redis_db
* variable, just add size of "select 0" to request. This is add
* some overhead in talk with redis, but this way simplify parsing
* the redis answer in ngx_http_redis_process_header().
*/
if (vv[1] == NULL || vv[1]->not_found || vv[1]->len == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select 0 redis database" );
len += sizeof(REDIS_SELECT_CMD) + sizeof("$1") + sizeof(CRLF) + sizeof("0") - 1;
} else {
len += sizeof(REDIS_SELECT_CMD) + sizeof("$") - 1;
len += ngx_sprintf(lenbuf, "%d", vv[1]->len) - lenbuf;
len += sizeof(CRLF) - 1 + vv[1]->len;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select %s redis database", vv[1]->data);
}
len += sizeof(CRLF) - 1;
//redis_key 解析
vv[2] = ngx_http_get_indexed_variable(r, rlcf->index);
/* If nginx.conf have no redis_key return error. */
if (vv[2] == NULL || vv[2]->not_found || vv[2]->len == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"the "$redis_key" variable is not set");
return NGX_ERROR;
}
/* Count have space required escape symbols. */
escape = 2 * ngx_escape_uri(NULL, vv[2]->data, vv[2]->len, NGX_ESCAPE_REDIS);//对非普通字符进行编码转换
len += sizeof(REDIS_GET_CMD) + sizeof("$") - 1;
len += ngx_sprintf(lenbuf, "%d", vv[2]->len) - lenbuf;
len += sizeof(CRLF) - 1 + vv[2]->len + escape + sizeof(CRLF) - 1;
/* Create temporary buffer for request with size len. */
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = b;
cl->next = NULL;
r->upstream->request_bufs = cl;
/* add "auth " for request */
if (vv[0] != NULL && !(vv[0]->not_found) && vv[0]->len != 0) {
/* Add "auth " for request. */
b->last = ngx_sprintf(b->last, "%s$%d%s", REDIS_AUTH_CMD, vv[0]->len, CRLF);
b->last = ngx_copy(b->last, vv[0]->data, vv[0]->len);
*b->last++ = CR; *b->last++ = LF;
}
/* Get context redis_db from configuration file. */
ctx = ngx_http_get_module_ctx(r, ngx_http_redis_module);
ctx->key.data = b->last;
/*
* Add "0" as redis number db to request if redis_db undefined,
* othervise add real number from context.
*/
if (vv[1] == NULL || vv[1]->not_found || vv[1]->len == 0) {
b->last = ngx_sprintf(b->last, "%s$1%s", REDIS_SELECT_CMD, CRLF);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select 0 redis database" );
*b->last++ = '0';
} else {
b->last = ngx_sprintf(b->last, "%s$%d%s", REDIS_SELECT_CMD, vv[1]->len, CRLF);
b->last = ngx_copy(b->last, vv[1]->data, vv[1]->len);
ctx->key.len = b->last - ctx->key.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"select %V redis database", &ctx->key);
}
/* Add "rn". */
*b->last++ = CR; *b->last++ = LF;
b->last = ngx_sprintf(b->last, "%s$%d%s", REDIS_GET_CMD, vv[2]->len, CRLF);
/* Get context redis_key from nginx.conf. */
ctx = ngx_http_get_module_ctx(r, ngx_http_redis_module);
ctx->key.data = b->last;
/*
* If no escape symbols then copy data as is, othervise use
* escape-copy function.
*/
if (escape == 0) {
b->last = ngx_copy(b->last, vv[2]->data, vv[2]->len);
} else {
b->last = (u_char *) ngx_escape_uri(b->last, vv[2]->data, vv[2]->len,
NGX_ESCAPE_REDIS);
}
ctx->key.len = b->last - ctx->key.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http redis request: "%V"", &ctx->key);
/* Add one more "rn". */
*b->last++ = CR; *b->last++ = LF;
/*
* Summary, the request looks like this:
* "auth $redis_authrnselect $redis_dbrnget $redis_keyrn", where
* $redis_auth, $redis_db and $redis_key are variable's values.
*/
return NGX_OK;
}
static ngx_int_t
ngx_http_redis_reinit_request(ngx_http_request_t *r)
{
return NGX_OK;
}
static ngx_int_t
ngx_http_redis_process_header(ngx_http_request_t *r)
{
u_char *p, *len;
u_int c, try;
ngx_str_t line;
ngx_table_elt_t *h;
ngx_http_upstream_t *u;
ngx_http_redis_ctx_t *ctx;
ngx_http_redis_loc_conf_t *rlcf;
ngx_http_variable_value_t *vv;
ngx_int_t no_auth_cmd;
vv = ngx_http_get_indexed_variable(r, ngx_http_redis_auth_index);
no_auth_cmd = (vv == NULL || vv->not_found || vv->len == 0);
c = try = 0;
u = r->upstream;
p = u->buffer.pos;
/*
* Good answer from redis should looks like this:
* "+OKrn+OKrn$8rn12345678rn"
*
* Here is:
* "+OKrn+OKrn" is answer for first two commands
* "auth password" and "select 0"
* Next two strings are answer for command "get $redis_key", where
*
* "$8" is length of following next string and
* "12345678" is value of $redis_key, the string.
*
* So, if the first symbol is:
* "+" (good answer) - try to find 2 or 3 strings;
* "-" (bad answer) - try to find 1 string;
* othervise answer is invalid.
*/
if (*p == '+') {
if (no_auth_cmd) {
try = 2;
} else {
try = 3;
}
} else if (*p == '-') {
try = 1;
} else {
goto no_valid;
}
for (p = u->buffer.pos; p < u->buffer.last; p++) {
if (*p == LF) {
c++;
if (c == try) {
goto found;
}
}
}
return NGX_AGAIN;
found:
*p = '