You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by Tin Zaw <tz...@yahoo.com> on 2010/01/08 04:34:00 UTC

Is INKMimeHdrFieldInsert() working as intended?

OK, it's me, sorry to bug you again.

I am following replace-header plugin example to implement a plugin that sets a cookie. 

Here is my call back.

  case INK_EVENT_HTTP_SEND_RESPONSE_HDR:
    replace_header(txnp, contp);

And below is replace_header(). But when I run it, everything seems fine according to logs, but I don't find an extra header (I intercept the TS's response using another HTTP, burpSuite, proxy running on desktop with the browser) setting cookie in the response.

Logs show:
[Jan  8 03:25:32.019] Server {3086841568} DIAG: (add-cookie) ENTER replace_header.
[Jan  8 03:25:32.019] Server {3086841568} DIAG: (add-cookie) EXIT replace_header.

So, am I doing something wrong in code, or forgetting to set something in config? Or could it be something deeper?

Many thanks in advance!



static void
replace_header(INKHttpTxn txnp, INKCont contp)
{
  INKMBuffer resp_bufp;
  INKMLoc resp_loc;
  INKMLoc field_loc;

  const char *cookie_content = "terms=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT";

  INKDebug("add-cookie", "ENTER replace_header.");

  if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc)) {
    INKDebug("add-cookie", "INKHttpTxnServerRespGet failed.");
    goto done;
  }

    /* create a new field in the header */
    field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc);
    if (!field_loc) {
      INKDebug("add-cookie", "INKMimeHdrFieldCreate returns null.");
      INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
      goto done;
    }

    /* set its name */
    if (INK_SUCCESS != INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, INK_MIME_FIELD_SET_COOKIE, INK_MIME_LEN_SET_COOKIE)) {
      INKDebug("add-cookie", "INKMimeHdrFieldNameSet failed.");
    }

    /* set its value */
    //INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1);
    if (INK_SUCCESS != INKMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, cookie_content, -1)) {
      INKDebug("add-cookie", "INKMimeHdrFieldValueStringInsert failed.");
    }

    /* insert it into the header */
    if (INK_SUCCESS != INKMimeHdrFieldInsert(resp_bufp, resp_loc, field_loc, -1)) {
      INKDebug("add-cookie", "INKMimeHdrFieldInsert failed.");
    }
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);

done:
  INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
  INKDebug("add-cookie", "EXIT replace_header.");
}

Re: Is INKMimeHdrFieldInsert() working as intended?

Posted by Steve Jiang <sj...@apache.org>.
INKHttpTxnServerRespGet retrieves the response from the origin server 
(from READ_RESPONSE_HDR).  TS builds a response to send to the client 
from the server response between READ_RESPONSE_HDR and 
SEND_RESPONSE_HDR, so changing the server response at the latter hook 
will be too late.

If you want to change what is sent to the client you can either:

a) set your hook at READ_RESPONSE_HDR_HOOK and change the response from 
INKHttpTxnServerRespGet

b) keep it at SEND_RESPONSE_HDR_HOOK and modify the response from 
INKHttpTxnClientRespGet

I hope that was a clear enough explanation.

--Steve

Tin Zaw wrote, on 1/7/10 7:34 PM:
> OK, it's me, sorry to bug you again.
> 
> I am following replace-header plugin example to implement a plugin that sets a cookie. 
> 
> Here is my call back.
> 
>   case INK_EVENT_HTTP_SEND_RESPONSE_HDR:
>     replace_header(txnp, contp);
> 
> And below is replace_header(). But when I run it, everything seems fine according to logs, but I don't find an extra header (I intercept the TS's response using another HTTP, burpSuite, proxy running on desktop with the browser) setting cookie in the response.
> 
> Logs show:
> [Jan  8 03:25:32.019] Server {3086841568} DIAG: (add-cookie) ENTER replace_header.
> [Jan  8 03:25:32.019] Server {3086841568} DIAG: (add-cookie) EXIT replace_header.
> 
> So, am I doing something wrong in code, or forgetting to set something in config? Or could it be something deeper?
> 
> Many thanks in advance!
> 
> 
> 
> static void
> replace_header(INKHttpTxn txnp, INKCont contp)
> {
>   INKMBuffer resp_bufp;
>   INKMLoc resp_loc;
>   INKMLoc field_loc;
> 
>   const char *cookie_content = "terms=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT";
> 
>   INKDebug("add-cookie", "ENTER replace_header.");
> 
>   if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc)) {
>     INKDebug("add-cookie", "INKHttpTxnServerRespGet failed.");
>     goto done;
>   }
> 
>     /* create a new field in the header */
>     field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc);
>     if (!field_loc) {
>       INKDebug("add-cookie", "INKMimeHdrFieldCreate returns null.");
>       INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
>       goto done;
>     }
> 
>     /* set its name */
>     if (INK_SUCCESS != INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, INK_MIME_FIELD_SET_COOKIE, INK_MIME_LEN_SET_COOKIE)) {
>       INKDebug("add-cookie", "INKMimeHdrFieldNameSet failed.");
>     }
> 
>     /* set its value */
>     //INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1);
>     if (INK_SUCCESS != INKMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, cookie_content, -1)) {
>       INKDebug("add-cookie", "INKMimeHdrFieldValueStringInsert failed.");
>     }
> 
>     /* insert it into the header */
>     if (INK_SUCCESS != INKMimeHdrFieldInsert(resp_bufp, resp_loc, field_loc, -1)) {
>       INKDebug("add-cookie", "INKMimeHdrFieldInsert failed.");
>     }
>     INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
>     INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
> 
> done:
>   INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
>   INKDebug("add-cookie", "EXIT replace_header.");
> }

