Accessing HTTP request headers in nginx module

Hi,

I am writing a small nginx module that needs to get/parse HTTP request
header and depending on it’s value, needs to be do something.

ex. curl -X POST -H “OPERATION: add” http://localhost:80/calc
to add 2 numbers

ex. curl -X POST -H “OPERATION: divide” http://localhost:80/calc
to divide 2 numbers

etc…

I know in Mongoose web server’s callback routine, you can easily access
custom HTTP header with an API like

const char *value = mg_get_header(connection, “OPERATION”)

I could not find such an API in nginx. How can I get this functionality
in nginx module in handler routine ? Any help would be great

Thanks

In nginx, you have the http_ embedded variable in the core
module
http://nginx.org/en/docs/http/ngx_http_core_module.html#variablesto
access HTTP headers.
You can build the logic the way you want (through
maphttp://nginx.org/en/docs/http/ngx_http_map_module.htmlor
directives from the
rewrite module
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
).

Using all that you can redirect to specific URL or backend with whatever
arguments you wish to do the work for you.

It is the UNIX way of doing things to separate components in individual
processes, so I suggest you delegate the job to a third-party one which
will listen for incoming requests.
I would do that this way and keep nginx as close as possible from the
lean
genuine version to ensure updates are straightforward.

My 2 cents,

B. R.

Hello!

On Fri, Mar 28, 2014 at 01:06:00AM +0100, Mapper Uno wrote:

indicates that these are not “custom” headers. With reference to my
above example, how can I access my custom header “OPERATION” in module
handler ?

Please make sure to read not only the first sentence. Note the
“Also there are other variables” in the same paragraph. The
$http_* variables provide access to all headers, including any
custom ones. And it is documented as:

$http_name
arbitrary request header field; the last part of a variable name
is the field name converted to lower case with dashes replaced by
underscores

Therefore, you may either use the $http_operation variable to
access the header you are looking for. Or you may take a look at
the source code to find out how it’s implemented. Take a look at
the src/http/ngx_http_variables.c, functions
ngx_http_variable_unknown_header_in() and
ngx_http_variable_unknown_header() (first one says the header
should be searched in r->headers_in.headers list, second one does
actual search).


Maxim D.
http://nginx.org/

Thanks for reply. However, I am still not clear how to access the
“custom” headers in module handler. Pls see my comments inline.

B.R. wrote in post #1141281:

In nginx, you have the http_ embedded variable in the core
module
http://nginx.org/en/docs/http/ngx_http_core_module.html#variablesto
access HTTP headers.
I can see that variables can be accessed by http_, however
the link says “names matching the Apache Server variables”, which to me
indicates that these are not “custom” headers. With reference to my
above example, how can I access my custom header “OPERATION” in module
handler ?

You can build the logic the way you want (through
maphttp://nginx.org/en/docs/http/ngx_http_map_module.htmlor
directives from the
rewrite module
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
).

Using all that you can redirect to specific URL or backend with whatever
arguments you wish to do the work for you.

Again, here I am not interested in forwarding any URL to any backend
server based on header. I simply to access and possibly iterate through
list of HTTP headers in module handler where I have reference to
ngx_http_request_t *r

It is the UNIX way of doing things to separate components in individual
processes, so I suggest you delegate the job to a third-party one which
will listen for incoming requests.
I would do that this way and keep nginx as close as possible from the
lean
genuine version to ensure updates are straightforward.

My 2 cents,

B. R.

I could finally get hold of all the http header fields. A nice link that
describes how to access the headers

http://wiki.nginx.org/HeadersManagement

Thanks for all the replies.

Mapper Uno wrote in post #1141373:

Thanks Maxim for your reply. Since I am newbie, please excuse my
questions. I am still unable to retrieve the variable.

All I have in the handler routine is: ngx_http_request_t *r
I can see that r->headers_in.headers is a list, but then
when you say $http_operation, it is confusing me.

Could you please explain

Maxim D. wrote in post #1141328:

Hello!

On Fri, Mar 28, 2014 at 01:06:00AM +0100, Mapper Uno wrote:

indicates that these are not “custom” headers. With reference to my
above example, how can I access my custom header “OPERATION” in module
handler ?

Please make sure to read not only the first sentence. Note the
“Also there are other variables” in the same paragraph. The
$http_* variables provide access to all headers, including any
custom ones. And it is documented as:

$http_name
arbitrary request header field; the last part of a variable name
is the field name converted to lower case with dashes replaced by
underscores

Therefore, you may either use the $http_operation variable to
access the header you are looking for. Or you may take a look at
the source code to find out how it’s implemented. Take a look at
the src/http/ngx_http_variables.c, functions
ngx_http_variable_unknown_header_in() and
ngx_http_variable_unknown_header() (first one says the header
should be searched in r->headers_in.headers list, second one does
actual search).


Maxim D.
http://nginx.org/

Thanks Maxim for your reply. Since I am newbie, please excuse my
questions. I am still unable to retrieve the variable.

All I have in the handler routine is: ngx_http_request_t *r
I can see that r->headers_in.headers is a list, but then
when you say $http_operation, it is confusing me.

Could you please explain

Maxim D. wrote in post #1141328:

Hello!

On Fri, Mar 28, 2014 at 01:06:00AM +0100, Mapper Uno wrote:

indicates that these are not “custom” headers. With reference to my
above example, how can I access my custom header “OPERATION” in module
handler ?

Please make sure to read not only the first sentence. Note the
“Also there are other variables” in the same paragraph. The
$http_* variables provide access to all headers, including any
custom ones. And it is documented as:

$http_name
arbitrary request header field; the last part of a variable name
is the field name converted to lower case with dashes replaced by
underscores

Therefore, you may either use the $http_operation variable to
access the header you are looking for. Or you may take a look at
the source code to find out how it’s implemented. Take a look at
the src/http/ngx_http_variables.c, functions
ngx_http_variable_unknown_header_in() and
ngx_http_variable_unknown_header() (first one says the header
should be searched in r->headers_in.headers list, second one does
actual search).


Maxim D.
http://nginx.org/