# (52) Empty reply from server

**URL:** <https://www.ruby-forum.com/t/52-empty-reply-from-server/244459>\
**Category:** NGINX\
**Created:** [April 14, 2016, 12:04pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459 "2016-04-14T12:04:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [April 14, 2016, 12:04pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/1 "2016-04-14T12:04:56Z")

</div>

Hi All,

i am trying to set value in header with the help of below code snippet .

ngx\_table\_elt\_t \*h1;  
h1 = ngx\_list\_push(&r-\>headers\_out.headers);  
h1-\>hash = 1;  
ngx\_str\_set(&h1-\>key, “http\_user\_agent”);  
ngx\_str\_set(&h1-\>value, “user\_agent\_value”);

if i try to retrieve the same value then getting response “(52) Empty  
reply  
from server”

ngx\_http\_core\_loc\_conf\_t \*clcf;  
ngx\_str\_t \*type;  
ngx\_uint\_t hash;  
ngx\_str\_t name = ngx\_string(“http\_user\_agent”);  
clcf = ngx\_http\_get\_module\_loc\_conf(r, ngx\_http\_core\_module);  
hash = ngx\_hash\_key\_lc(name.data, name.len);  
type = ngx\_hash\_find(&clcf-\>types\_hash, hash, name.data, name.len);

Can anyone help me to fix this problem.

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 14, 2016, 6:56pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/2 "2016-04-14T18:56:12Z")

</div>

On Thu, Apr 14, 2016 at 03:34:40PM +0530, Pankaj Chaudhary wrote:

Hi there,

> i am trying to set value in header with the help of below code snippet .

Web searches for how to write nginx modules tend to point towards  
“Emiller’s Guide” or the “Nginx Development Kit”.

You may find good background reading there (allowing for any changes  
to the nginx internals since they were written) to help you create a  
“hello world” module.

After that, you’ll have a skeleton into which you can add your specific  
pieces, knowing that the basic starting point does work for you.

> ngx\_table\_elt\_t \*h1;  
> h1 = ngx\_list\_push(&r-\>headers\_out.headers);  
> h1-\>hash = 1;  
> ngx\_str\_set(&h1-\>key, “http\_user\_agent”);  
> ngx\_str\_set(&h1-\>value, “user\_agent\_value”);
> 
> if i try to retrieve the same value then getting response “(52) Empty reply  
> from server”

There, it looks like you wrote something to a “headers\_out.headers”  
data structure.

> ngx\_http\_core\_loc\_conf\_t \*clcf;  
> ngx\_str\_t \*type;  
> ngx\_uint\_t hash;  
> ngx\_str\_t name = ngx\_string(“http\_user\_agent”);  
> clcf = ngx\_http\_get\_module\_loc\_conf(r, ngx\_http\_core\_module);  
> hash = ngx\_hash\_key\_lc(name.data, name.len);  
> type = ngx\_hash\_find(&clcf-\>types\_hash, hash, name.data, name.len);

And there it looks like you tried to read something from a “types\_hash”  
data structure. So the “type” variable is probably NULL, and your code  
should do something sensible with that value.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 14, 2016, 8:07pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/3 "2016-04-14T20:07:28Z")

</div>

Hi Francis,

Thank you for your comments.  
Actually I have requirement to add value to header and then read same  
value  
like cookies values,URL.  
Is any API which I can use for the same.  
Please suggest.

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 14, 2016, 11:14pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/4 "2016-04-14T23:14:24Z")

</div>

On Thu, Apr 14, 2016 at 11:36:52PM +0530, Pankaj Chaudhary wrote:

Hi there,

> Actually I have requirement to add value to header and then read same value  
> like cookies values,URL.  
> Is any API which I can use for the same.

The referenced guides are probably the best starting points.

It sounds like you want to read headers\_in and write headers\_out;  
Emillers guide seems to have examples of that in sections 4.1 and 3.1.3,  
respectively.

Note that just using the words in this mail, your requirements can  
possibly be met in nginx.conf, not needing a new module at all. I  
suspect  
that your full requirements are much more detailed.

