gpt4 book ai didi

HTTP Header Key 可以重复吗?

转载 作者:可可西里 更新时间:2023-11-01 16:25:08 34 4
gpt4 key购买 nike

在JAVA HttpUrlConnection 中,请求Header设置的主要逻辑代码如下:

public synchronized void set(String k, String v) {
for (int i = nkeys; --i >= 0;)
if (k.equalsIgnoreCase(keys[i])) {
values[i] = v;
return;
}
add(k, v);
}

验证了key应该是唯一的,key要和value保持一对一的映射关系。

相反,在Response模块的HeaderFields中,结构定义为Entry>。即键与值不保持一对一的映射关系。

这是为什么? HTTP协议(protocol)有没有相关的约定?

添加:在HttpClient4中,请求Header设置的主要逻辑代码如下:

 /**
* Replaces the first occurence of the header with the same name. If no header with
* the same name is found the given header is added to the end of the list.
*
* @param header the new header that should replace the first header with the same
* name if present in the list.
*/
public void updateHeader(final Header header) {
if (header == null) {
return;
}
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header current = this.headers.get(i);
if (current.getName().equalsIgnoreCase(header.getName())) {
this.headers.set(i, header);
return;
}
}
this.headers.add(header);
}

响应头

/**
* Gets all of the headers with the given name. The returned array
* maintains the relative order in which the headers were added.
*
* <p>Header name comparison is case insensitive.
*
* @param name the name of the header(s) to get
*
* @return an array of length >= 0
*/
public Header[] getHeaders(final String name) {
final List<Header> headersFound = new ArrayList<Header>();
// HTTPCORE-361 : we don't use the for-each syntax, i.e.
// for (Header header : headers)
// as that creates an Iterator that needs to be garbage-collected
for (int i = 0; i < this.headers.size(); i++) {
final Header header = this.headers.get(i);
if (header.getName().equalsIgnoreCase(name)) {
headersFound.add(header);
}
}

return headersFound.toArray(new Header[headersFound.size()]);
}

它们和HttpUrlConnection是一样的

最佳答案

Does the HTTP protocol has relevant agreement?

是的。 RFC 2616 Section 4.2 "Message Headers"说:

Multiple message-header fields with the same field-name MAY bepresent in a message if and only if the entire field-value for thatheader field is defined as a comma-separated list [i.e., #(values)].It MUST be possible to combine the multiple header fields into one"field-name: field-value" pair, without changing the semantics of themessage, by appending each subsequent field-value to the first, eachseparated by a comma. The order in which header fields with the samefield-name are received is therefore significant to theinterpretation of the combined field value, and thus a proxy MUST NOTchange the order of these field values when a message is forwarded.

这由 RFC 7230 Section 3.2.2 "Field Order" 进一步扩展:

A sender MUST NOT generate multiple header fields with the same fieldname in a message unless either the entire field value for thatheader field is defined as a comma-separated list [i.e., #(values)]or the header field is a well-known exception (as noted below).

A recipient MAY combine multiple header fields with the same fieldname into one "field-name: field-value" pair, without changing thesemantics of the message, by appending each subsequent field value tothe combined field value in order, separated by a comma. The orderin which header fields with the same field name are received istherefore significant to the interpretation of the combined fieldvalue; a proxy MUST NOT change the order of these field values whenforwarding a message.

关于HTTP Header Key 可以重复吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24502132/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com