Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ | ||
|