(52) Empty reply from server

Francis Daly francis at daoine.org
Thu Apr 28 07:09:55 UTC 2016

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.

> 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);

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.)

> 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);

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,

Francis Daly        francis at daoine.org

More information about the nginx mailing list