Permalink
Browse files

added errcode key and errmsg key

  • Loading branch information...
1 parent 3397cd4 commit c5215c7e54da1f2ca5c00c7eb51661a53b142e75 @liseen liseen committed Mar 29, 2012
Showing with 253 additions and 5 deletions.
  1. +133 −0 src/ngx_http_rds_json_filter_module.c
  2. +3 −0 src/ngx_http_rds_json_filter_module.h
  3. +14 −5 src/ngx_http_rds_json_handler.c
  4. +103 −0 t/ret.t
View
133 src/ngx_http_rds_json_filter_module.c
@@ -20,6 +20,9 @@
#define ngx_http_rds_json_content_type "application/json"
+#define ngx_http_rds_json_errcode_default_key "\"errcode\""
+#define ngx_http_rds_json_errstr_default_key "\"errstr\""
+
static unsigned ngx_http_rds_json_filter_used = 0;
@@ -47,6 +50,10 @@ static char * ngx_http_rds_json_success_property(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char * ngx_http_rds_json_user_property(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
+static char * ngx_http_rds_json_errcode_key(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
+static char * ngx_http_rds_json_errstr_key(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
static char * ngx_http_rds_json(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_rds_json_pre_config(ngx_conf_t *cf);
@@ -89,6 +96,24 @@ static ngx_command_t ngx_http_rds_json_commands[] = {
0,
NULL },
+ { ngx_string("rds_json_errcode_key"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF
+ |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
+ |NGX_CONF_TAKE1,
+ ngx_http_rds_json_errcode_key,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
+ { ngx_string("rds_json_errstr_key"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF
+ |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
+ |NGX_CONF_TAKE1,
+ ngx_http_rds_json_errstr_key,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
{ ngx_string("rds_json_format"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
@@ -416,6 +441,12 @@ ngx_http_rds_json_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_str_set(&conf->root, "\"data\"");
}
+ ngx_conf_merge_str_value(conf->errcode_key, prev->errcode_key,
+ "\"errcode\"");
+
+ ngx_conf_merge_str_value(conf->errstr_key, prev->errstr_key,
+ "\"errstr\"");
+
ngx_conf_merge_str_value(conf->content_type, prev->content_type,
ngx_http_rds_json_content_type);
@@ -676,6 +707,108 @@ ngx_http_rds_json_user_property(ngx_conf_t *cf, ngx_command_t *cmd,
static char *
+ngx_http_rds_json_errcode_key(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf)
+{
+ ngx_http_rds_json_conf_t *jlcf = conf;
+ ngx_str_t *value;
+ uintptr_t escape;
+ u_char *p;
+
+ value = cf->args->elts;
+
+ if (jlcf->root.len) {
+ return "is duplicate";
+ }
+
+ if (value[1].len == 0) {
+ return "takes an empty value";
+ }
+
+ escape = ngx_http_rds_json_escape_json_str(NULL, value[1].data,
+ value[1].len);
+
+ jlcf->errcode_key.len = value[1].len + escape + sizeof("\"\"") - 1;
+
+ p = ngx_palloc(cf->pool, jlcf->errcode_key.len);
+ if (p == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ jlcf->errcode_key.data = p;
+
+ *p++ = '"';
+
+ if (escape == 0) {
+ p = ngx_copy(p, value[1].data, value[1].len);
+
+ } else {
+ p = (u_char *) ngx_http_rds_json_escape_json_str(p, value[1].data,
+ value[1].len);
+ }
+
+ *p++ = '"';
+
+ if (p - jlcf->errcode_key.data != (ssize_t) jlcf->errcode_key.len) {
+ return "sees buffer error";
+ }
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_rds_json_errstr_key(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf)
+{
+ ngx_http_rds_json_conf_t *jlcf = conf;
+ ngx_str_t *value;
+ uintptr_t escape;
+ u_char *p;
+
+ value = cf->args->elts;
+
+ if (jlcf->errstr_key.len) {
+ return "is duplicate";
+ }
+
+ if (value[1].len == 0) {
+ return "takes an empty value";
+ }
+
+ escape = ngx_http_rds_json_escape_json_str(NULL, value[1].data,
+ value[1].len);
+
+ jlcf->errstr_key.len = value[1].len + escape + sizeof("\"\"") - 1;
+
+ p = ngx_palloc(cf->pool, jlcf->errstr_key.len);
+ if (p == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ jlcf->errstr_key.data = p;
+
+ *p++ = '"';
+
+ if (escape == 0) {
+ p = ngx_copy(p, value[1].data, value[1].len);
+
+ } else {
+ p = (u_char *) ngx_http_rds_json_escape_json_str(p, value[1].data,
+ value[1].len);
+ }
+
+ *p++ = '"';
+
+ if (p - jlcf->errstr_key.data != (ssize_t) jlcf->errstr_key.len) {
+ return "sees buffer error";
+ }
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
ngx_http_rds_json(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
dd("set filter used to 1");
View
3 src/ngx_http_rds_json_filter_module.h
@@ -48,6 +48,9 @@ typedef struct {
* key */
ngx_array_t *user_props; /* rds_json_user_property */
+ ngx_str_t errcode_key;
+ ngx_str_t errstr_key;
+
size_t buf_size;
/* for rds_json_ret */
View
19 src/ngx_http_rds_json_handler.c
@@ -35,7 +35,9 @@ ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
/* calculate the buffer size */
- len = sizeof("{\"errcode\":") - 1
+ len = sizeof("{") - 1
+ + conf->errcode_key.len
+ + sizeof(":") - 1
+ conf->errcode.len
+ sizeof("}") - 1
;
@@ -44,9 +46,11 @@ ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
escape = ngx_http_rds_json_escape_json_str(NULL,
errstr.data, errstr.len);
- len += sizeof("\"errstr\":\"") - 1
+ len += sizeof(",") - 1
+ + conf->errstr_key.len
+ + sizeof(":\"") - 1
+ errstr.len + escape
- + sizeof("\",") - 1
+ + sizeof("\"") - 1
;
}
@@ -112,11 +116,16 @@ ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
*b->last++ = '{';
- b->last = ngx_copy_literal(b->last, "\"errcode\":");
+ b->last = ngx_copy(b->last, conf->errcode_key.data, conf->errcode_key.len);
+ *b->last++ = ':';
b->last = ngx_copy(b->last, conf->errcode.data, conf->errcode.len);
if (errstr.len) {
- b->last = ngx_copy_literal(b->last, ",\"errstr\":\"");
+ *b->last++ = ',';
+ b->last = ngx_copy(b->last,
+ conf->errstr_key.data, conf->errstr_key.len);
+ *b->last++ = ':';
+ *b->last++ = '"';
if (escape == 0) {
b->last = ngx_copy(b->last, errstr.data, errstr.len);
View
103 t/ret.t
@@ -4,6 +4,7 @@ use lib 'lib';
use Test::Nginx::Socket;
repeat_each(3);
+#repeat_each(1);
plan tests => repeat_each() * 3 * blocks();
@@ -71,3 +72,105 @@ Content-Type: application/json
--- response_body chop
{"errcode":400,"errstr":"Invalid \"limit\" argument."}
+
+
+=== TEST 5: location internal rewrite
+--- config
+ location @err403 {
+ rds_json_success_property ret;
+ rds_json_ret 403 "Forbidden";
+ }
+
+ location /foo {
+ error_page 403 = @err403;
+ return 403;
+ }
+--- request
+ GET /foo
+--- response_headers
+Content-Type: application/json
+--- response_body chop
+{"errcode":403,"errstr":"Forbidden","ret":false}
+
+
+
+=== TEST 6: rds_json_errcode_key
+--- config
+ location /foo {
+ rds_json_errcode_key "ecode";
+ rds_json_success_property ret;
+ rds_json_ret 403 "Forbidden";
+ }
+--- request
+ GET /foo
+--- response_headers
+Content-Type: application/json
+--- response_body chop
+{"ecode":403,"errstr":"Forbidden","ret":false}
+
+
+=== TEST 7: rds_json_errcode_key
+--- config
+ rds_json_errcode_key "ecoderoot";
+
+ location /foo {
+ rds_json_success_property ret;
+ rds_json_ret 403 "Forbidden";
+ }
+--- request
+ GET /foo
+--- response_headers
+Content-Type: application/json
+--- response_body chop
+{"ecoderoot":403,"errstr":"Forbidden","ret":false}
+
+
+=== TEST 8: rds_json_errcode_key
+--- config
+ rds_json_errcode_key "ecoderoot";
+
+ location /foo {
+ rds_json_errcode_key "ecode";
+ rds_json_success_property ret;
+ rds_json_ret 403 "Forbidden";
+ }
+--- request
+ GET /foo
+--- response_headers
+Content-Type: application/json
+--- response_body chop
+{"ecode":403,"errstr":"Forbidden","ret":false}
+
+
+
+=== TEST 9: rds_json_errstr_key
+--- config
+ rds_json_errstr_key "msg";
+
+ location /foo {
+ rds_json_success_property ret;
+ rds_json_ret 403 "Forbidden";
+ }
+--- request
+ GET /foo
+--- response_headers
+Content-Type: application/json
+--- response_body chop
+{"errcode":403,"msg":"Forbidden","ret":false}
+
+
+=== TEST 10: rds_json_errstr_key
+--- config
+ rds_json_errstr_key "msg";
+
+ location /foo {
+ rds_json_errstr_key "msg2";
+ rds_json_success_property ret;
+ rds_json_ret 403 "Forbidden";
+ }
+--- request
+ GET /foo
+--- response_headers
+Content-Type: application/json
+--- response_body chop
+{"errcode":403,"msg2":"Forbidden","ret":false}

0 comments on commit c5215c7

Please sign in to comment.