It is worth making sure that you are very clear on what you are trying  
to do; otherwise you won’t be able to know when you’ve done it.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 18, 2016, 12:56pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/5 "2016-04-18T12:56:07Z")

</div>

not able to read header value after setting…

any help most welcome

On Fri, Apr 15, 2016 at 1:17 PM, Pankaj Chaudhary  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 19, 2016, 1:35am UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/6 "2016-04-19T01:35:32Z")

</div>

On Mon, Apr 18, 2016 at 04:25:24PM +0530, Pankaj Chaudhary wrote:

Hi there,

> not able to read header value after setting…
> 
> any help most welcome

You have your nginx “hello world” module, yes?

You can show your config and your “curl -i” request and response with  
your module’s output, yes?

Are you able to modify your module to read a particular request header  
and write the value to the response body?

Are you able to modify your module to add a particular response header  
with a particular value?

At what particular point does your implementation fail?

If you can describe that, perhaps someone here will be able to offer  
some more direct help.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 15, 2016, 9:48am UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/7 "2016-04-15T09:48:14Z")

</div>

Hi Francis,

thank you!

i have checked Emillers guide but i am not able to use the same for my  
problem.

I have my product in c and currently running on Apache and now i have to  
move this product on nginx.  
In Apache apr\_table\_set() can use set value to header and later that  
value  
can read from header by calling  
apr\_table\_get().  
I am achieving the same behavior apr\_table\_set() with ngx\_list\_push()  
call  
but not able to find like apr\_table\_get() in nginx.

Reagrds,  
Pankaj

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 19, 2016, 7:43pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/8 "2016-04-19T19:43:24Z")

</div>

On Tue, Apr 19, 2016 at 03:51:17PM +0530, Pankaj Chaudhary wrote:

Hi there,

> i am trying to search for one header with the specified name
> 
> i am not able to get header value .

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

lists four ways to try this. You are using something like #2: “Quick  
search”.

But your code snippet does not match exactly the types and return values  
of that example. I don’t know if that is something worth investigating.

Also, you may be able to make use of #3: “Blazing fast” instead.

> ngx\_str\_t val = ngx\_string(“http\_cookie”);

And one final thing - I suspect that the request header “Cookie” will  
be presented in the header\_in structure under the name “cookie”, not  
the name “http\_cookie”. Possibly just making that change will cause your  
code to find the value you want.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 19, 2016, 12:21pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/9 "2016-04-19T12:21:47Z")

</div>

Hi all,

i am trying to search for one header with the specified name

i am not able to get header value .

below my code snippet.

ngx\_http\_core\_main\_conf\_t \*clcf;  
ngx\_str\_t \*type;  
ngx\_uint\_t hash;  
ngx\_str\_t val = ngx\_string(“http\_cookie”);

clcf = ngx\_http\_get\_module\_main\_conf(r, ngx\_http\_core\_module);

hash = ngx\_hash\_key\_lc(val.data, val.len);

type = ngx\_hash\_find(&clcf-\>headers\_in\_hash, hash, val.data, val.len);

if (type != NULL) {

// type is not null but type-\>data value is null

}

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 20, 2016, 9:24am UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/10 "2016-04-20T09:24:44Z")

</div>

> > But your code snippet does not match exactly the types and return values  
> > of that example. I don’t know if that is something worth investigating.

I have tried to change match exactly the types and return values  
of that example. but below is response curl: (52) Empty reply from  
server

> > Also, you may be able to make use of #3: “Blazing fast” instead.

but same response

> > And one final thing - I suspect that the request header “Cookie” will  
> > be presented in the header\_in structure under the name “cookie”, not  
> > the name “http\_cookie”. Possibly just making that change will cause your  
> > code to find the value you want.

Tried to use the same now i am getting response only one word “cookie”  
I have used below example to set custom header

