Nginx module of URL authorization
2440 단어 nginx memcached
config 에 추가:
CORE_LIBS="$CORE_LIBS -L/byread/bin/libmemcached/lib -lmemcached"
구체 적 인 실현:
#include <libmemcached/memcached.h>
static ngx_int_t
ngx_http_sukai_authorize_handler(ngx_http_request_t *r)
{
ngx_http_sukai_authorize_loc_conf_t *conf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_sukai_authorize_module);
if(conf->enable) {
memcached_st *mcd;
memcached_return_t rc;
ngx_uint_t vlen;
uint32_t flag;
char *val = NULL;
memcached_server_st *servers;
mcd = memcached_create(NULL);
servers = memcached_server_list_append(NULL,"127.0.0.1",11211,&rc);
rc = memcached_server_push(mcd,servers);
char byid[18];
char authkey[128];
char realuri[128];
char prekey[] = "book_auth_";
sscanf((char *)r->uri.data,"/%[0-9]/%[0-9a-z]%s",byid,authkey,realuri);
val = memcached_get(mcd,strcat(prekey,byid),strlen(byid) + strlen(prekey),&vlen,&flag,&rc);
if (val == NULL || ngx_strcmp(authkey, val) != 0) {
return NGX_HTTP_NOT_ALLOWED;
}
r->uri.len = strlen(realuri);
char* pRealUrl = (char*)malloc(strlen(realuri) + 1);
strcpy(pRealUrl ,realuri);
r->uri.data = (u_char *)pRealUrl;
r->valid_unparsed_uri = 0;
//rc = memcached_set(mcd,"realuri2",8,(char *)r->uri.data,r->uri.len,(time_t)180,(uint32_t)0);
//rc = memcached_delete(mcd,prekey,strlen(prekey),(time_t)0);
memcached_server_free(servers);
memcached_free(mcd);
if (NULL != val) free(val);
} else {
char byid[18];
char authkey[128];
char realuri[128];
sscanf((char *)r->uri.data,"/%[0-9]/%[0-9a-z]%s",byid,authkey,realuri);
r->uri.len = strlen(realuri);
char* pRealUrl = (char*)malloc(strlen(realuri) + 1);
strcpy(pRealUrl ,realuri);
r->uri.data = (u_char *)pRealUrl;
r->valid_unparsed_uri = 0;
}
return NGX_OK;
}