An unexpected newline appears when use memc and echo_location_async, how to delete the newline?

I use the agenzh’s two modules

**
nginx configuration:

location ~ "^/property/([0-9]+)" {
    charset utf-8;
    default_type 'text/javascript';
    echo -n "$arg_callback({\"property_$1\":\"";
    echo_location_async /statistic/operator

“cmd=incr&key=property_$1&value=1”;
echo -n “")”;
}

# GET /statistic/operator?cmd=incr&key=property_23214&value=2
# GET /statistic/operator?cmd=decr&key=property_23214&value=1
location /statistic/operator {
    internal;
    set $memc_cmd $arg_cmd;
    set $memc_key $arg_key;
    set $memc_value $arg_value;
    memc_pass statistics_server_pool;
    memc_cmds_allowed incr decr set get delete add;
    add_header X-Memc-Flags 'Statistic Crement';
}

Request URL:

http://domain.net/property/23458?hi=231231&callback=himycallback201203455

The Unexpect incorrect Output: (Current Configuration)

himycallback201203455({“property_23458”:24
})

The new line after 24 is unexpected. and how can l delete the new line ?

I expected the correct Output: ( how to configuration the nginx ? )

himycallback201203455({“property_23458”:24})

Hello!

On Fri, Jun 15, 2012 at 9:49 PM, Xiangong Y. [email protected]
wrote:

The Unexpect incorrect Output: (Current Configuration)

himycallback201203455({“property_23458”:24
})
The new line after 24 is unexpected. and how can l delete the new line ?

Please ensure that the value you inserted into memcached does not have
the trailing new line by using telnet to access your memcached, like
this:

$ telnet <your-memcached-host> <your-memcached-port>
get property_23458

If the original value in your memcached server contains a trailing
newline, then neither ngx_memc nor ngx_echo will automatically strip
it for you.

Also, to do JSONP padding in nginx, you’re recommended to use the
ngx_xss module which is more efficient and more secure:

https://github.com/agentzh/xss-nginx-module

Best regards,
-agentzh

Hello!

On Sat, Jun 16, 2012 at 10:08 AM, agentzh [email protected] wrote:

Please ensure that the value you inserted into memcached does not have
the trailing new line by using telnet to access your memcached, like
this:

Sorry, I was reading too fast. For the memcached commands other than
“get”, ngx_memc will return the raw memcached responses, and in your
case, for the “incr” command, the response is ended by a CR LF
sequence.

If it bothers you, then you’re recommended to use ngx_lua with
lua-resty-memcached instead, for example:

the following line is not required if the ngx_openresty bundle is

used.
lua_package_path “/path/to/lua-resty-memcached/lib/?.lua;;”;

server {
listen 8080;

location ~ "^/property/([0-9]+)" {
    charset utf-8;
    default_type 'application/json';

    xss_callback_arg callback;
    xss_get on;

    content_by_lua '
        local memcached = require "resty.memcached"
        local memc = memcached:new()

        memc:set_timeout(500) -- 500 ms

        local ok, err = memc:connect("127.0.0.1", 11211)
        if not ok then
            ngx.log(ngx.ERR, "failed to connect to memcached: ", 

err)
return ngx.exit(500)
end

        local key = "property_" .. ngx.var[1]
        local newval, err = memc:incr(key, 1)
        if not newval then
            ngx.log(ngx.ERR, "failed to incr the key in memcached: 

", err)
return ngx.exit(500)
end

        -- the connection pool capacity is set to 1024 connections
        memc:set_keepalive(0, 1024)

        ngx.print([[{"]], key, [[":"]], newval, [["}]])
    ';
}

}

Then we can get:

$ curl -i 

localhost:8080/property/23458?callback=himycallback201203455
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sat, 16 Jun 2012 02:31:57 GMT
Content-Type: application/x-javascript; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive

himycallback201203455({"property_23458":"30"});

And when the callback argument is not given:

$ curl -i localhost:8080/property/23458
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sat, 16 Jun 2012 02:32:51 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive

{"property_23458":"31"}

The example above utilizes the following components:

http://wiki.nginx.org/HttpLuaModule
https://github.com/agentzh/lua-resty-memcached
https://github.com/agentzh/xss-nginx-module

If you’re too lazy to download and install all these dependencies, you
can try out the ngx_openresty bundle instead:

http://openresty.org/#Download

Best regards,
-agentzh

Thank you very much, agentzh!
I’ll immediately try the xss-nginx-module module in your guide.

2012/6/16 agentzh [email protected]