ngx\_int\_tset\_custom\_header\_in\_headers\_out(ngx\_http\_request\_t \*r,  
ngx\_str\_t \*key, ngx\_str\_t \*value) {  
ngx\_table\_elt\_t \*h;

```
/* All we have to do is just to allocate the header... */
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
    return NGX_ERROR;
}

/* ... setup the header key ... */
h->key = *key;

/* ... and the value. */
h->value = *value;

/* Mark the header as not deleted. */
h->hash = 1;

return NGX_OK;}

```

below is “curl -i” response

HTTP/1.1 200 OK  
Server: nginx/1.9.12  
Date: Wed, 20 Apr 2016 15:25:20 GMT  
Content-Type: text/plain  
Content-Length: 14  
Connection: keep-alive  
cookie: thisitestvalue  
user\_agent: 10.9 user  
get-rese: Cookie

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 21, 2016, 9:35am UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/11 "2016-04-21T09:35:12Z")

</div>

Hi,

Thank You!

yes i am able to write successfully but while reading i am not getting  
the  
my written value in header.

for Example from my source code:-I am writing “thisitestvalue” value  
against key “Set-Cookie” but if trying to read value against key  
“Set-Cookie”  
not getting .

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 20, 2016, 9:39pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/12 "2016-04-20T21:39:02Z")

</div>

On Wed, Apr 20, 2016 at 12:54:13PM +0530, Pankaj Chaudhary wrote:

Hi there,

> > > But your code snippet does not match exactly the types and return values  
> > > of that example. I don’t know if that is something worth investigating.
> 
> I have tried to change match exactly the types and return values  
> of that example. but below is response curl: (52) Empty reply from server

> ngx\_int\_tset\_custom\_header\_in\_headers\_out(ngx\_http\_request\_t \*r,  
> ngx\_str\_t \*key, ngx\_str\_t \*value) {

The code snippet you show seems to relate to writing your own http  
headers.

> get-rese: Cookie  
> The “curl” output you show seems to show your own http headers being  
> successfully written.

So it looks like what you have shown, is working.

That’s good.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 22, 2016, 8:52pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/13 "2016-04-22T20:52:21Z")

</div>

On Thu, Apr 21, 2016 at 01:04:40PM +0530, Pankaj Chaudhary wrote:

Hi there,

> yes i am able to write successfully but while reading i am not getting the  
> my written value in header.
> 
> for Example from my source code:-I am writing “thisitestvalue” value  
> against key “Set-Cookie” but if trying to read value against key “Set-Cookie”  
> not getting .

The curl output you showed included

cookie: thisitestvalue

And did not include “Set-Cookie:”. Perhaps you were testing different  
code then.

And if you write in to the headers\_out structure, you would need to  
read from the headers\_out structure for what you just wrote (except you  
already know what you just wrote, so I’m not sure why you would want to  
read it again).

If you have copied the example code, and do not get the desired output,  
then presumably either you copied it wrong, or the example code is  
wrong.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 26, 2016, 1:22pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/14 "2016-04-26T13:22:51Z")

</div>

Hi Francis,

thank you for your input!

I have requirement to create own cookie based on input and wirte the  
that  
cookie in header.  
whenever i need that i can read from header and use it.

for example:-

I have created my own cookie “thissomevalue” worte in header and later  
the  
same read from header.

Please check my code and let me know why i am not able to read the value  
from header.

Below code snippet to set header value in request header:-

ngx\_table\_elt\_t _cookie;  
cookie = ngx\_list\_push(&r-\>headers\_in.headers);  
cookie-\>lowcase\_key = (u\_char_) “cookie”;  
ngx\_str\_set(&cookie-\>key, “Cookie”);  
ngx\_str\_set(&cookie-\>value, “somevalue”);  
cookie-\>hash = ngx\_crc32\_long(cookie-\>lowcase\_key, cookie-\>key.len);

Below code snippet to read set value from header:-

ngx\_http\_core\_main\_conf\_t \*clcf;  
ngx\_str\_t \*type;  
ngx\_uint\_t key;  
ngx\_str\_t val = ngx\_string(“cookie”);  
clcf = ngx\_http\_get\_module\_main\_conf(r, ngx\_http\_core\_module);  
key= ngx\_hash\_key\_lc(val.data, val.len);  
type = ngx\_hash\_find(&clcf-\>headers\_in\_hash, key, val.data, val.len);

