(52) Empty reply from server

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.

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. [email protected]

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.

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. [email protected]

not able to read header value after setting…

any help most welcome

On Fri, Apr 15, 2016 at 1:17 PM, Pankaj Chaudhary
[email protected]

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. [email protected]

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

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 .

https://www.nginx.com/resources/wiki/start/topics/examples/headers_management/
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. [email protected]

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

}

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

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 .

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. [email protected]

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. [email protected]

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!

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 [email protected]

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

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 [email protected]

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

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. [email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs