在读 werkzeug 的源码,我有个疑惑?

2019-07-03 16:10:59 +08:00
 hzwjz

werkzeug

    # Warn if the final value of the cookie is less than the limit. If the
    # cookie is too large, then it may be silently ignored, which can be quite
    # hard to debug.
    cookie_size = len(rv)

    if max_size and cookie_size > max_size:
        value_size = len(value)
        warnings.warn(
            'The "{key}" cookie is too large: the value was {value_size} bytes'
            " but the header required {extra_size} extra bytes. The final size"
            " was {cookie_size} bytes but the limit is {max_size} bytes."
            " Browsers may silently ignore cookies larger than this.".format(
                key=key,
                value_size=value_size,
                extra_size=cookie_size - value_size,
                cookie_size=cookie_size,
                max_size=max_size,
            ),
            stacklevel=2,
        )

    return rv

说一下自己对注释的理解,如果 cookie 的最终值小于限定的值,则产生警告信息。如果 cookie 太大了,则忽略,因此可能难以进行调试。

但是可以看到,后续的if语句是判断 max_size 为 True,并且 cookie_size > max_size 时,会发出警告。

我在 werkzeug 的 discord 里面问过这个问题,也没人回答,不知道是不是自己理解得不对还是怎么的?还请 v 友们多多指教。

2254 次点击
所在节点    Python
9 条回复
est
2019-07-03 16:22:19 +08:00
- Warn if the final value of the cookie is less than the limit
+ Warn if the final value of the cookie is larger than the limit
est
2019-07-03 16:22:50 +08:00
- cookie is too large, then it may be silently ignored
+ cookie is too large, then it may be silently ignored by the browser
hzwjz
2019-07-03 16:34:14 +08:00
@est #2 意思就是如果 cookie_size 大于 max_size 时,发出警告,但是在浏览器上,对于 cookie 的 size 太大,则忽略。看来是自己粗心了,漏掉了 warn 函数里面的信息了。
hzwjz
2019-07-03 16:34:14 +08:00
@est #2 意思就是如果 cookie_size 大于 max_size 时,发出警告,但是在浏览器上,对于 cookie 的 size 太大,则忽略。看来是自己粗心了,漏掉了 warn 函数里面的信息了。
ryd994
2019-07-03 16:39:25 +08:00
# Warn if the final value of the cookie is less than the limit. If the
# cookie is too large, then it may be silently ignored, which can be quite
# hard to debug.

我的理解是,如果 cookie 太大,那可能没有错误信息就挂掉了。这就很难调试。所以如果 cookie 太大,就改用以下错误信息而不显示 cookie 内容。这样至少你能看见一部分错误信息。

may 此除是有可能的意思,而不是允许、会发生的意思
hzwjz
2019-07-03 17:51:06 +08:00
@ryd994 #5 它的注释跟代码判断逻辑不合呢,注释是 less than,而代码的逻辑是 larger than,这就是我疑惑的地方。

综上,注释的首行应该改为 1L 的 Warn if the final value of the cookie is larger than the limit,同时我也这么认为。而说到“ If the
cookie is too large, then it may be silently ignored, which can be quite hard to debug.”指的应该是在浏览器中可能忽略掉这个太大的 cookie。但是警告信息只能在服务端看到吧。
zkqiang
2019-07-03 18:19:19 +08:00
可以去提 PR 了
mcfog
2019-07-03 18:35:28 +08:00
hzwjz
2019-07-03 22:14:24 +08:00
@zkqiang #7 像是#1 的 v 友 @est 已经 PR 了:satisfied:
est
2019-07-05 00:00:47 +08:00
@hzwjz github.com/pallets/werkzeug/pull/1598 是的。已经 merge 了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/579690

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX