我可以将内容长度标头注入Chrome吗? [英] Can I inject a Content-Length header into Chrome?

查看:61
本文介绍了我可以将内容长度标头注入Chrome吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作我的第一个Chrome扩展程序,这已经是一次颇具学习的体验。我几乎完成了,但这最后一块是杀了我。



我知道谷歌不希望你使用Content-Length(如这里),但我正在使用身份验证的API在POST时需要它。 此扩展说它可以做到,但我觉得我写的东西应该可以工作。这是最近在Chrome的新版本中被封锁的吗?



这些是我发送的头文件:

  {
ReplaceHeaders:true,
rh-Authentication:< my auth token>,
rh-Timestamp:< ;时间戳>,
rh-Content-Length:body.length
}

这是将新的标题替换为rh标题的部分(除了rh-之外,所有内容都是rh-),如果这样做更合理的话。):

  chrome.webRequest.onBeforeSendHeaders.addListener(function(data){
newHeaders = false;

_.find(data (header.name ===ReplaceHeaders)&&(header.value)){
_.find(data.requestHeaders,function( h){
if(h.name.substring(0,3)===rh-){
if(newHeaders === false){newHeaders = [];}
newHeaders.push({
name:h.name.substring(3,h.name.length),
value:h.value
});
}
});
return;
}
});

if(!! newHeaders){
//更新标题
return {requestHeaders:newHeaders};
}
},{urls:[< all_urls>]},[requestHeaders,blocking]);

当我发出GET请求时,我没有rh-ContentLength标题,它起作用没问题(我可以看到SendHeaders,onHeadersReceived等)。一切都被替换,并且响应如预期。但是,当我使用rh-Content-Length发出POST请求时,我只能看到onSendHeaders,之后什么也没有。



我的onSendHeaders对于GET和POST都有新格式标题。

解决方案

好的,所以我在凌晨3点发布。在得到一些急需的睡眠后,我今天早上醒来,并与一位朋友谈论此事。他指出,这个错误实际上是在我的ajax调用中。我解决了这个问题,现在我可以从我的扩展中发送Content-Length。上面的代码确实有效。



关于我之前发布的代码的一些注记,以供任何人稍后搜索:


  • 我使用下划线循环访问数组

  • 这将剥离所有其他标题,并仅添加以rh-开头的标题。
  • 您必须在onBeforeSendHeaders.addListener()中使用[requestHeaders,blocking]作为最后一个参数。

  • 在您的manifest.json权限中,确保您有webRequest和webRequestBlocking

    $ b

    尽管未经过测试,在其他标题,Chrome说它不会通过。以下是谷歌表示它在onBeforeSendHeaders中没有提供的头文件列表(如 here ):
    $ b


    • 授权

    • 缓存控制

    • 连接

    • 内容长度

    • 主持人

    • If-Modified-Since

    • If-None-Match

    • If-Range

    • 部分数据


    • 代理授权

    • 代理连接

    • 传输编码



    希望这有助于您。


    I'm trying to make my first Chrome Extension and it has been quite a learning experience. I'm almost done, but this last piece is killing me.

    I know Google doesn't want you to use Content-Length (as stated here), but I'm working with an API who's authentication requires it when I POST. This extension says it can do it but I feel like what I wrote should work. Was this recently blocked in a new version of Chrome?

    These are the headers I am sending:

    {
        "ReplaceHeaders": true,
        "rh-Authentication": "<my auth token>",
        "rh-Timestamp": "<timestamp>",
        "rh-Content-Length": body.length
    }
    

    This is the section that replaces the "rh" headers with the new headers (everything with "rh-", except for the "rh-" if that even makes sense.):

    chrome.webRequest.onBeforeSendHeaders.addListener(function(data) {
        newHeaders = false;
    
        _.find(data.requestHeaders, function(header) {
            if ((header.name === "ReplaceHeaders") && (header.value)) {
                _.find(data.requestHeaders, function(h) {
                    if (h.name.substring(0, 3) === "rh-") {
                        if (newHeaders === false) { newHeaders = []; }
                        newHeaders.push({
                            name: h.name.substring(3, h.name.length),
                            value: h.value
                        });
                    }
                });
                return;
            }
        });
    
        if (!!newHeaders) {
            // Update headers
            return {requestHeaders: newHeaders};
        }
    }, { urls: ["<all_urls>"] }, ["requestHeaders", "blocking"]);
    

    When I make a GET request I don't have the "rh-ContentLength" header and it works fine (I can see onSendHeaders, onHeadersReceived, etc.). Everything is replaced and the response is as expected. But when I make a POST request with "rh-Content-Length" I only see onSendHeaders and nothing after that.

    My onSendHeaders for both GET and POST have the newly formatted headers.

    解决方案

    Ok, so I posted that at 3:00 AM. After getting some much needed sleep, I woke up this morning and talked to a friend about it. He pointed out that the mistake was actually in my ajax call. I fixed that and now I can send Content-Length from my extension. The above code does work.

    A few notes for the code I posted above for anybody searching for this later:

    • I'm using underscore to loop through the arrays
    • This strips all other headers and only adds the headers starting with "rh-". You may or may not want that.
    • You have to have ["requestHeaders", "blocking"] as your last argument in onBeforeSendHeaders.addListener()
    • In your manifest.json permissions make sure you have "webRequest" and "webRequestBlocking"

    Although not tested, this should work on other headers that Chrome says it won't pass. Here is a list of the headers Google says it doesn't provide in onBeforeSendHeaders (as documented here):

    • Authorization
    • Cache-Control
    • Connection
    • Content-Length
    • Host
    • If-Modified-Since
    • If-None-Match
    • If-Range
    • Partial-Data
    • Pragma
    • Proxy-Authorization
    • Proxy-Connection
    • Transfer-Encoding

    Hope this helps.

    这篇关于我可以将内容长度标头注入Chrome吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