You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by oh...@cox.net on 2012/06/19 05:31:08 UTC

How to *add* a cookie in module?

Hi,

This is a followup to an earlier post/question, "How to access client certificate PEM and incoming request headers in a module?".

As before, I'm starting with mod_headers.c, and then tweaking it, partly experimenting with modules, and partly for a project that I'm working on (eventually).

What I'm trying to do now is:

- I have a test Tomcat instance that is being proxied by Apache, with a small JSP that just dumps the HTTP headers
- The Apache has my modified mod_headers.

I now need to add ("inject") an additional cookie to the incoming request so that when the JSP dumps the headers, it'll show whatever cookies originally existed, plus the one that my modified mod_headers module added.

As before, I'm adding my code to the beginning of "ap_headers_insert_output_filter()" in mod_headers.c:

    printf("\n\nIn ap_headers_insert_output_filter: About to call FIRST dump_request...\n");
    dump_request(r);
    printf("In ap_headers_insert_output_filter: Returned from calling FIRST dump_request...\n");

    printf("\n\nIn ap_headers_insert_output_filter: About to call apr_table_addn() to add 'Cookie' to r->headers_in\n");
    apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
    printf("In ap_headers_insert_output_filter: Returned from calling apr_table_addn()...\n");

    printf("\n\nIn ap_headers_insert_output_filter: About to call ap_headers_fixup()...\n");
    ap_headers_fixup(r);
    printf("In ap_headers_insert_output_filter: Returned from calling ap_headers_fixup()...\n");

    printf("\n\nIn ap_headers_insert_output_filter: About to call SECOND dump_request()...\n");
    dump_request(r);
    printf("In ap_headers_insert_output_filter: Returned from calling SECOND dump_request()...\n");

.I'm running Apache in single process mode, so I can see the printf output, and for that first call to dump_request(), I can see a "Cookie" header with a JSESSION cookie, and then in the second call to dump_request, I can see a "Cookie" header, but it has only my "MyCookie" cookie.  In other words, it looks like when the:

apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");

was executed, it overwrote the "Cookie" header in the r->headers_in table?

Can anyone tell me how I can *add* a cookie in my module?

Thanks,
Jim

P.S.  BTW, by the time the headers are displayed by the JSP on the proxied Tomcat, it shows ONLY the JSESSIONID cookie, i.e., it doesn't look like the MyCookie cookie got passed to Tomcat?

Re: How to *add* a cookie in module?