Re: Is INKMimeHdrFieldInsert() working as intended?

Posted by Steve Jiang <sj...@apache.org>.
INKHttpTxnServerRespGet retrieves the response from the origin server 
(from READ_RESPONSE_HDR).  TS builds a response to send to the client 
from the server response between READ_RESPONSE_HDR and 
SEND_RESPONSE_HDR, so changing the server response at the latter hook 
will be too late.

If you want to change what is sent to the client you can either:

a) set your hook at READ_RESPONSE_HDR_HOOK and change the response from 
INKHttpTxnServerRespGet

b) keep it at SEND_RESPONSE_HDR_HOOK and modify the response from 
INKHttpTxnClientRespGet

I hope that was a clear enough explanation.

--Steve


Tin Zaw wrote, on 1/7/10 7:34 PM:
> OK, it's me, sorry to bug you again.
> 
> I am following replace-header plugin example to implement a plugin that sets a cookie. 
> 
> Here is my call back.
> 
>   case INK_EVENT_HTTP_SEND_RESPONSE_HDR:
>     replace_header(txnp, contp);
> 
> And below is replace_header(). But when I run it, everything seems fine according to logs, but I don't find an extra header (I intercept the TS's response using another HTTP, burpSuite, proxy running on desktop with the browser) setting cookie in the response.
> 
> Logs show:
> [Jan  8 03:25:32.019] Server {3086841568} DIAG: (add-cookie) ENTER replace_header.
> [Jan  8 03:25:32.019] Server {3086841568} DIAG: (add-cookie) EXIT replace_header.
> 
> So, am I doing something wrong in code, or forgetting to set something in config? Or could it be something deeper?
> 
> Many thanks in advance!
> 
> 
> 
> static void
> replace_header(INKHttpTxn txnp, INKCont contp)
> {
>   INKMBuffer resp_bufp;
>   INKMLoc resp_loc;
>   INKMLoc field_loc;
> 
>   const char *cookie_content = "terms=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT";
> 
>   INKDebug("add-cookie", "ENTER replace_header.");
> 
>   if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc)) {
>     INKDebug("add-cookie", "INKHttpTxnServerRespGet failed.");
>     goto done;
>   }
> 
>     /* create a new field in the header */
>     field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc);
>     if (!field_loc) {
>       INKDebug("add-cookie", "INKMimeHdrFieldCreate returns null.");
>       INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
>       goto done;
>     }
> 
>     /* set its name */
>     if (INK_SUCCESS != INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, INK_MIME_FIELD_SET_COOKIE, INK_MIME_LEN_SET_COOKIE)) {
>       INKDebug("add-cookie", "INKMimeHdrFieldNameSet failed.");
>     }
> 
>     /* set its value */
>     //INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1);
>     if (INK_SUCCESS != INKMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, cookie_content, -1)) {
>       INKDebug("add-cookie", "INKMimeHdrFieldValueStringInsert failed.");
>     }
> 
>     /* insert it into the header */
>     if (INK_SUCCESS != INKMimeHdrFieldInsert(resp_bufp, resp_loc, field_loc, -1)) {
>       INKDebug("add-cookie", "INKMimeHdrFieldInsert failed.");
>     }
>     INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
>     INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
> 
> done:
>   INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
>   INKDebug("add-cookie", "EXIT replace_header.");
> }