if (type != NULL)  
{  
ngx\_table\_elt\_t _test\_val;  
test\_val= ngx\_list\_push(&r-\>headers\_out.headers);  
test\_val-\>lowcase\_key = (u\_char_) “test\_val”;  
ngx\_str\_set(&test\_val-\>key, “Test\_Val”);  
ngx\_str\_set(&test\_val-\>value, type-\>data);  
test\_val-\>hash = ngx\_crc32\_long(test\_val-\>lowcase\_key,  
test\_val-\>key.len);  
}

curl response:-Test\_val was accepting “somevalue”  
HTTP/1.1 200 OK  
Server: nginx/1.9.12  
Date: Tue, 26 Apr 2016 19:13:40 GMT  
Content-Type: text/plain  
Content-Length: 34  
Connection: keep-alive  
Test\_Val: Cookie

Hello, This is Nginx test Module!

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 27, 2016, 9:57am UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/15 "2016-04-27T09:57:10Z")

</div>

Hi Bart,

thank you for response.

I have referred the /root/Downloads/nginx-1.9.14/src/http/v2 module  
code.

Below is that sample code.  
static ngx\_int\_t  
ngx\_http\_v2\_parse\_authority(ngx\_http\_request\_t \*r, ngx\_http\_v2\_header\_t  
\*header)  
{  
ngx\_table\_elt\_t \*h;  
ngx\_http\_header\_t \*hh;  
ngx\_http\_core\_main\_conf\_t \*cmcf;

```
static ngx_str_t host = ngx_string("host");

h = ngx_list_push(&r->headers_in.headers);
if (h == NULL) {
    return NGX_ERROR;
}

h->hash = ngx_hash_key(host.data, host.len);

h->key.len = host.len;
h->key.data = host.data;

h->value.len = header->value.len;
h->value.data = header->value.data;

h->lowcase_key = host.data;

cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
                   h->lowcase_key, h->key.len);

```

Best Regards,  
Pankaj

On Tue, Apr 26, 2016 at 6:23 PM, Valentin V. Bartenev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 26, 2016, 2:53pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/16 "2016-04-26T14:53:58Z")

</div>

On Tuesday 26 April 2016 16:52:22 Pankaj Chaudhary wrote:

> I have created my own cookie “thissomevalue” worte in header and later the  
> same read from header.

You shouldn’t do that (I believe even in Apache) because it’s an  
expensive  
operation in comparison to just storing the value in local variable.

> Please check my code and let me know why i am not able to read the value  
> from header.

Because you write your value in one structure, but try to find it in  
another.

> Below code snippet to set header value in request header:-
> 
> ngx\_table\_elt\_t \*cookie;  
> cookie = ngx\_list\_push(&r-\>headers\_in.headers);

Here you have inserted your value into header list  
(r-\>headers\_in.headers).

> ngx\_uint\_t key;  
> ngx\_str\_t val = ngx\_string(“cookie”);  
> clcf = ngx\_http\_get\_module\_main\_conf(r, ngx\_http\_core\_module);  
> key= ngx\_hash\_key\_lc(val.data, val.len);  
> type = ngx\_hash\_find(&clcf-\>headers\_in\_hash, key, val.data, val.len);

Here you are trying to find your value in the static input headers hash  
(clcf-\>headers\_in\_hash), that is created in  
ngx\_http\_init\_headers\_in\_hash()  
on configuration stage.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 27, 2016, 6:21pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/17 "2016-04-27T18:21:53Z")

</div>

Hi,  
Thank you,  
I got this point.  
But in my case i need to set cookie value in header later read from  
header  
the same value.  
Is any example which i can follow for my requirement, can you suggest  
please.

Regards,  
Pankaj

On Wed, Apr 27, 2016 at 9:08 PM, Valentin V. Bartenev [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 27, 2016, 5:38pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/18 "2016-04-27T17:38:39Z")

</div>

On Wednesday 27 April 2016 13:26:47 Pankaj Chaudhary wrote:

