Permalink
Browse files

moved ngx_http_rds_json_ret_handler into a separate compilation unit,…

… ngx_http_rds_json_handler.c.
  • Loading branch information...
1 parent a5440a9 commit a5a765fb66c9e49a7269faba017d5e17ccd5cba9 @agentzh agentzh committed Jan 24, 2010
Showing with 127 additions and 120 deletions.
  1. +2 −0 .gitignore
  2. +2 −2 config
  3. +2 −102 src/ngx_http_rds_json_filter_module.c
  4. +107 −0 src/ngx_http_rds_json_handler.c
  5. +9 −0 src/ngx_http_rds_json_handler.h
  6. +5 −16 test/t/openresty.t
View
@@ -58,4 +58,6 @@ src/processor.h
src/processor.c
src/rds.h
src/utils.h
+src/handler.c
+src/handler.h
util/bench
View
4 config
@@ -1,5 +1,5 @@
ngx_addon_name=ngx_http_rds_json_filter_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_rds_json_filter_module"
-NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_rds_json_filter_module.c $ngx_addon_dir/src/ngx_http_rds_json_processor.c $ngx_addon_dir/src/ngx_http_rds_json_util.c $ngx_addon_dir/src/ngx_http_rds_json_output.c"
-NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/resty_dbd_stream.h $ngx_addon_dir/src/ngx_http_rds_json_filter_module.h $ngx_addon_dir/src/ngx_http_rds_json_processor.h $ngx_addon_dir/src/ngx_http_rds_json_util.h $ngx_addon_dir/src/ngx_http_rds.h $ngx_addon_dir/src/resty_dbd_stream.h $ngx_addon_dir/src/ngx_http_rds_json_output.h $ngx_addon_dir/src/ngx_http_rds_utils.h"
+NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_rds_json_filter_module.c $ngx_addon_dir/src/ngx_http_rds_json_processor.c $ngx_addon_dir/src/ngx_http_rds_json_util.c $ngx_addon_dir/src/ngx_http_rds_json_output.c $ngx_addon_dir/src/ngx_http_rds_json_handler.c"
+NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/resty_dbd_stream.h $ngx_addon_dir/src/ngx_http_rds_json_filter_module.h $ngx_addon_dir/src/ngx_http_rds_json_processor.h $ngx_addon_dir/src/ngx_http_rds_json_util.h $ngx_addon_dir/src/ngx_http_rds.h $ngx_addon_dir/src/resty_dbd_stream.h $ngx_addon_dir/src/ngx_http_rds_json_output.h $ngx_addon_dir/src/ngx_http_rds_utils.h $ngx_addon_dir/src/ngx_http_rds_json_handler.h"
@@ -4,12 +4,13 @@
*/
-#define DDEBUG 1
+#define DDEBUG 0
#include "ddebug.h"
#include "ngx_http_rds_json_filter_module.h"
#include "ngx_http_rds_json_util.h"
#include "ngx_http_rds_json_processor.h"
+#include "ngx_http_rds_json_handler.h"
#include <ngx_config.h>
@@ -27,8 +28,6 @@ ngx_http_output_header_filter_pt ngx_http_rds_json_next_header_filter;
ngx_http_output_body_filter_pt ngx_http_rds_json_next_body_filter;
-static ngx_int_t ngx_http_rds_json_ret_handler(ngx_http_request_t *r);
-
static char *ngx_http_rds_json_ret(ngx_conf_t *cf, ngx_command_t *cmd,
void* conf);
@@ -342,102 +341,3 @@ ngx_http_rds_json_ret(ngx_conf_t *cf, ngx_command_t *cmd,
return NGX_CONF_OK;
}
-
-static ngx_int_t
-ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
-{
- ngx_chain_t *cl;
- ngx_buf_t *b;
- size_t len;
- ngx_http_rds_json_conf_t *conf;
- ngx_str_t errstr;
- ngx_int_t rc;
- uintptr_t escape = 0;
-
- dd("entered ret handler");
-
- conf = ngx_http_get_module_loc_conf(r,
- ngx_http_rds_json_filter_module);
-
- /* evaluate the final value of conf->errstr */
-
- if (ngx_http_complex_value(r, conf->errstr, &errstr) != NGX_OK) {
- return NGX_ERROR;
- }
-
- /* calculate the buffer size */
-
- len = sizeof("{\"errcode\":") - 1
- + conf->errcode.len
- + sizeof("}") - 1
- ;
-
- if (errstr.len) {
- escape = ngx_http_rds_json_escape_json_str(NULL,
- errstr.data, errstr.len);
-
- len += sizeof(",\"errstr\":\"") - 1
- + errstr.len + escape
- + sizeof("\"") - 1
- ;
- }
-
- /* create the buffer */
-
- cl = ngx_alloc_chain_link(r->pool);
- if (cl == NULL) {
- return NGX_ERROR;
- }
-
- b = ngx_create_temp_buf(r->pool, len);
- if (b == NULL) {
- return NGX_ERROR;
- }
-
- b->last_buf = 1;
- cl->buf = b;
-
- /* copy data over to the buffer */
-
- b->last = ngx_copy_const_str(b->last, "{\"errcode\":");
-
- b->last = ngx_copy(b->last, conf->errcode.data, conf->errcode.len);
-
- if (errstr.len) {
- b->last = ngx_copy_const_str(b->last, ",\"errstr\":\"");
-
- if (escape == 0) {
- b->last = ngx_copy(b->last, errstr.data, errstr.len);
- } else {
- b->last = (u_char *) ngx_http_rds_json_escape_json_str(b->last,
- errstr.data, errstr.len);
- }
-
- *b->last++ = '"';
- }
-
- *b->last++ = '}';
-
- if (b->last != b->end) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "rds_json: rds_json_ret: buffer error");
-
- return NGX_ERROR;
- }
-
- /* send headers */
-
- r->headers_out.status = NGX_HTTP_OK;
- r->headers_out.content_type = conf->content_type;
- r->headers_out.content_type_len = conf->content_type.len;
-
- rc = ngx_http_send_header(r);
- if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
- return rc;
- }
-
- dd("output filter...");
-
- return ngx_http_output_filter(r, cl);
-}
-
@@ -0,0 +1,107 @@
+#define DDEBUG 0
+#include "ddebug.h"
+
+#include "ngx_http_rds_json_handler.h"
+#include "ngx_http_rds_json_util.h"
+
+
+ngx_int_t
+ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
+{
+ ngx_chain_t *cl;
+ ngx_buf_t *b;
+ size_t len;
+ ngx_http_rds_json_conf_t *conf;
+ ngx_str_t errstr;
+ ngx_int_t rc;
+ uintptr_t escape = 0;
+
+ dd("entered ret handler");
+
+ conf = ngx_http_get_module_loc_conf(r,
+ ngx_http_rds_json_filter_module);
+
+ /* evaluate the final value of conf->errstr */
+
+ if (ngx_http_complex_value(r, conf->errstr, &errstr) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ /* calculate the buffer size */
+
+ len = sizeof("{\"errcode\":") - 1
+ + conf->errcode.len
+ + sizeof("}") - 1
+ ;
+
+ if (errstr.len) {
+ escape = ngx_http_rds_json_escape_json_str(NULL,
+ errstr.data, errstr.len);
+
+ len += sizeof(",\"errstr\":\"") - 1
+ + errstr.len + escape
+ + sizeof("\"") - 1
+ ;
+ }
+
+ /* create the buffer */
+
+ cl = ngx_alloc_chain_link(r->pool);
+ if (cl == NULL) {
+ return NGX_ERROR;
+ }
+
+ b = ngx_create_temp_buf(r->pool, len);
+ if (b == NULL) {
+ return NGX_ERROR;
+ }
+
+ b->last_buf = 1;
+
+ cl->buf = b;
+ cl->next = NULL;
+
+ /* copy data over to the buffer */
+
+ b->last = ngx_copy_const_str(b->last, "{\"errcode\":");
+
+ b->last = ngx_copy(b->last, conf->errcode.data, conf->errcode.len);
+
+ if (errstr.len) {
+ b->last = ngx_copy_const_str(b->last, ",\"errstr\":\"");
+
+ if (escape == 0) {
+ b->last = ngx_copy(b->last, errstr.data, errstr.len);
+ } else {
+ b->last = (u_char *) ngx_http_rds_json_escape_json_str(b->last,
+ errstr.data, errstr.len);
+ }
+
+ *b->last++ = '"';
+ }
+
+ *b->last++ = '}';
+
+ if (b->last != b->end) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds_json: rds_json_ret: buffer error");
+
+ return NGX_ERROR;
+ }
+
+ /* send headers */
+
+ r->headers_out.status = NGX_HTTP_OK;
+ r->headers_out.content_type = conf->content_type;
+ r->headers_out.content_type_len = conf->content_type.len;
+
+ rc = ngx_http_send_header(r);
+ if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
+ return rc;
+ }
+
+ dd("output filter...");
+
+ return ngx_http_output_filter(r, cl);
+}
+
@@ -0,0 +1,9 @@
+#ifndef NGX_HTTP_RDS_JSON_HANDLER_H
+#define NGX_HTTP_RDS_JSON_HANDLER_H
+
+#include "ngx_http_rds_json_filter_module.h"
+
+ngx_int_t ngx_http_rds_json_ret_handler(ngx_http_request_t *r);
+
+#endif /* NGX_HTTP_RDS_JSON_HANDLER_H */
+
View
@@ -21,8 +21,6 @@ our $http_config = <<'_EOC_';
_EOC_
our $config = <<'_EOC_';
- default_type 'application/json';
-
xss_get on;
xss_callback_arg _callback;
@@ -53,18 +51,9 @@ order by created asc";
location @err500 { rds_json_ret 500 "Internal Server Error"; }
location @err502 { rds_json_ret 502 "Bad Gateway"; }
-
- location @err503 {
- echo_duplicate 1 '{"errcode":503,"errstr":"Service Unavailable"}';
- }
-
- location @err404 {
- echo_duplicate 1 '{"errcode":404,"errstr":"Not Found"}';
- }
-
- location @err400 {
- echo_duplicate 1 '{"errcode":404,"errstr":"Bad Request"}';
- }
+ location @err503 { rds_json_ret 503 "Service Unavailable"; }
+ location @err404 { rds_json_ret 404 "Not Found"; }
+ location @err400 { rds_json_ret 400 "Bad Request"; }
_EOC_
@@ -84,7 +73,7 @@ GET /=/view/PostsByMonth/~/~?_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
-foo({"errcode":400,"errstr":"Bad month argument"});
+foo({"errcode":400,"errstr":"Bad \"month\" argument"});
@@ -96,7 +85,7 @@ GET /=/view/PostsByMonth/~/~?month=1234&_callback=foo
--- response_headers
Content-Type: application/x-javascript
--- response_body chop
-foo({"errcode":400,"errstr":"Bad month argument"});
+foo({"errcode":400,"errstr":"Bad \"month\" argument"});

0 comments on commit a5a765f

Please sign in to comment.