0

我正在使用Apache2::Cookie(即带有 mod_perl 的 Apache2)来解析 cookie。

my %cookies = Apache2::Cookie->fetch;
do_something($cookies{"cookie1"});

此代码已在生产环境中运行多年,没有任何问题。我刚刚了解到具有特定格式的 cookie 会导致抛出异常Expected token not present。有问题的 cookie 由客户端 JavaScript 生成:

document.cookie = "val=a,b"

Apache2::Cookie 似乎不喜欢逗号。

我可以用 捕获这个错误eval,但是 cookie 检索是在代码中的很多地方完成的(是的,它可能已经被分解了,但坦率地说,代码是如此简单,没有必要)。无论如何,它现在就在那里,我必须追踪并捕获我没有设置且不需要的这个 cookie 的异常。

有没有比重构几十个调用更简单的方法来摆脱这个异常Apache2::Cookie->fetch 通过重新定义Apache2::Cookie::fetch,或者通过设置一个全局标志libapreq以不吐出这个(我找不到任何东西),或者我错过了其他一些好主意。

4

2 回答 2

0

(yes, it could have been factored out, but frankly the code is so simple there was no need).

I would take this opportunity to fix this oversight, instead of making another

If you insist, you could learn something from CGI::Cookie

 sub fetch {
     my $class = shift;
     my $raw_cookie = get_raw_cookie(@_) or return;
     return $class->parse($raw_cookie);
 }

 sub get_raw_cookie {
   my $r = shift;
   $r ||= eval { $MOD_PERL == 2                    ? 
                   Apache2::RequestUtil->request() :
                   Apache->request } if $MOD_PERL;

   return $r->headers_in->{'Cookie'} if $r;

   die "Run $r->subprocess_env; before calling fetch()" 
     if $MOD_PERL and !exists $ENV{REQUEST_METHOD};

   return $ENV{HTTP_COOKIE} || $ENV{COOKIE};
 }
于 2011-11-15T11:01:28.797 回答
0

我遇到了同样的问题,你可以在这里找到解决方案:

我的 Apache 日志中出现“预期的令牌不存在”错误

于 2012-05-31T04:21:39.520 回答