> {
> 
> ```
> cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
> 
> hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
> h->lowcase_key, h->key.len);
> 
> ```

[..]

This code does what it supposed to do. It adds a parsed “host” header  
to the input headers list, and then it tries to find and call  
appropriate  
handler (the ngx\_http\_process\_host() function in this particular case).

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [April 28, 2016, 9:10am UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/19 "2016-04-28T09:10:22Z")

</div>

On Tue, Apr 26, 2016 at 04:52:22PM +0530, Pankaj Chaudhary wrote:

Hi there,

> I have requirement to create own cookie based on input and wirte the that  
> cookie in header.  
> whenever i need that i can read from header and use it.

I confess that I do not understand what that requirement actually  
is. There are headers in the request from the client to nginx; there  
may be header-like things in whatever nginx does when communicating  
with an upstream; there may be header-like things in the response from  
that upstream; and there are headers in the response from nginx to the  
client. And it is not clear to me what your module architecture is.

But that’s ok; I don’t have to understand it. You want to do some  
specific  
things in an nginx module.

> ngx\_table\_elt\_t \*cookie;  
> cookie = ngx\_list\_push(&r-\>headers\_in.headers);

You are writing into the headers\_in structure. Normally, that is what  
came from the client, so I guess you must have a plan for why you are  
doing that.

(If I wanted to test “can I read from headers\_in”, I would probably add  
a  
“MyHeader” to my curl request, and look for that in my code.)

> ngx\_uint\_t key;  
> ngx\_str\_t val = ngx\_string(“cookie”);  
> clcf = ngx\_http\_get\_module\_main\_conf(r, ngx\_http\_core\_module);  
> key= ngx\_hash\_key\_lc(val.data, val.len);  
> type = ngx\_hash\_find(&clcf-\>headers\_in\_hash, key, val.data, val.len);

As mentioned elsewhere, you are not reading from the headers\_in  
structure. So there’s a reasonable chance that what you wrote into one  
structure will not be found in another one.

Also, you are treating the output of ngx\_hash\_find() as a ngx\_str\_t\*.

The example code I see treats is as a ngx\_http\_header\_t\*.

Is that an important difference?

(As in: is that why you print the header name, but not the header  
value? Possibly not, if the original request did not have any Cookie  
header; but test rather than assume, if the documentation is not clear  
to you.)

> if (type != NULL)  
> {

The example code I see has separate handling for “header is unknown or  
is not hashed yet”, and “header is hashed but not cached yet”. You  
seem to skip testing for the second possibility here.

> ngx\_table\_elt\_t _test\_val;  
> test\_val= ngx\_list\_push(&r-\>headers\_out.headers);  
> test\_val-\>lowcase\_key = (u\_char_) “test\_val”;  
> ngx\_str\_set(&test\_val-\>key, “Test\_Val”);  
> ngx\_str\_set(&test\_val-\>value, type-\>data);

I’d also suggest that if you are not sure what value your content has,  
use the simplest possible method to print it somewhere you can read  
it. Usually, that means logging, since that should not have a complex  
data structure.

> test\_val-\>hash = ngx\_crc32\_long(test\_val-\>lowcase\_key, test\_val-\>key.len);  
> }

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Pankaj\_Chaudhary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@Pankaj\_Chaudhary](https://www.ruby-forum.com/u/Pankaj_Chaudhary)\
**Post date:** [May 4, 2016, 3:47pm UTC](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459/20 "2016-05-04T15:47:21Z")

</div>

On Wednesday 27 April 2016 21:51:33 Pankaj Chaudhary wrote:

> Hi,  
> Thank you,  
> I got this point.  
> But in my case i need to set cookie value in header later read from header  
> the same value.  
> Is any example which i can follow for my requirement, can you suggest  
> please.

I believe there are no examples for your requirement, since it’s not  
something that is expected to be done with headers.

As I’ve already wrote before, saving and accessing the same value from  
headers structure is a bad idea. I don’t understand the reason why  
someone ever wants to access his own value such a strange way.

Maybe if you clarify what the purpose of your module is, a better  
solution  
will be found.

wbr, Valentin V. Bartenev

[Next page](https://www.ruby-forum.com/t/52-empty-reply-from-server/244459.md?page=2)
