# An unexpected newline appears when use memc and echo\_location\_async, how to delete the newline?

**URL:** <https://www.ruby-forum.com/t/an-unexpected-newline-appears-when-use-memc-and-echo-location-async-how-to-delete-the-newline/220661>\
**Category:** NGINX\
**Created:** [June 15, 2012, 3:50pm UTC](https://www.ruby-forum.com/t/an-unexpected-newline-appears-when-use-memc-and-echo-location-async-how-to-delete-the-newline/220661 "2012-06-15T15:50:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 15, 2012, 3:50pm UTC](https://www.ruby-forum.com/t/an-unexpected-newline-appears-when-use-memc-and-echo-location-async-how-to-delete-the-newline/220661/1 "2012-06-15T15:50:41Z")

</div>

I use the agenzh’s two modules

> **[Memc | NGINX](https://www.nginx.com/resources/wiki/modules/memc/)**
>
> The Memc module is an An extended version of the standard memcached module that supports set, add, delete, and many more memcached commands.

> **[HTTP Echo Module | NGINX](https://www.nginx.com/resources/wiki/modules/echo/)**
>
> The HTTP Echo module brings "echo", "sleep", "time", "exec" and more shell-style goodies to your NGINX configuration files.

\*\*  
_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](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})

---

<div class="post-metadata">

**Author:** ![Xiangong\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/94ad74/32.png) [@Xiangong\_Y](https://www.ruby-forum.com/u/Xiangong_Y)\
**Post date:** [June 16, 2012, 4:08am UTC](https://www.ruby-forum.com/t/an-unexpected-newline-appears-when-use-memc-and-echo-location-async-how-to-delete-the-newline/220661/2 "2012-06-16T04:08:36Z")

</div>

Hello!

On Fri, Jun 15, 2012 at 9:49 PM, Xiangong Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![Xiangong\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/94ad74/32.png) [@Xiangong\_Y](https://www.ruby-forum.com/u/Xiangong_Y)\
**Post date:** [June 16, 2012, 4:34am UTC](https://www.ruby-forum.com/t/an-unexpected-newline-appears-when-use-memc-and-echo-location-async-how-to-delete-the-newline/220661/3 "2012-06-16T04:34:55Z")

</div>

Hello!

On Sat, Jun 16, 2012 at 10:08 AM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Xiangong\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/94ad74/32.png) [@Xiangong\_Y](https://www.ruby-forum.com/u/Xiangong_Y)\
**Post date:** [June 16, 2012, 5:36am UTC](https://www.ruby-forum.com/t/an-unexpected-newline-appears-when-use-memc-and-echo-location-async-how-to-delete-the-newline/220661/4 "2012-06-16T05:36:52Z")

</div>

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

2012/6/16 agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
