Permalink
Browse files

now the module compiles.

  • Loading branch information...
agentzh committed Jan 13, 2010
1 parent 29740b3 commit 079825dbf831afdb9322b584456d72cb142b4039
View
@@ -56,3 +56,4 @@ src/util.h
src/util.c
src/processor.h
src/processor.c
+src/rds.h
View
@@ -0,0 +1,147 @@
+
+/*
+ * Copyright (C) agentzh
+ */
+
+#ifndef NGX_HTTP_RDS_H
+#define NGX_HTTP_RDS_H
+
+
+#include <nginx.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+typedef struct {
+ uint16_t std_errcode;
+ uint16_t drv_errcode;
+ ngx_str_t errstr;
+
+ uint64_t affected_rows;
+ uint64_t insert_id;
+ uint16_t column_count;
+
+} ngx_http_rds_header_t;
+
+
+static ngx_int_t
+ngx_http_rds_parse_header(ngx_http_request_t *r, ngx_buf_t *b,
+ ngx_http_rds_header_t *header)
+{
+ size_t rest;
+
+ rest = sizeof(uint8_t) /* endian type */
+ + sizeof(uint32_t) /* format version */
+ + sizeof(uint8_t) /* result type */
+
+ + sizeof(uint16_t) /* standard error code */
+ + sizeof(uint16_t) /* driver-specific error code */
+
+ + sizeof(uint16_t) /* driver-specific errstr len */
+ + 0 /* driver-specific errstr data */
+ + sizeof(uint64_t) /* affected rows */
+ + sizeof(uint64_t) /* insert id */
+ + sizeof(uint16_t) /* column count */
+ ;
+
+ if (b->last - b->pos < rest) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds: header is incomplete in the buf");
+ return NGX_ERROR;
+ }
+
+ /* check endian type */
+
+ if ( *(uint8_t *) b->pos !=
+#if (NGX_HAVE_LITTLE_ENDIAN)
+ 0
+#else /* big endian */
+ 1
+#endif
+ )
+ {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds: endian type in the header differ");
+ return NGX_ERROR;
+ }
+
+ b->pos += sizeof(uint8_t);
+
+ /* check RDS format version number */
+
+ if ( *(uint32_t *) b->pos != (uint32_t) resty_dbd_stream_version) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds: RDS format version differ");
+ return NGX_ERROR;
+ }
+
+ b->pos += sizeof(uint32_t);
+
+ /* check RDS format type */
+
+ if (*b->pos != 0) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds: RDS format type must be 0 for now");
+ return NGX_ERROR;
+ }
+
+ b->pos++;
+
+ /* save the standard error code */
+
+ header->std_errcode = *(uint16_t) b->pos;
+
+ b->pos += sizeof(uint16_t);
+
+ /* save the driver-specific error code */
+
+ header->drv_errcode = *(uint16_t) b->pos;
+
+ b->pos += sizeof(uint16_t);
+
+ /* save the error string length */
+
+ header->errstr.len = *(uint16_t) b->pos;
+
+ b->pos += sizeof(uint16_t);
+
+ /* check the rest data's size */
+
+ rest = header->errstr.len
+ + sizeof(uint64_t) /* affected rows */
+ + sizeof(uint64_t) /* insert id */
+ + sizeof(uint16_t) /* column count */
+ ;
+
+ if (b->last - b->pos < rest) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds: header is incomplete in the buf");
+ return NGX_ERROR;
+ }
+
+ /* save the error string data */
+
+ header->errstr.data = b->pos;
+
+ b->pos += header->errstr.len;
+
+ /* save affected rows */
+
+ header->affected_rows = *(uint64_t *) b->pos;
+
+ b->pos += sizeof(uint64_t);
+
+ header->insert_id = *(uint64_t *)b->pos;
+
+ b->pos += sizeof(uint64_t);
+
+ header->column_count = *(uint16_t *) b->pos;
+
+ b->pos += sizeof(uint16_t);
+
+ return NGX_OK;
+}
+
+
+#endif /* NGX_HTTP_RDS_H */
+
@@ -4,15 +4,21 @@
*/
+#define DDEBUG 1
+#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_config.h>
+#define ngx_http_rds_json_content_type "application/json"
+
static ngx_conf_enum_t ngx_http_rds_json_formats[] = {
{ ngx_string("none"), json_format_none },
- { ngx_string("compact"), json_format_compat },
+ { ngx_string("compact"), json_format_compact },
{ ngx_string("pretty"), json_format_pretty },
{ ngx_null_string, 0 }
};
@@ -99,7 +105,7 @@ ngx_http_rds_json_header_filter(ngx_http_request_t *r)
return ngx_http_rds_json_next_header_filter(r);
}
- if (ngx_http_rds_json_test_content_type(r, &conf->types) != NGX_OK) {
+ if (ngx_http_rds_json_test_content_type(r) != NGX_OK) {
return ngx_http_rds_json_next_header_filter(r);
}
@@ -136,7 +142,6 @@ ngx_http_rds_json_header_filter(ngx_http_request_t *r)
static ngx_int_t
ngx_http_rds_json_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
- ngx_int_t rc;
ngx_chain_t *cl;
ngx_http_rds_json_ctx_t *ctx;
@@ -153,7 +158,7 @@ ngx_http_rds_json_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
switch (ctx->state) {
case state_expect_header:
return ngx_http_rds_json_process_header(r, in, ctx);
- case state_expect_col;
+ case state_expect_col:
return ngx_http_rds_json_process_col(r, in, ctx);
case state_expect_row:
return ngx_http_rds_json_process_row(r, in, ctx);
@@ -162,17 +167,23 @@ ngx_http_rds_json_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
case state_expect_more_field_data:
return ngx_http_rds_json_process_more_field_data(r, in, ctx);
case state_done:
+
/* mark the remaining bufs as consumed */
+
for (cl = in; cl; cl = cl->next) {
- if (cl->buf->tempory && cl->buf->memory) {
- ngx_pfree(cl->buf->start);
+
+ if (cl->buf->temporary && cl->buf->memory) {
+ ngx_pfree(r->pool, cl->buf->start);
}
+
cl->buf->pos = cl->buf->last;
}
+
return NGX_DONE;
+
default:
- ngx_log_error(NGX_LOG_ERR, c->log, 0,
- "rds_json: invalid internal state: %d"
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "rds_json: invalid internal state: %d",
ctx->state);
return NGX_ERROR;
@@ -212,7 +223,7 @@ ngx_http_rds_json_create_conf(ngx_conf_t *cf)
* conf->content_type = { 0, NULL };
*/
- conf
+ conf->format = NGX_CONF_UNSET_UINT;
return conf;
}
@@ -224,9 +235,10 @@ ngx_http_rds_json_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_http_rds_json_conf_t *prev = parent;
ngx_http_rds_json_conf_t *conf = child;
- ngx_conf_merge_value(conf->format, prev->format, json_format_none);
+ ngx_conf_merge_uint_value(conf->format, prev->format, json_format_none);
- ngx_conf_merge_str_value(conf->content_type, prev->content_type, ngx_string("application/json"));
+ ngx_conf_merge_str_value(conf->content_type, prev->content_type,
+ ngx_http_rds_json_content_type);
return NGX_CONF_OK;
}
@@ -8,6 +8,7 @@
#include <ngx_core.h>
#include <ngx_http.h>
+#include <nginx.h>
extern ngx_module_t ngx_http_rds_json_filter_module;
@@ -37,7 +38,8 @@ typedef enum {
state_expect_col,
state_expect_row,
state_expect_field,
- state_expect_more_field_data
+ state_expect_more_field_data,
+ state_done
} ngx_http_rds_json_state_t;
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) agentzh
+ */
+
+
+#define DDEBUG 1
+#include "ddebug.h"
+
+#include "ngx_http_rds_json_processor.h"
+
+
+ngx_int_t
+ngx_http_rds_json_process_header(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx){
+ /* TODO */
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_http_rds_json_process_col(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx){
+ /* TODO */
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_http_rds_json_process_row(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx){
+ /* TODO */
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_http_rds_json_process_field(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx){
+ /* TODO */
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_http_rds_json_process_more_field_data(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx){
+ /* TODO */
+ return NGX_OK;
+}
+
@@ -0,0 +1,34 @@
+
+/*
+ * Copyright (C) agentzh
+ */
+
+#ifndef NGX_HTTP_RDS_JSON_PROCESSOR_H
+#define NGX_HTTP_RDS_JSON_PROCESSOR_H
+
+
+#include "ngx_http_rds_json_filter_module.h"
+
+#include <ngx_core.h>
+#include <ngx_http.h>
+#include <nginx.h>
+
+
+ngx_int_t ngx_http_rds_json_process_header(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx);
+
+ngx_int_t ngx_http_rds_json_process_col(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx);
+
+ngx_int_t ngx_http_rds_json_process_row(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx);
+
+ngx_int_t ngx_http_rds_json_process_field(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx);
+
+ngx_int_t ngx_http_rds_json_process_more_field_data(ngx_http_request_t *r,
+ ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx);
+
+
+#endif /* NGX_HTTP_RDS_JSON_PROCESSOR_H */
+
@@ -4,8 +4,9 @@
#include "resty_dbd_stream.h"
#include "ngx_http_rds_json_util.h"
+
ngx_int_t
-ngx_http_rds_json_test_content_type(r)
+ngx_http_rds_json_test_content_type(ngx_http_request_t *r)
{
ngx_str_t *type;
@@ -1,7 +1,10 @@
#ifndef NGX_HTTP_RDS_JSON_UTIL_H
#define NGX_HTTP_RDS_JSON_UTIL_H
-ngx_int_t ngx_http_rds_json_test_content_type(r);
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+ngx_int_t ngx_http_rds_json_test_content_type(ngx_http_request_t *r);
#endif /* NGX_HTTP_RDS_JSON_UTIL_H */

0 comments on commit 079825d

Please sign in to comment.