# Help with reading response from upstream u-\>buffer

**URL:** <https://www.ruby-forum.com/t/help-with-reading-response-from-upstream-u-buffer/219269>\
**Category:** NGINX\
**Created:** [April 26, 2012, 2:22am UTC](https://www.ruby-forum.com/t/help-with-reading-response-from-upstream-u-buffer/219269 "2012-04-26T02:22:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 26, 2012, 2:22am UTC](https://www.ruby-forum.com/t/help-with-reading-response-from-upstream-u-buffer/219269/1 "2012-04-26T02:22:47Z")

</div>

Hi there,

In my module which uses upstream to talk to a backend, i do the  
following in the response processing method (u-\>process\_header)

{  
ngx\_http\_upstream\_t \* u = r-\>upstream;  
size\_t myResponseLength = (u-\>buffer.last - u-\>buffer.pos);  
size\_t origUpstreamResponseLength = (u-\>buffer.last -  
u-\>buffer.start);  
std::string myResponseString ((u\_char \*) u-\>buffer.pos,  
myResponseLength);

```
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "u->length is %d

```

and Original Upstream response length is %d and myResponseLength is  
%d", u-\>length, origUpstreamResponseLength, myResponseLength);

```
....

```

}

My actual response length from upstream is ~16kb, while i get  
u-\>length printed as 0, Original upstream response length and  
myResponseLength printed as 1448 bytes. I also see in nginx debug  
logs, “Transfer-Encoding: chunked”. Is the difference in length  
printed, because of this encoding? Is there a way to disable chunked  
transfer, so that i can get the entire response in one go (into my  
string), as shown in code? If not, what is the right method to read  
the entire response from upstream buffer into a temporary string (so  
that i can do further parsing)?

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 26, 2012, 3:01am UTC](https://www.ruby-forum.com/t/help-with-reading-response-from-upstream-u-buffer/219269/2 "2012-04-26T03:01:57Z")

</div>

Maxim had once mentioned about checking out memcache module for an  
example of non-buffered protocol handling, and i modeled my code  
around it. Is the behavior below because of buffered protocol (http)?

Thanks,  
Ashish
