Skip to content

Commit

Permalink
added set_quote_json_str command
Browse files Browse the repository at this point in the history
  • Loading branch information
liseen committed Jul 12, 2010
1 parent 70f4970 commit 56abbee
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README
Expand Up @@ -28,6 +28,16 @@ Synopsis
# now $foo is: 'hello\n\n\'\"\\'
}

location /json {
set $foo "hello\n\n'\"\\";
set_quote_json_str $foo $foo;

# OR in-place editing:
# set_quote_json_str $foo;

# now $foo is: "hello\n\n'\"\\"
}

location /baz {
set $foo "hello%20world";
set_unescape_uri $foo $foo;
Expand Down Expand Up @@ -86,6 +96,8 @@ Directives

set_quote_sql_str

set_quote_json_str

set_unescape_uri

set_escape_uri
Expand Down
4 changes: 2 additions & 2 deletions config
@@ -1,5 +1,5 @@
ngx_addon_name=ngx_http_set_misc_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_set_misc_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_quote_json.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_quote_json.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h"
CFLAGS="$CFLAGS -DNGX_HAVE_OPENSSL_MD5_H -DNGX_HAVE_OPENSSL_SHA1_H -DNDK_SET_VAR -DNDK_HASH -DNDK_SHA1 -DNDK_UPSTREAM_LIST -DNDK_MD5"
17 changes: 17 additions & 0 deletions src/ngx_http_set_misc_module.c
Expand Up @@ -7,6 +7,7 @@
#include "ngx_http_set_hashed_upstream.h"
#include "ngx_http_set_unescape_uri.h"
#include "ngx_http_set_quote_sql.h"
#include "ngx_http_set_quote_json.h"
#include "ngx_http_set_escape_uri.h"
#include "ngx_http_set_local_today.h"
#include "ngx_http_set_hash.h"
Expand Down Expand Up @@ -48,6 +49,13 @@ static ndk_set_var_t ngx_http_set_misc_quote_sql_str_filter = {
NULL
};

static ndk_set_var_t ngx_http_set_misc_quote_json_str_filter = {
NDK_SET_VAR_VALUE,
ngx_http_set_misc_quote_json_str,
1,
NULL
};

static ndk_set_var_t ngx_http_set_misc_encode_base32_filter = {
NDK_SET_VAR_VALUE,
ngx_http_set_misc_encode_base32,
Expand Down Expand Up @@ -114,6 +122,15 @@ static ngx_command_t ngx_http_set_misc_commands[] = {
0,
&ngx_http_set_misc_quote_sql_str_filter
},
{
ngx_string ("set_quote_json_str"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
ndk_set_var_value,
0,
0,
&ngx_http_set_misc_quote_json_str_filter
},
{
ngx_string ("set_if_empty"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
Expand Down
160 changes: 160 additions & 0 deletions src/ngx_http_set_quote_json.c
@@ -0,0 +1,160 @@
#define DDEBUG 0
#include "ddebug.h"
#include <ndk.h>
#include "ngx_http_set_quote_json.h"

ngx_int_t
ngx_http_set_misc_quote_json_str(ngx_http_request_t *r,
ngx_str_t *res, ngx_http_variable_value_t *v)
{
size_t len;
u_char *p;
size_t escape;

if (v->not_found || v->len == 0) {
res->data = (u_char *) "null";
res->len = sizeof("null") - 1;
return NGX_OK;
}

escape = ngx_http_set_misc_escape_json_str(NULL, v->data, v->len);

len = sizeof("''") - 1
+ v->len
+ escape;

p = ngx_palloc(r->pool, len);
if (p == NULL) {
return NGX_ERROR;
}

res->data = p;
res->len = len;

*p++ = '\"';

if (escape == 0) {
p = ngx_copy(p, v->data, v->len);

} else {
p = (u_char *) ngx_http_set_misc_escape_json_str(p, v->data, v->len);
}

*p++ = '\"';

if (p != res->data + res->len) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"set_quote_sql_str: buffer error");
return NGX_ERROR;
}

return NGX_OK;
}


uintptr_t
ngx_http_set_misc_escape_json_str(u_char *dst, u_char *src, size_t size)
{
ngx_uint_t n;

static u_char hex[] = "0123456789abcdef";

if (dst == NULL) {
/* find the number of characters to be escaped */

n = 0;

while (size) {
/* UTF-8 char has high bit of 1 */
if ((*src & 0x80) == 0) {
switch (*src) {
case '\r':
case '\n':
case '\\':
case '"':
case '\f':
case '\b':
case '\t':
n++;
break;
default:
if (*src < 32) {
n += sizeof("\\u00xx") - 2;
}

break;
}
}

src++;
size--;
}

return (uintptr_t) n;
}

while (size) {
if ((*src & 0x80) == 0) {
switch (*src) {
case '\r':
*dst++ = '\\';
*dst++ = 'r';
break;

case '\n':
*dst++ = '\\';
*dst++ = 'n';
break;

case '\\':
*dst++ = '\\';
*dst++ = '\\';
break;

case '"':
*dst++ = '\\';
*dst++ = '"';
break;

case '\f':
*dst++ = '\\';
*dst++ = 'f';
break;

case '\b':
*dst++ = '\\';
*dst++ = 'b';
break;

case '\t':
*dst++ = '\\';
*dst++ = 't';
break;

default:
if (*src < 32) { /* control chars */
*dst++ = '\\';
*dst++ = 'u';
*dst++ = '0';
*dst++ = '0';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0x0f];
} else {
*dst++ = *src;
}
break;
} /* switch */

src++;

} else {
*dst++ = *src++;
}

size--;
}

return (uintptr_t) dst;
}


17 changes: 17 additions & 0 deletions src/ngx_http_set_quote_json.h
@@ -0,0 +1,17 @@

#ifndef NGX_SET_QUOTE_JSON
#define NGX_SET_QUOTE_JSON


#include <ngx_core.h>
#include <ngx_config.h>
#include <ngx_http.h>


ngx_int_t ngx_http_set_misc_quote_json_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v);

uintptr_t ngx_http_set_misc_escape_json_str(u_char *dst, u_char *src, size_t size);


#endif /* NGX_SET_QUOTE_SQL */

0 comments on commit 56abbee

Please sign in to comment.