简体   繁体   English

如何获取setHeader覆盖现有的标头?

[英]How can I get setHeader to overwrite an existing header?

I'm working on a CF interface to the ChromeLogger extension ( shameless plug ), which uses HTTP headers to log data from a server side language to the Chrome console. 我正在使用ChromeLogger扩展程序( 无耻插件 )的CF接口,该接口使用HTTP标头将数据从服务器端语言记录到Chrome控制台。

Over the course of a request, the log() method can be called multiple times. 在请求过程中,可以多次调用log()方法。 For each call, I am writing the header in the format that ChromeLogger needs to display the data correctly. 对于每次通话,我都以ChromeLogger正确显示数据所需的格式编写标头。 In CF10, this works fine - each subsequent call to setHeader() overwrites the previously set header with the same name. 在CF10中,这可以正常工作-每次对setHeader()调用都将覆盖具有相同名称的先前设置的标头。 In CF9, however, I am seeing multiple headers with the same name. 但是,在CF9中,我看到了多个具有相同名称的标头。

This sample code demonstrates the issue: 此示例代码演示了该问题:

<cfscript>
pc = getPageContext().getResponse();
pc.setHeader( "test-header", "value 1" );
pc.setHeader( "test-header", "value 2" );
pc.setHeader( "test-header", "value 3" );
</cfscript>

In CF9, I see three headers named "test-header," each with their own value. 在CF9中,我看到三个名为“ test-header”的标头,每个标头都有自己的值。 In CF10, I see one header named "test-header," with a value of "value 3." 在CF10中,我看到一个名为“ test-header”的标头,其值为“ value 3”。 According to the Java docs for this method, the latter is correct (emphasis mine): 根据此方法的Java文档 ,后者是正确的(强调我的意思):

Sets a response header with the given name and value. 用给定的名称和值设置响应头。 If the header had already been set, the new value overwrites the previous one . 如果已设置标题,则新值将覆盖前一个值 The containsHeader method can be used to test for the presence of a header before setting its value. containsHeader方法可用于在设置标题值之前测试标题的存在。

Using the cfheader tag has the same results, presumably because it just wraps the setHeader() method. 使用cfheader标记具有相同的结果,大概是因为它只是包装setHeader()方法。

I'm aware that I can build up the header over the course of the request and then call setHeader() one time at the end via onRequestEnd() , but I'd like this component to be as self-contained as possible - the less the end user has to modify their code to implement it, the better. 我知道我可以在请求的过程中构建标头,然后通过onRequestEnd()最后一次调用setHeader() onRequestEnd() ,但是我希望此组件尽可能地独立-最终用户为实现该目标而修改其代码的次数越少越好。

Is there any other way in CF9 to overwrite an existing header? CF9中还有其他方法可以覆盖现有标头吗?

Looks like it could be a bug in ColdFusion 9. https://bugbase.adobe.com/index.cfm?event=bug&id=3041696 This bug was entered by Adam Cameron back in June 2010 for version 9.0 (as far as I can tell). 看起来它可能是ColdFusion 9中的错误。https: //bugbase.adobe.com/index.cfm ? event = bug & id =3041696这个错误是Adam Cameron在2010年6月针对9.0版输入的(据我所知)告诉)。 It is listed with a status of 'Deferred' and reason of 'NotEnoughTime'. 列出的状态为“延迟”,原因为“ NotEnoughTime”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM