Extract individual cookie from multiple cookies

Maxim Dounin mdounin at mdounin.ru
Sat Mar 5 12:54:30 MSK 2011


Hello!

On Fri, Mar 04, 2011 at 08:25:46PM +0530, shirish deshpande wrote:

> Hi,
> 
> Am developing an nginx module. I get multiple cookies in a http
> request. I need to extract individual cookie from the multiple cookies
> that I get. How do I achive this?
> 
> Is there an api to extract individual cookie?

ngx_http_parse_multi_header_lines()

See ngx_http_userid_module.c for examples.

> 
> >From what I understand:
> 
> + The cookies are present in request->headers_in as array
> 
>   cookies == request->headers_in->cookies

Yes (incorrect syntax, but doesn't really matter).

> + The length of this array is 1, even when we get multiple cookies.

No.  This won't be 1 as long as you get multiple headers from 
client.

> + Further investigation reveals that the element is a hash.
> 
>   cookies->elts is a hash.
> 
>   key   is cookie
>   value is all cookies
>   eg:
>   key -> cookie
>   value -> "cookie1=value1;cookie2=value2;cookie3=value3"

This is header and it's value.

> + So will I need to parse the cookie string in "value" to extract
>   individual cookies?
> 
>   Or is there a api that will help in extract individual cookies.

The ngx_http_parse_multi_header_lines() is a api to extract 
individual cookies.

> + I cannot use ngx_http_parse_multi_header_lines() as I may not always
>   know the name of the cookie (the cookie name may vary depending on
>   id).

If you don't know id, you don't need an api to extract individual 
cookies.  Guess you want to iterate all cookies, right?

There is no such api, though iterating over r->headers_in.cookies 
and parsing values should do the trick.  Refer to 
ngx_http_parse_multi_header_lines() implementation for an example.

Maxim Dounin



More information about the nginx mailing list