Posted by oh...@cox.net.
---- Sorin Manolache <so...@gmail.com> wrote: 
> On 2012-06-19 07:26, ohaya@cox.net wrote:
> > Hi,
> >
> > I spoke too soon :(....  The apr_table_mergen puts a comma (",") in between each cookie name/value pair, rather than a semicolon (";").
> >
> > So, does anyone know how I can accomplish the merge of the cookie headers, but with semicolons in between the name/value pairs?
> 
> AFAIK there's no direct way to add a cookie to the Cookie request 
> header. If you're sure that you already have a Cookie request header, 
> you can use the "RequestHeader edit" directive (RequestHeader edit 
> Cookie ".*" "$1; my_cookie"). However, afaik there's no directive 
> implementing something like if_present_edit_else_set.
> 
> You'll have to do it manually:
> 
> const char *cookie = apr_table_get(r->headers_in, "Cookie");
> if (cookie == NULL)
>     apr_table_set(r->headers_in, "Cookie", my_cookie);
> else
>     apr_table_setn(r->headers_in, "Cookie", apr_pstrcat(r->pool, cookie, 
> "; ", my_cookie, NULL));
> 
> --
> Sorin
> 
> 

Sorin,

Thanks!  I kind of figured that'd be the case.  I'll give that a try later today.

Jim

Re: How to *add* a cookie in module?

Posted by Sorin Manolache <so...@gmail.com>.
On 2012-06-19 07:26, ohaya@cox.net wrote:
> Hi,
>
> I spoke too soon :(....  The apr_table_mergen puts a comma (",") in between each cookie name/value pair, rather than a semicolon (";").
>
> So, does anyone know how I can accomplish the merge of the cookie headers, but with semicolons in between the name/value pairs?

AFAIK there's no direct way to add a cookie to the Cookie request 
header. If you're sure that you already have a Cookie request header, 
you can use the "RequestHeader edit" directive (RequestHeader edit 
Cookie ".*" "$1; my_cookie"). However, afaik there's no directive 
implementing something like if_present_edit_else_set.

You'll have to do it manually:

const char *cookie = apr_table_get(r->headers_in, "Cookie");
if (cookie == NULL)
    apr_table_set(r->headers_in, "Cookie", my_cookie);
else
    apr_table_setn(r->headers_in, "Cookie", apr_pstrcat(r->pool, cookie, 
"; ", my_cookie, NULL));

--
Sorin



Re: How to *add* a cookie in module?

Posted by oh...@cox.net.
---- ohaya@cox.net wrote: 
> 
> ---- ohaya@cox.net wrote: 
> > Hi,
> > 
> > This is a followup to an earlier post/question, "How to access client certificate PEM and incoming request headers in a module?".
> > 
> > As before, I'm starting with mod_headers.c, and then tweaking it, partly experimenting with modules, and partly for a project that I'm working on (eventually).
> > 
> > What I'm trying to do now is:
> > 
> > - I have a test Tomcat instance that is being proxied by Apache, with a small JSP that just dumps the HTTP headers
> > - The Apache has my modified mod_headers.
> > 
> > I now need to add ("inject") an additional cookie to the incoming request so that when the JSP dumps the headers, it'll show whatever cookies originally existed, plus the one that my modified mod_headers module added.
> > 
> > As before, I'm adding my code to the beginning of "ap_headers_insert_output_filter()" in mod_headers.c:
> > 
> >     printf("\n\nIn ap_headers_insert_output_filter: About to call FIRST dump_request...\n");
> >     dump_request(r);
> >     printf("In ap_headers_insert_output_filter: Returned from calling FIRST dump_request...\n");
> > 
> >     printf("\n\nIn ap_headers_insert_output_filter: About to call apr_table_addn() to add 'Cookie' to r->headers_in\n");
> >     apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
> >     printf("In ap_headers_insert_output_filter: Returned from calling apr_table_addn()...\n");
> > 
> >     printf("\n\nIn ap_headers_insert_output_filter: About to call ap_headers_fixup()...\n");
> >     ap_headers_fixup(r);
> >     printf("In ap_headers_insert_output_filter: Returned from calling ap_headers_fixup()...\n");
> > 
> >     printf("\n\nIn ap_headers_insert_output_filter: About to call SECOND dump_request()...\n");
> >     dump_request(r);
> >     printf("In ap_headers_insert_output_filter: Returned from calling SECOND dump_request()...\n");
> > 
> > .I'm running Apache in single process mode, so I can see the printf output, and for that first call to dump_request(), I can see a "Cookie" header with a JSESSION cookie, and then in the second call to dump_request, I can see a "Cookie" header, but it has only my "MyCookie" cookie.  In other words, it looks like when the:
> > 
> > apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
> > 
> > was executed, it overwrote the "Cookie" header in the r->headers_in table?
> > 
> > Can anyone tell me how I can *add* a cookie in my module?
> > 
> > Thanks,
> > Jim
> > 
> > P.S.  BTW, by the time the headers are displayed by the JSP on the proxied Tomcat, it shows ONLY the JSESSIONID cookie, i.e., it doesn't look like the MyCookie cookie got passed to Tomcat?
> 
> 
> Hi,
> 
> I think that I found one way to fix this.  Instead of:
> 
> apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
> 
> I did:
> 
> apr_table_mergen(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
> 
> and I now see the cookie that I added, both in the dump_request() output and in the JSP output.
> 
> Jim


Hi,

I spoke too soon :(....  The apr_table_mergen puts a comma (",") in between each cookie name/value pair, rather than a semicolon (";").  

So, does anyone know how I can accomplish the merge of the cookie headers, but with semicolons in between the name/value pairs?

Jim

Re: How to *add* a cookie in module?

Posted by oh...@cox.net.
---- ohaya@cox.net wrote: 
> Hi,
> 
> This is a followup to an earlier post/question, "How to access client certificate PEM and incoming request headers in a module?".
> 
> As before, I'm starting with mod_headers.c, and then tweaking it, partly experimenting with modules, and partly for a project that I'm working on (eventually).
> 
> What I'm trying to do now is:
> 
> - I have a test Tomcat instance that is being proxied by Apache, with a small JSP that just dumps the HTTP headers
> - The Apache has my modified mod_headers.
> 
> I now need to add ("inject") an additional cookie to the incoming request so that when the JSP dumps the headers, it'll show whatever cookies originally existed, plus the one that my modified mod_headers module added.
> 
> As before, I'm adding my code to the beginning of "ap_headers_insert_output_filter()" in mod_headers.c:
> 
>     printf("\n\nIn ap_headers_insert_output_filter: About to call FIRST dump_request...\n");
>     dump_request(r);
>     printf("In ap_headers_insert_output_filter: Returned from calling FIRST dump_request...\n");
> 
>     printf("\n\nIn ap_headers_insert_output_filter: About to call apr_table_addn() to add 'Cookie' to r->headers_in\n");
>     apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
>     printf("In ap_headers_insert_output_filter: Returned from calling apr_table_addn()...\n");
> 
>     printf("\n\nIn ap_headers_insert_output_filter: About to call ap_headers_fixup()...\n");
>     ap_headers_fixup(r);
>     printf("In ap_headers_insert_output_filter: Returned from calling ap_headers_fixup()...\n");
> 
>     printf("\n\nIn ap_headers_insert_output_filter: About to call SECOND dump_request()...\n");
>     dump_request(r);
>     printf("In ap_headers_insert_output_filter: Returned from calling SECOND dump_request()...\n");
> 
> .I'm running Apache in single process mode, so I can see the printf output, and for that first call to dump_request(), I can see a "Cookie" header with a JSESSION cookie, and then in the second call to dump_request, I can see a "Cookie" header, but it has only my "MyCookie" cookie.  In other words, it looks like when the:
> 
> apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");
> 
> was executed, it overwrote the "Cookie" header in the r->headers_in table?
> 
> Can anyone tell me how I can *add* a cookie in my module?
> 
> Thanks,
> Jim
> 
> P.S.  BTW, by the time the headers are displayed by the JSP on the proxied Tomcat, it shows ONLY the JSESSIONID cookie, i.e., it doesn't look like the MyCookie cookie got passed to Tomcat?


Hi,

I think that I found one way to fix this.  Instead of:

apr_table_addn(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");

I did:

apr_table_mergen(r->headers_in, "Cookie", "MyCookie=AAAAAAAAAAAAAAAABBBBBBBBBBB");

and I now see the cookie that I added, both in the dump_request() output and in the JSP output.

Jim